Skip to content

[PyTorch] Multi-tensor NVFP4 2D master-weight cast - #3869

Open
sraman-rgb wants to merge 8 commits into
NVIDIA:mainfrom
sraman-rgb:sraman/nvfp4-partial-cast-fused
Open

sraman-rgb wants to merge 8 commits into
NVIDIA:mainfrom
sraman-rgb:sraman/nvfp4-partial-cast-fused

Conversation

@sraman-rgb

Copy link
Copy Markdown
Collaborator

Add NVTE_NVFP4_FUSED_MASTER_WEIGHT_CAST (default 0). With it set, quantize_master_weights casts the master shards of a distributed optimizer to 2D NVFP4 model weights with four new multi-tensor kernels (common/recipe/nvfp4_multi_tensor.cu, C API in recipe.h, pybind nvfp4_2d_multi_tensor_*):

  • tile amaxes read from the FP32 master shards directly; values are rounded to the model dtype (BF16) in registers instead of materializing master.to(bf16);
  • per-tensor global amaxes taken as the maximum of the all-reduced tile amaxes, which replaces the per-tensor compute_amax kernels and the second all-reduce;
  • fused scale and partial cast for all tensors, one thread per 16x16 tile with vector loads and 8-byte stores of whole tiles, using the same mul_cvt_fp32_to_fp4_4x conversion as nvfp4_2d_partial_cast.

Each kernel takes up to 24 tensors per launch with the metadata passed as kernel parameters, so the calls are CUDA-graph capturable. The FP4 data, scale-inverses and amaxes are bit-identical to the default path; FSDP sharded weights, mixed dtypes and other unsupported layouts fall back to it.

On one VR200, 32 x (4096x7168 | 7168x2048) BF16 NVFP4 weights with FP32 masters: 13.76 ms -> 0.59 ms of kernel time per call.

Description

Please include a brief summary of the changes, relevant motivation and context.

Fixes # (issue)

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

Add NVTE_NVFP4_FUSED_MASTER_WEIGHT_CAST (default 0). With it set,
quantize_master_weights casts the master shards of a distributed
optimizer to 2D NVFP4 model weights with four new multi-tensor kernels
(common/recipe/nvfp4_multi_tensor.cu, C API in recipe.h, pybind
nvfp4_2d_multi_tensor_*):

- tile amaxes read from the FP32 master shards directly; values are
  rounded to the model dtype (BF16) in registers instead of
  materializing master.to(bf16);
- per-tensor global amaxes taken as the maximum of the all-reduced tile
  amaxes, which replaces the per-tensor compute_amax kernels and the
  second all-reduce;
- fused scale and partial cast for all tensors, one thread per 16x16
  tile with vector loads and 8-byte stores of whole tiles, using the
  same mul_cvt_fp32_to_fp4_4x conversion as nvfp4_2d_partial_cast.

Each kernel takes up to 24 tensors per launch with the metadata passed as
kernel parameters, so the calls are CUDA-graph capturable. The FP4 data,
scale-inverses and amaxes are bit-identical to the default path; FSDP
sharded weights, mixed dtypes and other unsupported layouts fall back to
it.

On one VR200, 32 x (4096x7168 | 7168x2048) BF16 NVFP4 weights with FP32
masters: 13.76 ms -> 0.59 ms of kernel time per call.

Signed-off-by: Siddhartha Raman S <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Siddhartha Raman S <[email protected]>
@greptile-apps

greptile-apps Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5RetriggerView in Greptile

[Medium impact] The PR appears safe to merge; no actionable defects were found.

Summary

The PR replaces per-tensor NVFP4 master-weight casts with multi-tensor CUDA kernels.

  • NVFP4 master-weight casts process many shards in fewer kernel launches.
  • Distributed NVFP4 casts write each rank’s shard into the matching model-weight storage.

The earlier fallback disagreement is addressed: every rank now follows the same collective path. No new actionable issues were found.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Master-weight shards] --> B[Multi-tensor tile amax]
  B --> C[One MAX all-reduce]
  C --> D[Global amax per tensor]
  D --> E[Global and tile scales]
  E --> F[Multi-tensor partial FP4 cast]
Loading

Reviews (4) · Last reviewed commit: "Merge branch 'main' into sraman/nvfp4-pa..." · Reviewed by Greptile

pre-commit-ci Bot and others added 2 commits November 13, 2026 19:06
The fused path's eligibility used each rank's own master shards (contiguity, start offset,
dtype), so one rank could fall back to the default path, which runs two all-reduces, while
the others ran the fused path's one, and training would hang. Eligibility now depends only
on the model weights, which every rank holds identically. A rank whose own shards the
multi-tensor kernels cannot read casts them to the model dtype and computes its tile amaxes
and partial cast with the per-tensor kernels, within the fused path's collective schedule.
The test adds an odd slice boundary, so one rank takes the per-tensor kernels and the
others the multi-tensor ones.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Siddhartha Raman S <[email protected]>
@nvMelissa nvMelissa added the 3.0 label Nov 30, 2026
Signed-off-by: Kirthi Shankar Sivamani <[email protected]>
Signed-off-by: Kirthi Shankar Sivamani <[email protected]>
Signed-off-by: Kirthi Shankar Sivamani <[email protected]>
@ksivaman

ksivaman commented Dec 4, 2026

Copy link
Copy Markdown
Member

/te-ci

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants