ggml-cuda: fix cublasSgemm crash with zero-dim matrices in speculative decoding#39
Merged
spiritbuun merged 2 commits intoMay 5, 2026
Conversation
…e decoding Problem ------- During speculative decoding (DFlash/copyspec), draft verification produces non-consecutive token positions which cause slot management to generate sub-matrix batches where row_diff evaluates to 0. cuBLAS requires ldc >= max(1, m) where m = row_diff. When row_diff == 0, ldc is also 0 (ldc = row_diff when id != ctx.device), violating the cuBLAS precondition and triggering CUBLAS_STATUS_INVALID_VALUE. Root cause ---------- No guard exists before the cublasSgemm/cublasGemmEx calls in ggml_cuda_op_mul_mat_cublas() to handle zero-dimension sub-matrices. Unlike OpenBLAS and MKL which define zero-size GEMMs as no-ops, cuBLAS treats them as invalid parameters and aborts. Fix --- Add an early-return guard after row_diff and ldc are computed. When any GEMM dimension (m, n, k) is zero, return immediately. This is safe because dst_dd_i is untouched and the caller does not read partial results. Tested ------ Qwen3.6-27B Q4_K_M + DFlash draft, 55k context, RTX 3090 24GB - Before: crash after ~27k tokens prefill during draft verification - After: stable generation, multiple sequential requests without crash Related upstream issues ----------------------- - ggml-org#22105 (DFlash PR: hybrid models cannot discard rejected suffixes via seq_rm due to non-decomposable recurrent state) - ggml-org#19929 (non-consecutive token position with Qwen3.5 vision models on multi-GPU setups) - ggml-org#21569 (DFlash discussion: MoE hybrid limitations) Authored-by: Gaston Parravicini
Previous guard checked row_diff, src1_ncols, ne10 but missed ne00 (lda) and ldc. When ne00 == 0 or ldc == 0, cuBLAS also aborts with CUBLAS_STATUS_INVALID_VALUE even if m, n, k > 0. Extends the early-return guard to cover all invalid GEMM dimensions. Authored-by: Gastón Parravicini
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
During speculative decoding (DFlash/copyspec), draft verification produces non-consecutive token positions which cause slot management to generate sub-matrix batches where row_diff evaluates to 0.
cuBLAS requires ldc >= max(1, m) where m = row_diff. When row_diff == 0, ldc is also 0 (ldc = row_diff when id != ctx.device), violating the cuBLAS precondition and triggering CUBLAS_STATUS_INVALID_VALUE.
Root cause
No guard exists before the cublasSgemm/cublasGemmEx calls in ggml_cuda_op_mul_mat_cublas() to handle zero-dimension sub-matrices. Unlike OpenBLAS and MKL which define zero-size GEMMs as no-ops, cuBLAS treats them as invalid parameters and aborts.
Fix
Add an early-return guard after row_diff and ldc are computed. When any GEMM dimension (m, n, k) is zero, return immediately. This is safe because dst_dd_i is untouched and the caller does not read partial results.
Tested
Qwen3.6-27B Q4_K_M + DFlash draft, 55k context, RTX 3090 24GB
Related upstream issues
Authored-by: Gaston Parravicini
Overview
Fix cublasSgemm/cublasGemmEx crash triggered during DFlash speculative decoding when draft verification produces zero-dimension sub-matrices. Adds an early-return guard in
ggml_cuda_op_mul_mat_cublas()before any cuBLAS call.Additional information
Requirements
-YES — Claude (Anthropic) was used to assist in root cause analysis,
identifying the zero-dimension guard as the fix, and drafting the commit
message. The patch was applied, compiled, and tested on hardware by the
author. All code reviewed line by line before submission.