Skip to content

[BUG] GGUFCudaKernel keeps an extra device copy of the quantized weight for each distinct batch size (up to 8 per layer) #4216

Closed

Description

@Qubitium

Severity: medium (up to ~9× the GGUF weight VRAM; also against AGENTS.md's storage rule in spirit)
Version: 7.11.0 @ 9dad249, with llama-cpp-python 0.3.36 (CUDA)

Summary

In nn_modules/qlinear/gguf_cpp.py:

  • build_quantized_matmul_cuda_plan (~L610-692) allocates a ggml backend buffer and uploads qweight into every plan;
  • plans are keyed by input row count (_cuda_plan_key, ~L1088);
  • up to _MAX_GGML_CUDA_PLANS = 8 plans are kept (L136).

Reproduce

import ctypes, numpy as np, torch
from gptqmodel.nn_modules.qlinear.gguf_cpp import GGUFCudaKernel, _get_ggml_bridge
K = N = 8192
m = GGUFCudaKernel(bits="q4_k_m", group_size=-1, sym=True, desc_act=False, in_features=K, out_features=N, bias=False)
raw = np.random.default_rng(0).integers(0, 256, size=tuple(m.qweight.shape), dtype=np.uint8)
blk = raw.reshape(-1, 144); blk[:, 0:4] = np.array([1e-3, 1e-3], np.float16).view(np.uint8)
m.qweight.copy_(torch.from_numpy(raw)); m = m.cuda(); m.post_init()
lib = _get_ggml_bridge()._ggml_base
lib.ggml_backend_buffer_get_size.argtypes = [ctypes.c_void_p]; lib.ggml_backend_buffer_get_size.restype = ctypes.c_size_t
for M in range(1, 11):
    m(torch.randn(M, K, device="cuda", dtype=torch.float16))
    print(M, len(m._ggml_cuda_plans), sum(lib.ggml_backend_buffer_get_size(p.buffer) for p in m._ggml_cuda_plans.values()) / 2**20, "MiB")

Actual: retained ggml buffers grow 36 → 72 → … → 290 MiB as the batch size changes (qweight itself is 36 MiB).

Expected: one weight copy per device, shared by all plans; only the activation/output tensors vary per batch size.


Found during an AI-assisted review (Claude Code) of 9dad249; repros were run on RTX 4090/5090 (CUDA 13) and/or Radeon AI PRO R9700 (ROCm) unless marked static/suspected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions