Repository navigation
fix(gguf): return an empty output for zero-row GGUF CUDA inputs (#4743) - #4778
Merged
Merged
Conversation
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
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.
Problem
GGUFCudaKernel(GGUF_CPP_CUDA, the kernel AUTO selects for Q4_0/Q8_0 GGUF checkpoints on CUDA) had no empty-input guard. Forx.shape == (0, K)or(B, 0, K)it built a ggml plan containing zero-sized tensors, and the arena allocation raised: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.forwardnow starts with the standard guard, before the plan cache and the weight upload:No plan is built, no arena growth and no weight upload happen for M=0.
input_rows()derives the row count fromlen(x.shape)rather thanx.ndimso duck-typed tensors (test doubles) work; semantics are unchanged.Tests
New
tests/kernels/test_gguf_cuda_zero_rows_4743.py:(0, K),(2, 0, K),(2, 3, 0, K)— asserts shape/dtype/device, and that the bridge, weight upload and plan cache are untouched;Verification
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_sizesfails identically on parent (cachedvalidate_oncefrom an earlier test file in the same session).ruff check gptqmodel tests: clean.Closes #4743