Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

test(conftest): restore torch.distributions validation after each test (#5491)

εŒΊζ’“η5da2f52be218
diff --git a/conftest.py b/conftest.py
index a8f7b46c8..f6eb46fb4 100644
--- a/conftest.py
+++ b/conftest.py
@@ -29,6 +29,7 @@ from pathlib import Path
import numpy as np
import pytest
import torch
+from torch.distributions import Distribution
import kornia
from kornia.core.download import load_state_dict_from_url
@@ -134,6 +135,23 @@ def dtype(dtype_name) -> torch.dtype:
return TEST_DTYPES[dtype_name]
[email protected](autouse=True)
+def keep_distribution_validation():
+ """Leave ``torch.distributions`` argument validation as each test found it.
+
+ The first Dynamo entry in a process, ``torch.compile`` included, runs a one-time setup that calls
+ ``Distribution.set_default_validate_args(False)`` process-wide. Without this fixture every test after
+ that entry runs with validation off, so a test pinned on a validation error passes or fails depending
+ on whether a compile test ran before it in the session.
+ """
+ validate_args = Distribution._validate_args
+ try:
+ yield
+ finally:
+ if Distribution._validate_args != validate_args:
+ Distribution.set_default_validate_args(validate_args)
+
+
@pytest.fixture
def torch_optimizer(optimizer_backend):
"""Return torch optimizer based on backend selection.
diff --git a/tests/test_conftest.py b/tests/test_conftest.py
index f10016a45..3437c1c22 100644
--- a/tests/test_conftest.py
+++ b/tests/test_conftest.py
@@ -25,6 +25,8 @@ import torch
from conftest import _is_subprocess_isolated_test, skip_half_precision_on_cuda
+from testing.base import DYNAMO_UNAVAILABLE_REASON, 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_
@@ -384,3 +386,38 @@ class TestDeviceAgnosticSelection:
"--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)