Skip to content

[metrics] Average FLOPs over DP by the number of summed values - #5879

Merged
fegin merged 1 commit into
mainfrom
fegin/stack/8
Nov 8, 2026
Merged

fegin merged 1 commit into
mainfrom
fegin/stack/8

Conversation

@fegin

@fegin fegin commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Stacked PRs:


Summary (human-written)

With fake_pg, the tflops becomes tflops // dp_mesh_size. The reason is that allreduce is an no-op but dp_mesh_size is 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 fake allreduce, the tflops is 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:

all-reduce sum divided by logged
real PG 800 8 (mesh size) 100
fake PG, main 100 8 (mesh size) 12.5 (8x too low)
fake PG, this PR 100 1 (summed count) 100

What changed

  • New dist_utils.dist_mean(x, mesh): all-reduces [x, 1] and returns sum / 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 average of FLOPs.

Alternatives considered

Numerics / performance

Testing

  • test_trainer_accumulates_reused_accelerator_graph_losses[fake_dp=True] runs train_step on an 8-way fake DP mesh. On main it logs 75.0 instead of 600.0.
  • CPU tests for the trainer, distributed utils, FLOPs estimators, and observability pass.

Known issues / follow-ups

  • The TorchFT trainer divides its FLOPs and loss sums by the mesh size in the same way. Not changed here.

@pytorch-bot pytorch-bot Bot added the ciflow/rl label Nov 6, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 6, 2026
## 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
@fegin

fegin commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

@claude review the PR

@claude

claude Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @fegin's task in 1m 24s —— View job


Approve -- please fill in the empty human-written summary before merging. See review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. 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 same dist_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 at dist_mean would 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_tokens is also only the local count there, so sum(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_dp parametrization rather than adding a new one, and it guards the regression (75.0 on main vs 600.0 here). In the test, get_optional_mesh returns the DP mesh for every name, including "pp". That doesn't matter here because no_registered_aux_losses makes collect_aux_loss_metrics return 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.

@fegin
fegin marked this pull request as ready for review November 6, 2026 02:04
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 6, 2026
@github-actions
github-actions Bot requested a review from pzhan9 November 6, 2026 02:04
Comment on lines +112 to +113
torch.stack((x, torch.ones_like(x))),
reduceOp=c10d.ReduceOp.SUM.name,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious why do this vs. use the AVG reduceOp?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use AVG reduceOp, it will still be an no-op. So we still get incorrect flops when using fake pg.

@fegin
fegin merged commit a24a35c into main Nov 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants