# RuntimeError: CUBLAS_STATUS_INVALID_VALUE when calling cublasSgemmStridedBatched with lda=0

- **ID:** `cuda/cublas-gemm-invalid-lda`
- **Domain:** cuda
- **Category:** runtime_error
- **Error Code:** `CUBLAS_STATUS_INVALID_VALUE (1)`
- **Verification:** ai_generated
- **Fix Rate:** 90%

## 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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| CUDA 11.8 | active | — | — |
| CUDA 12.1 | active | — | — |
| cuBLAS 11.11 | active | — | — |
| cuBLAS 12.0 | active | — | — |

## Workarounds

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;** (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;
   ```
2. **Add a debug assertion before the cuBLAS call to check that leading dimensions are positive and valid. Example: assert(lda > 0 && lda >= rows);** (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);
   ```

## Dead Ends

- **** — 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. (95% 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. (90% 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. (85% fail)
