Repository navigation
perf(cuda): tree attention folds whole key groups in their programs (drafted rounds -5% at 90k, -9..-10% at 242k; drafts equal serial) - #268
Closed
Arminova wants to merge 1 commit into
Conversation
…16-row windows -16..-18% from 32k keys) A 16-row verify window writes and re-reads an fp32 partial a 512-key chunk (16 rows x 24 heads x 256), about a third of the traffic past the cache itself. Every GROUP (4) chunks now form a group by absolute key position: a group the committed keys fill can fold its chunks in one program (each chunk's partial as before, folded in order with the merge's own step) and write one partial. The chunks after the folded groups are written one by one, and the merge folds them group by group with that same step, so a row folds the same partials in the same order however its keys split between committed rows and its path: drafts equal serial. - groups(p, w): fold in their programs only once groups x rows >= MIN_GROUPED (64), so short contexts keep their chunk-wide launches; the kernels read the folded count back from the stream's slot count, so plans keep their layout. Either way gives the same bits. - slots(p, w) replaces ceil((p + w) / CHUNK) in the 27B's graph refresh and Qwen3.6's MTP refresh. - The tail is unchanged (512-key chunks), so a row still re-reads at most one partial chunk. - Outputs move by float rounding against 0.6.2 (the merge folds by group); over 99.9% of values are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Oct 2, 2026
Closed
Closed
Owner
|
Thank you, @Arminova. Folding whole key groups in the tree attention programs is in TensorFold 0.6.4, under your name with small changes to fit main: https://github.com/ashhart/TensorFold/releases/tag/v0.6.4 Drafted rounds ran about 3.5% faster at 90k tokens in our run. Long replies can now differ from 0.6.3 in the last bits, because the fp32 sums run in a different order at the same precision; the release notes say so. On a DGX Spark the combined Flash Next stack matched one-token decoding in 12 of 12 cells. Thank you for the careful work. |
8 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The 27B's tree attention writes an fp32 partial a 512-key chunk and the merge reads them all back. For a 16-row verify window that is 16 rows × 24 heads × 256 × 4 bytes a chunk, roughly a third of the traffic beyond the cache itself. On one GB10 the kernel runs at about 1.05× the cache-read floor for one row but about 1.5× for 16 rows. This PR has whole key groups write one partial instead of four. Drafted rounds get 5% faster at 90k and 9–10% faster at 242k, short contexts are unchanged, and drafts still equal serial.
Design
GROUP= 4 chunks (SPAN= 2,048 keys) form a group. A group that a stream's committed keys fill can fold its four chunk partials inside one_sharedprogram (each chunk computed as before) and write one partial._fold, the merge's own step, used in the kernel and in_merge. The chunks after the folded groups are written one by one, and_mergefolds them group by group with the same step, then folds each group into the total.groups(p, w)folds them only once groups × window rows ≥MIN_GROUPED(64). Folding at short contexts leaves too few programs: 2k keys ran 1.35–1.8× slower when every group folded.slots(p, w), which replacesceil((p + w) / CHUNK)in the 27B's graph refresh and Qwen3.6's MTP refresh), so plans keep their layout.Measured on one DGX Spark (GB10)
Tree attention alone (24 q heads / 4 KV heads, head dim 256, median of 50, shipped kernel vs this branch in one process), ms a layer:
End to end through
tensorfold serve,z-lab/Qwen3.8-27B-DFlash2,--parallel 3 --thinking --prefill-fp8, on two public checkpoints:Vontra/Qwen3.8-27B-MLX-4bitandarmin1/Qwen3.8-27B-NVFP4-FP8-Mixed-LH(ModelOpt NVFP4/FP8 mixed,--precision full). Round time = decode seconds over rounds from/health. Prompts are seeded non-repeating log lines plus one 512-token code request, greedy.ms a drafted round:
"draft": false(one row a round) is unchanged up to 90k and 2% faster beyond.Short contexts, alternating 0.6.2 and this PR twice on the MLX checkpoint (one stream, greedy, thinking off), tok/s: counting 140.3 / 144.7 against 144.7 / 144.6, code 77.4 / 78.2 against 78.3 / 78.4, prose 50.1 / 50.0 against 50.0 / 49.8.
Tests
tests/cuda/test_attention.py:MIN_GROUPED0 (fold every group in its program), the default, and never;slots/groupsarithmetic;slots.tests/cuda/test_qwen27_prefill.py: after a 9,000-token prompt (four groups), each row of a 16-row draft tree equals serial decoding along its path. The tree folds the groups in their programs and a lone row in the merge.On one GB10, the attention, prefill-attention, all
test_qwen27_*,test_qwen36_*, CLI, geometry and KV-room suites pass (444 passed, 20 skipped).Notes
--kv-dtype int8for the 27B), which edits the same kernel. If both land, the int8 path takes the same group fold in a short follow-up.groups(p, w)at 0 (no folding there), which changes no bits.GROUP= 4 andMIN_GROUPED= 64 are the values measured on GB10. Other GPUs may prefer others, and either changes no bits.