Repository navigation
Description
Summary
#4134 (commit 6f9ad69, for #4125) registers a persistent qzeros_format buffer on every PackedGroupedQuantLinear (GPTQ and AWQ), and _write_state_dict_layout_marker always writes it. GPTQModel.save() skips this key on purpose (get_state_dict_for_save), but HF save_pretrained() / push_to_hub does not, so the two save paths produce different checkpoints.
Code
GPTQModel/gptqmodel/nn_modules/qlinear/__init__.py
Lines 1078 to 1168 in 6c3421c
| # Persistent layout marker. A state dict's ``_metadata`` is not a | |
| # tensor, so safetensors and plain dict copies drop it; this buffer | |
| # keeps the v1/v2 (or runtime-layout) code inside the payload. | |
| # The initial value must be a valid checkpoint code: an export shell is | |
| # built and then filled by accelerate, which round-trips the module's | |
| # own ``state_dict()`` before any save hook runs. ``0`` is reserved for | |
| # "kernel-private runtime layout", so a placeholder ``0`` would make | |
| # that internal load look like a corrupt payload. | |
| self.register_buffer( | |
| QZEROS_FORMAT_BUFFER, | |
| t.ones((), dtype=t.int64), | |
| persistent=True, | |
| ) | |
| def _emitted_layout_code(self) -> int: | |
| """Value written to the ``qzeros_format`` buffer for this state dict. | |
| ``0`` marks a kernel-private runtime layout that has no inverse. The | |
| code lets a reload fail loudly even when the caller saved the tensors | |
| without going through a writer that refuses. AWQ has no v1/v2 | |
| convention, so its checkpoint words are always ``2`` (logical). | |
| """ | |
| if self._is_runtime_layout(): | |
| return 0 | |
| return 2 | |
| def _is_runtime_layout(self) -> bool: | |
| """Whether the packed buffers hold a kernel-private runtime layout. | |
| Marlin, Machete, Swordfish, BitBLAS and ExLlamaV2 rewrite `qweight` | |
| (and often `qzeros`/`g_idx`) in place into a layout that has no inverse. | |
| Such a module can serve inference and read-only `state_dict()` callers, | |
| but it is not a checkpoint. | |
| """ | |
| return any( | |
| bool(getattr(self, flag, False)) | |
| for flag in ("_marlin_repacked", "_native_shuffled", "_awq_repacked") | |
| ) | |
| def _write_fused_train_state_snapshot(self, destination, prefix: str, keep_vars: bool) -> None: | |
| """Emit the checkpoint-layout tensors captured before a CPU transform. | |
| The CPU int4pack kernels (GPTQ and AWQ alike) replace `qweight` and | |
| `qzeros` in place with int4pack words and float affine offsets. Those | |
| runtime operands are not a checkpoint: the integer v1/v2 conversion | |
| turns float offsets into `inf`/`65536`, and the int4pack `qweight` has a | |
| different shape. The snapshot taken by `capture_fused_train_state` | |
| still holds the checkpoint tensors, so serialization emits those | |
| instead of the transformed buffers. | |
| A modified runtime layout has no inverse snapshot; exporting it must | |
| fail closed rather than write garbage. | |
| """ | |
| snapshot = getattr(self, "_fused_train_state", None) | |
| if not snapshot: | |
| return | |
| if not getattr(self, "_fused_train_state_exportable", True): | |
| raise RuntimeError( | |
| f"{self.__class__.__name__} holds a modified CPU int4pack runtime state " | |
| "that cannot be converted back to the checkpoint layout. Load " | |
| "checkpoint-layout weights before saving." | |
| ) | |
| for name, buffer_name in snapshot.items(): | |
| tensor = self._buffers.get(buffer_name) | |
| if isinstance(tensor, t.Tensor): | |
| destination[prefix + name] = tensor if keep_vars else tensor.detach() | |
| def _save_to_state_dict(self, destination, prefix, keep_vars): | |
| super()._save_to_state_dict(destination, prefix, keep_vars) | |
| self._write_fused_train_state_snapshot(destination, prefix, keep_vars) | |
| self._write_state_dict_layout_marker(destination, prefix, keep_vars) | |
| def _write_state_dict_layout_marker(self, destination, prefix: str, keep_vars: bool) -> None: | |
| """Emit the layout code as a tensor so every transport keeps it. | |
| A per-module ``_metadata`` entry is free but is not a tensor: a | |
| safetensors round trip (Trainer/accelerate) or a plain | |
| ``{k: v for k, v in sd.items()}`` copy drops it, and the receiving | |
| module then reads v2 words as v1. The buffer is a registered persistent | |
| buffer, which is what lets ``load_state_dict`` consume it instead of | |
| reporting an unexpected key. | |
| """ | |
| marker = self._buffers.get(QZEROS_FORMAT_BUFFER) | |
| if not isinstance(marker, t.Tensor): | |
| return | |
| value = marker if keep_vars else marker.detach() | |
| destination[prefix + QZEROS_FORMAT_BUFFER] = value.clone().fill_( |
Trigger
model.model.save_pretrained(dir) (or push_to_hub) on a GPTQ/AWQ-quantized model.
Expected vs actual
- Expected: same tensor set as
GPTQModel.save(), no*.qzeros_formatkeys. - Actual: one extra int64
<layer>.qzeros_formattensor per quantized linear. The marker is written even insidecheckpoint_serialization().
Impact
Published checkpoints contain keys that config.json and the GPTQ/AWQ format do not describe. Strict third-party loaders (vLLM/SGLang load_weights, older GPT-QModel) may reject or warn on them. Reloading in current GPT-QModel works, so in-repo round-trip tests do not catch it.
Evidence (CPU, commit 6c3421c)
Tiny Llama, RTNConfig(bits=4, group_size=128), TorchLinear:
save() keys: 63 qzeros_format keys: 0
save_pretrained() keys: 77 qzeros_format keys: 14
extra in save_pretrained: ['model.layers.0.mlp.down_proj.qzeros_format', 'model.layers.0.mlp.gate_proj.qzeros_format', ...]
config checkpoint_format: gptq
Validation limits
CPU-only. Tolerance of each third-party loader to the extra keys was not tested.
Suggested fix
Skip the marker in _write_state_dict_layout_marker when is_checkpoint_serialization() is true (reload layout already comes from config.json checkpoint_format). Add a test asserting the save_pretrained key set matches GPTQModel.save() and has no *.qzeros_format keys.
Activity
fix(checkpoint): stop HF save_pretrained from writing qzeros_format m…