40 layers › 2 groups of 6 layers › 5 reusing layers › Compressed sparse attention › Window keys and values
Key–value RMSNorm
×learnedself.kv_norm(…) · 705
Rescale each position’s features using their root mean square, then apply learned gain.
- in
- [B batch, 1 position, 512 d_h]
- out
- [B batch, 1 position, 512 d_h]
- parameters
- 512 · × 2 groups × 5 layers = 5,120
Parameters
512 parameters per instance.
| Component | Calculation | Parameters |
|---|---|---|
| Learned gain | 512 | |
| Per-instance total | 512 | |
Each normalization instance owns its learned gain vector.
Across 5 independent layers: parameters.
Derived from the configured feature width; the model total matches the checkpoint’s tensors.
Codemodel.py 281–293, 700–720
class RMSNorm(nn.Module):
def __init__(self, dim: int, eps: float = 1e-6):
super().__init__()
self.dim = dim
self.eps = eps
self.weight = nn.Parameter(torch.ones(dim))
def forward(self, x: torch.Tensor):
dtype = x.dtype
x = x.float()
var = x.square().mean(-1, keepdim=True)
x = x * torch.rsqrt(var + self.eps)
return (self.weight * x).to(dtype) def _window_kv(self, x, freqs_cis, start_pos):
"""This layer's sliding-window K and the window positions every query may attend to. The K
stays fp8, quantized over the whole post-RoPE vector, RoPE tail included."""
bsz, seqlen, _ = x.size()
win = self.window_size
kv = self.kv_norm(self.wkv(x))
apply_rotary_emb(kv[..., -self.rope_head_dim :], freqs_cis)
act_quant(kv, fp8_block_size, scale_fmt, scale_dtype, True)
if start_pos == 0: # prefill: attend over this chunk, seeding the ring buffer for decode
if seqlen <= win:
self.window_kv_cache[:bsz, :seqlen] = kv
else:
cutoff = seqlen % win
self.window_kv_cache[:bsz, cutoff:win], self.window_kv_cache[:bsz, :cutoff] = kv[:, -win:].split(
[win - cutoff, cutoff], dim=1
)
window_kv = kv
else: # decode: one token into the ring buffer, attend over the whole window
self.window_kv_cache[:bsz, start_pos % win] = kv.squeeze(1)
window_kv = self.window_kv_cache[:bsz]
return window_kv, get_window_topk_idxs(win, bsz, seqlen, start_pos)Sources2
Direct evidence
RMSNorm divides by the root mean square over the features, computed in float32, and multiplies by a learned gain.
rmsnorm · deepseek-ai/DeepSeek-V4.1-Flash / inference/model.py
Pinned revision 2cba9e42aa02 ↗ · lines 281–293
Read code excerpt
class RMSNorm(nn.Module):
def __init__(self, dim: int, eps: float = 1e-6):
super().__init__()
self.dim = dim
self.eps = eps
self.weight = nn.Parameter(torch.ones(dim))
def forward(self, x: torch.Tensor):
dtype = x.dtype
x = x.float()
var = x.square().mean(-1, keepdim=True)
x = x * torch.rsqrt(var + self.eps)
return (self.weight * x).to(dtype)Direct evidence
The window keys and values: one projection and a norm, the rotary part rotated, quantised to FP8, written to the ring buffer.
attention-window · deepseek-ai/DeepSeek-V4.1-Flash / inference/model.py
Pinned revision 2cba9e42aa02 ↗ · lines 700–720
Read code excerpt
def _window_kv(self, x, freqs_cis, start_pos):
"""This layer's sliding-window K and the window positions every query may attend to. The K
stays fp8, quantized over the whole post-RoPE vector, RoPE tail included."""
bsz, seqlen, _ = x.size()
win = self.window_size
kv = self.kv_norm(self.wkv(x))
apply_rotary_emb(kv[..., -self.rope_head_dim :], freqs_cis)
act_quant(kv, fp8_block_size, scale_fmt, scale_dtype, True)
if start_pos == 0: # prefill: attend over this chunk, seeding the ring buffer for decode
if seqlen <= win:
self.window_kv_cache[:bsz, :seqlen] = kv
else:
cutoff = seqlen % win
self.window_kv_cache[:bsz, cutoff:win], self.window_kv_cache[:bsz, :cutoff] = kv[:, -win:].split(
[win - cutoff, cutoff], dim=1
)
window_kv = kv
else: # decode: one token into the ring buffer, attend over the whole window
self.window_kv_cache[:bsz, start_pos % win] = kv.squeeze(1)
window_kv = self.window_kv_cache[:bsz]
return window_kv, get_window_topk_idxs(win, bsz, seqlen, start_pos)