Repository navigation
fix(awq): keep checkpoint scales and bias precision in Torch forward - #3800
Conversation
`AwqTorchLinear.post_init` and `_ensure_runtime_dtype` rewrote `scales` and `bias` in place with the activation dtype. The rewrite was permanent: a BF16 scale below the FP16 subnormal range became zero and a later BF16 forward stayed zero, and a stored FP32 bias was narrowed before the reduction, so a finite cancellation became `-inf`/`NaN` in FP16 or a rounded `0` in BF16. Convert the scales for dequantization and the bias for the epilogue as temporary operands only. `matmul_fp32_reduce` now receives the stored bias, so a wide bias is added after the wide reduction and rounded once. Add FP64-oracle regressions for the wide-bias cancellation and the BF16-to-FP16-to-BF16 scale case, and document the flow.
|
Review completed. I found two precision-loss boundaries beyond the original forward fix and pushed corrections in
I checked actual safetensors JSON headers with HTTP byte-range reads (no weight payloads). These public W4A16 checkpoints do not support a general assumption that bias or scales are FP32:
For comparison, the unquantized Qwen2-1.5B-Instruct checkpoint stores its sampled The code audit also found backend-specific contracts: AWQ GEMM/GEMV allocate or pack FP16 operands, while FP8 and VPTQ explicitly allocate FP32 bias. Native AWQ kernels cannot be made FP32-safe by a blanket buffer change. This PR preserves any higher precision source operands in the Torch AWQ path; other backends need their own kernel and loader validation before claiming that support. Verification: focused Torch AWQ and packing tests 13 passed on the current NVIDIA host, including an independent packed-code FP64 oracle and checkpoint reload; FP8/GGUF loader regressions 38 passed; Ruff workflow is green. Four FP8 native CUDA tests fail identically on the untouched PR head in this local runtime, and two native ParoQuant tests are blocked by local |
Qubitium
left a comment
There was a problem hiding this comment.
Reviewed and verified the Torch AWQ precision fix, including pack and checkpoint-load regressions. Detailed findings and public checkpoint dtype audit are in the PR discussion.
|
Reviewed after the merge (
Minor: the BF16 row in the failure table is inaccurate for the |
TL;DR
AwqTorchLinearpermanently rewrote its storedscalesandbiaswith thecurrent activation dtype. Convert those operands temporarily instead, and pass
the stored bias to the existing wide epilogue. Packing and checkpoint loading
also preserve source operand dtypes. Net change: 5 files,
+370/−22, no new public API.
1. The problem
post_init()narrowed any non-halfscales/biasto FP16, and_ensure_runtime_dtype()replaced both buffers in place on every forward whoseactivation dtype differed. The converted values were stored back into the
registered buffers, so the checkpoint precision was lost for all later
forwards and for a re-saved checkpoint.
flowchart TD A["AwqTorchLinear.forward"] --> B["_ensure_runtime_dtype"] B --> C["self.scales = self.scales.to(device, dtype)"] B --> D["self.bias = self.bias.to(device, dtype)"] C --> E["dequantize_gemm reads the mutated scales"] D --> F["matmul_fp32_reduce sees a bias already narrowed"] F --> G["wide-bias path cannot select"] E --> H["output"] G --> H C -. "permanent" .-> X["stored BF16 scale below FP16 range becomes 0"] D -. "permanent" .-> Y["stored FP32 bias becomes -inf or rounds"] classDef failure fill:#fce8e6,stroke:#a33,color:#111 class X,Y failurepost_initscales/biasto FP16torch_awq.py:89-94before this changescales/biasin placetorch_awq.py:145-150before this change_ensure_runtime_dtype(device, compute_dtype)torch_awq.py:279before this changematmul_fp32_reduce(..., bias=self.bias)after narrowingtorch_awq.py:288before this changeutils/model.py:2805before this changepackcast FP32 source scales and bias to FP16torch_awq.py:122-126before this changeObserved on revision
e17a1a29b, with an independent FP64 Torch oracle:-65535cancelling a65536reduction, FP161-inf101e-40, FP16 forward, then BF16 forward1.469e-390(scale stored as0)post_initThis contradicts the documented Torch AWQ contract in
docs/cuda-quantized-bias-epilogue.md: a checkpoint bias stored in FP32 isadded after a wide reduction and rounded once.
2. The new flow
flowchart TD L["load_checkpoint_in_model_then_tie_weights"] --> M["Accelerate model-dtype cast"] M --> N["_restore_checkpoint_buffers: restore Torch AWQ source dtype"] N -- "source present and shape matches" --> A["AwqTorchLinear.forward"] N -- "missing or mismatched source" --> X["RuntimeError"] P["AwqTorchLinear.pack: keep source scales and bias dtypes"] --> A A --> B["_scales_for_compute: temporary activation-dtype copy"] B --> C["dequantize_gemm with the temporary scales"] C --> D["temporary dequantized weight in activation dtype"] D --> E["_bias_for_compute: move/contiguous only, keep stored dtype"] E --> F{"matmul_fp32_reduce"} F -- "bias wider or incomparable" --> G["linear_wide_bias: wide reduction, bias added in FP32"] F -- "same dtype" --> H["FP32-output addmm with bias"] G --> I["single cast to activation dtype"] H --> I I --> J["adapter, reshape, return"] C -. "stored scales/bias unchanged" .-> K["checkpoint precision retained"] D -. "released after matmul" .-> K classDef failure fill:#fce8e6,stroke:#a33,color:#111 classDef stored fill:#e6f4ea,stroke:#137333,color:#111 class X failure class K stored_scales_for_computereturns the stored tensor unchanged when it alreadymatches the activation device and dtype, so the common FP16/BF16 path adds no
allocation.
_bias_for_computenever casts;matmul_fp32_reduceselectslinear_wide_biaswhen the bias cannot be represented exactly in theactivation dtype, adds it to the FP32 reduction, and rounds once.
load_checkpoint_in_model_then_tie_weightsrestores sourcescales and bias after Accelerate's model-dtype cast, and
packretains thesource dtypes when producing a Torch AWQ checkpoint.
3. Failure and ambiguity contract
0; BF16 output0-inf; output-inforNaN1-65536; output01post_init-65535loaded with FP16 model dtype-inf-65535pack-inf-655351e-40passed topack0The native
AwqGEMMLinearCUDA kernel has its own activation-dtype operandcontract and is not changed here.
4. Change inventory
gptqmodel/nn_modules/qlinear/torch_awq.pypack; add_scales_for_compute/_bias_for_computegptqmodel/utils/model.pytests/kernels/test_awq_torch.pydocs/awq-runtime-dtype-precision.mdREADME.md5. Tests
test_awq_torch_wide_checkpoint_bias_rounds_after_wide_accumulationtest_awq_torch_forward_preserves_checkpoint_scales_and_biastest_awq_torch_pack_keeps_fp32_source_bias/test_awq_torch_pack_keeps_fp32_source_scalestest_awq_torch_loader_preserves_checkpoint_operand_dtypesThe original forward regressions fail on
e17a1a29band pass with this change. The existingtests/kernels/test_awq_torch.py::test_awq_torch_matches_manual_dequantcontinues to pass for FP16 and BF16. The new oracle decodes the packed INT4
words independently in FP64.
6. Validation
Commands (from the repository root):
The focused AWQ/ParoQuant selection completed with 597 passed. Three
tests/test_awq_weight_mean.pycases and twotests/test_paroquant.pyrotation/autotune cases also fail on unmodified
main; they are unrelated tothis change and reproduce on
e17a1a29b.Numerical results were reproduced on NVIDIA CUDA and AMD ROCm devices with the
same values in the table above. No expanded or dequantized weight is retained:
the temporary weight is released after the matmul, and only packed weights,
scales, zeros, and bias persist.
Review follow-up on the current NVIDIA CUDA host: the focused Torch AWQ and
packing selection passed 13 tests; FP8/GGUF checkpoint loader regressions
passed 38 tests; Ruff passed. Four FP8 native CUDA cases fail in this local
runtime on both the untouched PR head and this revision. Two ParoQuant native
cases cannot run because local
nvccfails to produce the AWQ extension.7. Reviewer notes
post_init()no longer normalizes a non-half checkpoint to FP16. A modulewhose
scalesare FP32 is still served for FP16/BF16 activations; fusioneligibility checks read the stored dtype and remain fail-closed.
_runtime_scales/_runtime_bias, which are existingParoLinearcache attributes.activation-dtype operand contract.
biases in FP16, so FP32 is not assumed for every W4A16 model. The Torch AWQ
path nevertheless preserves FP32 when a source or checkpoint provides it.