Counting Parameters and FLOPs
How big is a model, how much memory does it need, and how long will training take? For transformers, a handful of formulas answer these questions to within a few percent, and every LLM practitioner uses them: about parameters, about floating-point operations per token for a forward pass, and about for training on tokens.
Parameters
From The Transformer Block: each of blocks holds about parameters (attention , MLP ), plus the embedding (and an untied output layer, another ):
Small terms (biases, norm gains, position tables) add well under 1%.
FLOPs per token
A matrix-vector product with an matrix costs multiplications and about as many additions: FLOPs. Every parameter in a dense layer is used once per token, so
plus attention scores: each layer computes and , about FLOPs per token for context length , which matters only when is comparable to or larger. The backward pass costs about twice the forward pass (gradients with respect to both activations and weights), so training costs
for training tokens.
def transformer(L, d, V, n_ctx, tied=True):
N_blocks = 12 * L * d * d
N_emb = V * d * (1 if tied else 2)
N = N_blocks + N_emb
flops_tok = 2 * N + 4 * L * n_ctx * d # matmuls + attention scores
return N, flops_tok
for name, cfg in {"GPT-2 small": (12, 768, 50257, 1024), "7B-class": (32, 4096, 32000, 4096),
"70B-class": (80, 8192, 32000, 4096)}.items():
N, f = transformer(*cfg)
print(f"{name:12s}: N = {N / 1e9:6.2f}B params, forward {f / 1e9:7.1f} GFLOPs/token "
f"(2N = {2 * N / 1e9:7.1f}), attention share {(f - 2 * N) / f:.1%}")GPT-2 small : N = 0.12B params, forward 0.3 GFLOPs/token (2N = 0.2), attention share 13.3%
7B-class : N = 6.57B params, forward 15.3 GFLOPs/token (2N = 13.1), attention share 14.0%
70B-class : N = 64.69B params, forward 140.1 GFLOPs/token (2N = 129.4), attention share 7.7%
Training time
Divide the total FLOPs by the hardware's sustained throughput: peak FLOP/s times model FLOPs utilization (MFU), typically 30 to 50%.
N, D = 7e9, 2e12 # 7B parameters, 2 trillion tokens
C = 6 * N * D
peak = 989e12 # bf16 dense peak of one H100-class GPU, FLOP/s
mfu = 0.4
gpu_seconds = C / (peak * mfu)
print(f"training compute: {C:.2e} FLOPs")
print(f"GPU-hours: {gpu_seconds / 3600:,.0f} on 1024 GPUs: {gpu_seconds / 1024 / 86400:.1f} days")training compute: 8.40e+22 FLOPs
GPU-hours: 58,982 on 1024 GPUs: 2.4 days
Memory
| What | Bytes per parameter (typical) |
|---|---|
| inference weights in bf16 | 2 |
| inference weights in 4-bit | about 0.5 |
| training: bf16 weights + bf16 gradients + fp32 master + Adam moments |
Training also needs activation memory, roughly proportional to batch sequence (reduced by checkpointing), and inference needs the KV cache.
Arithmetic intensity
A GPU is fast at FLOPs but comparatively slow at moving bytes. Arithmetic intensity = FLOPs per byte moved. Generating one token at batch size 1 reads every weight (2 bytes) to do 2 FLOPs with it: intensity about 1, far below the hundreds a GPU needs to be compute-bound. So small-batch inference is memory-bandwidth bound: tokens per second bandwidth / model size in bytes. Batching many requests reuses each weight read, which is why serving systems batch aggressively.
In software
sum(p.numel() for p in model.parameters())counts parameters; profilers (torch.profiler,fvcore) count FLOPs.- Papers report training compute in FLOPs (e.g. to for frontier models) and hardware in GPU-hours.
- The scaling laws are written in terms of , and .
Exercises
Exercise 1: Forward FLOPs
How many FLOPs does a 13B-parameter model need to process a 1000-token prompt (ignoring attention)?
Answer
.
Solution
FLOPs.
Exercise 2: Decode speed bound
A 7B model in bf16 runs on a GPU with 3.35 TB/s memory bandwidth. Estimate the maximum tokens per second at batch size 1.
Answer
About 240 tokens/s.
Solution
Each token reads all GB of weights: . Real systems reach somewhat less.
Exercise 3: Compute budget (code)
How many tokens can a 1B-parameter model be trained on with FLOPs?
Answer
About tokens.
Solution
.
print(f"{1e21 / (6 * 1e9):.2e}")1.67e+11
Test yourself
A forward pass of an -parameter transformer costs roughly how many FLOPs per token?
Training on tokens costs about…
Why is single-request LLM generation usually memory-bandwidth bound?
- Because of the softmax
- Every weight must be read from memory for only about 2 FLOPs of work per token
- GPUs have little memory
- Attention is quadratic