Skip to content

fix(gguf): return an empty output for M=0 on the CUDA kernel (#4743) - #4780

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-4743-gguf-cuda-zero-row
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-4743-gguf-cuda-zero-row

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4743.

Problem

GGUFCudaKernel is what AUTO selects for GGUF Q4_0/Q4_K checkpoints on CUDA. Its forward had no empty-input
guard, so a zero-row activation - an MoE expert that received no tokens, or an empty batch slice - reached the ggml
plan builder and raised:

RuntimeError: GGUFCudaKernel GGML plan has no scratch tensors.

Every other kernel returns an empty (..., N) output for M=0 through
BaseQuantLinear.input_rows()/empty_linear_output() (Marlin, ExLlamaV2, Triton, Torch, all AWQ kernels, QQQ, FP8,
NF12, GGUF_TRITON, GGUF_TORCH, EXL3), and so does the CPU sibling GGUFCppKernel.

Fix

gptqmodel/nn_modules/qlinear/gguf_cpp.py - take the same path in GGUFCudaKernel.forward, at the top of the
method:

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

It runs before the device check, the weight upload and the plan cache, so an M=0 input

  • builds no zero-sized ggml plan (the reported crash),
  • never grows the shared per-device arena,
  • does not need a CUDA context at all, and
  • still applies an adapter, like every other kernel's empty path.

A non-empty non-CUDA input still gets the only supports CUDA inference error, so the diagnostic is unchanged for
real matmuls.

Tests

New tests/kernels/test_gguf_cuda_empty_rows_4743.py (12 cases, no ggml CUDA backend required):

  • (0, K), (2, 0, K) and (0, 3, K) return (..., N) with the input dtype/device, numel() == 0, and no
    cached plan - in FP16, BF16 and FP32.
  • _get_ggml_bridge is patched to raise: the empty path must never reach the bridge (this is the "no plan, no arena"
    guarantee, not just the output shape).
  • A non-empty CPU input still raises only supports CUDA inference.

The _CudaTensor fake in tests/kernels/test_gguf_cuda_scratch_arena_4387.py now exposes ndim/requires_grad,
so that pre-existing test drives the same input_rows() guard as a real tensor instead of failing on the new call.

Red on unmodified main (dba142d2f): 11 of the 12 new cases fail (the twelfth asserts unchanged non-empty
behaviour). Green on this branch: 132 passed / 78 skipped for the new file plus the GGUF CUDA arena, plan-weight,
path-selection and kernel-selection suites. ruff check clean.

`GGUFCudaKernel` is what AUTO selects for GGUF Q4_0/Q4_K on CUDA. It had no
empty-input guard, so a zero-row activation - an MoE expert that received no
tokens, or an empty batch slice - reached the ggml plan builder and raised

    RuntimeError: GGUFCudaKernel GGML plan has no scratch tensors.

Every other kernel returns an empty `(..., N)` output for M=0 through
`input_rows()`/`empty_linear_output()`, and so does the CPU sibling
`GGUFCppKernel`.

Take the same path here, before the device check, the weight upload and the
plan cache: an M=0 input needs no device and no native backend, and this way
no zero-sized plan is built and the shared arena does not grow.

Tests: new `tests/kernels/test_gguf_cuda_empty_rows_4743.py` pins the output
contract for `(0, K)`, `(B, 0, K)` and `(0, S, K)` in FP16/BF16/FP32, asserts
the ggml bridge is never reached for those shapes, and keeps the CUDA-only
diagnostic for a non-empty non-CUDA input. The ggml CUDA backend is not needed
to run them. The `_CudaTensor` fake in the #4387 arena test now exposes
`ndim`/`requires_grad` so it drives the same guards as a real tensor.

Red on unmodified main (dba142d): 11 of the 12 new cases fail.
@Qubitium
Qubitium merged commit 5bc86f7 into main Nov 17, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4743-gguf-cuda-zero-row branch November 17, 2026 14:50
Qubitium added a commit that referenced this pull request Nov 17, 2026
…rd (#4782)

#4778 and #4780 fixed #4743 in parallel and both added the same
`input_rows()`/`empty_linear_output()` guard to
`GGUFCudaKernel.forward`. Merged main therefore runs the check twice, with
the second one unreachable.

Keep one guard, placed before `original_shape` is computed so an M=0 input
does no work at all, and fold both comments into it.

No behaviour change: both guards were identical and both sat before the
device check, the weight upload and the plan cache.

Co-authored-by: Qubitium <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant