RuntimeError: cuDNN 错误:调用 cudnnSetRNNDescriptor_v8 时出现 CUDNN_STATUS_BAD_PARAM
RuntimeError: cuDNN error: CUDNN_STATUS_BAD_PARAM when calling cudnnSetRNNDescriptor_v8
ID: pytorch/cudnn-benchmark-algo-failure
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| PyTorch 2.0.0 | active | — | — | — |
| PyTorch 2.1.0 | active | — | — | — |
| cuDNN 8.9.0 | active | — | — | — |
| CUDA 11.8 | active | — | — | — |
根因分析
在启用 torch.backends.cudnn.benchmark 的情况下,RNN 使用了 cuDNN 启发式搜索不支持的隐藏层大小或层数,导致 RNN 描述符初始化参数无效。
English
torch.backends.cudnn.benchmark enabled with an RNN that uses a non-default hidden size or num_layers that cuDNN does not support in its heuristic search, causing invalid parameters in RNN descriptor initialization.
官方文档
https://pytorch.org/docs/stable/notes/cuda.html#cudnn-benchmark解决方案
-
禁用 cuDNN 基准测试:torch.backends.cudnn.benchmark = False。如果错误仍然存在,可进一步设置 torch.backends.cudnn.enabled = False。
-
使用能被 64 整除的隐藏层大小(如 256 而非 250),并确保 num_layers <= 8。示例:model = nn.LSTM(input_size=128, hidden_size=256, num_layers=2, batch_first=True)。
-
在模型创建前设置 torch.backends.cudnn.enabled = False,强制使用 PyTorch 原生 RNN 实现,避免 cuDNN 内核。
无效尝试
常见但无效的做法:
-
60% 失败
Upgrading PyTorch alone does not fix the cuDNN parameter validation; the root cause is the RNN configuration, not the library version.
-
70% 失败
Setting torch.backends.cudnn.deterministic = True does not prevent the benchmark from running the failing heuristic; it only affects algorithm selection.
-
50% 失败
Changing batch size does not affect the RNN descriptor parameters (hidden_size, num_layers), so the error persists.