Skip to content

attention: shared-scan BF16 paged decode for Qwen3.8-27B — about 2x decode at 210K context, exact - #335

Merged
AzeezIsh merged 1 commit into
Atlas-Inf:mainfrom
Enntity:q27/shared-scan-upstream
Nov 10, 2026
Merged

AzeezIsh merged 1 commit into
Atlas-Inf:mainfrom
Enntity:q27/shared-scan-upstream

Conversation

@data-angel

Copy link
Copy Markdown
Contributor

Summary

Decode attention over a BF16 KV cache gets a second kernel, attn_shared_scan, behind ATLAS_ATTN_SHARED_SCAN=1. On
Qwen3.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 main at 1f6de550b.

paged_decode_attn runs 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_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 in order.

Results

One GB10, Qwen3.8-27B NVFP4, DFlash2 γ=8, ATLAS_EXACT_VERIFY=1, BF16 KV, temperature 0. The two arms are one build
and differ only in the switch.

Off On
One attention layer at 210K tokens, 8 rows, idle machine 16.9 ms 5.1 ms (168 GB/s of K+V)
Agentic action turn, 210,382 prompt tokens (tok/s) 18.5 38.2
Agentic action turn, 210,882 prompt tokens (tok/s) 15.4 32.9
Thinking-heavy turn, 211,437 prompt tokens (tok/s) 9.5, 10.3 19.8
Serial decode at 211K, disable_mtp (tok/s) 3.2 6.8
Decode step at 210K 364 ms 172–179 ms
Decode step on a short code prompt 87.2 ms 87.2 ms
Decode step on a short thinking prompt 86.4 ms 86.8 ms

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 Done lines, the drivers and the kernel check output
are 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/atlas
c125bf9a8), not on this branch. Six of the seven files in this PR are byte-identical there and the seventh differs
by 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_VERIFY needs every verify row to equal the same position decoded alone. The shared scan keeps that:

  • A row's superblock size is a function of its own length only (max(64, pow2ceil(L) / 32), at most 32
    superblocks), and softmax steps are aligned to absolute positions.
  • Every sum is per query. Positions past a row's length are masked by select, and slots past the group's longest
    row are staged as zeros, so a neighbour's data or uninitialised cache never enters a row's arithmetic.
  • Rows that share blocks are found on the device from the block tables, per superblock. Callers do not change.
  • Value weights enter the V product as two BF16 terms (16 mantissa bits).

The output is not bit-identical to paged_decode_attn: 0.18% of output elements differ, each by one BF16 step. The
switch 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 in
    16..=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 for
    64 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 shape
    fits and attention is not sliding. Everything else falls through to paged_decode_attn_bf16 as before.
  • crates/spark-model/examples/attn_shared_scan_check.rs (gpu-examples): the GPU gate. Each row of a batched
    launch 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:

  • tree verify (ATLAS_DFLASH_TREE),
  • sampled decoding (every end-to-end run here is temperature 0).

With those two checked it could be the default for the qwen3.8-27b target. It does not apply to other targets
today: the kernel is built for head dim 256 and lives in the qwen3.6-27b tree.

Notes for reviewers

  • Env budget. This adds one ATLAS_* name. scripts/check_env_budget.py is already over on main (1119
    against 1091) and reads 1120 here, so that check will be red. I can move the switch to a MODEL.toml field if you
    prefer that to an env name.
  • PDL. The two kernels are launched without the PDL attribute and do not call atlas_pdl_enter(), so they are
    on no PDL list (pdl_target_kernels_wait_exactly_when_listed).
  • Workspace. The partials workspace relies, like the model's other decode scratch, on the scheduler running one
    forward at a time: a scan and its reduce are adjacent on their stream.
  • Side finding, not from this change. At about 210K tokens greedy output depends on which checkpoint the prefill
    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 -- --check
  • ATLAS_SKIP_BUILD=1 CUDARC_CUDA_VERSION=13000 cargo clippy --workspace --tests
  • python3 scripts/check_spdx.py, python3 scripts/check_kernel_shadows.py, typos on the changed files
  • cargo test --workspace --locked --no-fail-fast in CI's environment: 8,563 passed, 1 failed, 281 ignored. The
    failure is hip_kernel_is_the_unmodified_upstream_implementation (spark-model/tests/dflash2_selector_abi.rs),
    which fails on main too: kernels/strix-hip/common/dflash2_candidate_selector.cu no longer has the pinned hash.
    This PR does not touch it. pdl_target_kernels_wait_exactly_when_listed and the two new unit tests pass.
  • ATLAS_TARGET_MODEL='*' cargo test -p atlas-kernels --no-run, then -- --ignored: every gb10 target compiles
    with the new kernel in the qwen3.6-27b tree, and the PTX registry tests pass.
  • Real qwen3.8-27b / nvfp4 build of spark and of the attn_shared_scan_check example.
  • cargo doc --workspace --no-deps fails, as it does on main, on three private intra-doc links
    (qwen4exp_decode_fuse.rs:26, exact_verify.rs:19 and :79). None is in this change.
  • scripts/check_env_budget.py: red, see Notes for reviewers.
  • GPU check and end-to-end turns on this branch: not run. They were run on the fork branch, where six of these
    seven files are byte-identical and shared_scan.rs differs by one doc comment.

On the fork branch (Enntity/atlas c125bf9a8), one GB10:

  • attn_shared_scan_check: every row byte-identical to that position alone; 0.18% of elements one BF16 step from
    the scalar kernel.
  • 8 prompts × thinking on/off, 250 tokens: speculative output identical to serial on 16 of 16 cells alone and 16 of
    16 run eight at a time.
  • Serial against speculative from the same starting state at 211,437 prompt tokens, 250 tokens: identical.
  • The Results table above.

Authorship: AI-authored.

🤖 Generated with Claude Code

`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]>
@data-angel
data-angel requested a review from AzeezIsh as a code owner November 10, 2026 06:01
@AzeezIsh
AzeezIsh merged commit 03f3333 into Atlas-Inf:main Nov 10, 2026
17 of 24 checks passed
@AzeezIsh

Copy link
Copy Markdown
Contributor

Merged after an end-to-end run on one GB10 (sparky). Setup: this head ced3e045ee, a 27B-target build, with #333's DFlash2 γ=8 profile (ATLAS_EXACT_VERIFY=1, 64 seqs, 262K, BF16 KV, prefix caching). The only variable is ATLAS_ATTN_SHARED_SCAN.

GPU gate (attn_shared_scan_check) passes. Every row is byte-identical to serial across the 17 chain bases. Against the scalar kernel, 0.18% of elements differ, by at most one BF16 step. --time at 210K tokens:

1 layer scalar shared scan
1 row 14.9 ms (57.7 GB/s) 5.0 ms (171.9 GB/s)
8 rows 17.0 ms (50.6 GB/s) 5.3 ms (163.0 GB/s)

Exactness (SparkQwen greedy_eq, 8 prompts × 320 tokens, thinking low):

  • Switch off: 8/8 identical to main's serial reference at C1 and C8.
  • Switch on: serial decode with the switch on == DFlash2 with it on, 8/8, at C1, C8 and 64-way (8 × C8), and at C1/C8 behind a 4.4K cached prefix.
  • Off vs on serial: 2/8 identical. As you note, greedy text parts at near-ties, e.g. one prompt at char 421.

Long context (187,105-token prompt, 256 tokens, thinking off; rep 0 cold, reps 1–2 prefix hits):

off on
decode tok/s, cold / warm / warm 16.2 / 16.5 / 16.5 36.5 / 35.7 / 35.6
cold TTFT 343.1 s 343.3 s
warm TTFT 0.70 s 0.73 s / 0.69 s

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:

  1. With the switch off, the 187K reply differs between the cold run and the warm prefix-hit reruns (177 vs 129 tokens). With it on, all three runs give the same text. That fits your side finding about restore-dependent greedy output at long context.
  2. A 254,848-token prompt hit the 600 s --request-timeout after a 591 s cold prefill (finish_reason="timeout" after 5 tokens). The identical rerun then logged Prefix cache hit: 254848 tokens … but no SSM snapshot — recomputing all KV: the timed-out request left only a finish-leaf snapshot past the prompt end. That's worth a look separately. It isn't from this PR.

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: nq-114b-pr335-scan on sparky.

@AzeezIsh

Copy link
Copy Markdown
Contributor

Follow-up on side observation 2 (a 254K request truncated by the deadline, after which the identical rerun logged no SSM snapshot — recomputing all KV): I couldn't reproduce it on main 1f6de55 with the same 27B DFlash2 profile. Prefix reuse held in four controlled runs: a ~40K request truncated by a per-request timeout; a cold 260K request (no deadline) on a fresh server and again after filling the Marconi pool; and a 260K request truncated by a 640 s deadline after pool pressure. Every rerun restored from the last intermediate checkpoint (260,288 tokens) with ~2.2 s TTFT, against 617 s cold (jobs 126, 127, 129b, 130 on sparky). The original miss came 2 s after the last checkpoint, at decode token 5; that's the one difference I didn't recreate. Treat it as unconfirmed.

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