40 layers › Window layer › Stream weights for the feed-forward
Flattened streams
×tensorx
The four streams as one vector per position.
- shape
- [B batch, T positions, 20480 streams × d]
Sources
Contextual evidence from Flatten 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 Mix projection
A block owns its attention and mixture, two RMSNorms, and for each sublayer a projection of the flattened streams to 24 mix numbers with 24 offsets and 3 scales.
block-init · deepseek-ai/DeepSeek-V4.1-Flash / inference/model.py
Pinned revision 2cba9e42aa02 ↗ · lines 907–946
Read code excerpt
class Block(nn.Module):
"""A block whose residual stream is `hc_mult` parallel copies (Hyper-Connections).
Attention and FFN each sit between `hc_pre` (collapse the copies into one sublayer input) and
`hc_post` (expand back out, mixing the residual in through `comb`). `hc_mixes` derives all three
coefficient sets from the stream itself, `comb` made doubly stochastic by Sinkhorn.
The coefficients a sublayer computes are used by the *next* one -- see `forward`.
"""
attention_cls = Attention
def __init__(
self,
layer_id: int,
args: ModelArgs,
engram_layout: EngramLayout | None = None,
):
super().__init__()
self.layer_id = layer_id
self.norm_eps = args.norm_eps
self.attn = self.attention_cls(layer_id, args)
self.ffn = MoE(layer_id, args)
self.engram = None
if engram_layout is not None and layer_id in engram_layout.layer_ids:
self.engram = Engram(args, layer_id, engram_layout)
self.attn_norm = RMSNorm(args.dim, self.norm_eps)
self.ffn_norm = RMSNorm(args.dim, self.norm_eps)
self.hc_mult = hc_mult = args.hc_mult
self.hc_sinkhorn_iters = args.hc_sinkhorn_iters
self.hc_eps = args.hc_eps
mix_hc = (2 + hc_mult) * hc_mult
hc_dim = hc_mult * args.dim
with set_dtype(torch.float32):
self.hc_attn_fn = nn.Parameter(torch.empty(mix_hc, hc_dim))
self.hc_ffn_fn = nn.Parameter(torch.empty(mix_hc, hc_dim))
self.hc_attn_base = nn.Parameter(torch.empty(mix_hc))
self.hc_ffn_base = nn.Parameter(torch.empty(mix_hc))
self.hc_attn_scale = nn.Parameter(torch.empty(3))
self.hc_ffn_scale = nn.Parameter(torch.empty(3))