TExcelTAD
Home › Math for Deep Learning & LLMs › Counting Parameters and FLOPs

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 12Ld212Ld^2 parameters, about 2N2N floating-point operations per token for a forward pass, and about 6ND6ND for training on DD tokens.

Parameters

From The Transformer Block: each of LL blocks holds about 12d212d^2 parameters (attention 4d24d^2, MLP 8d28d^2), plus the embedding VdVd (and an untied output layer, another VdVd):

N≈12Ld2+VdN \approx 12Ld^2 + Vd

Small terms (biases, norm gains, position tables) add well under 1%.

FLOPs per token

A matrix-vector product with an m×nm \times n matrix costs mnmn multiplications and about as many additions: 2mn2mn FLOPs. Every parameter in a dense layer is used once per token, so

Cforward≈2N FLOPs per tokenC_{\text{forward}} \approx 2N \text{ FLOPs per token}

plus attention scores: each layer computes QKTQK^T and AVAV, about 4nd4nd FLOPs per token for context length nn, which matters only when nn is comparable to dd or larger. The backward pass costs about twice the forward pass (gradients with respect to both activations and weights), so training costs

Ctrain≈6ND FLOPsC_{\text{train}} \approx 6ND \text{ FLOPs}

for DD training tokens.

python · try it yourself
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%.

python · try it yourself
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 2+2+4+4+4=162 + 2 + 4 + 4 + 4 = 16

Training also needs activation memory, roughly proportional to batch ×\times sequence ×\times dd ×\times LL (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 ≈\approx 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. 102310^{23} to 102510^{25} for frontier models) and hardware in GPU-hours.
  • The scaling laws are written in terms of NN, DD and C=6NDC = 6ND.

Exercises

Exercise 1: Forward FLOPs

How many FLOPs does a 13B-parameter model need to process a 1000-token prompt (ignoring attention)?

Answer

2.6×10132.6 \times 10^{13}.

Solution

2⋅13×109⋅1000=2.6×10132 \cdot 13 \times 10^9 \cdot 1000 = 2.6 \times 10^{13} 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 1414 GB of weights: 3.35×1012/1.4×1010≈2393.35 \times 10^{12}/1.4 \times 10^{10} \approx 239. Real systems reach somewhat less.

Exercise 3: Compute budget (code)

How many tokens can a 1B-parameter model be trained on with 102110^{21} FLOPs?

Answer

About 1.7×10111.7 \times 10^{11} tokens.

Solution

D=C/(6N)D = C/(6N).

python · try it yourself
print(f"{1e21 / (6 * 1e9):.2e}")
1.67e+11

Test yourself

A forward pass of an NN-parameter transformer costs roughly how many FLOPs per token?

  1. NN
  2. 2N2N
  3. 6N6N
  4. N2N^2

Training on DD tokens costs about…

  1. 2ND2ND
  2. 6ND6ND
  3. ND2ND^2
  4. 12ND12ND

Why is single-request LLM generation usually memory-bandwidth bound?

  1. Because of the softmax
  2. Every weight must be read from memory for only about 2 FLOPs of work per token
  3. GPUs have little memory
  4. Attention is quadratic