Commit 51c5491
committed
[FSDP][Loss] Switch gradient reduction to SUM
Flip gradient reduction from mean to pure SUM, so each parameter receives the
sum of per-rank local-component gradients, i.e. the global loss gradient, with
no compensating divides. This is the single atomic semantic change; the three
cancelling x world_size factors and the reduce divides are removed together.
- BaseModel/MoE/Dense.fully_shard call set_gradient_reduce_sum() at the end.
- MoE.scale_and_reduce_grad drops the expert div_(ep_size) and the replicated
div_(flat_mesh.size()); only the coalesced SUM all_reduce remains.
- CE: drop the WORLD autograd all_reduce; the loss stays this rank's local
component (display global value restored by the C2 detached pipeline).
- Balancing: use local_gating_sum directly instead of all_reduce_autograd;
the global detached statistics (tokens_global/seqlen_global/scale_global) are
kept unchanged.
- Z-loss: drop the x world_size in the global-average branch.
Verified on torch 2.10 (bf16 force-sum): distributed full gradient reproduces a
single-process full-batch token-mean CE reference at EP=1 and EP=2 (norm-ratio
median 0.9998, EP-invariant); global display loss unchanged through the flip;
balancing+z backward stays finite. Regression tests cover the bf16 reduce-sum
mechanism, token-mean parity with grad-acc=2, aux-loss finite gradients, and an
isolated fp32 balancing-only gate-gradient A/B (new local+SUM vs old
all_reduce_autograd+AVG) asserting element-wise equality.1 parent a13bca8 commit 51c5491
8 files changed
Lines changed: 443 additions & 32 deletions
File tree
- tests/model
- xtuner/v1
- loss
- model
- compose
- intern_s1
- dense
- moe
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | 9 | | |
11 | 10 | | |
12 | 11 | | |
| |||
282 | 281 | | |
283 | 282 | | |
284 | 283 | | |
285 | | - | |
286 | | - | |
287 | | - | |
288 | | - | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
289 | 288 | | |
290 | 289 | | |
291 | 290 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
155 | 155 | | |
156 | 156 | | |
157 | 157 | | |
158 | | - | |
159 | | - | |
160 | 158 | | |
161 | 159 | | |
162 | 160 | | |
163 | 161 | | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
174 | 170 | | |
175 | 171 | | |
176 | 172 | | |
| |||
298 | 294 | | |
299 | 295 | | |
300 | 296 | | |
301 | | - | |
302 | 297 | | |
303 | 298 | | |
304 | | - | |
305 | | - | |
306 | | - | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
307 | 303 | | |
308 | | - | |
309 | | - | |
| 304 | + | |
310 | 305 | | |
311 | | - | |
312 | 306 | | |
313 | 307 | | |
314 | | - | |
| 308 | + | |
315 | 309 | | |
316 | 310 | | |
317 | 311 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
676 | 676 | | |
677 | 677 | | |
678 | 678 | | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
679 | 683 | | |
680 | 684 | | |
681 | 685 | | |
| |||
1388 | 1392 | | |
1389 | 1393 | | |
1390 | 1394 | | |
| 1395 | + | |
| 1396 | + | |
| 1397 | + | |
| 1398 | + | |
| 1399 | + | |
| 1400 | + | |
| 1401 | + | |
| 1402 | + | |
| 1403 | + | |
| 1404 | + | |
| 1405 | + | |
| 1406 | + | |
| 1407 | + | |
| 1408 | + | |
| 1409 | + | |
1391 | 1410 | | |
1392 | 1411 | | |
1393 | 1412 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
138 | 138 | | |
139 | 139 | | |
140 | 140 | | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
141 | 147 | | |
142 | 148 | | |
143 | 149 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
101 | 105 | | |
102 | 106 | | |
103 | 107 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
300 | 300 | | |
301 | 301 | | |
302 | 302 | | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
303 | 307 | | |
304 | 308 | | |
305 | 309 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1193 | 1193 | | |
1194 | 1194 | | |
1195 | 1195 | | |
| 1196 | + | |
| 1197 | + | |
| 1198 | + | |
| 1199 | + | |
1196 | 1200 | | |
1197 | 1201 | | |
1198 | 1202 | | |
| |||
1222 | 1226 | | |
1223 | 1227 | | |
1224 | 1228 | | |
1225 | | - | |
1226 | | - | |
| 1229 | + | |
| 1230 | + | |
| 1231 | + | |
1227 | 1232 | | |
1228 | | - | |
1229 | 1233 | | |
1230 | 1234 | | |
1231 | 1235 | | |
| |||
1252 | 1256 | | |
1253 | 1257 | | |
1254 | 1258 | | |
1255 | | - | |
1256 | | - | |
| 1259 | + | |
| 1260 | + | |
1257 | 1261 | | |
1258 | 1262 | | |
1259 | 1263 | | |
| |||
0 commit comments