40 layers › 4 layers › 3 reusing layers
Write the streams
×no parametersself.hc_post(x, residual, attn_post, attn_comb) · 986
Add the sublayer’s output to every stream by its writing weight, on top of a combination of the streams.
- in
- [B batch, T positions, 5120 d] [B batch, T positions, 4 streams, 5120 d] [B batch, T positions, 4 streams] [B batch, T positions, 4 streams, 4 streams]
- out
- [B batch, T positions, 4 streams, 5120 d]
Explanation
This takes the place of a residual addition: no stream is carried over unchanged, and the sublayer’s output enters all four.
Codemodel.py 948–966, 968–994
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) 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_preSources2
Direct evidence
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)Direct evidence
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