Expand description
2026-09-25: DeepSeek-V4.1 Flash attention on the GPU: one forward for every layer.
A layer’s LayerRole::ratio selects its kind. Ratio 0 attends over a
sliding window of its own fp8-rounded kv rows. Ratio > 0 attends over that
window plus compressed positions chosen by an indexer from the latent cache
the kv-source layers (is_kv_source) publish in SharedV41. The index
selection is computed by the index-source layers and reused by the ratio > 0
layers after them; the candidate-source layer also publishes a block-level
candidate mask that later index sources apply.
This is the device form of deepseek_v41_ref::compress::attention_any, and
attn_v41_tests.rs compares it with the reference layer by layer:
- projections on
dense_gemm_bf16/dense_gemv_bf16(bf16 weights) or theQ2_KK-quant kernels; - RMSNorm, RoPE, the fp8/fp4 quantisers, the compressor pooling, the
indexer scores, sparse attention with the sink and the
wo_acolumn slices onkernels/gb10/deepseek-v4-flash/nvfp4/attn_v41.cu; - the candidate-block and index top-k selections on the CPU through the
reference’s
torch_cpu_topk_set, which reproduces torch’s CPU tie order among equal scores; the scores come back in one stream-ordered download.
Layer state (window ring, compressor partial group, the latent and
index-key caches) lives on the device; the shared index selection and
candidate mask in SharedV41 are host vectors.
Owner: model-arch, DeepSeek-V4.1. Invariants:
- A ratio > 0 layer fails (does not attend) when no kv source has published
a latent cache (
forward,decode_prep_role). forwardrefuses a selection wider thanwindow + index_topkor 2048, thesparse_attnscore buffer.
Structs§
- AttnV41
- 2026-09-25: The attention runtime: kernels, RoPE tables, and workspaces for
up to
max_tokenspositions per call. - Attn
V41Cfg - 2026-09-25: Model-wide attention geometry, built from
ModelConfigand theMETRALE_DS41_*limits by the DeepSeek-V4.1 weight loader. - Attn
V41Layer State - 2026-09-25: One sequence’s attention state for one layer, across prefill
and decode: the bf16 window ring
[window, hd]; on kv sources also the compressor’s partial group (f32 kv and score,[ratio, hd]each, used at ratio > 1), the bf16 latent cache[max_seq / ratio, hd]and the bf16 index-key cache[max_seq / ratio, index_hd]. - Attn
V41Layer Weights - 2026-09-25: One layer’s resident attention weights. Projections are
[out, in]:wo_ais[groups * o_rank, gw],wo_b[dim, groups * o_rank].sink([n_heads]),q_norm([q_rank]) andkv_norm([hd]) are f32. - Attn
V41Run - 2026-09-25: The intermediates of one layer’s forward.
qandoare bf16[tokens, n_heads, hd](qafter RoPE,obefore the inverse rotation);rows_aare the window rows (the chunk’s own rows atstart_pos == 0, the ring otherwise);rows_bthe kv source’s latent cache, of whichrows_b_lenrows are attended;idxis[tokens][topk], window slots then compressed positions offset byrows_a_len;outis bf16[tokens, dim]. - Compressor
Weights Gpu - 2026-09-25: Compressor weights (kv sources only). At ratio > 1
kvandgateare f32[hd, dim]; at ratio 1kvis bf16 andgateisNone.normis f32[hd]. - Indexer
Weights Gpu - 2026-09-25: Indexer weights (index sources only): bf16
wq_b[index_heads * index_hd, q_rank]andweights_proj[index_heads, dim]; on a layer that is also a kv source, bf16wk[index_hd, hd]and f32k_norm[index_hd]. - Layer
Role - 2026-09-25: A layer’s compress ratio and which shared slots it writes or reads.
- Shared
V41 - 2026-09-25: What source layers publish for the layers after them: the
latest kv source’s latent cache (
compress_kv) and index-key cache (index_k); the latest index source’s selectiontopk_idxs([queries][topk], offset past the window rows, -1 = absent); the candidate source’s maskcandidates([queries][cand_width]).compress_lenis the largest group count written so far and only grows.
Enums§
- AttnMat
- 2026-09-25: An attention projection on the device: bf16, or raw
Q2_Kblocks.Q3_Kis refused byAttnV41::gemm. 2026-09-25: A resident[N, K]row-major projection from the GGUF path: expanded bf16, or the rawQ2_K/Q3_Kblocks (WeightDtype::Q2K/Q3K) that the K-quant GEMV and MMQ kernels read.