Repository navigation
fix(gguf): return an empty output for M=0 on the CUDA kernel (#4743) - #4780
Merged
Merged
Conversation
`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
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4743.
Problem
GGUFCudaKernelis what AUTO selects for GGUF Q4_0/Q4_K checkpoints on CUDA. Itsforwardhad no empty-inputguard, so a zero-row activation - an MoE expert that received no tokens, or an empty batch slice - reached the ggml
plan builder and raised:
Every other kernel returns an empty
(..., N)output for M=0 throughBaseQuantLinear.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 inGGUFCudaKernel.forward, at the top of themethod:
It runs before the device check, the weight upload and the plan cache, so an M=0 input
A non-empty non-CUDA input still gets the
only supports CUDA inferenceerror, so the diagnostic is unchanged forreal 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 nocached plan - in FP16, BF16 and FP32.
_get_ggml_bridgeis patched to raise: the empty path must never reach the bridge (this is the "no plan, no arena"guarantee, not just the output shape).
only supports CUDA inference.The
_CudaTensorfake intests/kernels/test_gguf_cuda_scratch_arena_4387.pynow exposesndim/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-emptybehaviour). 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 checkclean.