Skip to content

fix(awq): decode CPU dense fallbacks in a range-safe dtype - #5495

Merged
Qubitium merged 1 commit into
mainfrom
fix/5367-awq-fallback
Nov 19, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/5367-awq-fallback

Conversation

@ZX-ModelCloud

@ZX-ModelCloud ZX-ModelCloud commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

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 as 15 * 8192 = 122880 overflows FP16 to inf; zero activations in the same row then produce 0 * inf = NaN, even though the true output 0.25 * 122880 = 30720 is representable in FP16.

The same decode path existed in TorchAtenAwqLinear and TorchCpuAvx512AwqLinear, so all three CPU fallbacks now share the range-aware decode policy already used by AwqTorchLinear.

What Changed

  • Moved the existing range-aware decode-dtype selection from AwqTorchLinear into _awq_decode.py as AwqDecodeRangeMixin; AwqTorchLinear behavior is unchanged.
  • TorchFusedAwqLinear and TorchAtenAwqLinear now decode with the range-safe dtype and use linear_wide_bias when the decode had to be widened. The reconstructed weight stays local to that forward.
  • TorchCpuAvx512AwqLinear keeps 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.
  • Added regression coverage for FP32/FP16 (fused, ATen) and BF16 (AVX512) activations, including the input gradient and a check that no dense weight is retained.
  • No API or checkpoint-format changes. No behavior switch was added.

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 skipped
python -m pytest tests/kernels/test_awq_torch.py tests/test_awq_shift_capture_5094.py -q -ra
# 68 passed
python -m pytest tests/test_torch_cpu_avx512_awq.py -q -ra
# 46 passed, 1 failed (pre-existing on base, see below)
python -m pytest tests/kernels/test_kernel_compile_levels_cpu.py -q -ra -k "Awq or awq"
# 16 passed
./format/check.sh
# All checks passed

Red evidence: on the pre-fix base, the new forward regression fails with NaN for TorchFusedAwqLinear, TorchAtenAwqLinear, and TorchCpuAvx512AwqLinear. After the fix, all three return the independent expected values 30720.0 and gradient 122880.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

  • This change does not alter a major logic flow, or the required diagrams and docs page are included below.
  • New-flow Mermaid diagram(s) describe the shipped code using real symbol names.
  • The removed/replaced mechanism is shown or tabulated with file:line evidence.
  • Failure and ambiguity paths are marked and visually distinct from success.
  • A reference page under docs/ repeats the diagrams, titled for the mechanism, and is linked from README.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

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching and used the project's normal extension points where possible.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions.

Kernel Accuracy Requirements

  • For kernel-related changes, accuracy drift was measured against an applicable independent Torch FP32/FP64 oracle, or the reason this is not applicable is documented.
  • Fusion support changes preserve AUTO's native class and add actual-device FP16/BF16 tests where supported, native shape/workspace/descriptor checks, mutation, direct source calls, exact restoration and packed-storage checks.
  • The complete kernel inventory/device report lists unavailable dependencies, architecture restrictions and unqualified formats; skips are not validation.

Accuracy evidence: the regression compares the public forward against the independently computed scalar 0.25 * (15 - 0) * 8192 = 30720 and the input gradient against 122880; 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.

@ZX-ModelCloud
ZX-ModelCloud marked this pull request as ready for review November 19, 2026 08:33
@Qubitium
Qubitium merged commit b17bb31 into main Nov 19, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/5367-awq-fallback branch November 19, 2026 08:34
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.
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] TorchFusedAwqLinear's dequantization fallback returns NaN for a finite result

2 participants