Transformer Calculations
I’m embarrassed to admit I should have learned this ages ago. It took the CS336 assignments for me to finally understand it.
I’m assuming you already know the basic Transformer architecture and the concepts behind it.
Calculating parameter counts
Let’s use the CS336 architecture as our example.

LLMs look enormous, but much of their size comes from repeating Transformer blocks. Each block has three main components: multi-head attention (MHA), a feed-forward network (FFN), and RMSNorm. We’ll count the parameters in a block first, then move on to the rest of the model. A few abbreviations before we start:
$B$ = Batch size, $S$ = Seqence Length, $H = d$ = Hidden Layer Size, $V$ = Vocabulary size $d_{ff} = \frac{8}{3}H$
Transformer Block
RMSNorm
Start with the RMSNorm formula. Here $X \in R^{1\times d} = {x_1, x_2, …, x_d}$ and $g \in R^{1\times d}$:
$$
RMSNorm(x_i) = \frac{x_i}{RMS(X)}g_i\
RMS(X) = \sqrt{\frac{\sum_{i=1}^{d}{x_i^2}}{d} + \epsilon}
$$
So one RMSNorm layer has $H$ parameters.
MHA
For input X, MHA generally has $W_q, W_k, W_v, W_o \in R^{d \times d}$, giving $4H^2$ parameters. That’s it.
FFN
These days, SwiGLU is commonly used for the FFN. To keep its size comparable to a traditional FFN, its intermediate layer has size $d_{ff}= \frac{8}{3}d$, while a traditional FFN has $d_{ff} = 4d$. The two therefore have the same parameter count. For a traditional FFN:
$$
2 \times H \times 4H = 8H^2
$$
For SwiGLU, the formula is:
$$
SwiGLUFFN(x) = W_3(SiLU(W_2X)\otimes (W_1X))
$$
Here $W_1 \in R^{d \times d_{ff}}$, $W_2 \in R^{d \times d_{ff}}$, $W_3 \in R^{d_{ff} \times d}$, so the total is:
$$
3 \times d \times d_{ff} = 3 \times d \times \frac{8}{3}d = 8d^2 = 8H^2
$$
Each Transformer block has two RMSNorms, one MHA and one FFN. Add them up:
$$
2\times H + 1\times 4H^2 + 1\times 8H^2 = 12H^2 + 2H
$$
With $L$ Transformer blocks, the count becomes:
$$
L(12H^2 + 2H) = 2L(6H^2 + H)
$$
Embedding + Readout Layer
These two large matrices have opposite shapes: $W \in R^{V \times d}$, so together they have $2VH$ parameters.
Finally, add the H parameters in the separate final RMSNorm:
$$
2VH + 2L(6H^2 + H) + H
$$
The number of parameters we need to store depends only on $V, H, L$. For an example, let’s take GPT-3, the root of all evil. Its advertised 175B parameters come from this configuration:
| Item | Number |
|---|---|
| L | 96 |
| H | 12288 |
| V | 50257 |
| S | 2048 |
| A | 96 |
Let’s calculate the total:
1 | L = 96 |
175B—that checks out. How much disk space does it need? In bf16, each parameter takes 16 bits, or 2 bytes:
1 | P * 2 / 1024**3 |
326.3 GB. Not bad: you could carry that on a single SSD. But that only covers inference. What happens when we train?
AdamW parameter count
First, each parameter’s gradient takes up the same amount of space as the parameter itself.
Next, look at the AdamW formula:

Both $m$ and $v$ are the size of the model parameters: they record the first and second moments for each one. Together with the gradients, that gives us $3P$ extra values. We’re not done, though. Computing gradients also means saving values from the forward pass.
Activation count (trade speed for GPU memory)
If you’re desperate for GPU memory, you can avoid storing these values. I generally leave gradient checkpointing off because it’s too slow. Backpropagation needs each layer’s input; here’s an example that might make the reason clearer:
$$
Y = W_2\sigma(XW_1)
$$
This is a typical two-layer neural network. Differentiate with respect to $W_2$ and you get:
$$
\frac{\partial Loss}{\partial W_2} = \frac{\partial Loss}{\partial Y}\frac{\partial Y}{\partial W_2} = [\sigma(XW_1)]^T \frac{\partial Loss}{\partial Y}
$$
See why we need the input? We could recompute it instead of saving it, but that costs more computation. So let’s count the results we need to keep at each layer.
RMSNorm
From the formula above, an RMSNorm input is BSH. There are two inputs of that size, giving $2BSH$.
MHA
Here’s the MHA formula, which I’m sure everyone can recite from memory:
$$
MHA(X) = softmax(\frac{QK^T}{\sqrt{d}})V
$$
MHA first computes the Q, K, V and O projections. Their input is X, but the results must themselves be saved as inputs to later computations: $4BSH$. The result of $QK^T$ goes into softmax and has size $BAS^2$, where A is the number of attention heads. Softmax then feeds a multiplication by $V$, so we save another $BAS^2$.
So the MHA activations come to:
$$
4BHS + 2BAS^2
$$
FFN
SwiGLU produces more activations than a traditional FFN. Looking back at its formula, we save $W_1X, W_2X$ for the next step, or $2 \times BSd_{ff}$. We also save the SiLU result for the elementwise product, adding $BSd_{ff}$. That’s $3BSd_{ff} = 8BSH$ altogether. The output is $BSH$ and goes to the next normalization layer.
For one complete Transformer block, that gives:
$$
2BSH + 4BSH + 2BAS^2 + 8BSH = 14BSH + 2BAS^2
$$
Others
The final RMSNorm uses $BSH$, as does the input to the readout layer. If we calculate a loss from the readout, its input is $BSV$.
The total activation count is therefore:
$$
Activation = \underbrace{2BSH}{\text{Last RMS + Readout}}
+
\underbrace{BSV}{\text{Loss Function}}
+
\underbrace{L\left(
14BSH
+
2BAS^2
\right)}_{\text{all Transformer layers}}
$$
For training, the GPU memory requirement is:
$$
\underbrace{P}_\text{Model} + \underbrace{P}_\text{Gradient} + \underbrace{2P}_\text{AdamW} + Activation
$$
Back to GPT-3. Those two extra configuration values above are useful after all! Suppose we train with a batch size of one:
1 | S = 2048 |
Suppose the model uses bf16, the optimizer uses FP32, and the optimizer keeps an additional FP32 copy of P for higher-precision updates. The memory requirement is:
$$
\underbrace{2P}_\text{Model, BF16} + \underbrace{3\times4P}_\text{optimizer FP32} + \underbrace{2P}_\text{Gradient BF16} + \underbrace{2\times Activation}_\text{BF16} = 2817.73GB
$$
With B set to one, the batch-dependent activations account for 207.3 GB and grow in proportion to batch size. That gives us about 2.8 TB of GPU memory in total. An NVIDIA A100 has 80 GB, and a machine has eight cards:
1 | (16*P + 2*activation) / 1024 ** 3 / (80*8) |
So it takes five machines just to run it, and pipeline parallelism is unavoidable. The official run used B = 1536. No wonder it needed over ten thousand V100s.
Now that the V100 is increasingly obsolete, I wonder whether, like the swallows of old aristocratic halls, it might fly into ordinary homes. After endless AI exploitation, could the noble V100 that nurtured ChatGPT’s early form be sold affordably and enjoy its retirement in the homelabs of us hardware scavengers?
Calculating FLOPs
Parameter count and compute are not the same thing, so we need a separate calculation for FLOPs.
A quick refresher: much of an LLM’s work is matrix multiplication. Given $A \in R^{M \times N}, B \in R^{N \times K}, C = AB$, each element of $C \in R^{M \times K}$ comes from multiplying a row of A by a column of B and adding the results. That’s K multiplications and K−1 additions per element, or roughly 2K operations. The whole multiplication costs:
$$
2MNK
$$
Now let’s count the forward pass, using $N = B \times S$.
RMSNorm
This is mainly an [N, H] x [H] elementwise multiplication, taking $NH$ operations. It is not much and will be omitted below. Softmax is similar: GEMV-level, with too low a compute-to-memory ratio, bottlenecked by GPU memory.
MHA
The four QKVO projections cost $4 \times 2 \times NH^2 = 8NH^2$.
$QK^T$ is [N, H] x [H, N] = [N, N], costing $2HN^2$. $Score \times V$ is [N, N] x [N, H] = [N, H], also costing $2HN^2$, for a total of $4HN^2$.
Add those together:
$$
4HN^2 + 8NH^2
$$
FFN
Again, we’ll use SwiGLU. You can calculate the traditional FFN with the same method.
The two [N, H] x [H, d_ff] = [N, d_ff] operations cost $2 \times 2NHd_{ff}$. Mapping back to d, [N, d_ff] x [d_ff, H] = [N, H], costs another $2NHd_{ff}$. Together:
$$
6NHd_{ff} = 6NH \times \frac{8}{3}H = 16NH^2
$$
A Transformer block therefore costs:
$$
24NH^2 + 4HN^2 = 2N(12H^2 + 2HN)
$$
Earlier, we found that one layer has:
$$
P_0 = 12H^2 + 2H
$$
By proportional approximation, a model with $P$ parameters needs about $2NP$ operations for its forward pass. With $N = BS$, that’s about $2P$ per token.
Backpropagation
Backpropagation here is essentially backpropagation through matrix multiplications. For a linear layer we calculate two gradients:
- The gradient of W itself $\nabla W = X^T \nabla Y$
- The gradient of X, passed along$\nabla X = \nabla Y W^T$
That takes twice as much computation as the forward pass.
As a rough estimate, training FLOPs = three times forward FLOPs.
The forward pass takes roughly 2N times the parameter count, so training on one token takes about 6P operations.
That felt easier than working out the activations.
How long would GPT-3 take to train? We don’t know the total number of batches, so let’s use the 6P rule for a rough estimate. I found that it used 300B tokens:
1 | 300e9 * 6 * P |
An A100 delivers 312 TFLOPS in BF16. On one A100, that would take:
1 | 300e9 * 6 * P / 312e12 / 3600 / 24 |
More than ten thousand days! A human life only lasts thirty-odd thousand days. What if we use nine clusters of eight GPUs, ignoring communication latency and the like?
1 | 300e9 * 6 * P / (312e12 * 72) / 3600 / 24 |
What if we throw the current top-end GB200 NVL72 rack at it, with 72 clusters totaling about 180 PFLOPS?
1 | 300e9 * 6 * P / (180e15) / 3600 / 24 |
That’s eight times faster. The A100 launched in 2020 and the B200 in 2024, so NVIDIA delivered an eightfold increase in AI performance in four years—20% over the previous year each year. I wonder whether you’d call that incremental progress.
