Indexer queries

Hadamard rotation

×

Mix the 128 features with a scaled Hadamard transform.

QIˉ=QI~ 1128H128{\htmlData{light=output hadamard}{\bar {\htmlClass{vl-stream-q}{Q^{I}}}}}={\htmlData{light=rotated rotate}{\tilde {\htmlClass{vl-stream-q}{Q^{I}}}}}\,\tfrac{1}{\sqrt{128}}\mathbf{H}_{128}
in
Q~I\tilde Q^{I} [B batch, 64 index heads, 1 position, 128 index d_h]
out
QˉI\bar Q^{I} [B batch, 64 index heads, 1 position, 128 index d_h]

Followed by FP8 quantization of queries and keys in groups of 128 features (model.py 474–475).

Explanation

The transform is orthonormal, so products between queries and keys are unchanged.

Spreading outlying features across all coordinates keeps the FP8 quantization error small.

Sources

In coderotate_activation(q)

Open pinned code ↗

Direct evidence

The indexer rotates its queries and keys with a scaled Hadamard transform before quantizing them.

model.py 428–432 ↗ · deepseek-ai/DeepSeek-V3.2-Exp @ 87e509a2e5a1

Excerpt
def rotate_activation(x: torch.Tensor) -> torch.Tensor:
    assert x.dtype == torch.bfloat16
    from fast_hadamard_transform import hadamard_transform
    hidden_size = x.size(-1)
    return hadamard_transform(x, scale=hidden_size ** -0.5)

Indexer scores combine per-head query–key products with learned per-head weights, add the causal mask and keep the top-k positions for each query.

model.py 457–487 ↗ · deepseek-ai/DeepSeek-V3.2-Exp @ 87e509a2e5a1

Excerpt
    def forward(self, x: torch.Tensor, qr: torch.Tensor, start_pos: int, freqs_cis: torch.Tensor, mask: Optional[torch.Tensor]):
        bsz, seqlen, _ = x.size()
        end_pos = start_pos + seqlen
        q = self.wq_b(qr)
        q = q.view(bsz, seqlen, self.n_heads, self.head_dim)
        q_pe, q_nope = torch.split(q, [self.rope_head_dim, self.head_dim - self.rope_head_dim], dim=-1)
        # rope in indexer is not interleaved
        q_pe = apply_rotary_emb(q_pe, freqs_cis, False)
        q = torch.cat([q_pe, q_nope], dim=-1)
        k = self.wk(x)
        k = self.k_norm(k)
        k_pe, k_nope = torch.split(k, [self.rope_head_dim, self.head_dim - self.rope_head_dim], dim=-1)
        # rope in indexer is not interleaved
        k_pe = apply_rotary_emb(k_pe.unsqueeze(2), freqs_cis, False).squeeze(2)
        k = torch.cat([k_pe, k_nope], dim=-1)
        q = rotate_activation(q)
        k = rotate_activation(k)
        q_fp8, q_scale = act_quant(q, block_size, self.scale_fmt)
        k_fp8, k_scale = act_quant(k, block_size, self.scale_fmt)
        self.k_cache[:bsz, start_pos:end_pos] = k_fp8
        self.k_scale_cache[:bsz, start_pos:end_pos] = k_scale
        weights = self.weights_proj(x.float()) * self.n_heads ** -0.5
        weights = weights.unsqueeze(-1) * q_scale * self.softmax_scale
        index_score = fp8_index(q_fp8.contiguous(), weights, self.k_cache[:bsz, :end_pos].contiguous(), self.k_scale_cache[:bsz, :end_pos].contiguous())
        if mask is not None:
            index_score += mask
        topk_indices = index_score.topk(min(self.index_topk, end_pos), dim=-1)[1]
        topk_indices_ = topk_indices.clone()
        dist.broadcast(topk_indices_, src=0)
        assert torch.all(topk_indices == topk_indices_), f"{topk_indices=} {topk_indices_=}"
        return topk_indices