Skip to content

Fuse MXFP8 WGRAD accumulation across microbatches - #4911

Merged
anijain2305 merged 9 commits into
mainfrom
gh/anijain2305/51/head
Sep 28, 2026
Merged

anijain2305 merged 9 commits into
mainfrom
gh/anijain2305/51/head

Conversation

@anijain2305

@anijain2305 anijain2305 commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

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:

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-poisoned]
[ghstack-poisoned]
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 27, 2026
[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
[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.

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
[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.

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
[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.

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
[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.

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
[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.

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
# 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(

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.

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

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.

thanks for temporary decoupling. grad_dtype/reduce_dtype PR is ready for review: pytorch/pytorch#194434

[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.

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
anijain2305 marked this pull request as ready for review September 27, 2026 15:30
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Sep 27, 2026
@anijain2305 anijain2305 added ciflow/b200 Trigger B200 CI ciflow/fake-pg Run 1-GPU Fake PG integration tests and removed ciflow/fake-pg Run 1-GPU Fake PG integration tests labels Sep 27, 2026
@anijain2305
anijain2305 changed the base branch from gh/anijain2305/51/base to main September 28, 2026 04:16
@anijain2305
anijain2305 merged commit d060087 into main Sep 28, 2026
17 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/b200 Trigger B200 CI ciflow/fake-pg Run 1-GPU Fake PG integration tests 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.

3 participants