32 independent transformer blocks › Attention
Query–key dot product
×no parameterstorch.matmul(xq, keys.transpose(2, 3))
Measure the feature agreement of rotated queries and their grouped keys.
- in
- [B batch, 32 q heads, T positions, 128 d_h] [B batch, 32 q heads, T positions, 128 d_h]
- out
- [B batch, 32 q heads, T queries, T keys]
Codemodel.py 179–184
xq = xq.transpose(1, 2) # (bs, n_local_heads, seqlen, head_dim)
keys = keys.transpose(1, 2) # (bs, n_local_heads, cache_len + seqlen, head_dim)
values = values.transpose(
1, 2
) # (bs, n_local_heads, cache_len + seqlen, head_dim)
scores = torch.matmul(xq, keys.transpose(2, 3)) / math.sqrt(self.head_dim)Sources1
Direct evidence
Queries multiply transposed keys over their common head-feature axis.
attn-dot · meta-llama/llama3 / llama/model.py
Pinned revision a0940f9cf706 ↗ · lines 179–184
Read code excerpt
xq = xq.transpose(1, 2) # (bs, n_local_heads, seqlen, head_dim)
keys = keys.transpose(1, 2) # (bs, n_local_heads, cache_len + seqlen, head_dim)
values = values.transpose(
1, 2
) # (bs, n_local_heads, cache_len + seqlen, head_dim)
scores = torch.matmul(xq, keys.transpose(2, 3)) / math.sqrt(self.head_dim)