Skip to content

fix(qlinear): dequantize to model compute dtype, not scales dtype (#4370) - #4403

Merged
Qubitium merged 1 commit into
mainfrom
fix/dequantize-dtype-4370
Nov 16, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/dequantize-dtype-4370

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4370.

Problem

GPTQ checkpoints always store FP16 scales, and #4184/#4327 restores every BaseQuantLinear.scales at its checkpoint dtype. dequantize_model() derived the dense target dtype from scales.dtype, so a BF16 model produced FP16 nn.Linear weights while norms/embeddings stayed BF16; the first forward then raised RuntimeError: expected m1 and m2 to have the same dtype ... BFloat16 != Half. The same logic had been copied into torch_fused.py and torch_aten_kernel.py.

Fix

Added resolve_dequantize_dtype() in gptqmodel/nn_modules/qlinear/__init__.py and used it from all three dequantize_model() implementations:

  1. explicit dtype= argument (new optional parameter);
  2. the dtype of the model's non-quantized floating parameters/persistent buffers (embeddings, norms, lm_head) — preferring FP16/BF16 over an incidental FP32 buffer;
  3. the layer's bias/scales storage dtype when the model owns no non-quantized tensors (bare quantized layer);
  4. FP16 as the last resort.

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.py had 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

  • New 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.
  • Combined run → 38 passed, 1 skipped.
  • ruff check on all changed files → clean.

Residual risk

  • torch_int8.py's dequantize_model still hardcodes FP16; the issue scoped the three Torch/TorchFused/TorchAten kernels, so it is untouched. A BF16 model dequantized through BACKEND.GPTQ_TORCH_INT8 can still produce FP16 dense layers.
  • Dtype inference relies on non-quantized parameters/persistent buffers. A model with only quantized layers falls back to bias/scales; genuinely mixed-precision models prefer FP16/BF16 over FP32.
  • CPU-only validation; this is dtype selection, not kernel math.

)

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.
@Qubitium
Qubitium merged commit 266a448 into main Nov 16, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/dequantize-dtype-4370 branch November 16, 2026 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant