29/42
Part 5 of 6 · The check and the projectionLearn and do

Without the check, the error comes late and never mentions heads

Here's what happens without the check.

qkv = nn.Linear(10, 30)
q, k, v = qkv(torch.randn(1, 4, 10)).split(10, dim=2)
try:
    q.view(1, 4, 3, 10 // 3)
except RuntimeError as e:
    print(e)
shape '[1, 4, 3, 3]' is invalid for input of size 40

Ten channels don't fit in three heads of three. With C = 10 and H = 3, d = 10 // 3 = 3:

HeadChannels
00, 1, 2
13, 4, 5
26, 7, 8
none9

nn.Linear(10, 30) is a valid layer, and it knows nothing about heads. So the module builds fine, and the error waits for the first forward pass.

In the editor, finish __init__: the check, self.n_head and the fused self.qkv. It prints the layer's weight shape, then tries 10 channels in 3 heads.