Skip to content

fix(moe): propagate NaN through the FP8 casts and floor gradient casts at 1e-30 - #4896

Open
MarioSieg wants to merge 3 commits into
mainfrom
mario/fp8-cast-fixes
Open

MarioSieg wants to merge 3 commits into
mainfrom
mario/fp8-cast-fixes

Conversation

@MarioSieg

@MarioSieg MarioSieg commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • NaN propagation. The per-token, grouped per-token and grouped per-channel Triton casts in fp8_utils.py took their amax with tl.max and tl.maximum (both drop NaN) and clamped with tl.clamp (which turns NaN into -448). A NaN in the input was cast to finite garbage, so a NaN in activations or gradients could go unnoticed in the FP8 paths. The amax reduction, its floor and the clamp now use propagate_nan=tl.PropagateNan.ALL: the NaN's group gets a NaN scale and NaN values, and the other groups are unchanged. Finite inputs cast to the same bytes as before. The 128x128 weight cast has no clamp and is unchanged.
  • Gradient amax floor at 1e-30. The casts can now take an amax_floor (default ACTIVATION_AMAX_FLOOR = 1e-10). The FP8 grouped GEMM backward (deepgemm_fp8 experts: the dgrad per-token cast and the wgrad per-channel cast of the output gradient) and the FP8 blockwise linear/bmm backwards cast the output gradient with GRAD_AMAX_FLOOR = 1e-30. Gradients far below the loss's scale then keep exact scales instead of flushing toward zero. Activation casts, in both the forward and the backward, keep 1e-10, so the forward still matches vLLM's per-token-group quant bit for bit.
  • Routing-dependent Triton specialization: nothing to change. I checked every Triton launch on the MoE path for integer args that change with routing, since Triton would compile a new variant whenever such an arg first becomes 1 or a multiple of 16. The FP8 cast kernels only get row counts through the grid and tensors. In the NVFP4 epilogue and SwiGLU-backward kernels, the per-expert row counts and offsets are multiples of the 32-row group alignment. nvfp4_to_mxfp8 and the Mega MoE MX casts take padded or per-rank token counts. So no do_not_specialize is needed.
  • Adds test_token_and_channel_casts_propagate_nan to tests/unit/train/models/test_fp8_utils.py.

GPU validation is still pending: this was written without access to a GPU.

Test plan

  • ruff check and ruff format on the changed files
  • Triton interpreter (TRITON_INTERPRET=1, CPU) comparing the old and new casts on the same inputs: for every cast, with and without UE8M0, only the groups that contain a NaN change, and their scales are now NaN. With the 1e-30 floor, only a group with amax below 1e-10 changes. The interpreter does not emulate e4m3 rounding or NaN encoding faithfully, so it could not check the exact fp8 bytes.
  • The new test's body on CPU under the Triton interpreter, with the fp8 NaN-encoding assertion relaxed
  • GPU: tests/unit/train/models/test_fp8_utils.py (including the vLLM bit-match tests) and tests/unit/train/models/test_fp8_grouped_gemm.py

Note

Medium Risk
Changes FP8 quantization numerics and NaN semantics on training backward paths (MoE grouped GEMM and blockwise linear), though forward activation quantization stays vLLM-aligned at 1e-10.

Overview
NaN propagation in FP8 casts: Per-token and grouped per-token/channel Triton kernels no longer turn NaNs into finite FP8 values. Amax reduction and clamp now use NaN-propagating reductions (_amax, _nan_max) and propagate_nan=tl.PropagateNan.ALL on clamp, so a NaN in a quantization group yields NaN scales and values for that group only; other groups stay bit-identical to before on finite inputs.

Configurable amax floors: Casts accept amax_floor (default ACTIVATION_AMAX_FLOOR = 1e-10 for vLLM-aligned activations). FP8 linear and grouped-GEMM backward paths cast output gradients with GRAD_AMAX_FLOOR = 1e-30 so tiny gradients keep exact scales instead of being floored at 1e-10. Forward activation casts are unchanged at 1e-10.

Adds GPU test test_token_and_channel_casts_propagate_nan for the three affected cast variants.

Reviewed by Cursor Bugbot for commit d61984a. Bugbot is set up for automated code reviews on this repo. Configure here.

…s at 1e-30

The per-token and per-channel Triton casts took their amax with tl.max and floored it with tl.maximum, both of which
drop NaN, and clamped with tl.clamp, which turns NaN into -448. A NaN in the input then left the cast as finite
garbage. The amax, its floor and the clamp now propagate NaN, so the group's scale and values are NaN. Finite inputs
cast to the same bytes as before.

The casts of gradients in the FP8 grouped GEMM and FP8 linear backwards now floor their amax at 1e-30 instead of
1e-10, so gradients far below the loss's scale keep exact scales instead of flushing toward zero. Activation casts,
in the forward and backward, keep the 1e-10 floor that matches vLLM's per-token-group quant.
@MarioSieg
MarioSieg marked this pull request as ready for review December 2, 2026 18:40

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

Development

Successfully merging this pull request may close these issues.

1 participant