696. Compute the idle bubble fraction of a pipeline-parallel schedule
Pipeline parallelism puts consecutive layers on different GPUs. With an all-forward then all-backward schedule and equal stage times, the first and last stages sit idle while the pipeline fills and drains. Splitting the batch into more micro-batches shrinks that bubble. This is a cost model, not a real run.
With p stages and m micro-batches, each stage is busy for m time slots out of m + p - 1 in each phase (forward and backward alike), so
bubble = (p - 1) / (m + p - 1)
Write bubble_fraction(stages, microbatches) returning a list of bubble fractions, one per value in microbatches, for the given number of stages. If stages is 1 the bubble is 0.
def bubble_fraction(stages, microbatches)Example 1
bubble_fraction(4, [1, 4]) = [0.75, 3 / 7].
Example 2
Input: stages = 8, microbatches = [8, 16, 32, 64]
Output: [0.466667, 0.304348, 0.179487, 0.098592]
Example 3
Input: stages = 1, microbatches = [1, 5]
Output: [0.0, 0.0]
Constraints
- Python 3 standard library. Code runs in your browser.
- Your code must pass all 4 tests to be accepted.
Hint 1
Each stage is idle for p - 1 of the m + p - 1 time slots, so the bubble is just that ratio.
Hint 2
For every micro-batch count m in the list, compute (stages - 1) / (m + stages - 1) and collect the results in a list.
Hint 3
When stages is 1 the numerator is 0, so the same formula already gives 0 and needs no special case; a list comprehension is enough.
Common mistakes
- (p - 1) / m (the buggy version) is the bubble time relative to useful time, not a fraction of total time, and it exceeds 1 for small m.
Understand the idea
In pipeline parallelism each GPU holds a few consecutive layers, and micro-batches flow through like cars on an assembly line. At the start the later stages wait for work to arrive, and at the end the early stages have nothing left: that idle time is the bubble. More micro-batches spread the same fill and drain cost over more useful work.
p = 4 stages. With m = 1 micro-batch, each phase takes m + p - 1 = 4 slots and each stage is busy for 1, so the bubble is 3 / 4 = 0.75. With m = 8, slots = 11 and the bubble is 3 / 11 = 0.2727. With m = 32 it falls to 3 / 35 = 0.0857.
bubble = (p - 1) / (m + p - 1), computed for each m in the list, and 0 when p = 1
Using (p - 1) / m measures idle time relative to useful time, which is a different number and can exceed 1. This simple cost model guides how many micro-batches to use when training large models with pipeline parallelism.