Chapter 4: All the Transformer Math You Need to Know
Scaling Book Exercises – Chapter 4
Chapter: 4 – All the Transformer Math You Need to Know
Source: handwritten Onyx Boox A4 PDF
Note: Transcribed from handwritten solutions; book markdown used only for exercise statements and notation.
Exercise 1 – Parameter count, attention fraction, and KV cache
Exercise statement
How many parameters does a model with , , , and have? What fraction of these are attention parameters? How large are our KV caches per token? You can assume and multi-head attention with int8 KVs.
Solution
Scan pages: 1–2
The total parameters of a model are:
If we assume , , we get:
Let , , . Hence:
Attention params are hence:
so the fraction is:
The size of the KV cache is and since , we get per token i.e.
Exercise 2 – FLOPs under sharding
Exercise statement
How many total FLOPs are required to perform on {'X': 4, 'Y': 8, 'Z': 4}. How many FLOPs are performed by each TPU?
Solution
Scan pages: 2
Let .
Assume the intended operation is . Each device performs
FLOPs. Hence total FLOPs is:
Exercise 3 – FLOPs for a tensor contraction
Exercise statement
How many FLOPs are involved in performing ?
Solution
Scan pages: 3
Assume the intended operation is:
by means of:
we do FLOPs.
Exercise 4 – Self-attention arithmetic intensity and effective cost
Exercise statement
What is the arithmetic intensity of self-attention, ignoring the Q/K/V/O projections? Give the answer as a function of the Q and KV lengths and . At what context length is attention FLOPs-bound? Given the HBM bandwidth of our TPUs, plot the effective relative cost of attention to the FFW block as the context length grows.
Solution
Scan pages: 4–8
Assume our naive attention algorithm writes every intermediate result back to HBM and also that . Hence we do:
- .
- .
- .
Assume that .
Step 1:
Step 2:
Step 3:
Assume Flash Attention, so does not need to be in HBM.
Let :
If , we are FLOPs bound. If we assume v5e, then , hence .
If by effective relative cost they mean:
where is context length, not time.
Assume so that in the FFW block we become compute bound, hence:
Therefore:
If , then:
If , then:
Assume :
Exercise 5 – Attention FLOPs vs. QKVO projection FLOPs
Exercise statement
At what sequence length are self-attention FLOPs equal to the QKVO projection FLOPs?
Solution
Scan pages: 9
Assuming FLOPs without training, , , and . Then we have to solve for:
So at sequence length, self-attention FLOPs equal the QKVO projection FLOPs.
Exercise 6 – Rematerialization FLOPs
Exercise statement
Say we only save the output of each of the 7 main matmuls in a Transformer layer during our forward pass, namely Q, K, V, O + the three FFW matrices. How many extra FLOPs do we need to “rematerialize” during the backwards pass?
Solution
Scan pages: 9–12
Define a general layer as:
In a computational graph, during the backward pass, it will receive , and it will compute , and send down the graph. Note that in general:
so it depends on as well. With this in mind, we can see that if we draw the computational graph we will need to rematerialize:
- for .
- for .
- for .
where we have ignored the layernorm operations. Therefore we need:
to rematerialize during the backward pass.
Exercise 7 – DeepSeek V3 utilization
Exercise statement
DeepSeek v3 says it was trained for 2.79M H800 hours on 14.8T tokens. Given that it has 37B activated parameters, roughly what hardware utilization did they achieve? Hint: note that they used FP8 FLOPs without structured sparsity.
Solution
Scan pages: 12
FLOPs for V3 = .
.
Available FLOPs in H800 hours, if using FP8 = .
Hardware Utilization:
Exercise 8 – MoE compute-bound batch size
Exercise statement
Mixture of Experts (MoE) models have copies of a standard dense MLP block, and each token activates of these experts. What batch size in tokens is required to be compute-bound for an MoE with weights in int8 on TPU v5e? For DeepSeek, which has 256 routed experts and , what is this number?
Solution
Scan pages: 13–15
Assume training FLOPs. copies of standard MLP blocks, and each token activates of these experts. Consider the following model of MoE:
where and .
To compute the MoE layer, we will group the tokens belonging to an expert and then matmul. So in total we will do matmuls. Assume that every token randomly chooses numbers from .
Define:
where if token has sampled .
The expected value of gives us the expected value of the batch dimension of the matmul of expert :
So then we do:
For e in {1,...,E}
1: [T*k/E, D] @ int8[D, D]
Therefore:
Therefore for being compute bound:
So for DeepSeek V3 this number is . (Note: While transcribing this execirse from the handwritten notes, I realized that I made an error and assumed that the operations were done in int8, but they are done in FP16, so I’ve corrected the of by 2 error in this transccribed notes, in the handwritten notes is not corrected).