Skip to content

fix(dist_reduce): run mesh all_reduce when DTensor mesh is orthogonal - #3204

Closed
saforem2 wants to merge 1 commit into
pytorch:mainfrom
saforem2:fix/dist-reduce-dtensor-skip
Closed

saforem2 wants to merge 1 commit into
pytorch:mainfrom
saforem2:fix/dist-reduce-dtensor-skip

Conversation

@saforem2

@saforem2 saforem2 commented May 4, 2026

Copy link
Copy Markdown
Contributor

Since #2963, _dist_reduce short-circuits all DTensor inputs with return float(x.full_tensor().item()), skipping the requested mesh all_reduce entirely. The justification in the comment ("avoid double-counting") only holds when the DTensor's own mesh covers the requested reduction mesh; when they are orthogonal — as in the trainer's loss reduction where the loss is a Replicated DTensor on the TP mesh and the requested mesh is loss_mesh = batch x cp — the cross-batch sum is silently dropped.

Symptom: every TP > 1 run reports loss = true_loss / dp_world_size. For a 2B run at TP=2 with 24 ranks, step-1 loss is 1.08 instead of the ~12.95 ln(vocab) baseline observed at TP=1. Gradients and optimizer steps are unaffected because backward goes through loss.backward() not _dist_reduce — only the printed/logged loss is wrong.

Fix: detect mesh axis overlap via mesh_dim_names. If the DTensor's mesh shares any axis with the requested mesh, full_tensor() already covers that axis and we skip the explicit all_reduce as before. If the meshes are disjoint, we materialize via full_tensor() and fall through to the plain-tensor path so the requested-mesh all_reduce runs.

Adds a unit test covering both the orthogonal case (was broken) and the same-mesh case (must not double-count).

Since pytorch#2963, `_dist_reduce` short-circuits all DTensor inputs with
`return float(x.full_tensor().item())`, skipping the requested mesh
all_reduce entirely. The justification in the comment ("avoid
double-counting") only holds when the DTensor's own mesh covers the
requested reduction mesh; when they are orthogonal — as in the
trainer's loss reduction where the loss is a Replicated DTensor on the
TP mesh and the requested mesh is `loss_mesh = batch x cp` — the
cross-batch sum is silently dropped.

Symptom: every TP > 1 run reports `loss = true_loss / dp_world_size`.
For a 2B run at TP=2 with 24 ranks, step-1 loss is 1.08 instead of the
~12.95 ln(vocab) baseline observed at TP=1. Gradients and optimizer
steps are unaffected because backward goes through `loss.backward()`
not `_dist_reduce` — only the printed/logged loss is wrong.

Fix: detect mesh axis overlap via `mesh_dim_names`. If the DTensor's
mesh shares any axis with the requested mesh, `full_tensor()` already
covers that axis and we skip the explicit all_reduce as before. If the
meshes are disjoint, we materialize via `full_tensor()` and fall
through to the plain-tensor path so the requested-mesh all_reduce
runs.

Adds a unit test covering both the orthogonal case (was broken) and
the same-mesh case (must not double-count).
Copilot AI review requested due to automatic review settings May 4, 2026 01:54
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label May 4, 2026
@pytorch-bot

pytorch-bot Bot commented May 4, 2026

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/8gpu

Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Fixes _dist_reduce handling of DTensor inputs so that reductions requested on an orthogonal mesh are no longer skipped (restoring correct cross-mesh loss/metric aggregation in TP>1 runs).

Changes:

  • Update _dist_reduce DTensor path to materialize via full_tensor() and only short-circuit when the DTensor mesh overlaps the requested reduction mesh.
  • Add _meshes_share_axes() helper to detect mesh-axis overlap via mesh_dim_names.
  • Add unit tests covering orthogonal-mesh reduction and same-mesh no-double-count behavior for DTensor inputs.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
torchtitan/distributed/utils.py Adjusts DTensor reduction logic to ensure requested mesh all-reduces run for orthogonal meshes; introduces mesh-overlap helper.
tests/unit_tests/test_dist_reduce_dtensor.py Adds regression tests for DTensor reductions on orthogonal meshes and for avoiding double-counting on same-mesh reductions.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +95 to +100
"""
a_names = set(a.mesh_dim_names or ())
b_names = set(b.mesh_dim_names or ())
return bool(a_names & b_names)


Comment on lines +72 to +75
"with extra_pg when the DTensor's mesh covers the "
"requested reduction mesh: ``full_tensor()`` already "
"reduces over that mesh and extra_pg is orthogonal. "
"Pass a plain tensor when using extra_pg."
saforem2 added a commit to saforem2/torchtitan that referenced this pull request May 4, 2026
Upstream `_dist_reduce` (torchtitan/distributed/utils.py) since
2026-04-27 (commit 1786292) short-circuits DTensor inputs by returning
`float(x.full_tensor().item())` and skipping the requested mesh
all_reduce. This is correct only when the DTensor's mesh covers the
reduction mesh. The trainer's loss reduction passes orthogonal meshes
(loss is a Replicated DTensor on the TP mesh; reduction is requested
across loss_mesh = batch x cp), so the cross-batch sum is silently
dropped — every TP > 1 run reports `loss = true_loss / dp_world_size`.

Workaround: convert loss DTensor to a plain tensor before the
dist_sum/dist_max calls in `train_step`, so the regular all_reduce
path runs. Verified on agpt_2b at TP=2 (step-1 loss 12.94, matches
the known TP=1 baseline of 12.95).

Filed upstream as pytorch#3204 with a proper fix that
detects mesh axis overlap. Once that lands, this workaround can be
removed.

Adds docs/guides/loss-reporting-tp-dist-reduce.md describing the
diagnosis, blast radius (production 80B dashboards under-reported by
dp_world_size), and the workaround, plus
docs/upstream-issues/dist_reduce_dtensor_skip.md with the upstream PR
reference.
saforem2 added a commit to saforem2/torchtitan that referenced this pull request May 4, 2026
TP > 1 loss-reporting bug diagnosis + workaround + upstream PR pytorch#3204,
agpt_50b_wide as a working compile + AC + TP=2 dense config (replaces
the OOM-crashing agpt_60b), and partial validation-loss wiring
through blendcorpus's already-built validation split.
saforem2 added a commit to saforem2/torchtitan that referenced this pull request May 4, 2026
First entry in a new docs/meeting-notes/ directory for pre-meeting
agendas, discussion summaries, and action-item tracking. Files are
named YYYY-MM-DD-<meeting>.md so dates are unambiguous and the
directory sorts chronologically.

Today's agenda surfaces the TP > 1 loss-reporting bug (production 80B
W&B is 1536x under-reporting; PR pytorch#3204 awaiting review), 2B/20B 512N
production status with the v1-vs-v2 lm-eval headline, the new
agpt_50b_wide bisect target showing the 80B compile+AC+TP=2 crash is
depth-sensitive (does not reproduce at 48 layers), and the open
validation-loss wiring waiting on consensus about cadence.
# plain-tensor path with the materialized full tensor.
x_mesh = x.device_mesh
x = x.full_tensor()
if mesh is None or _meshes_share_axes(x_mesh, mesh):

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.

  1. do we need to handle the mesh is None specially here?
  2. when _meshes_share_axes is True, it doesn't mean the result is correct? but you would just return. I think a workaround is to just error out when two meshes are not identical.

saforem2 added a commit to saforem2/torchtitan that referenced this pull request May 5, 2026
Surgical update + structural pass in one. The file was last edited
2026-04-27 and missed every major thread from the last week:

- bf16-master RMSNorm-freeze fix (2026-04-29) and the v1→v2 production
  restart, with the +19.8pp ARC-Easy / +15.4pp HellaSwag smoking-gun
  validation at 503B tokens.
- TP > 1 loss-reporting bug filed as pytorch#3204, local
  workaround in trainer.py + new EzpzValidator subclass.
- agpt_50b_wide as a smaller bisect target proving the 80B
  compile+AC+TP=2 crash is depth-sensitive (does NOT reproduce at
  48 layers, only at 84).
- HSDP `aten.normal_.default` failure on `dp_replicate × dp_shard > 1`.
- Recurring Aurora `signal 9` NODE_FAIL pattern across 3 jobs.
- EzpzValidator + blendcorpus validation split wired in (default off).

Reorganization:

- Move the rule-shaped lines (never kill jobs, never discard local
  changes, terse responses) from "User Preferences" up to "Golden
  Rules" so they get the priority they deserve.
- New "Recent Findings (2026-04-29 → 2026-05-04)" section between
  Competitions and Common Pitfalls. Each finding links to its
  canonical writeup under docs/.
- Refresh "Production Training Status" with v2 numbers (2B 512N
  step 5,073 / 510B tokens, 20B 512N step ~862 / loss 3.47, 20B
  256N NODE_FAIL at step 364) and drop the stale v1 entries (still
  preserved per-trajectory under docs/production/).
- Refresh "Known Bugs / Open Issues" — add TP loss-reporting,
  HSDP init crash, depth-sensitive 80B crash, Aurora NODE_FAIL.
  Existing bugs that are still active stay; outdated ones removed.
- Refresh "Common Pitfalls" with HSDP and TP-loss callouts and the
  --dataloader.dataset-path silent-override warning.
- Update "Key Paths" with the docs/ subdirs added since the last
  edit (summaries/, meeting-notes/, evals/, experiments/,
  upstream-issues/) and the new top-level files (validator.py,
  blendcorpus/).
- Tighten "User Preferences" down to the truly preference-only items
  (formatting, table style, doc-cross-linking) since the rules moved
  up.

7 cross-links to docs/ verified to resolve.

@fegin fegin 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.

I prefer using to_local() for now to fix the issue: #3159.

Comment on lines +66 to +68
x_mesh = x.device_mesh
x = x.full_tensor()
if mesh is None or _meshes_share_axes(x_mesh, mesh):

@fegin fegin May 7, 2026 •

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.

I want to argue that the only clean workaround is to_local(). Then we don't need to infer whether it is full dtensor or non-full dtensor + TP. The actual fix is to deprecate the non-full dtensor case. Then we will unconditionally apply full_tensor() here. But given that it is hard to do deprecation without full dtensor being ready, we should just make it a plain tensor, which was what we always did for the TP case before this April -- we changed the TP-enabled model to always use DTensor for activation and loss this April.

I fixed this issue in #3159. If you want to land with this PR, I prefer landing to_local().

cc., @tianyu-l

@saforem2

Copy link
Copy Markdown
Contributor Author

Closing as superseded.

The orthogonal-mesh under-reporting was fixed by @fegin in #3159
(commit d64eabcce, merged 2026-05-18) — switching the DTensor
branch of _dist_reduce from full_tensor() to to_local() + the
explicit mesh all_reduce. From that PR's issue list:

Our _dist_reduce under-reports loss for non full tensor:
full_tensor() reduces only over the DTensor's own mesh (TP-only
in legacy), skipping DP. Loss was under-reported by
dp_shard × cp. Switched to to_local() then mesh all_reduce
over loss_mesh.

That's the same case the #3204 tests covered (Replicate-on-TP loss
reduced across an orthogonal batch mesh). Verified on current
upstream/main: _dist_reduce now does x = x.to_local() for the
DTensor path, then runs the requested funcol.all_reduce —
no double-count, no skipped reduction.

@fegin / @tianyu-l — thanks for the review and pointer to #3159.
The to_local() shape is cleaner than the full_tensor() +
mesh-overlap helper I had here, agreed.

@saforem2 saforem2 closed this Jun 12, 2026
saforem2 added a commit to saforem2/torchtitan that referenced this pull request Jun 12, 2026
…rker

Reflects that upstream PR pytorch#3159 (commit d64eabc, 2026-05-18) landed
the `to_local()` fix and PR pytorch#3204 was closed as superseded.

- guides/loss-reporting-tp-dist-reduce.md: rewritten as resolved doc
  with the historical window, the symptom table, the as-shipped fix
  shape, and the affected-window-only advice for 80B W&B traces.
- upstream-issues/dist_reduce_dtensor_skip.md: status header flipped
  to resolved with the upstream commit/PR link and the disposition
  of PR pytorch#3204.
- docs/README.md: status column updated for both linked docs.
- .claude/CLAUDE.md: Recent Findings entry rewritten as past-tense
  with the fix commit; Known Bugs entry removed entirely.
- summaries/2026-06-05_to_2026-06-12.md: bump four→five upstream syncs,
  add 54th-sync row to the syncs table, and replace bullet-list-marker
  "+ cherry-picks" with "and cherry-picks" to fix a markdown reflow
  that broke the opening paragraph.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu 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.

4 participants