Size a model: params, FLOPs, memory, GPUs
For a given architecture and budget, compute N, training FLOPs (6ND), training memory per GPU, and a compute-optimal or deliberately overtrained N and D. Choose a sensible combination of data, tensor and pipeline parallelism, and handle MoE's active vs total parameters.
Builds on: Overtraining small models for cheap inference, Sharded data parallelism (ZeRO / FSDP), Tensor parallelism, Pipeline parallelism, Mixture-of-experts layers
What the animation says
The napkin chain
Someone hands you an architecture, a token budget, and a GPU cluster. Before anything runs, you want five answers: parameters, compute, days, memory, and how to split the work. They link into one chain. The architecture gives the parameter count. Pick the tokens, and you get compute. Divide compute by what the cluster delivers, and you get days. A second branch: parameters become bytes, and bytes decide how to split across GPUs.
Counting parameters
Our example: thirty-two layers, and a model width of four thousand ninety-six. Each block's attention has four d-squared weights: query, key, value, and output. The feed-forward layer widens to four d and back down. That's eight d squared, twice the attention. Twelve d squared per block, times thirty-two layers: about six point four billion parameters. Embeddings add memory, but we leave them out of this count. Now train it. For each parameter and each token, how many floating-point operations?
Compute and days
Six. Two for the forward pass, four for the backward pass. So compute is six, times parameters, times tokens. With two trillion tokens, that's about seven point seven times ten to the twenty-two. That's over three hundred tokens per parameter. Compute-optimal is about twenty, so this model is deliberately overtrained. Now divide by the cluster: five hundred twelve GPUs, a petaflop each, at forty percent utilization. About four point four days.
Memory and layout
Training state costs about sixteen bytes per parameter: weights, gradients, and Adam's optimizer states. Six point four billion parameters is about a hundred and three gigabytes. One eighty-gigabyte GPU can't hold it. Sharded data parallel, called FSDP, splits it across all five hundred twelve GPUs: point two gigabytes each. That's the default. Add tensor parallel only if one layer plus its activations won't fit, or data parallel outgrows the batch. Pipeline parallel comes last, across nodes, with many microbatches. Your turn: drag tensor parallel to where it belongs.
Mixture of experts
Same model, but now each block has sixteen expert feed-forward layers, and each token uses two. Memory follows total parameters: about seventy-one billion, so two point two gigabytes per GPU. Compute follows active parameters: about ten point seven billion, so about seven point three days. Same napkin, two counts. Total parameters set memory. Active parameters set FLOPs.
The chain, and the layout rule
The sizing formulas
Sizing a 32-layer, d=4096 model
A napkin calculator (dense or MoE)
def size(L, d, D, gpus, F=1e15, u=0.4, E=1, k=1):
attn, ffn = 4*d*d, 8*d*d
N_tot = L*(attn + E*ffn) # sets memory
N_act = L*(attn + k*ffn) # sets FLOPs
C = 6*N_act*D
days = C/(gpus*F*u)/86400
gb = 16*N_tot/gpus/1e9 # FSDP over all GPUs
return N_tot/1e9, N_act/1e9, C, round(days, 1), round(gb, 2)
print(size(32, 4096, 2e12, 512)) # dense
print(size(32, 4096, 2e12, 512, E=16, k=2)) # MoE
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses