Skip to content

[BUG] GPTQModel.load(...).save(...) corrupts qzeros (every zero point shifted; sharded checkpoints become garbage) #4166

Closed

Description

@Qubitium

Severity: critical — silent model corruption
Version: 7.11.0 @ 6c3421c. Introduced with the qzeros_format buffer in #4134 (6f9ad69).

Summary

Re-saving a loaded GPTQ checkpoint with GPTQModel.save() rewrites every qzeros tensor with the wrong values. The output is still labelled as a valid checkpoint.

  • A gptq_v2 checkpoint gets +1 on every zero point.
  • A sharded gptq (v1) checkpoint is converted once per shard, so zero points drift by +5/+6.

Reproduce

Run on a CPU host where AUTO/TORCH resolves to TorchCpuAvx512Linear, which has REQUIRES_FORMAT_V2.

from gptqmodel import GPTQModel, BACKEND
m = GPTQModel.load("<sharded gptq v1 checkpoint, e.g. TinyLlama-1.1B-Chat-v1.0-GPTQ-4bit-sharded>",
                   backend=BACKEND.TORCH)
m.save("out")
# then compare qzeros between src and out (unpack 4-bit fields)
Source Result
Llama-3.2-1B-Instruct GPTQ v2 All 112 qzeros tensors changed; every nibble went 8 → 9 (0x88888888 → 0x99999999). Still labelled gptq_v2.
TinyLlama-1.1B GPTQ v1, sharded All 154 qzeros changed, 7 → 12/13. Reloading out: wikitext ppl ~1.4M vs 3.87 for the source; generation degenerates to "Comun Comun Comun…".
Single-file v1 (sym=False), AutoAWQ GEMM Byte-exact (unaffected)

Root cause

  1. models/writer.py:~2526 (get_model_with_quantize) calls load_checkpoint_in_model_then_tie_weights without a device_map.
  2. With device_map=None, accelerate 1.15 calls torch set_model_state_dict once per checkpoint file, so module.load_state_dict runs for the whole model on every shard.
  3. Keys missing from the current shard are filled from the module's own state, including the default qzeros_format=1 buffer.
  4. GPTQQuantLinear._load_from_state_dict → _restore_qzeros_format (nn_modules/qlinear/__init__.py:~1585-1670) reads the default marker as "incoming v1" and converts v1 → v2 in place again.
  5. The final write converts back only once.

Trace: set_model_state_dict <- load_checkpoint_in_model <- get_model_with_quantize with marker tensor(1) for modules whose qzeros were already converted.

Suggested fix

  • Make the conversion idempotent. Only convert when the incoming state dict actually contains the qzeros key for this module (prefix + "qzeros" in state_dict), and take the marker only from the same state dict, never from the module's default buffer.
  • And/or pass a device_map so accelerate loads each tensor once.
  • Add a regression test: load → save → byte-compare qzeros for v1 single-file, v1 sharded, and v2, with a REQUIRES_FORMAT_V2 kernel selected.

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

    Related qzeros_format side effects (HF save_pretrained extra keys, W4A re-save failure, Machete/Swordfish refusal gap): #4171. Separate write-side zero-offset bug for format=gptq_v2: #4167.

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