Repository navigation
[metrics] Average FLOPs over DP by the number of summed values - #5879
Conversation
## Summary (human-written)
<!-- Write this yourself, not with an AI tool. 2-5 sentences: the problem, why
this approach, and what you checked yourself. -->
## Why
Since #4819, the trainer logs the DP mean of the per-replica FLOPs as `dist_sum(flops) / dp_mesh.size()`. A fake process group's all-reduce changes nothing, so on a fake-PG run the sum is only the local value, and the logged TFLOPS and MFU are divided by the DP degree. The FLOPs estimator itself is correct. Real process groups are not affected.
**Example.** The llama3 debug model on a fake process group with NGPU=8 (DP8, no CP) logs 8.946e6 FLOPs per token on main, about 1/8 of the 7.158e7 it logged before #4819. The estimator gives the same per-token FLOPs as before, so the logged TFLOPS and MFU are about 1/8 of the right value: 1 over the DP degree. With this change it logs 7.158e7 again.
## What changed
- `dist_utils.dist_mean` all-reduces `[x, 1]` and divides the sum by the count. On a real process group this equals `dist_sum(x) / mesh.size()`. On a fake process group it returns the local value.
- `Trainer.train_step` and the RL trainer use it for the DP mean of FLOPs.
## Alternatives considered
- Log local FLOPs, like `tps` uses local tokens. That drops the DP mean that #4819 added for batches whose FLOPs differ across DP ranks.
- Skip the reduction when the backend is fake. That adds a backend check to the trainer for a case the count handles on its own.
## Numerics / performance
Loss and grad norm are not affected. On real process groups, the logged FLOPs do not change.
Fake-PG runs, NUMA-bound, step 10 unless noted. FLOPs per token = logged FLOPs per second / logged tokens per second, so it does not depend on the hardware.
| Config | Base | FLOPs per token | vs before #4819 |
|---|---|---|---|
| llama3 debug model, NGPU=8 (DP8, no CP), step 9 | before #4819 (base 887d895) | 7.158e7 | 1 |
| same | origin/main | 8.946e6 | about 1/8 |
| same | this change | 7.158e7 | within 0.1% |
| `muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake` (DP8/CP8/EP64), from a later commit in this stack | before #4819 (base 887d895) | 3.274e11 | 1 |
| same | main's FLOPs logging (later stack without this fix) | 4.093e10 | about 1/8 |
| same | this change | 3.274e11 | within 0.1% |
The llama3 run has no CP, and main is still about 8x low, so the cause is the DP degree, not CP. With this change, FLOPs per token match the pre-#4819 formula on both configs. The per-batch estimator could differ from that formula for sliding-window or document masking, but it does not on these two configs.
## Testing
- `test_trainer_accumulates_reused_accelerator_graph_losses[fake_dp=True]` runs `Trainer.train_step` on an 8-way DP mesh of a fake process group. It catches the logged FLOPs being divided by the DP degree. On main it logs 75.0 instead of 600.0.
- CPU tests: trainer, GraphTrainer trace module, FLOPs estimators, distributed utils, Muse Spark recipe and model, optimizer, invalid loss, checkpointer, observability. All pass except `test_debug_recipe_tokenizer[muse_spark_debugmodel_mm]`, which also fails on main.
## Known issues / follow-ups
- The TorchFT trainer divides its FLOPs and loss sums by `dp_mesh.size()` and `ft_pg.size()` in the same way. It is not changed here.
stack-info: PR: #5879, branch: fegin/stack/8
|
@claude review the PR |
|
Claude finished @fegin's task in 1m 24s —— View job Approve -- please fill in the empty human-written summary before merging. See review |
There was a problem hiding this comment.
Verdict: Approve -- please address the items below before merging -- the fix is small and correct, it removes code from both callers, and real-PG numerics stay the same.
What this PR does. The DP mean of per-replica FLOPs used to be dist_sum(flops) / dp_mesh.size(). A fake PG all-reduce is a no-op, so on a fake PG that formula gives local / dp_degree. The new dist_utils.dist_mean all-reduces [x, 1] and divides by the reduced count. On a real PG the count equals mesh.size(). On a fake PG it stays 1. Trainer.train_step and the RL trainer's optim_step now call it.
Example. Fake PG, DP=8, local FLOPs = 600:
main: dist_sum(600) / 8 -> 600 / 8 = 75 (MFU logged 8x low)
this PR: sum([600, 1]) -> [600, 1] -> 600 / 1 = 600
real PG: sum([600, 1]) -> [4800, 8] -> 4800 / 8 = 600 (same as main)
Must fix
- PR description -- "Summary (human-written)" is empty. Please fill it in before merging.
Should consider
torchtitan/experiments/torchft/trainer.py:470-483-- this file has the samedist_sum(...) / dp_mesh.size()and/ ft_pg.size()pattern, so fake-PG torchft runs still log FLOPs that are too low. The description already lists it as a follow-up. That is fine because it is an experiment, but a TODO pointing atdist_meanwould keep it from being forgotten.
Details
- Gate: the problem exists on main. Fake-PG runs are used to estimate TFLOPS and MFU, and FLOPs is the only logged metric that divides by a hard-coded mesh size. Loss is still right on a fake PG:
global_valid_tokensis also only the local count there, sosum(local_loss_sum / global_tokens)is the local mean. Using the count is a better fit than checking for the fake backend in the trainer. - Real-PG numerics: the count is an exact float64 integer equal to
mesh.size(). FLOP counts are integer-valued and well under 2^53, so the float64 sum is exact in any order, and stacking a second element doesn't change the result. The extra element in the all-reduce costs nothing measurable..tolist()replaces an.item()that already sat at this logging point, so it adds no sync. - Test: it extends an existing test with a
fake_dpparametrization rather than adding a new one, and it guards the regression (75.0 on main vs 600.0 here). In the test,get_optional_meshreturns the DP mesh for every name, including"pp". That doesn't matter here becauseno_registered_aux_lossesmakescollect_aux_loss_metricsreturn early.
Not verified: I couldn't run the CPU test in this environment. A maintainer still needs to review, since the PR touches trainer.py.
| torch.stack((x, torch.ones_like(x))), | ||
| reduceOp=c10d.ReduceOp.SUM.name, |
There was a problem hiding this comment.
curious why do this vs. use the AVG reduceOp?
There was a problem hiding this comment.
If we use AVG reduceOp, it will still be an no-op. So we still get incorrect flops when using fake pg.
Stacked PRs:
Summary (human-written)
With fake_pg, the tflops becomes
tflops // dp_mesh_size. The reason is thatallreduceis an no-op butdp_mesh_sizeis a constant value not affected by whether it is fake pg or real pg. This PR submit another row that contains all 1 so that when doing real or fakeallreduce, thetflopsis always accurate (fake pg tflops is pure computation).Why
Since #4819, the trainer logs the DP average of FLOPs as
dist_sum(flops) / dp_mesh.size(). On a fake process group the all-reduce is a no-op, so the "sum" is only the local value, but it is still divided by the DP size. Logged TFLOPS and MFU come out 1/DP of the right value. Real process groups are not affected.Example. DP8, each rank does 100 TFLOPS:
What changed
dist_utils.dist_mean(x, mesh): all-reduces[x, 1]and returnssum / count. On a real process group this equalsdist_sum(x) / mesh.size(). On a fake process group it returns the local value.Trainer.train_stepand the RL trainer use it for the DP average of FLOPs.Alternatives considered
Numerics / performance
Testing
test_trainer_accumulates_reused_accelerator_graph_losses[fake_dp=True]runstrain_stepon an 8-way fake DP mesh. On main it logs 75.0 instead of 600.0.Known issues / follow-ups