Skip to content

[MEM] #4918 (fix of #4873) incomplete: CPU int4 kernels still keep the full qweight snapshot for GPTQ desc_act on ATen/Fused (+3.28 GiB on Llama-3.1-8B, incl. AUTO fp16) and for AWQ AVX512 (2x); both inverses verified bitwise #5013

Closed

Description

@Qubitium

Found on main @ c2b0bf6, x86-64 AVX512-BF16 host (CPU only, OMP_NUM_THREADS=8, torch 2.15).

#4873 was closed by #4918 (b218dce93). #4918 fixed item 3 of that issue: the Fused/Paro operands no longer pin the safetensors mmap, and I confirmed this. Items 1 and 2 were not addressed and still reproduce on main. In both cases the CPU int4 kernel keeps a full checkpoint-layout copy of qweight (_fused_train_state_qweight) for the life of the model, even though a bitwise inverse exists. I verified both inverses below.

1. TorchCpuAvx512AwqLinear (CPU AUTO for bf16 AWQ) keeps the full qweight snapshot

gptqmodel/nn_modules/qlinear/torch_cpu_avx512_awq.py:40: _FUSED_TRAIN_STATE_REBUILD = None ("no verified inverse").

2. GPTQ desc_act checkpoints on TorchAtenLinear / TorchFusedLinear keep the full qweight snapshot

gptqmodel/nn_modules/qlinear/__init__.py:1334-1341 (_can_rebuild_checkpoint_qweight): the kind == "gptq" branch returns False whenever ret_idx is not the identity. The comment says the ATen transform reorders rows "without keeping the gather". However, the module does keep ret_idx: it is an attribute of every transformed module (attr:ret_idx below). Scattering the unpacked codes back through it is an exact inverse, the same thing rebuild_avx512_qweight already does for the AVX512 kind.

This path is not opt-in. AUTO with dtype=torch.float16 on CPU selects TorchAtenLinear.

Measured

GPTQModel.load(ckpt, backend=..., dtype=..., device="cpu"), then one forward of 1/7/64 tokens. Tensor bytes are unique storages of params, buffers and tensor attributes.

ckpt                                   backend / kernel                       dtype  ckpt MiB  tensors after fwd MiB  snapshot MiB  RSS after fwd
Meta-Llama-3.1-8B-Instruct-GPTQ-INT4   GPTQ_TORCH_ATEN  (TorchAtenLinear)      bf16   5470      9216                    3354          10.29 GB
(desc_act=True)                        GPTQ_TORCH_FUSED (TorchFusedLinear)     bf16   5470      9216                    3354          10.29 GB
                                       AUTO             (TorchAtenLinear)      fp16   5470      9008                    3354          12.54 GB
                                       AUTO             (TorchCpuAvx512Linear) bf16   5470      5732                     130           6.80 GB  <- fixed path, for comparison
Qwen2.5-0.5B-Instruct AWQ 4-bit g128   AUTO (TorchCpuAvx512AwqLinear)          bf16    437       621                     177           -
AWQ-Llama-3.2-1B-g128-gemm             AUTO (TorchCpuAvx512AwqLinear)          bf16      -         -                     464 (qweight) -
Qwen2.5-0.5B-Instruct GPTQ desc_act    GPTQ_TORCH_ATEN / GPTQ_TORCH_FUSED      bf16    438       631                     172           -

The 8B desc_act checkpoint on ATen/Fused holds +3.28 GiB of host RAM (1.68x the checkpoint) that the AVX512 path no longer holds.

Verified inverses (bitwise against the snapshot buffer the module holds)

# item 2: GPTQ desc_act, ATen/Fused (runtime int4pack -> checkpoint qweight)
from gptqmodel.nn_modules.qlinear._cpu_int4pack_layout import int4pack_to_codes
codes = int4pack_to_codes(mod.qweight)                     # [out, in], runtime (ret_idx) order
ret_idx = mod._build_ret_idx().to(torch.long)
restored = torch.empty_like(codes); restored.index_copy_(1, ret_idx, codes)
grouped = restored.t().reshape(-1, mod.pack_factor, mod.out_features).to(torch.int32)
packed = torch.zeros((grouped.shape[0], mod.out_features), dtype=torch.int32)
for i in range(mod.pack_factor):
    packed.bitwise_or_(grouped[:, i, :] << (i * mod.bits))
assert torch.equal(packed, mod._fused_train_state_qweight)

# item 1: AWQ AVX512
from gptqmodel.nn_modules.qlinear.torch_cpu_avx512 import unpack_hf_int4
from gptqmodel.quantization.awq.utils.packing_utils import pack_awq_columns, AWQ_ORDER
codes = unpack_hf_int4(mod.qweight, out_features=mod.out_features, in_features=mod.in_features)
assert torch.equal(pack_awq_columns(codes.t().to(torch.int32), mod.bits, order=AWQ_ORDER),
                   mod._fused_train_state_qweight)

Results:

Meta-Llama-3.1-8B-Instruct-GPTQ-INT4  GPTQ_TORCH_ATEN   224/224 modules bitwise equal (all non-identity ret_idx)
Meta-Llama-3.1-8B-Instruct-GPTQ-INT4  GPTQ_TORCH_FUSED  224/224
Qwen2.5-0.5B-Instruct GPTQ desc_act   GPTQ_TORCH_ATEN   168/168, GPTQ_TORCH_FUSED 168/168
Qwen2.5-0.5B-Instruct AWQ             AUTO (AVX512 AWQ) 168/168
AWQ-Llama-3.2-1B-g128-gemm            AUTO (AVX512 AWQ) 112/112

Suggested fix

  • Item 2: in rebuild_gptq_qweight, scatter the codes back through ret_idx when _ret_idx_is_identity() is False, as in the snippet above. Then drop the kind == "gptq" and not identity() early return in _can_rebuild_checkpoint_qweight. _rebuild_layout_key already tags this case "reordered", so the once-per-layout verify from perf(cpu): verify the int4pack inverse once per layout instead of rebuilding every qweight (#4884) #4965 keys it separately.
  • Item 1: give TorchCpuAvx512AwqLinear a rebuild kind backed by unpack_hf_int4 + pack_awq_columns(..., order=AWQ_ORDER). ParoLinear._rebuild_checkpoint_qweight (avx512 layout) already uses this pair. Call finalize_fused_train_state at the end of its transform_cpu.
  • finalize_fused_train_state keeps the bitwise guard, so a wrong inverse still falls back to the clone.

Test that should have caught it

tests/test_cpu_int4_snapshot_dedup_4395.py does not cover desc_act on ATen/Fused or the AWQ AVX512 class. Parametrize it over all CPU int4pack classes with desc_act in {False, True}, and assert that no _fused_train_state_qweight buffer stays resident after the first forward.

Severity

Medium. +3.28 GiB host RAM (+68%) on an 8B desc_act GPTQ model with the CPU ATen/Fused kernels, including the default AUTO + fp16 CPU load. Bf16 AWQ on CPU AUTO holds 2.0x its quantized-linear bytes (177 MiB of snapshot next to 178 MiB of packed weights on Qwen2.5-0.5B, 464 MiB on Llama-3.2-1B). This is the residue of #4783/#4873.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

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

  2. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent release: agent-root-5013 finished this issue. Fix: #5033, squash-merged as 2620daf28.

    Both remaining CPU int4 layouts now drop the resident qweight clone, because both inverses are exact:

    1. GPTQ desc_act (TorchAtenLinear / TorchFusedLinear) — _can_rebuild_checkpoint_qweight no longer bails out for a non-identity ret_idx. The transform packs weight.index_select(0, ret_idx) and the module keeps ret_idx (argsort(g_idx), a permutation of [0, in_features)), so index_copy_ with that index is the exact inverse. AUTO with dtype=torch.float16 on CPU selects this kernel, so this was the non-opt-in case.
    2. TorchCpuAvx512AwqLinear — new rebuild_avx512_awq_qweight, the pure-Torch inverse of the extension's pack_awq_int4 (reverse nibble read plus pack_awq_columns(..., order=AWQ_ORDER)). transform_cpu now ends with finalize_fused_train_state, and _FUSED_TRAIN_STATE_REBUILD = "avx512_awq".

    Both are still fail-closed: the clone is only released after finalize_fused_train_state rebuilds the checkpoint words and compares them bitwise with the pre-transform clone; a mismatch keeps the clone.

    Verification:

    • New tests/test_cpu_int4_snapshot_5013.py (17 nodes): 17 failed on unmodified main @ 8fd8644a1 (neither inverse existed) → 16 passed, 1 skipped on the branch, where the skip is the cross-check against the real extension pack_awq_int4 (needs AVX512-BF16). It covers the ret_idx scatter against an independent checkpoint-word oracle (group sizes 32/128), the drop + bitwise-identical state_dict() for both GPTQ kernels, the AVX512 AWQ nibble inverse over all 32 bits of every word (5 shapes), the real transform_cpu path, and both fail-closed cases. The file now runs in the AWQ CPU unit CI lane.
    • Post-merge on 2620daf28: 70 passed, 1 skipped across the new file plus test_cpu_int4_snapshot_dedup_4395.py, test_cpu_int4_verify_cost_4884.py and test_docs_contract_refs.py. CI: 9/9 green (AWQ CPU unit 1054 passed / 110 skipped).

    Measured (Qwen2.5-0.5B-Instruct, 4-bit g128 desc_act, CPU fp16 TorchAtenLinear, unique tensor storages before/after one forward):

    tree _fused_train_state_qweight buffers after forward snapshot bytes
    origin/main 168 620.3 MiB 172.0 MiB
    this PR 0 449.6 MiB 1.3 MiB

    The residual 1.3 MiB is the qzeros snapshot, which this change does not touch.

    Caveat stated in the PR: this host has no AVX512-BF16, so the AWQ AVX512 extension kernel cannot load. The AWQ inverse is verified against a line-by-line Python mirror of int4_qzero_group_scaled_pack.cpp (cross-checked against the real kernel automatically on a host that has it) and the module-level test drives the real transform_cpu with that mirror installed as the pack kernel. No AVX512 end-to-end claim. The issue's 8B numbers could not be reproduced here (no 8B desc_act checkpoint cached); the 0.5B delta is the same mechanism at 1/16 the width.

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