Skip to content

feat(fsdp): use TE general_gemm for mixed-precision wgrad in FSDP path - #3822

Merged
Victarry merged 3 commits into
NVIDIA:mainfrom
Victarry:denliu/fsdp-te-general-gemm
Apr 2, 2026
Merged

Victarry merged 3 commits into
NVIDIA:mainfrom
Victarry:denliu/fsdp-te-general-gemm

Conversation

@Victarry

@Victarry Victarry commented Mar 12, 2026 •

Copy link
Copy Markdown

Summary

  • Replace torch.matmul with TE general_gemm in the FSDP gradient accumulation fusion path to support mixed-precision output (e.g. bf16 input → fp32 main_grad buffer)
  • torch.matmul requires the out tensor dtype to match the compute dtype, which forces FSDP to use grad_reduce_in_bf16 when gradient_accumulation_fusion is enabled
  • TE's general_gemm supports arbitrary out_dtype via cuBLAS, removing this hard dependency
  • Uses inline lazy import to avoid circular dependency with megatron.core.extensions.transformer_engine

Test plan

  • Verified on DeepSeek-V3-proxy (MLA+MoE+MTP) with FSDP (fsdp_double_buffer=True, gradient_accumulation_fusion=True, fp32 grad buffer, TP1/PP1/EP4/DP2, 8×H100)
  • Training converges normally (~103 TFLOP/s/GPU, lm_loss decreases from 10.35 to 0.30)

🤖 Generated with Claude Code

@Victarry
Victarry requested review from a team as code owners March 12, 2026 07:27
@copy-pr-bot

copy-pr-bot Bot commented Mar 12, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@svcnvidia-nemo-ci
svcnvidia-nemo-ci marked this pull request as draft March 12, 2026 07:27
@github-actions

Copy link
Copy Markdown
Contributor

This PR has been automatically converted to draft because all PRs must start as drafts.

When you are ready for review, click Ready for Review to begin the review process. This will:

  1. Add the oncall reviewer (optional reviewer)
  2. Add required review teams based on your changes

See the contribution guide for more details.

@Victarry
Victarry marked this pull request as ready for review March 12, 2026 08:01
@svcnvidia-nemo-ci
svcnvidia-nemo-ci requested a review from a team March 12, 2026 08:01
@Victarry
Victarry requested a review from shjwudp March 12, 2026 08:01
@svcnvidia-nemo-ci svcnvidia-nemo-ci added Final Review PR is in the "final review" stage complexity: low labels Mar 12, 2026
@shjwudp

shjwudp commented Mar 12, 2026

Copy link
Copy Markdown
Contributor

/ok to test eb769df

@svcnvidia-nemo-ci svcnvidia-nemo-ci added this to the Core 0.16 milestone Mar 12, 2026
Replace torch.matmul with TE general_gemm in the FSDP gradient
accumulation fusion path to support mixed-precision output (e.g.
bf16 input -> fp32 main_grad buffer).

torch.matmul requires the `out` tensor dtype to match the compute
dtype, which forces FSDP to use grad_reduce_in_bf16 when
gradient_accumulation_fusion is enabled. TE's general_gemm supports
arbitrary out_dtype via cuBLAS, removing this hard dependency.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@Victarry
Victarry force-pushed the denliu/fsdp-te-general-gemm branch from eb769df to f2235d6 Compare March 12, 2026 08:41
@Victarry

Copy link
Copy Markdown
Author

/ok to test f2235d6

@shjwudp

shjwudp commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

/ok to test b28bae0

@shjwudp
shjwudp requested review from a team and cspades March 24, 2026 15:21

@cspades cspades left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yup, this will mean that users can use (megatron_fsdp_)main_grads_dtype=torch.float32 and (megatron_fsdp_)grad_comm_dtype=torch.float32 even with FP8 params / BF16 gradients, thanks to out_dtype, thanks @Victarry !

@cspades

cspades commented Mar 25, 2026

Copy link
Copy Markdown
Member

Rebasing on main to hopefully get rid of the CodeCov failure.

@shjwudp

shjwudp commented Mar 25, 2026

Copy link
Copy Markdown
Contributor

/ok to test 02a87e2

@svcnvidia-nemo-ci svcnvidia-nemo-ci added Approved All necessary approvals have been made and removed Final Review PR is in the "final review" stage labels Apr 1, 2026
@Victarry
Victarry added this pull request to the merge queue Apr 1, 2026
@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/23875974307

@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/23876360777

@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/23877162308

Merged via the queue into NVIDIA:main with commit 8d7a3f8 Apr 2, 2026
66 of 70 checks passed
@Victarry
Victarry deleted the denliu/fsdp-te-general-gemm branch April 2, 2026 04:42
yangbofun pushed a commit to xlm-research/Megatron-LM that referenced this pull request May 22, 2026
NVIDIA#3822)

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Jianbin Chang <shjwudp@gmail.com>
Co-authored-by: Cory Ye <44509866+cspades@users.noreply.github.com>
yhgalaxy pushed a commit to yhgalaxy/Megatron-LM that referenced this pull request Jun 17, 2026
NVIDIA#3822)

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Jianbin Chang <shjwudp@gmail.com>
Co-authored-by: Cory Ye <44509866+cspades@users.noreply.github.com>
Signed-off-by: yhgalaxy <yhgalaxy@outlook.com>
jon-barker pushed a commit to jon-barker/Megatron-LM that referenced this pull request Jul 10, 2026
NVIDIA#3822)

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Jianbin Chang <shjwudp@gmail.com>
Co-authored-by: Cory Ye <44509866+cspades@users.noreply.github.com>
Signed-off-by: Jon Barker <jbarker@aws-cmh-slurm-1-vscode-02.cm.cluster>
terminator123 pushed a commit to 021ai/Megatron-LM that referenced this pull request Aug 3, 2026
NVIDIA#3822)

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Jianbin Chang <shjwudp@gmail.com>
Co-authored-by: Cory Ye <44509866+cspades@users.noreply.github.com>

This branch was previously deployed

1 inactive deployment
test — 02a87e2d Deployed Mar 25, 2026 by copy-pr-bot[bot] via cicd-wait-in-queue #10458
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Approved All necessary approvals have been made complexity: low

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants