The diagram you know from the papers, the shape at every step, and the PyTorch that makes it — the same block three ways. Hover any box, shape, or line of code and the matching two light up.
# x: your tokens (n, d) xQ, K, V = x@Wq, x@Wk, x@Wv # (n, dₖ) eachscores = Q @ K.transpose(-2,-1) / dₖ**0.5 # (n, n)scores = scores.masked_fill(mask, -inf) # (n, n)A = scores.softmax(dim=-1) # (n, n) rows→1out = A @ V # (n, dₖ)
◆ derived shapes & dataflow from the standard
attention definition · ◇ illustrative n·dₖ are
example sizes. Want a shape proven for every n? /research prove.