40 layers › 2 groups of 6 layers › 5 reusing layers › Compressed sparse attention
Window ring
×state · kept between callsself.window_kv_cache · model.py 663–668, 708–719
The window keys and values of up to the last 128 positions, kept between forward calls in a ring of 128 slots.
- shape
- w [B batch, 128 window slots, 512 d_h]
A prefill attends over its own positions, each query over the last 128 up to itself, and only seeds the ring: the last 128 positions of the prompt, or all of them when the prompt is shorter (model.py 417–420, 708–716). One vector per position serves as key and as value. The ring holds bfloat16 values that were rounded through 8-bit floats before being written, one scale per 32 features (model.py 707), and it does not grow with the sequence.
Codemodel.py 410–720
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)def get_window_topk_idxs(window_size: int, bsz: int, seqlen: int, start_pos: int):
"""Which sliding-window cache slots each query attends to; -1 marks a slot holding nothing.
The cache is a ring of `window_size` slots. Prefill needs one row per query, each seeing its own
causal window. A decode step has a single query that sees the whole ring, listed oldest first.
Order within a row does not matter to `sparse_attn`, which handles every slot independently.
"""
if start_pos == 0:
end = torch.arange(seqlen).unsqueeze(1)
idxs = (end - window_size + 1).clamp(0) + torch.arange(min(seqlen, window_size))
idxs = torch.where(idxs > end, -1, idxs) # before the sequence started
else:
oldest = start_pos % window_size + 1
idxs = torch.cat([torch.arange(oldest, window_size), torch.arange(oldest)])
idxs = torch.where(idxs > start_pos, -1, idxs) # ring still filling
# sparse_attn needs real [b, m, topk] int32 memory, hence the materializing expand
return idxs.int().unsqueeze(0).expand(bsz, -1, -1).contiguous()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)Sources3
Contextual 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)Contextual evidence
The positions a query may attend to inside the sliding window: in a prefill the last 128 up to itself, in a decode step the whole ring.
window-indices · deepseek-ai/DeepSeek-V4.1-Flash / inference/model.py
Pinned revision 2cba9e42aa02 ↗ · lines 410–426
Read code excerpt
def get_window_topk_idxs(window_size: int, bsz: int, seqlen: int, start_pos: int):
"""Which sliding-window cache slots each query attends to; -1 marks a slot holding nothing.
The cache is a ring of `window_size` slots. Prefill needs one row per query, each seeing its own
causal window. A decode step has a single query that sees the whole ring, listed oldest first.
Order within a row does not matter to `sparse_attn`, which handles every slot independently.
"""
if start_pos == 0:
end = torch.arange(seqlen).unsqueeze(1)
idxs = (end - window_size + 1).clamp(0) + torch.arange(min(seqlen, window_size))
idxs = torch.where(idxs > end, -1, idxs) # before the sequence started
else:
oldest = start_pos % window_size + 1
idxs = torch.cat([torch.arange(oldest, window_size), torch.arange(oldest)])
idxs = torch.where(idxs > start_pos, -1, idxs) # ring still filling
# sparse_attn needs real [b, m, topk] int32 memory, hence the materializing expand
return idxs.int().unsqueeze(0).expand(bsz, -1, -1).contiguous()Contextual 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)