This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/docs/QSA.md
T
Deadcode 4a61d7296b radiance 1.14.0
New models and formats:
- libr3d: a kernel library for RDNA3 (gfx11) cards
- nomic-embed-text-v2-moe (the nomicbert plugin)
- Gemma 4 E2B with its per-layer MLP width, and Google's unmodified E2B/E4B directories in bf16
- DeepSeek-V4.1-Flash converted straight from its release: the Engram tables, the fused imatrix and
  rad-convert --chat-template for a release that ships no template
- GGUF Q8_0, Q4_K and Q6_K tensors import as their planes; rad-convert writes them without a recipe
- Qwen3.8-Flash-Next-FP8 converts and serves through qwen4exp_fp8

Speed:
- hc_read's decode kernels as one grid (34.7 -> 25.2 us at 4 rows) and its tiled up-mix pipelined
- gemm_fp8a8: the narrow form's scales staged through LDS (Flash-Next int8 at 32 rows 83.6 -> 68.3
  us), the gated three-tile form at 8 waves (gate_up at 48 rows 272.9 -> 180.6 us)
- Qwen3.8-27B: the delta net's out-projection read beside its decode recurrent update (x1 19.40 ->
  19.10 ms a step), and the verify's split-KV attention sized by context depth (x3 at 180K 42.4 ->
  40.3 ms a step)
- Hy-Memory (HYV3) decode: fused QK norm, RoPE and KV store, residual adds folded into the norms,
  coalesced one-token expert GEMMs
- spechub: the coupled verify walks the live candidates only (151 -> 31 us a launch)

Speculation:
- DFlash2: lossless coupled acceptance at T > 0 (--draft-coupling on)
- A block drafter's proposals verified as a tree, on by default for the Gemma 4 31B's DFlash2, with
  a block-16 tree that runs its short block under load

Features:
- Decode-hot residency: --draft-expert-placement host, --expert-profile, --expert-pin-top, and
  --expert-stats FILE for the routed experts each layer selects
- --ngram-placement ram=DIR: one n-gram table shared by engines on separate cards
- Prefix cache: --prefix-resume exact (default) resumes a hit on the cold run's own chunk grid, so it
  answers bit-identically; snapshots at a head other prompts share, from 528-token prompts, and for
  tip-only one drafter window below the tip; hits on models whose KV groups have different block
  sizes; the snapshot store keeps a floor of 8 ahead of --host-headroom-mib
- Typed tool-call argument tags; rad-chatparse --request renders and reads back one request as the
  server does
- MiniMax-H3: keyframes ride a latent upscale
- Gemma 3's fused all-reduce junction on the lossy wire

Fixes:
- A speculative step shorter than its band (an MTP step of 4 rows at gqa 8) no longer stops the
  engine with a scratch refusal at issue (#293)
- --p2p off: prefill back to 1.12.0's rate; 1.13.0's push check read a host-memory slot over the link
  twice (#323)
- A chat template that cannot render a conversation is refused at load, not mid-request
- imatrix entries found under llama.cpp's spelling; the W4A8 prefill's tail scale read; the GDN conv
  kernels on an f32 weight; dense integer rows at the group they compute (a Q8_0 weight at group 32)
- --expert-profile refuses a profile recorded on another model
- hc_read's one-grid wait traps past its deadline instead of reading a stale value

Changes: Plugin ABI 31, unchanged. RADIANCE_DRAFT_TREE, DFLASH2_TREE_NODES, DFLASH2_TREE_TEMP and
DFLASH_Q_TEMP are gone (trees run where the probe says they pay; temperatures 1.25 / 0.6).
2026-10-29 12:04:24 +01:00

13 KiB

QSA: Qwen4-Exp's sparse attention

The full-attention layers of Qwen4-Exp (every fourth: 3, 7, … 47) do not attend to the whole history. A small indexer scores 4-token blocks of the key history, and the attention reads the best 512 of them plus the incomplete tail, so attention cost is constant past 2051 tokens.

The declaration side is arch/common/rad_qsa.h. The kernels are libr4d's qsa_block_key, qsa_score, qsa_select, qsa_work and qsa_tail_store; the sparse attention is the ordinary attn_paged. Schemas and operand contracts are in docs/OPS.md.

Shape

From the checkpoint: indexer_n_heads 4, indexer_kv_heads 1, indexer_head_dim 128, indexer_budget 2048, indexer_compress_ratio 4, so block_topk = 2048 / 4 = 512 blocks. rotary_dim is the main attention's (head_dim 256 * partial_rotary_factor 0.25 = 64), applied to the first 64 of the indexer's 128 dims from the layer's cos/sin table; the plugin refuses a checkpoint where it does not fit. Weights per QSA layer:

model.language_model.layers.<L>.self_attn.indexer.index_qk_proj.weight   [640, 2560]
model.language_model.layers.<L>.self_attn.indexer.q_layernorm.weight     [128]
model.language_model.layers.<L>.self_attn.indexer.k_layernorm.weight     [128]

640 = (4 query heads + 1 key head) x 128.

Computation

qk        = index_qk_proj(hidden)             [T, 640]
q, raw_k  = split(qk, [512, 128])             q -> [T, 4, 128], raw_k -> [T, 128]
q         = rope(q_layernorm(q), pos)         per query, at the query's own position

per complete block b of 4 consecutive key tokens:
  pooled  = mean over the 4 raw_k, in f32, cast back to bf16
  blk_k_b = rope(k_layernorm(pooled), pos of the block's first token)

per query:
  score_b = sum over the 4 heads of relu(q_h . blk_k_b) / sqrt(128)
  keep    = indices of the top min(512, n_blocks) scores
  visible = the 4 tokens of every kept block, plus the tail
            (the (pos+1) % 4 tokens after the last complete block, attended unconditionally)

The query side is qk_norm_rope_gate's k side at n_head 0, which reads [tokens, heads, head_dim] at the operand's row pitch (the query half of the heads + 1 projection). It needs the model's cos/sin table and is bounded by the attention block's fusion row count, above which the table is not filled; otherwise rmsnorm and rope run over column slices.

Block keys

A block key is independent of the query and, once its four tokens exist, permanent. The reference recomputes block keys inside the per-query loop; the engine computes each once, when the token with t % 4 == 3 arrives, and caches it. The indexer's cache is block keys, [n_blocks, 128] bf16 (64 bytes per token per QSA layer), plus a tail of at most 3 raw keys for the next block.

12 QSA layers x 64 B = 768 B per token, about 3% of the main KV cache's 24 KiB per token (2 heads x 256 x 2 (K and V) x 2 bytes x 12 at bf16): 192 MiB per sequence at 262144 tokens, 24 MiB at 32768.

Exactness bound

With pos + 1 <= budget + ratio - 1 = 2051 visible tokens there are at most 512 complete blocks, the top-k keeps all of them and the tail is always attended, so QSA equals dense causal attention bit for bit. Below the bound, a step with more than one query per sequence (prefill, mixed or speculative) issues dense attention, which reads each key once per query tile where the sparse path reads a table per query. Plain decode (one query per sequence) takes the sparse path at every context: there is no tile to amortise, and the sparse path stays exercised. The bound is the test for every change here (see "Checking").

Attention over a selected set

With the full-attention KV group paged at 4, a compress block is a page and the selected set is a paged block table, so sparse attention is the existing attn_paged with a 512-entry table.

The page size is a kernel constant. The QSA attention units (r4d_attn_paged_h256_gqa12.hip, r4d_attn_paged_h256_gqa6_bs4.hip) build r4d_attn_paged_gqa8.hip with G8_BLOCK_SIZE 4, and their rows declare C_EQ("block_size", 4). RadKVGroupDecl carries no block size: the core takes it from the resolved attention kernel, and the plugin asks for 4 through the attention op's block_size parameter. The rows and the #define must agree: a row claiming 16 against a kernel compiled at 4 resolves at declare and fails at issue.

attn_paged takes the block table and seqused_k as ordinary operands (arch/common/rad_block_attn_gated_fp8.h), so the sparse attention is issued with sel in place of the sequence's table and qsa_select's seqused in place of its length. Nothing in core/ knows about QSA.

Declaration order is the reverse of issue order. attn_paged declares sel and seqused as reads, and the buffer plan comes from declared reads, so the architecture sets them on the attention block before attn.declare() and declares the indexer after it.

Block-key storage

Block keys live in their own RAD_KV_FULL group, one page per compress block, indexed by page id. The scorer reads bk[page_id] through a table the KV manager already maintains, and the top-512 output is a list of attention page ids, which is the block table the attention reads.

A sequence's block-key pages differ from its attention pages, but both groups are paged at ratio, so block index b means the same in both. Each op takes the table of the group it serves:

op table it takes
qsa_score the block-key group's, because it gathers block keys
qsa_select the attention group's, because its output is what the attention reads

Swapping them scores one sequence's keys through another's pages. A RAD_BUF_PERSIST array indexed by (slot, block) would be simpler to declare but needs a translation in three places.

A RAD_KV_FULL group stores [pages, kv_heads, block_size, 2*head_dim]. With kv_heads = 1, block_size = ratio and head_dim = n / (2*ratio) (here 128 / 8 = 16), a page holds exactly one block key of n values (2 * head_dim * ratio == n is asserted). A group's page size comes from the first later-declared op with a block_size (Builder::find_kv_consumer), so the group is declared immediately before qsa_block_key, whose block_size sizes it.

The tail

A block key needs ratio consecutive raw keys; at decode three come from earlier steps. The tail is a RAD_KV_LINEAR group with state_dim {ratio + max_spec, head_dim}, bound to every QSA layer including the MTP head's. The alternatives all produce fluent wrong text:

a tail stored as… what goes wrong when
one buffer for the whole model layer L pools layer L-1's raw keys, because index_qk_proj is per layer every decode step: three of every four keys in a completing block
a plane indexed by the batch row a request that outlives an earlier one inherits its keys, because the scheduler orders a step by priority and stable-sorts decode first any step after a concurrent request retires
ratio rows indexed pos % ratio a rejected draft restarts the next step at a position this one already overwrote, so a completed block is rebuilt from a future token's key any speculative step that straddles a block boundary and does not accept everything

The tail is read on a later step than the one that wrote it and is keyed by layer, sequence and position; cu_seqlens gives only a token's sequence within the step. A KV group slot is zeroed on hand-out and checkpointed with the linear state, so a resume onto a half-built block reads the keys it was built from. A RAD_KV_WINDOW of width ratio holds only the step's last four tokens, which is the completing block at decode but not within a prefill chunk.

qsa_work gathers the blocks completing this step into the flat list qsa_block_key reads, and qsa_tail_store writes this step's raw keys into the state. qsa_work is issued before qsa_tail_store, because the gather reads tail keys the store overwrites. Position p lands at row p % ring. The work list has ceil(T / ratio) + n_seq items; items past the real count get page = -1 and are skipped, so it is one launch per layer per step.

Per-query selection (prefill)

At prefill every query in a chunk selects its own set from one shared history. Both scoring ops take cu_seqlens, so m is a query row while the block table stays the sequence's. Each query is bounded by its own position, n = min(nc[seq], (pos + 1) / ratio) (at decode, nc[seq]).

The attention is issued one row per query: attn_paged indexes its block table and length by row, so T queries issued as T rows of q_len 1, each with its own sel and seqused, is per-query sparse attention. Launches are chunked at max(max_seqs, 256) rows (the gated form takes the whole step), because the decode kernel's split-KV scratch is sized at declare from max_seqs and q_len.

The partial page is decided by position. At decode bt[nc] == -1 on a block boundary, since nothing is allocated past the sequence, so bt[n] >= 0 would work there. Mid-chunk, the same query finds a later token's live page in bt[n], which seqused would then cover. The partial page is appended only when (pos + 1) % ratio != 0.

Cost

The indexer is the only part of a decode step that grows with context: qsa_score and qsa_select scale with the number of compressed blocks, once per layer. r4d_selftest --perfqsa CTX [M] [iters] times them at a real context length with no model, passing the declared width and the live context separately (launch geometry comes from the declaration, work from nc).

  • qsa_score reads the key history once per 4-row query tile, matching a decode sequence's 1 + n_spec rows at depth 3; a tile straddling two sequences is processed per part.
  • qsa_select splits the block list into chunks that each find a local top-k, exact because a chunk's local top-k contains every global top-k member in it. The proof and the tie rule are in libr4d/r4d_qsa_select_bf16.hip.
  • Wave reductions use DPP (row_xmask for XOR offsets 8..1, v_permlanex16_b32 across rows), not __shfl_xor, which lowers to ds_bpermute_b32 and queues on the LDS pipe. Same lanes and add order, so bit-identical (libr4d/r4d_common.h, r4d_wave_sum).

Any two forms of the selection kernel must produce the same bytes, not only the same set: the attention sums its softmax over pages in table order, and float addition is not associative.

The score plane is the one buffer whose size is a product of the deployment's bounds — a row of max_ctx / ratio floats a query, 512 MiB at a 2048-row step and 256K of context. Under --prefill-span, where every step that mixes sequences fits in a MoE pass (RadBuildCtx::max_mixed_tok), it is declared a pass tall instead of --max-num-batched-tokens rows: a wider step is one sequence, scored and selected in windows of the plane's rows, each handed as a sequence of its own rows. Every operand a query row reads is its own (query, position, selection), so a row is scored and selected exactly as in one pass, and a row two windows share is written twice with the same bytes.

The caller bounds the history. Both ops size their grid from the declared width (the live count is device data), so a short prompt would otherwise launch for the whole --max-model-len. The architecture narrows the score operand's column count to a host-side ceiling from the batch, with the pitch unchanged. Every term of the ceiling is generous, because an underestimate silently drops blocks.

Checking

There is deliberately no switch that forces dense attention above the bound, where it is not the model the checkpoint was trained as. libref implements all five QSA ops (ref_qsa_block_key in libref/ref_attn.cpp; ref_qsa_score, ref_qsa_select, ref_qsa_tail_store and ref_qsa_work in libref/ref_qsa.cpp), so each device kernel has a row to compare against. No oracle gives the whole-subsystem comparison, a dense run against a sparse one above the bound. Changes are checked with:

  • r4d_selftest oracles: qsa_block_key and qsa_score against f64 evaluations, qsa_work and qsa_tail_store against a host walk of the batch, qsa_select against a host sort, a prefill case for the per-query join, the wide-launch form against the single-workgroup form, and split against unsplit at bit identity on mostly-tied scores, so the tie rule decides;
  • RADIANCE_DEBUG_QSA: prints nc, nsel, seqused and the head of the table for the first few steps. nc above topk is intended dropping, not a divergence. It synchronises the stream.

Count the reach, not the prompt. The bound is on the context each query sees, so a prompt a few tokens under it crosses the bound during decode and is expected to start differing there.