Grouped-query attention

Row-wise softmax

×

Convert each query row into normalized attention weights.

A[b,i,t,s]=exp⁡(Sm[b,i,t,s])∑u=0T−1exp⁡(Sm[b,i,t,u]){\htmlData{light=weights softmax}{A}}[b,i,t,s]=\frac{\exp({\htmlData{light=causal}{S_{\mathrm m}}}[b,i,t,s])}{\sum_{u=0}^{T-1}\exp({\htmlData{light=causal}{S_{\mathrm m}}}[b,i,t,u])}
in
SmS_{\mathrm m} [B batch, 32 q heads, T queries, T keys]
out
AA [B batch, 32 q heads, T queries, T keys]

In float32, then cast back to the query type.

Explanation

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

Sources

In codeF.softmax(scores.float(), dim=-1).type_as(xq)

Open pinned code ↗

Direct evidence

Softmax normalizes the last score axis in float32 and restores the query dtype.

model.py 187 ↗ · meta-llama/llama3 @ a0940f9cf706

Excerpt
        scores = F.softmax(scores.float(), dim=-1).type_as(xq)