You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[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
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
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.
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
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:
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.
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.
.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.
Activity
Qubitium commentedon Nov 17, 2026
Agent claim released:
agent-53ba17eb4d83finished this issue.Pull request: #4856
Fixing: reproduce, root-cause, add regression test, keep existing suites green.
Qubitium commentedon Nov 17, 2026
Agent claim:
agent-root-4805-qqqcpuis 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
Qubitium commentedon Nov 17, 2026
Agent release:
agent-root-4805-qqqcpuis 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)fromBon the first CPU forward and_packed_cpu_matmulthen reads only that cache, soBis dead weight on the CPUinference path exactly as reported.
_torch_weight_state_key()hashesB,s_groupands_channel, so the cache also keepsBreachable forinvalidation.
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 atonce, and each is a place this codebase has already regressed:
state_dict()/save must still emit the checkpointBbytes. That needs anexact inverse of the whole chain (
pack_hf_int4, the.t(), thecodes ^ 8XOR, and the QQQ tile permutation
_perm_i), not just of the packer. Theinverse of
_unpack_weight_codesdoes not exist in the class today.load_state_dict()into a module that has already droppedBmust restoreit, and
_torch_weight_state_key()has to stay correct onceBis gone..to("cuda")must restoreB, 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 ATenint4pack family, plus a bitwise rebuild test for a
tile-permuted QQQcheckpoint — 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 acaptured clone the way
finalize_fused_train_statedoes, and only then dropBon the CPU path — leaving
_torch_weight_state_keykeyed on the cache instead ofB.fix(qqq): replace the CPU checkpoint layout with the packed cache (#4805
fix(qqq): build the CPU packed cache outside inference mode (#5197) (#…