Retrieved head outputs
×A weighted combination of values for each query position and head.
- shape
- [B batch, 32 q heads, 1 position, 128 d_h]
Sources
In codeoutput
Contextual evidence
The head outputs are transposed back and flattened before the output projection.
model.py 188–189 ↗ · meta-llama/llama3 @ a0940f9cf706
Excerpt
output = torch.matmul(scores, values) # (bs, n_local_heads, seqlen, head_dim)
output = output.transpose(1, 2).contiguous().view(bsz, seqlen, -1)Attention weights multiply the matching grouped values to produce a field of head outputs.
model.py 175–188 ↗ · meta-llama/llama3 @ a0940f9cf706
Excerpt
values = repeat_kv(
values, self.n_rep
) # (bs, cache_len + seqlen, n_local_heads, head_dim)
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)
if mask is not None:
scores = scores + mask # (bs, n_local_heads, seqlen, cache_len + seqlen)
scores = F.softmax(scores.float(), dim=-1).type_as(xq)
output = torch.matmul(scores, values) # (bs, n_local_heads, seqlen, head_dim)