Skip to content

[BUG] Quantizing with format=FORMAT.GPTQ_V2 saves zero points +1 (model accuracy destroyed) #4167

Closed

Description

@Qubitium

Severity: high — silent corruption on the write path
Version: 7.11.0 @ 6c3421c

Summary

quantize() then save() with format=gptq_v2 writes qzeros that are one step too high. Loading existing v2 checkpoints is fine; only checkpoints written this way are affected.

Reproduce

from gptqmodel import GPTQModel, QuantizeConfig, BACKEND
from gptqmodel.quantization import FORMAT
for fmt in (FORMAT.GPTQ, FORMAT.GPTQ_V2):
    cfg = QuantizeConfig(bits=4, group_size=128, sym=False, desc_act=False, format=fmt)
    m = GPTQModel.load("Qwen/Qwen2.5-0.5B-Instruct", cfg)
    m.quantize(calib, batch_size=1); m.save(f"q_{fmt}")
    m2 = GPTQModel.load(f"q_{fmt}", backend=BACKEND.GPTQ_TORCH)
    # NLL on a short prompt
Format NLL after save → reload
gptq 1.488
gptq_v2 16.679

Unit level, packing with pack_module(..., quantize_config=QuantizeConfig(format=GPTQ_V2)) into TorchLinear:

  • saved zeros equal z+1 with marker 2;
  • reloading and running forward gives relative error 0.945 (vs 3.4e-4 with FORMAT.GPTQ).

Root cause

  • utils/model.py:1386-1403: after packing, module.qzero_format(format=2) is set only when resolve_quant_format(...) == FORMAT.GPTQ. For GPTQ_V2 the module keeps _qzeros_format = 1 from __init__ (nn_modules/qlinear/__init__.py:1327), even though the packer wrote logical (v2) zeros.
  • checkpoint_layout_qzeros() (__init__.py:~1549) and the writer's _checkpoint_layout_buffer (utils/model.py:~2562) then treat the logical zeros as v1 and add 1.

Related: the raw pack() / pack_original / pack_block / pack_gpu methods never set qzero_format(2). A direct pack() caller gets logical words labelled v1 (marker 1), which reload one step low. hf_pack_layer already handles this correctly.

Suggested fix

  • After packing, always record that the live buffer holds logical zeros (qzero_format(2)), whatever the export format.
  • Let checkpoint_layout_qzeros() convert only when the target format is v1.
  • Add an e2e test for format=gptq_v2 quantize → save → reload.

Found during an AI-assisted review (Claude Code) of 6c3421c; repros were run on RTX 4090/5090 (CUDA 13) and Radeon AI PRO R9700 (ROCm) unless marked static/suspected.

Activity

  1. added
    bug
    on Nov 16, 2026
  2. Qubitium commented on Nov 16, 2026

    @Qubitium
    CollaboratorAuthor

    Second user-facing path with the same root cause, found at 9dad249: resuming staged GSQ from its per-layer checkpoint shifts every zero point by +1 (silent corruption on resume).

    • quantization/gsq_training.py:~1365-1368 calls TorchLinear.pack_original(...). That writes logical (v2) zero words but leaves _qzeros_format=1.
    • So the layer checkpoint (looper/gsq_training_model.py:~468) stores qzeros_format=1 next to v2 words.
    • On resume, _restore_packed_layer (gsq_training_model.py:~109-127) runs load_state_dict. That triggers the v1→v2 conversion (_restore_qzeros_format) and adds 1 to every zero.
    import copy, tempfile, torch
    from pathlib import Path
    from transformers import LlamaConfig, LlamaForCausalLM
    from gptqmodel.looper import gsq_training_model as staged
    torch.manual_seed(7)
    cfg = LlamaConfig(vocab_size=128, hidden_size=64, intermediate_size=128, num_attention_heads=4, num_key_value_heads=4, num_hidden_layers=1)
    orig = LlamaForCausalLM(cfg).half().eval(); dense = copy.deepcopy(orig); resumed = copy.deepcopy(orig)
    d = Path(tempfile.mkdtemp()); docs = [{"input_ids": list(range(1, 17))}]
    staged.quantize_llama_gsq_model(orig, docs, bits=3, group_size=32, checkpoint_directory=d)
    staged.quantize_llama_gsq_model(resumed, docs, bits=3, group_size=32, checkpoint_directory=d)   # resumes from layer-00.pt
    ids = torch.tensor([[1, 2, 3, 4, 5, 6, 7, 8]])
    with torch.no_grad(): ld, lo, lr = (m(ids, use_cache=False).logits.float() for m in (dense, orig, resumed))
    print((lo - ld).abs().max(), (lr - ld).abs().max())   # 0.024 vs 0.757 -- only qzeros differ (logical 4 -> 5)

    At HEAD, these repo tests fail from this root cause:

    • test_gsq_staged_public.py::test_staged_llama_resumes_identical_packed_block[*]
    • test_gsq_distributed_model.py::test_distributed_model_capture_checkpoint_and_resume[*]
    • test_gsq_distributed_failures.py (3 tests)
    • test_rco_paper.py::test_projection_materialization_packed_bytes_and_save_reload
    • test_torch.py::test_torch_quant_embeddings_pack_original_handles_unaligned_vocab (diff exactly 1.0)
    • test_pack_tails.py::test_partial_words_checkpoint_round_trip[*] (the pack_module(GPTQ_V2) case)

    Setting qzero_format(2) inside pack_original (and the other raw packers) would fix all of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions