Key–value RMSNorm
×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]
Parameters512 × 2 groups
512 parameters per instance.
| Component | Parameters |
|---|---|
| Learned gain | 512 |
One in each of the 2 groups, each with its own weights.
Sources
In codeself.kv_norm(…) · 705
Direct evidence
RMSNorm divides by the root mean square over the features, computed in float32, and multiplies by a learned gain.
model.py 281–293 ↗ · deepseek-ai/DeepSeek-V4.1-Flash @ 2cba9e42aa02
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)The window keys and values: one projection and a norm, the rotary part rotated, quantised to FP8, written to the ring buffer.
model.py 700–720 ↗ · deepseek-ai/DeepSeek-V4.1-Flash @ 2cba9e42aa02
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)