Skip to content

[MEM] #4845/#4842/#4849 (fixes of #4783/#4785) incomplete: AWQ AVX512 (CPU AUTO) still keeps the full qweight snapshot (2.04x), desc_act on ATen/Fused still snapshots (+3.35 GiB on 8B), TorchFusedLinear still pins the safetensors mmap (+5.35 GB RSS) #4873

Closed

Description

@Qubitium

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

#4845 (fix of #4783) and #4842/#4849 (fix of #4785) fixed the GPTQ TorchCpuAvx512Linear path and the ATen paths. On 8B, the AUTO bf16 GPTQ load drops from 9060 to 5732 MiB of tensors and from 15.25 to 6.66 GB RSS. Three sibling paths that the two issues named are still unchanged:

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

gptqmodel/nn_modules/qlinear/torch_cpu_avx512_awq.py:40 still has _FUSED_TRAIN_STATE_REBUILD = None, with the comment "no verified inverse". #4783's suggested fix named this line. #4845 added rebuild_avx512_qweight only for the GPTQ class. The AWQ class runs the same pack_hf_int4 layout through the shared AVX512 kernel, so unpack_hf_int4 inverts it too. Only the AWQ nibble order differs.

2. desc_act checkpoints on ATen / Fused still keep the full snapshot

PackedGroupedQuantLinear._can_rebuild_checkpoint_qweight (qlinear/__init__.py, kind == "gptq" branch) still returns False for a non-identity ret_idx. #4783 item 2 asked for this case. #4845 only allows the permutation for the AVX512 kind.

3. TorchFusedLinear / TorchFusedAwqLinear CPU transforms never detach their operands from the safetensors mmap (#4785)

#4842 routed g_idx/scales through detach_cpu_operand in torch_aten_kernel.py and torch_cpu_avx512.py. #4849 did the same for the two AWQ siblings. torch_fused.py::transform_cpu (and torch_fused_awq.py::transform_cpu_awq) were not changed. #4785 named TorchFusedLinear explicitly. self.scales keeps the checkpoint dtype by design (#4178), and g_idx is the loader's tensor, so both stay views of the shard mapping and pin every page the loader touched.

Measured (GPTQModel.load(ckpt, backend=..., dtype=torch.bfloat16, device="cpu"), one forward of 32 tokens)

Tensor bytes are unique storages; "mapped" means module tensors whose data_ptr() lies inside a *.safetensors mapping in /proc/self/maps.

ckpt / backend                                          quant-linear MiB   _fused_train_state_* MiB   mapped tensors          RSS
AWQ-Llama-3.2-1B-g128-gemm, AUTO=TorchCpuAvx512AwqLinear    482 -> 982           482.1 (100%)          0                       2.50 GB  (same at 6fe07cb6d)
Meta-Llama-3.1-8B-Instruct-GPTQ-INT4 (desc_act),
   GPTQ_TORCH_FUSED=TorchFusedLinear                       3463 -> 7212          3354                  448 (g_idx + scales)    14.76 GB
     ... after cloning those 448 buffers (108.75 MiB) + gc                                                                       9.41 GB  (-5.35 GB)
Qwen2.5-0.5B-Instruct-gptq-4bit (desc_act g32),
   GPTQ_TORCH_ATEN                                          198 -> 455           176.0                 0 (fixed by #4842)
   GPTQ_TORCH_FUSED                                         198 -> 455           176.0                 387 (g_idx + scales)   (same at 6fe07cb6d)
For comparison, fixed path: 8B AUTO=TorchCpuAvx512Linear   3463 -> 3728           130                   0                       6.66 GB

In every case state_dict() is still bitwise equal to the checkpoint (672/672 and 336/336 tensors), so only residency is affected.

Minimal repro

import gc, psutil, torch
from gptqmodel import GPTQModel, BACKEND
m = GPTQModel.load("Meta-Llama-3.1-8B-Instruct-GPTQ-INT4", backend=BACKEND.GPTQ_TORCH_FUSED,
                   dtype=torch.bfloat16, device="cpu")
with torch.inference_mode():
    m.model(torch.randint(100, 1000, (1, 32)))
snap = sum(b.numel() * b.element_size() for x in m.model.modules()
           for n, b in x._buffers.items() if n.startswith("_fused_train_state_"))
files = [tuple(int(v, 16) for v in l.split()[0].split("-")) for l in open("/proc/self/maps") if ".safetensors" in l]
n = 0
for x in m.model.modules():
    for k, b in list(x._buffers.items()):
        if isinstance(b, torch.Tensor) and any(a <= b.data_ptr() < e for a, e in files):
            x._buffers[k] = b.clone(); n += 1
print(snap / 2**20, n, psutil.Process().memory_info().rss / 2**30)
gc.collect(); print(psutil.Process().memory_info().rss / 2**30)
# 3354.0 448 14.76  ->  9.41
# AWQ: GPTQModel.load("AWQ-Llama-3.2-1B-g128-gemm", dtype=torch.bfloat16, device="cpu") (AUTO) -> snapshot 482.1 MiB

Suggested fix

  • Give TorchCpuAvx512AwqLinear a _FUSED_TRAIN_STATE_REBUILD kind: unpack_hf_int4, then re-apply the AWQ order and pack_awq. finalize_fused_train_state already guards it with a bitwise verify.
  • In _can_rebuild_checkpoint_qweight, allow a non-identity ret_idx for the ATen/Fused gptq kind, and scatter the rows back in rebuild_gptq_qweight the way rebuild_avx512_qweight does.
  • In TorchFusedLinear.transform_cpu and TorchFusedAwqLinear.transform_cpu_awq, pass scales, g_idx and bias through detach_cpu_operand, as the ATen siblings now do.

Tests that should have caught it

test_cpu_avx512_snapshot_rebuild_4783.py covers only the GPTQ AVX512 class. test_cpu_int4_mmap_detach_4785.py and test_cpu_awq_int4_mmap_detach_4785.py cover AVX512 and ATen but not TorchFusedLinear/TorchFusedAwqLinear. Parametrize both over all six CPU int4pack classes, with desc_act in {False, True}.

Severity

Medium. The default CPU path for bf16 AWQ still holds 2.04x the quantized-linear bytes. Any desc_act GPTQ checkpoint on ATen/Fused keeps +3.35 GiB on 8B. The Fused path still reports +5.35 GB RSS on 8B.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Item 1 has a ready inverse in the tree: ParoLinear._rebuild_checkpoint_qweight (paro.py, layout == "avx512") already rebuilds an AWQ checkpoint qweight from the same AVX512 packer output (TorchCpuAvx512AwqLinear.pack_awq_int4_kernel), using unpack_hf_int4(...) followed by pack_awq_columns(codes.t().to(torch.int32), bits, order=AWQ_ORDER), and finalize_fused_train_state verifies it bitwise there. TorchCpuAvx512AwqLinear could reuse that function as its _FUSED_TRAIN_STATE_REBUILD kind, and then call finalize_fused_train_state at the end of transform_cpu, which it does not do today.

    Note that this inverse has the same full-rebuild verify cost as #4884, so the two should be fixed together.

    🤖 Generated with Claude Code

  2. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Another sibling with the same missing detach_cpu_operand step: ParoLinear (CPU AUTO for Paro). Since #4832 (fix of #4814), paro.py::_ensure_cpu_layout rebinds qweight/qzeros to the packed AVX512/ATen operands. However, scales, theta, pairs and channel_scales remain zero-copy views of the safetensors mapping, so the replaced checkpoint qweight pages stay resident. As a result, the #4814 saving is invisible in RSS.

    Qwen2.5-0.5B-Instruct Paro 4-bit g128, GPTQModel.load(..., dtype=torch.bfloat16, device="cpu"), one forward of 32 tokens, same measurement as in the issue body:

                             tensor MiB (before -> after fwd)   RSS after fwd   mapped module tensors          RSS after cloning them + gc
    5b5830789 (pre-#4832)    443.2 -> 629.6                      1.99 GB         1008 (incl. qweight/qzeros)    1.72 GB
    5cded90c9                443.2 -> 448.2                      1.99 GB          672 (scales, theta, pairs,     1.54 GB  (-0.45 GB; the clones are 11.4 MiB)
                                                                                      channel_scales)
    

    The fp16 (ATen) path also keeps bias and the dense weight views mapped (795 tensors).

    Fix: the same as for the Fused kernels. After _ensure_cpu_layout succeeds, pass the retained per-column operands through detach_cpu_operand.

    🤖 Generated with Claude Code

  3. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-root-4873-fused-detach is working on this issue now.

    Started 2026-11-17T18:22:11Z. 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: detach the operands TorchFusedLinear/TorchFusedAwqLinear/ParoLinear keep as safetensors views (item 3 + the Paro follow-up); the AWQ AVX512 qweight rebuild stays out of scope unless it lands verified

  4. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released — fixed and merged.

    #4918 landed as b218dce93 on main; the issue is closed.

  5. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Fixed by #4918 (merged as b218dce93).

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