Phase 05/Splitting a model across GPUs/Pipeline parallelism
PHASE 05 · Splitting a model across GPUs

Pipeline parallelism

0%
7 h5 subtopics2 lessons · 2 required
In plain English

Pipeline parallelism gives each GPU a block of consecutive layers. A request enters GPU 1, gets processed by its layers, and the result is handed to GPU 2, and so on down the line. It is a factory assembly line. The problem with an assembly line is that when the first car enters, only the first station is busy and the others wait, and the same happens in reverse at the end. These idle gaps are called bubbles. The fix is to keep many cars on the line at once, split into micro-batches, so every station nearly always has work. Pipeline parallelism needs very little talking between GPUs, just one hand-off per stage, which is why it is the method of choice when GPUs sit in different machines joined by an ordinary network.

Why it matters

PP is how models that exceed a single node (405B and larger) are served, and it is the parallelism whose efficiency depends most on the scheduler. Bubble analysis is also the mental model behind disaggregated prefill/decode designs.

Hiring signal

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

Learning objectives

  • Implement a pipeline with point-to-point sends and receives between stages using torch.distributed.
  • Compute pipeline bubble fraction as a function of stages and micro-batches and verify it with a simulator.
  • Compare GPipe-style and 1F1B-style schedules and explain why inference only needs the forward half.
  • Describe how continuous batching interacts with pipeline stages in a serving engine.

Key concepts

Stage
A contiguous group of layers assigned to one device.
Micro-batch
A slice of the batch that moves through the pipeline independently so stages can overlap work.
Pipeline bubble
Idle time at pipeline start and end; fraction roughly (p-1)/(m+p-1) for p stages and m micro-batches.
Point-to-point communication
send/recv between adjacent ranks, as opposed to collectives; the only communication PP needs.
Stage balancing
Assigning layers so each stage takes equal time; the embedding and LM head make the first and last stages heavier.
Virtual pipeline stages (interleaving)
Giving each device several non-contiguous layer chunks to shrink bubbles at the cost of more communication.

Subtopics

  1. 1. Stage assignment and activation hand-off

    Splitting layers across devices; only the hidden-state tensor of shape (batch, seq, d_model) crosses a stage boundary.

  2. 2. Bubbles and micro-batching

    Derive (p-1)/(m+p-1) idle fraction; why decode with batch 1 is the worst case for PP.

  3. 3. Schedules

    GPipe, 1F1B, interleaved 1F1B; inference uses forward-only schedules and can keep several requests in flight per stage.

  4. 4. PP inside serving engines

    How vLLM runs pipeline_parallel_size with asynchronous stage execution and multiple in-flight micro-batches of a continuous batch.

  5. 5. Combining TP and PP

    2D layouts such as TP=8 inside each node and PP across nodes; computing total GPUs and per-GPU memory.

Resources

Hands-on lessons

L1

Pipeline bubble simulator

CPU · your Macscript90 minrequired

Goal · A pure-Python discrete-event simulator that schedules micro-batches across p stages and reports utilisation for GPipe and 1F1B forward-only schedules.

  1. 1Model each stage as a worker with a fixed per-micro-batch time t and an optional transfer time between stages.
  2. 2Implement a GPipe-style forward schedule and compute total time, per-stage busy time, and bubble fraction.
  3. 3Sweep p in {2, 4, 8} and m in {1, 2, 4, 8, 16, 32}; compare measured bubble fraction with (p-1)/(m+p-1).
  4. 4Add an unbalanced case where stage 0 is 1.5x slower and show how the pipeline throughput drops to the slowest stage.
  5. 5Render an ASCII or matplotlib Gantt chart of stage activity for p=4, m=4.
Deliverable
pp_sim.py, a CSV of (p, m, measured_bubble, formula_bubble), and one Gantt chart image.
Check
Measured bubble fraction equals the formula to within 1 percent for all balanced cases; with m=1 and p=4 utilisation is 25 percent.
L2

Two-stage pipeline with send/recv on gloo

CPU · your Macscript120 minrequired

Goal · Run GPT-2 small split into two stages on two CPU processes using dist.send/dist.recv and reproduce the single-process logits.

  1. 1Load gpt2 from transformers; rank 0 keeps embeddings plus layers 0-5, rank 1 keeps layers 6-11, final norm, and the LM head.
  2. 2Implement a forward where rank 0 runs its layers and dist.send the hidden states; rank 1 dist.recv, runs its layers, and prints logits.
  3. 3Split a batch of 8 prompts into 4 micro-batches and overlap by issuing isend before computing the next micro-batch.
  4. 4Compare rank 1 logits with a single-process reference using torch.allclose(atol=1e-4).
  5. 5Time the run with m=1 and m=4 micro-batches; relate the difference to your l1 simulator.
  6. 6Launch with torchrun --nproc_per_node=2 pp_gpt2.py.
Deliverable
pp_gpt2.py and a note with the timings for m=1 versus m=4 and the bytes sent per micro-batch (batch * seq * 768 * 4).
Check
Logits match the reference; the bytes sent per micro-batch match the formula exactly; m=4 is faster than m=1 in wall-clock time.

Build your own

Build your own pipeline scheduler

PythonCPU · your Mac~7 h

A discrete-event simulator for GPipe and 1F1B micro-batch schedules with a validator and bubble accounting, then the same schedule driving GPT-2 across gloo ranks with send/recv.

The real thing: github.com/NVIDIA/Megatron-LM/blob/main/megatron/core/pipeline_parallel/schedules.py ↗

How you know a stage passes

Write tests/test_schedule.py first. The simulator is pure Python with deterministic stage times, so tests check timelines exactly, a validator asserts that no stage runs two micro-batches at once and that every forward waits for the previous stage, and bubble fractions are compared with the closed form (p - 1) / (m + p - 1). The last stage launches `torchrun --nproc_per_node=N pp_gpt2.py` via subprocess and compares saved logits with a single-process run. Run `pytest -q`.

Stages, in order. Each one leaves you with something that runs.0/6
  1. 01the event model
    Stages have a per-micro-batch forward time; a schedule is a list of (stage, micro-batch, start, end) events; the validator and a utilisation function exist.
    test · With p=4 equal stages and a single micro-batch the makespan is 4t and utilisation is exactly 25 percent; the validator rejects a hand-written timeline with an overlapping event.
  2. 02GPipe forward-only
    All m micro-batches flow through the pipeline in order; the simulator reports makespan, per-stage idle time and bubble fraction.
    test · Bubble fraction equals (p - 1) / (m + p - 1) to 1e-9 for p in 2, 4, 8 and m from 1 to 16; the validator passes every generated timeline.
  3. 031F1B with backward passes
    Forward and backward micro-batches interleave so each stage alternates one forward and one backward in steady state, bounding in-flight activations.
    test · 1F1B has the same bubble fraction as GPipe for equal stages, but peak in-flight activations per stage is at most p instead of m (asserted for p=4, m=16).
  4. 04uneven stages and layer partitioning
    Given per-layer costs (embedding and lm_head included), a partitioner assigns layers to stages to minimise the slowest stage, and the simulator uses the resulting uneven times.
    test · For GPT-2 small with embedding on stage 0 and lm_head on the last stage the partitioner beats the equal 6/6 split on makespan at m=8; the simulated bubble drops by at least 5 points.
  5. 05decode as a pipeline of waves
    Model serving where each decode step is a wave of m micro-batches through p stages, reporting per-token latency and tokens per second against m.
    test · Tokens per second approaches p times a single stage as m grows (within 10 percent at m=16 for p=4) while per-token latency grows linearly in m; the CSV and plot are written to results/.
  6. 06run it on gloo ranks
    pp_gpt2.py splits GPT-2 small into 2 or 4 stages, each rank executes your GPipe schedule for 4 micro-batches with dist.send and dist.recv of activations, and timestamps every event.
    test · Logits equal the single-process model within atol 1e-4 on 2 and 4 ranks; the bubble fraction measured from rank timestamps is within 10 points of the simulator prediction for the same stage times.

Knowledge check

QWhat is the bubble fraction for 4 stages and 4 micro-batches?

(4-1)/(4+4-1) = 3/7, about 43 percent idle.

QWhy does pipeline parallelism tolerate slow networks better than tensor parallelism?

PP sends one activation tensor per stage boundary per micro-batch, whereas TP needs two all-reduces per layer; the number and size of messages is far smaller.

QWhy is the first or last pipeline stage often given fewer transformer layers?

The embedding table and the LM head (vocab-sized matrices) add compute and memory to those stages, so balancing requires fewer layers there.

QHow does continuous batching help a pipelined serving engine?

It keeps several micro-batches of different requests in flight so every stage has work, which fills the bubbles that a single sequential request would leave.

Completion

  • All 5 subtopics marked done
  • All 2 required lessons done
  • Lesson l1 simulator reproduces the bubble formula.
  • Lesson l2 two-stage GPT-2 matches reference logits.
  • Can explain when to use PP versus TP in one paragraph with a bandwidth argument.
Topic progress is derived; it reaches 100% when both checks above pass.Next recommended topic →