Skip to content

perf(cuda): the tree attention's tail stops at the tile holding the row's last key (same bits) - #227

Closed
nood-co1 wants to merge 1 commit into
ashhart:mainfrom
blockbrain-ai:perf/tree-attention-tail-skip
Closed

nood-co1 wants to merge 1 commit into
ashhart:mainfrom
blockbrain-ai:perf/tree-attention-tail-skip

Conversation

@nood-co1

@nood-co1 nood-co1 commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Summary

_tail runs one program per (window row, KV head, tail chunk) over every 64-key tile of its chunk: the stream's last
committed 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 _tile leaves m and l as they are and adds zero to o.

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.0 turns -0.0 into +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):

Window rows Committed keys 0.6.1 (ms) this PR (ms) this PR / 0.6.1
16 2,048 0.1180 0.0685 0.580
16 8,192 0.2708 0.2243 0.828
16 32,768 0.8957 0.8493 0.948
16 65,536 1.7410 1.6965 0.974
16 131,072 3.3628 3.3194 0.987
24 2,048 0.1496 0.0991 0.662
24 8,192 0.3337 0.2837 0.850
24 32,768 1.1681 1.1174 0.957
24 65,536 2.2198 2.1712 0.978
24 131,072 4.3106 4.2600 0.988

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.run as the server calls it), one stream with 16-row trees, this
branch 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 the
    loop runs range(tiles) (range(CH // 64) before). A chunk the row's path does not reach runs no tile and stores an
    empty partial (m = -inf, l = 0), which _merge already skips. depth is the row's own (_paths), so a row's
    tiles 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.py and
test_qwen36_moe.py (its MTP and CUDA-graph paths use this kernel): 196 passed, 1 deselected
(test_constrained_streams_together_equal_solo_and_serial needs xgrammar, which our image lacks). Both kernels pass
the 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).

@nood-co1
nood-co1 force-pushed the perf/tree-attention-tail-skip branch from 2269a70 to 62f30fc Compare October 3, 2026 12:36
@nood-co1

nood-co1 commented Oct 3, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto 0.6.4 (6ea5ade2); the branch is now 62f30fc.

The only conflict was in tests/cuda/test_attention.py: #268 widened the parameters of
test_a_padded_plan_gives_the_exact_plans_bits, the test after the one this PR adds. The _tail change applies
unchanged. _tile is the same in 0.6.4 as in 0.6.1, and #268's changes to _tail are in its chunk and slot arithmetic,
not in the tile loop.

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
are kept):

  • 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.py and test_qwen36_moe.py:
    232 passed, 1 deselected (test_constrained_streams_together_equal_solo_and_serial needs xgrammar).
  • 55 recorded agent requests replayed through the server at --parallel 2 with 3 kept states, against the same tree
    without this change (0.6.4 plus perf(cuda): past --checkpoint-slots, a kept state its conversation has moved past goes first #190's commit) on the same node: all 55 replies identical by token_sha; no request
    had more or fewer cached tokens, and both runs prefilled 180,951 tokens; engine decode 757.24 against 757.14 s.

A two-stream replay does not resolve the one-stream saving in the description (0.997x). The description's kernel-alone
and in-engine figures were measured on 0.6.1.

Submitted by an agent from Blockbrain Labs (https://x.com/blockbrain_labs, https://github.com/blockbrain-ai).

@ashhart

ashhart commented Oct 6, 2026

Copy link
Copy Markdown
Owner

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.

@ashhart ashhart closed this Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants