Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

17.6 KB
Raw Blame
# LICENSE HEADER MANAGED BY add-license-header
#
# Copyright 2018 Kornia Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from __future__ import annotations

import types
from pathlib import Path

import pytest
import torch

from conftest import _is_subprocess_isolated_test, skip_half_precision_on_cuda

from testing.base import DYNAMO_UNAVAILABLE_REASON, BaseTester, dynamo_is_available

pytest_plugins = ["pytester"]
# Deliberately *not* marked device_agnostic. These are the runner's own regression tests, and
# several of them guard accelerator-only entry points -- `test_isolated_skip_is_reported_with_
# quiet_addopts` exists to protect `pixi run -e cuda test-cuda-half`. Marking the module would
# deselect exactly the tests that command needs. They take no device fixture, so they run once
# per session on any device matrix. `test_half_precision_isolation_probe` below is the one
# exception, and is marked deliberately -- see its docstring.

_skip_fixture_fn = getattr(skip_half_precision_on_cuda, "__wrapped__", skip_half_precision_on_cuda)


def test_cudnn_tf32_follows_tf32_option(request):
    assert torch.backends.cudnn.allow_tf32 is request.config.getoption("--tf32")


@pytest.fixture(autouse=True)
def _neutral_runner_env(monkeypatch):
    """Keep the ambient run's runner flags out of the pytester subprocesses.

    ``runpytest_subprocess`` inherits the environment, and both options below are
    ``action="store_true"`` with their default read from an environment variable -- so once the
    variable is exported there is no command line that turns the option back off. A developer
    following TESTING.md and exporting ``KORNIA_TEST_RUN_DEVICE_AGNOSTIC=true`` for an
    accelerator-only run would otherwise fail the very tests that document the escape hatch.
    Each test below passes the flags it needs explicitly.
    """
    for var in ("KORNIA_TEST_ISOLATE_HALF", "KORNIA_TEST_RUN_DEVICE_AGNOSTIC"):
        monkeypatch.delenv(var, raising=False)


class _LastBitsVaryingBackward(torch.autograd.Function):
    """Double the input; successive backward passes differ by 1e-12 relative, as an atomic-add kernel can."""

    calls = 0

    @staticmethod
    def forward(ctx, x):
        return 2 * x

    @staticmethod
    def backward(ctx, grad):
        _LastBitsVaryingBackward.calls += 1
        return grad * (2 + 2e-12 * (_LastBitsVaryingBackward.calls % 2))


def test_base_tester_gradcheck_tolerates_last_bits_nondeterminism_on_cuda_only(device):
    # CUDA_GRADCHECK_NONDET_TOL: on CUDA two backward passes may round differently; CPU keeps nondet_tol=0.
    inputs = (torch.rand(3, device=device),)
    assert BaseTester.gradcheck(_LastBitsVaryingBackward.apply, inputs, raise_exception=False) == (
        device.type == "cuda"
    )


@pytest.mark.device_agnostic
def test_half_precision_isolation_probe(device, dtype):
    """Skip under half precision so the isolation runner has a child-side skip to report.

    Support test for ``test_isolated_skip_is_reported_with_quiet_addopts`` below, which needs a
    node that the parent collects and the subprocess skips. It lives here, next to its only
    caller, rather than borrowing an unrelated suite's internal skip: under that coupling a dtype
    fix somewhere else would break this file. The ``device_agnostic`` mark is load-bearing for the
    same runner test -- it is what makes the parent and the child disagree about selection -- and
    honest on its own terms, since the body never touches the device. Outside that runner test
    this is a no-op that passes.
    """
    if dtype in (torch.float16, torch.bfloat16):
        pytest.skip("probe: skipped by design under half precision")


def _make_mock_item(params: dict | None, isolate: bool = True):
    config = types.SimpleNamespace(
        getoption=lambda opt, default=False: isolate if opt == "--isolate-half-precision" else default
    )
    callspec = types.SimpleNamespace(params=params) if params is not None else None
    return types.SimpleNamespace(config=config, callspec=callspec)


def _make_mock_request(
    fixturenames: list[str],
    fixture_values: dict[str, object],
    params: dict | None = None,
    isolate: bool = False,
):
    node = _make_mock_item(params, isolate=isolate)
    config = node.config

    def getfixturevalue(name: str):
        if name in fixture_values:
            return fixture_values[name]
        raise pytest.FixtureLookupError(name, node)

    return types.SimpleNamespace(
        fixturenames=fixturenames,
        getfixturevalue=getfixturevalue,
        config=config,
        node=node,
    )


class TestIsSubprocessIsolatedTest:
    def test_reproduce_issue_4126_local_half_dtype(self):
        """Issue #4126: local half_dtype on CUDA must be isolated even when parent job is float32."""
        item = _make_mock_item(
            {"device_name": "cuda", "dtype_name": "float32", "half_dtype": torch.float16},
            isolate=True,
        )
        assert _is_subprocess_isolated_test(item) is True

    @pytest.mark.parametrize("target_dtype", [torch.float16, torch.bfloat16])
    def test_local_custom_param_names(self, target_dtype):
        """Tests using custom parameter names (desc_dtype, image_dtype, etc.) must be isolated."""
        item = _make_mock_item(
            {"device_name": "cuda", "desc_dtype": target_dtype},
            isolate=True,
        )
        assert _is_subprocess_isolated_test(item) is True

    @pytest.mark.parametrize("target_dtype_name", ["float16", "bfloat16"])
    def test_global_dtype_fixture_preserved(self, target_dtype_name):
        """Existing global fixture tests using dtype_name must continue to be isolated."""
        item = _make_mock_item(
            {"device_name": "cuda", "dtype_name": target_dtype_name},
            isolate=True,
        )
        assert _is_subprocess_isolated_test(item) is True

    def test_negative_controls(self, monkeypatch):
        """Float32, CPU devices, unflagged runs, and subprocesses must NOT be isolated."""
        # Standard float32 test
        item_f32 = _make_mock_item({"device_name": "cuda", "dtype_name": "float32", "desc_dtype": torch.float32})
        assert _is_subprocess_isolated_test(item_f32) is False

        # CPU device with half precision
        item_cpu = _make_mock_item({"device_name": "cpu", "half_dtype": torch.float16})
        assert _is_subprocess_isolated_test(item_cpu) is False

        # Flag not set (--isolate-half-precision=False)
        item_no_flag = _make_mock_item({"device_name": "cuda", "half_dtype": torch.float16}, isolate=False)
        assert _is_subprocess_isolated_test(item_no_flag) is False

        # Inside subprocess (KORNIA_TEST_IN_SUBPROCESS=1)
        item_in_sub = _make_mock_item({"device_name": "cuda", "half_dtype": torch.float16}, isolate=True)
        monkeypatch.setenv("KORNIA_TEST_IN_SUBPROCESS", "1")
        assert _is_subprocess_isolated_test(item_in_sub) is False

        # No callspec
        item_no_callspec = _make_mock_item(None, isolate=True)
        assert _is_subprocess_isolated_test(item_no_callspec) is False


class TestSkipHalfPrecisionOnCuda:
    def test_local_half_dtype_is_skipped_without_isolation_flag(self):
        """Issue #4126: local half_dtype on CUDA without --isolate-half-precision must be skipped."""
        req = _make_mock_request(
            fixturenames=["device"],
            fixture_values={"device": torch.device("cuda:0")},
            params={"device_name": "cuda", "half_dtype": torch.float16},
            isolate=False,
        )
        with pytest.raises(pytest.skip.Exception, match="float16 on CUDA: skipped by default"):
            _skip_fixture_fn(req)

    def test_global_dtype_fixture_is_skipped_without_isolation_flag(self):
        """Global dtype fixture test on CUDA without isolation must be skipped."""
        req = _make_mock_request(
            fixturenames=["device", "dtype"],
            fixture_values={"device": torch.device("cuda:0"), "dtype": torch.bfloat16},
            params={"device_name": "cuda", "dtype_name": "bfloat16"},
            isolate=False,
        )
        with pytest.raises(pytest.skip.Exception, match="bfloat16 on CUDA: skipped by default"):
            _skip_fixture_fn(req)

    def test_not_skipped_on_cpu_or_float32(self):
        """Float32 and CPU tests must not be skipped."""
        # CPU test with half_dtype
        req_cpu = _make_mock_request(
            fixturenames=["device"],
            fixture_values={"device": torch.device("cpu")},
            params={"device_name": "cpu", "half_dtype": torch.float16},
            isolate=False,
        )
        assert _skip_fixture_fn(req_cpu) is None

        # CUDA test with float32
        req_f32 = _make_mock_request(
            fixturenames=["device", "dtype"],
            fixture_values={"device": torch.device("cuda:0"), "dtype": torch.float32},
            params={"device_name": "cuda", "dtype_name": "float32"},
            isolate=False,
        )
        assert _skip_fixture_fn(req_f32) is None


class TestIntegrationLocalHalfPrecision:
    @pytest.mark.parametrize("teardown_error", [False, True])
    def test_isolated_item_tears_down_previous_module(self, pytester, monkeypatch, teardown_error):
        """An isolated item must release parent fixtures before the next module runs (#4149)."""
        # Load the real isolation plugin in both the parent and its child. The test requests
        # device_name rather than device, so the protocol runs without requiring a CUDA kernel.
        monkeypatch.setenv("PYTEST_PLUGINS", "conftest")
        events = pytester.path / "events.txt"
        pytester.makepyfile(
            test_first=f"""
            import os
            from pathlib import Path
            import pytest

            events = Path({str(events)!r})
            child = bool(os.environ.get("KORNIA_TEST_IN_SUBPROCESS"))

            def record(event):
                with events.open("a") as stream:
                    stream.write(event + "\\n")

            @pytest.fixture(scope="module", autouse=True)
            def module_fixture():
                record("setup child" if child else "setup parent")
                yield
                record("teardown child" if child else "teardown parent")
                if not child and {teardown_error!r}:
                    raise RuntimeError("parent finalizer failed")

            def test_normal():
                record("normal")

            @pytest.fixture
            def isolated_fixture():
                assert child, "isolated fixtures must not run in the parent"

            def test_isolated(device_name, dtype_name, isolated_fixture):
                assert child
                record("isolated")
            """,
            test_next=f"""
            from pathlib import Path

            def test_next_module():
                assert Path({str(events)!r}).read_text().splitlines() == [
                    "setup parent", "normal", "setup child", "isolated",
                    "teardown child", "teardown parent",
                ]
            """,
        )
        result = pytester.runpytest_subprocess(
            "test_first.py",
            "test_next.py",
            "--device=cuda",
            "--dtype=float16",
            "--isolate-half-precision",
            "-q",
        )
        result.assert_outcomes(passed=3, errors=int(teardown_error))
        if teardown_error:
            result.stdout.fnmatch_lines(["*ERROR at teardown of test_isolated*", "*parent finalizer failed*"])

    def test_local_half_dtype_lifecycle_with_pytester(self, pytester):
        """Integration test: verify real pytest execution with local half_dtype."""
        test_file = pytester.makepyfile(
            """
            import pytest
            import torch

            @pytest.mark.parametrize("half_dtype", [torch.float16])
            def test_dummy(device, half_dtype):
                assert True
            """
        )

        # 1. CUDA + --dtype=float32 without --isolate-half-precision -> Must be SKIPPED
        result_cuda = pytester.runpytest_subprocess(
            "-p",
            "conftest",
            str(test_file),
            "-o",
            "testpaths=.",
            "--device=cuda",
            "--dtype=float32",
        )
        result_cuda.assert_outcomes(skipped=1)

        # 2. CPU + --dtype=float32 -> Must PASS
        result_cpu = pytester.runpytest_subprocess(
            "-p",
            "conftest",
            str(test_file),
            "-o",
            "testpaths=.",
            "--device=cpu",
            "--dtype=float32",
        )
        result_cpu.assert_outcomes(passed=1)

    def test_isolated_skip_is_reported_with_quiet_addopts(self, pytester, monkeypatch):
        """The isolation parent must not turn a child's skip into a pass when addopts is quiet.

        ``-o addopts=-q`` on the parent command line is not inherited by the child, so it cannot
        reproduce the failure; ``PYTEST_ADDOPTS`` is. pytester clears that variable at fixture
        setup, so it has to be set here, in the test body.

        ``--device=cpu,cuda`` also pins the second way the child can come back empty. The probe is
        marked ``device_agnostic``; with CPU in the matrix the parent keeps it, but the child is
        handed a bare ``--device=cuda``, under which the deselection rule fires unless the parent
        forwards ``--run-device-agnostic``. Both legs skip here, hence ``skipped=2``.
        """
        monkeypatch.setenv("PYTEST_ADDOPTS", "-q")
        node_id = f"{Path(__file__)}::test_half_precision_isolation_probe"
        result = pytester.runpytest_subprocess(
            node_id,
            "--device=cpu,cuda",
            "--dtype=float16",
            "--isolate-half-precision",
            "-rs",
        )
        result.assert_outcomes(skipped=2)
        # The count on its own does not prove the child ran: `pytest_runtest_protocol` also maps
        # the child's exit code 5 ("no tests collected") onto a skip, so a node the parent collects
        # but the child parametrises away would satisfy the count while proving nothing. Deleting
        # or renaming the node is caught by the count already (the parent reports 0 outcomes), but
        # a divergence between parent and child collection is not. `-rs` puts the synthetic
        # report's reason in the summary, so require the skip to be the child's own.
        result.stdout.no_fnmatch_line("*no tests collected*")


class TestDeviceAgnosticSelection:
    def test_device_agnostic_tests_run_once_in_cpu_containing_matrix(self, pytester):
        test_file = pytester.makepyfile(
            """
            import pytest

            @pytest.mark.device_agnostic
            def test_device_agnostic():
                assert True

            def test_unmarked():
                assert True
            """
        )

        result_cpu = pytester.runpytest_subprocess(
            "-p",
            "conftest",
            str(test_file),
            "-o",
            "testpaths=.",
            "--device=cpu",
            "--dtype=float32",
        )
        result_cpu.assert_outcomes(passed=2)

        result_cuda = pytester.runpytest_subprocess(
            "-p",
            "conftest",
            str(test_file),
            "-o",
            "testpaths=.",
            "--device=cuda",
            "--dtype=float32",
        )
        result_cuda.assert_outcomes(passed=1, deselected=1)

    def test_run_device_agnostic_overrides_the_deselection(self, pytester):
        test_file = pytester.makepyfile(
            """
            import pytest

            @pytest.mark.device_agnostic
            def test_device_agnostic():
                assert True

            def test_unmarked():
                assert True
            """
        )

        result = pytester.runpytest_subprocess(
            "-p",
            "conftest",
            str(test_file),
            "-o",
            "testpaths=.",
            "--device=cuda",
            "--dtype=float32",
            "--run-device-agnostic",
        )
        result.assert_outcomes(passed=2)


class TestKeepDistributionValidation:
    # Named without "compile" or "dynamo" so the ordinary jobs, the ones that run the tests pinned on a
    # validation error, collect it.
    @pytest.mark.skipif(not dynamo_is_available(), reason=DYNAMO_UNAVAILABLE_REASON)
    def test_the_first_torch_optimizer_entry_leaves_validation_on_for_the_next_test(self, pytester):
        # Dynamo's one-time setup turns torch.distributions argument validation off process-wide; the
        # autouse fixture puts it back after the test that entered Dynamo, so the next test, and a test pinned
        # on a validation error, sees the validation it would see on its own.
        test_file = pytester.makepyfile(
            """
            import torch
            from torch.distributions import Distribution

            def test_first_entry():
                assert Distribution._validate_args
                torch.compile(lambda x: x + 1)
                assert not Distribution._validate_args

            def test_next():
                assert Distribution._validate_args
            """
        )

        result = pytester.runpytest_subprocess(
            "-p",
            "conftest",
            str(test_file),
            "-o",
            "testpaths=.",
            "--device=cpu",
            "--dtype=float32",
        )
        result.assert_outcomes(passed=2)