Skip to content

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
ashhart:mainfrom
Arminova:perf/27b-tree-attention-groups
Closed

Arminova wants to merge 1 commit into
ashhart:mainfrom
Arminova:perf/27b-tree-attention-groups

Conversation

@Arminova

@Arminova Arminova commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

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

  • Groups by absolute key position. Every 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 _shared program (each chunk computed as before) and write one partial.
  • One fold step, shared. The fold is _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 _merge folds them group by group with the same step, then folds each group into the total.
    • So a group gives the same bits whether one program folded it or the merge did.
    • A drafted row (its path fills the group) and the same row alone (the group committed) therefore fold the same partials in the same order: drafts equal serial.
  • A free policy. Because both paths give the same bits, which groups fold in-kernel is a free choice. 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.
    • The kernels read the folded count back from the stream's slot count (slots(p, w), which replaces ceil((p + w) / CHUNK) in the 27B's graph refresh and Qwen3.6's MTP refresh), so plans keep their layout.
  • The tail is unchanged (512-key chunks), so a row still re-reads at most one partial chunk. This is complementary to perf(cuda): the tree attention's tail stops at the tile holding the row's last key (same bits) #227, which bounds the tail's loop.
  • Bits against 0.6.2. Folding by group changes float rounding: over 99.9% of output values are unchanged and the largest difference is 1.2e-4. In the runs below, every one of the 100 greedy task answers is identical to 0.6.2's on both checkpoints.

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:

Keys 16 rows, 0.6.2 this PR 1 row, 0.6.2 this PR
2,048 0.110 0.111 1.01× 0.065 0.066
8,192 0.260 0.248 0.95× 0.175 0.176
32,768 0.875 0.733 0.84× 0.584 0.594
90,000 2.329 1.924 0.83× 1.666 1.682
131,072 3.334 2.744 0.82× 2.427 2.341
180,000 4.578 3.756 0.82× 3.353 3.221

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-4bit and armin1/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:

Prompt tokens MLX 4-bit, 0.6.2 this PR NVFP4/FP8 mixed, 0.6.2 this PR
2,880 82.9 82.8 106.1 106.0
45,150 102.2 99.4 (−3%) 125.4 121.0 (−4%)
90,250 121.6 115.0 (−5%) 144.9 138.3 (−5%)
180,470 159.9 146.8 (−8%) 184.0 171.2 (−7%)
242,400 188.6 170.1 (−10%) 213.1 192.9 (−9%)
  • "draft": false (one row a round) is unchanged up to 90k and 2% faster beyond.
  • Prompt fill is unchanged.
  • On both checkpoints, the 100-task suite (GSM8K, JSON, executed code, instructions) gives the same answers as 0.6.2, byte for byte. The needle at 194,893 tokens is correct.

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:

  • rows of chains whose keys end just before, at and after a group's edge equal their serial bits (9 shapes, 1 to 128 rows), each under MIN_GROUPED 0 (fold every group in its program), the default, and never;
  • streams that fold groups beside streams that don't, in one launch, equal each alone;
  • a padded (graph) plan folding groups equals the exact plan;
  • grouped attention matches the torch reference past several groups;
  • slots / groups arithmetic;
  • the padded-plan test now refreshes 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

…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]>
@ashhart

ashhart commented Oct 3, 2026

Copy link
Copy Markdown
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.

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