Skip to content

[MEM] QQQ on CPU (AUTO QQQTorchLinear) keeps a second packed copy of every weight in _torch_packed_cache next to the unused checkpoint B: QQQ-Llama-3-8B +3.5 GiB host RAM (5.4 -> 9.0 GiB tensors, RSS 4.2 -> 11.2 GB) after the first forward #4805

Closed

Description

@Qubitium

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

What is wrong

On CPU, QQQTorchLinear is AUTO for QQQ in both fp16 and bf16. On the first forward, _packed_weight_for_cpu (nn_modules/qlinear/qqq.py:817-836) builds _torch_packed_cache = (pack_hf_int4(codes), s_group fp32, s_channel fp32) for the AVX512 qqq_int4 kernel. It keeps that cache for the life of the module, next to the checkpoint B / s_group / s_channel buffers.

B is never read again on that path (forward -> _packed_cpu_matmul uses only the cache), so the packed weight is resident twice, and the group scales a further 2x because they are widened to fp32. Round 10 listed this as "plausible, not verified" in #4387; it is now measured. #4395/#4532 removed the same kind of duplicate for the GPTQ/AWQ CPU int4 kernels, but did not touch QQQ.

Measured (unique tensor storages of the HF model, CPU, AUTO, after a few forwards)

QQQ-Llama-3-8b-g128 (ckpt 5442 MiB), dtype=bf16, 224 x QQQTorchLinear
  after load:      5447 MiB   (B 3328, s_group 104, s_channel 5)        RSS 4.2 GB
  after forwards:  8988 MiB   (+ _torch_packed_cache 3541 MiB, +65%)     RSS 11.2 GB
Qwen2.5-0.5B-Instruct QQQ 4-bit g128 (ckpt 437 MiB), fp16 and bf16
  B 170.6 MiB  ->  + _torch_packed_cache 182.5 MiB (quantized linears 2.07x), model 440 -> 622 MiB

Minimal repro

import torch
from gptqmodel import GPTQModel
m = GPTQModel.load("QQQ-Llama-3-8b-g128", dtype=torch.bfloat16, device="cpu")   # AUTO -> QQQTorchLinear
mods = [x for x in m.model.modules() if type(x).__name__ == "QQQTorchLinear"]
nb = lambda ts: sum(t.numel() * t.element_size() for t in ts) / 2**20
print(nb([x.B for x in mods]))                                       # 3328.0
with torch.inference_mode():
    m.model(torch.randint(100, 1000, (1, 8)))
print(nb([t for x in mods for t in (x._torch_packed_cache or ())]))  # 3541.25

Suggested fix

Keep a single resident weight on CPU:

  • either have the qqq_int4 CPU op read the checkpoint B layout (with _perm) directly;
  • or, once the cache is built, drop B/s_group/s_channel and rebuild them for state_dict()/save/.to(cuda). The ingredients exist: _unpack_weight_codes and the inverse scale permutations are already in this class. The verify-then-drop pattern from fix(cpu-int4): rebuild checkpoint qweight instead of keeping a clone (#4395) #4532 (RebuildCandidate) fits.

Also store the cached scales in the checkpoint dtype, or convert them per call; fp32 doubles them. Restoring them in fp16 is exact.

Test that should have caught it

tests/test_qqq* CPU tests check numerics only. Add a CPU residency assertion: after the first forward, a QQQ module's tensor bytes stay within 1.1x of its checkpoint bytes. The same parametrized residency test is worth adding for every CPU kernel (see #4783).

Severity

Medium: +3.5 GiB host RAM (+65%) on an 8B QQQ model on the default CPU path.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-53ba17eb4d83 finished this issue.

    Pull request: #4856

    Fixing: reproduce, root-cause, add regression test, keep existing suites green.

  2. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

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

    Started 2026-11-17T17:04:34Z. 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: stop QQQTorchLinear keeping the checkpoint B next to _torch_packed_cache, and keep the cached scales at checkpoint width

  3. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent release: agent-root-4805-qqqcpu is releasing this claim after reading the code; I am not going to ship a cosmetic subset of this one.

    What I confirmed

    QQQTorchLinear._packed_weight_for_cpu() (qqq.py:817) builds
    (pack_hf_int4(codes), s_group, s_channel) from B on the first CPU forward and
    _packed_cpu_matmul then reads only that cache, so B is dead weight on the CPU
    inference path exactly as reported. _torch_weight_state_key() hashes B,
    s_group and s_channel, so the cache also keeps B reachable for
    invalidation.

    Why I stopped

    The scales half of the suggested fix is easy and exact (cache them at the
    checkpoint dtype, widen per call). It is also about 55 MiB of the 3541 MiB the
    issue reports, so shipping it alone under "fixes #4805" would misrepresent the
    result.

    The part that matters — dropping B — needs three contracts kept intact at
    once, and each is a place this codebase has already regressed:

    1. state_dict()/save must still emit the checkpoint B bytes. That needs an
      exact inverse of the whole chain (pack_hf_int4, the .t(), the codes ^ 8
      XOR, and the QQQ tile permutation _perm_i), not just of the packer. The
      inverse of _unpack_weight_codes does not exist in the class today.
    2. load_state_dict() into a module that has already dropped B must restore
      it, and _torch_weight_state_key() has to stay correct once B is gone.
    3. .to("cuda") must restore B, because the CUDA QQQ kernels read it.

    That is the same contract that #4466 (test_resave_dense_parameter_dtype) and
    #4788 (snapshot/buffer race) exist to protect. Doing it properly wants a
    RebuildCandidate-style verified inverse like the one #4532 added for the ATen
    int4pack family, plus a bitwise rebuild test for a tile-permuted QQQ
    checkpoint — a self-contained piece of work, not a drive-by change.

    If someone picks this up, the shape I would suggest: add
    _rebuild_checkpoint_B() next to _unpack_weight_codes, verify it against a
    captured clone the way finalize_fused_train_state does, and only then drop B
    on the CPU path — leaving _torch_weight_state_key keyed on the cache instead of
    B.

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