# 运行时错误：调用 cublasGemmEx 时返回 CUBLAS_STATUS_NOT_SUPPORTED

- **ID:** `cuda/cublas-gemm-params-unsupported-combination`
- **领域:** cuda
- **类别:** runtime_error
- **错误码:** `CUBLAS_STATUS_NOT_SUPPORTED`
- **验证级别:** ai_generated
- **修复率:** 82%

## 根因

当前 GPU 架构上的 cuBLAS 库不支持输入矩阵数据类型（atype、btype、ctype）与计算类型的组合。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| CUDA 11.8 | active | — | — |
| CUDA 12.1 | active | — | — |
| cuBLAS 11.11 | active | — | — |
| cuBLAS 12.0 | active | — | — |

## 解决方案

1. ```
   使用 torch.cuda.is_bf16_supported() 检查 bfloat16 支持情况后再使用。例如：if torch.cuda.is_bf16_supported(): model = model.to(torch.bfloat16) else: model = model.to(torch.float16)
   ```
2. ```
   显式设置计算类型以匹配输入类型。在 PyTorch 中，使用 torch.set_default_dtype(torch.float32) 或将张量转换为支持的类型组合，如 Ampere+ GPU 上的 float16。
   ```
3. ```
   通过设置环境变量 CUBLAS_WORKSPACE_CONFIG=:4096:8 禁用 cuBLAS 并回退到自定义内核。这会强制 cuBLAS 使用可能支持该类型组合的不同代码路径。
   ```

## 无效尝试

- **** — CUDA version alone doesn't guarantee support; the GPU's compute capability (e.g., sm_70 vs sm_80) determines which type combinations are valid. (65% 失败率)
- **** — The algorithm parameter doesn't change data type compatibility; it only affects performance and precision for supported type combinations. (90% 失败率)
- **** — While float32 is widely supported, this workaround may cause out-of-memory errors for large models or reduce performance if the original types were optimized (e.g., half precision). (40% 失败率)
