ao icon indicating copy to clipboard operation
ao copied to clipboard

[nvfp4 training] Stochastic rounding (cvt.rs) fails to assemble on SM12x; kernels gated on is_sm_at_least_100()

Open rlaope opened this issue 1 week ago • 2 comments

Description

On consumer Blackwell (SM 12.x: RTX 5090, RTX PRO 6000), the NVFP4 training path fails in backward because the Triton kernels emit cvt.rs.satfinite.e2m1x4.f32 (stochastic rounding). ptxas does not support cvt.rs on sm_120a:

triton.runtime.errors.PTXASError: PTXAS error: Internal Triton PTX codegen error
ptxas-blackwell /tmp/....ptx, line 12087; error   : Feature '.rs' not supported on .target 'sm_120a'
ptxas-blackwell fatal   : Ptx assembly aborted due to errors

The kernels and tests gate this path with is_sm_at_least_100(), which is also true on 12.x. The RTNE path (cvt.rn.satfinite.e2m1x2.f32) assembles and runs fine on SM120. The nvfp4_training tests that run real kernels (pass or fail, as opposed to skip) on SM120 split exactly along RTNE vs SR.

  • Kernel: convert_8xfp32_to_4xfp4_packed_rs in torchao/prototype/moe_training/nvfp4_training/hadamard_utils.py (used by hadamard_quantize_row_col_triton.py and group_rht_quantize_row_col_triton.py). The CuteDSL kernels (_cutedsl_kernels_impl.py::_mul_cvt_rs_e2m1x8_f32) use the same instruction, but there cutedsl_nvfp4_kernels_available() also only checks is_sm_at_least_100().
  • Runtime gates: if torch.cuda.is_available() and not is_sm_at_least_100(): raise NotImplementedError(...) in hadamard_quantize_row_col_triton.py, group_hadamard_utils.py and friends. On SM12x these pass, and the user gets the ptxas error above, with a ~minute-long autotune before each failure, instead of a clear "requires SM100 (10.x)" message.
  • Test gates: pytestmark = pytest.mark.skipif(not is_sm_at_least_100(), reason="Requires SM100+") in test/prototype/moe_training/nvfp4_training/test_{hadamard,group_rht}_quantize_row_col.py and test_nvfp4_linear.py.

NVFP4Linear backward always uses SR (_rht_quantize_row_col(..., stochastic_rounding=True) in nvfp4_linear.py), so NVFP4 training does not work at all on SM12x. Either the gates should be cc 10.x (like _check_mxfp4_support in PyTorch, and #5158 for the MXFP4 gemm tests), or SR needs a software fallback on 12.x (RTNE + explicit random bits). That choice is for maintainers. I can send the gate change as a PR if that is the preferred direction.

Failing tests on SM120 (torchao main 0a49cd7):

  • test_group_rht_quantize_row_col.py: test_group_rht_stochastic_rounding_launches[*-triton], test_group_rht_sr_reconstructs[*-triton], test_group_rht_rng_state_controls_stochastic_rounding[*-triton], test_group_rht_sr_unbiased[*-triton] (18)
  • test_hadamard_quantize_row_col.py: *stochastic_rounding-triton, test_triton_rht_quantize_rs_midpoint_distribution, test_rht_quantize_rs_at_most_one_fp4_step_from_rtne[*-triton], test_triton_rht_quantize_row_col_cuda_graph_compile (10)
  • test_nvfp4_linear.py: test_forward_sqnr_and_finite_grads[auto|triton], test_torch_compile_default_mode[auto|triton], test_torch_compile_cuda_graph[auto|triton] (6). The auto compile variants first hit a separate No module named 'cutlass' error from find_spec under dynamo, filed separately.

In the same run (pytest test/prototype/moe_training/nvfp4_training/test_{hadamard,group_rht}_quantize_row_col.py test_nvfp4_linear.py -rA): 34 failed, 122 passed, 36 skipped. All 34 failures are this ptxas .rs error except the two auto compile cases noted above. The 116 passing -triton cases are all RTNE / amax / validation tests (test_rht_quantize_rtne_*, test_rht_quantize_fast_math_sqnr, test_group_rht_correctness, ...), and none of them use SR.

Steps to reproduce

# standalone: cvt.rn assembles on sm_120a, cvt.rs does not
import torch, triton, triton.language as tl

@triton.jit
def _k(x_ptr, r_ptr, o_ptr, RS: tl.constexpr):
    i = tl.arange(0, 64)
    x = tl.load(x_ptr + i)
    if RS:
        r = tl.load(r_ptr + i)
        y = tl.inline_asm_elementwise(
            asm="{ .reg .b16 h; cvt.rs.satfinite.e2m1x4.f32 h, {$1, $2, $3, $4}, $5; cvt.u32.u16 $0, h; }",
            constraints="=r,r,r,r,r,r", args=[x, x, x, x, r], dtype=tl.uint32, is_pure=True, pack=1)
    else:
        y = tl.inline_asm_elementwise(
            asm="{ .reg .b8 b; cvt.rn.satfinite.e2m1x2.f32 b, $1, $2; cvt.u32.u8 $0, b; }",
            constraints="=r,r,r", args=[x, x], dtype=tl.uint32, is_pure=True, pack=1)
    tl.store(o_ptr + i, y)

x = torch.randn(64, device="cuda")
r = torch.randint(0, 2**31, (64,), device="cuda", dtype=torch.int32).view(torch.uint32)
o = torch.empty(64, device="cuda", dtype=torch.uint32)
_k[(1,)](x, r, o, RS=False)  # OK
_k[(1,)](x, r, o, RS=True)   # PTXASError: Feature '.rs' not supported on .target 'sm_120a'

End to end:

import torch
from torchao.prototype.moe_training.nvfp4_training.nvfp4_training import NVFP4Linear
from torchao.quantization.quantize_.common.kernel_preference import KernelPreference

layer = NVFP4Linear(256, 256, bias=False, kernel_preference=KernelPreference.TRITON).cuda().bfloat16()
x = torch.randn(256, 256, device="cuda", dtype=torch.bfloat16, requires_grad=True)
layer(x).sum().backward()   # PTXASError ... Feature '.rs' not supported on .target 'sm_120a'

Environment

torch 2.14.1+cu130, triton 3.8.0 (ptxas-blackwell from the triton wheel)
torchao main 0a49cd7 (source tree on top of the 0.18.0 wheel's compiled extensions)
GPU: NVIDIA RTX PRO 6000 Blackwell Max-Q (SM 12.0), driver 590.48.01 (CUDA 13.1)
OS: Ubuntu 22.04

rlaope avatar Nov 24 '26 10:11 rlaope

I think the software fallback we tried is too slow for the training path I had in mind.

AI-drafted experiment notes

Thanks for isolating the failing instruction and offering to implement a guard. We tried keeping the existing Philox inputs and implementing the FP4 stochastic conversion in software on an RTX 5090. On TorchAO 0a49cd7, the unmodified midpoint test hit the same assembler error. With the prototype, that test passed, as did the explicit Triton NVFP4Linear forward and backward test at M = K = N = 512.

For the grouped quantizer, a 384 × 512 input took 25.81 µs with stochastic rounding and 5.53 µs with nearest rounding. At 4096 × 2048 the times were 69.77 µs and 15.07 µs. The largest difference in our dense sweep was at 1024 × 2432 with fast math, 61.32 µs and 6.91 µs. These are medians for one quantizer call on the same inputs. The stochastic path includes random-number generation as well as conversion. Whole-step training throughput remains unmeasured.

ylwang0701 avatar Nov 24 '26 15:11 ylwang0701

Submitted PR #5166 implementing the SM10x capability gate (_is_sm_10x()) for NVFP4 stochastic rounding kernels, runtime dispatch, CUDA-graph pre-warmup, and test suites.

shotsan avatar Nov 24 '26 22:11 shotsan