Expand description
DeepSeek-V4.1 compressed attention (compress_ratio > 0): the Compressor (softmax
pooling of ratio tokens into one latent, or a plain projection at ratio 1), the Indexer
(fp4 query heads against one shared fp4 key per compressed position, rectified scores combined
by weights_proj), select_candidate_blocks (level one of the two-level top-k), the
SharedAttentionRuntime slots layers hand down the stack, YaRN RoPE with
compress_rope_theta, the two fp4 quantisers, and the full Attention.forward for any ratio.
CPU reference against the golden; the ratio-0 pieces are shared with attn.rs.
Precision follows the reference: the ratio-2 compressor is f32 end to end until its RMSNorm casts to bf16; the indexer’s q and k go through the fp4 e2m1 round trip (e8m0 scales, block 32) and its scores are bf16 tensors; the compressed KV latent goes through e2m1 with e4m3 scales (block 16). Index selections are compared exactly, tensors at bf16 tolerance.
Structs§
- Comp
Attn Cfg - Comp
Attn Run - Compressor
State - Compressor
Weights - Indexer
Cfg - Indexer
Weights - Layer
Attn State - Shared
Runtime SharedAttentionRuntime: what source layers publish and later layers read.
Constants§
- FP4_
BLOCK fp4_block_size(model.py:28).- LATENT_
BLOCK - Block size the compressed KV latent is quantised with (
fp4_act_quant(latent, 16, ...)).
Functions§
- attention_
any Attention.forwardfor anycompress_ratio, mutating the layer state and the shared slots.- compressor
Compressor.forward: the pre-RoPE latent[groups][hd](bf16 values), orNonewhile a group is still filling up in decode.- fp4_
quant_ e4m3_ inplace fp4_act_quant(x, 16, inplace=True, scale_dtype=e4m3): amax floored at6 * 2^-9, scale =amax / 6rounded through e4m3, values to e2m1, dequantised back to bf16.- fp4_
quant_ e8m0_ inplace fp4_act_quant(x, block, inplace=True)with e8m0 scales: scale = pow2 ceiling ofamax / 6(amax floored at6 * 2^-126), values to e2m1, dequantised back to bf16.- indexer
Indexer.forward. Returns[queries][topk]compressed-position indices, offset byoffset, -1 where unreachable; also publishes index keys when this layer owns them.- linear_
f32 - f32
F.linear(the ratio-2 compressor’swkv/wgateare fp32 weights on an fp32 input). - select_
candidate_ blocks select_candidate_blocks:logits[queries][width]with unreachable positions at -inf;compress_lens[q]. Returns the keep mask[queries][width].- to_
e2m1_ rne - Round onto the e2m1 grid with ties to the even code, keeping the sign (
_to_e2m1_rne). - torch_
cpu_ topk_ set - The index SET torch’s CPU
topk(k, largest=True)returns forvals, ties included. For the row lengths here (k * 64 > n) torch runsstd::nth_elementon (value, index) pairs with a descending comparator and returns the firstkpairs, so among equal values (the indexer’s many -inf entries) the picks are whatever libstdc++’s introselect leaves in front. That order is deterministic and this is a line-for-line emulation of it (__introselect,__move_median_to_first,__unguarded_partition,__insertion_sort). Pinned bytopk_tie_order_matches_torch_cpuagainst this box’s torch. - yarn_
freqs_ cis precompute_freqs_cisWITH YaRN (original_seq_len > 0), as the compress layers build it fromcompress_rope_theta.[seqlen][dim/2]of (cos, sin).