Repository navigation
fix(macaq): keep finite MXFP4 weights wide before the dot (#5631) - #5667
Merged
Merged
Conversation
`MacaqLinear._forward_torch` decoded the packed weight at the activation dtype before the FP32 reduction. A finite MXFP4 weight above the FP16 range (65536 for the reported layer) decodes to `inf`, so the zero lanes of the dot contribute `0 * inf = NaN` even though the stored value is finite and the exact output (16384) is representable. Select the decode dtype from the packed E8M0 scales: when the largest decoded magnitude overflows the activation dtype, or the smallest nonzero one rounds to zero, decode the weight in FP32 and widen the dot's activation operand, then round only the final output. The per-dtype answer is precomputed on load/move so a compiled graph reads it as a Python constant instead of a host reduction, the same contract as the AWQ decode-range cache (#5495). The narrow path is unchanged when the packed range fits. Adds tests/test_macaq_weight_narrow_5631.py covering the FP16 overflow (NaN before the fix), the FP16 underflow direction, the unchanged narrow path, BF16 (every FP32-finite MXFP4 magnitude is BF16-finite) and a state-dict reload.
Resolve the macaq.py conflict by keeping the extracted decode_mxfp4_scales() helper and adopting upstream's 2**exponent decode (#5627), which avoids the CUDA exp2 rounding of 2**-127.
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.
Summary
MacaqLinear._forward_torchdecoded the packed MXFP4 weight at the activation dtype before the FP32 reduction. A finite weight above the FP16 range (65536 in the reported layer) therefore decoded toinf, and the zero lanes of the dot contributed0 * inf = NaN, even though the stored value is finite and the exact output (16384) is representable.post_init()/load_quant_result()and refreshed throughclear_weight_cache()on device/dtype moves and state-dict loads, so a compiled graph reads a Python constant instead of doing a host reduction. This mirrors the AWQ decode-range cache (fix(awq): decode CPU dense fallbacks in a range-safe dtype #5495).dequantize_mxfp4now shares its E8M0 decode throughdecode_mxfp4_scales(); behavior is unchanged.BF16 needs no wide decode for an FP32-finite MXFP4 value: the format's largest FP32-finite magnitude is still below BF16's maximum, and its smallest nonzero magnitude is above BF16's smallest subnormal. The regression test pins that so only FP16 selects the wide path.
Tests
tests/test_macaq_weight_narrow_5631.py(new, 7 tests): FP16 overflow returns 16384 and finite (NaN before the fix), matches the FP32 reference, both dot operands are FP32, in-range weights still decode at the activation dtype, BF16 stays narrow, the FP16 underflow direction decodes wide, and a state-dict load refreshes the answer.tests/test_macaq.py,tests/test_macaq_fp32_reduce_4221.py,tests/test_macaq_wide_bias_5632.py,tests/test_macaq_forward_dtype_4217.pyplus the new file: 146 passed, 1 skipped.tests/test_macaq_single_output_4472.py,tests/test_macaq_compile_flip_memory_4801.py,tests/test_macaq_compile_flip_residency_4801.py,tests/test_macaq_rotation_serialization_4225.py,tests/test_macaq_triton_forward_dtype_4315.py,tests/test_checkpoint_resume_nf12_macaq_4792.py: 27 passed.uvx [email protected] check gptqmodel tests: All checks passed!Fixes #5631.