# RuntimeError: cuDNN 错误：调用 cudnnSetRNNDescriptor_v8 时出现 CUDNN_STATUS_BAD_PARAM

- **ID:** `pytorch/cudnn-benchmark-algo-failure`
- **领域:** pytorch
- **类别:** runtime_error
- **错误码:** `CUDNN_STATUS_BAD_PARAM`
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在启用 torch.backends.cudnn.benchmark 的情况下，RNN 使用了 cuDNN 启发式搜索不支持的隐藏层大小或层数，导致 RNN 描述符初始化参数无效。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| PyTorch 2.0.0 | active | — | — |
| PyTorch 2.1.0 | active | — | — |
| cuDNN 8.9.0 | active | — | — |
| CUDA 11.8 | active | — | — |

## 解决方案

1. ```
   禁用 cuDNN 基准测试：torch.backends.cudnn.benchmark = False。如果错误仍然存在，可进一步设置 torch.backends.cudnn.enabled = False。
   ```
2. ```
   使用能被 64 整除的隐藏层大小（如 256 而非 250），并确保 num_layers <= 8。示例：model = nn.LSTM(input_size=128, hidden_size=256, num_layers=2, batch_first=True)。
   ```
3. ```
   在模型创建前设置 torch.backends.cudnn.enabled = False，强制使用 PyTorch 原生 RNN 实现，避免 cuDNN 内核。
   ```

## 无效尝试

- **** — Upgrading PyTorch alone does not fix the cuDNN parameter validation; the root cause is the RNN configuration, not the library version. (60% 失败率)
- **** — Setting torch.backends.cudnn.deterministic = True does not prevent the benchmark from running the failing heuristic; it only affects algorithm selection. (70% 失败率)
- **** — Changing batch size does not affect the RNN descriptor parameters (hidden_size, num_layers), so the error persists. (50% 失败率)
