Phase 05/Splitting a model across GPUs/Multi-GPU inference and parallelism strategies
PHASE 05 · Splitting a model across GPUs

Multi-GPU inference and parallelism strategies

0%
6 h5 subtopics2 lessons · 1 required
In plain English

A 70-billion-parameter model in 16-bit numbers needs about 140 GB just to store its weights, and the largest common GPU holds 80 GB. So the model must be cut into pieces that live on different GPUs. There are three ways to cut. You can give each GPU a complete copy and split the users between them (data parallelism), you can slice every layer so that each GPU holds a strip of every weight matrix (tensor parallelism), or you can give each GPU a block of whole layers and pass activations along like a bucket brigade (pipeline parallelism). Think of copying a long book for many readers. Data parallelism prints more copies; tensor parallelism has several scribes write different columns of each page at once; pipeline parallelism has each scribe write a different chapter and hand the manuscript on. Each choice trades memory, speed, and how much the GPUs must talk to each other.

Why it matters

Every production serving flag such as tensor_parallel_size or pipeline_parallel_size in vLLM, SGLang, or TensorRT-LLM is an answer to the question this topic asks. Picking wrongly can double latency or waste half the GPUs you pay for.

Hiring signal

Skills this topic builds, with the number of the 46 analysed job postings that ask for each. Companies asking: Anthropic, Anyscale, Baseten, Cerebras, Cohere, CoreWeave, Crusoe, Cursor, Databricks, ElevenLabs, Fireworks, Liquid AI, Modal, nvidia, OpenAI, Perplexity, Thinking Machines, Together AI, xAI.See all skills →

Learning objectives

  • Compute the memory footprint of weights plus KV cache for a given model, batch, and context length.
  • Compare data, tensor, and pipeline parallelism on memory, latency, throughput, and communication volume.
  • Choose a parallelism layout for a given model size and GPU topology and justify it in writing.
  • Explain why inference favours tensor parallelism inside a node and pipeline parallelism across nodes.

Key concepts

Data parallelism (DP)
Each replica holds the full model; requests are spread across replicas. Adds throughput, not capacity.
Tensor parallelism (TP)
Each weight matrix is split across GPUs; every layer needs a collective to combine partial results.
Pipeline parallelism (PP)
Consecutive layers are assigned to different GPUs; activations flow from stage to stage.
Expert parallelism (EP)
For mixture-of-experts models, different experts live on different GPUs and tokens are routed to them.
Memory wall
The point where weights plus KV cache exceed a single GPU; the primary reason to go multi-GPU for inference.
Communication-to-compute ratio
Bytes moved between GPUs per FLOP computed; the quantity that decides whether a split is worth it.
NVLink versus PCIe versus Ethernet/InfiniBand
The three bandwidth tiers (hundreds of GB/s, tens of GB/s, a few to tens of GB/s) that dictate which parallelism goes where.

Subtopics

  1. 1. Why one GPU is not enough

    Weight memory, KV cache memory, and bandwidth limits; computing when a model stops fitting.

  2. 2. The three classical parallelisms

    DP, TP, PP compared on memory per GPU, per-token latency, and bytes communicated per layer.

  3. 3. Hardware topology

    NVLink/NVSwitch inside a node, PCIe, and InfiniBand or Ethernet between nodes; reading nvidia-smi topo -m.

  4. 4. Inference versus training parallelism

    No gradients and no optimizer state, small batches, latency sensitivity; why inference layouts differ from training ones.

  5. 5. Layout decision rules

    TP within a node up to 8, PP across nodes, DP replicas for throughput; where MoE expert parallelism fits.

Resources

documentation
paper
article

Hands-on lessons

L1

Build a memory and layout calculator

CPU · your Macscript120 minrequired

Goal · A Python script that, given a model config and GPU type, prints the weight and KV cache memory and recommends TP/PP/DP values.

  1. 1Write a function that reads a Hugging Face config.json and returns parameter count (check against the reported count for Llama-3-8B and 70B).
  2. 2Add KV cache bytes per token = 2 * n_layers * n_kv_heads * head_dim * bytes_per_elem; multiply by batch * context.
  3. 3Model GPUs as (memory_GB, intra_node_bw_GBps, inter_node_bw_GBps) for A10G 24 GB, A100 80 GB, H100 80 GB.
  4. 4Implement the rule search over TP in {1,2,4,8} and PP in {1,2,4} that keeps weights plus KV cache under 90 percent of memory, preferring the smallest TP, then the smallest PP.
  5. 5Estimate per-layer TP communication bytes as 2 * batch * d_model * bytes_per_elem per all-reduce and print total bytes per decode step.
  6. 6Run for Llama-3-8B at 8k context and Llama-3-70B at 32k context on each GPU type and tabulate the results.
Deliverable
layout_calc.py plus a markdown table of recommendations for six (model, GPU) pairs.
Check
Llama-3-70B bf16 reports about 140 GB of weights and is placed on TP=4 for 80 GB GPUs at 32k context (TP=2 leaves only ~4 GB under the 90 percent cap, less than one 32k sequence of KV at ~10.7 GB); Llama-3-8B fits on a single 24 GB GPU at short context but not at 8k with batch 32.
L2

Data parallel versus tensor parallel on gloo

CPU · your Macscript90 minoptional

Goal · Run the same tiny MLP under DP (two independent copies) and TP (sharded) with torch.distributed on two CPU processes and compare per-rank memory and bytes communicated.

  1. 1Write dp_vs_tp.py that initialises torch.distributed with backend gloo and reads rank and world_size from the environment.
  2. 2In DP mode each rank loads the full MLP and processes half the batch; in TP mode each rank holds half the hidden dimension and all-reduces the output.
  3. 3Count parameters per rank in each mode and the bytes passed to dist.all_reduce.
  4. 4Launch with torchrun --nproc_per_node=2 dp_vs_tp.py --mode dp and --mode tp.
  5. 5Assert the TP output equals the single-process output within 1e-5.
Deliverable
dp_vs_tp.py and a short table of params per rank and bytes communicated per forward pass for each mode.
Check
TP shows half the parameters per rank and nonzero communication; DP shows full parameters and zero communication; outputs match the reference.

Build your own

Build your own parallelism planner

PythonCPU · your Mac~5 h

A CLI that takes a Hugging Face model config and a cluster description and emits the TP/PP/DP layouts that fit, with per-GPU memory, bytes moved per decode step and an estimated step time for each.

The real thing: huggingface.co/spaces/nanotron/ultrascale-playbook ↗

How you know a stage passes

Write tests/test_planner.py first. Every stage adds pure-function tests with hand-computed numbers (GPT-2 small, Llama-3-8B and Llama-3-70B configs are checked into tests/fixtures as config.json files) plus one CLI test that runs `plan` through subprocess and checks the exit code and the JSON it writes. Run `pytest -q`; a stage is done when its tests pass and all earlier tests still pass.

Stages, in order. Each one leaves you with something that runs.0/6
  1. 01memory from a config file
    Given a config.json and a dtype, the planner returns parameter count, weight bytes and KV cache bytes per token (GQA aware).
    test · Parameter counts for gpt2, Llama-3-8B and Llama-3-70B are within 1 percent of the Hugging Face numbers; Llama-3-70B bf16 KV per token equals 2 * 80 * 8 * 128 * 2 = 327,680 bytes exactly.
  2. 02cluster description and feasibility filter
    A cluster YAML lists GPU type, count per node, node count, intra-node and inter-node link bandwidth; the planner enumerates every (TP, PP, DP) with TP*PP*DP equal to the GPU count and rejects layouts whose per-GPU weights plus a minimum KV budget exceed memory.
    test · For Llama-3-70B bf16 on 4 x 80 GB, TP=1 is rejected and TP=2 and TP=4 are accepted; for Llama-3-8B bf16 on one 16 GB T4 nothing fits and the CLI exits 1 with a message naming the shortfall in GB.
  3. 03communication bytes per decode step
    For each layout the planner reports TP all-reduce bytes per step (two all-reduces per layer, batch * hidden * dtype bytes each) and PP activation bytes per micro-batch, then converts them to time with an alpha-beta link model.
    test · Llama-3-70B at TP=8 and batch 1 reports 2 * 80 * 8192 * 2 = 2,621,440 bytes per step of all-reduce payload; the NVLink time estimate is at least 5x lower than the PCIe estimate for the same layout.
  4. 04step time and ranking
    Decode step time is estimated as weight-read time (per-GPU weight bytes over HBM bandwidth) plus communication time plus a fixed per-collective latency, and layouts are ranked by tokens per second per dollar from an hourly price table.
    test · On a single NVLink node the top-ranked layout for 70B is TP=4 or TP=8, never PP>1; across two nodes joined by 100 Gbps Ethernet the planner ranks PP=2 across nodes above TP=16 across nodes; a snapshot test pins the ranking for three fixed scenarios.
  5. 05KV budget and maximum concurrency
    After weights and a configurable activation reserve, the remaining memory becomes a KV budget converted into maximum concurrent tokens and maximum sequences at a given context length.
    test · Llama-3-8B bf16 on one 80 GB GPU with a 90 percent utilisation cap yields a maximum-concurrency number for 8,192-token requests within 15 percent of the figure vLLM printed in your Phase 3 logs; the unit test asserts the formula with a hand-computed value.
  6. 06the plan command and case studies
    `plan --model <config> --cluster <yaml> --dtype bf16 --context 8192` prints a ranked table and writes plan.json; the README walks through three case studies with the numbers.
    test · The CLI exits 0 and plan.json validates against the schema in tests; the recommended layout for 70B bf16 on 4 x 80 GB is TP=4, and for 8B on 8 x A10G it is DP=8 with TP=1, matching the layout rules you derived in the lessons.

Knowledge check

QYou have a 70B bf16 model and four 80 GB GPUs in one node. Which parallelism do you pick first and why?

Tensor parallelism of 4 over NVLink. Weights need about 140 GB so one GPU cannot hold them, and TP keeps per-token latency low because all GPUs work on every layer at once.

QWhy does data parallelism not help a model that does not fit on one GPU?

Each DP replica needs the complete model in memory; DP only adds throughput by serving more requests in parallel.

QWhy is inference parallelism planned differently from training parallelism?

Inference has no gradients or optimizer state, so memory is weights plus KV cache, batches are small, and per-token latency matters, so layouts favour TP over large-batch techniques like ZeRO.

QWhat does the communication-to-compute ratio tell you?

Whether the time spent moving partial results between GPUs will be hidden by, or dominate, the time spent on matrix multiplies.

Completion

  • All 5 subtopics marked done
  • All 1 required lessons done
  • All subtopics marked completed.
  • Lesson l1 calculator produces correct memory numbers for Llama-3-8B and 70B.
  • Can state the TP-in-node, PP-across-nodes rule and explain the bandwidth reason behind it.
Topic progress is derived; it reaches 100% when both checks above pass.Next recommended topic →