Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion tests/unit_tests/cpu/tools/test_tools_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions torchtitan/tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading