Appendix A Napkin Math for LLM Interviews
A.1 Parameter Counts and Memory Footprints
These formulas size the weight tensors and the memory budget before writing a single line of code. The parameter derivations and their implications for architecture decisions are developed in subsection 3.1.1; the precision trade-offs behind the training-memory figure are covered in subsection 9.3.1.
Formulas
Parameters per transformer layer. With hidden dimension , SwiGLU expansion , query heads, and key-value heads (MHA: ; GQA: ):
Total parameters (embedding table included):
Memory footprint:
The 16 B/param training figure: bf16 working copy (2 B) + fp32 master weights (4 B) + fp32 gradients (4 B) + AdamW first and second moments (4+4 B). Activation memory (batch sequence length dependent) is excluded.
Worked Example - 7B Model
Config: d = 4096, N = 32 heads, d_h = 128, L = 32 layers,
d_ff = 11008 = floor(8 * 4096 / 3), V = 32000, bf16
Attention per layer : 4 * 4096^2 = 67.1 M
MLP per layer : 3 * 4096 * 11008 = 135.3 M
Layer total : 202.4 M
Body (32 layers) : 32 * 202.4 M = 6.48 B
Embedding table : 32000 * 4096 = 0.13 B
Grand total : ~ 6.6 B (LLaMA-1 7B = 6.74 B)
Inference bf16 : 6.7e9 * 2 = 13.4 GB
Training AdamW : 6.7e9 * 16 = 107.2 GB
Largest model on 8 x H100 (no activations):
8 * 80 GB / 16 B/param = 40 B params (rough ceiling)
Rules of Thumb
-
2 attention per layer for SwiGLU models (135 M vs. 67 M at ); the MLP holds 67% of each layer’s parameters.
-
Inference (bf16): 2 GB per billion parameters. A 70 B model needs 140 GB.
-
Training (AdamW): 16 GB per billion parameters. A 7 B model needs 107 GB before activations.
-
Ceiling on H100s (80 GB each): billion parameters (ignoring activations).
-
GQA with KV heads: reduces KV cache and KV weight contribution by factor . Use (group size 8) as the production default for models 7 B.
Caveats
-
Bias terms are omitted; all modern frontier models drop them in attention and MLP.
-
For vanilla ReLU/GeLU MLPs use , not . Mixing the two conventions inflates the count by 50%.
-
Activation memory scales with batch size sequence length and can rival weight memory at large batch sizes; use gradient checkpointing to trade it back for compute.
-
Embedding weights are sometimes tied between input and output projections, halving the contribution.
A.2 FLOPs per Forward and Backward Pass
FLOPs are the currency of training cost estimates and the input to every napkin-math scheduling question. The arithmetic intensity framework that connects FLOPs to hardware utilization is in subsection 8.2.1; isoFLOP analysis and Chinchilla compute-optimal ratios are in chapter 14.
Formulas
Single matrix multiply :
All other operations (softmax, layer norm, element-wise activations) are linear in tensor size and negligible compared to the matrix multiplications for large .
Forward pass (attention + MLP; dominant terms for ):
where is the total number of tokens in the batch and is the parameter count. The attention quadratic adds FLOPs; this becomes comparable to the linear terms when (e.g., , ).
Backward pass (parameter gradients activation gradients):
Full training step:
Model FLOPs Utilization (MFU):
MFU peak FLOP/s (dense, no sparsity)
Worked Example - 70B Model, 15 T Tokens, 1024 H100s
Total training FLOPs = 6 * 70e9 * 15e12 = 6.3e24
H100 bf16 peak (dense, no sparsity) = 989 TFLOP/s
At MFU = 0.5 : 989e12 * 0.5 = 4.945e14 FLOP/s per GPU
1024 GPUs : 1024 * 4.945e14 = 5.06e17 FLOP/s total
Training time = 6.3e24 / 5.06e17 = 1.245e7 s ~ 144 days
Sanity check (8 H100s, 1 week, MFU = 0.5):
FLOPs = 8 * 4.945e14 * 7 * 86400 = 2.4e21
Chinchilla-optimal params (N_T = 20P): 2.4e21 / (6 * 20) ~ 20B params
Rules of Thumb
-
One training step: FLOPs.
-
H100 at MFU 0.5 (bf16 dense): useful FLOP/s per GPU.
-
H100-GPU-day: FLOPs.
-
Chinchilla-optimal: tokens. Modern runs use - tokens to amortize inference cost (see chapter 14).
-
MFU target: for well-tuned distributed runs; signals a system-level bottleneck (communication, data stall, or fragmentation).
-
Attention quadratic term: adds relative to the linear terms; double adds attention FLOPs but only MLP FLOPs.
Caveats
-
The approximation assumes MLP matrix multiplications dominate. Small or long can push attention FLOPs to the same order.
-
Gradient checkpointing re-runs a partial forward pass during the backward, multiplying by while saving activation memory.
-
MFU degrades rapidly under heavy communication (tensor parallelism all-reduces, pipeline bubbles); measure on the actual cluster before committing to a layout.
-
FP8 training theoretically doubles peak FLOP/s (H100: 1979 TFLOP/s dense); treat it as an advanced option requiring careful numerical validation.
A.3 KV-Cache Sizing and Latency Estimates
Generation is memory-bandwidth-limited: every optimization targets bytes transferred, not FLOPs. The arithmetic intensity argument is in subsection 8.2.1; the full latency-throughput model is in subsection 15.2.2; KV-reduction architectures (GQA, MLA, local attention) are in chapter 16.
Formulas
KV cache per sequence (bf16; key-value heads; head dimension ; context tokens):
(First factor of 2: K and V tensors; last factor of 2: bytes per bf16 element.)
Weight memory (bf16):
Decode step time at batch size (memory-bandwidth-limited):
Decode throughput (tokens per second):
KV-crossover batch size (where KV cache memory weight memory):
Throughput gains are superlinear in below and sublinear above it.
Maximum batch size (VRAM-limited):
GQA with heads (down from in MHA):
Worked Example - Llama-2 13B on a Single H100
Config: P = 13 B, L = 40, N = 40, N_kv = 40, H = 128
BW = 3.35 TB/s, VRAM = 80 GB, S = 1024, bf16
M_weights = 2 * 13e9 = 26 GB
C_kv = 2 * 40 * 40 * 128 * 1024 * 2 bytes = 838 MB ~ 0.84 GB/seq
B* = 26 / 0.84 ~ 31
B_max = (80 - 26) / 0.84 ~ 64 (barely fits)
B | Total mem (GB) | Step time (ms) | TPS (tok/s) | Fits?
1 | 26.8 | 8.0 | 125 | yes
16 | 39.4 | 11.8 | 1,356 | yes
32 | 52.9 | 15.8 | 2,025 | yes
64 | 79.8 | 23.8 | 2,689 | barely
128 | 133.5 | 39.8 | 3,215 | no (> 80 GB)
With GQA, N_kv = 8 (Llama-3 style; reduction factor = 40/8 = 5):
C_kv = 0.84 * (8/40) = 0.168 GB
B* = 26 / 0.168 ~ 155
B_max = (80 - 26) / 0.168 ~ 321
B = 128: total mem = 26 + 128 * 0.168 = 47.5 GB (fits)
step time = 47.5 / 3350 = 14.2 ms
TPS = 128 / 0.0142 ~ 9,000 tok/s (~3.3x vs. MHA at B=64)
Rules of Thumb
-
Single-sequence latency (): GBBW GB/s ms per token. Llama-2 13 B on H100: ms.
-
Crossover batch : where throughput gains shift from superlinear to sublinear. Compute it first; batching beyond yields diminishing returns.
-
GQA production default: (group size 8) shrinks by the reduction factor with accuracy loss at 7 B scale.
-
KV quantization (int8): halves , doubling at the same VRAM; use before adding hardware.
-
Doubling context : doubles , halves . At , for Llama-2 13 B drops from 31 to 8.
-
Replica parallelism: independent copies give throughput with zero extra latency and no interGPU communication; prefer it over larger batches.
Caveats
-
These estimates apply to the decode (generation) phase only. Prefill is compute-bound, not memory-bound; see subsection 15.1.1.
-
Attention during decode has arithmetic intensity independent of batch size, because each sequence owns its own KV cache. See subsection 15.1.2.
-
Inter-GPU communication (tensor/pipeline parallelism) is excluded from the step-time formula; add allreduce latency when sharding the model across multiple devices.
-
Flash Attention reduces HBM traffic during prefill but does not alter the decode formula above-the KV cache must still be streamed fully from HBM at each generation step.