Grouped-query attention

Row-wise softmax

×

Convert each query row into normalized attention weights.

A[b,i,t,s]=exp⁡(Ss[b,i,t,s])∑u=01000exp⁡(Ss[b,i,t,u]){\htmlData{light=weights softmax}{A}}[b,i,t,s]=\frac{\exp({\htmlData{light=scale}{S_{\mathrm s}}}[b,i,t,s])}{\sum_{u=0}^{1000}\exp({\htmlData{light=scale}{S_{\mathrm s}}}[b,i,t,u])}
in
SsS_{\mathrm s} [B batch, 32 q heads, 1 query, 1001 keys]
out
AA [B batch, 32 q heads, 1 query, 1001 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)