Skip to content

fix(gguf): return an empty output for zero-row GGUF CUDA inputs (#4743) - #4778

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4743
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4743

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Problem

GGUFCudaKernel (GGUF_CPP_CUDA, the kernel AUTO selects for Q4_0/Q8_0 GGUF checkpoints on CUDA) had no empty-input guard. For x.shape == (0, K) or (B, 0, K) it built a ggml plan containing zero-sized tensors, and the arena allocation raised:

RuntimeError: GGUFCudaKernel GGML plan has no scratch tensors.

Every other CUDA kernel — and the CPU sibling GGUFCppKernel — returns an empty (..., N) output for M=0. M=0 is a real case: MoE experts that receive no tokens, empty batch slices.

Fix

GGUFCudaKernel.forward now starts with the standard guard, before the plan cache and the weight upload:

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

No plan is built, no arena growth and no weight upload happen for M=0.

input_rows() derives the row count from len(x.shape) rather than x.ndim so duck-typed tensors (test doubles) work; semantics are unchanged.

Tests

New tests/kernels/test_gguf_cuda_zero_rows_4743.py:

  • parametrized M=0 shapes (0, K), (2, 0, K), (2, 3, 0, K) — asserts shape/dtype/device, and that the bridge, weight upload and plan cache are untouched;
  • non-empty input still reaches the kernel with the flattened row count.

Verification

  • New file: 4 passed. Red on parent: 3 failed with GGML plan has no scratch tensors.
  • tests/kernels/test_gguf_{cpp,cuda_plan_weights,cuda_scratch_arena_4387,cuda_weight_storage_4387,cuda_zero_rows_4743}.py: 16 passed, 24 skipped, 1 failed — test_kernel_builds_the_weight_once_for_many_batch_sizes fails identically on parent (cached validate_once from an earlier test file in the same session).
  • ruff check gptqmodel tests: clean.

Closes #4743

GGUFCudaKernel (GGUF_CPP_CUDA, the AUTO kernel for Q4_0/Q8_0 GGUF
checkpoints on CUDA) built a ggml plan for M=0, which contains zero-sized
tensors, and the arena allocation raised
'GGUFCudaKernel GGML plan has no scratch tensors'.

Guard the forward with the same input_rows()/empty_linear_output() pair every
other kernel uses, before the plan cache and the weight upload, so M=0
returns an empty (..., N) output and allocates nothing.

input_rows() now derives the row count from len(x.shape) instead of x.ndim so
it also works for duck-typed tensors; semantics are unchanged.
@Qubitium
Qubitium merged commit d2060d4 into main Nov 17, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4743 branch November 17, 2026 14:45
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