Skip to content

[Audit][offload] Forced disk offload converts GPTQ zero points twice to build an unused size estimate #5850

Closed

Description

@Qubitium

Observed behavior

Forced disk offload builds a full exported state dict to compute the byte count even though force=True bypasses the size check. The first state dict's tensor values are retained while the writer builds a second state dict. For GPTQ v1 output from a Torch runtime kernel, both passes decode and repack the zero points.

At 078e7f51cfc30ffe8503fad91fd41d293af9ef29, calibrated finalizers call offload_to_disk(..., force=True) after packing. _offload_disk_locked builds a list of exported tensors unconditionally, then _bundle_module_state_dict calls state_dict() again to write the real bundle. The GPTQ Torch state-dict hook converts live logical zero points to v1 via an allocating unpack/shift/repack operation. Calibrated finalizer, Unused size preflight, Bundle state dict, GPTQ zero layout conversion.

On PyTorch 2.15.0+cpu with one Torch thread, forced disk offload of a 4096 × 4096 INT4/G32 TorchLinear converted the zero points twice. Weak references measured 524,288 bytes of simultaneous live converted output at the second allocation. Guarding the size preflight removed one conversion and reduced that measured allocation to 262,144 bytes. This count covers the converted outputs. Each removed conversion also avoids its unpacking and repacking work. The durable checkpoint words, v1 marker, disk-offloaded forward, and restored live forward all stayed exact.

Public GPTQModel.quantize() with INT4/G32, GPTQ v1 and disk offload on a two-layer Llama dropped from 28 to 14 v2-to-v1 conversions across the 14 projections. All 77 state-dict tensors and logits agreed bitwise. This cost is distinct from the live layout integrity fix in #4380 and the checkpoint serialization cost in #5749.

Reproduce

import weakref
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import patch

import torch
from safetensors.torch import load_file

from gptqmodel import BACKEND
from gptqmodel.nn_modules.qlinear.torch import TorchLinear
from gptqmodel.quantization import FORMAT
from gptqmodel.utils import model_dequant, offload

torch.set_num_threads(1)
torch.manual_seed(202)
proj = TorchLinear(
    bits=4, group_size=32, sym=False, desc_act=False, bias=False,
    in_features=4096, out_features=4096, pack_dtype=torch.int32,
    backend=BACKEND.GPTQ_TORCH, format=FORMAT.GPTQ, register_buffers=True,
)
proj.qweight.random_(-2**31, 2**31)
proj.qzeros.random_(-2**31, 2**31)
proj.scales.uniform_(0.0005, 0.003)
proj.g_idx.copy_(torch.arange(4096) // 32)
proj.qzero_format(2)
proj._checkpoint_format_on_disk = FORMAT.GPTQ
wrapper = torch.nn.Module()
wrapper.add_module("proj", proj)
checkpoint_before = proj.state_dict()
inputs = torch.randn(1, 4096, dtype=torch.float16)
expected = proj(inputs).clone()

convert = model_dequant._revert_gptq_v1_qzeros_correction
refs = []
live_at_conversion = []

def observe(*args, **kwargs):
    result = convert(*args, **kwargs)
    refs.append((weakref.ref(result), result.untyped_storage().nbytes()))
    live_at_conversion.append(sum(n for ref, n in refs if ref() is not None))
    return result

with TemporaryDirectory() as path:
    with patch.object(model_dequant, "_revert_gptq_v1_qzeros_correction", observe):
        offload.offload_to_disk(proj, wrapper, path, force=True, module_name="proj")
    print(len(refs), max(live_at_conversion))  # 2 524288
    bundle = load_file(Path(path) / "proj" / "module.safetensors")
    assert all(torch.equal(bundle[key], tensor) for key, tensor in checkpoint_before.items())
    assert int(bundle["qzeros_format"]) == 1
    assert torch.equal(proj(inputs), expected)
    offload.undo_offload_to_disk(proj, device=torch.device("cpu"))
    assert proj.qzero_format() == 2
    assert torch.equal(proj(inputs), expected)

Suggested fix

Compute the threshold only when it controls the decision. Leave creation and serialization of the durable state dict in the bundle writer.

# In _offload_disk_locked, after resolving module_offload_dir.
if not force:
    total_bytes = sum(_tensor_nbytes(tensor) for tensor in module.state_dict().values())
    if total_bytes <= _SMALL_MODULE_OFFLOAD_BYTES:
        return

Activity

  1. added
    scanned
    safe
    on Nov 20, 2026
  2. Qubitium commented on Nov 20, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-f7b500b8ed4c finished this issue.

    Pull request: #5881

    Reproducing the duplicate qzero conversion on CPU

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

    performancesafescanned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions