Causal attention

Softmax

×

Normalize each query row over key positions.

A[b,i,t,s]=exp⁡(S~[b,i,t,s])∑u=0T−1exp⁡(S~[b,i,t,u]){\htmlData{light=weights-state softmax}{A}}[b,i,t,s]=\frac{\exp({\htmlData{light=masked causal}{\tilde S}}[b,i,t,s])}{\sum_{u=0}^{T-1}\exp({\htmlData{light=masked causal}{\tilde S}}[b,i,t,u])}
in
S~\tilde S [B batch, 128 heads, T queries, T keys]
out
AA [B batch, 128 heads, T queries, T keys]

In float32 on the CPU backend, which casts queries, keys and values to float32 (cpu_sdpa.py 65–67, 78); on a GPU the flash-attention kernel computes it (flash_attn.py 463–481).

Explanation

bb, ii, tt and ss index batch, head, query and key; uu runs over the keys.

The cards follow vLLM’s CPU prefill backend, which writes attention out in PyTorch (cpu_sdpa.py 65–79); on a GPU the flash-attention backend computes the same in one kernel (flash_attn.py 463–481).

Sources

In codetorch.softmax(attn_scores, dim=-1, dtype=torch.float32)

Open pinned code ↗

Direct evidence

On the prefill path kv_b_proj rebuilds each head’s content keys and values from the normalized latent, each head’s key joins its content part and the shared rotary key, and the prefill backend runs attention over the new tokens.

mla_attention.py 3207–3227 ↗ · vllm-project/vllm @ 5df63a7203cd

Excerpt
        kv_nope = self.kv_b_proj(kv_c_normed)[0].view(
            -1, self.num_heads, self.qk_nope_head_dim + self.v_head_dim
        )
        k_nope, v = kv_nope.split([self.qk_nope_head_dim, self.v_head_dim], dim=-1)
        k = self._concat_k_nope_k_pe(k_nope, k_pe)

        if use_fp8_prefill:
            k = k.to(prefill_metadata.q_data_type)
            v = v.to(prefill_metadata.q_data_type)

        output_prefill = prefill_metadata.prefill_backend.run_prefill_new_tokens(
            q=q,
            k=k,
            v=v,
            return_softmax_lse=has_context,
            out=(
                output.view(-1, self.num_heads, self.v_head_dim)
                if output_scale is not None
                else None
            ),
            output_scale=output_scale,