先画时间轴:stage 越靠后,同一 micro-batch 越晚到达
🎯 先猜一猜
p=3、m=4,在时间 t=4 查看 stage=2。
此时 micro_idx=t-stage 是多少,是否应记录?
先补的知识
- p 表示 Pipeline stage 数量,m 表示 micro-batch 数量;编号都从 0 开始。
- timeline 是 Python 列表;timeline[t] 又是一个列表,保存该时间步活跃的 (stage, micro_idx) 元组。
- micro-batch 每经过一个 stage 就晚一个时间步,因此在时间 t、阶段 stage 上,对应编号是 micro_idx = t - stage。
- 只有 0 <= micro_idx < m 时该任务真实存在;负编号表示还没到达,编号达到 m 表示所有 micro-batch 已经过完。
图解原理
把 micro-batch 想成依次进入流水线的小批任务。第 0 个任务在 t=0 进入 stage 0,t=1 到 stage 1;与此同时第 1 个任务进入 stage 0。于是活跃项沿着“时间向右、stage 向下”的对角线移动。
| 时间 t | p=3、m=4 时的 active 列表 | 活跃槽位数 |
|---|---|---|
| 0 | [(0,0)] | 1 |
| 1 | [(0,1),(1,0)] | 2 |
| 2 | [(0,2),(1,1),(2,0)] | 3 |
| 3 | [(0,3),(1,2),(2,1)] | 3 |
| 4 | [(1,3),(2,2)] | 2 |
| 5 | [(2,3)] | 1 |
为什么有 m + p - 1 步
4 个 micro-batch 依次进入需要 4 步;最后一个进入后,还要再穿过剩余 p-1=2 个 stage,所以总共 6 步。
一条公式决定格子内容
t 与 stage
计算micro_idx = t - stage
保留条件0 <= micro_idx < m
语法热身:用两道工序和五个订单练对角线调度
stations, orders = 2, 5
steps = stations + orders - 1
schedule = []
for tick in range(steps):
working = []
for station in range(stations):
order_id = tick - station
if 0 <= order_id < orders:
working.append((station, order_id))
schedule.append(working)例子中的变量 -> Notebook TODO 变量/操作
stations->p,orders->m。steps-> Notebook 的总时间步数,来自 stage 数与 micro-batch 数。tick/station/order_id->t/stage/micro_idx。schedule/working->timeline/active:都是嵌套 Python list,不涉及 Tensor dtype 或 device。
巩固一下
p=3、m=4 时,timeline 应包含多少个时间步?
学完这一段,试着做
用一个小动作确认自己理解了;最后再进入官方题目。