All courses

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

▶ Watch this lesson Start the whole course

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.

From an idea to a GPU plan

Suppose someone hands you an architecture, a token budget and a GPU cluster. You want to know: - How many parameters does it have? - How many FLOPs and days will training take? - Will it fit in GPU memory? - How should you split the work across GPUs? - Should you make the model bigger, or train it on more data? Each piece you have learned answers one link of this chain. This thread joins them into one calculation you can do on a napkin.

The chain, and the layout rule

**Compute:** architecture → $N$ → choose $D$ → $C$ → GPU-days. **Memory:** total parameters → bytes → how to split. **Layout:** - Use sharded DP (FSDP) by default. Its per-layer all-gathers and reduce-scatters overlap with compute, so it scales well even across nodes. - Add TP (≤8 GPUs, inside a node) in either of two cases. One: even with sharding, a GPU cannot hold one layer's gathered weights plus its activations. Two: DP would exceed the number of sequences in the global batch. - Add PP across nodes only if still needed, with many microbatches. GPUs = TP × PP × DP.

The sizing formulas

- Parameters: $N\approx 12Ld^2$ per model, from $4d^2$ attention + $8d^2$ FFN per block. This assumes $h_{kv}=h$ and $d_{ff}=4d$; SwiGLU with $8d/3$ also gives $8d^2$. The embedding matrices ($Vd$ each for $W_E$ and $W_U$, or one $Vd$ if tied) add memory but are not counted in $N$. - Compute: $C\approx 6ND$. - Choosing $N$ for a target ratio $\rho=D/N$ (tokens per parameter): $N=\sqrt{C/(6\rho)}$. Compute-optimal is $\rho\approx20$, giving $N=\sqrt{C/120}$. - Time: $C/(G\cdot F\cdot u)$ seconds, where $G$ = number of GPUs, $F$ = peak FLOP/s per GPU, $u$ = utilization. - Memory: about $16$ bytes per parameter ÷ shards, plus activations. - MoE: total parameters set memory; active parameters set $C$.

Sizing a 32-layer, d=4096 model

- $N=12\cdot32\cdot4096^2\approx6.4$B. - Train on $D=2$T tokens. That is about 310 tokens per parameter, so it is overtrained; compute-optimal would be about 129B tokens. - $C=6\cdot6.4\text{e}9\cdot2\text{e}12\approx7.7\text{e}22$. - 512 GPUs at $10^{15}$ FLOP/s with 40% utilization: $7.7\text{e}22/(512\cdot4\text{e}14)\approx3.8\text{e}5$ s ≈ 4.4 days. - States: $16\cdot6.4\text{B}\approx103$ GB, which is more than one 80 GB GPU. FSDP over 512 GPUs leaves 0.2 GB each. Assume the global batch has at least 512 sequences and the activations fit. Then pure sharded DP suffices; no TP or PP needed.

A napkin calculator (dense or MoE)

Set `E` = number of experts and `k` = experts used per token. Here `k` is the MoE top-k, not the sampling cutoff. `E=k=1` gives a dense model. Router and embedding parameters are ignored. Memory covers weights, gradients and Adam states only. Activations come on top.
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

- $N\approx12Ld^2$, $C\approx6ND$, and $N=\sqrt{C/(6\rho)}$ for $\rho$ tokens per parameter ($\rho\approx20$ is compute-optimal). - Training state is about 16 bytes per parameter, sharded across GPUs. - Split work with sharded DP (FSDP) by default, TP inside a node, and PP across nodes only if needed. - MoE: memory follows total parameters, FLOPs follow active parameters.

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.