import math
import torch
from torch import nn
torch.manual_seed(0)
B, T, C, H = 2, 5, 12, 4
d = C // H
qkv = nn.Linear(C, 3 * C)
x = torch.randn(B, T, C)
out = qkv(x)
print(qkv.weight.shape)
print(out.shape)
torch.Size([36, 12])
torch.Size([2, 5, 36])
It stores its weight out by in, 3C×C, plus a bias of 3C. It works on the last axis, so every position is projected the same way.
Where the 3H small projections went:
You could build 3H small Linear layers. One fused layer does the same math in one matmul: x is read once, and the GPU gets one big job instead of many small ones. This is the fused projection the practice asks for.