Repository navigation
fix(awq): keep checkpoint scales and bias out of the activation dtype (#4178) - #4247
Merged
Merged
Conversation
…#4178) AwqGEMMLinear._ensure_runtime_dtype replaced the registered scales and bias buffers with activation-dtype copies on every forward. One BF16 forward on an FP16 checkpoint narrowed 87.5% of the scales permanently, so a later FP16 forward drifted from 3.0e-4 to 1.7e-3 relative error and state_dict() no longer matched the checkpoint. Convert scales and bias into temporaries for the call, and stop post_init from narrowing the loaded buffers. The CPU int4pack transforms (TorchAten/TorchFused and their AWQ twins) also built their packed scales_and_zeros metadata by overwriting self.scales; keep the checkpoint buffer and build the metadata separately. A BF16 activation now keeps FP16 scale precision in the FP32 metadata instead of rounding it to BF16, and biased BF16 layers request the FP32 product so the bias is added before the single output cast. FP16 compute is unchanged. The _apply invalidation now keys off the activation dtype the packed layout was built for rather than scales.dtype, since the checkpoint buffer no longer tracks the compute dtype.
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.
Fixes #4178
Root cause
AwqGEMMLinear._ensure_runtime_dtype(gptqmodel/nn_modules/qlinear/gemm_awq.py:262-266before this change) replaced the registeredscalesandbiasbuffers with activation-dtype copies on every forward.post_initalso narrowed non-half buffers to FP16. One BF16 forward on an FP16 checkpoint therefore permanently rounded 87.5% of the scales and the bias to BF16; a later FP16 forward and any re-saved checkpoint used the narrowed values.The CPU int4pack transforms had the same defect for their packed affine metadata:
_cpu_int4pack_scale_dtypeselected the activation dtype andtransform_cpuassignedself.scales = self.scales.to(...), so every symmetric BF16 forward rewrote the checkpoint scale buffer. Biased BF16 layers also rounded the int4pack product to BF16 before adding the bias.Fix
AwqGEMMLinearnow converts scales/bias into per-call temporaries (_scales_for_compute/_bias_for_compute) and passes them toAwqGemmFn.post_initkeeps the loaded dtype. The registered buffers are never written by a forward.TorchAtenLinear,TorchFusedLinear,TorchAtenAwqLinear,TorchFusedAwqLinear) build their packedscales_and_zerosmetadata from a temporarycompute_scales;self.scaleskeeps the checkpoint dtype._cpu_int4pack_scale_dtypekeeps FP16 scale precision for BF16 activations: the fused CPU op already widens BF16 to FP32, so non-BF16-representable FP16 scales use FP32 metadata instead of being rounded to BF16. FP16 activations keep their historical FP16 compute dtype.out_dtype=torch.float32), so the bias is added before the single output cast, matching the existing FP16 behavior._applyinvalidation is keyed by_cpu_transform_dtype(the activation dtype the packed layout was built for) rather thanscales.dtype, because the checkpoint buffer no longer tracks the compute dtype.Before / after (issue repro, 2048x1024, g128, FP16 checkpoint)
Independent FP64 oracle that decodes the packed AWQ words itself:
scales/biasdtypebfloat16float16state_dict()scales bit-identical to checkpointstate_dict()bias bit-identical to checkpointSymmetric BF16 CPU int4pack (FP16 checkpoint scales,
2e-3oracle tolerance):TorchFusedAwqLinearrel errTorchAtenAwqLinearrel errbfloat16(mutated)float16(unchanged)Tests
New regression file
tests/kernels/test_awq_runtime_dtype_storage.py:state_dict()scales/bias stay bit-identical to the checkpoint (AwqGEMMLinearand the already-correctAwqTorchLinearguard).2e-3of the independent FP64 oracle.Fail-before (parent
origin/main, same test file):5 failed, 1 passed(theAwqTorchLinearguard passes as expected).Pass-after:
6 passed.Commands and results (CUDA venv,
OMP_NUM_THREADS=4, RTX 4090GPU-d458708b-c760-bbf1-08fb-8b8803a2157d):ROCm venv (
HIP_VISIBLE_DEVICES=2, R9700):The skip is the CUDA-only
AwqGEMMLinearcase.tests/test_kernel_audit_regressions.pyhas 21 pre-existing failures on this host (identical before and after this change; e.g.test_checkpoint_layout_gate_uses_discriminating_operands), andtests/test_inference_fp16_overflow.py::test_marlin_bias_cancels_before_the_fp16_cast[gptq]fails on the parent with the Marlinnum_groups must be > 1error. Neither is caused by this change.Risk / limitation
2.858e-3to1.631e-3. FP16 compute is unchanged.qzerosstill holds affine offsets (an existing packed-layout design); only the registeredscalescheckpoint buffer is preserved._applynow uses_cpu_transform_dtypeto decide when that layout must be re-finalized.