Merge and project

Merge heads

×

Join all retrieved head features into the model-width field.

[B, 32, T, 128][B,\,32,\,T,\,128]
→  [B, T, 32, 128]\to\;[B,\,T,\,32,\,128]
→  [B, T, 4096]\to\;[B,\,T,\,4096]
in
HH [B batch, 32 q heads, T positions, 128 d_h]
out
HH [B batch, T positions, 4096 d]
Explanation

Heads are concatenated in order: head ii fills features 128i,…,128i+127{128i},\dots,{128i+127}.

Sources

In codeoutput.transpose(1, 2).contiguous().view(bsz, seqlen, -1)

Open pinned code ↗

Direct evidence

The head output is transposed and reshaped into the full model-width field.

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)