Repository navigation
In-place loss accumulation - #4146
Merged
Merged
Conversation
xmfan
requested review from
fegin,
tianyu-l,
wconstab and
wwwjn
as code owners
August 14, 2026 00:43
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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: