Repository navigation
[BUG] Follow-up on #4061 + HF interop: load_state_dict(state_dict()) corrupts GPTQ v1 models (PPL 9.8 -> 1.9e6, regression), partial/PEFT load flips qzeros format -> garbage re-save, RTN gemm writes quant_method gptq, GGUF ckpt loads in HF as random weights, torch.save(model) PicklingError #4070
Description
Audit of main @ ad0525c, RTX 4090. Ecosystem paths covered: PyTorch state_dict, PEFT, HF transformers loading and pickling. Items 1 and 2 are regressions from #4061 (for #4053).
1. load_state_dict(model.state_dict()) silently corrupts every GPTQ v1 model on the Torch/Triton kernels (high, regression from #4061)
#4061 added _save_to_state_dict → _write_checkpoint_layout_qzeros (nn_modules/qlinear/__init__.py:1246-1297), so state_dict() now emits v1 qzeros (0x77777777). _load_from_state_dict copies those v1 words into the live buffer and sets _qzeros_format=1, but never converts them back to v2. TorchLinear / TritonV2 forward always decodes v2, so every zero point is off by one. strict=True reports all keys matched, so there's no warning.
Llama-3.2-1B GPTQ 4-bit g128, backend gptq_triton (gptq_torch same)
fresh load ppl/next: 9.82 ' Berlin'
load_state_dict(sd) <All keys matched successfully>
after reload ppl/next: 1856980.9 'imid'
same script on 2c032e0^ (pre-#4061): 9.918 ' Berlin'
This breaks the standard torch.save(m.state_dict()) / load_state_dict pattern, Trainer/accelerate checkpoint restore, and weight copies between loaded models.
Fix: in _load_from_state_dict, when the incoming words are in the checkpoint v1 layout and the kernel runs on v2, convert in place (convert_gptq_v1_to_v2_format_module) and keep the format at 2.
Test gap: tests/test_checkpoint_interop_4053.py only checks the save side; add a load_state_dict(state_dict()) identity test.
2. A partial load_state_dict (e.g. PeftModel.from_pretrained) flips _qzeros_format to 1 while the words stay v2, so the next save is garbage (medium-high, #4061)
qlinear/__init__.py:1283-1297 resets _qzeros_format from self.format even when qzeros is not among the loaded keys. After that, _write_checkpoint_layout_qzeros sees format 1 and skips the conversion, so v2 words are written labelled v1. As a side effect, the fused Triton dequant gate (torch.py:617, qzero_format()==2) turns off after any adapter load.
real PEFT adapter load: _qzeros_format 2 -> 1, state_dict qzeros word still v2 (-2004318072)
orig 9.821 | after load_state_dict({}, strict=False) 9.918 | save_pretrained -> reload: 613770.5 ' discipline'
Fix: only update _qzeros_format when prefix + "qzeros" is among the loaded keys.
3. RTNConfig(format="gemm") (AWQ layout) writes quant_method: "gptq", so HF from_pretrained raises (medium)
quantization/config.py:5164 writes QUANT_METHOD_FIELD: self.method rather than self.export_quant_method(), while RTN_EXPORT_FORMATS advertises GEMM/GEMV/GEMV_FAST/LLM_AWQ. The resulting config has format: gemm with quant_method: gptq:
GPTQModel.load: OK (AwqMarlin)
HF from_pretrained: ValueError: Unsupported format: FORMAT.GEMM for quantization method METHOD.GPTQ
quant_method edited to "awq": HF output == GPTQModel output (max diff 0.0)
vLLM/SGLang would route it as GPTQ too (not tested). Fix: serialize export_quant_method().
4. GPTQModel GGUF checkpoints load in transformers 5.22 as random weights, without an error (medium)
GPTQModel writes quant_method: "gguf" to config.json, but that name belongs to HF's own GgufHfQuantizer, which expects a .gguf file. HF reports all 168 linear .weight as MISSING (randomly initialized) and qweight as UNEXPECTED, then returns the model:
GGUF q4_k_m Qwen2.5-0.5B: HF vs GPTQModel logits max diff 25.6, argmax agreement 0.0, next token ' Berlin' vs '坞'
Fix: use a non-colliding quant_method for GPTQModel's packed GGUF tensors, or warn/refuse HF compatibility at save time and document it.
5. torch.save(model) raises PicklingError for TorchLinear/TritonV2-backed models (low)
nn_modules/qlinear/torch.py:393: optimize() stores self.dequantize_weight = torch_compile(<bound method>) on the instance, and the loader calls it automatically:
PicklingError: Can't pickle <function TorchLinear.dequantize_weight>: it's not the same object as ...
Marlin-backed models pickle fine. Fix: keep the compiled callable in a private, non-pickled attribute, or add __getstate__.
Checked, clean
- Autograd: gradients match an fp32 dense-dequant reference for gptq_torch/triton, awq_gemm/gemm_triton/torch, fp8_torch, gguf_torch and the CPU fused/aten/avx512 kernels.
- PEFT LoRA: gradients are correct with gradient checkpointing, fp16 and bf16.
- Inference-only kernels:
.train()raises clearly, and PEFT merge onto a quantized base raises. - HF loading: GPTQ v1/v2 and AWQ gemm load bitwise equal to
GPTQModel.load. - Saving: tokenizer files, chat template, generation_config and sharded index are preserved.
- Multi-model / threaded use: two models in one process, threaded forwards,
deepcopyand dtype casts all work.
Plausible, not verified
- FP8 checkpoints write
quant_method: "fp8"with a 1-D per-rowweight_scale_inv(divide semantics) and noweight_block_size/activation_scheme. HF parses that as FineGrainedFP8 with a default 128×128 block and multiply semantics. Here HF crashed earlier (missingkernelspackage); with it installed, the output is likely garbage.
🤖 Generated with Claude Code
Activity
fix(checkpoint): repair v1 state_dict loads, HF quant methods, pickli…