diff --git a/tests/unit_tests/cpu/tools/test_tools_utils.py b/tests/unit_tests/cpu/tools/test_tools_utils.py index 678a751f742..09a4b8ef6ae 100644 --- a/tests/unit_tests/cpu/tools/test_tools_utils.py +++ b/tests/unit_tests/cpu/tools/test_tools_utils.py @@ -10,7 +10,11 @@ import torch from torchtitan.tools.garbage_collector import GarbageCollector -from torchtitan.tools.utils import get_cuda_flash_attention_impl, get_local_device +from torchtitan.tools.utils import ( + _configure_compile_backend, + get_cuda_flash_attention_impl, + get_local_device, +) class _FakeDeviceModule: @@ -21,6 +25,20 @@ def device_count(self) -> int: return self.num_devices +def test_configure_compile_backend(monkeypatch: pytest.MonkeyPatch) -> None: + set_default_backend = Mock() + monkeypatch.setenv("TORCHTITAN_COMPILE_BACKEND", "custom_device_backend") + monkeypatch.setattr( + torch.compiler, + "set_default_backend", + set_default_backend, + ) + + _configure_compile_backend() + + set_default_backend.assert_called_once_with("custom_device_backend") + + def test_gc_debug_collects_once_per_step(monkeypatch: pytest.MonkeyPatch) -> None: gc_collect = Mock() monkeypatch.setattr("torchtitan.tools.garbage_collector.gc.collect", gc_collect) diff --git a/torchtitan/tools/utils.py b/torchtitan/tools/utils.py index 5e1810eddc9..eb59202076d 100644 --- a/torchtitan/tools/utils.py +++ b/torchtitan/tools/utils.py @@ -66,7 +66,14 @@ def get_device_info() -> tuple[str, ModuleType]: return device_type, device_module +def _configure_compile_backend() -> None: + """Configure the default ``torch.compile`` backend from the environment.""" + if backend := os.environ.get("TORCHTITAN_COMPILE_BACKEND"): + torch.compiler.set_default_backend(backend) + + device_type, device_module = get_device_info() +_configure_compile_backend() def get_local_device() -> torch.device: