Buckets:
| import json | |
| import unittest | |
| from pathlib import Path | |
| from training.runtime import RuntimeConfig, load_runtime_config | |
| class RuntimeConfigTests(unittest.TestCase): | |
| def load_from_data(self, data: dict[str, object]): | |
| config_path = Path(__file__).with_name(".runtime-test-config.json") | |
| try: | |
| config_path.write_text(json.dumps(data), encoding="utf-8") | |
| return load_runtime_config(config_path) | |
| finally: | |
| config_path.unlink(missing_ok=True) | |
| def test_runtime_config_defaults_to_bounded_cpu(self) -> None: | |
| config_path = ( | |
| Path(__file__).resolve().parents[2] | |
| / "training" | |
| / "configs" | |
| / "runtime.json" | |
| ) | |
| config = load_runtime_config(config_path) | |
| self.assertIsInstance(config, RuntimeConfig) | |
| self.assertEqual(config.device, "cpu") | |
| self.assertGreater(config.num_threads, 0) | |
| self.assertLessEqual(config.num_threads, 8) | |
| self.assertFalse(config.use_cuda) | |
| self.assertFalse(config.use_amp) | |
| def test_runtime_config_rejects_mismatched_cuda_flag(self) -> None: | |
| with self.assertRaises(ValueError): | |
| self.load_from_data( | |
| { | |
| "device": "cuda:0", | |
| "num_threads": 4, | |
| "seed": 42, | |
| "use_cuda": False, | |
| "use_amp": False, | |
| } | |
| ) | |
| def test_runtime_config_accepts_explicit_cuda_device(self) -> None: | |
| config = self.load_from_data( | |
| { | |
| "device": "cuda:0", | |
| "num_threads": 4, | |
| "seed": 42, | |
| "use_cuda": True, | |
| "use_amp": False, | |
| } | |
| ) | |
| self.assertEqual(config.device, "cuda:0") | |
| self.assertTrue(config.use_cuda) | |
| def test_runtime_config_rejects_unbounded_thread_count(self) -> None: | |
| with self.assertRaises(ValueError): | |
| self.load_from_data( | |
| { | |
| "device": "cpu", | |
| "num_threads": 9, | |
| "seed": 42, | |
| "use_cuda": False, | |
| "use_amp": False, | |
| } | |
| ) | |
| def test_runtime_config_rejects_invalid_seed(self) -> None: | |
| with self.assertRaises(ValueError): | |
| self.load_from_data( | |
| { | |
| "device": "cpu", | |
| "num_threads": 4, | |
| "seed": "42", | |
| "use_cuda": False, | |
| "use_amp": False, | |
| } | |
| ) | |
| def test_runtime_config_rejects_accelerated_precision(self) -> None: | |
| for field in ("use_cuda", "use_amp"): | |
| with self.subTest(field=field), self.assertRaises(ValueError): | |
| self.load_from_data( | |
| { | |
| "device": "cpu", | |
| "num_threads": 4, | |
| "seed": 42, | |
| "use_cuda": field == "use_cuda", | |
| "use_amp": field == "use_amp", | |
| } | |
| ) | |
| def test_cpu_runtime_config_remains_the_default(self) -> None: | |
| config_path = Path(__file__).resolve().parents[2] / "training" / "configs" / "runtime.json" | |
| config = load_runtime_config(config_path) | |
| self.assertEqual(config.device, "cpu") | |
| self.assertFalse(config.use_cuda) | |
| if __name__ == "__main__": | |
| unittest.main() | |
Xet Storage Details
- Size:
- 3.6 kB
- Xet hash:
- 20cd53b883184abffabfcb97030aa8d66a73781d2e7e828bbca3a384424d08cd
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.