Implementation:Kornia Kornia Conftest
| Knowledge Sources | |
|---|---|
| Domains | Infrastructure, Testing |
| Last Updated | 2026-02-09 15:00 GMT |
Overview
This is the root pytest configuration file (conftest.py) for the Kornia project, providing test fixtures, parametrization helpers, CLI options, and session setup for device/dtype/optimizer-aware testing.
Description
The conftest.py file at the repository root configures the entire Kornia test suite. It dynamically discovers available hardware (CPU, CUDA, TPU/XLA, MPS) via get_test_devices() and defines test dtypes (bfloat16, float16, float32, float64) via get_test_dtypes(). It also detects available torch.compile backends. The module provides pytest fixtures (device, dtype, torch_optimizer) that are parametrized via CLI options or environment variables (KORNIA_TEST_DEVICE, KORNIA_TEST_DTYPE, KORNIA_TEST_OPTIMIZER, KORNIA_TEST_RUNSLOW). The pytest_generate_tests hook generates cross-product parametrizations of device/dtype/optimizer combinations while respecting a DEVICE_DTYPE_BLACKLIST. Session startup warms up torch.compile, sets up a weights cache directory, and configures HuggingFace caching. The module also provides test data loading via a data fixture that downloads from kornia/data_test, and an add_doctest_deps fixture that injects numpy, torch, and kornia into doctest namespaces. A detailed pytest_report_header function outputs CPU/GPU info, dependency versions, and available optimizers.
Usage
This file is automatically loaded by pytest. It is not imported directly but configures the test environment. Use the CLI options or environment variables to control test parametrization.
Code Reference
Source Location
- Repository: Kornia
- File: conftest.py
- Lines: 1-367
Key Fixtures
@pytest.fixture()
def device(device_name) -> torch.device: ...
@pytest.fixture()
def dtype(dtype_name) -> torch.dtype: ...
@pytest.fixture()
def torch_optimizer(optimizer_backend): ...
@pytest.fixture(autouse=True)
def add_doctest_deps(doctest_namespace): ...
@pytest.fixture(scope="session")
def data(request): ...
Key Functions
def get_test_devices() -> dict[str, torch.device]: ...
def get_test_dtypes() -> dict[str, torch.dtype]: ...
def pytest_generate_tests(metafunc) -> None: ...
def pytest_collection_modifyitems(config, items): ...
def pytest_addoption(parser): ...
def pytest_sessionstart(session): ...
def pytest_report_header(config): ...
I/O Contract
CLI Options / Environment Variables
| Option | Env Variable | Default | Description |
|---|---|---|---|
| --device | KORNIA_TEST_DEVICE | cpu | Device(s) to test on. Use all for all available. |
| --dtype | KORNIA_TEST_DTYPE | float32 | Data type(s) to test. Use all for all. |
| --optimizer | KORNIA_TEST_OPTIMIZER | inductor | Optimizer backend(s). Use all for all. |
| --runslow | KORNIA_TEST_RUNSLOW | false | Whether to run tests marked as slow. |
Test Data URLs
| Key | Description |
|---|---|
| loftr_homo | LoFTR outdoor and homography test data. |
| loftr_fund | LoFTR indoor and fundamental matrix test data. |
| adalam_idxs | AdaLAM test indices. |
| disk_outdoor | DISK outdoor keypoint test data. |
| dexined | DexiNed edge detection model weights. |
Session Setup
The pytest_sessionstart hook performs the following:
- Warms up torch.compile with dummy functions/modules to reduce first-run latency.
- Creates the weights/ cache directory.
- Configures torch.hub and HF_HOME to use the cache directory.
Usage Examples
# Run tests on CPU with float32
# pytest --device cpu --dtype float32
# Run tests on all available devices and dtypes
# pytest --device all --dtype all
# Run slow tests
# pytest --runslow
# Using environment variables (for CI)
# KORNIA_TEST_DEVICE=cuda KORNIA_TEST_DTYPE=float32 pytest
# Using the data fixture in a test
@pytest.mark.parametrize("data", ["loftr_homo"], indirect=True)
def test_loftr(data):
assert "keypoints0" in data