RuntimeError: 调用 cublasSgemmStridedBatched 时出现 CUBLAS_STATUS_INVALID_VALUE,lda=0
RuntimeError: CUBLAS_STATUS_INVALID_VALUE when calling cublasSgemmStridedBatched with lda=0
ID: cuda/cublas-gemm-invalid-lda
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| CUDA 11.8 | active | — | — | — |
| CUDA 12.1 | active | — | — | — |
| cuBLAS 11.11 | active | — | — | — |
| cuBLAS 12.0 | active | — | — | — |
根因分析
cuBLAS GEMM 或批处理 GEMM 例程要求前导维度(lda、ldb、ldc)非零且至少与矩阵宽度或高度相同;零前导维度会导致无效值错误。
English
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.
官方文档
https://docs.nvidia.com/cuda/cublas/index.html#cublas-t-routine解决方案
-
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;
-
Add a debug assertion before the cuBLAS call to check that leading dimensions are positive and valid. Example: assert(lda > 0 && lda >= rows);
无效尝试
常见但无效的做法:
-
95% 失败
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.
-
90% 失败
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.
-
85% 失败
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.