|
| 1 | +# Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import importlib.util |
| 16 | +import os |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +import pytest |
| 20 | +import torch |
| 21 | + |
| 22 | + |
| 23 | +pytestmark = pytest.mark.unit |
| 24 | + |
| 25 | +EXAMPLE_DIR = Path(__file__).resolve().parents[3] / "examples" / "models" / "diffusion_gemma" |
| 26 | +PARITY_SCRIPTS = ("toy_parity.py", "toy_training_parity.py", "real_encoder_parity.py", "real_fp32_parity.py") |
| 27 | + |
| 28 | + |
| 29 | +def _load_fp32_precision(): |
| 30 | + spec = importlib.util.spec_from_file_location("fp32_precision", EXAMPLE_DIR / "fp32_precision.py") |
| 31 | + module = importlib.util.module_from_spec(spec) |
| 32 | + spec.loader.exec_module(module) |
| 33 | + return module |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture |
| 37 | +def tf32_state(): |
| 38 | + matmul, cudnn = torch.backends.cuda.matmul.allow_tf32, torch.backends.cudnn.allow_tf32 |
| 39 | + override = os.environ.pop("NVIDIA_TF32_OVERRIDE", None) |
| 40 | + yield |
| 41 | + os.environ.pop("NVIDIA_TF32_OVERRIDE", None) |
| 42 | + if override is not None: |
| 43 | + os.environ["NVIDIA_TF32_OVERRIDE"] = override |
| 44 | + torch.backends.cuda.matmul.allow_tf32, torch.backends.cudnn.allow_tf32 = matmul, cudnn |
| 45 | + |
| 46 | + |
| 47 | +def test_disable_tf32_also_disables_cublas_tf32(tf32_state): |
| 48 | + fp32_precision = _load_fp32_precision() |
| 49 | + torch.backends.cuda.matmul.allow_tf32 = True |
| 50 | + torch.backends.cudnn.allow_tf32 = True |
| 51 | + |
| 52 | + assert fp32_precision.disable_tf32() is True |
| 53 | + assert os.environ["NVIDIA_TF32_OVERRIDE"] == "0" |
| 54 | + assert torch.backends.cuda.matmul.allow_tf32 is False |
| 55 | + assert torch.backends.cudnn.allow_tf32 is False |
| 56 | + |
| 57 | + |
| 58 | +def test_torch_flags_alone_do_not_count_as_tf32_disabled(tf32_state): |
| 59 | + fp32_precision = _load_fp32_precision() |
| 60 | + torch.backends.cuda.matmul.allow_tf32 = False |
| 61 | + torch.backends.cudnn.allow_tf32 = False |
| 62 | + |
| 63 | + assert fp32_precision.tf32_disabled() is False |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.parametrize("script", PARITY_SCRIPTS) |
| 67 | +def test_parity_scripts_disable_tf32_through_the_shared_helper(script): |
| 68 | + source = (EXAMPLE_DIR / script).read_text() |
| 69 | + |
| 70 | + assert "disable_tf32()" in source |
| 71 | + assert "allow_tf32 = False" not in source |
0 commit comments