Repository navigation
fix(qlinear): dequantize to model compute dtype, not scales dtype (#4370) - #4403
Merged
Merged
Conversation
) GPTQ checkpoints restore FP16 scales even for a BF16 model, so deriving the dense dtype from scales.dtype produced FP16 Linear weights inside a BF16 model and the first forward raised a dtype mismatch. Resolve the target dtype from the model's non-quantized parameters/persistent buffers, with an explicit dtype override and the layer storage dtype as the last fallback. Apply the same resolution to TorchLinear, TorchFusedLinear, and TorchAtenLinear, and add a CPU regression that dequantizes a BF16 tiny GPTQ model and runs a forward.
This was referenced Nov 16, 2026
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 #4370.
Problem
GPTQ checkpoints always store FP16 scales, and #4184/#4327 restores every
BaseQuantLinear.scalesat its checkpoint dtype.dequantize_model()derived the dense target dtype fromscales.dtype, so a BF16 model produced FP16nn.Linearweights while norms/embeddings stayed BF16; the first forward then raisedRuntimeError: expected m1 and m2 to have the same dtype ... BFloat16 != Half. The same logic had been copied intotorch_fused.pyandtorch_aten_kernel.py.Fix
Added
resolve_dequantize_dtype()ingptqmodel/nn_modules/qlinear/__init__.pyand used it from all threedequantize_model()implementations:dtype=argument (new optional parameter);A BF16 model now dequantizes to BF16 dense layers; an FP16 model stays FP16.
dequantize_model()remains backward compatible (the new argument is optional).tests/models/test_non_w4a_lifecycle.pyhad encoded the old FP16-for-BF16 behavior and was already failing at HEAD; its expected weight/bias dtype now follows the parameterized model dtype instead of hard-coding FP16.Tests
tests/test_dequantize_model_compute_dtype_4370.py→ 10 passed; on pre-fix source 7 failed, 3 passed (all BF16 and explicit-dtype cases fail).tests/models/test_non_w4a_lifecycle.py→ 16 passed.tests/test_cpu_int4pack_dispatch_contracts.py→ 12 passed, 1 skipped.ruff checkon all changed files → clean.Residual risk
torch_int8.py'sdequantize_modelstill hardcodes FP16; the issue scoped the three Torch/TorchFused/TorchAten kernels, so it is untouched. A BF16 model dequantized throughBACKEND.GPTQ_TORCH_INT8can still produce FP16 dense layers.