Skip to content

Commit 30ebbe9

Browse files
committed
Restore dynamic PyTorch precision setting explicitly in tests
1 parent 80657ba commit 30ebbe9

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

‎tests/tests_algorithms/test_sdm_context.py‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,20 @@ def test_precision_setting_and_restoration_after_failure(
148148
learner.params["precision"] = precision
149149
autocast = Mock(return_value=nullcontext())
150150
monkeypatch.setattr(torch.amp, "autocast", autocast)
151-
monkeypatch.setattr(torch.backends.cuda.matmul, "allow_tf32", True)
152-
with pytest.raises(RuntimeError, match="interrupted"):
153-
with _autocast(learner):
154-
assert torch.backends.cuda.matmul.allow_tf32 == (
155-
device == "cpu" or precision != "fp32"
156-
)
157-
raise RuntimeError("interrupted")
158-
assert torch.backends.cuda.matmul.allow_tf32
151+
# PyTorch exposes this setting dynamically, so restore it explicitly:
152+
# monkeypatch's static attribute inspection can mistake it for a new attribute.
153+
original_tf32 = torch.backends.cuda.matmul.allow_tf32
154+
try:
155+
torch.backends.cuda.matmul.allow_tf32 = True
156+
with pytest.raises(RuntimeError, match="interrupted"):
157+
with _autocast(learner):
158+
assert torch.backends.cuda.matmul.allow_tf32 == (
159+
device == "cpu" or precision != "fp32"
160+
)
161+
raise RuntimeError("interrupted")
162+
assert torch.backends.cuda.matmul.allow_tf32
163+
finally:
164+
torch.backends.cuda.matmul.allow_tf32 = original_tf32
159165
autocast.assert_called_once_with(
160166
device.split(":")[0],
161167
dtype=torch.float16,

0 commit comments

Comments
 (0)