# 运行时错误：cuDNN 错误：调用 cudnnBatchNormalizationForwardTraining 时返回 CUDNN_STATUS_BAD_PARAM，epsilon=1e-06

- **ID:** `cuda/cudnn-bn-epsilon-too-small`
- **领域:** cuda
- **类别:** runtime_error
- **错误码:** `CUDNN_STATUS_BAD_PARAM`
- **验证级别:** ai_generated
- **修复率:** 92%

## 根因

cuDNN 批量归一化要求 epsilon 至少为 1e-5（对于 float16 等某些数据类型要求更高），以避免数值不稳定；1e-6 的值太小，会触发 BAD_PARAM 错误。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| cuDNN 8.9.5 | active | — | — |
| cuDNN 9.0 | active | — | — |
| PyTorch 2.0 | active | — | — |
| PyTorch 2.1 | active | — | — |

## 解决方案

1. ```
   将 epsilon 设置为 >= 1e-5 的值。在 PyTorch 中：nn.BatchNorm2d(num_features, eps=1e-5)。对于 float16 模型，使用 eps=1e-4 或更高。这是推荐的修复方法。
   ```
2. ```
   如果使用硬编码 epsilon 的预训练模型，请在加载后覆盖它：model.bn_layer.eps = 1e-5。然后根据需要重新初始化批量归一化统计信息。
   ```
3. ```
   仅将批量归一化层转换为 float32：model.bn_layer = model.bn_layer.float()。这允许使用较小的 epsilon 值，但可能会增加内存使用量。
   ```

## 无效尝试

- **** — While it avoids the BAD_PARAM error, a large epsilon reduces the effectiveness of batch normalization, potentially degrading model accuracy. (30% 失败率)
- **** — This works but disables all cuDNN optimizations, significantly slowing down training. It's an overreaction if only the epsilon is wrong. (10% 失败率)
- **** — The error halts execution immediately; ignoring it is not possible without modifying the source code to catch the exception. (100% 失败率)
