Skip to content

[Common/PyTorch] Add stochastic rounding to the MXFP8 cast - #4

Open
HJSang wants to merge 1 commit into
miles-mainfrom
Hejian/te-mxfp8-stochastic-rounding
Open

HJSang wants to merge 1 commit into
miles-mainfrom
Hejian/te-mxfp8-stochastic-rounding

Conversation

@HJSang

@HJSang HJSang commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

Why

The end-to-end 4-bit MoE recipe (radixark/miles#4952) runs the backward GEMMs in MXFP8: saved NVFP4 forward tensors are dequantized, transposed and re-quantized to MXFP8, and incoming gradients are cast to MXFP8, both with stochastic rounding so the re-quantization stays unbiased. The MXFP8 cast only rounds to nearest. This PR adds the stochastic-rounding cast; the backward_override mode that consumes it is a follow-up.

What

  • MXFP8Quantizer(stochastic_rounding=False). When on, the generic MXFP8 kernel rounds each element with cvt.rs.satfinite.e4m3x4.f32 / e5m2x4 (sm_100a / sm_103a) on Philox bits: one 32-bit draw per four elements, with the seed/offset/subsequence scheme NVFP4 already uses.
    • Block (E8M0) scales are computed exactly as before; only element rounding changes.
    • The specialized cast-only kernel rounds to nearest only, so it is skipped when the option is on.
    • With the option off, the output is unchanged.
  • Dispatch: the single-tensor quantize helpers (forward and backward) accept stochastic rounding for MXFP8 as well as NVFP4, and the host checks the RNG state.
  • No silent fallback to round-to-nearest:
    • fused activation, dbias, dact-dbias and cuDNN norm paths use their unfused path (MXFP8Quantizer::quantize) for a stochastic-rounding quantizer;
    • grouped quantize, bgrad_group_quantize and MXFP8 attention raise instead.
  • The existing _LOOSE_PRECISION cvt.rs FP8 helpers in ptx.cuh feed a constant random operand, so this PR adds a separate helper, cvt_fp32_to_fp8_4x_with_stochastic_rounding.

Tests (B200, CUDA 13.0, torch 2.14.1)

Check Result
New tests/pytorch/mxfp8/test_mxfp8_stochastic_rounding.py: scales equal round-to-nearest; round-to-nearest equals a PyTorch cast bit for bit; every SR value is one of its two FP8 grid neighbours; unbiased over 512 draws; same seed gives the same bits. Covers row / column / both directions, bf16 / fp32 input, E4M3 / E5M2 output, and a ragged 192×160 shape 45 passed
mxfp8/test_mxfp8_quantize_swizzle_fusion.py, mxfp8/test_mxfp8_group_quantize_graph_safe.py 306 passed
test_quantized_tensor.py -k mxfp8 31 passed
Ad hoc: tex.gelu and tex.bgrad_quantize with a stochastic-rounding quantizer round stochastically onto valid neighbours OK

Not covered

  • Grouped MXFP8 quantize (the MoE GroupedLinear split path) still rejects stochastic rounding. It is the next PR, before backward_override="mxfp8".
  • Tested on sm_100 (B200) only. The sm_103a target is built but not run on a B300.
  • The full numerics suite was not run.

🤖 Generated with Claude Code

MXFP8 backward GEMMs that re-quantize saved forward tensors and gradients need
unbiased rounding (the end-to-end 4-bit MoE recipe, radixark/miles#4952). The
MXFP8 cast only rounded to nearest.

- MXFP8Quantizer(stochastic_rounding=False): the generic MXFP8 kernel rounds
  each element with cvt.rs.satfinite.e4m3x4 / e5m2x4 on Philox bits (one 32-bit
  draw per four elements, seeded like NVFP4). Block scales are unchanged; the
  round-to-nearest cast-only kernel is skipped when the option is on.
- The single-tensor quantize dispatch accepts stochastic rounding for MXFP8.
- Fused activation, dbias, dact-dbias and cuDNN norm paths fall back to their
  unfused paths for a stochastic-rounding quantizer; grouped quantize,
  bgrad_group_quantize and MXFP8 attention reject it instead of silently
  rounding to nearest.

Tested on B200: tests/pytorch/mxfp8/test_mxfp8_stochastic_rounding.py (45
passed), existing MXFP8 swizzle-fusion and group-quantize tests (306 passed),
MXFP8 cases of test_quantized_tensor.py (31 passed).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Hejian Sang <sanghj0923@gmail.com>
@HJSang
HJSang marked this pull request as ready for review November 19, 2026 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant