|
8 | 8 | from hydra import compose, initialize_config_dir |
9 | 9 | from omegaconf import OmegaConf |
10 | 10 |
|
11 | | -from cybench.util.config_utils import adjust_model_cfg_to_dataset |
| 11 | +from cybench.util.config_utils import ( |
| 12 | + adjust_model_cfg_to_dataset, |
| 13 | + apply_force_cpu_to_frozen_model_cfg, |
| 14 | + is_cybench_force_cpu, |
| 15 | + walk_forward_force_cpu, |
| 16 | +) |
12 | 17 |
|
13 | 18 |
|
14 | 19 | class _FakeTorchDataset: |
@@ -45,3 +50,49 @@ def test_adjust_model_cfg_sets_missing_placeholder_dims(): |
45 | 50 | assert adjusted.torch_model.temporal_in_dim == 12 |
46 | 51 | assert adjusted.torch_model.context_in_dim == 5 |
47 | 52 | assert not OmegaConf.is_missing(adjusted.torch_model, "temporal_in_dim") |
| 53 | + |
| 54 | + |
| 55 | +def test_is_cybench_force_cpu(monkeypatch): |
| 56 | + monkeypatch.delenv("CYBENCH_FORCE_CPU", raising=False) |
| 57 | + assert is_cybench_force_cpu() is False |
| 58 | + monkeypatch.setenv("CYBENCH_FORCE_CPU", "1") |
| 59 | + assert is_cybench_force_cpu() is True |
| 60 | + monkeypatch.setenv("CYBENCH_FORCE_CPU", "yes") |
| 61 | + assert is_cybench_force_cpu() is True |
| 62 | + |
| 63 | + |
| 64 | +def test_walk_forward_force_cpu_from_hydra_overrides(monkeypatch): |
| 65 | + monkeypatch.delenv("CYBENCH_FORCE_CPU", raising=False) |
| 66 | + cfg = OmegaConf.create({"model": {"device": "cpu"}, "experiment": {"device": "cuda"}}) |
| 67 | + assert walk_forward_force_cpu(cfg) is True |
| 68 | + cfg = OmegaConf.create({"model": {"device": "auto"}, "experiment": {"device": "cpu"}}) |
| 69 | + assert walk_forward_force_cpu(cfg) is True |
| 70 | + cfg = OmegaConf.create({"model": {"device": "auto"}, "experiment": {"device": "cuda"}}) |
| 71 | + assert walk_forward_force_cpu(cfg) is False |
| 72 | + |
| 73 | + |
| 74 | +def test_apply_force_cpu_to_frozen_model_cfg(): |
| 75 | + frozen = OmegaConf.create( |
| 76 | + { |
| 77 | + "_target_": "cybench.models.tabular_foundation_model.TabDPTModel", |
| 78 | + "name": "tabdpt", |
| 79 | + "device": "cuda", |
| 80 | + "allow_cpu_fallback": False, |
| 81 | + } |
| 82 | + ) |
| 83 | + out = apply_force_cpu_to_frozen_model_cfg(frozen) |
| 84 | + assert out.device == "cpu" |
| 85 | + assert out.allow_cpu_fallback is True |
| 86 | + |
| 87 | + |
| 88 | +def test_apply_force_cpu_to_frozen_torch_model_cfg(): |
| 89 | + frozen = OmegaConf.create( |
| 90 | + { |
| 91 | + "framework": "torch", |
| 92 | + "device": "cuda", |
| 93 | + "torch_model": {"input_size": 6, "device": "cuda"}, |
| 94 | + } |
| 95 | + ) |
| 96 | + out = apply_force_cpu_to_frozen_model_cfg(frozen) |
| 97 | + assert out.device == "cpu" |
| 98 | + assert out.torch_model.device == "cpu" |
0 commit comments