Skip to content

[Common] Reject odd width and start offset in NVFP4 2D partial cast - #4032

Open
ArchanaChetan07 wants to merge 2 commits into
NVIDIA:mainfrom
ArchanaChetan07:fix/nvfp4-partial-cast-odd
Open

ArchanaChetan07 wants to merge 2 commits into
NVIDIA:mainfrom
ArchanaChetan07:fix/nvfp4-partial-cast-odd

Conversation

@ArchanaChetan07

@ArchanaChetan07 ArchanaChetan07 commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Description

nvfp4_2d_partial_cast_kernel writes each pair of adjacent elements (columns 4k..4k+3 of a thread, taken two at a
time) as one whole byte. It uses a byte index from the first element of the pair and nibble parity from the global
element index. This is correct only when every output byte holds two elements of the same row and the same shard,
that is, when w and start_offset are both even. Otherwise, as #4003 shows, the kernel writes the wrong byte or
nibble. Because the update is a non-atomic read-modify-write, thread blocks can also race on a byte shared across a
tile boundary.

In-tree callers never pass odd values:

  • NVFP4Quantizer requires the last dimension to be a multiple of 32 (nvfp4_tensor.py). So w is always even, and
    in practice a multiple of the 16-wide tile.
  • _cast_master_weights_to_nvfp4 slices the FP4 buffer as rowwise_bytes[start_offset // 2 : (end_offset + 1) // 2].
    That only gives each rank whole bytes when shard boundaries are even. Megatron-LM's distributed optimizer pads
    parameter starts to 64 elements and bucket ends to DP-divisible sizes, including in its packed NVFP4 buffer layout.

So this PR adds no device-side work: no nibble atomics, no per-element byte index. Instead it makes the existing
requirement explicit. nvfp4_2d_partial_cast now rejects an odd w or an odd start_offset with NVTE_CHECK, and
the C API docs say so. The kernel itself is unchanged.

Note: open PR #3869 adds the same start_offset check as part of a larger change. This PR also covers w and adds
tests. I'm happy to rebase onto it or drop the overlapping line, whichever the maintainers prefer.

Fixes #4003

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

  • transformer_engine/common/recipe/nvfp4.cu: NVTE_CHECK(w % 2 == 0) and NVTE_CHECK(start_offset % 2 == 0) in
    nvfp4_2d_partial_cast, before the launch.
  • transformer_engine/common/include/transformer_engine/recipe.h: document both requirements for
    nvte_nvfp4_2d_partial_cast and nvte_nvfp4_2d_partial_cast_v2.
  • tests/pytorch/nvfp4/test_nvfp4_2d_partial_cast.py (new, single GPU, run by qa/L0_pytorch_unittest):

Testing

  • nvfp4.cu compiles for sm_100a with nvcc 13.0.88 (standalone nvcc -c, -gencode arch=compute_100a,code=sm_100a).
  • clang-format 18.1.6 (-style=file) and black (line length 100) leave the changed files unchanged.
  • Not run yet: the new tests have not been run on any GPU. The partial-cast kernel needs Blackwell (sm_100+), and
    I don't have access to one yet. A full TE build and test run on B200/GB200 is still pending, and CI results would
    be welcome.

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

Prepared with AI assistance (Claude Code).

🤖 Generated with Claude Code

nvfp4_2d_partial_cast_kernel writes each pair of adjacent elements as
one whole byte. With an odd width a byte holds elements of two rows,
and with an odd start offset it holds elements of two shards. The
kernel then writes the wrong byte or nibble and can race on a byte
shared between thread blocks (NVIDIA#4003).

Callers in TE never pass these values. NVFP4 tensors need a width that
is a multiple of 32, and the distributed-optimizer shards of a packed
FP4 buffer start on byte boundaries. So check both on the host instead
of making the kernel nibble-atomic. The check adds no device work.

Add a single-GPU test that casts a tensor shard by shard, including
widths that are not a multiple of the tile size and shards that split
rows and tiles, and checks the bytes against a cast of the whole
tensor. It also checks that odd widths and offsets are rejected.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: ArchanaChetan07 <[email protected]>
@ArchanaChetan07
ArchanaChetan07 marked this pull request as ready for review December 5, 2026 02:41
@greptile-apps

greptile-apps Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 4/5RetriggerView in Greptile

[Medium impact] The change affects the NVFP4 2D partial-cast API and its callers.

The tests still need a CUDA build-version guard before merging.

Findings

  1. P1 Tests fail on older CUDA ▶

Summary

This PR rejects odd widths and odd shard starts before launching the NVFP4 partial-cast kernel.

  • NVFP4 partial casts reject odd widths and shard starts.

Reviews (3) · Last reviewed commit: "[PyTorch] Skip NVFP4 partial-cast reject..." · Reviewed by Greptile

torch.testing.assert_close(sharded, reference, atol=0, rtol=0)


@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA is required")

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.

P1 Tests fail on older CUDA

The CUDA-only skip runs these cases on supported builds made with CUDA 12.1–12.7. On those builds, FP4_TYPE_SUPPORTED is false, so nvte_nvfp4_2d_partial_cast_v2 raises “FP4 support requires CUDA 12.8+” before reaching either new check. All four cases then fail their expected-message match. Skip this test when the common library was built without FP4 support.

Knowledge Base Used: Native CUDA kernel platform

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.

Good catch, thanks. Fixed in 57270c8: the whole test file is now gated on is_nvfp4_available(), the same as the rest of tests/pytorch/nvfp4, so it skips on builds without FP4 support instead of failing on the "FP4 support requires CUDA 12.8+" error.

nvte_nvfp4_2d_partial_cast_v2 raises "FP4 support requires CUDA 12.8+"
before the new width and offset checks when TE is built with CUDA
older than 12.8, so the rejection test fails there. Gate the whole
file on NVFP4 support, like the rest of tests/pytorch/nvfp4.

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

greptile-apps Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Want your agent to iterate on Greptile's feedback? Try greploops.

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

None yet

Projects

None yet

1 participant