Skip to content

ggml-cuda: fix cublasSgemm crash with zero-dim matrices in speculative decoding#39

Merged
spiritbuun merged 2 commits into
spiritbuun:masterfrom
Tongas:fix/cublas-zero-dim-speculative-decoding
May 5, 2026
Merged

ggml-cuda: fix cublasSgemm crash with zero-dim matrices in speculative decoding#39
spiritbuun merged 2 commits into
spiritbuun:masterfrom
Tongas:fix/cublas-zero-dim-speculative-decoding

Conversation

@Tongas
Copy link
Copy Markdown

@Tongas Tongas commented May 3, 2026

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

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

  • I have read and agree with the contributing guidelines
  • AI usage disclosure:
    -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.

…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
@Tongas Tongas marked this pull request as draft May 3, 2026 03:10
@spiritbuun spiritbuun marked this pull request as ready for review May 5, 2026 17:55
@spiritbuun spiritbuun merged commit dcf305b into spiritbuun:master May 5, 2026
9 of 47 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants