Unit 6 of 7 · about 4 hours · derive, measure
Parameter, FLOP and memory accounting for attention
Attention scales quadratically in sequence length. The budget must be derived before launch, because the out-of-memory error arrives after the design is fixed.
You'll be able to
- Derive attention and MLP parameter, FLOP, score-memory and activation costs across batch, heads, sequence and width.
- Measure score materialization for a stated shape and report it in correct binary units.
- Explain which term dominates in short- versus long-sequence regimes and what the math-backend upcast changes.
Covers
Read
In this order, only the parts listed
0 of 3 done
- Complexity, sequential ops and path lengthsD2L - attention and transformer implementation route
Comparing CNNs, RNNs, and Self-Attention: with ops and path for CNNs, with ops and path for RNNs, with ops and path for self-attention
- Quadratic attention and model sizesUnderstanding Deep Learning - Transformers
Section 12.2.1 (p. 209) for attention weights independent of width; Section 12.9 (pp. 227 to 228) for roughly half the interactions in decoders and the sparsification gallery; BERT and GPT-3 parameter listings (pp. 219 and 224 to 225) as worked sizes
Treat the sparsification gallery as orientation only; sparse kernels are a later topic.
- Backends and precision behaviorPyTorch - scaled dot product attention
The three-implementation Note with backend selection functions, and the Numerical accuracy paragraph: math backend keeps
fp32intermediates forhalfandbf16inputs, and outputs may differ by backendSkip grouped-query attention; it belongs to decoder specialization.
Focus on
- Count with multiply-add as 2 FLOP: QKVO projections , scores plus weighting , width- MLP . At 512 and 1024 that is about 5.37 GFLOP for attention and 8.59 GFLOP for the MLP.
- Score memory is heads times entries in the working dtype: 16 heads at 512 in
fp32is exactly 16 MiB, 1 MiB per head. At 4096 the same block needs 1 GiB. - Causal masking keeps pairs, so the necessary score FLOPs roughly halve, as UDL notes. A dense matmul followed by a mask still computes all scores; only a kernel that skips masked tiles saves the work, and only a fused kernel avoids materializing the scores.
- The math backend upcasts
halfandbf16intermediates tofp32, so measured bytes can exceed a naive dtype-times-entries count. Fused backends can change numerics without changing the asymptotic count.
Common mistakes · 3
- Mixing MACs with FLOPs. One multiply-add is 2 FLOP; state the convention or totals disagree by exactly 2x.
- Quoting GB for GiB quantities. 16 times times 4 bytes is bytes: 1 GiB, not 1 GB.
- Trusting fused-kernel numerics to match the math path bit for bit. The docs warn outputs may differ by backend; test within tolerance, not equality.
Try it yourself
Closed-book, before checking anything
fp32: attention parameters 4.19M, MLP parameters 8.39M, score memory 16 MiB, attention forward about 5.37 GFLOP. Then scale scores to n 4096 and show 1 GiB.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.How many score bytes do 8 heads at 1024 in
fp32need?Answer8 times entries times 4 bytes equals 33,554,432 bytes, which is 32 MiB.
2.Why does decoder attention need only about half the encoder score FLOPs, and when is the saving real?Answer
Causality keeps the lower triangle: pairs instead of , roughly half for long sequences. A dense matmul plus mask still spends all ; the saving is real only in a kernel that skips masked blocks.
3.How many bias-free attention parameters does one BERT-width layer hold?Answer
4 times equals 4,194,304, about 4.19M. Biases would add 4 times 1024 on top.
4.Sequence length grows from 512 to 4096 at fixed width. What scales by 64, and what becomes the main cost?Answer
Score entries, bytes and score FLOPs all scale by equals 64, while projection FLOPs scale by 8 and weights stay fixed. The score tensor grows from 16 MiB to 1 GiB per layer and example, so it is usually the first memory limit. Since is now above , score FLOPs also exceed the projection FLOPs.
Practice
- D2L Attention Scoring Functions: Exercise 3 (cost scaling)
Exercises: cost versus key, query and value dimensionality and count, plus the memory-bandwidth follow-up
Answer with the derived plus model and the 512-case numbers, stating the multiply-add convention.
- TensorTonic: Transformer Training FLOP Estimator
The estimator problem in the PyTorch and einops group of the free language-modeling-from-scratch plan
It estimates one training step from forward matrix multiplications plus a supplied forward attention cost. Match its forward matmul terms to your and counts, then document how it counts the backward pass and every term it includes or leaves out.
You're done when
Your hand table matches measured score bytes and a profiler FLOP count within stated conventions, and you can name the dominant attention term on either side of equals ( once the MLP is included).
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 2 more in the section
- Understanding Deep Learning - TransformersSimon J. D. Prince · primary
Use Chapter 12.1-12.11 and the matching notebooks for dot-product self-attention, extension to transformers, transformer layers and vision transformers.
- D2L - attention and transformer implementation routeD2L authors · required
Use queries-keys-values, attention-scoring-functions, multihead-attention, self-attention-and-positional-encoding, transformer and vision-transformer. Implement masked single-head attention, multi-head attention and one pre-normalized block before comparing with PyTorch.
- PyTorch - scaled dot product attentionPyTorch · reference
Use the official function documentation as a differential oracle for shapes, masks, dropout and backend behavior after the manual version passes intervention tests.
Also in this section
- Deep-ML candidate - attention problemsDeep-ML · optional
Use 490 Scaled Dot-Product Attention as the fifth platform admission sample. If the judge catches a seeded masking error, use at most one of 490, 491, 53, 904 or 905 as a changed-prompt drill.
- TrenTorch - attention and block questionsTrenTorch · support
Solve the multi-head split, sinusoidal positional encoding, causal mask and pre-norm versus post-norm questions the units name, then carry the same checks into your own block.