Repository navigation
Paged attention: opt-in side-by-side pass A (one key/value read per kv head) - #420
Draft
jonathan308 wants to merge 4 commits into
Draft
jonathan308 wants to merge 4 commits into
jonathan308 wants to merge 4 commits into
Conversation
…ass A) When a kv head's query heads split over GQA/HPT pass-A threadgroups (large head dims: the Qwen 3.5 27B's full attention, Gemma 4's global layers), each threadgroup loaded the partition's keys and values. The side-by-side pass A (paged_attention_part_side_impl) puts those head groups next to each other in one threadgroup of up to 512 threads: head group g owns simdgroups [g * NSG, (g + 1) * NSG) and keeps the plain form's token stride, online softmax and simdgroup merge order, so every partial and (m, l) keeps its bits. The merge records go through threadgroup memory HRED heads at a time to stay within 32 KB. Partitions and their boundaries do not change. Decode takes the form from an attended length of 49,152 tokens (DARKBLOOM_CBV2_PAGED_SIDE_BY_SIDE_TOKENS; 0 turns it off), in the direct-slab and the segmented decode. Packed pages, query rotation, masks and verify windows keep the plain form; the plain bodies do not change. Unit/Paged/PagedSideBySideRuleTests checks the sizing, the length rule and the declared buffers. Kernel/Cache/PagedSideBySideKernelTests checks that both decodes give the plain form's output and slab bytes (bfloat16 and float16; 27B, Gemma 4 global and d128/GQA 16 shapes; with and without the fused write and softcap). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each sequence reserves pages for the nominal maximum length, so two 4,096-token rows did not fit the 16 MiB pool (capacityExhausted in CI). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Timed in the engine (BenchCBv2 paged arm, one request, 64 decode steps), the side-by-side form was not faster: on an M5 Ultra 25.1 ms against 24.6 ms per step for the 27B at 65,536 tokens and 9.5 ms against 9.3 ms for Gemma 4 QAT 4-bit at 65,536; the M5 Max runs were within their own noise. The per-layer gains measured with the kernel alone (1.08x and 1.22x at 65,536 tokens) do not carry over to whole steps. The output was the same with the form on and off in every run, so the form stays, off by default: DARKBLOOM_CBV2_PAGED_SIDE_BY_SIDE_TOKENS=<n> uses it from n attended tokens. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
Proposed changes
What was slow
Paged decode splits a kv head's query heads over
GQA / HPTpass-A threadgroups when the head dim is large, so that neither the threadgroup memory nor the per-thread accumulator grows with the full GQA factor (PagedAttentionKernel.headsPerThreadgroup). Two production shapes split: the Qwen 3.5 / 3.8 27B's full-attention layers (head dim 256, GQA 6: 2 threadgroups of 3 heads) and Gemma 4's global layers (head dim 512, GQA 8: 4 threadgroups of 2 heads). Each of those threadgroups loads the same partition's keys and values.docs/engine-v2/kernel-research.mdrecords this trade-off (option C). At long context the repeated read is a large part of the step.What changes
A second pass-A kernel,
paged_attention_part_side_impl, puts the head groups of one kv head next to each other in one threadgroup (512 threads for both shapes above):gowns simdgroups[g * NSG, (g + 1) * NSG)and keeps the plain kernel's token stride, online softmax and simdgroup merge order, so each head's partial and(m, l)has the plain kernel's bits;PTOKand the merge pass do not change;HREDheads at a time to stay within 32 KB (27B: 3; Gemma 4 global: 1);PagedAttentionKernel.sideBySideHeadsPerRoundsizes the form, and it is opt-in:DARKBLOOM_CBV2_PAGED_SIDE_BY_SIDE_TOKENS=<n>takes it from n attended tokens (default0, off), in both the direct-slab decode (PagedAttentionKernel.decode) and the segmented decode (PagedSegmentAttention.decode). The plain kernel bodies and their names do not change. Packed (quantized) pages, query rotation, masks, visible ranges and verify windows keep the plain form.Why opt-in: it keeps the bits everywhere it was checked, but whole decode steps in the engine were not faster (next section), so it does not meet the bar for default-on. It is kept as a bit-exact building block; a later change can turn it on for the chips and lengths where an engine timing shows a gain.
Measurements
Per attention layer, today's kernel time over the side-by-side form (M5 Ultra; the engine's kernel text replayed through
mx.fast.metal_kernelwith the engine's template arguments and dispatch shapes, random bfloat16 pages, in-order and shuffled page tables; bits equal in 180 of 180 checks):Derived whole-step gain (the attention share of a step measured on the same Mac): 27B 1.07x at 131,072 and 1.21x at 262,144 tokens; Gemma 1.06x at 65,536 and 1.28x at 131,072.
In the engine (BenchCBv2 paged arm, one request, one long synthetic prompt, 64 decode steps, output digests over token ids and log-probability bits; local composition of this branch with the bench repairs of #422):
The M5 Ultra is a desktop and its prefill times matched within 0.2%, so its pair is the clean one: about 2% slower with the form. The M5 Max is a laptop under other load; its pairs vary with run order. The per-layer gains above (kernel alone, the plain slab accessor, fixed partitions) do not carry over to whole steps; a likely part of the gap is that the engine's segmented decode sizes partitions for the plain form's threadgroup count. A Gemma run at 131,072 tokens on the M5 Ultra was stopped by the host-memory guard and is not in the table.
Tests
Unit/Paged/PagedSideBySideRuleTests: the sizing of the measured shapes and of d128/GQA 16, the threadgroup memory and thread bounds, the length rule and its environment variable, and the buffers the side-by-side bodies declare.Kernel/Cache/PagedSideBySideKernelTests: the direct-slab decode with the form forced on and off gives the same output and slab bytes (bfloat16 and float16; head dim 256 / GQA 6, 512 / GQA 8, 128 / GQA 16; with and without the fused write and softcap; three rows of different lengths over a shuffled page table), and the segmented decode gives the same output and stored rows over three decode steps.Fork Tests on this head (
62de2fe6, run 43542881370), on a GPU:PagedSideBySideRuleTestspasses;directDecodeKeepsThePlainBitspasses 6 of 6 cases andsegmentedDecodeKeepsThePlainBits2 of 2, so in both decodes the side-by-side pass A gives the plain pass A's output and slab bytes. (The first head's segmented test failed in setup: its pool held one 4,096-token row, not two;62de2fe6sizes it for two 2,048-token rows.)Checklist
pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes (pre-commit is not installed here; the changed Swift files were formatted with the swift-format of Xcode 27.2, the command.pre-commit-config.yamlruns)changelog.d/paged-attention-side-by-side.md,fork.yaml)Security
No encryption, attestation, signing, key or weight-hash code changes. The change is inside the paged decode kernels and their dispatch.
Freshness
mainf41a5f9af307(read 2026-11-14 01:25 UTC). The branch was rebased onto it without conflicts; none ofmain's new changes touches the paged attention files.scripts/check-unit-test-files.shpasses; swift-format (Xcode 27.2) changes none of the changed Swift files.AI usage
accurately describes the code changes.
🤖 Generated with Claude Code