Skip to content

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

Open
cael-ling wants to merge 2 commits into
NVIDIA:mainfrom
cael-ling:pr/nvfp4-row-scaled-moe-backward
Open

[PyTorch] Enable NVFP4 row-scaled (per-token) backward for GroupedLinear#3324
cael-ling wants to merge 2 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

This PR enables row-scaled NVFP4 backward computation for PyTorch GroupedLinear, replacing the previous unsupported path with per-expert dense GEMM execution.

  • Routes row-scaled NVFP4 grouped GEMMs through existing dense general_gemm kernels.
  • Adds columnwise amax allocation and metadata support for row-scaled NVFP4 transposes.
  • Allows the general columnwise amax reduction kernel to run below SM 10.0.
  • Adds bit-exact backward comparisons between GroupedLinear and equivalent independent Linear layers.

Confidence Score: 5/5

The PR appears safe to merge with no concrete blocking or non-blocking defects identified in the reviewed changes.

The grouped path consistently delegates row-scaled operands to existing dense kernels, while the cast allocation changes provide the corresponding per-column amax shape and the tests compare all resulting outputs and gradients bit-for-bit.

Important Files Changed

Filename Overview
transformer_engine/pytorch/cpp_extensions/gemm.py Extends the existing per-expert dense GEMM fallback to row-scaled NVFP4 tensors in either grouped operand.
transformer_engine/pytorch/csrc/extensions/cast.cpp Enables columnwise storage for row-scaled NVFP4 and allocates per-column amax metadata using the actual tensor shape.
transformer_engine/common/cast/nvfp4/quantize_transpose_nvfp4.cuh Removes the SM 10.0 restriction from the architecture-independent columnwise amax reduction kernel.
tests/pytorch/test_grouped_linear.py Adds bit-exact forward, dgrad, and wgrad coverage against equivalent independent dense Linear layers.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
    A[GroupedLinear backward] --> B[Row-scaled NVFP4 activation]
    B --> C{Grouped GEMM wrapper}
    C --> D[Per-expert general_gemm loop]
    D --> E[Columnwise amax and transpose]
    E --> F[NVFP4 weight-gradient GEMM]
    F --> G[Per-expert weight gradients]
Loading

Reviews (1): Last reviewed commit: "[pre-commit.ci] auto fixes from pre-comm..." | Re-trigger Greptile

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.

1 participant