Skip to content

[PyTorch] Enable NVFP4 row-scaled (per-token) backward for GroupedLinear - #3324

Merged
timmoon10 merged 4 commits into
NVIDIA:mainfrom
cael-ling:pr/nvfp4-row-scaled-moe-backward
Aug 14, 2026
Merged

[PyTorch] Enable NVFP4 row-scaled (per-token) backward for GroupedLinear#3324
timmoon10 merged 4 commits into
NVIDIA:mainfrom
cael-ling:pr/nvfp4-row-scaled-moe-backward

Conversation

@cael-ling

Copy link
Copy Markdown
Contributor

Description

Extend the row-scaled NVFP4 support added for dense Linear (originally #2931 and #3206) to the MoE GroupedLinear module, so the wgrad is computed in NVFP4 instead of falling back to high precision.

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

Please list the changes introduced in this PR:

  • Change A
  • Change B

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Extend the row-scaled NVFP4 support added for dense Linear to the
MoE GroupedLinear module, so the wgrad is computed in NVFP4 instead of
falling back to high precision.

Signed-off-by: Cael Ling <caell@nvidia.com>
@github-actions github-actions Bot added the community-contribution PRs from external contributor outside the core maintainers, representing community-driven work. label Aug 7, 2026
@greptile-apps

greptile-apps Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR enables row-scaled NVFP4 weight-gradient computation for PyTorch GroupedLinear by routing affected grouped GEMMs through per-expert dense kernels and allocating columnwise per-column amax metadata.

  • Extends grouped GEMM dispatch to accept row-scaled NVFP4 operands in either position.
  • Enables row-scaled NVFP4 bulk allocation with columnwise usage.
  • Makes the portable columnwise amax reduction available across compiled architectures.
  • Adds bit-exact GroupedLinear-versus-Linear backward coverage.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
transformer_engine/pytorch/cpp_extensions/gemm.py Extends the existing row-scaled NVFP4 fallback loop from B-only operands to row-scaled operands in either GEMM position.
transformer_engine/pytorch/csrc/extensions/cast.cpp Enables columnwise bulk storage for row-scaled NVFP4 and attaches per-column amax buffers using their allocated shapes.
transformer_engine/common/cast/nvfp4/quantize_transpose_nvfp4.cuh Removes an unnecessary SM100 compile-time guard from the architecture-independent columnwise amax reduction.
tests/pytorch/test_grouped_linear.py Adds bit-exact forward, dgrad, and wgrad comparison against independent dense Linear experts for quantized row-scaled backward.

Sequence Diagram

sequenceDiagram
  participant GL as GroupedLinear backward
  participant GG as general_grouped_gemm
  participant DG as per-expert general_gemm
  participant Q as NVFP4 quantize/transpose
  participant W as Weight gradients
  GL->>GG: NT wgrad with expert splits
  GG->>DG: Route each row-scaled expert
  DG->>Q: Quantize activation and gradient operands
  Q-->>DG: FP4 data and row/column amax metadata
  DG-->>GG: Expert weight gradient
  GG-->>W: Combined expert gradients
Loading

Reviews (3): Last reviewed commit: "Remove unnecessary comment" | Re-trigger Greptile

Comment thread transformer_engine/common/cast/nvfp4/quantize_transpose_nvfp4.cuh Outdated
Comment thread transformer_engine/pytorch/csrc/extensions/cast.cpp Outdated
Co-authored-by: Tim Moon <4406448+timmoon10@users.noreply.github.com>
Signed-off-by: Tim Moon <4406448+timmoon10@users.noreply.github.com>
timmoon10
timmoon10 previously approved these changes Aug 11, 2026

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

LGTM, pending CI.

@timmoon10

Copy link
Copy Markdown
Member

/te-ci

@ptrendx ptrendx self-assigned this Aug 11, 2026
@cael-ling
cael-ling requested a review from timmoon10 August 14, 2026 01:13
Comment thread transformer_engine/pytorch/cpp_extensions/gemm.py Outdated
Signed-off-by: Tim Moon <4406448+timmoon10@users.noreply.github.com>
@timmoon10
timmoon10 merged commit 621da35 into NVIDIA:main Aug 14, 2026
10 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution PRs from external contributor outside the core maintainers, representing community-driven work.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants