Round the ue8m0 FP8 scale before quantizing so dequant matches the stored inverse#46763
Merged
SunMarc merged 1 commit intoJun 24, 2026
Merged
Conversation
…ored inverse For scale_fmt="ue8m0", Fp8Quantize._quantize_one quantized the weight with the unrounded block scale but stored a weight_scale_inv rounded up to a power of two. Fp8Dequantize multiplies by that stored inverse, so the round-trip was off by up to a full octave per block. Round the inverse scale first, re-derive the forward scale from it, then quantize, matching DeepGEMM's order. The scale_fmt="float" path is unchanged.
Contributor
|
CI Dashboard: View test results in Grafana |
Member
|
cc @SunMarc for quants |
SunMarc
approved these changes
Jun 22, 2026
SunMarc
left a comment
Member
There was a problem hiding this comment.
Indeed, otherwise, it doesn't match. cc @ArthurZucker for confirmation
| ) | ||
| torch.testing.assert_close(dequantized_q, expected_q, rtol=1e-2, atol=1e-2) | ||
|
|
||
| def test_fp8_ue8m0_quantize_dequantize_round_trip(self): |
Member
There was a problem hiding this comment.
don't put that here, you should put that in quantization folder / fp8 folder.
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
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.
Fp8Quantize._quantize_onequantizes the weight with the unrounded block scale, then forscale_fmt="ue8m0"(DeepSeek-V4 style) rounds only the storedweight_scale_invup to a power of two. At load timeFp8Dequantizecomputesweight * weight_scale_inv, so the dequant scale can be up to a full octave (~2x) larger than the scale the weight was actually divided by. Nothing flags it: the shapes and dtypes are valid, the weights are just wrong.DeepSeek's own DeepGEMM reference rounds the scale first and quantizes with that same rounded scale (
deep_gemm/utils/math.py:sf = ceil_to_ue8m0(sf); x_fp8 = x / sf), so quant and dequant agree. This change matches that ordering: roundinv_scalesto the power-of-two grid, re-derivescales = 1 / inv_scales, then quantize. The rounding direction is unchanged; only the order moves. Thescale_fmt="float"path never reassignsscales, so non-ue8m0 checkpoints quantize bit-identically.Tests:
test_fp8_ue8m0_quantize_dequantize_round_tripchecks the round-trip (main ~0.6, with this change ~0.022, matching the e4m3 floor);test_fp8_float_scale_fmt_quantization_unchangedasserts thescale_fmt="float"path stays bit-identical to the original formula across 100 random 128x128 blocks. CPU-only.