Repository navigation
attention: shared-scan BF16 paged decode for Qwen3.8-27B — about 2x decode at 210K context, exact - #335
Conversation
`paged_decode_attn` runs one CTA per (query head, row) and each walks its KV head's whole K and V in scalar FP32. On Qwen3.8-27B (24 query heads over 4 KV heads) an 8-row speculative verify fetches every cached key and value 48 times a layer; at 210K tokens that scan is about 270 ms of a 365 ms decode step. `attn_shared_scan` reads each KV head once for the rows that share its blocks and scores every query on tensor cores; `attn_shared_scan_reduce` folds the per-superblock partials. Rows that share blocks are found on the device from the block tables, so no caller changes. Positions are cut by the row's own length and every sum is per query, so a row's bytes do not depend on the rows launched beside it, which is what ATLAS_EXACT_VERIFY needs. Value weights enter the V product as two BF16 terms (16 mantissa bits). Off by default. With ATLAS_ATTN_SHARED_SCAN=1 the BF16 decode arm takes the shared scan for serial decode and every verify width (qwen3.6-27b kernel tree: head dim 256, block size 16..=256). `examples/attn_shared_scan_check.rs` gates it on the GPU: each row of a batched launch byte-identical to that position run alone with every later slot NaN, across step, superblock and size-class boundaries; 0.18% of output elements differ from the scalar kernel, each by one BF16 step. Measured on one DGX Spark, Qwen3.8-27B with DFlash2 and exact verify: - one layer at 210K tokens, 8 rows: 16.9 ms -> 5.1 ms (168 GB/s of K+V) - real agentic turns at 210K, greedy: 18.5 / 15.4 / 9.5 tok/s -> 38.2 / 32.9 / 19.8 tok/s; serial decode 3.2 -> 6.8 tok/s - short prompts: step 87.2 ms either way (code), 86.4 -> 86.8 ms (thinking) - speculative and 8-way concurrent output identical to serial on 16/16 short cells, and serial equals speculative over 250 tokens at 211K The two kernels are launched without the PDL attribute and do not call atlas_pdl_enter(), so they are on no PDL list. Not covered end to end: tree verify with the switch on, sampled decoding. Co-Authored-By: Claude <[email protected]>
|
Merged after an end-to-end run on one GB10 (sparky). Setup: this head GPU gate (
Exactness (SparkQwen
Long context (187,105-token prompt, 256 tokens, thinking off; rep 0 cold, reps 1–2 prefix hits):
Short context: decode-floor (MinHeap, 1500 cap) is 55.4 tok/s off vs 57.9 on. The texts differ (1067 vs 1096 tokens), so that's level, not a gain. 0 ERROR lines in all three serves. Two side observations:
The red CI jobs here predate #339. The stale selector pin and the rustdoc links are fixed on main now; the env budget, cargo-deny, the benchmark gate and #333's LoC files remain red, as on main. Receipts: |
|
Follow-up on side observation 2 (a 254K request truncated by the deadline, after which the identical rerun logged |
Summary
Decode attention over a BF16 KV cache gets a second kernel,
attn_shared_scan, behindATLAS_ATTN_SHARED_SCAN=1. OnQwen3.8-27B with DFlash2 and exact verify it roughly doubles decode at long context and leaves short prompts level.
It follows #333 and is one commit on
mainat1f6de550b.paged_decode_attnruns one CTA per (query head, row), and each walks its KV head's whole K and V in scalar FP32.Qwen3.8-27B has 24 query heads over 4 KV heads, so an 8-row speculative verify fetches every cached key and value 48
times a layer. At 210K tokens that scan is about 270 ms of a 365 ms decode step, around 50 GB/s on a machine that
streams weights at about 200 GB/s.
attn_shared_scanreads each KV head once for the rows that share its blocks and scores every query on tensor cores.attn_shared_scan_reducefolds the per-superblock partials in order.Results
One GB10, Qwen3.8-27B NVFP4, DFlash2 γ=8,
ATLAS_EXACT_VERIFY=1, BF16 KV, temperature 0. The two arms are one buildand differ only in the switch.
disable_mtp(tok/s)The published check output was captured beside a live server and reads 18.2 ms and 5.7 ms for that layer; the idle
figures in the table are from an earlier run of the same check and are not in the receipts.
Below about 500 cached tokens a layer costs 0.02 ms more than the scalar kernel (about 0.3 ms a step), and from
1,000 tokens up it is faster. Greedy text can part ways between the arms at a near tie, because the change reorders
floating-point sums; compare step times across arms, not tok/s.
The turns are the last requests of a real agentic coding session, replayed so each one is a prefix-cache hit with a
few hundred to 1,300 new tokens. The session is private, so its texts are replaced by their SHA-256. Request rows, the server's own
Donelines, the drivers and the kernel check outputare in Enntity/sparkqwen
results/2026-11-10-27b-shared-scan.Where the numbers were measured. They were measured on our fork's Qwen3.8-27B branch (
Enntity/atlasc125bf9a8), not on this branch. Six of the seven files in this PR are byte-identical there and the seventh differsby one doc comment. On this branch I ran the build and the checks under Testing; I did not re-run the GPU check or the
end-to-end turns on it.
Why a row's bytes do not depend on its neighbours
ATLAS_EXACT_VERIFYneeds every verify row to equal the same position decoded alone. The shared scan keeps that:max(64, pow2ceil(L) / 32), at most 32superblocks), and softmax steps are aligned to absolute positions.
row are staged as zeros, so a neighbour's data or uninitialised cache never enters a row's arithmetic.
The output is not bit-identical to
paged_decode_attn: 0.18% of output elements differ, each by one BF16 step. Theswitch and the shape test are fixed for the life of the process, so serial decode and every verify width take the
same arithmetic.
What's in it
kernels/gb10/qwen3.6-27b/nvfp4/attn_shared_scan.cu: the two kernels. Head dim 256, block size a power of two in16..=256, up to 48 query heads per KV head.
crates/spark-model/src/layers/ops/shared_scan.rs: the switch, the shape test, the partials workspace (48 MiB for64 rows, never freed or moved because a captured graph holds its pointer) and the two launches.
run_paged_decode.rs: the BF16 arm takes the shared scan when the switch is on, the kernels are loaded, the shapefits and attention is not sliding. Everything else falls through to
paged_decode_attn_bf16as before.crates/spark-model/examples/attn_shared_scan_check.rs(gpu-examples): the GPU gate. Each row of a batchedlaunch must be byte-identical to that position run alone with every later slot set to NaN, across step, superblock
and size-class boundaries, in 16-row, 8-row, offset, interleaved and reversed launches. It also has a control that
a V change is seen, a closeness bound against the scalar kernel, and
--time.Opt-in, and what would make it the default
Off by default; nothing changes without the switch. Per CONTRIBUTING's rule for opt-in arms: it wins at long context
and is level at short context. I left it off because two paths are not covered end to end with it on:
ATLAS_DFLASH_TREE),With those two checked it could be the default for the
qwen3.8-27btarget. It does not apply to other targetstoday: the kernel is built for head dim 256 and lives in the
qwen3.6-27btree.Notes for reviewers
ATLAS_*name.scripts/check_env_budget.pyis already over onmain(1119against 1091) and reads 1120 here, so that check will be red. I can move the switch to a
MODEL.tomlfield if youprefer that to an env name.
atlas_pdl_enter(), so they areon no PDL list (
pdl_target_kernels_wait_exactly_when_listed).forward at a time: a scan and its reduce are adjacent on their stream.
restored from (the same turn differs after 187 reasoning characters between a restore from the previous turn's
checkpoint and from its own). It is the same with the switch off.
Testing
On this branch (Rust and kernel steps in the GB10 release builder image, CPU only; no GPU run):
cargo fmt --all -- --checkATLAS_SKIP_BUILD=1 CUDARC_CUDA_VERSION=13000 cargo clippy --workspace --testspython3 scripts/check_spdx.py,python3 scripts/check_kernel_shadows.py,typoson the changed filescargo test --workspace --locked --no-fail-fastin CI's environment: 8,563 passed, 1 failed, 281 ignored. Thefailure is
hip_kernel_is_the_unmodified_upstream_implementation(spark-model/tests/dflash2_selector_abi.rs),which fails on
maintoo:kernels/strix-hip/common/dflash2_candidate_selector.cuno longer has the pinned hash.This PR does not touch it.
pdl_target_kernels_wait_exactly_when_listedand the two new unit tests pass.ATLAS_TARGET_MODEL='*' cargo test -p atlas-kernels --no-run, then-- --ignored: every gb10 target compileswith the new kernel in the
qwen3.6-27btree, and the PTX registry tests pass.qwen3.8-27b/nvfp4build ofsparkand of theattn_shared_scan_checkexample.cargo doc --workspace --no-depsfails, as it does onmain, on three private intra-doc links(
qwen4exp_decode_fuse.rs:26,exact_verify.rs:19and:79). None is in this change.scripts/check_env_budget.py: red, see Notes for reviewers.seven files are byte-identical and
shared_scan.rsdiffers by one doc comment.On the fork branch (
Enntity/atlasc125bf9a8), one GB10:attn_shared_scan_check: every row byte-identical to that position alone; 0.18% of elements one BF16 step fromthe scalar kernel.
16 run eight at a time.
Authorship: AI-authored.
🤖 Generated with Claude Code