Skip to content

[MEM] Host memory: CPU int4 kernels keep a 2nd checkpoint-layout qweight (2.17x resident), GGUF_CPP_CPU copies the whole packed weight per forward (522 MiB/token), pinned host cache never released, dense embed_tokens re-serialized into offload dir #4395

Closed

Description

@Qubitium

Audit of main @ beb6db8 (first measured at 27c6b7b), host RAM and CPU kernels. Llama-3.2-1B / Llama-3.1-8B GPTQ with defaults; CPU runs with OMP_NUM_THREADS=8. RSS is split into anon/file/shmem.

1. CPU int4 kernels keep a full checkpoint-layout copy of qweight next to the runtime layout: 2.04–2.17× resident (medium; dedup)

nn_modules/qlinear/_fused_train_state.py:44-48 clones qweight, qzeros and scales. It is called from TorchFused, TorchAten, AVX512 and their three AWQ variants. #3984 mentioned this for TorchFused only, and docs/torch_fused_int4_transformations.md:234-243 calls it intended.

2048x8192 4-bit layer, TorchFused bf16: packed int4 weight 8.00 MiB, resident after first forward 2.17x
Llama-3.2-1B: +482 MiB after the first forward (qweight 464, scales 14.5, qzeros 3.6)
  of which pure duplicates even at the same layout: snapshot `scales` byte-identical to the live one (14.5 MiB);
  `self.qzeros` repeats scales_and_zeros[...,1] (29 MiB)

The int4pack permutation has an exact inverse, so the checkpoint qweight can be rebuilt on demand for state_dict()/save instead of stored. Projected saving: ~525 MiB on 1B and ~3.5 GB on 8B.

2. GGUF_CPP_CPU allocates a ggml buffer and copies the whole packed weight on every forward (medium; #4216's fix only covered CUDA)

nn_modules/qlinear/gguf_cpp.py:578-677 allocates at :627, copies the full qweight with ggml_backend_tensor_set (:630-635) and frees it at :676, on every call.

Q4_K 2048x8192 layer: M=1 stock 2.40 ms vs zero-copy 0.105 ms; M=128 7.57 vs 5.27 ms (bitwise equal)
Llama-3.2-1B Q4_K_M, one token: 169 ms vs 27 ms, logits bitwise equal (stock path copies 522 MiB per token)

Fix: wrap the existing storage with ggml_backend_cpu_buffer_from_ptr, and make qweight 32-byte aligned in post_init (the mmapped views are unaligned).

3. Pinned host memory from quantize and reload stays in PyTorch's pinned-memory cache for the life of the process (low-medium)

Quantize pins every tensor ≥8 MiB of every layer (models/base.py:4152 post_quantize → utils/model.py:325-380 move_module_to_cpu_pinned). The reload path (utils/model.py:3195 → accelerate → safetensors.load_file) pins a staging copy of the whole embedding. Nothing ever calls torch._C._host_emptyCache(), so this non-swappable memory (RssShmem) is never returned:

Llama-3.2-1B, after reload + del:            RssShmem 0.582 G   -> after torch._C._host_emptyCache(): 0.070 G (baseline)
after quantize + del + gc + cuda.empty_cache: +0.114 GiB pinned (8B: +0.47 GiB; reload: +1.0 GiB = lm_head size)

In one contended 8B GPTQ run, 10.82 GiB was still pinned after del (peak RSS 13.97 GiB). That pile-up didn't reproduce, but the never-released part is deterministic. Fix: call torch._C._host_emptyCache() when quantize() ends and after the loader's checkpoint load, and consider a bounded staging ring instead of pinning whole layers.

4. Quantize writes a byte-identical copy of the dense embed_tokens into the offload dir, then reads it back (low-medium)

looper/module_looper.py:2003-2020 → offload_to_disk(base_modules) → utils/offload.py:204 _bundle_module_state_dict re-serializes the module with safetensors.save_file, even though the source checkpoint already holds the exact bytes and offload_to_safetensors_reference (offload.py:270) exists for this case.

Llama-3.2-1B: model.embed_tokens/module.safetensors 525,336,576 B bf16 (128256, 2048), sha256 identical to the source tensor
  = 55% of the run's 0.96 GiB offload dir;  Llama-3.1-8B: ~1.05 GB of a 4.36 GiB dir

Fix: for base modules whose tensors are untouched source tensors, write a safetensors reference instead of a copy (same for the final norm).

5. LazyTurtle's "re-read once per layer" warning overstates the I/O (low, wording)

utils/structure.py:2408-2412 warns that a non-layer-wise shard is "re-read once per layer". On Llama-3.1-8B the shards are re-opened 10/12/12/2 times, but the bytes pulled through get_tensor total 13.98 GiB, i.e. 1.0× the checkpoint (read_bytes 11.0 GiB, file RSS ≤1.07 GiB). Reword it to say the header is re-parsed (re-open + mmap) per layer; the data is read once.

Checked, clean

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 16, 2026

    @Qubitium
    CollaboratorAuthor

    Claimed by an AI agent. Investigating: host-RAM/CPU findings — GGUF_CPP_CPU per-forward weight copy, pinned host cache retention, dense embed_tokens re-serialization, CPU int4 checkpoint-layout duplicate, and LazyTurtle warning wording.

  2. Qubitium commented on Nov 16, 2026

    @Qubitium
    CollaboratorAuthor

    Claimed by an AI agent: item 1 (CPU int4 checkpoint-layout snapshot dedup). Items 2-5 are merged via #4439.

  3. Qubitium commented on Nov 16, 2026

    @Qubitium
    CollaboratorAuthor

    Claimed by an AI agent: item 1 (CPU int4 fused-train checkpoint-layout snapshot dedup). Reusing branch fix/cpu-int4-snapshot-4395. Items 2-5 landed in #4439.

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