Skip to content

fix(awq): keep checkpoint scales and bias out of the activation dtype (#4178) - #4247

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4178-awqgemm-dtype
Nov 16, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4178-awqgemm-dtype

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4178

Root cause

AwqGEMMLinear._ensure_runtime_dtype (gptqmodel/nn_modules/qlinear/gemm_awq.py:262-266 before this change) replaced the registered scales and bias buffers with activation-dtype copies on every forward. post_init also 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_dtype selected the activation dtype and transform_cpu assigned self.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

  • AwqGEMMLinear now converts scales/bias into per-call temporaries (_scales_for_compute/_bias_for_compute) and passes them to AwqGemmFn. post_init keeps the loaded dtype. The registered buffers are never written by a forward.
  • The CPU int4pack transforms (TorchAtenLinear, TorchFusedLinear, TorchAtenAwqLinear, TorchFusedAwqLinear) build their packed scales_and_zeros metadata from a temporary compute_scales; self.scales keeps the checkpoint dtype.
  • _cpu_int4pack_scale_dtype keeps 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.
  • Biased BF16 CPU int4pack layers now request the FP32 product (out_dtype=torch.float32), so the bias is added before the single output cast, matching the existing FP16 behavior.
  • _apply invalidation is keyed by _cpu_transform_dtype (the activation dtype the packed layout was built for) rather than scales.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:

Step Before After
FP16 forward rel err 3.050e-04 3.050e-04
After one BF16 forward: stored scales/bias dtype bfloat16 float16
FP16 forward rel err afterwards 1.748e-03 3.050e-04
Fraction of scales changed 87.50% 0.00%
state_dict() scales bit-identical to checkpoint False True
state_dict() bias bit-identical to checkpoint False True

Symmetric BF16 CPU int4pack (FP16 checkpoint scales, 2e-3 oracle tolerance):

Path Before After
TorchFusedAwqLinear rel err 2.858e-03 1.631e-03
TorchAtenAwqLinear rel err 2.858e-03 1.631e-03
Stored scales after forward bfloat16 (mutated) float16 (unchanged)

Tests

New regression file tests/kernels/test_awq_runtime_dtype_storage.py:

  • FP16 checkpoint -> one BF16 forward -> later FP16 forward still matches the FP64 oracle, and state_dict() scales/bias stay bit-identical to the checkpoint (AwqGEMMLinear and the already-correct AwqTorchLinear guard).
  • Symmetric BF16 CPU int4pack for both AWQ and GPTQ twins: stored scales unchanged and output within 2e-3 of the independent FP64 oracle.

Fail-before (parent origin/main, same test file): 5 failed, 1 passed (the AwqTorchLinear guard passes as expected).
Pass-after: 6 passed.

Commands and results (CUDA venv, OMP_NUM_THREADS=4, RTX 4090 GPU-d458708b-c760-bbf1-08fb-8b8803a2157d):

pytest tests/kernels/test_awq_runtime_dtype_storage.py -q
-> 6 passed

pytest tests/kernels/test_awq_runtime_dtype_storage.py tests/kernels/test_awq_torch.py \
  tests/kernels/test_awq_native_eval_autograd.py tests/test_torch_fused_asym_qzeros.py \
  tests/kernels/test_awq_cpu_fused_post_init.py tests/kernels/test_awq_cuda_fp32_reduce.py \
  tests/test_awq_pack_scale_representability.py tests/kernels/test_awq_torch_fused.py -q
-> 86 passed, 1 skipped

ruff check gptqmodel docs/eora tests setup.py
-> All checks passed!

ROCm venv (HIP_VISIBLE_DEVICES=2, R9700):

pytest tests/kernels/test_awq_runtime_dtype_storage.py tests/test_torch_fused_asym_qzeros.py \
  tests/kernels/test_awq_torch.py -q
-> 29 passed, 1 skipped

The skip is the CUDA-only AwqGEMMLinear case. tests/test_kernel_audit_regressions.py has 21 pre-existing failures on this host (identical before and after this change; e.g. test_checkpoint_layout_gate_uses_discriminating_operands), and tests/test_inference_fp16_overflow.py::test_marlin_bias_cancels_before_the_fp16_cast[gptq] fails on the parent with the Marlin num_groups must be > 1 error. Neither is caused by this change.

Risk / limitation

  • BF16 numerics change for existing CPU int4pack users: the product now stays FP32 until the bias add, and FP16 checkpoint scales are no longer rounded to BF16 for BF16 activations. This is the intended accuracy fix; measured AWQ symmetric BF16 error drops from 2.858e-3 to 1.631e-3. FP16 compute is unchanged.
  • The packed runtime qzeros still holds affine offsets (an existing packed-layout design); only the registered scales checkpoint buffer is preserved. _apply now uses _cpu_transform_dtype to decide when that layout must be re-finalized.

…#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.
@Qubitium
Qubitium merged commit af465d4 into main Nov 16, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4178-awqgemm-dtype branch November 16, 2026 05:53
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] AwqGEMMLinear permanently rewrites checkpoint scales/bias to the activation dtype

1 participant