Skip to content

[MEM] #4532 incomplete: CPU AUTO kernel TorchCpuAvx512Linear (and every desc_act ckpt on ATen/Fused) still keeps the full checkpoint qweight snapshot: Llama-3.1-8B GPTQ +3.4 GiB host RAM (+63%) after first forward; the inverse exists and is bitwise #4783

Closed

Description

@Qubitium

Found on main @ dba142d, x86-64 host with AVX512-BF16 (CPU only, OMP_NUM_THREADS=8).

What is wrong

#4532, the fix for #4395 item 1, only drops the checkpoint qweight clone for TorchAtenLinear / TorchFusedLinear (and their AWQ variants), and only when ret_idx is the identity. Two cases still keep the full checkpoint snapshot next to the runtime layout:

  1. TorchCpuAvx512Linear. This is what CPU AUTO selects for any bf16 GPTQ model on an AVX512-BF16 host (priority 120, versus 110 for ATen). nn_modules/qlinear/torch_cpu_avx512.py:121-126 declares _FUSED_TRAIN_STATE_NAMES = ("qweight", "qzeros", "scales", "bias") and _FUSED_TRAIN_STATE_REBUILD = None. The comment says "The AVX512 extension packs a different layout with no verified inverse". However, the same file ships the inverse: unpack_hf_int4 (:45-74, "Inverse of pack_hf_int4"), which dequantize_weight already uses. The scales/bias clones are not needed either, because the transform only widens them fp16/bf16 -> fp32, and narrowing back is exact.
  2. Any desc_act=True checkpoint on ATen/Fused. PackedGroupedQuantLinear._can_rebuild_checkpoint_qweight (nn_modules/qlinear/__init__.py:1288-1296) returns False when ret_idx is not the identity. The permutation is known (_build_ret_idx()), and dequantize_weight already undoes it, so the rows can be restored the same way.

So the 2x host residency that #4395 measured is still there for the default CPU kernel, and for every act-order checkpoint.

Measured (resident tensor bytes of the HF model, unique storages, before vs after the first forward)

Llama-3.1-8B-Instruct-GPTQ-INT4 (desc_act=True, ckpt 5470 MiB), dtype=bf16, device=cpu
  AUTO -> TorchCpuAvx512Linear x224 : 5467 -> 9060 MiB  (_fused_train_state_* snapshot = 3458 MiB, +63%)
  BACKEND.GPTQ_TORCH_ATEN           : 5467 -> 9216 MiB  (snapshot = 3354 MiB: qweight clone kept, ret_idx non-identity)
Qwen2.5-0.5B-Instruct GPTQ 4-bit g128 (desc_act=False), dtype=bf16
  AUTO -> TorchCpuAvx512Linear x168 : quant-linear bytes 189 -> 366 MiB (snapshot 177 MiB)
  BACKEND.GPTQ_TORCH_ATEN           : snapshot 1.3 MiB (the #4532 path works here)

The inverse is exact. For every AVX512 module after the first forward, I rebuilt the GPTQ qweight from the live words: unpack_hf_int4, then undo ret_idx, then pack 8 nibbles per int32 along in_features. It matched the snapshot bitwise, and scales.to(snapshot dtype) matched the snapshot scales bitwise:

Qwen2.5-0.5B GPTQ (identity order):        ok 168 / bad 0
Llama-3.1-8B-Instruct-GPTQ-INT4 (desc_act): ok 224 / bad 0

Minimal repro

import torch
from gptqmodel import GPTQModel, BACKEND
from gptqmodel.nn_modules.qlinear.torch_cpu_avx512 import TorchCpuAvx512Linear, unpack_hf_int4
from gptqmodel.nn_modules.qlinear._fused_train_state import _materialize_snapshot_entry

m = GPTQModel.load("Meta-Llama-3.1-8B-Instruct-GPTQ-INT4", dtype=torch.bfloat16, device="cpu")  # AUTO
with torch.inference_mode():
    m.model(torch.randint(100, 1000, (1, 4)))
snap = sum(b.numel() * b.element_size() for mod in m.model.modules()
           for n, b in mod._buffers.items() if n.startswith("_fused_train_state_"))
print(snap / 2**20)   # 3458.0 MiB

ok = bad = 0
for mod in m.model.modules():
    if not isinstance(mod, TorchCpuAvx512Linear):
        continue
    ref = _materialize_snapshot_entry(mod, mod._fused_train_state["qweight"])
    codes = unpack_hf_int4(mod.qweight, mod.out_features, mod.in_features)
    if not mod._ret_idx_is_identity():
        r = torch.empty_like(codes); r[:, mod._build_ret_idx().long()] = codes; codes = r
    c = codes.t().to(torch.int64).reshape(mod.in_features // 8, 8, mod.out_features)
    q = sum(c[:, i] << (4 * i) for i in range(8)).to(torch.uint32).view(torch.int32)
    ok += torch.equal(q, ref); bad += not torch.equal(q, ref)
print(ok, bad)        # 224 0

Suggested fix

  • Give TorchCpuAvx512Linear (and torch_cpu_avx512_awq.py:40) a _rebuild_checkpoint_qweight built on unpack_hf_int4, with the same verify-then-drop RebuildCandidate flow fix(cpu-int4): rebuild checkpoint qweight instead of keeping a clone (#4395) #4532 added. Rebuild qzeros from the transformed zeros and scales/bias by casting back, instead of snapshotting them.
  • In _can_rebuild_checkpoint_qweight, undo ret_idx instead of refusing non-identity order. The verify step already guards correctness.

Expected saving: about 3.4 GiB on an 8B model (resident goes from 1.66x back to ~1.0x of the checkpoint), and 177 MiB on Qwen2.5-0.5B.

Test that should have caught it

tests/test_fused_train_state* / the #4532 tests assert the snapshot is gone only for ATen/Fused with identity order. Add a parametrization over every CPU int4 kernel class (AVX512 GPTQ/AWQ included) and desc_act in {False, True}. It should assert that _fused_train_state_* buffers total less than 1% of the packed bytes after the first forward, and that state_dict() is still bitwise equal to the checkpoint.

Severity

Medium: host RAM on the default CPU path, roughly +60-95% of the quantized linears' bytes, and +3.4 GiB on 8B.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-root-4783-avxsnap is working on this issue now.

    Started 2026-11-17T16:58:36Z. The claim expires 4.0h after this comment was last edited (heartbeat). Other agents: do not start this issue while the claim is live.

    claiming: give TorchCpuAvx512Linear a verified checkpoint-rebuild inverse so the full qweight snapshot can be dropped

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions