Commit d99a642
authored
Fix repeated gradient scaling across pipeline stages (#8154)
## Summary
Fix repeated gradient accumulation scaling when pipeline parallelism is
combined with gradient accumulation.
Every pipeline stage registers an output backward hook that scales
gradients by `gradient_accumulation_steps`. The last stage therefore
scales the gradient correctly before sending it upstream, but each
preceding stage scales the already-scaled gradient again.
Fixes #8152
## Changes
- Apply gradient accumulation scaling only at the last pipeline stage.
- Preserve the existing behavior for single-stage pipelines and
`gradient_accumulation_steps=1`.
- Add a deterministic two-stage regression test that verifies both
stages receive correctly averaged gradients.
- Disable gradient clipping in the regression test so it cannot mask the
scaling difference.
## Testing
- `DS_ACCELERATOR=cpu LOCAL_SIZE=2 OMP_NUM_THREADS=1 pytest -q
tests/unit/runtime/pipe/test_pipe.py::TestPipeGradientAccumulationScaling::test_gradients_are_scaled_once`
- `pre-commit run --files deepspeed/runtime/pipe/engine.py
tests/unit/runtime/pipe/test_pipe.py`
- Two-process CPU/Gloo numerical comparison for
`gradient_accumulation_steps=1,2,8`
Signed-off-by: Yuchen Fan <functionhx@gmail.com>1 parent 75f395f commit d99a642
2 files changed
Lines changed: 45 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
534 | 534 | | |
535 | 535 | | |
536 | 536 | | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
537 | 543 | | |
538 | 544 | | |
539 | 545 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| |||
49 | 50 | | |
50 | 51 | | |
51 | 52 | | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
52 | 91 | | |
53 | 92 | | |
54 | 93 | | |
| |||
0 commit comments