Layers

Compressed sparse layers

×

The residual is four parallel streams. Each sublayer reads them as one weighted sum and writes its output back into all four, with weights it computes from the streams themselves. Here: compressed sparse attention and a mixture of experts; layers 4, 6, …, 60.

hffn=∑jpjffn Xn,j′h_{\mathrm{ffn}}=\sum_{j}p^{\mathrm{ffn}}_{j}\,X'_{n,j}
Xn+1,k=ukffn Δffn+∑jCjkffn Xn,j′X_{n+1,{\htmlClass{vl-stream-k}{k}}}=u^{\mathrm{ffn}}_{{\htmlClass{vl-stream-k}{k}}}\,\Delta_{\mathrm{ffn}}+\sum_{j}C^{\mathrm{ffn}}_{jk}\,X'_{n,j}
All 68 steps
Stream weights for attention:\text{Stream weights for attention:}
xf=reshape⁡(Xn)x^{f}=\operatorname{reshape}(X_n)
m=xfWmix⊤mean⁡((xf)2)+εm=\frac{x^{f}W_{\mathrm{mix}}^{\top}}{\sqrt{\operatorname{mean}\big((x^{f})^{2}\big)+\varepsilon}}
pjattn=σ(s1 mj+bj)+ϵp^{\mathrm{attn}}_{j}=\sigma\big(s_{1}\,m_j+b_j\big)+\epsilon
ukattn=2 σ(s2 m4+k+b4+k)u^{\mathrm{attn}}_{{\htmlClass{vl-stream-k}{k}}}=2\,\sigma\big(s_{2}\,m_{4+{\htmlClass{vl-stream-k}{k}}}+b_{4+{\htmlClass{vl-stream-k}{k}}}\big)
Cjkattn=Sinkhorn⁡ ⁣(softmax⁡k(s3 m8+4(j−1)+k+b8+4(j−1)+k))C^{\mathrm{attn}}_{jk}=\operatorname{Sinkhorn}\!\Big(\operatorname{softmax}_{{\htmlClass{vl-stream-k}{k}}}\big(s_{3}\,m_{8+4(j-1)+{\htmlClass{vl-stream-k}{k}}}+b_{8+4(j-1)+{\htmlClass{vl-stream-k}{k}}}\big)\Big)
hattn=∑jpjattn Xn,jh_{\mathrm{attn}}=\sum_{j}p^{\mathrm{attn}}_{j}\,X_{n,j}
Compressed sparse attention:\text{Compressed sparse attention:}
qa=xattnWDQ{\htmlClass{vl-stream-q}{q^{a}}}=x_{\mathrm{attn}}W_{DQ}
q=cQWUQ{\htmlClass{vl-stream-q}{q}}=c^{{\htmlClass{vl-stream-q}{Q}}}W_{UQ}
qh,th=qt,hmean⁡(qt,h2)+ε{\htmlClass{vl-stream-q}{q^{h}_{h,t}}}=\frac{{\htmlClass{vl-stream-q}{q_{t,h}}}}{\sqrt{\operatorname{mean}\big({\htmlClass{vl-stream-q}{q_{t,h}^{2}}}\big)+\varepsilon}}
Qh,t,448:=Rt qh,t,448:h{\htmlClass{vl-stream-q}{Q_{h,t,448:}}}={\htmlClass{vl-stream-context}{R_t}}\,{\htmlClass{vl-stream-q}{q^{h}_{h,t,448:}}}
wa=xattnWKVw^{a}=x_{\mathrm{attn}}W_{KV}
Wt=[ FP8⁡(wt,:448n);  Rt wt,448:n ]W_{t}=\big[\,\operatorname{FP8}(w^{n}_{t,:448});\;{\htmlClass{vl-stream-context}{R_t}}\,w^{n}_{t,448:}\,\big]
kc=xattnWC{\htmlClass{vl-stream-k}{k^{c}}}=x_{\mathrm{attn}}W_{C}
gc=xattnWGg^{c}=x_{\mathrm{attn}}W_{G}
kjp=∑i∈block j−1Sib⊙kib+∑i∈block jSia⊙kia{\htmlClass{vl-stream-k}{k^{p}_{j}}}=\sum_{i\in\text{block }j-1}S^{b}_{i}\odot {\htmlClass{vl-stream-k}{k^{b}_{i}}}+\sum_{i\in\text{block }j}S^{a}_{i}\odot {\htmlClass{vl-stream-k}{k^{a}_{i}}}
S=softmax⁡i(gi+Bi mod 4)S=\operatorname{softmax}_{i}\big(g_{i}+B_{i\bmod 4}\big)
kn=γC⊙kpmean⁡((kp)2)+ε{\htmlClass{vl-stream-k}{k^{n}}}=\gamma_{C}\odot\frac{{\htmlClass{vl-stream-k}{k^{p}}}}{\sqrt{\operatorname{mean}\big(({\htmlClass{vl-stream-k}{k^{p}}})^{2}\big)+\varepsilon}}
Zj=[ FP8⁡(kj,:448n);  R4j kj,448:n ]Z_{j}=\big[\,\operatorname{FP8}({\htmlClass{vl-stream-k}{k^{n}_{j,:448}}});\;{\htmlClass{vl-stream-context}{R_{4j}}}\,{\htmlClass{vl-stream-k}{k^{n}_{j,448:}}}\,\big]
kic=xattnWIC{\htmlClass{vl-stream-k}{k^{ic}}}=x_{\mathrm{attn}}W_{IC}
gic=xattnWIGg^{ic}=x_{\mathrm{attn}}W_{IG}
kjip=∑i∈block j−1Sib⊙kib+∑i∈block jSia⊙kia{\htmlClass{vl-stream-k}{k^{ip}_{j}}}=\sum_{i\in\text{block }j-1}S^{b}_{i}\odot {\htmlClass{vl-stream-k}{k^{b}_{i}}}+\sum_{i\in\text{block }j}S^{a}_{i}\odot {\htmlClass{vl-stream-k}{k^{a}_{i}}}
S=softmax⁡i(gi+Bi mod 4)S=\operatorname{softmax}_{i}\big(g_{i}+B_{i\bmod 4}\big)
kin=γIC⊙kipmean⁡((kip)2)+ε{\htmlClass{vl-stream-k}{k^{in}}}=\gamma_{IC}\odot\frac{{\htmlClass{vl-stream-k}{k^{ip}}}}{\sqrt{\operatorname{mean}\big(({\htmlClass{vl-stream-k}{k^{ip}}})^{2}\big)+\varepsilon}}
KjI←FP4⁡(H [ kj,:64in;  R4j kj,64:in ]){\htmlClass{vl-stream-k}{K^{I}_{j}}}\leftarrow\operatorname{FP4}\Big(\mathcal{H}\,\big[\,{\htmlClass{vl-stream-k}{k^{in}_{j,:64}}};\;{\htmlClass{vl-stream-context}{R_{4j}}}\,{\htmlClass{vl-stream-k}{k^{in}_{j,64:}}}\,\big]\Big)
qi=cQWIQ{\htmlClass{vl-stream-q}{q^{i}}}=c^{{\htmlClass{vl-stream-q}{Q}}}W_{IQ}
Qh,tI=FP4⁡(H [ qt,h,:64i;  Rt qt,h,64:i ]){\htmlClass{vl-stream-q}{Q^{I}_{h,t}}}=\operatorname{FP4}\Big(\mathcal{H}\,\big[\,{\htmlClass{vl-stream-q}{q^{i}_{t,h,:64}}};\;{\htmlClass{vl-stream-context}{R_t}}\,{\htmlClass{vl-stream-q}{q^{i}_{t,h,64:}}}\,\big]\Big)
wI=xattnWIW /128⋅64w^{I}=x_{\mathrm{attn}}W_{IW}\,/\sqrt{128\cdot64}
St,jI=∑hwt,hI ReLU⁡(Qh,tI⋅KjI)S^{I}_{t,j}=\sum_{h}w^{I}_{t,h}\,\operatorname{ReLU}\big({\htmlClass{vl-stream-q}{Q^{I}_{h,t}}}\cdot {\htmlClass{vl-stream-k}{K^{I}_{j}}}\big)
Jtc=top⁡1024(St,jI  :  j<⌊(t+1)/4⌋)J^{c}_{t}=\operatorname{top}_{1024}\big(S^{I}_{t,j}\;:\;j<\lfloor(t+1)/4\rfloor\big)
Jtw={max⁡(0, t−127),…,t}J^{w}_{t}=\{\max(0,\,t-127),\ldots,t\}
Y=[ W;  Z ]Y=[\,W;\;Z\,]
J=[ Jw;  Jc+∣W∣ ]J=[\,J^{w};\;J^{c}+\lvert W\rvert\,]
Gt=Y[ Jt ]G_{t}=Y[\,J_{t}\,]
Sh,t,i=Qh,t⋅Gt,i /512S_{h,t,i}={\htmlClass{vl-stream-q}{Q_{h,t}}}\cdot G_{t,i}\,/\sqrt{512}
Ah,t,i=exp⁡Sh,t,iexp⁡sh+∑jexp⁡Sh,t,jA_{h,t,i}=\frac{\exp S_{h,t,i}}{\exp s_{h}+\sum_{j}\exp S_{h,t,j}}
Oh,t=∑iAh,t,i Gt,iO_{h,t}=\sum_{i}A_{h,t,i}\,G_{t,i}
Oˉh,t,448:=Rt−1 Oh,t,448:\bar O_{h,t,448:}={\htmlClass{vl-stream-context}{R_t^{-1}}}\,O_{h,t,448:}
ot,rg=[ Oˉh,t ]h∈r WOA,ro^{g}_{t,r}=\big[\,\bar O_{h,t}\,\big]_{h\in r}\,W_{OA,r}
Δattn=ogWOB\Delta_{\mathrm{attn}}=o^{g}W_{OB}
Xn,k′=ukattn Δattn+∑jCjkattn Xn,jX'_{n,{\htmlClass{vl-stream-k}{k}}}=u^{\mathrm{attn}}_{{\htmlClass{vl-stream-k}{k}}}\,\Delta_{\mathrm{attn}}+\sum_{j}C^{\mathrm{attn}}_{jk}\,X_{n,j}
Stream weights for the feed-forward:\text{Stream weights for the feed-forward:}
xf=reshape⁡(Xn′)x^{f}=\operatorname{reshape}(X'_n)
m=xfWmix⊤mean⁡((xf)2)+εm=\frac{x^{f}W_{\mathrm{mix}}^{\top}}{\sqrt{\operatorname{mean}\big((x^{f})^{2}\big)+\varepsilon}}
pjffn=σ(s1 mj+bj)+ϵp^{\mathrm{ffn}}_{j}=\sigma\big(s_{1}\,m_j+b_j\big)+\epsilon
ukffn=2 σ(s2 m4+k+b4+k)u^{\mathrm{ffn}}_{{\htmlClass{vl-stream-k}{k}}}=2\,\sigma\big(s_{2}\,m_{4+{\htmlClass{vl-stream-k}{k}}}+b_{4+{\htmlClass{vl-stream-k}{k}}}\big)
Cjkffn=Sinkhorn⁡ ⁣(softmax⁡k(s3 m8+4(j−1)+k+b8+4(j−1)+k))C^{\mathrm{ffn}}_{jk}=\operatorname{Sinkhorn}\!\Big(\operatorname{softmax}_{{\htmlClass{vl-stream-k}{k}}}\big(s_{3}\,m_{8+4(j-1)+{\htmlClass{vl-stream-k}{k}}}+b_{8+4(j-1)+{\htmlClass{vl-stream-k}{k}}}\big)\Big)
hffn=∑jpjffn Xn,j′h_{\mathrm{ffn}}=\sum_{j}p^{\mathrm{ffn}}_{j}\,X'_{n,j}
Mixture of experts:\text{Mixture of experts:}
z=xffnWRz=x_{\mathrm{ffn}}W_{{\htmlClass{vl-stream-context}{R}}}
s=softplus⁡(z)s=\sqrt{\operatorname{softplus}(z)}
T=top⁡6(s+b)\mathcal T=\operatorname{top}_{6}(s+b)
Gi=2.5 si∑j∈Tsj  (i∈T)G_{i}=2.5\,\frac{s_{i}}{\sum_{j\in\mathcal T}s_{j}}\;(i\in\mathcal T)
g=xffnW1g=x_{\mathrm{ffn}}W_{1}
u=xffnW3u=x_{\mathrm{ffn}}W_{3}
gˉ=min⁡(g, 10)\bar g=\min(g,\,10)
uˉ=clip⁡(u, −10, 10)\bar u=\operatorname{clip}(u,\,-10,\,10)
a=gˉ σ(gˉ)a=\bar g\,\sigma(\bar g)
h=a⊙uˉh=a\odot\bar u
Ei=hW2E_i=hW_{2}
yrouted=∑i∈TGi Eiy^{\mathrm{routed}}=\sum_{i\in\mathcal T}G_{i}\,E_{i}
g=xffnW1g=x_{\mathrm{ffn}}W_{1}
u=xffnW3u=x_{\mathrm{ffn}}W_{3}
gˉ=min⁡(g, 10)\bar g=\min(g,\,10)
uˉ=clip⁡(u, −10, 10)\bar u=\operatorname{clip}(u,\,-10,\,10)
a=gˉ σ(gˉ)a=\bar g\,\sigma(\bar g)
h=a⊙uˉh=a\odot\bar u
yshared=hW2y^{\mathrm{shared}}=hW_{2}
Δffn=yrouted+yshared\Delta_{\mathrm{ffn}}=y^{\mathrm{routed}}+y^{\mathrm{shared}}
Xn+1,k=ukffn Δffn+∑jCjkffn Xn,j′X_{n+1,{\htmlClass{vl-stream-k}{k}}}=u^{\mathrm{ffn}}_{{\htmlClass{vl-stream-k}{k}}}\,\Delta_{\mathrm{ffn}}+\sum_{j}C^{\mathrm{ffn}}_{jk}\,X'_{n,j}
in
X2k+2X_{2k+2} [B batch, T positions, 4 streams, 7168 d]
out
Xn+1X_{n+1} [B batch, T positions, 4 streams, 7168 d]
Parameters25,768,646,838 × 29 layers

25,768,646,838 parameters per layer, across its learned components

ComponentParameters
Stream weights for attention688,155
Attention RMSNorm7,168
Compressed sparse attention331,292,416
Stream weights for the feed-forward688,155
MoE RMSNorm7,168
Mixture of experts25,435,963,776
Per-instance total25,768,646,838

One of 29 independent layers.

Sources

In codeBlock(layer_id, args)

Open pinned code ↗

Direct evidence

A block owns attention, the mixture of experts, two RMSNorms, and for each sublayer a projection of the flattened four streams to 24 mix numbers, their 24 offsets and three scales.

model.py 647–671 ↗ · deepseek-ai/DeepSeek-V4-Pro @ b5968e9190ef

Excerpt
class Block(nn.Module):
    """Transformer block with Hyper-Connections (HC) mixing.
    Instead of a simple residual, HC maintains `hc_mult` copies of the hidden state.
    hc_pre: reduces hc copies -> 1 via learned weighted sum (pre-weights from Sinkhorn).
    hc_post: expands 1 -> hc copies via learned post-weights + combination matrix."""
    def __init__(self, layer_id: int, args: ModelArgs):
        super().__init__()
        self.layer_id = layer_id
        self.norm_eps = args.norm_eps
        self.attn = Attention(layer_id, args)
        self.ffn = MoE(layer_id, args)
        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))

The block runs the two sublayers alike: compute the mixes from the streams, read with them, normalise, apply the sublayer, write the streams.

model.py 688–700 ↗ · deepseek-ai/DeepSeek-V4-Pro @ b5968e9190ef

Excerpt
    def forward(self, x: torch.Tensor, start_pos: int, input_ids: Optional[torch.Tensor]) -> torch.Tensor:
        residual = x
        x, post, comb = self.hc_pre(x, self.hc_attn_fn, self.hc_attn_scale, self.hc_attn_base)
        x = self.attn_norm(x)
        x = self.attn(x, start_pos)
        x = self.hc_post(x, residual, post, comb)

        residual = x
        x, post, comb = self.hc_pre(x, self.hc_ffn_fn, self.hc_ffn_scale, self.hc_ffn_base)
        x = self.ffn_norm(x)
        x = self.ffn(x, input_ids)
        x = self.hc_post(x, residual, post, comb)
        return x

hc_pre: flatten the streams, project them to the mix numbers divided by their root mean square, split them by Sinkhorn, and read the streams with the reading weights; hc_post: each new stream is the writing weight times the sublayer’s output plus the streams combined by a column of the combination.

model.py 673–686 ↗ · deepseek-ai/DeepSeek-V4-Pro @ b5968e9190ef

Excerpt
    def hc_pre(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], y: [b,s,hc,d]
        shape, dtype = x.size(), x.dtype
        x = x.flatten(2).float()
        rsqrt = torch.rsqrt(x.square().mean(-1, keepdim=True) + self.norm_eps)
        mixes = F.linear(x, hc_fn) * rsqrt
        pre, post, comb = hc_split_sinkhorn(mixes, hc_scale, hc_base, self.hc_mult, self.hc_sinkhorn_iters, self.hc_eps)
        y = torch.sum(pre.unsqueeze(-1) * x.view(shape), dim=2)
        return y.to(dtype), post, comb

    def hc_post(self, x: torch.Tensor, residual: torch.Tensor, post: torch.Tensor, comb: torch.Tensor):
        # x: [b,s,d], residual: [b,s,hc,d], post: [b,s,hc], comb: [b,s,hc,hc], y: [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)