40 layers › 2 groups of 6 layers › Source layer, pairs pooled › Compressed sparse attention
Rotate, round to FP4
×no parametersapply_rotary_emb(latent[..., -rd:], freqs); fp4_act_quant(latent, 16, …) · 752–760
Rotate the last 64 features at the position of the pair’s first token and round to 4-bit floats.
Why
Storage. The compressed entries are stored in 4-bit floats to cut the cache: against 8-bit entries the footprint nearly halves, in memory and when moved to disk. The values are converted back before attention, so the format serves storage and needs no 4-bit arithmetic. DeepSeek V4.1 report §2.4.4
The format is E2M1 with one scale per 16 features. The report bounds the entries’ magnitude near √512 ≈ 22.6, from normalization gains close to 1 and a rotation that keeps the norm, far inside the format’s range. It drops the second, global scale of NVFP4 to simplify the cache layout: with entries this small the omission costs no measurable accuracy. DeepSeek V4.1 report §2.4.4
Rounding comes after the rotation: rounding before it improved accuracy only marginally and would add work to every decode step. DeepSeek V4.1 report §2.4.4
- in
- [B batch, T/2 compressed positions, 512 d_h]
- out
- [B batch, T/2 compressed positions, 512 d_h]
Rotate the last 64 features of the latent by position, in pairs (model.py 392–406), with base 160,000 and YaRN scaling. The indexer reads the latent before this step (model.py 749–750).
Codemodel.py 369–406, 722–763
def _compress_topk_idxs(self, x, qr, latent, start_pos, offset, compress_len):
"""Which compressed positions each query attends to. Index sources run their own indexer;
the layers in between reuse the result their source published."""
if not self.is_index_source:
return shared_attn.topk_idxs
bsz, seqlen, _ = x.size()
if compress_len == 0:
idxs = torch.empty(bsz, seqlen, 0, dtype=torch.int32, device=x.device)
else:
assert self.indexer is not None
if self.indexer.freqs_cis is None:
self.indexer.freqs_cis = self.freqs_cis
idxs = self.indexer(x, qr, latent, start_pos, offset)
shared_attn.topk_idxs = idxs
return idxs
def _compress_kv(self, x, qr, start_pos, offset):
"""The shared compressed KV and the compressed positions every query may attend to. This
layer compresses its own KV only when it is a source; otherwise it just reads the cache."""
bsz, seqlen, _ = x.size()
ratio = self.compress_ratio
compress_len = (start_pos + seqlen) // ratio
latent = None
if self.is_kv_source:
latent = self.compressor(x, start_pos)
shared_attn.compress_kv = self.compress_kv_cache
# the indexer needs the latent before RoPE, so it runs before the cache is written
idxs = self._compress_topk_idxs(x, qr, latent, start_pos, offset, compress_len)
if latent is not None:
# a latent stands for the first token of its group, so group j takes position j * ratio
freqs = (
self.freqs_cis[: seqlen - seqlen % ratio : ratio]
if start_pos == 0
else self.freqs_cis[start_pos + 1 - ratio].unsqueeze(0)
)
apply_rotary_emb(latent[..., -self.rope_head_dim :], freqs)
# Compressed KV uses groups of 16 with E4M3 scales; the indexer uses 32 with E8M0.
fp4_act_quant(latent, 16, True, scale_dtype=torch.float8_e4m3fn)
self.compress_kv_cache[:bsz, start_pos // ratio : start_pos // ratio + latent.size(1)] = latent
# read after the write, so this does not depend on the slice aliasing the cache
return shared_attn.compress_kv[:bsz, :compress_len], idxsdef precompute_freqs_cis(dim, seqlen, original_seq_len, base, factor, beta_fast, beta_slow) -> torch.Tensor:
"""Rotary frequencies as complex exponentials, one row per position.
With original_seq_len > 0 this applies YaRN: dimensions whose wavelength already fits inside the
training context keep their frequency, those far beyond it are divided by `factor`, and the
`beta_fast`..`beta_slow` band in between is faded across with a linear ramp.
"""
freqs = 1.0 / (base ** (torch.arange(0, dim, 2, dtype=torch.float32) / dim))
if original_seq_len > 0:
# the dim whose wavelength completes `rotations` turns over the training context
def corrected_dim(rotations):
return dim * math.log(original_seq_len / (rotations * 2 * math.pi)) / (2 * math.log(base))
low = max(math.floor(corrected_dim(beta_fast)), 0)
high = min(math.ceil(corrected_dim(beta_slow)), dim - 1)
ramp = ((torch.arange(dim // 2, dtype=torch.float32) - low) / max(high - low, 1e-3)).clamp(0, 1)
smooth = 1 - ramp
freqs = freqs / factor * (1 - smooth) + freqs * smooth
freqs = torch.outer(torch.arange(seqlen), freqs)
return torch.polar(torch.ones_like(freqs), freqs)
def apply_rotary_emb(x: torch.Tensor, freqs_cis: torch.Tensor, inverse: bool = False) -> torch.Tensor:
"""Rotate `x` in place, taking adjacent element pairs as complex numbers. Accepts [b, s, d] and
[b, s, h, d]; `inverse` conjugates the rotation, which is how the attention output gets the
query's rotation removed again so the cache can stay in one shared rotated form."""
y = x
x = torch.view_as_complex(x.float().unflatten(-1, (-1, 2)))
if inverse:
freqs_cis = freqs_cis.conj()
if x.ndim == 3:
freqs_cis = freqs_cis.view(1, x.size(1), x.size(-1))
else:
freqs_cis = freqs_cis.view(1, x.size(1), 1, x.size(-1))
x = torch.view_as_real(x * freqs_cis).flatten(-2)
y.copy_(x)
return ySources2
Direct evidence
A key–value source compresses its own input and writes the shared cache; an index source runs its indexer before the latent is rotated; every other layer reads what the nearest earlier source published.
attention-compress · deepseek-ai/DeepSeek-V4.1-Flash / inference/model.py
Pinned revision 2cba9e42aa02 ↗ · lines 722–763
Read code excerpt
def _compress_topk_idxs(self, x, qr, latent, start_pos, offset, compress_len):
"""Which compressed positions each query attends to. Index sources run their own indexer;
the layers in between reuse the result their source published."""
if not self.is_index_source:
return shared_attn.topk_idxs
bsz, seqlen, _ = x.size()
if compress_len == 0:
idxs = torch.empty(bsz, seqlen, 0, dtype=torch.int32, device=x.device)
else:
assert self.indexer is not None
if self.indexer.freqs_cis is None:
self.indexer.freqs_cis = self.freqs_cis
idxs = self.indexer(x, qr, latent, start_pos, offset)
shared_attn.topk_idxs = idxs
return idxs
def _compress_kv(self, x, qr, start_pos, offset):
"""The shared compressed KV and the compressed positions every query may attend to. This
layer compresses its own KV only when it is a source; otherwise it just reads the cache."""
bsz, seqlen, _ = x.size()
ratio = self.compress_ratio
compress_len = (start_pos + seqlen) // ratio
latent = None
if self.is_kv_source:
latent = self.compressor(x, start_pos)
shared_attn.compress_kv = self.compress_kv_cache
# the indexer needs the latent before RoPE, so it runs before the cache is written
idxs = self._compress_topk_idxs(x, qr, latent, start_pos, offset, compress_len)
if latent is not None:
# a latent stands for the first token of its group, so group j takes position j * ratio
freqs = (
self.freqs_cis[: seqlen - seqlen % ratio : ratio]
if start_pos == 0
else self.freqs_cis[start_pos + 1 - ratio].unsqueeze(0)
)
apply_rotary_emb(latent[..., -self.rope_head_dim :], freqs)
# Compressed KV uses groups of 16 with E4M3 scales; the indexer uses 32 with E8M0.
fp4_act_quant(latent, 16, True, scale_dtype=torch.float8_e4m3fn)
self.compress_kv_cache[:bsz, start_pos // ratio : start_pos // ratio + latent.size(1)] = latent
# read after the write, so this does not depend on the slice aliasing the cache
return shared_attn.compress_kv[:bsz, :compress_len], idxsDirect evidence
Rotary frequencies with the YaRN scaling above the original context, and the rotation of adjacent feature pairs, with an inverse.
rope · deepseek-ai/DeepSeek-V4.1-Flash / inference/model.py
Pinned revision 2cba9e42aa02 ↗ · lines 369–406
Read code excerpt
def precompute_freqs_cis(dim, seqlen, original_seq_len, base, factor, beta_fast, beta_slow) -> torch.Tensor:
"""Rotary frequencies as complex exponentials, one row per position.
With original_seq_len > 0 this applies YaRN: dimensions whose wavelength already fits inside the
training context keep their frequency, those far beyond it are divided by `factor`, and the
`beta_fast`..`beta_slow` band in between is faded across with a linear ramp.
"""
freqs = 1.0 / (base ** (torch.arange(0, dim, 2, dtype=torch.float32) / dim))
if original_seq_len > 0:
# the dim whose wavelength completes `rotations` turns over the training context
def corrected_dim(rotations):
return dim * math.log(original_seq_len / (rotations * 2 * math.pi)) / (2 * math.log(base))
low = max(math.floor(corrected_dim(beta_fast)), 0)
high = min(math.ceil(corrected_dim(beta_slow)), dim - 1)
ramp = ((torch.arange(dim // 2, dtype=torch.float32) - low) / max(high - low, 1e-3)).clamp(0, 1)
smooth = 1 - ramp
freqs = freqs / factor * (1 - smooth) + freqs * smooth
freqs = torch.outer(torch.arange(seqlen), freqs)
return torch.polar(torch.ones_like(freqs), freqs)
def apply_rotary_emb(x: torch.Tensor, freqs_cis: torch.Tensor, inverse: bool = False) -> torch.Tensor:
"""Rotate `x` in place, taking adjacent element pairs as complex numbers. Accepts [b, s, d] and
[b, s, h, d]; `inverse` conjugates the rotation, which is how the attention output gets the
query's rotation removed again so the cache can stay in one shared rotated form."""
y = x
x = torch.view_as_complex(x.float().unflatten(-1, (-1, 2)))
if inverse:
freqs_cis = freqs_cis.conj()
if x.ndim == 3:
freqs_cis = freqs_cis.view(1, x.size(1), x.size(-1))
else:
freqs_cis = freqs_cis.view(1, x.size(1), 1, x.size(-1))
x = torch.view_as_real(x * freqs_cis).flatten(-2)
y.copy_(x)
return y