Repository navigation
[BUG] HF bridge: quantizing through transformers with GPTQConfig(backend="marlin") / "exllama_v2" calibrates every layer and then crashes at pack ("cannot pack quantized weights"), because hf_select_quant_linear_v2(pack=True) returns the inference-only kernel #6191
Description
Summary
backend is a normal GPTQConfig field: transformers lists it as a loading attribute and stores it in the saved quantization_config. When a user quantizes a dense model through transformers and asks for Marlin (or ExLlamaV2) for inference:
cfg = GPTQConfig(bits=4, dataset=texts, tokenizer=tok, backend="marlin")
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct", quantization_config=cfg, device_map="cuda:0", dtype=torch.float16)Optimum runs the whole GPTQ calibration (every decoder layer) and then calls GPT-QModel's public bridge hf_select_quant_linear_v2(..., backend="marlin", pack=True) to choose the packing class. GPT-QModel resolves the explicit backend straight to MarlinLinear, which has no pack API, and raises:
ValueError: Selected backend `BACKEND.MARLIN` with kernel `MarlinLinear` cannot pack quantized weights for format `FORMAT.GPTQ_V2`.
All calibration work is lost, nothing is saved, and the error gives no hint that the HF flow has no separate "packing backend". backend="exllama_v2" fails the same way. backend="torch"/"auto" work, and so does reloading the result with GPTQConfig(backend="marlin"). The HF user has a single backend field that has to mean both "inference kernel" and "packing kernel", so there is no way to quantize with transformers and get Marlin in the same session.
GPT-QModel's own API had the same problem and #6154 fixed it (#6162): an inherited inference-only backend now packs with the method default and warns. The HF bridge entry point that Optimum calls was not changed, so the transformers path still fails, and only after the full calibration has finished.
Introduced by
Pre-existing. The pack / _supports_pack_api gate in select_quant_linear dates from 96ff08b (#2456), and hf_select_quant_linear_v2 forwards the HF backend and pack=True to it unchanged. Still present at HEAD 9123364.
Location
gptqmodel/utils/importer.py:1386 (hf_select_quant_linear_v2) passes the user's backend through:
backend = normalize_backend(backend, quant_method=quant_method)
...
return select_quant_linear(..., backend=backend, ..., pack=pack, ...)gptqmodel/utils/importer.py:1836-1840 (select_quant_linear, explicit-backend branch):
if pack:
if not _supports_pack_api(qlinear):
raise ValueError(
f"Selected backend `{backend}` with kernel `{qlinear.__name__}` cannot pack quantized weights for format `{format}`."
)Caller: optimum 2.3.0 GPTQQuantizer.pack_model() -> select_quant_linear(device_map, pack=True) -> hf_select_quant_linear_v2(..., backend=self.backend, pack=True). This runs after quantize_model has calibrated all blocks.
Reproduction
I ran this with gptqmodel at 9123364, transformers 5.23.0, optimum 2.3.0, torch 2.15 on one RTX 5090:
import torch
from datasets import load_dataset
from transformers import AutoModelForCausalLM, AutoTokenizer, GPTQConfig
mid = "Qwen/Qwen2.5-0.5B-Instruct"
tok = AutoTokenizer.from_pretrained(mid)
texts = [t for t in load_dataset("Salesforce/wikitext", "wikitext-2-raw-v1", split="train")["text"] if len(t) > 200][:128]
for backend in ["torch", "marlin", "exllama_v2"]:
cfg = GPTQConfig(bits=4, group_size=128, dataset=texts, tokenizer=tok, backend=backend)
try:
m = AutoModelForCausalLM.from_pretrained(mid, quantization_config=cfg, device_map="cuda:0", dtype=torch.float16)
print(backend, "OK", type(m.model.layers[0].self_attn.q_proj).__name__)
except Exception as e:
print(backend, "FAIL", e)Output (the quantization progress bars for all 24 blocks finish before each failure):
torch OK TorchLinear
marlin FAIL Selected backend `BACKEND.MARLIN` with kernel `MarlinLinear` cannot pack quantized weights for format `FORMAT.GPTQ_V2`.
exllama_v2 FAIL Selected backend `BACKEND.GPTQ_EXLLAMA_V2` with kernel `ExllamaV2Linear` cannot pack quantized weights for format `FORMAT.GPTQ_V2`.
Traceback tail:
transformers/quantizers/quantizer_gptq.py:103 _process_model_after_weight_loading
optimum/gptq/quantizer.py:591 quantize_model
optimum/gptq/quantizer.py:651 pack_model
optimum/gptq/quantizer.py:200 select_quant_linear
gptqmodel/utils/importer.py:1434 hf_select_quant_linear_v2
gptqmodel/utils/importer.py:1838 select_quant_linear
ValueError: Selected backend `BACKEND.MARLIN` with kernel `MarlinLinear` cannot pack quantized weights for format `FORMAT.GPTQ_V2`.
With the default backend, the same flow quantizes, saves, and reloads with Marlin correctly (wikitext2 PPL 20.26 in-session and after reload). So the only problem is the pack-time kernel choice.
Expected vs actual
- Expected:
GPTQConfig(backend="marlin")quantization through transformers succeeds. Packing should use a packable kernel for the same format (the method default, as fix(quantize): do not pack with an inherited inference-only backend (#6154) #6162 now does forGPTQModel.quantize), and Marlin should still be used for inference, either in-session after repack or at the latest on reload, since the saved config keepsbackend: "marlin". If this combination is meant to be rejected, it should be rejected before calibration starts. - Actual: calibration runs to completion and then a
ValueErrorthrows all of the work away.
Suggested fix
In hf_select_quant_linear_v2, when pack=True and the requested backend resolves to a kernel without a pack API, select the packing class as AUTO would for that format/bits/group_size/sym/desc_act (and warn once), as _backend_can_pack / inherited_quantize_backend already do for the native quantize() path. Optimum then packs with that class; post-init or the reload picks Marlin for inference. Alternatively, validate the backend's pack capability when the GPTQ quantizer is constructed (before calibration) so the failure is immediate.
Activity
fix(hf): packable kernel selection and legacy floor-width qzeros in t…
fix(hf): packable kernel selection and legacy floor-width qzeros in t…
fix(hf): consolidate packable selection, per-layer shape dispatch and…
fix(hf): packable selection, per-layer shape dispatch and legacy qzer…