|
| 1 | +""" |
| 2 | +Copyright 2026 Google LLC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +""" |
| 16 | + |
| 17 | +import time |
| 18 | +import unittest |
| 19 | + |
| 20 | +from maxdiffusion.utils.tile_size_grid_search import ( |
| 21 | + MXU_TILE, |
| 22 | + VPU_LANE, |
| 23 | + BenchResult, |
| 24 | + BlockBenchmark, |
| 25 | + bkv_candidates, |
| 26 | + bq_candidates, |
| 27 | + grid_search, |
| 28 | + padding_of, |
| 29 | + smart_grid, |
| 30 | + time_callable, |
| 31 | + vmem_bkv_ceiling, |
| 32 | +) |
| 33 | + |
| 34 | +# per-shard seq the ring U=1 kernel tiles (75600 / 8); the empirical winner is bq=9472, bkv=1024. |
| 35 | +RING_SEQ = 9450 |
| 36 | +VMEM_64MB = 64 * 1024 * 1024 |
| 37 | + |
| 38 | + |
| 39 | +class _MockRingBench(BlockBenchmark): |
| 40 | + """Synthetic benchmark encoding the measured ring behaviour: fewer Q-tiles is faster, the |
| 41 | + bkv_compute sweet spot is ~1024, odd-128 blocks pay a half-MXU-pass penalty, and a score |
| 42 | + tile that overflows VMEM OOMs. Lets us test the orchestrator with no TPU.""" |
| 43 | + |
| 44 | + label = "mock-ring" |
| 45 | + |
| 46 | + def __init__(self, seq=RING_SEQ, vmem=VMEM_64MB): |
| 47 | + self.seq, self._vmem = seq, vmem |
| 48 | + |
| 49 | + def tiled_seq_lens(self): |
| 50 | + return (self.seq, self.seq) |
| 51 | + |
| 52 | + def vmem_bytes(self): |
| 53 | + return self._vmem |
| 54 | + |
| 55 | + def run(self, bq, bkv, *, bkv_compute=None, iters=10, warmup=2): |
| 56 | + cmp = bkv_compute or bkv |
| 57 | + if bq * cmp * 4 + 15e6 > self._vmem: # score tile f32 + ~15MB ring overhead |
| 58 | + return BenchResult(bq, bkv, cmp, "oom") |
| 59 | + n_q = padding_of(self.seq, bq).n_blocks |
| 60 | + n_kv = padding_of(self.seq, bkv).n_blocks |
| 61 | + ms = 60 + 4.0 * n_q + 0.9 * n_kv - 3.0 * min(cmp, 1024) / 1024 |
| 62 | + ms += 1.4 * (bq % MXU_TILE != 0) + 1.4 * (bkv % MXU_TILE != 0) |
| 63 | + return BenchResult(bq, bkv, cmp, "ok", mean_ms=round(ms, 2), std_ms=0.2, compile_ms=25000.0) |
| 64 | + |
| 65 | + |
| 66 | +class PaddingMathTest(unittest.TestCase): |
| 67 | + |
| 68 | + def test_padding_of(self): |
| 69 | + p = padding_of(RING_SEQ, 1024) |
| 70 | + self.assertEqual(p.n_blocks, 10) |
| 71 | + self.assertEqual(p.padded_len, 10240) |
| 72 | + self.assertEqual(p.pad, 790) |
| 73 | + |
| 74 | + def test_single_tile_is_low_pad(self): |
| 75 | + self.assertEqual(padding_of(RING_SEQ, 9472).pad, 22) # 37 * 256 |
| 76 | + |
| 77 | + |
| 78 | +class CandidateTest(unittest.TestCase): |
| 79 | + |
| 80 | + def test_bq_fewest_tile_ladder(self): |
| 81 | + # single-block ceiling fits, so the ladder starts at n=1 (bq=9472) and includes the winner. |
| 82 | + bqs = bq_candidates(RING_SEQ, k=3, spread=0) |
| 83 | + self.assertEqual(bqs[0], 9472) |
| 84 | + self.assertTrue(all(b % VPU_LANE == 0 for b in bqs)) |
| 85 | + |
| 86 | + def test_bkv_largest_fits_includes_winner(self): |
| 87 | + ceil = vmem_bkv_ceiling(9472, vmem_bytes=VMEM_64MB) |
| 88 | + bkvs = bkv_candidates(RING_SEQ, k=3, max_block=ceil) |
| 89 | + self.assertIn(1024, bkvs) # the measured optimum, largest 256-mult that fits at bq=9472 |
| 90 | + |
| 91 | + def test_smart_grid_pairs_winner(self): |
| 92 | + pairs = smart_grid(RING_SEQ, RING_SEQ, vmem_bytes=VMEM_64MB, dtype_bytes=4) |
| 93 | + self.assertIn((9472, 1024), pairs) |
| 94 | + |
| 95 | + def test_candidates_not_strictly_256(self): |
| 96 | + # 128-multiples (e.g. 896) must be admissible, not filtered out. |
| 97 | + bkvs = bkv_candidates(RING_SEQ, k=3, max_block=vmem_bkv_ceiling(9472, vmem_bytes=VMEM_64MB)) |
| 98 | + self.assertTrue(any(b % MXU_TILE != 0 for b in bkvs)) |
| 99 | + |
| 100 | + |
| 101 | +class TimingTest(unittest.TestCase): |
| 102 | + |
| 103 | + def test_compile_excluded_from_mean(self): |
| 104 | + calls = {"n": 0} |
| 105 | + |
| 106 | + def fake_fn(): |
| 107 | + calls["n"] += 1 |
| 108 | + time.sleep(0.20 if calls["n"] == 1 else 0.01) # call #1 = "compile" |
| 109 | + return calls["n"] |
| 110 | + |
| 111 | + mean, _, times, compile_ms = time_callable(fake_fn, iters=5, warmup=2) |
| 112 | + self.assertGreater(compile_ms, 150.0) # the 200ms first call is captured here... |
| 113 | + self.assertLess(mean, 30.0) # ...and NOT in the steady-state mean |
| 114 | + self.assertEqual(len(times), 5) |
| 115 | + |
| 116 | + |
| 117 | +class OrchestratorTest(unittest.TestCase): |
| 118 | + |
| 119 | + def test_smart_search_picks_measured_winner(self): |
| 120 | + res = grid_search(_MockRingBench(), mode="smart", iters=10, log=lambda *a, **k: None) |
| 121 | + self.assertIsNotNone(res.best) |
| 122 | + self.assertEqual((res.best.bq, res.best.bkv), (9472, 1024)) |
| 123 | + |
| 124 | + def test_oom_configs_pruned_not_raised(self): |
| 125 | + # a tiny VMEM budget OOMs the big pairs; search must still return (or None), never raise. |
| 126 | + res = grid_search( |
| 127 | + _MockRingBench(vmem=8 * 1024 * 1024), |
| 128 | + mode="smart", |
| 129 | + iters=2, |
| 130 | + log=lambda *a, **k: None, |
| 131 | + ) |
| 132 | + self.assertTrue(any(r.status == "oom" for r in res.results)) |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == "__main__": |
| 136 | + unittest.main() |
0 commit comments