DeepSeek V4-Flash

Compressed sparse layers

Read the streams

×

Collapse the four streams into one vector per position, weighted by the reading weights.

hattn=∑jpjattn Xn,j{\htmlData{light=attention-read}{h_{\mathrm{attn}}}}=\sum_{j}{\htmlData{light=attention-read-weights attention-weights}{p^{\mathrm{attn}}_{j}}}\,X_{n,j}
in
XnX_n [B batch, T positions, 4 streams, 4096 d]pattnp^{\mathrm{attn}} [B batch, T positions, 4 streams]
out
hattnh_{\mathrm{attn}} [B batch, T positions, 4096 d]
Sources

In codetorch.sum(pre.unsqueeze(-1) * x.view(shape), dim=2) · 680

Open pinned code ↗

Direct evidence

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-Flash @ 60d8d70770c6

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)