Repository navigation
fix(awq): keep the compiled decode dtype at the eager choice (#4794) - #4822
Merged
Merged
Conversation
#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.
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
AwqTorchLineardequantized its whole weight to FP64 inside every compiled graph._stored_decode_range_fits()returnedFalsefor every dtype undertorch.compiler.is_compiling()(or CUDA-graph capture), so_decode_dtype_for_activation()fell through both questions toreturn 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_keyreads.is_inference()/data_ptr()on the host, which dynamo rejects) but it answeredFalseinstead of the real answer. #4754 then keptSUPPORTS_COMPILE = Trueon 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_dtypeas plain Python bools. Dynamo guards on those values, so a change recompiles.clear_weight_cache(), which the base class already invokes onpost_init(), a device/dtype move (_apply), aload_state_dictand a training transition. That is the same contracttensor_cache_keydocuments; an in-place mutation of the scales still requires an explicitclear_weight_cache().precomputed.get(dtype, False)instead of a blanketFalse, so it never escalates to FP64 unless the eager answer did._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)replacesself.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
AwqTorchLinearnow 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
ruff check gptqmodel docs/eora tests setup.py(theformat/check.shgate): All checks passed.Red / green evidence
Reverting only the
torch_awq.pyhunk and keeping the new tests:test_awq_torch_compiled_decode_dtype_matches_eager[fits-0.001-3-2-*]assert torch.float64 is torch.float16...[weight-overflow-10000.0-7-0-*]assert torch.float64 is torch.float32...[scale-underflow-1e-08-1-0-*]assert torch.float64 is torch.float32test_awq_torch_compiled_decode_follows_the_scales[*]assert torch.float64 is torch.float32test_awq_torch_compiled_forward_is_bitwise_eager[*]assert torch.equal(actual, eager)test_awq_torch_compiled_decode_keeps_fp64_past_fp32[*](control)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_eagerruns a realtorch.compile(..., backend="eager", fullgraph=True)trace, so it exercises the compiled branch rather than a monkeypatchedis_compiling.Kernel Accuracy Requirements
Module level,
AwqTorchLinear2048x8192, fp16, M=2 decode, same host,torch.compiledefault backend:torch.float64torch.float16The 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 constantFalse; no stage, ordering, dispatch table or tensor ownership changes.Model Support / Module Plan
Not applicable — no
MODEL_MAPentry, definition or fusion config is touched.Review Requirements
tests/kernels/test_awq_torch.py).torch.compiler.is_compilingto reach the branch cheaply, and one test uses a realtorch.compiletrace.Notes
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.#4794also asked for a module-levelcompiled < eagertiming 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.