Commit a98dec0
Fix gradient accumulation under FSDP2: defer cross-replica all-reduce (#162)
Fixes #161 (`ddp_sync_grad` is a silent no-op under FSDP2, so
`trainer.grad_accum_iter` saves no communication).
**Change.** Add an FSDP2 branch to `ddp_sync_grad`: on non-boundary
micro-steps, defer only the cross-replica all-reduce via
`FSDPModule.set_requires_all_reduce(enabled, recurse=False)` on every
`FSDPModule`. Restore it in the `finally` block. The intra-node
reduce-scatter still runs each micro-step, so gradients stay sharded and
per-rank memory does not grow. DDP behavior is unchanged.
**Validation** (2x 8xA100-80GB, Ethernet, HSDP shard=8 replicate=2,
batch 64/rank, fleet whose accum=1 step is 22.2 s):
| Config | s/step |
|---|---|
| accum=1 | 22.2 |
| accum=4, without this fix (measured behavior = 4x) | 88.8 |
| accum=4, with this fix | 54.3 / 54.3 / 54.9 |
Decomposition from the two measured points: compute 10.8 s + sync 11.4 s
per micro-batch. Throughput 1.63x at accum=4. Loss stayed in the
expected band.
One commit, 18 added lines, no API change.
---------
Co-authored-by: lfengad <liangf@nvidia.com>1 parent 5d6dedc commit a98dec0
1 file changed
Lines changed: 46 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
241 | 241 | | |
242 | 242 | | |
243 | 243 | | |
244 | | - | |
245 | | - | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
252 | 247 | | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
253 | 288 | | |
254 | 289 | | |
255 | | - | |
| 290 | + | |
256 | 291 | | |
| 292 | + | |
| 293 | + | |
257 | 294 | | |
258 | 295 | | |
259 | 296 | | |
| |||
0 commit comments