Causal mask
\[
M_{ij}=\begin{cases}0,&j\le i,\\-\infty,&j>i.\end{cases}
\]
Add it before softmax. Future weights then become zero.
Guided reference · 5 steps
Follow the residual stream through attention, verify every tensor shape, inspect masking and softmax, then finish with normalization and SwiGLU.
Step 1 · See the complete path before opening it
Step 2 · Keep the tensor-shape ledger
| Quantity | Shape | Invariant to remember |
|---|---|---|
| \(X,\bar X,Q,K,V\) | \(B\times T\times d_{\text{model}}\) | Projection acts on the final feature axis. |
| Split Q, K, V | \(B\times h\times T\times d_h\) | \(d_{\text{model}}=h\,d_h\); every head keeps all \(T\) tokens. |
| Scores and probabilities | \(B\times h\times T\times T\) | Rows are queries; columns are keys. |
| One head output | \(B\times T\times d_h\) | \(P_rV_r\) blends value vectors. |
| Concatenated/output state | \(B\times T\times d_{\text{model}}\) | Required for residual addition. |
Step 3 · Add position, then prevent peeking
Add it before softmax. Future weights then become zero.
With the course's counterclockwise convention, the sign is \(n-m\).
Step 4 · Turn one score row into weights
The three displayed rounded finite weights total \(1.001\); the unrounded values total exactly \(1\).
Step 5 · Normalize and transform each token's features
See RESOURCES.md for the primary papers and Lesson 23 for a fully substituted walkthrough.