Commit c85c870
Fix gradient accumulation for Z2+offload (#6550)
The ZeRO 1/2 optimizer performs incorrect gradient accumulation in the
path for ZeRO2 + Offloading. This issue is caused by two main reasons:
1) The micro_step_id in the ZeRO 1/2 optimizer is:
- Initialized to 0 in the constructor.
- Reset to -1 during the backward pass.
For example, given a gradient accumulation step of 4, the micro_step_id
changes as follows:
- For the first global step: 1, 2, 3, 4.
- Subsequently: 0, 1, 2, 3.
2) Gradients are copied to the buffer on the first micro step and
accumulated in the buffer during the following micro steps. However, the
current code incorrectly copies gradients at steps that are not at the
accumulation boundary.
This PR aligns the micro_step_id initialization in both the constructor
and the backward pass, and corrects the condition for copying and
accumulating gradients.
Co-authored-by: Olatunji Ruwase <olruwase@microsoft.com>
Co-authored-by: Logan Adams <114770087+loadams@users.noreply.github.com>1 parent 0fbe96a commit c85c870
1 file changed
+4
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| 42 | + | |
42 | 43 | | |
43 | 44 | | |
44 | 45 | | |
| |||
224 | 225 | | |
225 | 226 | | |
226 | 227 | | |
227 | | - | |
| 228 | + | |
228 | 229 | | |
229 | 230 | | |
230 | 231 | | |
| |||
1231 | 1232 | | |
1232 | 1233 | | |
1233 | 1234 | | |
1234 | | - | |
1235 | | - | |
1236 | | - | |
| 1235 | + | |
1237 | 1236 | | |
1238 | 1237 | | |
1239 | 1238 | | |
| |||
1824 | 1823 | | |
1825 | 1824 | | |
1826 | 1825 | | |
1827 | | - | |
| 1826 | + | |
1828 | 1827 | | |
1829 | 1828 | | |
1830 | 1829 | | |
| |||
0 commit comments