|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import unittest |
| 4 | +from pathlib import Path |
| 5 | +from unittest.mock import MagicMock, patch |
| 6 | + |
| 7 | +import torch |
| 8 | + |
| 9 | +from engine.config import ModelConfig, TrainingConfig |
| 10 | +from engine.training_impl import _try_compile_model |
| 11 | + |
| 12 | + |
| 13 | +class CompileModelTests(unittest.TestCase): |
| 14 | + def test_compile_model_defaults_to_false(self) -> None: |
| 15 | + config = TrainingConfig(output_dir=Path("tmp")) |
| 16 | + self.assertFalse(config.compile_model) |
| 17 | + |
| 18 | + def test_try_compile_model_returns_eager_when_disabled(self) -> None: |
| 19 | + model = torch.nn.Linear(10, 10) |
| 20 | + compiled = _try_compile_model(model, "cuda", enabled=False) |
| 21 | + self.assertIs(compiled, model) |
| 22 | + |
| 23 | + def test_try_compile_model_skips_on_cpu(self) -> None: |
| 24 | + model = torch.nn.Linear(10, 10) |
| 25 | + compiled = _try_compile_model(model, "cpu", enabled=True) |
| 26 | + self.assertIs(compiled, model) |
| 27 | + |
| 28 | + @patch("torch.compile", side_effect=RuntimeError("Inductor compiler error")) |
| 29 | + def test_try_compile_model_catches_compiler_error_and_falls_back(self, _mock_compile) -> None: |
| 30 | + model = torch.nn.Linear(10, 10) |
| 31 | + with patch("torch.cuda.is_available", return_value=True): |
| 32 | + compiled = _try_compile_model(model, "cuda", enabled=True) |
| 33 | + self.assertIs(compiled, model) |
| 34 | + |
| 35 | + |
| 36 | +if __name__ == "__main__": |
| 37 | + unittest.main() |
0 commit comments