Summary
Transformer accounting converts model dimensions into estimates for parameters, FLOPs, and memory. These estimates show whether a training or serving plan can fit and how long it may take.
Why AI labs care
Large-model plans should start with arithmetic. A candidate should be able to estimate:
- model size;
- training compute;
- memory per device;
- KV-cache size;
- the effect of sequence length;
- the active compute of a mixture-of-experts model.
Exact framework code comes later.
Symbols
Use these symbols for a decoder-only transformer:
| Symbol | Meaning |
|---|---|
| number of sequences in a batch | |
| tokens per sequence | |
| model width | |
| feed-forward width | |
| number of layers | |
| number of query heads | |
| number of key/value heads | |
| size of each head | |
| vocabulary size |
Usually, . Grouped-query attention uses .
The number of tokens in one batch is .
Parameter count
Gated feed-forward block
Many current models use three matrices in each feed-forward block:
- two projections from to ;
- one projection from back to .
The count per layer is:
A non-gated block has two large matrices and uses about parameters.
Attention projections
Query and output projections each use about parameters. Key and value projections each use about .
The count per layer is:
When , this becomes:
Reducing saves key/value parameters and KV-cache memory.
Embeddings and norms
A token embedding has parameters. The output layer may share that matrix or add another parameters. Normalization parameters are small compared with the large matrices.
A useful total is:
For many dense models, the feed-forward blocks contain most parameters.
FLOPs for training
A matrix multiply uses about two FLOPs per multiply-add. Its backward pass computes gradients for the input and the weight. The forward and backward passes together cost about three times the forward pass.
For the large parameter matrices, a common training estimate is:
This estimate omits some attention work, normalization, routing, and other small operations. It is useful for a first estimate.
For a mixture-of-experts model, use active parameters per token for the compute estimate. Use total parameters for weight memory.
Attention FLOPs
Attention scores and the weighted value sum add work that grows with .
For standard self-attention, the training cost of these two matrix operations is roughly:
Under common model ratios, attention-score FLOPs become comparable to the other large matrix operations when the sequence length reaches several times the model width. The exact point depends on architecture, masking, and the attention kernel.
Long context can become expensive before this FLOP crossover because activation and KV memory also grow with sequence length.
Training memory
Count each component separately:
| Component | Common storage |
|---|---|
| BF16 parameters | 2 bytes per parameter |
| BF16 gradients | 2 bytes per parameter |
| Adam first moment | 4 bytes per parameter |
| Adam second moment | 4 bytes per parameter |
| Optional FP32 master weights | 4 bytes per parameter |
This gives about 12 bytes per parameter without FP32 master weights and about 16 with them.
Then add:
- saved activations;
- temporary kernel buffers;
- communication buffers;
- allocator overhead.
Activation memory depends on batch tokens, width, layers, and which intermediate values are saved. It can exceed model-state memory at long context. Activation checkpointing reduces saved values and repeats part of the forward work during backpropagation.
Inference memory
Inference has no gradients or optimizer state. It stores weights, temporary activations, and a KV cache for each active sequence.
KV-cache bytes per sequence are approximately:
where is bytes per stored value. The factor 2 stores both keys and values.
For a batch of active sequences, multiply by the number of sequences. Paged allocation reduces unused reserved space. It does not change the bytes needed for tokens that are present.
Small example
A one-billion-parameter model in BF16 needs about 2 GB for weights.
During training with BF16 gradients and FP32 Adam moments, model state needs about 12 GB before activations and temporary buffers. If FP32 master weights are kept, the estimate becomes about 16 GB.
During inference, the same model may fit easily while long KV caches limit the number of active requests.
In an interview
Use this order:
- Write the model dimensions.
- Estimate feed-forward, attention, and vocabulary parameters.
- Use FLOPs per training token as a first estimate.
- Add attention-score FLOPs for long context.
- Separate model state from activations and temporary buffers.
- Compute KV bytes per token and per request.
- State which assumptions may change the result.
- Compare the estimate with measured utilization before making a cost claim.
Common mistakes
- Using total MoE parameters to estimate per-token compute.
- Forgetting the backward pass.
- Counting weights while omitting gradients and optimizer state.
- Assuming every training stack keeps FP32 master weights.
- Ignoring activation memory.
- Using maximum context without multiplying KV memory by active requests.
- Treating the rule as exact at long context.
Related: train a 100B parameter model, KV cache, and activation checkpointing. Further practice: Transformer math in the JAX Scaling Book.