Skip to content

In-place loss accumulation - #4146

Merged
xmfan merged 1 commit into
mainfrom
xmfan/stack/42
Aug 14, 2026
Merged

xmfan merged 1 commit into
mainfrom
xmfan/stack/42

Conversation

@xmfan

@xmfan xmfan commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Stacked PRs:


CUDA graph outputs alias static storage that is overwritten by each replay. Previously, wrap_with_cuda_graph cloned the returned loss after every forward-backward call even though Trainer only consumes loss values on logging steps.

Move loss ownership into Trainer. Return the graph-owned loss directly, clone the first loss once on logging steps, and accumulate subsequent losses in place. Non-logging steps no longer preserve or aggregate the returned loss. Apply the same behavior to TorchFT and remove the stale FSDP always-reshard TODO.

Backward still executes inside the forward-backward function. This only changes aggregation of detached loss values used for metrics.

Test Plan:

  • pytest -q tests/unit_tests/test_trainer.py torchtitan/experiments/torchft/tests (8 passed)
  • pyrefly check torchtitan/distributed/cudagraph.py torchtitan/trainer.py tests/unit_tests/test_trainer.py (0 errors)

CUDA graph outputs alias static storage that is overwritten by each replay. Previously, wrap_with_cuda_graph cloned the returned loss after every forward-backward call even though Trainer only consumes loss values on logging steps.

Move loss ownership into Trainer. Return the graph-owned loss directly, clone the first loss once on logging steps, and accumulate subsequent losses in place. Non-logging steps no longer preserve or aggregate the returned loss. Apply the same behavior to TorchFT and remove the stale FSDP always-reshard TODO.

Backward still executes inside the forward-backward function. This only changes aggregation of detached loss values used for metrics.

Test Plan:
- pytest -q tests/unit_tests/test_trainer.py torchtitan/experiments/torchft/tests (8 passed)
- pyrefly check torchtitan/distributed/cudagraph.py torchtitan/trainer.py tests/unit_tests/test_trainer.py (0 errors)

stack-info: PR: #4146, branch: xmfan/stack/42
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 14, 2026
@xmfan xmfan changed the title Avoid CUDA graph loss clones on non-logging steps In-place loss accumulation Aug 14, 2026

@tianyu-l tianyu-l 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.

nice

@xmfan
xmfan merged commit 2807d3f into main Aug 14, 2026
33 of 35 checks passed
saforem2 added a commit to saforem2/torchtitan that referenced this pull request Aug 16, 2026
Job 12473170 failed 3/3 arms (rc=143, ZERO steps) after the merge:

    AttributeError: 'FaultTolerantTrainer' object has no attribute 'fwd_bwd_fn'

pytorch#3559 (CUDA-graph capture) + pytorch#4146 (in-place loss accumulation) added a
`fwd_bwd_fn` indirection to Trainer.__init__, and forward_backward_step now
dispatches through it. FaultTolerantTrainer deliberately does not call
super().__init__(), so the new assignment never ran.

Binds the plain body, NOT the CUDA-graph wrapper: wrap_with_cuda_graph is a
CUDA capture path, disable_cuda_graphs defaults True, and no XPU run has
exercised it. Behaviour is therefore identical to pre-merge.

This is the second time an upstream attribute has gone missing this way
(num_pipeline_parallel_microbatches was the first), and both times the merge
was CONFLICT-FREE and touched no ezpz file -- so nothing flagged it and it
surfaced only as a dead run. Adds tests/test_trainer_init_parity.py, a
static AST diff of the attributes each __init__ sets. Pure stdlib, runs
without torch, and mutation-tested: removing the fwd_bwd_fn line makes it
exit 1 naming self.fwd_bwd_fn; restoring it exits 0.

Run it on every upstream sync.
saforem2 added a commit to saforem2/torchtitan that referenced this pull request Aug 16, 2026
… clean numerics

Conflict-free merge that touched NO ezpz file and still broke the branch
twice, both times via symbols in files the merge never opened:

  pytorch#4045       deleted maybe_enable_async_tp  -> ImportError in agpt + moe
  pytorch#3559/pytorch#4146 added Trainer.fwd_bwd_fn       -> AttributeError, 3/3 arms dead

Both replayed and verified on the RC: compile TP=4 and MoE EP=1 both pass
10 steps.

Also records that the loss A/B FIRST reported "the merge CHANGED numerics"
and that this was MY error, not upstream's -- I ran it without a control.
Re-running the same pre-merge commit twice reproduces the divergence
exactly (0.00106 / 0.00260 / 0.01201 at steps 8/9/10, matching pre-vs-post
to three decimals), so the merge is numerically clean.

The control exposed a standing problem worth more attention than the sync:
--debug.deterministic does NOT produce bit-identical runs on this stack,
contrary to the contract in CLAUDE.md. Every "identical loss" check we run
is currently blind to regressions below ~0.012 at step 10. Untriaged, with
the 1-rank-vs-multi-rank / TP split noted as the way to separate a
nondeterministic kernel from a nondeterministic collective.
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.

2 participants