Splitting a model across GPUs: memory, compute and network

55 min7 partsAdvancedFast-moving topicChecked Dec 2026
Start the lessonShort steps, each with something to do
What you'll learn
  • 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:

  1. train_bytes_per_gpu(params, tp, pp, dp, zero) and serve_bytes_per_gpu(...): what each GPU holds to train and to serve.
  2. allreduce_seconds(size, n, bandwidth), dp_sync_seconds(...) and tp_seconds_per_layer(...): the time each split spends on the network.
  3. bubble(pp, microbatches) and plans(params, gpus, per_node, memory): a pipeline's idle time, and every split of a cluster that fits.
CallReturns
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.

Part 1 of 7about 3 min
Why split a model

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")
Predict

How many 80 GB GPUs does the training state alone fill, before any activations?

Pick one to see what it prints.
Pick an answer above to see the rest
Predict

Which of the three splits do you think needs the fastest links between GPUs?

Pick an answer above to see the rest
Part 1 done
Part 2 of 7about 11 min
Data parallel and sharded states

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.

Predict

Every data-parallel copy holds all 16 bytes a parameter. Which part is biggest?

Pick an answer above to see the rest
Predict

On 64 GPUs, which ZeRO stage first fits the 70-billion-parameter model's training state into 80 GB?

Pick one to see what it prints.
Pick an answer above to see the rest

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")
Predict

Stage 3 moves 420 GB a GPU each step, at 50 GB/s. Is the network the bottleneck?

Pick one to see what it prints.
Pick an answer above to see the rest
Part 2 done
Part 3 of 7about 9 min
Tensor parallel

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.

Predict

Each GPU holds a slice of a layer's weights and computes a partial result. What has to happen before the next layer starts?

Pick an answer above to see the rest
Predict

Why does tensor parallelism stay inside one node?

Pick one to see what it prints.
Pick an answer above to see the rest
Part 3 done
Part 4 of 7about 10 min
Pipeline parallel

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.

Predict

A step's batch goes through 4 stages as one piece. What is GPU 4 doing while GPU 1 runs the first layers?

Pick an answer above to see the rest
Predict

16 stages and 32 microbatches. Roughly how big is the bubble?

Pick one to see what it prints.
Pick an answer above to see the rest
Part 4 done
Part 5 of 7about 8 min
Serving

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")
Predict

TP 2 already fits the 140 GB of weights on two GPUs. Why serve on 4 or 8?

Pick one to see what it prints.
Pick an answer above to see the rest
Part 5 done
Part 6 of 7about 11 min
The worked example

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:

  1. Memory: what each GPU holds, and what fits.
  2. Compute: 6 operations per parameter per token, at a realistic share of the peak.
  3. Traffic: data-parallel traffic on InfiniBand, tensor-parallel traffic on NVLink.
  4. 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)
Predict

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?

Pick one to see what it prints.
Pick an answer above to see the rest
Predict

Llama 3 used tensor parallel 8. Why exactly 8?

Pick an answer above to see the rest
Part 6 done
Part 7 of 7about 3 min
Follow-ups

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.

Predict

Which part of memory grows when the sequence length doubles?

Pick an answer above to see the rest

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.

Check yourself
5 questions
  1. 1.How many bytes a parameter does training with Adam in mixed precision need, before activations?

  2. 2.What does ZeRO stage 3 add over stage 2, and what does it cost?

  3. 3.Why does tensor parallelism stay inside one node?

  4. 4.A pipeline has 8 stages and 28 microbatches. What's the bubble as a share of the ideal time?

  5. 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)
  1. 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”
  2. 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”
  3. 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.”
  4. 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.”
  5. 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”
  6. 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.”
  7. 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”
  8. 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.”
  9. 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.”
  10. 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.”
  11. 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.”
  12. 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.”
  13. 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.”
  14. 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.”
  15. 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).”
  16. 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)”
  17. 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”
  18. 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”
  19. 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”
  20. 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”
  21. NVIDIA H100 Tensor Core GPU · NVIDIA · Nov 19, 2026
    “Interconnect NVIDIA NVLink: 900GB/s PCIe Gen5: 128GB/s”
  22. 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”
  23. 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.”
  24. 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.”
  25. 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.”
  26. 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”
  27. 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”
  28. 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.”
  29. 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”
  30. 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.”
  31. 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”
  32. 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%”
  33. 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”
  34. 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.”
  35. 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).”