21/42
Part 4 of 6 · Shapes and the maskLearn

Every shape, from (B, T, C) in to (B, T, C) out

Here's the whole forward pass, one line per step.

StepShape, and what it is
x(B, T, C), the input
self.qkv(x)(B, T, 3C): q, k and v together
.split(C, dim=2)(B, T, C) each: q, k and v
.view(B, T, H, d)(B, T, H, d): channels cut into heads
.transpose(1, 2)(B, H, T, d): heads first
q @ k.transpose(-2, -1)(B, H, T, T): the scores
/ math.sqrt(d)(B, H, T, T): scaled
.masked_fill(...)(B, H, T, T): the future set to -inf
.softmax(dim=-1)(B, H, T, T): the weights
att @ v(B, H, T, d): the values blended
.transpose(1, 2)(B, T, H, d): positions first
.reshape(B, T, C)(B, T, C): heads merged
self.proj(y)(B, T, C): the output

d=C/Hd = C / H the whole way down. The scores are the only step with two TT axes: one row per query, one column per key.