Skip to content

fix(macaq): keep finite MXFP4 weights wide before the dot (#5631) - #5667

Merged
Qubitium merged 2 commits into
mainfrom
fix/5631-macaq-weight-narrow
Nov 20, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/5631-macaq-weight-narrow

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

MacaqLinear._forward_torch decoded 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 to inf, and the zero lanes of the dot contributed 0 * inf = NaN, even though the stored value is finite and the exact output (16384) is representable.

  • The Torch fallback now picks the decode dtype from the packed E8M0 scales: if the largest decoded magnitude overflows the activation dtype, or the smallest nonzero one rounds to zero, the weight is decoded in FP32 and the dot's activation operand is widened; only the final output is rounded.
  • The per-dtype answer is precomputed at post_init() / load_quant_result() and refreshed through clear_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).
  • The narrow path is untouched when the packed range fits, so ordinary checkpoints keep the existing decode and reduction.
  • dequantize_mxfp4 now shares its E8M0 decode through decode_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.py plus 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.

`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.
Comment thread tests/test_macaq_weight_narrow_5631.py Dismissed
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.
@Qubitium
Qubitium merged commit 179a0e7 into main Nov 20, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/5631-macaq-weight-narrow branch November 20, 2026 05:31
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.

[BUG] MACaQ W4A16 Torch narrows finite MXFP4 weights before the dot: a bias-free FP16 output is NaN instead of 16384

1 participant