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

相关推荐
HUIMU_17 分钟前
DAY12&DAY13-新世纪DL(Deeplearning/深度学习)战士:破(改善神经网络)1
人工智能·深度学习
致Great38 分钟前
DeepResearch开源与闭源方案对比
人工智能·chatgpt
黎燃1 小时前
AI驱动的供应链管理:需求预测实战指南
人工智能
天波信息技术分享1 小时前
AI云电脑盒子技术分析——从“盒子”到“算力云边缘节点”的跃迁
人工智能·电脑
CoderJia程序员甲1 小时前
GitHub 热榜项目 - 日榜(2025-08-16)
人工智能·ai·开源·github
KirkLin1 小时前
Kirk:练习时长两年半的AI Coding经验
人工智能·程序员·全栈
mit6.8241 小时前
[1Prompt1Story] 注意力机制增强 IPCA | 去噪神经网络 UNet | U型架构分步去噪
人工智能·深度学习·神经网络
挽淚2 小时前
(小白向)什么是Prompt,RAG,Agent,Function Calling和MCP ?
人工智能·程序员
Jina AI2 小时前
回归C++: 在GGUF上构建高效的向量模型
人工智能·算法·机器学习·数据挖掘·回归
Coovally AI模型快速验证2 小时前
YOLO、DarkNet和深度学习如何让自动驾驶看得清?
深度学习·算法·yolo·cnn·自动驾驶·transformer·无人机