Fix FP8 (FLOAT8E4M3FN) quantization scale using wrong reference distribution#29350
Merged
tianleiwu merged 1 commit intoJun 27, 2026
Merged
Conversation
…n scale compute_scale_zp_float8 builds the reference distribution whose standard deviation scales weights/activations for FLOAT8E4M3FN (scale = std_data / std_f8). It used 'all_values = [float(i) for i in range(256)]', i.e. the integers 0..255 (std ~= 73.90), instead of reinterpreting each of the 256 byte patterns as a float8_e4m3fn value (the finite set spanning -448..448, std ~= 100.06). A regression from the ONNX 1.19 integration that replaced onnx's float8e4m3_to_float32 helper. As a result every FP8 scale was ~35% too large, degrading FP8-quantized model accuracy. Reinterpret the byte patterns via float8_e4m3fn; the existing NaN/inf filter then drops the 2 NaN patterns (254 values). Adds a regression test.
tianleiwu
approved these changes
Jun 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
compute_scale_zp_float8(inonnxruntime/python/tools/quantization/quant_utils.py) computes the FP8 quantization scale asscale = std_data / std_f8, wherestd_f8is the standard deviation of the representableFLOAT8E4M3FNvalues. It built that reference distribution as:That's the integers
0.0 .. 255.0— not the float8 values. It should reinterpret each of the 256 byte patterns as afloat8_e4m3fnvalue (the finite set spanning-448..448). This is a regression from the ONNX 1.19 integration that removedonnx.numpy_helper.float8e4m3_to_float32(the prior code was[float8e4m3_to_float32(i) for i in range(256)]); the repo's own reference notebookdocs/python/notebooks/quantization_f8.ipynbstill documents the correct algorithm.Effect:
std_f8is computed as 73.90 instead of 100.06, so every FP8 scale is ~35% too large, degrading FP8-quantized model accuracy. The path is live — called fromonnx_quantizer.pyandqdq_quantizer.py.Reproduction (real function)
Fix
The existing
not numpy.isnan(f) and not numpy.isinf(f)filter then drops the 2 NaN byte patterns, leaving the 254 finite float8 values.float8_e4m3fnandnumpyare already imported.Test
Adds
test_compute_scale_zp_float8toonnxruntime/test/python/quantization/test_quant_util.pyassertingscale == std / 100.0577(and linearity instd). It fails on the old code (std_f873.9) and passes after the fix.