diff --git a/fastvideo/tests/conftest.py b/fastvideo/tests/conftest.py index dd5d65764..a92c865e5 100644 --- a/fastvideo/tests/conftest.py +++ b/fastvideo/tests/conftest.py @@ -45,3 +45,21 @@ def env_overrides(): """ with contextlib.ExitStack() as stack: yield stack + + +@pytest.fixture +def tf32_disabled(): + """Disable TF32 for CUDA matmuls and cuDNN during the test, then restore the previous flags. + + On torch 2.12 ``torch.backends.cuda.matmul.allow_tf32`` is computed by ``cuBLASModule`` rather + than stored on it, so ``monkeypatch.setattr`` cannot undo it. Restore through the same legacy + flags: mixing them with the per-backend ``fp32_precision`` API makes later ``allow_tf32`` reads raise. + """ + matmul, cudnn = torch.backends.cuda.matmul, torch.backends.cudnn + previous = (matmul.allow_tf32, cudnn.allow_tf32) + matmul.allow_tf32 = False + cudnn.allow_tf32 = False + try: + yield + finally: + matmul.allow_tf32, cudnn.allow_tf32 = previous diff --git a/fastvideo/tests/transformers/test_hunyuan_batch.py b/fastvideo/tests/transformers/test_hunyuan_batch.py index 77dff563e..01887a66e 100644 --- a/fastvideo/tests/transformers/test_hunyuan_batch.py +++ b/fastvideo/tests/transformers/test_hunyuan_batch.py @@ -25,7 +25,7 @@ pytestmark = pytest.mark.skipif(not torch.cuda.is_available(), reason="Requires @pytest.fixture -def hunyuan_setup(monkeypatch, request): +def hunyuan_setup(request): # Environment writes go through the registry helpers (docs/contributing/env_vars.md). with contextlib.ExitStack() as stack: # Respect the port reserved by the CI runner when sharing a GPU host. @@ -37,8 +37,7 @@ def hunyuan_setup(monkeypatch, request): if "MASTER_ADDR" not in os.environ: stack.enter_context(envs.override_external("MASTER_ADDR", "127.0.0.1")) stack.enter_context(envs.FASTVIDEO_ATTENTION_BACKEND.override("TORCH_SDPA")) - monkeypatch.setattr(torch.backends.cuda.matmul, "allow_tf32", False) - monkeypatch.setattr(torch.backends.cudnn, "allow_tf32", False) + request.getfixturevalue("tf32_disabled") request.getfixturevalue("distributed_setup") yield diff --git a/fastvideo/tests/transformers/test_qwen_image21_attention.py b/fastvideo/tests/transformers/test_qwen_image21_attention.py index 2fa32f919..0c1253c09 100644 --- a/fastvideo/tests/transformers/test_qwen_image21_attention.py +++ b/fastvideo/tests/transformers/test_qwen_image21_attention.py @@ -24,12 +24,11 @@ from fastvideo.layers.linear import ReplicatedLinear from fastvideo.platforms import AttentionBackendEnum @pytest.fixture -def qwen_setup(env_overrides, monkeypatch, request): +def qwen_setup(env_overrides, request): if not torch.cuda.is_available(): pytest.skip("Requires one CUDA GPU") env_overrides.enter_context(envs.FASTVIDEO_ATTENTION_BACKEND.override("TORCH_SDPA")) - monkeypatch.setattr(torch.backends.cuda.matmul, "allow_tf32", False) - monkeypatch.setattr(torch.backends.cudnn, "allow_tf32", False) + request.getfixturevalue("tf32_disabled") request.getfixturevalue("distributed_setup") with torch.no_grad(), sdpa_kernel(SDPBackend.MATH): yield torch.device("cuda")