40 layers › 2 groups of 6 layers › Source layer, pairs pooled › Compressed sparse attention
Compressed cache
×state · kept between callsself.compress_kv_cache · model.py 669–679, 759–763
One vector for every two tokens of the whole sequence so far.
- shape
- z [B batch, T/2 compressed positions, 512 d_h]
A prefill writes one row for every two tokens of the prompt; an odd last token waits in the compressor for its pair (model.py 466–475, 761). In the interactive run the buffer is allocated for 32,768 rows per sequence, 512 features each (generate.py 124–126; model.py 669–679), and its filled part grows with the sequence. The layers after it, up to the next source layer, read it through the shared object and keep none of their own (model.py 746–748, 763, 1166–1180).
Codemodel.py 613–1180
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], idxsclass Attention(nn.Module):
"""Latent attention over two KV sources at once, concatenated into one `sparse_attn` call: a
sliding window of raw KV, plus -- when compress_ratio > 0 -- `index_topk` compressed positions
reaching further back. Q and the output projection are both low-rank, the latter grouped.
compress_ratio > 0 does not mean the layer compresses its own KV: only kv_source_layers do,
the rest read that same cache.
"""
def __init__(self, layer_id: int, args: ModelArgs):
super().__init__()
self.layer_id = layer_id
self.dim = args.dim
self.n_heads = args.n_heads
self.n_local_heads = args.n_heads // world_size
self.q_lora_rank = args.q_lora_rank
self.o_lora_rank = args.o_lora_rank
self.head_dim = args.head_dim
self.rope_head_dim = args.rope_head_dim
self.nope_head_dim = args.head_dim - args.rope_head_dim
self.n_groups = args.o_groups
self.n_local_groups = self.n_groups // world_size
self.window_size = args.window_size
self.compress_ratio = args.compress_ratios[layer_id]
self.eps = args.norm_eps
self.attn_sink = nn.Parameter(torch.empty(self.n_local_heads, dtype=torch.float32))
self.wq_a = Linear(self.dim, self.q_lora_rank)
self.q_norm = RMSNorm(self.q_lora_rank, self.eps)
self.wq_b = ColumnParallelLinear(self.q_lora_rank, self.n_heads * self.head_dim)
self.wkv = Linear(self.dim, self.head_dim)
self.kv_norm = RMSNorm(self.head_dim, self.eps)
self.wo_a = ColumnParallelLinear(
self.n_heads * self.head_dim // self.n_groups,
self.n_groups * args.o_lora_rank,
dtype=torch.bfloat16,
)
self.wo_b = RowParallelLinear(self.n_groups * args.o_lora_rank, self.dim)
self.softmax_scale = self.head_dim**-0.5
is_backbone = layer_id < args.n_layers
self.is_kv_source = is_backbone and layer_id in args.kv_source_layers
self.is_index_source = is_backbone and layer_id in args.index_source_layers
self.compressor: Compressor | None = None
self.indexer: Indexer | None = None
if self.is_kv_source:
self.compressor = Compressor(args, layer_id)
if self.is_index_source:
self.indexer = Indexer(args, layer_id)
self.window_kv_cache: torch.Tensor
self.register_buffer(
"window_kv_cache",
torch.zeros(args.max_batch_size, args.window_size, self.head_dim),
persistent=False,
)
if self.is_kv_source:
self.compress_kv_cache: torch.Tensor
self.register_buffer(
"compress_kv_cache",
torch.zeros(
args.max_batch_size,
args.max_seq_len // self.compress_ratio,
self.head_dim,
),
persistent=False,
)
if self.compress_ratio:
original_seq_len, rope_theta = (
args.original_seq_len,
args.compress_rope_theta,
)
else:
# disable YaRN and use base rope_theta in pure sliding-window attention
original_seq_len, rope_theta = 0, args.rope_theta
freqs_cis = precompute_freqs_cis(
self.rope_head_dim,
args.max_seq_len,
original_seq_len,
rope_theta,
args.rope_factor,
args.beta_fast,
args.beta_slow,
)
self.freqs_cis: torch.Tensor
self.register_buffer("freqs_cis", freqs_cis, persistent=False)class SharedAttentionRuntime:
"""What attention layers hand down the stack instead of recomputing. Layers run in order and
every source writes before its consumers read, so one slot each is enough and nothing needs
resetting between forwards. Sources: compress_kv and index_k from kv_source_layers, topk_idxs
from index_source_layers, candidates from candidate_source_layer."""
def __init__(self):
self.compress_kv: torch.Tensor | None = None
self.index_k: torch.Tensor | None = None
self.topk_idxs: torch.Tensor | None = None
self.candidates: torch.Tensor | None = None
# Only ever one model per process, same as world_size / rank / default_dtype above.
shared_attn = SharedAttentionRuntime()Sources3
Contextual 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], idxsContextual evidence
Attention owns a low-rank query, one key–value projection, a learned sink per head, a grouped low-rank output, a ring buffer for the window, and, by the two source lists, a compressor and an indexer; layers that attend to compressed positions use the larger rotary base with YaRN.
attention-init · deepseek-ai/DeepSeek-V4.1-Flash / inference/model.py
Pinned revision 2cba9e42aa02 ↗ · lines 613–698
Read code excerpt
class Attention(nn.Module):
"""Latent attention over two KV sources at once, concatenated into one `sparse_attn` call: a
sliding window of raw KV, plus -- when compress_ratio > 0 -- `index_topk` compressed positions
reaching further back. Q and the output projection are both low-rank, the latter grouped.
compress_ratio > 0 does not mean the layer compresses its own KV: only kv_source_layers do,
the rest read that same cache.
"""
def __init__(self, layer_id: int, args: ModelArgs):
super().__init__()
self.layer_id = layer_id
self.dim = args.dim
self.n_heads = args.n_heads
self.n_local_heads = args.n_heads // world_size
self.q_lora_rank = args.q_lora_rank
self.o_lora_rank = args.o_lora_rank
self.head_dim = args.head_dim
self.rope_head_dim = args.rope_head_dim
self.nope_head_dim = args.head_dim - args.rope_head_dim
self.n_groups = args.o_groups
self.n_local_groups = self.n_groups // world_size
self.window_size = args.window_size
self.compress_ratio = args.compress_ratios[layer_id]
self.eps = args.norm_eps
self.attn_sink = nn.Parameter(torch.empty(self.n_local_heads, dtype=torch.float32))
self.wq_a = Linear(self.dim, self.q_lora_rank)
self.q_norm = RMSNorm(self.q_lora_rank, self.eps)
self.wq_b = ColumnParallelLinear(self.q_lora_rank, self.n_heads * self.head_dim)
self.wkv = Linear(self.dim, self.head_dim)
self.kv_norm = RMSNorm(self.head_dim, self.eps)
self.wo_a = ColumnParallelLinear(
self.n_heads * self.head_dim // self.n_groups,
self.n_groups * args.o_lora_rank,
dtype=torch.bfloat16,
)
self.wo_b = RowParallelLinear(self.n_groups * args.o_lora_rank, self.dim)
self.softmax_scale = self.head_dim**-0.5
is_backbone = layer_id < args.n_layers
self.is_kv_source = is_backbone and layer_id in args.kv_source_layers
self.is_index_source = is_backbone and layer_id in args.index_source_layers
self.compressor: Compressor | None = None
self.indexer: Indexer | None = None
if self.is_kv_source:
self.compressor = Compressor(args, layer_id)
if self.is_index_source:
self.indexer = Indexer(args, layer_id)
self.window_kv_cache: torch.Tensor
self.register_buffer(
"window_kv_cache",
torch.zeros(args.max_batch_size, args.window_size, self.head_dim),
persistent=False,
)
if self.is_kv_source:
self.compress_kv_cache: torch.Tensor
self.register_buffer(
"compress_kv_cache",
torch.zeros(
args.max_batch_size,
args.max_seq_len // self.compress_ratio,
self.head_dim,
),
persistent=False,
)
if self.compress_ratio:
original_seq_len, rope_theta = (
args.original_seq_len,
args.compress_rope_theta,
)
else:
# disable YaRN and use base rope_theta in pure sliding-window attention
original_seq_len, rope_theta = 0, args.rope_theta
freqs_cis = precompute_freqs_cis(
self.rope_head_dim,
args.max_seq_len,
original_seq_len,
rope_theta,
args.rope_factor,
args.beta_fast,
args.beta_slow,
)
self.freqs_cis: torch.Tensor
self.register_buffer("freqs_cis", freqs_cis, persistent=False)Contextual evidence
One object hands compressed keys and values, index keys, selected positions and candidates from a source layer to the layers after it.
shared · deepseek-ai/DeepSeek-V4.1-Flash / inference/model.py
Pinned revision 2cba9e42aa02 ↗ · lines 1166–1180
Read code excerpt
class SharedAttentionRuntime:
"""What attention layers hand down the stack instead of recomputing. Layers run in order and
every source writes before its consumers read, so one slot each is enough and nothing needs
resetting between forwards. Sources: compress_kv and index_k from kv_source_layers, topk_idxs
from index_source_layers, candidates from candidate_source_layer."""
def __init__(self):
self.compress_kv: torch.Tensor | None = None
self.index_k: torch.Tensor | None = None
self.topk_idxs: torch.Tensor | None = None
self.candidates: torch.Tensor | None = None
# Only ever one model per process, same as world_size / rank / default_dtype above.
shared_attn = SharedAttentionRuntime()