Skip to content

Commit 96c0b0e

Browse files
devopsfortunatoashhart
authored andcommitted
perf: Flash Next CUDA prefill runs the head on the final chunk only
From #40, which made Flash Next prefill on CUDA about 7x faster than v0.3.4.1, byte-exact. There, prompts prefilled through the decode kernels in 64-row chunks at 128-145 tok/s on one DGX Spark, and most of that time was work whose results were never used: - expert kernels: a member tile with no rows returns before reading its expert, and member tiles are the fastest grid axis, so one expert's tiles share its weights in L2 - attention: partials and grid sized for the 2,051 keys a QSA row can read instead of the capacity; programs past a row's keys or complete blocks return - prefill computes the head only on the prompt's last chunk - 1,024-row prefill chunks; above 64 rows the distinct experts are grouped by a stable sort - member-list widths above 16 rows rounded up to a power of two, and the prefill kernels compiled at start Against v0.3.4.1 on one Spark at a 131,072-token capacity: 8k prompts 142 -> 1,008 tok/s, 32k 132 -> 993, 120k ~123 -> 914; through the server, a 7.7k-token prompt's first token after 7.9 s instead of 53.6 s. 0.3.5 reworked this path: 2,048-row prompt chunks, shared CUDA expert and matmul prefill kernels that launch only populated member tiles, attention partials capped at the keys a row reads, and expert grouping on the device. This commit keeps the change that still lands on it: the head runs on the final chunk alone. Its test checks that any chunking of a prompt past the sparse attention budget gives the one-chunk prefill's first token and state bit for bit. The start-up compile is ported separately for the current prompt kernels.
1 parent 6ed268b commit 96c0b0e

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎src/tensorfold/families/qwen4_exp/cuda/decode.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,11 @@ def prefill(e: Engine, prompt: Sequence[int], sampling: Sampling | None, *, mtp:
262262
for start in range(begin, len(prompt), e.prefill_rows):
263263
chunk = list(prompt[start:start + e.prefill_rows])
264264
R = len(chunk)
265-
last = forward(w, st, pb, chunk).clone()
265+
final = start + R >= len(prompt)
266+
# only the prompt's last row is sampled: the head runs on the final chunk alone
267+
logits = forward(w, st, pb, chunk, logits=final)
268+
if final:
269+
last = logits.clone()
266270
streams_last = pb.streams[R - 1:R].clone()
267271
if use_mtp:
268272
nxt = list(prompt[start + 1:start + R + 1])

‎tests/cuda/test_flashnext_forward.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,40 @@ def ask(prompt, **kw):
323323
ask([1500, 9, 10]) # an unrelated prompt: nothing to resume from
324324
cold, cold_stats = ask(prompt)
325325
assert cold_stats["cached"] == 0 and cold == warm, extend
326+
327+
328+
def _prefill_state(e: Engine) -> dict:
329+
st = e.st
330+
n, m = st.pos, st.mtp_len
331+
out = {"streams": e.last_streams, "rec": st.rec[st.cur[0]], "conv": st.conv,
332+
"mtp_kc": st.mtp_kc[:m], "mtp_vc": st.mtp_vc[:m], "mtp_ikc": st.mtp_ikc[:m]}
333+
for i in range(len(st.kc)):
334+
out[f"kc{i}"], out[f"vc{i}"], out[f"ikc{i}"] = st.kc[i][:n], st.vc[i][:n], st.ikc[i][:n]
335+
out[f"pooled{i}"] = st.pooled[i][:n // 4]
336+
return {k: v.clone() for k, v in out.items()}
337+
338+
339+
@pytest.mark.parametrize("sampling", [None, Sampling(seed=17, top_k=20, top_p=0.95)])
340+
def test_prefill_head_on_the_final_chunk_keeps_every_bit(monkeypatch, sampling):
341+
"""Past the sparse attention budget, any chunking with the head on the final chunk alone gives the one-chunk prefill's first token and whole state."""
342+
343+
from tensorfold.families.qwen4_exp.cuda import decode
344+
345+
w = _model(7)
346+
g = torch.Generator().manual_seed(8)
347+
prompt = torch.randint(1, V, (2600,), generator=g).tolist()
348+
one = Engine(w, capacity=4096, max_rows=8, prefill_rows=len(prompt))
349+
first = prefill(one, prompt, sampling)
350+
want = _prefill_state(one)
351+
del one
352+
heads = []
353+
real = decode.forward
354+
monkeypatch.setattr(decode, "forward", lambda *a, **kw: (heads.append(kw.get("logits", True)), real(*a, **kw))[1])
355+
for rows in (64, 256, 1024):
356+
heads.clear()
357+
e = Engine(w, capacity=4096, max_rows=8, prefill_rows=rows)
358+
assert prefill(e, prompt, sampling) == first
359+
got = _prefill_state(e)
360+
assert [k for k in want if not torch.equal(want[k], got[k])] == [], rows
361+
assert heads == [False] * (-(-len(prompt) // rows) - 1) + [True], rows
362+
del e

0 commit comments

Comments
 (0)