Stage imbalance and repartitioning
Locate the dependency critical path and amortize a safe partition change rather than treating average stage cost as throughput.
On this page
The question for today
What you’ll understand
- Compute an unequal-stage forward-wave recurrence.
- Separate critical-path time from total device work.
- Plan checkpointed repartitioning with state and amortization costs.
An average stage time can look healthy while one stage determines the cadence of every downstream microbatch. Repartitioning can reduce that bottleneck, but the change also moves parameters, optimizer state, activation boundaries, and communication contracts. This lesson uses the cost-aware motivation of GPipe and the explicit stage construction of PyTorch 2.8. Its small numerical examples model forward-only waves so that unequal-stage timing can be hand checked without pretending to solve a complete training schedule.
Find readiness and resource constraints together
A stage operation waits for its own input and for the stage’s device resource. For a forward-only chain with one serial operation per stage, these are the previous stage’s completion for the same microbatch and the same stage’s completion for the previous microbatch. The maximum of those readiness times determines its start. Backward, communication, recomputation, shared links, and multiple chunks add more dependencies; they cannot be handled by replacing every stage with an average measured number.
Profile representative inputs because attention costs can change with sequence length, and output projections or embeddings can give edge stages different work. Separate genuine compute from time waiting for communication or another rank. A stage that appears slow in a synchronized measurement may be a victim of an upstream bottleneck rather than its cause. Build a trace that identifies operation readiness, device occupancy, and transfer completion before deciding which layer to move. End-to-end iteration time is the objective, not equality of one selected profiler column.
A better cut can change another bottleneck
Moving operations changes more than arithmetic balance. A new boundary can increase payload size, shift a transfer to a slower physical path, or extend the lifetime of a residual activation. Extra retained memory can force a different microbatch size or recomputation policy. Tied parameters and optimizer shards also constrain legal movement. Reprofile a candidate after its graph and memory contracts are made valid. A compute-only improvement is useful evidence for a hypothesis, not a guarantee that the whole distributed job becomes faster.
Use critical-path and resource-work lower bounds as checks on a predicted makespan. A dependency path cannot complete faster than the sum of its required durations, and a device cannot perform more serial work than fits in elapsed time under the model. These bounds can expose an impossible speedup claim, but matching a bound does not certify a real schedule. Contention, incomplete overlap, or changed kernels can still add time. State which operations and resources are represented in each comparison.
Repartition at a consistent recovery boundary
A practical repartition often drains the intended update, saves a consistent checkpoint, rebuilds the partition and rank mapping, and restores parameters, optimizer moments, scheduler state, buffers, and data progress. Preserve random-state and update-count semantics as required by the experiment. Moving only weights can reset optimizer history or duplicate data even if outputs look plausible. Revalidate tensor contracts and compare a small subsequent update with the unrepartitioned reference. The time spent saving, moving, rebuilding, and reloading belongs in the cost decision.
The queue lab illustrates sustained imbalance only. Reset, set arrival rate to 4 and service rate to 3, predict backlog at one second, then Step twice: five units. Compare a fresh Reset with both arrival rate at 4 and service rate at 5 before two Steps: three units. The initial backlog is four. The instrument does not identify a critical path, move layers, rebuild checkpoints, or simulate an unequal-stage timeline; use the recurrence and an actual trace for those questions.
Work it through
Balance a declared three-stage forward wave
Four microbatches traverse three forward-only stages with costs 3, 7, and 4 ms. Ignore communication and all backward work. Compare a legal candidate with costs 5, 5, and 4 ms, then account for an assumed 60-ms one-time repartition cost.
For the original order, the first microbatch completes after 3 + 7 + 4 = 14 ms. The seven-ms stage sets the subsequent cadence, so final completion is 14 + 3 × 7 = 35 ms; the recurrence confirms completions 14, 21, 28, and 35 on the last stage.
The candidate keeps the first completion at 14 ms but changes the cadence to five, giving 14 + 3 × 5 = 29 ms. This is a six-ms improvement for the stated forward wave, not a full training result.
A 60-ms change cost takes ten such six-ms savings to break even. An eleventh comparable wave produces a positive net saving, assuming the improvement persists and no other cost changes.
A safe repartition is justified by a valid end-to-end improvement and sufficient remaining work, not balance alone.
The living notebook
Explore the model
Interactive lab · queue
A sustained service bottleneck analogy
Reset, set λ = 4 and μ = 3, predict Q at one second, then Step twice: Q = 5. Compare a fresh Reset with both λ = 4 and μ = 5 before two Steps: Q = 3. This initial-four fluid model does not compute pipeline critical paths or repartition a model.
- 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 forImproving a limiting service rate can reduce backlog, but a repartition needs graph, state, and end-to-end timing validation.
Pause & reason
Check your understanding
Your turn
Try it yourself
Three microbatches use forward-only costs (2,6,4) ms, compared with (4,4,4) ms. Under the stated recurrence and zero communication, compute the final completion times. If changing costs 40 ms, how many identical waves break even?
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
In these cases, sum the first-microbatch path and add two intervals at the bottleneck cadence, then verify with the recurrence.
Compare with the solution
The original completes at 12 + 2 × 6 = 24 ms, while the balanced candidate completes at 12 + 2 × 4 = 20 ms. Saving four milliseconds per wave amortizes 40 ms after ten waves; additional waves yield a positive net benefit under unchanged assumptions. Full training, boundary traffic, and checkpoint restoration may change both the saving and the one-time cost.
Keep the idea
Take it with you
- Unequal-stage timing follows readiness and device constraints, not average cost.
- A new cut can shift memory and communication bottlenecks even when compute improves.
- Repartitioning must preserve consistent state and amortize its real one-time cost.
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.