advanced28 min studyLesson 562 in Engineering

Fill-drain schedules and bubbles

Derive balanced-stage fill/drain time and distinguish elapsed idle fraction from overhead relative to ideal compute.

On this page

What you’ll understand

  • Trace all-forward then all-backward dependencies for a small pipeline.
  • Calculate bubble time under explicit balance and communication assumptions.
  • Keep elapsed-time and ideal-compute denominators distinct.

A fill-drain training iteration runs every microbatch forward, then propagates every microbatch backward before the next optimizer update. Devices are idle while the pipeline fills and drains, even when its stages have equal costs. The original GPipe schedule makes that tradeoff easy to analyze and keeps one parameter version throughout an iteration. This lesson follows GPipe and the Megatron-LM 2021 schedule analysis, with PyTorch 2.8 providing a version-pinned implementation reference.

Trace dependencies before counting bubbles

Label a forward operation by stage and microbatch. The first stage can process another microbatch while the next stage works on the previous one. A downstream forward cannot start until its own input arrives, so later stages begin with idle intervals. In the all-forward schedule, the backward phase begins after the forward wave finishes, then travels in the reverse direction. A stage’s backward also requires its matching saved or recomputed activation state, not just any received gradient.

For the following arithmetic, assume P stages, M microbatches, the same forward duration f on every stage, and the same backward duration b on every stage. A stage cannot run two modeled compute operations simultaneously; communication, update time, recomputation, and other overhead are omitted or already handled consistently. Forward and backward durations need not equal each other. These restrictions are what make the simple closed form useful. A partition with unequal costs or substantial exposed transfers needs an explicit dependency timeline instead.

Tflush=(M+P−1)(f+b),Tideal=M(f+b)T_{\mathrm{flush}}=(M+P-1)(f+b),\qquad T_{\mathrm{ideal}}=M(f+b)
For positive f and b in a common time unit and integer P,M at least one, a balanced GPipe-style all-forward/all-backward schedule has M+P−1 forward slots and the same number of backward slots. Ideal time here omits pipeline fill/drain while retaining each device’s M operations of each type.↔ Scroll wide equations horizontally.

Specify the denominator in an efficiency claim

The additional time over ideal computation is the bubble time. Dividing it by ideal time gives an overhead ratio, while dividing it by elapsed iteration time gives an elapsed idle fraction. Those numbers differ, especially when M is small. Under the balanced compute-only assumptions, average device compute utilization equals the useful fraction of elapsed time. It is not a profiler utilization reading and does not include simultaneous communication. Label the denominator rather than reporting an ambiguous bubble percentage.

Increasing the microbatch count reduces the modeled relative bubble, but it is not free. At a fixed global batch, smaller microbatches can change kernel efficiency and communication startup costs; at a fixed microbatch size, more microbatches increase the global batch and can change optimization behavior. An all-forward schedule can also retain activation state for all M microbatches. Choose the experiment’s invariant first, then measure the consequences of the change rather than attributing every throughput difference to the closed-form bubble term.

TbubbleTideal=P−1M,TbubbleTflush=P−1M+P−1,U=MM+P−1\frac{T_{\mathrm{bubble}}}{T_{\mathrm{ideal}}}=\frac{P-1}{M},\qquad \frac{T_{\mathrm{bubble}}}{T_{\mathrm{flush}}}=\frac{P-1}{M+P-1},\qquad U=\frac{M}{M+P-1}
These dimensionless ratios use T_bubble = (P−1)(f+b). The first is overhead relative to ideal compute, the second is idle fraction of elapsed time, and U is average compute-only device utilization under the balanced assumptions.↔ Scroll wide equations horizontally.

A flush is also an optimization boundary

Accumulate the correctly normalized microbatch gradients while the parameter version remains fixed, then perform the intended optimizer update after all required backward and gradient-reduction work completes. This ordering supports synchronous training semantics under additional loss, randomness, state, and arithmetic assumptions. It does not automatically make microbatch batch-normalization identical to full-batch statistics or floating-point summation bitwise identical. Distinguish optimizer equivalence from a favorable utilization curve, and include numerical tests alongside schedule timing.

The queue lab illustrates only cessation of admissions. Reset, set arrival rate to 4 and service rate to 4, predict backlog at one second, then Step twice: it remains four. For a separate drain experiment, Reset and explicitly set both arrival rate to 0 and service rate to 4 before two Steps: it reaches zero. The model has no stages or forward/backward waves, so its one-second drain cannot be substituted into a training bubble equation.

A three-stage flush with four microbatches

Use three balanced stages, four microbatches, one-millisecond forward operations, and two-millisecond backward operations. Ignore all communication and update time. Compute phase lengths, total time, and both bubble ratios.

  1. The forward wave needs 4 + 3 − 1 = 6 one-millisecond slots. The last stage’s final forward occupies the interval from five to six milliseconds. The reverse backward wave needs six two-millisecond slots, or twelve milliseconds.

  2. Elapsed time is 6 + 12 = 18 milliseconds. Ideal per-device computation is 4 × (1 + 2) = 12 milliseconds, leaving a six-millisecond bubble.

  3. The bubble is 6/12 = 50% of ideal time, but only 6/18 = 33⅓% of elapsed time. Compute-only average utilization is 12/18 = 66⅔%, not 50%.

The result

A single timeline supports different ratios; a meaningful efficiency statement names its denominator and assumptions.

Explore the model

Interactive lab

Interactive lab · queue

Stopping admission is different from continuing a balanced flow

Reset, set λ = 4 and μ = 4, predict Q at one second, then Step twice: Q = 4. For a fresh drain run, Reset and set both λ = 0 and μ = 4 before two Steps: Q = 0. This initial-four fluid queue has no training stages and cannot calculate pipeline bubbles.

Stopping admission is different from continuing a balanced flowFluid backlog starts at 4 and follows max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time formula assumes Poisson arrivals, exponential service and λ < μ. Current readings: Fluid backlog: 4; Net rate · λ − μ: −1 /s; M/M/1 mean residence: 1 s. All parameters are adjustable with the labeled controls below.02.557.51001.534.56Elapsed time · sFluid backlogλ < μ
Fluid backlogCurrent time
Fluid backlog
4
Net rate · λ − μ
−1 /s
M/M/1 mean residence
1 s
t = 0 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 finite flush stops admitting work, while a steady balanced arrival stream need not drain.

Check your understanding

For P = 3 and M = 4 under the balanced compute-only model, which statement uses the denominators correctly?

Try it yourself

Two balanced stages process six microbatches with f = b = one millisecond. Compute elapsed time, ideal time, bubble overhead relative to ideal, and elapsed idle fraction. State one omitted cost.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

There are M + P − 1 slots in each phase. Keep the two denominators separate.

Compare with the solution

Elapsed time is (6 + 2 − 1) × 2 = 14 milliseconds, while ideal compute is 6 × 2 = 12. The two-millisecond bubble is 1/6, approximately 16.67%, of ideal time and 1/7, approximately 14.29%, of elapsed time. Exposed communication, optimizer work, recomputation, stage imbalance, or gradient reduction can increase actual execution time.

Take it with you

  • Balanced fill/drain time follows from a specific dependency schedule and omitted-cost model.
  • Overhead relative to ideal compute differs from idle fraction of elapsed time.
  • Microbatch count affects memory and optimization as well as a modeled bubble.

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.