Expand description
2026-09-25: DeepSeek-V4.1 compressed attention (compress_ratio > 0), CPU
reference against the golden: YaRN RoPE with compress_rope_theta, the
two fp4 quantisers, linear_f32, and attention_any, the attention
forward for any ratio. The compressor and the indexer are in the
compressor and indexer submodules; the ratio-0 pieces are shared with
attn.rs.
Precision: the ratio > 1 compressor is f32 until its pooled result is rounded to bf16 for the RMSNorm; the indexer’s q and k go through the e2m1 round trip (power-of-two scales, block 32) and its scores are bf16 values; the compressed KV latent goes through e2m1 with e4m3 scales (block 16). The tests compare index selections exactly and tensors at bf16 tolerance.
Owner: model-arch, DeepSeek-V4.1 reference. Invariants: none beyond the types.
Structs§
- Comp
Attn Cfg - Comp
Attn Run - Compressor
State - Compressor
Weights - 2026-09-25:
wkvholds f32 values at ratio > 1 and bf16 values at ratio 1;wgateis required at ratio > 1 only. - Indexer
Cfg - Indexer
Weights - Layer
Attn State - Shared
Runtime - 2026-09-25: What source layers publish and later layers read: the latest
kv source’s whole latent
cache
[max_groups][hd]and index-key cache[max_groups][index_hd], the latest index selection[queries][topk](offset past the window rows), and the candidate mask[queries][width].
Constants§
- FP4_
BLOCK - 2026-09-25: Values per power-of-two scale in the indexer’s fp4 round trip.
- LATENT_
BLOCK - 2026-09-25: Values per e4m3 scale in the compressed KV latent’s fp4 round trip.
Functions§
- attention_
any - 2026-09-25: The attention forward for any
compress_ratio, updating the layer state and the shared slots. - compressor
- 2026-09-25: The pre-RoPE latent
[groups][hd](bf16 values), orNonewhile a group is still filling (prefill shorter thanratio, or a decode step that does not complete a group). - fp4_
quant_ e4m3_ inplace - 2026-09-25: The shims’
fp4_act_quantwith e4m3 scales, in place: amax floored at6 * 2^-9, scale =amax / 6rounded through e4m3, values to e2m1, dequantised and rounded to bf16. - fp4_
quant_ e8m0_ inplace - 2026-09-25: The shims’
fp4_act_quantwith e8m0 scales, in place: scale = power-of-two ceiling ofamax / 6(amax floored at6 * 2^-126), values to e2m1, dequantised and rounded to bf16. - indexer
- 2026-09-25: The indexer forward. Returns
([queries][topk], topk)compressed-position indices, offset byoffset, -1 where unreachable; also publishes index keys whenlatentandk_cacheare given (a kv source). - linear_
f32 - 2026-09-25: f32
x · w^T(the ratio > 1 compressor’swkv/wgateare f32 weights).wis[out_dim, in_dim]. - select_
candidate_ blocks - 2026-09-25: Candidate blocks: per query, score each
block-wide block oflogits[queries][width](unreachable positions at -inf) by its max, pin the block holding positioncompress_lens[q] - 1, keep the toptopk_blocksblocks that are not all -inf. Returns the keep mask[queries][width]. - to_
e2m1_ rne - 2026-09-25: Round onto the e2m1 grid with ties to the even code, keeping the
sign (as
_to_e2m1_rneinds41_ref_shims.py). - torch_
cpu_ topk_ set - 2026-09-25: The index set torch’s CPU
topk(k, largest=True)returns forvals, ties included, emulated as libstdc++’s introselect (__introselect,__move_median_to_first,__unguarded_partition,__insertion_sort) over (value, index) pairs with a descending comparator, keeping the firstkpairs. Among equal values (the indexer’s many -inf entries) the set is whatever that order leaves in front. Onlyk * 64 > nis emulated (asserted). Pinned bytopk_tie_order_matches_torch_cpu. - yarn_
freqs_ cis - 2026-09-25: The RoPE table with YaRN frequency correction, which the
compressed layers build from
compress_rope_theta:[seqlen][dim/2]of (cos, sin).