Skip to content

Commit 50c7d5c

Browse files
kardysmclaude
andcommitted
fix(strix): compile every kernel copy at start-up
Triton compiles a kernel once per tile config and per class of each integer argument (1, a multiple of 16, other), and the row count picks both. The start-up warm-up ran two lengths only, so the first request whose length was a multiple of 16 in a tile-config range waited for new copies: in the request matrix the first timed pass of S with 1 question (208 tokens) and of M with 2 (720 tokens), on both machines of rkinas#14 / rkinas#21. The warm-up now runs the projections on zero rows of every range and class (one row: the last row chunk of qkv / o, one readout row) and six short forwards for attention and rotary (lengths other / multiple of 16 against readout rows 1 / other / 16). Kernels are unchanged. basal-2.0 on the 8060S, request matrix: the old code compiled 18 copies during the matrix (empty Triton cache: S-q1 pass 1 1.82 s against 0.18 s), now none. Start-up 8.8 s against 7.9 s with a filled Triton cache, 18.7 against 13.8 s at the first start on a machine. test_warmup_compiles_every_kernel_copy: no row count (20-5000 rows, 1-528 readout rows) compiles a kernel after the warm-up; it fails without it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 81b37f7 commit 50c7d5c

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

‎basal/strix.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,51 @@ def __init__(self, model_dir, dtype="bfloat16", warm=True):
294294
for n in (min(300, self.cos.shape[0] - 1), min(1100, self.cos.shape[0] - 1)):
295295
toks = [[1] + list(range(1000, 1000 + n)), [1] + list(range(1000, 1000 + n // 2)) + [7, 8]]
296296
self.run_shared([([[t % V for t in x] for x in toks], [[30 % V, 31 % V]] * 2)])
297+
self._warm_shapes()
298+
299+
# Start-up forwards for attention and rotary (one tile config each): sequence lengths other and a multiple of 16,
300+
# each against readout rows 1, other and a multiple of 16 (the last layer's attention takes both counts)
301+
WARM_FORWARDS = ((17, 1), (32, 1), (17, 3), (32, 3), (17, 16), (32, 16))
302+
303+
@staticmethod
304+
def warm_rows(cfgs):
305+
"""Row counts that cover a (largest row count, config) list: 1 (the last row chunk of qkv / o, one readout
306+
row), then in every range one count that is not a multiple of 16 and one that is."""
307+
rows, lo = [1], 0
308+
for lim, _ in cfgs:
309+
rows += [lo + 17 + ((lo + 17) % 16 == 0), (lo // 16 + 2) * 16]
310+
lo = lim
311+
return rows
312+
313+
def _warm_row(self, L, nq):
314+
"""Inputs of `forward` for one packed row of L tokens and its last nq tokens read out: causal sequences of
315+
at most the context each (positions within the rotary tables), token ids within the vocabulary."""
316+
i = torch.arange(L, device=self.dev)
317+
P = self.cos.shape[0]
318+
ends = torch.clamp((i // P + 1) * P, max=L)
319+
return (i + 1000) % self.embed.shape[0], (i % P).to(torch.int32), ends.to(torch.int32), i[L - nq:]
320+
321+
@torch.no_grad()
322+
def _warm_shapes(self):
323+
"""Compile every kernel copy a request can need. Triton compiles a kernel once per tile config and per class of
324+
each integer argument: 1, a multiple of 16, other. The row count picks both (the config by its range in FFN /
325+
LINEAR, the class by its value), and a request that needs a copy not compiled yet waits for it: ~0.2-0.3 s per
326+
copy on the 8060S, ~1 ms when Triton's disk cache holds it (in the request matrix, the first timed pass of S
327+
with 1 question and of M with 2 did). The projections run on zero rows of warm_rows, as the forward calls them
328+
(same strides, residual for o / down); attention and rotary in the short WARM_FORWARDS."""
329+
z = lambda m, n: torch.zeros(m, n, device=self.dev, dtype=torch.bfloat16) # noqa: E731
330+
_, wqkv, bqkv, wo, bo, _, wg, bg, wu, bu, wd, bd = self.layers[0]
331+
for m in self.warm_rows(self.LINEAR["qkv"]):
332+
self._linear("qkv", z(m, self.hidden), wqkv, bqkv)
333+
for m in self.warm_rows(self.LINEAR["o"]):
334+
self._linear("o", z(m, wo.shape[1]), wo, bo, z(m, self.hidden))
335+
for m in self.warm_rows(self.LINEAR["down"]):
336+
self._linear("down", z(m, wd.shape[1]), wd, bd, z(m, self.hidden))
337+
for m in self.warm_rows(self.FFN):
338+
self._ffn_in(z(m, self.hidden), wg, bg, wu, bu)
339+
for L, nq in self.WARM_FORWARDS:
340+
self.forward(*self._warm_row(L, nq))
341+
self._wait()
297342

298343
def _oversize(self, pack, n):
299344
return len(pack[0]) > self.TOKEN_BUDGET

‎tests/test_strix.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,35 @@ def test_warmup_with_a_small_vocabulary():
235235
assert make(tiny(), warm=True).run_shared(groups_mixed()[:1])
236236

237237

238+
@gpu
239+
def test_warmup_compiles_every_kernel_copy():
240+
"""After the start-up warmup no row count compiles a kernel. Triton compiles one copy per tile config and per class
241+
of each integer argument (1, a multiple of 16, other), and the request that needs a new one waits for it (the first
242+
timed pass of S with 1 question and of M with 2 in the request matrix, before the warmup covered them)."""
243+
from basal import strix
244+
if not hasattr(triton, "knobs"):
245+
pytest.skip("Triton without knobs.runtime.jit_cache_hook")
246+
for k in (strix._attn_fwd, strix._swiglu_mm, strix._linear, strix._rmsnorm, strix._rope):
247+
k.device_caches.clear() # the copies other tests compiled in this process
248+
be = make(tiny(), warm=True)
249+
built = []
250+
251+
def hook(*, repr, **_):
252+
built.append(repr)
253+
return False # compile as usual
254+
255+
old, triton.knobs.runtime.jit_cache_hook = triton.knobs.runtime.jit_cache_hook, hook
256+
try:
257+
for L in (20, 48, 208, 512, 513, 720, 1536, 1537, 2160, 2304, 2305, 2320, 4609, 5000):
258+
for nq in (1, 2, 7, 16, 48, 513, 528):
259+
if nq <= L:
260+
be.forward(*be._warm_row(L, nq))
261+
be._wait()
262+
finally:
263+
triton.knobs.runtime.jit_cache_hook = old
264+
assert built == []
265+
266+
238267
def llama(**kw):
239268
base = dict(hidden_size=2048, intermediate_size=11008, num_attention_heads=16, num_key_value_heads=2,
240269
head_dim=128, num_hidden_layers=1, vocab_size=64) # basal-1.5 / basal-2.0 4.5B shapes

0 commit comments

Comments
 (0)