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. 我操作的思路

  1. 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

  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

  1. pytorch安装

pip3 install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cu128

  1. transforms

pip install transformers datasets tokenizers -i https://pypi.tuna.tsinghua.edu.cn/simple

相关推荐
liuyunshengsir11 分钟前
chromadb 安装和使用
人工智能·大模型
FIT2CLOUD飞致云17 分钟前
全面支持MCP协议,开启便捷连接之旅,MaxKB知识库问答系统v1.10.3 LTS版本发布
人工智能·开源
云水木石23 分钟前
ChatGPT-4o 在汉字显示上进步巨大
人工智能·chatgpt
Mr_LeeCZ39 分钟前
PyTorch 深度学习 || 7. Unet | Ch7.1 Unet 框架
人工智能·深度学习·机器学习
James. 常德 student42 分钟前
多GPU训练
人工智能·pytorch·深度学习
Jozky861 小时前
大语言模型在端到端智驾中的应用
人工智能·语言模型·自然语言处理
Y1nhl1 小时前
搜广推校招面经六十六
pytorch·python·深度学习·机器学习·广告算法·推荐算法·搜索算法
脑洞专家1 小时前
基于改进的点线融合和关键帧选择的视觉SLAM 方法
人工智能·机器学习·计算机视觉
丶Darling.2 小时前
深度学习与神经网络 | 邱锡鹏 | 第三章学习笔记
深度学习·神经网络·学习
明月看潮生3 小时前
青少年编程与数学 02-015 大学数学知识点 09课题、专业相关性分析
人工智能·青少年编程·数据科学·编程与数学·大学数学