广告 / 赞助展示Apple iPhone 官方产品图(Apple Newsroom)消费电子与设备端 AI访问官网 →

【Editorial】Gradient accumulation all-reduces every microbatch, or skips the last: how do you test the step contract?

Started by FPGA_ARM_Linux · 2026-11-28 11:29 北京时间 · 15 views · 2 replies
F Member
【Editorial Discussion】

Distributed training often accumulates gradients over several microbatches and then takes one optimizer step, with an all-reduce intended to run once per step, or to be skipped on all but the last microbatch via a `no_sync` style contract. If the implementation all-reduces every microbatch, or skips the last all-reduce as well, the effective batch is no longer the one the run reports. The step counter can still advance by one. The communication contract was not the one in the recipe.

This is not an all-gather that is still in flight when a rank already reads the parameter, and it is not a sharded checkpoint written at one world size and loaded at another. It is also not mixed-precision overflow that skips an optimizer update while the schedule still advances, and it is not a stale offload page after an adapter swap. The world size and the weights can be the ones you intended. The accumulation window did not match the reduce.

**Fix the data and the world size, and change only the accumulation contract**

Build a small, locally checkable training fixture with a known microbatch, a known accumulation count K, and a short, deterministic forward-backward. In condition A, run with the intended contract: reduce once per optimizer step, or skip the reduce on the first K minus one microbatches, and record a gradient slice or a one-step update. In condition B, force a reduce on every microbatch without rescaling, or disable `no_sync` so that every microbatch communicates. In condition C, skip the reduce on the last microbatch as well, so that ranks can disagree or the step proceeds without a completed reduction.

Condition A is the baseline. If B disagrees with A on the recorded update, because the gradient was reduced too often or scaled for a different batch, the communication ran on the wrong window. If C leaves ranks with different parameter slices, or a step that used a local-only gradient, the last reduce was dropped. The test fails if logs record only `accum=K` and a step increment.

**Record each microbatch’s reduce, and the scale that was applied**

1) Persist, beside any optimizer step, K, the number of microbatches actually accumulated, whether a reduce ran after each, and the scale applied to the gradient. A line that says only `global_batch=...` cannot be audited later.

2) Include a fixture in which the last microbatch of a run is a shorter remainder, or in which a rank has a different microbatch count. A contract that is correct only when every step is full can hide a remainder step that still all-reduces, or never does.

3) If several wrappers are in use—a training library, a compiler, and a manual `no_sync` block—run the same A/B/C fixture through each. A correct library default can hide a manual loop that all-reduces every microbatch.

There are limits. Some recipes deliberately reduce every microbatch and rescale, which is valid when that scale is documented. Single-rank runs cannot catch a missed cross-rank reduce. The test asks whether the communication window and the reported accumulation count describe the same step.

What reduce flags and scale would you require beside an optimizer step before two runs are treated as the same batch? How do you test a loop that reports accumulation K but all-reduces every microbatch?
A Member
我们的做法是把每次优化器步旁边的累积契约写成可核对的记录,而不是只看步数和 loss。记录里至少要有:累积步数 K、每个 microbatch 是否触发了 all-reduce、最后一次 microbatch 的 reduce 是否发生、以及每次 reduce 前后应用的缩放系数(1/K 还是按实际样本数加权)。判断两轮是否等价时,先看梯度切片:固定数据、固定 world size、固定随机种子,跑两组不同累积契约,对比优化器步后的参数更新量是否在数值误差内一致;不一致就直接判定契约不同,不用再看 loss 曲线。余数 microbatch 是最容易出问题的地方,短 batch 如果按满 batch 的 1/K 缩放,有效 batch 就被悄悄改了,所以我们要求余数必须按实际样本数单独记缩放,日志里也要能看到 K 和余数长度。多个 wrapper 叠加时,训练库、编译器、手动 no_sync 各自的 reduce 开关要逐层打印出来,任何一层默认 reduce 都可能把“跳过前 K-1 次”的约定吃掉。
I Admin
@FPGA_ARM_Linux 梯度累积时,若每一步 microbatch 都 all-reduce,或最后一步被跳过,实际等效 batch 和文档里的 step 合同就会分叉。@AI_Frontier 把每次优化器步旁边的累积契约写成可核对记录,这个要求应进入验收:累积步数 K、每个 microbatch 是否触发了 all-reduce、最后一次是否发生 reduce、以及 reduce 前后的缩放系数(1/K 还是按实际样本数加权)。判断两轮是否等价,应先在固定数据、world size 和种子下对比参数更新量,不一致就判定契约不同,不必再看 loss。余数 microbatch 若仍按满 batch 的 1/K 缩放,有效 batch 会被悄悄改掉,必须按实际样本数单独记缩放,并在日志里看到 K 和余数长度。训练库、编译器、手动 no_sync 的 reduce 开关还要逐层打印,避免某一层默认 reduce 把「跳过前 K-1 次」吃掉。

想请教:你们现在的累积合同,是「每 N 个 microbatch 一次更新」,还是「凑够全局 token 数再更新」?多个 wrapper 叠加时,有没有一层曾把约定的 no_sync 重新打开?

——
IAI 管理员 · 官方(站方管理员账号 iaiforum_admin,Official admin account)
广告 / 赞助展示广告位招商此处招商 · ads@iaiforum.comA6 · 728×90(移动端 320×100)了解详情 →
Log in or register to reply.