854. GPipe pipeline bubble and micro-batches
Why it matters Pipeline parallelism is standard for very large models. The bubble formula is the first thing to compute when choosing micro-batch counts.
Paper: "GPipe: Easy Scaling with Micro-Batch Pipeline Parallelism", Huang et al., 2018 (NeurIPS 2019), arXiv 1811.06965.
GPipe splits a model into K pipeline stages and a batch into M micro-batches. The idle "bubble" is (K - 1) / (M + K - 1) of the time. The paper notes the bubble is negligible when M >= 4K.
Write gpipe_plan(K, M, max_bubble).
Inputs: K and M are ints >= 1, max_bubble is a float in (0, 1).
Return a dict:
- "bubble": the bubble fraction for K and M, rounded to 4 decimals.
- "min_microbatches": the smallest int M' >= 1 whose bubble is <= max_bubble.
- "paper_rule_ok": True if M >= 4 * K, else False.
K = 1 has no bubble: bubble 0.0 and min_microbatches 1.
def gpipe_plan(K, M, max_bubble)Example 1
Input: gpipe_plan(4, 8, 0.1)
Output: {'bubble': 0.2727, 'min_microbatches': 27, 'paper_rule_ok': False}
Example 2
Input: K = 1, M = 1, max_bubble = 0.05
Output: {'bubble': 0.0, 'min_microbatches': 1, 'paper_rule_ok': False}
Example 3
Input: K = 8, M = 32, max_bubble = 0.2
Output: {'bubble': 0.1795, 'min_microbatches': 28, 'paper_rule_ok': True}
Constraints
- Python 3 standard library. Code runs in your browser.
- Your code must pass all 5 tests to be accepted.
Hint 1
bubble = (K - 1) / (M + K - 1).
Hint 2
Solve the inequality for M, then check the neighbours to avoid float edge errors.
Hint 3
paper_rule_ok is just M >= 4 * K.
Common mistakes
- Using (K - 1) / M as the bubble.
- Off-by-one at the exact boundary because of float rounding.
- Forgetting the K = 1 case.
Understand the idea
In a GPipe schedule the first stage starts while the others wait, and the last stage finishes while the others idle. Each of those ramps costs K - 1 micro-batch slots out of M + K - 1 total. With K = 4 and M = 8 the bubble is 3 / 11, about 0.2727. To get under 10 percent you need (K - 1) / (M + K - 1) <= 0.1, so M >= 27.
Shapes and data flow
Scalars in, dict out.