Splitting a model across GPUs: memory, compute and network
- Work out the memory a model needs to serve and to train, and how many GPUs that takes
- Shard training state with ZeRO or FSDP, and say what each stage saves and what it costs on the network
- Split layers with tensor parallelism, and say why it stays inside one node
- Split a model into pipeline stages, and size the microbatches that keep the bubble small
- Build the sheet that compares sharding plans on memory, compute and network
A sharding sheet, the way the Anthropic session asks
A candidate reports an Anthropic session that estimated compute, memory and network costs for different ways to shard a model, in a tool like a spreadsheet (1 source). This lesson's practice problem builds that sheet as code, in three levels:
train_bytes_per_gpu(params, tp, pp, dp, zero)andserve_bytes_per_gpu(...): what each GPU holds to train and to serve.allreduce_seconds(size, n, bandwidth),dp_sync_seconds(...)andtp_seconds_per_layer(...): the time each split spends on the network.bubble(pp, microbatches)andplans(params, gpus, per_node, memory): a pipeline's idle time, and every split of a cluster that fits.
Call Returns train_bytes_per_gpu(70e9, 1, 1, 64, zero=3)17.5 GB allreduce_seconds(1e9, 8, 50e9)0.035 s bubble(16, 32)0.47 of the ideal time plans(70e9, 64, 8, 80e9)(8, 1, 8) first: tensor parallel 8, inside one node
The lesson follows the outdoor shop's move to a 70-billion-parameter model, to serve and to fine-tune: memory per parameter, ZeRO, tensor and pipeline parallelism, serving, and the sheet itself. Five steps end with a small task in the editor.
A big model doesn't fit on one GPU, to serve or to train
The outdoor shop's 7-billion-parameter model from the latency lesson fits on one GPU. Now the shop wants a 70-billion-parameter model, to serve and to fine-tune on its support transcripts. Take Llama 3 70B's shape: 80 layers, a model dimension of 8,192, 64 attention heads and 8 key-value heads.
Serving keeps the weights in bf16, 2 bytes each. Training with Adam in mixed precision needs 16 bytes a parameter, as the ZeRO paper counts it: 2 for the bf16 weights, 2 for their gradients, and 12 for fp32 copies of the parameters, the momentum and the variance.
# Llama 3 70B's size, rounded
params = 70e9
print(f"serving in bf16: {params * 2 / 1e9:6,.0f} GB of weights")
print(f"training with Adam: {params * 16 / 1e9:6,.0f} GB of weights, gradients and optimizer states")
print(f"one H100: {80:6,} GB")
How many 80 GB GPUs does the training state alone fill, before any activations?
Which of the three splits do you think needs the fastest links between GPUs?
Data parallelism copies the model, and ZeRO shards the copies
In plain data parallelism, every GPU holds the whole model, runs a different slice of the batch, and then all the GPUs average their gradients with an all-reduce. The ZeRO paper counts that as 2 times the parameter count moved per GPU each step.
Every data-parallel copy holds all 16 bytes a parameter. Which part is biggest?
On 64 GPUs, which ZeRO stage first fits the 70-billion-parameter model's training state into 80 GB?
The traffic can hide behind the compute
Training costs about 6 operations per parameter per token: the scaling book counts 2 in the forward pass and 4 in the backward pass. Compare that with the network for one step of 4 million tokens on 64 GPUs. Each 8-GPU node has 400 GB/s into the InfiniBand network, 50 GB/s for each GPU, and the dense bf16 peak from the latency lesson is 989 TFLOPS.
# 400 GB/s into InfiniBand per 8-GPU node: 50 GB/s a GPU
gpus, ib = 64, 50e9
# tokens in one training step
tokens = 4e6
# 6 operations per parameter per token, at 40% of the dense peak
compute = 6 * params * tokens / (gpus * 989e12 * 0.40)
for stage, factor in ((2, 1.0), (3, 1.5)):
# 2 values a parameter each step, 2 bytes each, 1.5 times at stage 3
moved = factor * 2 * params * 2
print(f"stage {stage}: {moved / 1e9:,.0f} GB a GPU, {moved / ib:.1f} s on the network, {compute:.0f} s of compute")
Stage 3 moves 420 GB a GPU each step, at 50 GB/s. Is the network the bottleneck?
Tensor parallelism splits every layer, and talks inside every layer
Megatron-LM splits each layer's matrices across GPUs, so each GPU computes part of every layer.
Each GPU holds a slice of a layer's weights and computes a partial result. What has to happen before the next layer starts?
Why does tensor parallelism stay inside one node?
Pipeline parallelism splits the layers, and pays with a bubble
A pipeline gives each group of GPUs a run of layers, a stage. Each stage sends its activations to the next one and its gradients back, point to point, which the Megatron team calls cheaper than tensor parallelism's all-reduces.
A step's batch goes through 4 stages as one piece. What is GPU 4 doing while GPU 1 runs the first layers?
16 stages and 32 microbatches. Roughly how big is the bubble?
Serving splits a model for memory, and tensor parallelism for speed
Serving has no gradients or optimizer states, only weights and the KV cache. The latency lesson showed that each decode step reads all the weights, so splitting them with tensor parallelism also cuts what each GPU reads per step.
# bf16
weights = 70e9 * 2
for tp in (2, 4, 8):
per_gpu = weights / tp
print(f"TP {tp}: {per_gpu / 1e9:4.1f} GB of weights a GPU, {80 - per_gpu / 1e9:4.1f} GB left, {per_gpu / 3.35e12 * 1000:4.1f} ms to read them")
TP 2 already fits the 140 GB of weights on two GPUs. Why serve on 4 or 8?
A worked example: the sharding sheet
The prompt, from a candidate's Anthropic session: work out what different ways of sharding a model cost in compute, memory and network (1 source). Build it one column at a time:
- Memory: what each GPU holds, and what fits.
- Compute: 6 operations per parameter per token, at a realistic share of the peak.
- Traffic: data-parallel traffic on InfiniBand, tensor-parallel traffic on NVLink.
- The bubble, if there's a pipeline.
Here's the sheet for the shop's fine-tune on 64 GPUs, two ways, and the same plan at Llama 3 405B's scale, with its 126 layers and model dimension of 16,384.
def plan(tp, pp, dp, zero, params, tokens, layers, hidden, seq=8_192):
"""One training step's memory, traffic and compute, with tensor parallel inside a node."""
shard = params / (tp * pp)
weights, grads, states = 2.0, 2.0, 12.0
if zero >= 1:
states /= dp
if zero >= 2:
grads /= dp
if zero >= 3:
weights /= dp
memory = shard * (weights + grads + states) / 1e9
infiniband = (1.5 if zero >= 3 else 1.0) * 2 * (dp - 1) / dp * 2 * shard / 50e9
# sequences each tensor-parallel group runs
sequences = tokens / seq / dp
nvlink = sequences * layers / pp * 4 * 2 * (tp - 1) / tp * seq * hidden * 2 / 450e9
compute = 6 * params * tokens / (tp * pp * dp * 989e12 * 0.40)
print(f"TP {tp} DP {dp:6,} ZeRO {zero}: {memory:5.1f} GB, {infiniband:4.1f} s InfiniBand, {nvlink:4.1f} s NVLink, {compute:4.1f} s compute")
# the shop's fine-tune, sharded data parallel only
plan(1, 1, 64, 3, 70e9, 4e6, 80, 8_192)
# the shop's fine-tune, 8-way tensor parallel
plan(8, 1, 8, 1, 70e9, 4e6, 80, 8_192)
# 405B on 16,384 GPUs, sharded data parallel only
plan(1, 1, 16_384, 3, 405e9, 16e6, 126, 16_384)
The same sharded data parallel plan works for the shop's 70B model on 64 GPUs. What happens to it for 405B on 16,384 GPUs?
Llama 3 used tensor parallel 8. Why exactly 8?
Two follow-ups
Two follow-ups come up after a sharding question: long sequences, and the score to quote.
Long sequences
Llama 3 adds a fourth split, context parallelism, which divides the input context into segments to relieve the memory bottleneck of very long sequences.
Which part of memory grows when the sequence length doubles?
The practice problem, start to finish
That's every piece the practice problem needs. It asks for the three levels from the start of the lesson, from a blank file, and the interviewer follows up after each one.
1.How many bytes a parameter does training with Adam in mixed precision need, before activations?
2.What does ZeRO stage 3 add over stage 2, and what does it cost?
3.Why does tensor parallelism stay inside one node?
4.A pipeline has 8 stages and 28 microbatches. What's the bubble as a share of the ideal time?
5.Sharded data parallelism alone trains a 70B model well on 64 GPUs. Why does it fail for a 405B model on 16,384 GPUs?
Practice
Sources (35)
- The Llama 3 Herd of Models · Llama Team, AI @ Meta, arXiv 2407.21783 · Jul 31, 2024
“Layers 32 80 126 Model Dimension 4,096 8,192 16,384”
- The Llama 3 Herd of Models · Llama Team, AI @ Meta, arXiv 2407.21783 · Jul 31, 2024
“Attention Heads 32 64 128 Key/Value Heads 8 8 8”
- ZeRO: Memory Optimizations Toward Training Trillion Parameter Models · Rajbhandari et al., arXiv 1910.02054 · Oct 4, 2019
“Mixed-precision Adam has K = 12. In total, this results in 2Ψ + 2Ψ + KΨ = 16Ψ bytes of memory requirement.”
- ZeRO: Memory Optimizations Toward Training Trillion Parameter Models · Rajbhandari et al., arXiv 1910.02054 · Oct 4, 2019
“an fp32 copy of the parameters, momentum and variance, with memory requirements of 4Ψ, 4Ψ, and 4Ψ bytes, respectively.”
- The Llama 3 Herd of Models · Llama Team, AI @ Meta, arXiv 2407.21783 · Jul 31, 2024
“Tensor parallelism splits individual weight tensors into multiple chunks on different devices. Pipeline parallelism partitions the model vertically into stages by layers”
- ZeRO: Memory Optimizations Toward Training Trillion Parameter Models · Rajbhandari et al., arXiv 1910.02054 · Oct 4, 2019
“Therefore, the standard DP incurs 2Ψ data movement during each training step.”
- ZeRO: Memory Optimizations Toward Training Trillion Parameter Models · Rajbhandari et al., arXiv 1910.02054 · Oct 4, 2019
“1) Optimizer State Partitioning (Pos): 4x memory reduction, same communication volume as DP; 2) Add Gradient Partitioning (Pos+g): 8x memory reduction, same communication volume as DP”
- ZeRO: Memory Optimizations Toward Training Trillion Parameter Models · Rajbhandari et al., arXiv 1910.02054 · Oct 4, 2019
“3) Add Parameter Partitioning (Pos+g+p): Memory reduction is linear with DP degree Nd. For example, splitting across 64 GPUs (Nd = 64) will yield a 64x memory reduction. There is a modest 50% increase in communication volume.”
- Getting Started with Fully Sharded Data Parallel (FSDP2) · PyTorch Tutorials · Sep 2, 2025
“Comparing with DDP, FSDP reduces GPU memory footprint by sharding model parameters, gradients, and optimizer states.”
- Getting Started with Fully Sharded Data Parallel (FSDP2) · PyTorch Tutorials · Sep 2, 2025
“fully_shard registers forward/backward hooks to all-gather parameters before computation, and reshards parameters after computation.”
- All the Transformer Math You Need to Know (How to Scale Your Model) · Austin et al., Google DeepMind · Feb 4, 2025
“during training, we have a total of 6NPM FLOPs, compared to 2NPM during inference: 2NPM in the forward pass, 4NPM in the backward pass.”
- How to Think About GPUs (How to Scale Your Model) · Austin et al., Google DeepMind · Aug 18, 2025
“H100s have about 450GB/s of egress bandwidth each in the NVLink domain, and each node has 400GB/s of egress bandwidth into the IB network.”
- All About Rooflines (How to Scale Your Model) · Austin et al., Google DeepMind · Feb 4, 2025
“This means we can lower-bound training and inference time by using the maximum of computation and communication time.”
- All About Rooflines (How to Scale Your Model) · Austin et al., Google DeepMind · Feb 4, 2025
“Typically (but not always), computation within a single chip can be overlapped with communication within a chip and between chips.”
- How to Parallelize a Transformer for Training (How to Scale Your Model) · Austin et al., Google DeepMind · Feb 4, 2025
“This is ultimately a consequence of the fact that the computation time scales with the per-device batch size, while the communication time is independent of this quantity (since we are transferring model weights).”
- Performance reported by NCCL tests · NVIDIA nccl-tests, GitHub · Nov 19, 2026
“Considering that each rank has a bandwidth to the outside world of B, the time to perform an allReduce operation of S elements is at best : t = (S*2*(n-1)) / (n*B)”
- Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism · Shoeybi et al., arXiv 1909.08053 · Sep 17, 2019
“This approach splits both GEMMs in the MLP block across GPUs and requires only a single all-reduce operation in the forward pass ... and a single all-reduce in the backward pass”
- Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism · Shoeybi et al., arXiv 1909.08053 · Sep 17, 2019
“for the self attention block we exploit inherent parallelism in the multihead attention operation ... such that the matrix multiply corresponding to each attention head is done locally on one GPU”
- Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism · Shoeybi et al., arXiv 1909.08053 · Sep 17, 2019
“This enables us to perform all GEMMs in a simple transformer layer using only two all-reduces in the forward path and two in the backward path”
- Efficient Large-Scale Language Model Training on GPU Clusters Using Megatron-LM · Narayanan et al., arXiv 2104.04473 · Apr 9, 2021
“With tensor model parallelism, tensors of total size bsh ... need to be all-reduced among t ... model replicas twice each in the forward and backward pass for each layer”
- NVIDIA H100 Tensor Core GPU · NVIDIA · Nov 19, 2026
“Interconnect NVIDIA NVLink: 900GB/s PCIe Gen5: 128GB/s”
- Introduction to the NVIDIA DGX H100/H200 System · NVIDIA DGX H100/H200 User Guide · Nov 19, 2026
“4 x OSFP ports for 8 x NVIDIA ConnectX-7 Single Port InfiniBand Cards Each card provides the following speeds: InfiniBand (default): Up to 400Gbps”
- Efficient Large-Scale Language Model Training on GPU Clusters Using Megatron-LM · Narayanan et al., arXiv 2104.04473 · Apr 9, 2021
“Takeaway #1: When considering different forms of model parallelism, tensor model parallelism should generally be used up to degree g ... and then pipeline model parallelism can be used to scale up to larger models across servers.”
- The Llama 3 Herd of Models · Llama Team, AI @ Meta, arXiv 2407.21783 · Jul 31, 2024
“The innermost parallelism requires the highest network bandwidth and lowest latency, and hence is usually constrained to within the same server.”
- Efficient Large-Scale Language Model Training on GPU Clusters Using Megatron-LM · Narayanan et al., arXiv 2104.04473 · Apr 9, 2021
“Pipeline model parallelism features cheaper point-to-point communication.”
- Efficient Large-Scale Language Model Training on GPU Clusters Using Megatron-LM · Narayanan et al., arXiv 2104.04473 · Apr 9, 2021
“the fraction of ideal computation time spent in the pipeline bubble is: Bubble time fraction (pipeline bubble size) = t_pb / t_id = (p - 1) / m”
- Efficient Large-Scale Language Model Training on GPU Clusters Using Megatron-LM · Narayanan et al., arXiv 2104.04473 · Apr 9, 2021
“This means that the new schedule reduces the bubble time by v ... this schedule requires extra communication”
- The Llama 3 Herd of Models · Llama Team, AI @ Meta, arXiv 2407.21783 · Jul 31, 2024
“When using a BF16 number representation for the model parameters, Llama 3 405B does not fit in the GPU memory of a single machine with 8 Nvidia H100 GPUs.”
- The Llama 3 Herd of Models · Llama Team, AI @ Meta, arXiv 2407.21783 · Jul 31, 2024
“Within each machine, the high NVLink bandwidth enables the use of tensor parallelism ... Across nodes, however, connectivity has lower bandwidth and higher latency, so we use pipeline parallelism”
- Parallelism and Scaling · vLLM docs · Nov 19, 2026
“The common practice is to set the tensor parallel size to the number of GPUs in each node, and the pipeline parallel size to the number of nodes.”
- The Llama 3 Herd of Models · Llama Team, AI @ Meta, arXiv 2407.21783 · Jul 31, 2024
“Llama 3 405B is trained on up to 16K H100 GPUs, each running at 700W TDP with 80GB HBM3”
- The Llama 3 Herd of Models · Llama Team, AI @ Meta, arXiv 2407.21783 · Jul 31, 2024
“GPUs TP CP PP DP Seq. Len. Batch size/DP Tokens/Batch TFLOPs/GPU BF16 MFU 8,192 8 1 16 64 8,192 32 16M 430 43% 16,384 8 1 16 128 8,192 16 16M 400 41%”
- The Llama 3 Herd of Models · Llama Team, AI @ Meta, arXiv 2407.21783 · Jul 31, 2024
“we achieve an overall BF16 Model FLOPs Utilization (MFU; Chowdhery et al. (2023)) of 38-43% for the configurations shown in Table 4”
- The Llama 3 Herd of Models · Llama Team, AI @ Meta, arXiv 2407.21783 · Jul 31, 2024
“Context parallelism divides the input context into segments, reducing memory bottleneck for very long sequence length inputs.”
- Efficient Large-Scale Language Model Training on GPU Clusters Using Megatron-LM · Narayanan et al., arXiv 2104.04473 · Apr 9, 2021
“Our approach allows us to perform training iterations on a model with 1 trillion parameters at 502 petaFLOP/s on 3072 GPUs (per-GPU throughput of 52% of theoretical peak).”