|
| 1 | +"""Tests for keras_remote.core.accelerators — parser, registry, categories.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from keras_remote.core.accelerators import ( |
| 6 | + _GPU_ALIASES, |
| 7 | + GPUS, |
| 8 | + TPUS, |
| 9 | + GpuConfig, |
| 10 | + TpuConfig, |
| 11 | + get_category, |
| 12 | + parse_accelerator, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +class TestParseGpuDirect: |
| 17 | + def test_l4(self): |
| 18 | + result = parse_accelerator("l4") |
| 19 | + assert isinstance(result, GpuConfig) |
| 20 | + assert result.name == "l4" |
| 21 | + assert result.count == 1 |
| 22 | + assert result.gke_label == "nvidia-l4" |
| 23 | + assert result.machine_type == "g2-standard-4" |
| 24 | + |
| 25 | + @pytest.mark.parametrize("gpu_name", list(GPUS.keys())) |
| 26 | + def test_all_gpu_types_parse_with_count_1(self, gpu_name): |
| 27 | + result = parse_accelerator(gpu_name) |
| 28 | + assert isinstance(result, GpuConfig) |
| 29 | + assert result.count == 1 |
| 30 | + assert result.name == gpu_name |
| 31 | + |
| 32 | + |
| 33 | +class TestParseGpuMultiCount: |
| 34 | + def test_a100x4(self): |
| 35 | + result = parse_accelerator("a100x4") |
| 36 | + assert isinstance(result, GpuConfig) |
| 37 | + assert result.name == "a100" |
| 38 | + assert result.count == 4 |
| 39 | + |
| 40 | + def test_a100_80gbx4(self): |
| 41 | + result = parse_accelerator("a100-80gbx4") |
| 42 | + assert isinstance(result, GpuConfig) |
| 43 | + assert result.name == "a100-80gb" |
| 44 | + assert result.count == 4 |
| 45 | + |
| 46 | + |
| 47 | +class TestParseGpuAlias: |
| 48 | + def test_nvidia_tesla_t4(self): |
| 49 | + result = parse_accelerator("nvidia-tesla-t4") |
| 50 | + assert isinstance(result, GpuConfig) |
| 51 | + assert result.name == "t4" |
| 52 | + assert result.count == 1 |
| 53 | + |
| 54 | + def test_nvidia_tesla_v100x4(self): |
| 55 | + result = parse_accelerator("nvidia-tesla-v100x4") |
| 56 | + assert isinstance(result, GpuConfig) |
| 57 | + assert result.name == "v100" |
| 58 | + assert result.count == 4 |
| 59 | + |
| 60 | + |
| 61 | +class TestParseGpuErrors: |
| 62 | + def test_l4x8_invalid_count(self): |
| 63 | + with pytest.raises(ValueError, match="not supported"): |
| 64 | + parse_accelerator("l4x8") |
| 65 | + |
| 66 | + |
| 67 | +class TestParseTpuBare: |
| 68 | + def test_v5litepod(self): |
| 69 | + result = parse_accelerator("v5litepod") |
| 70 | + assert isinstance(result, TpuConfig) |
| 71 | + assert result.name == "v5litepod" |
| 72 | + assert result.chips == 4 |
| 73 | + assert result.topology == "2x2" |
| 74 | + |
| 75 | + @pytest.mark.parametrize("tpu_name", list(TPUS.keys())) |
| 76 | + def test_all_tpu_types_parse_with_default_chips(self, tpu_name): |
| 77 | + result = parse_accelerator(tpu_name) |
| 78 | + assert isinstance(result, TpuConfig) |
| 79 | + assert result.name == tpu_name |
| 80 | + assert result.chips == TPUS[tpu_name].default_chips |
| 81 | + |
| 82 | + |
| 83 | +class TestParseTpuChipCount: |
| 84 | + def test_v3_8(self): |
| 85 | + result = parse_accelerator("v3-8") |
| 86 | + assert isinstance(result, TpuConfig) |
| 87 | + assert result.name == "v3" |
| 88 | + assert result.chips == 8 |
| 89 | + assert result.topology == "2x2" |
| 90 | + |
| 91 | + def test_v3_32(self): |
| 92 | + result = parse_accelerator("v3-32") |
| 93 | + assert isinstance(result, TpuConfig) |
| 94 | + assert result.name == "v3" |
| 95 | + assert result.chips == 32 |
| 96 | + assert result.topology == "4x4" |
| 97 | + |
| 98 | + def test_v5litepod_1(self): |
| 99 | + result = parse_accelerator("v5litepod-1") |
| 100 | + assert isinstance(result, TpuConfig) |
| 101 | + assert result.chips == 1 |
| 102 | + assert result.topology == "1x1" |
| 103 | + |
| 104 | + |
| 105 | +class TestParseTpuTopology: |
| 106 | + def test_v5litepod_2x2(self): |
| 107 | + result = parse_accelerator("v5litepod-2x2") |
| 108 | + assert isinstance(result, TpuConfig) |
| 109 | + assert result.name == "v5litepod" |
| 110 | + assert result.chips == 4 |
| 111 | + assert result.topology == "2x2" |
| 112 | + |
| 113 | + def test_v5litepod_1x1(self): |
| 114 | + result = parse_accelerator("v5litepod-1x1") |
| 115 | + assert isinstance(result, TpuConfig) |
| 116 | + assert result.chips == 1 |
| 117 | + assert result.topology == "1x1" |
| 118 | + |
| 119 | + |
| 120 | +class TestParseTpuErrors: |
| 121 | + def test_v3_16_invalid_chips(self): |
| 122 | + with pytest.raises(ValueError, match="not supported"): |
| 123 | + parse_accelerator("v3-16") |
| 124 | + |
| 125 | + def test_v5litepod_3x3_invalid_topology(self): |
| 126 | + with pytest.raises(ValueError, match="Unknown accelerator"): |
| 127 | + parse_accelerator("v5litepod-3x3") |
| 128 | + |
| 129 | + |
| 130 | +class TestParseTpuConfigFields: |
| 131 | + def test_v3_8_full_config(self): |
| 132 | + result = parse_accelerator("v3-8") |
| 133 | + assert result.gke_accelerator == "tpu-v3-podslice" |
| 134 | + assert result.machine_type == "ct3p-hightpu-4t" |
| 135 | + assert result.num_nodes == 2 |
| 136 | + |
| 137 | + |
| 138 | +class TestParseCpu: |
| 139 | + def test_cpu(self): |
| 140 | + assert parse_accelerator("cpu") is None |
| 141 | + |
| 142 | + |
| 143 | +class TestParseNormalizationAndErrors: |
| 144 | + def test_whitespace_and_case(self): |
| 145 | + result = parse_accelerator(" A100X4 ") |
| 146 | + assert isinstance(result, GpuConfig) |
| 147 | + assert result.name == "a100" |
| 148 | + assert result.count == 4 |
| 149 | + |
| 150 | + def test_empty_string(self): |
| 151 | + with pytest.raises(ValueError, match="Unknown accelerator"): |
| 152 | + parse_accelerator("") |
| 153 | + |
| 154 | + def test_unknown_accelerator(self): |
| 155 | + with pytest.raises(ValueError, match="Unknown accelerator"): |
| 156 | + parse_accelerator("unknown") |
| 157 | + |
| 158 | + |
| 159 | +class TestGetCategory: |
| 160 | + def test_cpu(self): |
| 161 | + assert get_category("cpu") == "cpu" |
| 162 | + |
| 163 | + def test_gpu(self): |
| 164 | + assert get_category("l4") == "gpu" |
| 165 | + |
| 166 | + def test_tpu(self): |
| 167 | + assert get_category("v5litepod") == "tpu" |
| 168 | + |
| 169 | + |
| 170 | +class TestRegistryIntegrity: |
| 171 | + def test_all_gpus_have_nonempty_counts(self): |
| 172 | + for name, spec in GPUS.items(): |
| 173 | + assert len(spec.counts) > 0, f"GPU '{name}' has empty counts" |
| 174 | + |
| 175 | + def test_all_tpus_have_nonempty_topologies(self): |
| 176 | + for name, spec in TPUS.items(): |
| 177 | + assert len(spec.topologies) > 0, f"TPU '{name}' has empty topologies" |
| 178 | + |
| 179 | + def test_all_tpu_default_chips_valid(self): |
| 180 | + for name, spec in TPUS.items(): |
| 181 | + assert spec.default_chips in spec.topologies, ( |
| 182 | + f"TPU '{name}' default_chips={spec.default_chips} " |
| 183 | + f"not in topologies {list(spec.topologies.keys())}" |
| 184 | + ) |
| 185 | + |
| 186 | + def test_all_gpus_have_gke_label_alias(self): |
| 187 | + for name, spec in GPUS.items(): |
| 188 | + assert spec.gke_label in _GPU_ALIASES, ( |
| 189 | + f"GPU '{name}' gke_label '{spec.gke_label}' not in aliases" |
| 190 | + ) |
| 191 | + assert _GPU_ALIASES[spec.gke_label] == name |
0 commit comments