Repository navigation
Conversation
…ow's last key (same bits)
2269a70 to
62f30fc
Compare
|
Rebased onto 0.6.4 ( The only conflict was in On one DGX Spark (GB10), on a tree of 0.6.4 plus #190's commit plus this change (#190 changes only which prompt states
A two-stream replay does not resolve the one-stream saving in the description (0.997x). The description's kernel-alone Submitted by an agent from Blockbrain Labs (https://x.com/blockbrain_labs, https://github.com/blockbrain-ai). |
|
Thank you, @nood-co1. Stopping each row's tail at the tile that holds its last key is a tidy change, and checking every output bit for bit against the released kernel made it easy to trust. The Python engine is frozen (#286) and new work goes into the Zig engine, so this PR won't merge. The Zig engine's Metal tree attention already ends each row's tail loop at its last key, so that path behaves this way today. Zig CUDA runs Nemotron from the command line only, and its attention kernel there still walks every tile of a chunk, including tiles past a row's last key. When Zig CUDA serving comes, your bound is the change to make there, with your credit. |
Summary
_tailruns one program per (window row, KV head, tail chunk) over every 64-key tile of its chunk: the stream's lastcommitted keys, the row's root-to-row path, then tiles holding none of the row's keys (in the first tail chunk, the
tiles past the path; in a second tail chunk the path does not reach, all of them). Such a tile changes nothing: every
score is masked, so
_tileleavesmandlas they are and adds zero too.This PR stops the loop at the tile holding the row's last key (
p + depth - 1). The outputs are the released kernel's,bit for bit, up to the sign of an exact zero in
o(o * 1.0 + 0.0turns-0.0into+0.0).Measured
One DGX Spark (GB10, Triton 3.7.1, torch 2.13.0a0 NV 26.07), Qwen3.8-27B MLX 4-bit with DFlash2, 24 query heads, 4 KV
heads, head dim 256.
The kernel alone,
attention()a layer (median of 50 after 5 warm-ups, mean of two passes):The 0.6.1 column runs that kernel's arithmetic, launch for launch, from a test-only copy whose Python wrapper does a
little more per call; timed beside the released module in an earlier run (two passes at 16 and at 24 rows), the copy
took 1.53-2.08% longer at 2,048 keys, 0.41-1.46% at 8,192 and -0.02% to +0.64% at 32,768. The saving does not grow with
context: it is the tail's skipped tiles.
Inside the engine on the same GPU, in-process (
App.runas the server calls it), one stream with 16-row trees, thisbranch against 0.6.1's kernel on a sample of 55 recorded agent requests (our own DeepSeek Harness sessions on public
repositories): every reply is identical by
token_sha, with the same 7,084 rounds; decode 656.35 s against 658.44 s(0.9968x), per request a round 0.9964x on average (0.9971x median, 43 of 55 faster). After 32,000, 64,000 and
120,000-token prompts with 1,024 forced tokens the replies are identical; the second of two runs took 0.996x, 0.994x and
0.997x 0.6.1's time.
Change
cuda/kernels/attention.py,_tail:tiles = min(max(ceil((p + depth - chunk * CH) / 64), 0), CH // 64)and theloop runs
range(tiles)(range(CH // 64)before). A chunk the row's path does not reach runs no tile and stores anempty partial (
m = -inf,l = 0), which_mergealready skips.depthis the row's own (_paths), so a row'stiles depend on its own key range only, as before.
tests/cuda/test_attention.py:test_each_row_reaches_its_own_key_at_tile_edges: chains whose rows end just before,at and after a 64-key tile's edge, and paths of 128 keys across the 512-key chunk edge, each row's own key set to
dominate its scores, against the torch reference. With the bound rounded down (one tile short) all 18 cases fail; with
this PR's bound they pass.
Tests
On the GB10, this branch:
tests/cuda/test_attention.py,test_qwen27_forward.py,test_qwen27_prompt_end_cache.py,test_qwen27_multi.py,test_qwen27_kv_room.py,test_qwen27_shared_kv.py,test_qwen27_checkpoint_slots.pyandtest_qwen36_moe.py(its MTP and CUDA-graph paths use this kernel): 196 passed, 1 deselected(
test_constrained_streams_together_equal_solo_and_serialneedsxgrammar, which our image lacks). Both kernels passthe attention tests and further cases (rows ending at the 512-key chunk edges with paths of 1, 2 and 128 keys, binary
trees of 16 rows, four streams at 8,192 and 32,768 keys, a padded plan replayed in a CUDA graph); in the edge, tree and
stream cases each output of 0.6.1's kernel equals this branch's bit for bit. The error against an fp32 reference is the
same for both on every input tried. Not run: other GPUs, and families other than Qwen3.8 dense and the MoE family
above.
Submitted by an agent from Blockbrain Labs (https://x.com/blockbrain_labs, https://github.com/blockbrain-ai).