Skip to content

attention: shared-scan BF16 paged decode (ATLAS_ATTN_SHARED_SCAN) - #20

Merged
data-angel merged 1 commit into
q27/concfrom
q27/shared-scan
Nov 10, 2026
Merged

data-angel merged 1 commit into
q27/concfrom
q27/shared-scan

Conversation

@data-angel

Copy link
Copy Markdown

What

A second BF16 paged decode attention path for the qwen3.6-27b kernel tree (so Qwen3.6-27B and Qwen3.8-27B), behind ATLAS_ATTN_SHARED_SCAN=1. Off by default; with it off nothing changes.

Why

paged_decode_attn runs one CTA per (query head, row), and each one 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, at roughly 50 GB/s on a machine that streams weights at about 200 GB/s.

How

  • attn_shared_scan (kernels/gb10/qwen3.6-27b/nvfp4/attn_shared_scan.cu): a CTA owns one KV head over one superblock of positions, reads those keys and values once, and scores every query that attends them (the KV head's query heads, for every row that shares the blocks) with m16n8k16. attn_shared_scan_reduce folds a row's superblock partials in order.
  • Rows that share blocks are found on the device from the block tables, per superblock, so no caller describes groups and no metadata changed.
  • Row exactness for ATLAS_EXACT_VERIFY: superblocks and 16-position steps are cut by the row's own length at absolute alignment, every sum is per query, masked positions are masked by select, and slots past the group's longest row are staged as zeros. The kernel header has the full argument.
  • Value weights enter the V product as two BF16 terms (hi + lo, 16 mantissa bits).
  • ops/shared_scan.rs holds the switch, the shape test and the launch pair; the hook is one arm in run_paged_decode.rs.

Checks

examples/attn_shared_scan_check.rs (GPU): each row of a batched launch byte-identical to that position run alone with every later slot NaN, for 16 rows, 8 rows, a 3-row window, rows interleaved with another sequence and reversed order, at chain starts around step, superblock and size-class boundaries; a changed value at an attended position changes exactly the rows that attend it. Against the scalar kernel 0.18% of output elements differ, each by one BF16 step.

Measured on one DGX Spark, Qwen3.8-27B NVFP4 with DFlash2 and exact verify:

off on
one layer at 210K tokens, 8 rows 16.9 ms 5.1 ms
agentic turn at 210K, greedy (action) 18.5 tok/s 38.2 tok/s
agentic turn at 210K, greedy (action) 15.4 tok/s 32.9 tok/s
agentic turn at 210K, greedy (thinking) 9.5 tok/s 19.8 tok/s
serial decode at 211K 3.2 tok/s 6.8 tok/s
short prompt, step (code / thinking) 87.2 / 86.4 ms 87.2 / 86.8 ms

With the switch on: speculative and 8-way concurrent output identical to serial on 16/16 short cells each, and serial equal to speculative over 250 tokens at 211K from the same starting state.

The end-to-end numbers were taken on this change over 4594757; the branch is rebased onto 7fbbd2b (prefill checkpoint cadence, no decode change), rebuilt, and the GPU check rerun there.

Local gates: cargo clippy --workspace --tests and the example with cuda,gpu-examples clean, the two new unit tests pass, license headers and typos clean. cargo fmt --check reports ten files that are already unformatted on q27/conc; none are in this change.

Not covered

  • Tree verify (ATLAS_DFLASH_TREE) with the switch on, end to end.
  • Sampled decoding end to end (all runs above are greedy).
  • No product profile sets the switch yet.

🤖 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

Not covered end to end: tree verify with the switch on, sampled decoding.

Co-Authored-By: Claude <[email protected]>
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.

1 participant