Skip to content

[MEM] Kernel memory: per-module QQQ reduce scratch (5.25 GiB on 8B), GGUF_CPP_CUDA per-shape ggml plan buffers (6.4 GiB for a 211 MiB model), FP8 load peaks at 2.1x resident, QQQTorch int64/fp32 per-forward temporaries #4387

Closed

Description

@Qubitium

Audit of main @ 5065bf1 (first measured at 27c6b7b), RTX 4090. All numbers are per-process: torch.cuda.* counters plus this PID's process VRAM.

1. Every QQQLinear keeps its own reduce scratch: 5.25 GiB on Llama-3-8B, more than the weights (high)

nn_modules/qlinear/qqq.py:229-233 (__init__) and :520-524 (pack) register reduce_buffer = zeros(max_par*64, out_features, int32) per module, and pass it as C at :571. C is only scratch for the kernel's global reduce (qqq_gemm.cu:267,312,347); nothing in it carries over between calls. All 1024 rows are allocated even for decode. QQQTorchLinear (:594) inherits the buffer and never uses it.

Qwen2.5-0.5B qqq4:  168 modules, packed weights+scales 177.1 MiB, reduce_buffer total 1188.0 MiB, allocated 1625.3 MiB (ckpt 437 MiB)
  all modules pointed at ONE 19.0 MiB per-device buffer: allocated 804.6 MiB, logits bitwise equal (prefill 3x200 and decode)
QQQ-Llama-3-8b-g128: packed 3437 MiB, reduce_buffer 5376 MiB, allocated 10819 MiB   (a shared buffer would be ~56 MiB)

Fix: one lazily grown scratch per device, sized max_par*64 × max(N), or a per-call torch.empty(min(ceil16(M), max_par*64), N). Drop the buffer from QQQTorchLinear. A resident-vs-checkpoint bytes assertion in the QQQ kernel tests would catch this.

2. GGUF_CPP_CUDA keeps ggml plan buffers per module and per row count, outside the torch allocator: 6.4 GiB for a 211 MiB model (high)

gguf_cpp.py:744-800 (build_quantized_matmul_cuda_plan) allocates a ggml backend buffer per plan, holding that call's input, output and intermediates. Plans are kept per module and keyed by x.shape[0], up to _MAX_GGML_CUDA_PLANS = 8 each (:186, :1371-1401). The buffers are pure scratch, since run_quantized_matmul_cuda_plan copies x in and the output out (:828-850), and forwards are already serialized per device. #4268 (for #4216) shared the weight across plans but left this scratch, so the memory side of that fix is incomplete.

Qwen2.5-0.5B GGUF q4_k_m, GGUF_CPP_CUDA, prompts of increasing length (ggml sizes via ggml_backend_buffer_get_size):
  M=512:  ggml plan buffers  960 MiB (504 plans)   torch alloc 471 MiB   process VRAM 2268 MiB
  M=2048: ggml plan buffers 4368 MiB (672 plans)                         process VRAM 6240 MiB
  + M=2,3,8,300,1000: 6447 MiB (1344 plans)                              process VRAM 8706 MiB   (torch alloc still 471 MiB)
  del model: process VRAM 140 MiB  -> retention, not a leak

torch.cuda.memory_allocated() and empty_cache() never see this memory. The torch qweight also stays on CUDA next to the shared ggml weight copy, so the weight is held twice (211 + 211 MiB).

Fix: one per-device ggml scratch arena (or ggml_gallocr) shared by all modules and grown to the largest (M, K, N); alternatively, free plan buffers after each call and keep only the graph metadata. Free or move the torch qweight after upload.

3. FP8 checkpoint load peaks at 2.1× resident: accelerate upcasts every fp8 weight to the model dtype, and the fp8 bytes are then read from disk a second time (medium-high)

utils/model.py:3196 calls accelerate.load_checkpoint_in_model(dtype=...). That casts every non-integer tensor (accelerate modeling.py:316), so all fp8 weight buffers are materialized as fp16/bf16 on GPU and stay alive until _restore_checkpoint_buffers (:3220) re-reads the fp8 tensors from disk.

                     resident   load peak   fp16/bf16 copies at the peak
Qwen2.5-0.5B fp8     602 MiB    1285 MiB    683 MiB (n=240, set_module_tensor_to_device <- load_checkpoint_in_model)
Llama-3.2-1B fp8    1431 MiB    3287 MiB   1856 MiB
Llama-3.1-8B fp8    8666 MiB   18313 MiB  12256 MiB (n=208) + a 6057 MiB shard on GPU

So an 8B FP8 model that needs 8.5 GiB resident can't be loaded on a 16 GB card. Fix: keep the fp8 weight names out of the accelerate pass and assign them straight from safetensors (the key_converter / _remap_safetensors_checkpoint_keys hook in this function can do it), or load with dtype=None and cast only the dense floating tensors.

4. QQQTorchLinear (AUTO for bf16 QQQ on CUDA) rebuilds the dense weight on every forward through int64/fp32 temporaries (medium)

qqq.py:88-95 (_unpack_uint4 widens to int64 and uses floor_divide), :664-680 (fp32 gather/round/clamp), :764-771 (fp32 matmul fallback at small M).

Qwen up_proj 896x4864 (packed 2.08 MiB): 108 MiB transient at M=1 (int64 unpack temporaries 33 MiB each)
QQQ-Llama-3-8B bf16, AUTO: up_proj/down_proj +1448 MiB per forward at M=1, q_proj +408 MiB   (dense bf16 F.linear: ~0)

Fix: unpack with int32/uint8 bit ops and fuse the dequant, or cache the int8 weight (K×N bytes).

Checked, clean

  • Resident = checkpoint ±0.5% (no hidden dense/fp32 copies), flat over 20 shapes plus generate, and back to 0 after del:
    • GPTQ: Marlin (AUTO), ExllamaV2, Triton, Torch, desc_act
    • AWQ: Marlin, GEMM, Torch, ExllamaV2, GEMV, GEMV_Triton, GEMV_FAST
    • ParoQuant CUDA, MACaQ Torch/Triton, NF12, EXL3 (ExLlamaV3 and Torch), FP8 Torch, BnB (with the reload patch from Update readme table's style #2 of the companion issue)
  • fuse=True adds no duplicate, and repeated load/forward/del cycles are flat.
  • Tied embeddings: reloading a GPTQModel-quantized Qwen2.5-0.5B prints accelerate's "weights are not tied" warning, but lm_head.weight is embed_tokens.weight holds after load for every device_map tried. The warning is cosmetic; there is no duplicate.

Plausible, not verified

  • On CPU, QQQTorchLinear keeps _torch_packed_cache next to B (a host-side duplicate).
  • Loading a bf16-saved checkpoint with an fp16 dtype holds the whole shard on GPU plus the cast copy (+506 MiB peak on Llama-3.2-1B GPTQ). That is generic accelerate behaviour.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 16, 2026

    @Qubitium
    CollaboratorAuthor

    Additional data from ROCm (R9700, beb6db8): on Llama-3.1-8B QQQ, AUTO picks QQQTorchLinear, which carries the same per-module reduce_buffer without ever using it. After reload, GPU memory is 10.565 GiB against a 5.314 GiB checkpoint. The buffers also occupy host memory during quantize/save: RssAnon is 8.0 GiB after quantize (AWQ on the same model: 2.9 GiB), peaking at 13.3 GiB during save.

    🤖 Generated with Claude Code

  2. Qubitium commented on Nov 16, 2026

    @Qubitium
    CollaboratorAuthor

    Claimed by an AI agent. Investigating: per-module QQQ reduce_buffer scratch, QQQTorchLinear temporaries, GGUF_CPP_CUDA plan buffers, FP8 load peak.

  3. Qubitium commented on Nov 16, 2026

    @Qubitium
    CollaboratorAuthor

    Claimed by an AI agent: item 3 (FP8 checkpoint-load peak). Items 1, 2, 4 are already merged via #4436.

  4. Qubitium commented on Nov 16, 2026

    @Qubitium
    CollaboratorAuthor

    Claimed by an AI agent: item 3 (FP8 checkpoint-load peak). Reusing branch fix/fp8-load-peak-4387; will rebase on current main, validate, and open a PR. Items 1, 2, 4 landed in #4436.

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