FLOPs per token: 2N forward, 6N training
Derive about 2N FLOPs per token for a forward pass (ignoring the context-dependent attention term) and about 6N per token for training. Use C ≈ 6ND to estimate the cost of a training run.
Builds on: Counting transformer parameters, Counting FLOPs of matrix multiplies, Backpropagation as reverse-mode autodiff
What the animation says
A bill from two numbers
Before you train a model, you want its bill: how many GPU-hours will this run eat? Counting every matrix multiply, in every layer, for every token, is tedious. It turns out two numbers are enough: the parameter count, N, and the number of training tokens, D. Compute is almost exactly proportional to N. Let's see why, and where the factors two and six come from.
One weight, one multiply-add
Take one token passing through a weight matrix: two inputs, three outputs, six weights. Each weight is used exactly once. It multiplies its input entry, and the product is added into its output. One multiply plus one add: two FLOPs per weight, per token. Six weights, twelve FLOPs. Nearly all parameters sit in matrices like this, so a forward pass costs about two N per token. Training adds a backward pass. How many FLOPs per weight, per token, does backward add?
Backward is two matmuls
Four. Backward runs two matrix multiplies, each the same size as forward. One sends the gradient back to the inputs: the output gradient times W transposed. The other builds the gradient for the weights: the inputs transposed, times the output gradient. Each costs two FLOPs per weight per token, so backward is four N. Plus forward's two N, training is six N per token. Over D tokens, a whole run costs about six times N times D. So: a seven-billion-parameter model on two trillion tokens. Roughly how much compute?
From FLOPs to days
Six times seven billion times two trillion: 8.4 times ten to the twenty-two FLOPs. Now the hardware: a thousand GPUs, each peaking at ten to the fifteen FLOPs per second, running at forty percent utilization. Together, that's four times ten to the seventeen FLOPs every second. Divide: about two hundred ten thousand seconds. That's fifty-eight hours, roughly two point four days. Once trained, generating one token with a short context is just the forward pass: two N, fourteen billion FLOPs.
Where the rule bends
Let's check the factor two on a real block: width 4096, thirty-two layers. This line lists the four attention matrices and the two feed-forward ones. Forward FLOPs over parameters: exactly two. The rule ignores attention scores: comparing each token with up to T positions costs about four T d per block. The weights cost twenty-four d squared. So the ignored part is T over six d of the counted part. With d of 4096, at which context length does attention cost as much as the weights?
Every weight does one multiply-add per token
2N forward, 6N training, C ≈ 6ND
7B parameters, 2 trillion tokens, 1000 GPUs
Check: matmul FLOPs = 2 × params per token
d, L, T = 4096, 32, 1
# W_Q, W_K, W_V, W_O (all heads), FFN up, FFN down (d_ff = 4d)
mats = [(d, d)] * 4 + [(d, 4 * d), (4 * d, d)]
params = sum(a * b for a, b in mats) * L
fwd = sum(2 * T * a * b for a, b in mats) * L # (T x a)(a x b): 2*T*a*b
print(params / 1e9, fwd / T / params) # ~6.44 billion params, 2.0 FLOPs/param/token
D = 1e12
print(f"training C = {6 * params * D:.2e} FLOPs") # ~3.87e22
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses