Skip to content

[BUG] GGUFCudaKernel (AUTO for GGUF Q4_0 on CUDA) crashes on zero-row input: 'GGML plan has no scratch tensors' (every other kernel, incl. GGUFCppKernel on CPU, returns an empty output) #4743

Closed

Description

@Qubitium

Found on main @ b225a43, RTX 5090 (sm_120).

What is wrong

GGUFCudaKernel.forward (nn_modules/qlinear/gguf_cpp.py:1924-1975) has no empty-input guard. GGUF_CPP_CUDA is the kernel AUTO picks for Q4_0/Q8_0-style GGUF checkpoints on CUDA. On a zero-row input (x.shape == (0, K) or (B, 0, K)) it builds a ggml plan with zero-sized tensors and raises:

RuntimeError: GGUFCudaKernel GGML plan has no scratch tensors.        (gguf_cpp.py:1204, since #4436)
RuntimeError: GGUFCudaKernel failed to allocate GGML backend tensors.  (before #4436)

Every other CUDA kernel returns an empty (..., N) output for M=0 through BaseQuantLinear.input_rows() / empty_linear_output(). That covers Marlin, ExLlamaV2, Triton, Torch, all AWQ kernels, QQQ, FP8, NF12, GGUF_TRITON and GGUF_TORCH, and EXL3 after #4550. So does the CPU sibling GGUFCppKernel, which returns (0, N) and (2, 0, N) for the same tensors. M=0 is a real case: MoE experts that receive no tokens and empty batch slices. It has been fixed per-kernel before (#4042 Hadamard M=0, #4550 EXL3 M=0).

Repro

Qwen2.5-0.5B-Instruct quantized on main with GGUFConfig(bits=4) (Q4_0), loaded with BACKEND.AUTO, which selects GGUFCudaKernel for all 168 modules:

import torch
from gptqmodel import GPTQModel, BACKEND
m = GPTQModel.load(gguf_q4_0_ckpt, backend=BACKEND.AUTO, dtype=torch.float16, device="cuda:0")
mod = m.model.model.layers[0].mlp.down_proj
for shape in [(0, mod.in_features), (2, 0, mod.in_features), (1, mod.in_features)]:
    x = torch.randn(*shape, device="cuda", dtype=torch.float16)
    try:
        print(shape, tuple(mod(x).shape))
    except Exception as e:
        print(shape, type(e).__name__, e)
GGUFCudaKernel Q4_0
(0, 4864) -> RuntimeError GGUFCudaKernel GGML plan has no scratch tensors.
(2, 0, 4864) -> RuntimeError GGUFCudaKernel GGML plan has no scratch tensors.
(1, 4864) -> (1, 896)

The same happens on the Q4_K_M checkpoint with BACKEND.GGUF_CPP_CUDA, in fp16 and bf16, for every projection.

Suggested fix

At the top of GGUFCudaKernel.forward, do the same as the other kernels:

if self.input_rows(x) == 0:
    return self.empty_linear_output(x)

This must come before the plan cache, so no plan or arena growth happens for M=0. Add M=0 to the GGUF CUDA tests (test_gguf_triton_empty_launch.py covers only Triton).

Severity

Low-medium. It is a crash on a supported shape for the AUTO GGUF CUDA kernel, and nothing falls back.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

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

    Pull request: #4778

    claiming: fix on a worktree, PR when green

  2. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Claiming. Will add the M=0 guard to GGUFCudaKernel.forward (before the plan cache, per the suggestion) plus a regression test that also covers the Q4_K_M path, and verify every other GGUF kernel already returns an empty output for the same shapes.

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