Pipeline stages and partition costs
Partition execution with explicit compute, boundary traffic, memory, and backward dependencies rather than equal layer counts.
On this page
The question for today
What you’ll understand
- Define stage boundaries using executed operations and tensor dependencies.
- Evaluate compute balance alongside communication and memory feasibility.
- Derive a bottleneck bound without claiming it is a full schedule simulation.
A pipeline splits a model’s executed computation across devices and lets different microbatches occupy different stages. Equal numbers of transformer blocks are only a starting guess: embedding, attention, output projection, recomputation, and communication can have different costs. This lesson uses the original GPipe partitioning idea and the PyTorch 2.8 pipeline interface as concrete references. A feasible partition must preserve the model function and its backward dependencies before its timing can be optimized.
Partition the executed graph
A stage owns operations, parameters, buffers, and a contract for tensors crossing its boundaries. Module containment alone does not describe execution: a forward function can reuse a module, include functional operations, or carry a residual value across an apparent cut. The partition must route every required tensor with the correct identity, shape, dtype, and layout. PyTorch 2.8 distinguishes manual stage construction from graph-based splitting; a particular stage interface can impose a simpler sequential topology than an arbitrary model graph.
For a simple chain, stage s consumes the activation sent by stage s minus one, computes its output, and sends that output onward. Backward waits for the corresponding downstream activation gradient and uses saved or recomputed forward state to produce upstream gradients and parameter gradients. Attach a microbatch identifier to both directions. Receiving the correct-sized tensor for the wrong microbatch is still incorrect. Tied parameters, nonlocal residuals, and stateful buffers require additional explicit coordination rather than an assumption that every boundary is one interchangeable tensor.
Balance a feasible resource assignment
Profile forward and backward separately at the intended microbatch shape and precision. Include activation-checkpointing choices, kernel efficiency, and any tensor-parallel communication inside a stage. A partition with balanced forward work can have an imbalanced backward or a large boundary tensor that crosses a slower link. Memory feasibility includes weights, optimizer state, communication buffers, temporary workspaces, and the maximum simultaneously live activation state. Parameter count alone misses several of those terms and does not guarantee a stage fits.
For a fixed partition, the slowest serial stage resource constrains steady-state admission, but dependencies, fill, drain, and contention can make actual execution slower. A bandwidth estimate for a boundary is a lower bound under stated transfer assumptions, not necessarily fully exposed time. Placement also matters: the same graph cut can use an intra-node link in one mapping and an inter-node path in another. Record the physical rank placement alongside the logical partition so a reproducible cost estimate names the communication path it assumes.
Compare candidate cuts with the same objective
Choose whether the objective is iteration time, tokens per second, peak memory, or a constrained combination. Moving one layer can improve a compute balance while creating an infeasible activation boundary or duplicating a tied state. Evaluate candidate cuts against representative sequence lengths and avoid using one favorable batch as a universal partition. Once a small valid configuration runs, compare numerical losses and gradients with an unsplit reference before scaling it to additional ranks. This separates an execution mistake from an optimization hypothesis.
The queue lab isolates only a bottleneck-rate consequence. Reset, set arrival rate to 4 and service rate to 3, predict the backlog after one second, then Step twice. Compare a fresh Reset with both arrival rate at 2 and service rate at 3 before two Steps. The outcomes are five and three abstract units from an initial four. This fluid model does not partition layers, execute forward or backward passes, or account for communication and stage memory.
Work it through
Choose a cut before adding a communication model
Four consecutive layer groups have measured combined forward-plus-backward costs of 2, 6, 3, and 5 milliseconds per microbatch. Assume additive compute, two stages, and initially ignore communication. Compare the three contiguous cuts, then bound one declared boundary transfer.
Cut after group one gives stage costs 2 and 14 milliseconds. Cut after group two gives 8 and 8; cut after group three gives 11 and 5. The middle cut minimizes the compute-only maximum at eight milliseconds.
An eight-MiB boundary over an assumed eight-GiB/s path has movement lower bound (8/8192) seconds, approximately 0.977 milliseconds. This does not establish how much is exposed or whether traffic shares a bottleneck.
Recheck the middle cut’s tensor shape, memory feasibility, backward traffic, and overlap. Its compute balance makes it a candidate, not a demonstrated fastest end-to-end partition.
The compute-only cut is balanced, while a defensible final choice also needs boundary and lifetime evidence.
The living notebook
Explore the model
Interactive lab · queue
Admission versus one limiting service rate
Reset, set λ = 4 and μ = 3, predict Q at one second, then Step twice: Q = 5. Compare a fresh Reset with both λ = 2 and μ = 3 before two Steps: Q = 3. This fixed-rate fluid queue starts at four and does not implement a pipeline partition or training schedule.
- Fluid backlog
- 4
- Net rate · λ − μ
- −1 /s
- M/M/1 mean residence
- 1 s
Fluid backlog starts at 4 and follows max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time formula assumes Poisson arrivals, exponential service and λ < μ. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forA stage-rate bottleneck can create backlog, but graph partitioning and execution correctness require separate analysis.
Pause & reason
Check your understanding
Your turn
Try it yourself
For four groups costing 3, 5, 4, and 4 milliseconds under the same compute-only assumptions, evaluate all contiguous two-stage cuts. Name a condition that could still rule out the balanced candidate.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Accumulate each prefix and compare its cost with the remaining suffix.
Compare with the solution
The cuts produce (3,13), (8,8), and (12,4) milliseconds. The middle cut has the smallest maximum, eight milliseconds. It can still be unsuitable if its boundary shape is unsupported, required activation storage exceeds capacity, communication is costly, or tied state and residual dependencies are not implemented correctly. A real throughput claim needs a valid execution and measured end-to-end timing.
Keep the idea
Take it with you
- A pipeline boundary is an execution and tensor contract, not just a layer-count division.
- Balance compute only after enforcing memory and dependency feasibility.
- Rate bounds and boundary-transfer estimates do not replace a complete schedule measurement.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.