All courses

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

▶ Watch this lesson Start the whole course

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?

Budgeting a training run from two numbers

Before training a model you want to know how many GPU-hours it will cost. Counting every matmul in every layer for every token is tedious. It turns out you need only two numbers: the parameter count $N$ and the number of training tokens $D$. Why is compute almost exactly proportional to $N$, and where do the factors 2 and 6 come from?

Every weight does one multiply-add per token

In $XW$, each weight $w_{ij}$ is used once per token: it multiplies that token's input entry $i$, and the product is added into output $j$. That is 1 multiply + 1 add = **2 FLOPs per weight per token**. Almost all non-embedding parameters $N$ sit in such matrices, so a forward pass costs about $2N$ per token. The embedding lookup only copies a row, so it costs nothing. The unembedding is a $d\times V$ matmul costing $2dV$ per token. It is not part of $N$ and is usually left out, because the blocks dwarf it in large models.

2N forward, 6N training, C ≈ 6ND

For $X\in\mathbb{R}^{T\times d_{in}}$, $W\in\mathbb{R}^{d_{in}\times d_{out}}$: forward $XW$ costs $2Td_{in}d_{out}=2T\cdot$(weights in $W$). Backward does two matmuls of the same size per $W$: - input gradient $dY\,W^\top$: $(T\times d_{out})(d_{out}\times d_{in})$ - weight gradient $X^\top dY$: $(d_{in}\times T)(T\times d_{out})$ So backward costs $4N$ per token. $$\text{forward}\approx 2N,\quad \text{training}\approx 6N\ \text{FLOPs/token},\quad C\approx 6ND$$ Ignored: per token per block, $QK^\top$ and $AV$ each cost ~$2Td$ (dot products with up to $T$ positions, summed over heads), so ~$4Td$ in total. The weight matmuls cost $2\cdot 12d^2=24d^2$ ($4d^2$ attention + $8d^2$ FFN with $d_{ff}=4d$). The ratio is $T/(6d)$, so the attention term is negligible when $T\ll 6d$.

7B parameters, 2 trillion tokens, 1000 GPUs

$N=7\times10^9$, $D=2\times10^{12}$. $C=6\cdot 7\times10^9\cdot 2\times10^{12}=8.4\times10^{22}$ FLOPs. 1000 GPUs, each with a peak of $10^{15}$ FLOP/s, at 40% utilization: $4\times10^{17}$ FLOP/s in total. Time $=8.4\times10^{22}/4\times10^{17}=2.1\times10^5$ s ≈ 58 h ≈ 2.4 days. Later, with a short context, generating one token costs $2N=1.4\times10^{10}$ FLOPs.

Check: matmul FLOPs = 2 × params per token

Sum the FLOPs of a block's weight matmuls for one token and compare with the parameter count. The ratio comes out at exactly 2. Then estimate a training run with $6ND$.
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

Each weight costs one multiply-add (2 FLOPs) per token in the forward pass. Backward costs twice that. With $N$ the non-embedding parameters, inference costs ≈ $2N$ FLOPs per token, training ≈ $6N$, and a whole run costs $C\approx 6ND$. This holds while context length $T$ is small compared with $6d$.

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.