Skip to content

fix(awq): keep the compiled decode dtype at the eager choice (#4794) - #4822

Merged
Qubitium merged 1 commit into
mainfrom
fix/awq-compiled-fp64-decode
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/awq-compiled-fp64-decode

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

AwqTorchLinear dequantized its whole weight to FP64 inside every compiled graph. _stored_decode_range_fits() returned False for every dtype under torch.compiler.is_compiling() (or CUDA-graph capture), so _decode_dtype_for_activation() fell through both questions to return torch.float64. linear_wide_bias() then promoted the product too, so the GEMM ran in FP64.

That is a regression from #4737: the compile guard was moved in front of the decode-range cache key (correctly — tensor_cache_key reads .is_inference()/data_ptr() on the host, which dynamo rejects) but it answered False instead of the real answer. #4754 then kept SUPPORTS_COMPILE = True on top of it.

Observable effect: compiled is slower than eager (2.3x on the reporter's 4090, where FP64 is 1/64 rate), and its output drifts from eager (up to 0.03 on their checkpoint) instead of matching bitwise.

Fixes #4794.

What Changed

  • gptqmodel/nn_modules/qlinear/torch_awq.py
    • _refresh_decode_range_fits() computes the per-dtype answer eagerly — outside any compiled region — and stores it in _decode_range_fits_by_dtype as plain Python bools. Dynamo guards on those values, so a change recompiles.
    • It is called from clear_weight_cache(), which the base class already invokes on post_init(), a device/dtype move (_apply), a load_state_dict and a training transition. That is the same contract tensor_cache_key documents; an in-place mutation of the scales still requires an explicit clear_weight_cache().
    • The compiled/capture branch now returns precomputed.get(dtype, False) instead of a blanket False, so it never escalates to FP64 unless the eager answer did.
    • The reduction moved to _compute_decode_range_fits() and now performs one host sync instead of three (it also runs once per dtype on each move).
    • getattr(self, "scales", None) replaces self.scales, so the refresh is safe on a module that never registered buffers.
  • tests/kernels/test_awq_torch.py — 12 new nodes in the existing decode-range section (4 test functions; one parametrized over three scale cases; each on CPU and, where available, CUDA).

Behaviour change: none for eager. A compiled AwqTorchLinear now decodes in the same dtype as eager, so it is faster and numerically identical. The pathological BF16-scale-beyond-FP32 case still widens to FP64, under compile too.

Tests

cd tests
python -m pytest kernels/test_awq_torch.py -q -ra
# 54 passed in 8.00s   (12 of them new)

python -m pytest kernels/test_awq_torch.py -q -ra -k compiled
# 12 passed

python -m pytest test_awq_affine_range.py test_awq_dequant_layouts.py \
    test_awq_dynamic_group_size.py test_awq_hf_cpu.py test_awq_per_module_bits.py \
    test_awq_qqq_pack_shape_4567.py test_awq_rank_and_empty.py test_awq_mla_scaling.py -q -ra
# 210 passed in 236.59s

python -m pytest kernels/test_compile_capability_4739.py -q -ra -k awq
# 1 passed   (trace-clean: still 1 graph, 0 breaks with the dict lookup)

ruff check gptqmodel docs/eora tests setup.py (the format/check.sh gate): All checks passed.

Red / green evidence

Reverting only the torch_awq.py hunk and keeping the new tests:

Test node Broken code With fix
test_awq_torch_compiled_decode_dtype_matches_eager[fits-0.001-3-2-*] FAIL assert torch.float64 is torch.float16 PASS
...[weight-overflow-10000.0-7-0-*] FAIL assert torch.float64 is torch.float32 PASS
...[scale-underflow-1e-08-1-0-*] FAIL assert torch.float64 is torch.float32 PASS
test_awq_torch_compiled_decode_follows_the_scales[*] FAIL assert torch.float64 is torch.float32 PASS
test_awq_torch_compiled_forward_is_bitwise_eager[*] FAIL assert torch.equal(actual, eager) PASS
test_awq_torch_compiled_decode_keeps_fp64_past_fp32[*] (control) PASS PASS

Red run: 10 failed, 2 passed in 7.90s. Green run: 54 passed in 8.00s. The two red-run passes are the intended control: pre-fix code returns FP64 for everything, so the pathological case passes by accident and must keep passing after the fix.

test_awq_torch_compiled_forward_is_bitwise_eager runs a real torch.compile(..., backend="eager", fullgraph=True) trace, so it exercises the compiled branch rather than a monkeypatched is_compiling.

Kernel Accuracy Requirements

Module level, AwqTorchLinear 2048x8192, fp16, M=2 decode, same host, torch.compile default backend:

decode dtype compiled ms eager ms compiled vs eager
before torch.float64 0.290 0.934 differs, max abs 4.88e-04
after torch.float16 0.162 0.911 bitwise equal, max abs 0.0

The fix removes the FP64 decode; it does not change the eager numerics. This host is a datacenter GPU with fast FP64, so the absolute gap here (1.8x) is smaller than the reporter's 4090 (2.3x slower than eager).

Logic Flow Documentation

Not applicable: a localized bug fix inside one decision function. The compile branch of _stored_decode_range_fits() now returns the answer the eager branch already computes, instead of a constant False; no stage, ordering, dispatch table or tensor ownership changes.

Model Support / Module Plan

Not applicable — no MODEL_MAP entry, definition or fusion config is touched.

Review Requirements

  • Reviewed this PR.
  • Matches existing project structure and conventions (extends the existing decode-range section of tests/kernels/test_awq_torch.py).
  • No monkeypatching in the code under test; one test helper swaps torch.compiler.is_compiling to reach the branch cheaply, and one test uses a real torch.compile trace.
  • Minimal surface: one precomputed dict refreshed from the existing cache-clear hook.
  • Regression considered: eager path unchanged, trace-cleanliness re-verified, 210 AWQ family tests pass, FP64 widening preserved.

Notes

  • The precomputed answers cost one host sync per dtype per clear_weight_cache() (3 total) instead of three syncs on the first eager forward. That is one extra sync per module on load and fewer on the first forward.
  • #4794 also asked for a module-level compiled < eager timing assertion. I used decode-dtype parity plus a bitwise compiled/eager comparison instead: they pin the same root cause deterministically, while a timing assertion is load-dependent and would flake in CI.

#4737 moved the compile guard in front of the decode-range cache key and
returned False for every dtype, so _decode_dtype_for_activation fell through
to FP64 inside every compiled graph. linear_wide_bias() then promoted the
product, running the whole GEMM in FP64: the compiled AwqTorchLinear was
slower than eager and its output drifted from the eager one.

Decide the per-dtype range answers eagerly, where the scales can change
(post_init, a device/dtype move, a state-dict load, a training transition),
and read them back inside the compiled region as plain Python constants
instead of escalating to FP64.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant