Repository navigation
[Common] Reject odd width and start offset in NVFP4 2D partial cast - #4032
ArchanaChetan07 wants to merge 2 commits into
Conversation
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]>
Confidence Score: 4/5
[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
SummaryThis PR rejects odd widths and odd shard starts before launching the NVFP4 partial-cast kernel.
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") |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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]>
|
Want your agent to iterate on Greptile's feedback? Try greploops. |
Description
nvfp4_2d_partial_cast_kernelwrites each pair of adjacent elements (columns4k..4k+3of a thread, taken two at atime) 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
wandstart_offsetare both even. Otherwise, as #4003 shows, the kernel writes the wrong byte ornibble. 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:
NVFP4Quantizerrequires the last dimension to be a multiple of 32 (nvfp4_tensor.py). Sowis always even, andin practice a multiple of the 16-wide tile.
_cast_master_weights_to_nvfp4slices the FP4 buffer asrowwise_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_castnow rejects an oddwor an oddstart_offsetwithNVTE_CHECK, andthe C API docs say so. The kernel itself is unchanged.
Note: open PR #3869 adds the same
start_offsetcheck as part of a larger change. This PR also coverswand addstests. I'm happy to rebase onto it or drop the overlapping line, whichever the maintainers prefer.
Fixes #4003
Type of change
Changes
transformer_engine/common/recipe/nvfp4.cu:NVTE_CHECK(w % 2 == 0)andNVTE_CHECK(start_offset % 2 == 0)innvfp4_2d_partial_cast, before the launch.transformer_engine/common/include/transformer_engine/recipe.h: document both requirements fornvte_nvfp4_2d_partial_castandnvte_nvfp4_2d_partial_cast_v2.tests/pytorch/nvfp4/test_nvfp4_2d_partial_cast.py(new, single GPU, run byqa/L0_pytorch_unittest):test_sharded_partial_cast_matches_full: casts a tensor shard by shard into slices of one packed buffer andcompares the bytes with a cast of the whole tensor (
atol=rtol=0). The cases include widths that are not amultiple of 16 (the
kWidthAligned = falsepath: 40, 34), an odd row count, and even shard boundaries that splitrows and tiles. Skipped without NVFP4 support.
test_partial_cast_rejects_odd_width_or_offset: oddw(the [BUG]nvfp4.cu:nvfp4_2d_partial_cast_kernelcorrupts packed FP4 bytes/nibbles and races non-atomically onoutput[byte_idx]wheneverworstart_offsetis odd #4003 cases2x3and64x17) and oddstart_offset(1x16from offset 1, and a mid-tensor offset) raiseRuntimeError. This test needs only CUDA,because the check runs before the launch.
Testing
nvfp4.cucompiles forsm_100awith nvcc 13.0.88 (standalonenvcc -c,-gencode arch=compute_100a,code=sm_100a).-style=file) and black (line length 100) leave the changed files unchanged.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:
Prepared with AI assistance (Claude Code).
🤖 Generated with Claude Code