解决NVIDIA RTX 5080与PyTorch的兼容性问题

ubuntu系统24.04 ,安装了anaconda和spyder,安装了pytorch,
测试pytorch以及GPU:

import torch
import time

def benchmark_test():
    # 设置测试参数
    size = 1000
    iterations = 100
    
    print(f"=== 性能测试 ({iterations} 次 {size}x{size} 矩阵乘法) ===")
    
    # CPU测试
    device_cpu = torch.device('cpu')
    a = torch.randn(size, size, device=device_cpu)
    b = torch.randn(size, size, device=device_cpu)
    
    start_time = time.time()
    for _ in range(iterations):
        c = torch.mm(a, b)
    cpu_time = time.time() - start_time
    print(f"CPU 平均时间: {cpu_time/iterations:.4f} 秒")
    
    # GPU测试(如果可用)
    if torch.cuda.is_available():
        device_gpu = torch.device('cuda')
        a_gpu = torch.randn(size, size, device=device_gpu)
        b_gpu = torch.randn(size, size, device=device_gpu)
        
        # 预热GPU
        for _ in range(10):
            torch.mm(a_gpu, b_gpu)
        torch.cuda.synchronize()
        
        start_time = time.time()
        for _ in range(iterations):
            c_gpu = torch.mm(a_gpu, b_gpu)
        torch.cuda.synchronize()
        gpu_time = time.time() - start_time
        print(f"GPU 平均时间: {gpu_time/iterations:.4f} 秒")
        print(f"GPU 加速比: {cpu_time/gpu_time:.2f}x")

benchmark_test()

报错:

%runfile '/home/user/Documents/seg_models/attenunet/未命名2.py' --wdir
=== 性能测试 (100 次 1000x1000 矩阵乘法) ===
CPU 平均时间: 0.0020 秒
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
File ~/Documents/seg_models/attenunet/未命名2.py:49
     46         print(f"GPU 平均时间: {gpu_time/iterations:.4f} 秒")
     47         print(f"GPU 加速比: {cpu_time/gpu_time:.2f}x")
---> 49 benchmark_test()

File ~/Documents/seg_models/attenunet/未命名2.py:33, in benchmark_test()
     31 if torch.cuda.is_available():
     32     device_gpu = torch.device('cuda')
---> 33     a_gpu = torch.randn(size, size, device=device_gpu)
     34     b_gpu = torch.randn(size, size, device=device_gpu)
     36     # 预热GPU

RuntimeError: 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.

原因:

错误 "CUDA error: no kernel image is available for execution on the device" 通常发生在 CUDA 版本、GPU 架构和 PyTorch 版本不兼容时。
系统 CUDA 是 12.0,而 PyTorch 是为 12.1 编译的.

解决办法:
首先利用conda activate 进入环境:

# 1. 卸载任何旧的PyTorch安装 (如果之前安装过)
pip uninstall torch torchvision torchaudio -y
pip cache purge # 清理pip缓存

# 2. 安装支持CUDA 12.8的PyTorch Nightly Build
# 注意:--pre 标志用于安装预发布版本 (如 nightly build)
pip install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cu128

解决方案来源:
聚合AI平台
Claude 、Gemini、GPT 等大模型聚合ai平台

显卡算力来源:
显卡云算力平台

成功解决:
图片

posted @ 2026-01-22 16:43  点影成金  阅读(316)  评论(0)    收藏  举报