Repository navigation
Fuse MXFP8 WGRAD accumulation across microbatches - #4911
Merged
Merged
Conversation
[ghstack-poisoned]
anijain2305
added a commit
that referenced
this pull request
Sep 27, 2026
Pipeline parallelism runs backward once per microbatch, and each WGRAD is added into the running param.grad by a separate AccumulateGrad kernel. Fold every contribution after the first into that running gradient with F.scaled_addmm_, so the accumulation rides the GEMM epilogue. The first WGRAD is produced in the parameter's grad_dtype (falling back to param.dtype). Later ones accumulate in place into param.grad, clear it, and return the same buffer so AccumulateGrad reattaches it instead of adding it to itself. Today this takes effect with a BF16 reduce dtype. With an FP32 reduce dtype FSDP moves each gradient into its own accumulator, so param.grad stays None and the ordinary WGRAD runs; once FSDP sets the unsharded parameter's grad_dtype to the reduce dtype, the same path accumulates in FP32. Stacked weights reach their parameter through a flatten view whose backward casts to BF16, so they only fuse when grad_dtype is BF16. Traced execution (Dynamo, make_fx) uses the ordinary WGRAD because a graph cannot represent the read-and-clear of param.grad. ghstack-source-id: b212210 Pull-Request: #4911
anijain2305
added a commit
that referenced
this pull request
Sep 27, 2026
Pipeline parallelism runs backward once per microbatch, and each WGRAD is added into the running param.grad by a separate AccumulateGrad kernel. Fold every contribution after the first into that running gradient with F.scaled_addmm_, so the accumulation rides the GEMM epilogue. The first WGRAD is produced in the parameter's grad_dtype (falling back to param.dtype). Later ones accumulate in place into param.grad, clear it, and return the same buffer so AccumulateGrad reattaches it instead of adding it to itself. Today this takes effect with a BF16 reduce dtype. With an FP32 reduce dtype FSDP moves each gradient into its own accumulator, so param.grad stays None and the ordinary WGRAD runs; once FSDP sets the unsharded parameter's grad_dtype to the reduce dtype, the same path accumulates in FP32. The autograd function takes the parameter itself rather than its flattened [N, K] view, since a view's backward casts the gradient to the view's dtype and ignores grad_dtype. This lets stacked weights ([L, N, K]) fuse for any grad_dtype. Traced execution (Dynamo, make_fx) uses the ordinary WGRAD because a graph cannot represent the read-and-clear of param.grad. ghstack-source-id: d3eed45 Pull-Request: #4911
anijain2305
added a commit
that referenced
this pull request
Sep 27, 2026
Pipeline parallelism runs backward once per microbatch, and each WGRAD is added into the running param.grad by a separate AccumulateGrad kernel. Fold every contribution after the first into that running gradient with F.scaled_addmm_, so the accumulation rides the GEMM epilogue. The first WGRAD is produced in the parameter's grad_dtype (falling back to param.dtype). Later ones accumulate in place into param.grad, clear it, and return the same buffer so AccumulateGrad reattaches it instead of adding it to itself. Today this takes effect with a BF16 reduce dtype. With an FP32 reduce dtype FSDP moves each gradient into its own accumulator, so param.grad stays None and the ordinary WGRAD runs; once FSDP sets the unsharded parameter's grad_dtype to the reduce dtype, the same path accumulates in FP32. The autograd function takes the parameter itself rather than its flattened [N, K] view, since a view's backward casts the gradient to the view's dtype and ignores grad_dtype. This lets stacked weights ([L, N, K]) fuse for any grad_dtype. Traced execution (Dynamo, make_fx) uses the ordinary WGRAD because a graph cannot represent the read-and-clear of param.grad. ghstack-source-id: 4cefa23 Pull-Request: #4911
anijain2305
added a commit
that referenced
this pull request
Sep 27, 2026
Pipeline parallelism runs backward once per microbatch, and each WGRAD is added into the running param.grad by a separate AccumulateGrad kernel. Fold every contribution after the first into that running gradient with F.scaled_addmm_, so the accumulation rides the GEMM epilogue. The first WGRAD is produced in the parameter's grad_dtype (falling back to param.dtype). Later ones accumulate in place into param.grad, clear it, and return the same buffer so AccumulateGrad reattaches it instead of adding it to itself. Today this takes effect with a BF16 reduce dtype. With an FP32 reduce dtype FSDP moves each gradient into its own accumulator, so param.grad stays None and the ordinary WGRAD runs; once FSDP sets the unsharded parameter's grad_dtype to the reduce dtype, the same path accumulates in FP32. The autograd function takes the parameter itself rather than its flattened [N, K] view, since a view's backward casts the gradient to the view's dtype and ignores grad_dtype. This lets stacked weights ([L, N, K]) fuse for any grad_dtype. Traced execution (Dynamo, make_fx) uses the ordinary WGRAD because a graph cannot represent the read-and-clear of param.grad. ghstack-source-id: 4cefa23 Pull-Request: #4911
anijain2305
added a commit
that referenced
this pull request
Sep 27, 2026
Pipeline parallelism runs backward once per microbatch, and each WGRAD is added into the running param.grad by a separate AccumulateGrad kernel. Fold every contribution after the first into that running gradient with F.scaled_addmm_, so the accumulation rides the GEMM epilogue. The first WGRAD is produced in the parameter's grad_dtype (falling back to param.dtype). Later ones accumulate in place into param.grad, clear it, and return the same buffer so AccumulateGrad reattaches it instead of adding it to itself. Today this takes effect with a BF16 reduce dtype. With an FP32 reduce dtype FSDP moves each gradient into its own accumulator, so param.grad stays None and the ordinary WGRAD runs; once FSDP sets the unsharded parameter's grad_dtype to the reduce dtype, the same path accumulates in FP32. The autograd function takes the parameter itself rather than its flattened [N, K] view, since a view's backward casts the gradient to the view's dtype and ignores grad_dtype. This lets stacked weights ([L, N, K]) fuse for any grad_dtype. Traced execution (Dynamo, make_fx) uses the ordinary WGRAD because a graph cannot represent the read-and-clear of param.grad. ghstack-source-id: 068990a Pull-Request: #4911
anijain2305
added a commit
that referenced
this pull request
Sep 27, 2026
Pipeline parallelism runs backward once per microbatch, and each WGRAD is added into the running param.grad by a separate AccumulateGrad kernel. Fold every contribution after the first into that running gradient with F.scaled_addmm_, so the accumulation rides the GEMM epilogue. The first WGRAD is produced in the parameter's grad_dtype (falling back to param.dtype). Later ones accumulate in place into param.grad, clear it, and return the same buffer so AccumulateGrad reattaches it instead of adding it to itself. Today this takes effect with a BF16 reduce dtype. With an FP32 reduce dtype FSDP moves each gradient into its own accumulator, so param.grad stays None and the ordinary WGRAD runs; once FSDP sets the unsharded parameter's grad_dtype to the reduce dtype, the same path accumulates in FP32. The autograd function takes the parameter itself rather than its flattened [N, K] view, since a view's backward casts the gradient to the view's dtype and ignores grad_dtype. This lets stacked weights ([L, N, K]) fuse for any grad_dtype. Traced execution (Dynamo, make_fx) uses the ordinary WGRAD because a graph cannot represent the read-and-clear of param.grad. ghstack-source-id: 2b10bdf Pull-Request: #4911
anijain2305
added a commit
that referenced
this pull request
Sep 27, 2026
Pipeline parallelism runs backward once per microbatch, and each WGRAD is added into the running param.grad by a separate AccumulateGrad kernel. Fold every contribution after the first into that running gradient with F.scaled_addmm_, so the accumulation rides the GEMM epilogue. The first WGRAD is produced in the parameter's grad_dtype (falling back to param.dtype). Later ones accumulate in place into param.grad, clear it, and return the same buffer so AccumulateGrad reattaches it instead of adding it to itself. Today this takes effect with a BF16 reduce dtype. With an FP32 reduce dtype FSDP moves each gradient into its own accumulator, so param.grad stays None and the ordinary WGRAD runs; once FSDP sets the unsharded parameter's grad_dtype to the reduce dtype, the same path accumulates in FP32. The autograd function takes the parameter itself rather than its flattened [N, K] view, since a view's backward casts the gradient to the view's dtype and ignores grad_dtype. This lets stacked weights ([L, N, K]) fuse for any grad_dtype. Traced execution (Dynamo, make_fx) uses the ordinary WGRAD because a graph cannot represent the read-and-clear of param.grad. ghstack-source-id: 6b27ae4 Pull-Request: #4911
anijain2305
commented
Sep 27, 2026
| # https://github.com/pytorch/pytorch/pull/194434 makes FSDP set | ||
| # grad_dtype to reduce_dtype, removing that separate accumulation. | ||
| # Remove the xfail once it lands. | ||
| pytest.param( |
Contributor
Author
There was a problem hiding this comment.
@weifengpy I am opening this PR for review with an xfail for reduce_dtype fp32.
I think we can land this PR and wait for yours to merge and then remove the xfail. This will unblock the megatron comparison run.
Contributor
There was a problem hiding this comment.
thanks for temporary decoupling. grad_dtype/reduce_dtype PR is ready for review: pytorch/pytorch#194434
anijain2305
added a commit
that referenced
this pull request
Sep 27, 2026
Pipeline parallelism runs backward once per microbatch, and each WGRAD is added into the running param.grad by a separate AccumulateGrad kernel. Fold every contribution after the first into that running gradient with F.scaled_addmm_, so the accumulation rides the GEMM epilogue. The first WGRAD is produced in the parameter's grad_dtype (falling back to param.dtype). Later ones accumulate in place into param.grad, clear it, and return the same buffer so AccumulateGrad reattaches it instead of adding it to itself. Today this takes effect with a BF16 reduce dtype. With an FP32 reduce dtype FSDP moves each gradient into its own accumulator, so param.grad stays None and the ordinary WGRAD runs; once FSDP sets the unsharded parameter's grad_dtype to the reduce dtype, the same path accumulates in FP32. The autograd function takes the parameter itself rather than its flattened [N, K] view, since a view's backward casts the gradient to the view's dtype and ignores grad_dtype. This lets stacked weights ([L, N, K]) fuse for any grad_dtype. Traced execution (Dynamo, make_fx) uses the ordinary WGRAD because a graph cannot represent the read-and-clear of param.grad. ghstack-source-id: abc3cd9 Pull-Request: #4911
anijain2305
added a commit
that referenced
this pull request
Sep 27, 2026
For every microbatch after the first, the backward uses scaled_addmm_ to fuse the WGRAD accumulation into the GEMM itself. There are two cases: * reduce_dtype=bf16, param_dtype=bf16: this PR adds the fusion. * reduce_dtype=fp32, param_dtype=bf16: the param's grad_dtype is still bf16, so FSDP takes over the accumulation between microbatches and the fusion does not happen. An xfail test covers this, and pytorch/pytorch#194434 will fix it. This PR is still worth reviewing and possibly landing if the PyTorch FSDP PR takes longer, because it unblocks the Megatron comparison run. Neither torch.compile nor GraphTrainer supports backward mutations like param.grad = None, so the fusion is skipped for both. ghstack-source-id: abc3cd9 Pull-Request: #4911
anijain2305
marked this pull request as ready for review
September 27, 2026 15:30
anijain2305
requested review from
fegin,
tianyu-l,
wconstab and
wwwjn
as code owners
September 27, 2026 15:30
tianyu-l
approved these changes
Sep 27, 2026
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.
Stack from ghstack (oldest at bottom):
For every microbatch after the first, the backward uses scaled_addmm_ to fuse
the WGRAD accumulation into the GEMM itself.
There are two cases:
so FSDP takes over the accumulation between microbatches and the fusion does
not happen. An xfail test covers this, and
[FSDP2] Separate sharded and unsharded gradient dtypes pytorch#194434 will fix it.
This PR is still worth reviewing and possibly landing if the PyTorch FSDP PR
takes longer, because it unblocks the Megatron comparison run.
Neither torch.compile nor GraphTrainer supports backward mutations like
param.grad = None, so the fusion is skipped for both.