CUBLAS_STATUS_INVALID_VALUE (1) cuda runtime_error ai_generated true

RuntimeError: CUBLAS_STATUS_INVALID_VALUE when calling cublasSgemmStridedBatched with lda=0

ID: cuda/cublas-gemm-invalid-lda

Also available as: JSON · Markdown · 中文
90%Fix Rate
85%Confidence
1Evidence
2023-06-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
CUDA 11.8 active
CUDA 12.1 active
cuBLAS 11.11 active
cuBLAS 12.0 active

Root Cause

cuBLAS GEMM or batched GEMM routines require leading dimensions (lda, ldb, ldc) to be non-zero and at least as large as the matrix width or height; a zero leading dimension causes an invalid value error.

generic

中文

cuBLAS GEMM 或批处理 GEMM 例程要求前导维度(lda、ldb、ldc)非零且至少与矩阵宽度或高度相同;零前导维度会导致无效值错误。

Official Documentation

https://docs.nvidia.com/cuda/cublas/index.html#cublas-t-routine

Workarounds

  1. 95% success Ensure lda, ldb, and ldc are at least 1 and >= number of rows (for column-major) or columns (for row-major). Example fix: if (lda == 0) lda = matrix_width;
    Ensure lda, ldb, and ldc are at least 1 and >= number of rows (for column-major) or columns (for row-major). Example fix: if (lda == 0) lda = matrix_width;
  2. 85% success Add a debug assertion before the cuBLAS call to check that leading dimensions are positive and valid. Example: assert(lda > 0 && lda >= rows);
    Add a debug assertion before the cuBLAS call to check that leading dimensions are positive and valid. Example: assert(lda > 0 && lda >= rows);

中文步骤

  1. Ensure lda, ldb, and ldc are at least 1 and >= number of rows (for column-major) or columns (for row-major). Example fix: if (lda == 0) lda = matrix_width;
  2. Add a debug assertion before the cuBLAS call to check that leading dimensions are positive and valid. Example: assert(lda > 0 && lda >= rows);

Dead Ends

Common approaches that don't work:

  1. 95% fail

    Increasing batch size or matrix dimensions does not fix a zero leading dimension; the error persists because the root cause is a programming mistake, not resource limits.

  2. 90% fail

    Switching to a different cuBLAS routine (e.g., from gemmStridedBatched to gemm) only masks the symptom; the caller still passes lda=0, which may crash later or produce wrong results.

  3. 85% fail

    Setting lda to a negative value (e.g., -1) causes a different error (CUBLAS_STATUS_NOT_INITIALIZED or segfault) but does not resolve the zero-layout issue.