40 layers › Window layer
Reading weights
×supplied · no parameterspre_mix
How attention reads the streams: computed by the sublayer before this layer.
- shape
- [B batch, T positions, 4 streams]
Sources
Contextual evidence from First reading weights
The first reading mix selects the first stream alone.
identity-mix · deepseek-ai/DeepSeek-V4.1-Flash / inference/model.py
Pinned revision 2cba9e42aa02 ↗ · lines 1159–1163
Read code excerpt
def make_identity_pre_mix(x: torch.Tensor, hc_mult: int) -> torch.Tensor:
"""initial one-hot mix"""
pre_mix = x.new_zeros(x.size(0), x.size(1), hc_mult, dtype=torch.float32)
pre_mix[:, :, 0] = 1.0
return pre_mixContextual evidence from First reading weights
Tokens are embedded and repeated into four streams; each layer, after an Engram step where it has one, reads and writes the streams; the last reading mix collapses them, then the final norm and the head.
transformer-forward · deepseek-ai/DeepSeek-V4.1-Flash / inference/model.py
Pinned revision 2cba9e42aa02 ↗ · lines 1241–1272
Read code excerpt
@torch.inference_mode()
def forward(
self, input_ids: torch.Tensor, start_pos: int = 0, images=None, token_types: torch.Tensor | None = None
):
"""input_ids: [b, s], every entry a real token id -- generate.py only ever passes positions it
has already filled, so the padding it uses internally never reaches here. token_types /
images carry the VL inputs built by image_processor.prepare_vl_inputs; image spans must lie
inside the first (start_pos 0) chunk."""
image_mask = None if token_types is None else token_types >= 0 # TEXT is -1
# image tokens take no part in an n-gram and get no engram contribution; text-only needs no mask
engram_mask = None if image_mask is None else ~image_mask
engram_hashes = self.engram_hash(input_ids, start_pos, engram_mask) if self.engram_hash is not None else None
h = self.embed(input_ids)
if images is not None:
assert start_pos == 0, "image spans must be prefilled in a single chunk"
self.merge_image_embeddings(images, h)
# Expand to hc_mult copies for Hyper-Connections
h = h.unsqueeze(2).repeat(1, 1, self.hc_mult, 1)
main_hiddens = []
pre_mix = make_identity_pre_mix(h, self.hc_mult)
for i, layer in enumerate(self.layers):
if layer.engram is not None:
h = layer.engram(h, engram_hashes[:, :, layer.engram.layer_hash_index, :], engram_mask)
# the MTP head reads the attention input of its target layers, not their output
if i in self.target_layer_ids:
main_hiddens.append(h.mean(dim=2))
h, pre_mix = layer(h, start_pos, pre_mix, image_mask)
h = layer.hc_pre(h, pre_mix)
logits = self.head(self.norm(h))
output_ids = sample(logits, self.temperature)
main_hidden = torch.cat(main_hiddens, dim=-1) if main_hiddens else None
return output_ids, logits, main_hiddenContextual evidence from Read the streams
The mix numbers are a projection of the flattened streams divided by their root mean square; reading is a weighted sum of the streams; writing adds the sublayer output, weighted per stream, to a combination of the streams.
block-hc · deepseek-ai/DeepSeek-V4.1-Flash / inference/model.py
Pinned revision 2cba9e42aa02 ↗ · lines 948–966
Read code excerpt
def hc_mixes(self, x: torch.Tensor, hc_fn: torch.Tensor, hc_scale: torch.Tensor, hc_base: torch.Tensor):
"""x: [b,s,hc,d], hc_fn: [mix_hc, hc*d], hc_scale: [3], hc_base: [mix_hc]. Returns the
pre / post / comb coefficients, split out of one projection of the flattened stream."""
# normalized over the whole flattened hc*d stream, one statistic per token
x = x.flatten(2).float()
rsqrt = torch.rsqrt(x.square().mean(-1, keepdim=True) + self.norm_eps)
mixes = F.linear(x, hc_fn) * rsqrt
return hc_split_sinkhorn(mixes, hc_scale, hc_base, self.hc_mult, self.hc_sinkhorn_iters, self.hc_eps)
def hc_pre(self, x: torch.Tensor, pre_mix: torch.Tensor):
"""Collapse the hc copies into one, weighted by pre_mix. [b,s,hc,d] x [b,s,hc] -> [b,s,d]"""
y = torch.sum(pre_mix.unsqueeze(-1) * x.float(), dim=2)
return y.to(x.dtype)
def hc_post(self, x: torch.Tensor, residual: torch.Tensor, post: torch.Tensor, comb: torch.Tensor):
"""Expand the sublayer output back to hc copies and mix the residual in through `comb`.
x: [b,s,d], residual: [b,s,hc,d], post: [b,s,hc], comb: [b,s,hc,hc] -> [b,s,hc,d]"""
y = post.unsqueeze(-1) * x.unsqueeze(-2) + torch.sum(comb.unsqueeze(-1) * residual.unsqueeze(-2), dim=2)
return y.type_as(x)Contextual evidence from Read the streams
Attention reads the streams with the mix the previous sublayer computed and writes with its own; the mixture reads with the mix attention computed and writes with its own, and hands its reading mix to the next layer.
block-forward · deepseek-ai/DeepSeek-V4.1-Flash / inference/model.py
Pinned revision 2cba9e42aa02 ↗ · lines 968–994
Read code excerpt
def forward(
self,
x: torch.Tensor,
start_pos: int,
pre_mix: torch.Tensor,
image_mask: torch.Tensor | None,
*attn_args,
) -> tuple[torch.Tensor, torch.Tensor]:
"""`pre_mix` collapses the hc_mult copies down to one input for this block's attention. Each
sub-block's own `hc_mixes` produces the mix for the *next* one, so attention uses what the
previous layer's FFN produced and the FFN uses what this attention produced.
image_mask: [b, s] bool, True inside image spans (selects the VL routing bias)."""
residual = x
attn_pre, attn_post, attn_comb = self.hc_mixes(x, self.hc_attn_fn, self.hc_attn_scale, self.hc_attn_base)
x = self.hc_pre(x, pre_mix)
x = self.attn_norm(x)
x = self.attn(x, start_pos, *attn_args)
x = self.hc_post(x, residual, attn_post, attn_comb)
residual = x
ffn_pre, ffn_post, ffn_comb = self.hc_mixes(x, self.hc_ffn_fn, self.hc_ffn_scale, self.hc_ffn_base)
x = self.hc_pre(x, attn_pre)
x = self.ffn_norm(x)
x = self.ffn(x, image_mask)
x = self.hc_post(x, residual, ffn_post, ffn_comb)
return x, ffn_pre