CUDA error: no kernel image is available for execution on the device
1. 报错
在cuda+cudnn+pytorch环境下运行大模型,报如下错误信息:
Setting `pad_token_id` to `eos_token_id`:151643 for open-end generation.
收到请求 /chat error===========================>error:CUDA error: no kernel image is available for execution on the device
CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect.
For debugging consider passing CUDA_LAUNCH_BLOCKING=1
Compile with `TORCH_USE_CUDA_DSA` to enable device-side assertions.
2. 如何解决
出现这种问题的原因就是环境问题:
2.1 cuda+cudnn配置是否正确
2.2 cuda版本与pytorch的版本是否匹配
这个没有什么好办法因为每个人机器不一样(安装流程就是:1.安装cuda+cudnn;2.安装pytorch;3.安装transforms;),就是利用conda创建一个新的环境,然后重新安装环境
这里提供一个测试环境是否有问题的python代码,如果这个代码能够执行,那么cuda+cudnn+pytorch环境就基本没有什么问题:
import torch # 导入 PyTorch 库
print("PyTorch 版本:", torch.__version__) # 打印 PyTorch 的版本号
# 检查 CUDA 是否可用,并设置设备("cuda:0" 或 "cpu")
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print("设备:", device) # 打印当前使用的设备
print("CUDA 可用:", torch.cuda.is_available()) # 打印 CUDA 是否可用
print("cuDNN 已启用:", torch.backends.cudnn.enabled) # 打印 cuDNN 是否已启用
# 打印 PyTorch 支持的 CUDA 和 cuDNN 版本
print("支持的 CUDA 版本:", torch.version.cuda)
print("cuDNN 版本:", torch.backends.cudnn.version())
# 创建两个随机张量(默认在 CPU 上)
x = torch.rand(5, 3)
y = torch.rand(5, 3)
# 将张量移动到指定设备(CPU 或 GPU)
x = x.to(device)
y = y.to(device)
# 对张量进行逐元素相加
z = x + y
# 打印结果
print("张量 z 的值:")
print(z) # 输出张量 z 的内容>>>
print("PyTorch 版本:", torch.__version__) # 打印 PyTorch 的版本号
# 检查 CUDA 是否可用,并设置设备("cuda:0" 或 "cpu")
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print("设备:", device) # 打印当前使用的设备
print("CUDA 可用:", torch.cuda.is_available()) # 打印 CUDA 是否可用
print("cuDNN 已启用:", torch.backends.cudnn.enabled) # 打印 cuDNN 是否已启用
# 打印 PyTorch 支持的 CUDA 和 cuDNN 版本
print("支持的 CUDA 版本:", torch.version.cuda)
print("cuDNN 版本:", torch.backends.cudnn.version())
# 创建两个随机张量(默认在 CPU 上)
x = torch.rand(5, 3)
y = torch.rand(5, 3)
# 将张量移动到指定设备(CPU 或 GPU)
x = x.to(device)
y = y.to(device)
# 对张量进行逐元素相加
z = x + y
# 打印结果
print("张量 z 的值:")
print(z) # 输出张量 z 的内容
3. 我操作的思路
0. cuda安装
0.1 上nvidia官网下载驱动,安装
官网地址: 下载 NVIDIA 官方驱动 | NVIDIA
chmod +x NVIDIA-Linux-x86_64-570.133.07.run
./NVIDIA-Linux-x86_64-570.133.07.run
安装后,检测驱动是否安装成功命令: nvidia-smi
0.2 安装cuda
官网地址: CUDA Toolkit Archive | NVIDIA Developer
选择版本后,安装官网提示进行安装,注意和nvidia-smi命令展示的cuda版本一致。
1. cudnn
官网: https://developer.nvidia.com/cudnn-downloads
这个没有版本限制,直接下载最新,按照官网提示进行下载就好了,这里只是一个示例:
wget https://developer.download.nvidia.com/compute/cudnn/9.8.0/local_installers/cudnn-local-repo-ubuntu2404-9.8.0_1.0-1_amd64.deb
sudo dpkg -i cudnn-local-repo-ubuntu2404-9.8.0_1.0-1_amd64.deb
sudo cp /var/cudnn-local-repo-ubuntu2404-9.8.0/cudnn-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cudnn
3. pytorch安装
pip3 install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cu128
4. transforms
pip install transformers datasets tokenizers -i https://pypi.tuna.tsinghua.edu.cn/simple
更多推荐
所有评论(0)