Skip to content

fix(quant): keep E8M0/MXFP4/EXL3 pow exponents in float32 (#5297) - #5298

Merged
Qubitium merged 1 commit into
mainfrom
fix/e8m0-default-dtype-5297
Nov 19, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/e8m0-default-dtype-5297

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #5297. Regression from #5274 (which fixed #5263).

Problem

#5274 replaced torch.ldexp(torch.ones_like(x, dtype=torch.float32), e) with
torch.pow(2.0, e). A Python float base with an integer exponent tensor
follows the process default dtype, so the decoders returned whatever
torch.get_default_dtype() happened to be:

torch.float16 default
  before: float32 [5.88e-39, 7.45e-09, ..., 1.70e+38]
  after:  float16 [0.0, 0.0, 7.63e-06, 1.0, 8192.0, inf, inf]

The default dtype is float16 for the whole process while any
from_pretrained/from_config runs (transformers' local_torch_dtype), so a
concurrent GPTQModel.load(..., dtype=torch.float16) silently zeroed MXFP4 /
E8M0 source weights during another thread's dequantize.

Change

Widen the exponent to float32 before pow at every site #5274 converted:

  • quantization/dtype.py::decode_e8m0_scale
  • utils/kimi_k3.py::decode_mxfp4
  • nn_modules/exllamav3.py::ExllamaV3Linear._prescale_from_row_amax (same
    hazard: 2**116 clipped to inf under an fp16 default, zeroing a wide bf16
    row's pre-scale)

The result is float32 again, bit-exact over the whole E8M0 range (including the
denormal 2**-127 that exp2 rounds one ulp low on CUDA). The TT block-float
encoders were checked and already cast their exponents.

Tests

New tests/test_e8m0_default_dtype_5297.py (20 nodes) parametrizes the process
default dtype over float32 / bfloat16 / float16 / float64, on CPU and CUDA:

  • decode_e8m0_scale returns float32 and matches the float32 baseline for all
    256 bytes, with 2**-127 and 2**127 exact;
  • decode_mxfp4 returns the same weights as the float32 baseline for extreme
    E8M0 scales (including 2**23, inf under fp16);
  • the EXL3 pre-scale keeps a 3e38 bf16 row finite (2**116).

9 of the 20 fail on main (bfloat16 changes the dtype, float16/float64 change
the values or the dtype). The default dtype is restored by a fixture.

Existing decoders' suites still pass: 120 passed, 13 skipped
(test_ldexp_cross_device_5263, test_kimi_k3_support,
test_nvfp4_candidate_overflow, test_model_dequant_fp8,
test_exl3_wide_input).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment