Cutting the channels into heads takes two calls.
(2, 5, 12) viewed as (B, T, H, d) with H = 4. What's the new shape?
qh = q.view(B, T, H, d) print(qh.shape) qh = qh.transpose(1, 2) print(qh.shape)
torch.Size([2, 5, 4, 3]) torch.Size([2, 4, 5, 3])
Two calls, in this order:
Neither call copies: view splits the last axis, and transpose changes the order PyTorch reads the memory in. Each head is now a (T, d) grid, a whole sequence of its own.
view
transpose
(T, d)