Repository navigation
fix(awq): decode CPU dense fallbacks in a range-safe dtype - #5495
Merged
Merged
Conversation
ZX-ModelCloud
force-pushed
the
fix/5367-awq-fallback
branch
from
November 19, 2026 08:13
0639f3d to
52f0c43
Compare
ZX-ModelCloud
marked this pull request as ready for review
November 19, 2026 08:33
This was referenced Nov 20, 2026
Qubitium
added a commit
that referenced
this pull request
Nov 20, 2026
…) (#5667) `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.
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
Fixes #5367.
TorchFusedAwqLinear's differentiable dense fallback decoded packed AWQ weights in the checkpoint scale dtype (FP16) and cast the result to the activation dtype afterwards. A finite decoded coefficient such as15 * 8192 = 122880overflows FP16 toinf; zero activations in the same row then produce0 * inf = NaN, even though the true output0.25 * 122880 = 30720is representable in FP16.The same decode path existed in
TorchAtenAwqLinearandTorchCpuAvx512AwqLinear, so all three CPU fallbacks now share the range-aware decode policy already used byAwqTorchLinear.What Changed
AwqTorchLinearinto_awq_decode.pyasAwqDecodeRangeMixin;AwqTorchLinearbehavior is unchanged.TorchFusedAwqLinearandTorchAtenAwqLinearnow decode with the range-safe dtype and uselinear_wide_biaswhen the decode had to be widened. The reconstructed weight stays local to that forward.TorchCpuAvx512AwqLinearkeeps its natural checkpoint-scale decode dtype when it is safe, and widens only when that dtype cannot represent the decoded weight before casting to BF16.Tests
All commands below were run with PyTorch 2.15.0.
python -m pytest tests/test_inference_fp16_overflow.py \ tests/test_awq_torch_fused_mode_5018.py \ tests/test_cpu_trainable_kernels_grad_after_inference_5314.py \ tests/kernels/test_awq_cpu_fused_post_init.py \ tests/kernels/test_awq_torch_fused.py \ tests/kernels/test_awq_runtime_dtype_storage.py \ tests/kernels/test_cpu_fp32_fused_numerics.py -q -ra # 62 passed, 1 skippedpython -m pytest tests/kernels/test_awq_torch.py tests/test_awq_shift_capture_5094.py -q -ra # 68 passedpython -m pytest tests/test_torch_cpu_avx512_awq.py -q -ra # 46 passed, 1 failed (pre-existing on base, see below)./format/check.sh # All checks passedRed evidence: on the pre-fix base, the new forward regression fails with
NaNforTorchFusedAwqLinear,TorchAtenAwqLinear, andTorchCpuAvx512AwqLinear. After the fix, all three return the independent expected values30720.0and gradient122880.0.The one skipped test is the XPU variant of
test_torch_fused_awq_matches_checkpoint_module(Intel XPU runtime unavailable).Pre-existing failures on the base, also present before this change and unrelated to the touched code:
tests/test_torch_cpu_avx512_awq.py::test_awq_layer_preserves_activation_dtype_and_rank[dtype0]tests/test_kernel_audit_regressions.py::test_device_move_restores_checkpoint_layout[gptqmodel.nn_modules.qlinear.torch_aten_kernel-TorchAtenLinear]tests/test_kernel_audit_regressions.py::test_device_move_restores_checkpoint_layout[gptqmodel.nn_modules.qlinear.torch_fused-TorchFusedLinear]Unit tests have been executed and passed, or the reason they are not applicable is documented.
I added a new simple/fast unit test for this change, or documented why that is not applicable.
Model Support / Module Plan
Not applicable: no new model type, alias, definition, or module-plan change.
AWQ onboarding safety
Not applicable: no new model/alias and no AWQ grouping, search, replay, or fold change. This is a local numerical fix to the dense fallback decode.
Logic Flow Documentation
file:lineevidence.docs/repeats the diagrams, titled for the mechanism, and is linked fromREADME.md.Diagram summary: not applicable. This is a local numerical fallback fix. It reuses the range-aware decode policy that already ships in
AwqTorchLinear; it does not change dispatch, resolution order, or a selection mechanism.Review Requirements
Kernel Accuracy Requirements
Accuracy evidence: the regression compares the public forward against the independently computed scalar
0.25 * (15 - 0) * 8192 = 30720and the input gradient against122880; it also asserts that the module retains no dense[in_features, out_features]weight after the call.Notes
The widened decode is only selected when the checkpoint scale dtype would overflow or underflow. In-range checkpoints keep their previous decode dtype, so the AVX512 training gradient behavior is unchanged. The temporary dense weight is call-local and is not cached on the module.