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. 我操作的思路
- cuda安装
查看教程: 1.ubunbu 22.04安装nvidia驱动.mhtml
chmod +x NVIDIA-Linux-x86_64-570.133.07.run
./NVIDIA-Linux-x86_64-570.133.07.run
nvidia-smi
- cudnn
官网: https://developer.nvidia.com/cudnn-downloads
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
- pytorch安装
pip3 install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cu128
- transforms
pip install transformers datasets tokenizers -i https://pypi.tuna.tsinghua.edu.cn/simple