✗ 实验 123_2 失败: Deterministic behavior was enabled with either `torch.use_determi...
🚨 错误信息
✗ 实验 123_2 失败: Deterministic behavior was enabled with either `torch.use_deterministic_algorithms(True)` or `at::Context::setDeterministicAlgorithms(true)`, but this operation is not deterministic because it uses CuBLAS and you have CUDA >= 10.2. To enable deterministic behavior in this case, you must set an environment variable before running your PyTorch application: CUBLAS_WORKSPACE_CONFIG=:4096:8 or CUBLAS_WORKSPACE_CONFIG=:16:8. For more information, go to https://docs.nvidia.com/cuda/cublas/index.html#results-reproducibility
🤖 AI解决方案
错误翻译
原文:Deterministic behavior was enabled but this operation is not deterministic because it uses CuBLAS and you have CUDA >= 10.2
译文:已启用确定性行为,但由于使用了CuBLAS且CUDA版本>=10.2,此操作不是确定性的
原因分析
根本原因:PyTorch启用了确定性算法模式,但在CUDA 10.2及以上版本中,CuBLAS操作默认使用非确定性的工作空间配置。
具体位置:涉及CuBLAS运算的PyTorch操作(如矩阵乘法等线性代数运算)
解决方案
在执行Python代码前设置环境变量:
方法1:使用命令行
bash
Linux/Mac
export CUBLAS_WORKSPACE_CONFIG=:4096:8
python your_script.pyWindows (cmd)
set CUBLAS_WORKSPACE_CONFIG=:4096:8
python your_script.py
方法2:在Python代码中设置
python
import os
os.environ['CUBLAS_WORKSPACE_CONFIG'] = ':4096:8' # 或者 ':16:8'import torch
torch.use_deterministic_algorithms(True)
继续你的代码...
方法3:使用subprocess(推荐)
python
import subprocess
import osenv = os.environ.copy()
env['CUBLAS_WORKSPACE_CONFIG'] = ':4096:8'
subprocess.run(['python', 'your_script.py'], env=env)
️ 预防措施
推荐工具:
torch.backends.cudnn.deterministic = True配合CuBLAS配置