Sparse attention

Mix and project

×

Mix each head’s values by its weights, join the 128 heads and project back to 7,168 features.

Oi=O~i WUV,iO_i=\tilde O_i\,W_{UV,i}
Δattn=oWO\Delta_{\mathrm{attn}}=oW_{O}
All 4 steps
O~i=Ai c^KV\tilde O_i=A_i\,\hat c^{KV}
Oi=O~i WUV,iO_i=\tilde O_i\,W_{UV,i}
o=reshape⁡(O)o=\operatorname{reshape}(O)
Δattn=oWO\Delta_{\mathrm{attn}}=oW_{O}
in
AA [B batch, 128 heads, 1 query, 1001 keys]c^KV\hat c^{KV} [B batch, 1001 cached positions, 512 kv latent]
out
Δattn\Delta_{\mathrm{attn}} [B batch, 1 position, 7168 d]
Parameters117,440,512 × 58 layers

117,440,512 parameters per instance, across its learned components

ComponentCalculationParameters
Output projection16,384 × 7,168117,440,512

One in each of the 58 MoE layers, each with its own weights.

Explanation

The output projection is the only step that mixes heads.

Sources

In codex · 589–607

Open pinned code ↗

Direct evidence

When decoding one token, the implementation absorbs the key–value up-projection into queries and outputs and attends over the cached latent directly.

model.py 590–606 ↗ · deepseek-ai/DeepSeek-V3.2 @ a7e62ac04ecb

Excerpt
        else:                   # MQA decode
            if self.dequant_wkv_b is None and self.wkv_b.scale is not None:
                self.dequant_wkv_b = weight_dequant(self.wkv_b.weight, self.wkv_b.scale)
            wkv_b = self.wkv_b.weight if self.dequant_wkv_b is None else self.dequant_wkv_b
            wkv_b = wkv_b.view(self.n_local_heads, -1, self.kv_lora_rank)
            q_nope = torch.einsum("bshd,hdc->bshc", q_nope, wkv_b[:, :self.qk_nope_head_dim])
            scores = (torch.einsum("bshc,btc->bsht", q_nope, self.kv_cache[:bsz, :end_pos]) +
                      torch.einsum("bshr,btr->bsht", q_pe, self.pe_cache[:bsz, :end_pos])) * self.softmax_scale

            # indexer
            topk_indices = self.indexer(x, qr, start_pos, freqs_cis, mask)
            index_mask = torch.full((bsz, 1, end_pos), float("-inf"), device=x.device).scatter_(-1, topk_indices, 0)
            scores += index_mask.unsqueeze(2)

            scores = scores.softmax(dim=-1)
            x = torch.einsum("bsht,btc->bshc", scores, self.kv_cache[:bsz, :end_pos])
            x = torch.einsum("bshc,hdc->bshd", x, wkv_b[:, -self.v_head_dim:])

Head outputs are flattened and projected back to the model width.

model.py 607–608 ↗ · deepseek-ai/DeepSeek-V3.2 @ a7e62ac04ecb

Excerpt
        x = self.wo(x.flatten(2))
        return x