Torch reports an error AssertionError: Torch not compiled with CUDA enabled Solution torch adapts CUDA downgrade version, selects gpu version final solution

Error report

error one

Run these lines of commands under any python file

print("是否可用:", torch.cuda.is_available())        # 查看GPU是否可用
print("GPU数量:", torch.cuda.device_count())        # 查看GPU数量
print("torch方法查看CUDA版本:", torch.version.cuda)  # torch方法查看CUDA版本
print("GPU索引号:", torch.cuda.current_device())    # 查看GPU索引号
print("GPU名称:", torch.cuda.get_device_name(1))    # 根据索引号得到GPU名称

The output and error are as follows:

是否可用: True
...
RuntimeError: The NVIDIA driver on your system is too old (found version 10020).

Then the second error occurred after trying to downgrade the version:

error two

same order

print("是否可用:", torch.cuda.is_available())        # 查看GPU是否可用
print("GPU数量:", torch.cuda.device_count())        # 查看GPU数量
print("torch方法查看CUDA版本:", torch.version.cuda)  # torch方法查看CUDA版本
print("GPU索引号:", torch.cuda.current_device())    # 查看GPU索引号
print("GPU名称:", torch.cuda.get_device_name(1))    # 根据索引号得到GPU名称

Output result:

是否可用: False
...
AssertionError: Torch not compiled with CUDA enabled

Solution

First, enter nvidia-smi on the command line to view the CUDA version:

nvidia-smi

Check out the result:

The CUDA version of the server is 10.2.

Then go to the torch official website to view the torch version adapted to the old version of CUDA:

https://pytorch.org/get-started/previous-versions/

The following is to find such a torch version in the mirror source: 1.12.1 and the adaptation gpu

For example, we find in the torch image source of BFSU:

https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

We are the linux version, so choose linux:

saw the following:

This is what we are looking for. Right-click the blue word, 'Right click'-'Copy link address', get the download link, and install pytorch directly with this package:

conda install https://mirrors.bfsu.edu.cn/anaconda/cloud/pytorch/linux-64/pytorch-1.12.1-py3.8_cuda10.2_cudnn7.6.5_0.tar.bz2

Then use the following command to automatically install the adapted dependency package:

conda install pytorch

In this way, the installation is successful. Call the above python command again to test CUDA:

print("是否可用:", torch.cuda.is_available())        # 查看GPU是否可用
print("GPU数量:", torch.cuda.device_count())        # 查看GPU数量
print("torch方法查看CUDA版本:", torch.version.cuda)  # torch方法查看CUDA版本
print("GPU索引号:", torch.cuda.current_device())    # 查看GPU索引号
print("GPU名称:", torch.cuda.get_device_name(1))    # 根据索引号得到GPU名称

got the answer:

True
2
10.2
0
Tesla T4

torchvision is also the same:

conda install https://mirrors.bfsu.edu.cn/anaconda/cloud/pytorch/linux-64/torchvision-0.13.1-py38_cu102.tar.bz2
conda install torchvision

Then I will introduce these error reports in detail, so as to retell my experience of crossing the river by feeling the stones all the way.

The reason for the error RuntimeError: The NVIDIA driver on your system is too old (found version 10020).

Your installed torch does not match your CUDA version .

For example, in my environment, CUDA of 10.2 conflicts with pytorch of 1.13.0 (or 1.13.1, anyway, version 1.13).

NVIDIA too old solution

  1. 降低pytorch版本

  1. 升级CUDA。

解决办法很多,可以参考网上其他文章。本文重点在第二个报错Torch和CUDA的编译(compile)问题,因为我自己是使用学校的机房,没权限升级CUDA,所以介绍一下我尝试过的第一个办法,降低pytorch版本:

你在看这一段时,会发现前面和我最终的解决方法是一样的,但后面不同的方法让我走了弯路。

首先确定你的CUDA版本,在Linux命令行终端中输入nvidia-smi:

nvidia-smi

查看得到结果:

得到服务器的CUDA版本为10.2.

然后去torch官网中查看老版本CUDA适配的torch版本:

https://pytorch.org/get-started/previous-versions/

好了,到这一步,你可能就会按照它上面的操作,安装这几个版本的库了。

conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=10.2 -c pytorch

不过你可能会碰到的问题有:

  1. 某个包版本不匹配当前python版本

  1. 某个包版本和其他包冲突

  1. 某个包安装的版本和CUDA版本不匹配

可能你改进了第一、二个问题,终于把整套pytorch、torchvision下下来了,但你还是要面对第三个问题,就是继而报错AssertionError: Torch not compiled with CUDA enabled,不过我一步步介绍我的经历,先看看如何面对第一和第二个问题的:

自己调试去安装,我试过,最后还是选择了使用如下命令:

conda install --override-channels -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/ pytorch torchvision cudatoolkit=10.2

这条命令是说,指定使用镜像源https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/,指定cudatoolkit版本适配10.2的CUDA,然后根据这个适配,下载pytorch包和torchvision包。

下载好了,尝试代码,你会得到:

print("是否可用:", torch.cuda.is_available())        # 查看GPU是否可用
print("GPU数量:", torch.cuda.device_count())        # 查看GPU数量
print("torch方法查看CUDA版本:", torch.version.cuda)  # torch方法查看CUDA版本
print("GPU索引号:", torch.cuda.current_device())    # 查看GPU索引号
print("GPU名称:", torch.cuda.get_device_name(1))    # 根据索引号得到GPU名称

(输出结果:)
是否可用: False
...
AssertionError: Torch not compiled with CUDA enabled

这就是第二个报错了。

报错AssertionError: Torch not compiled with CUDA enabled的原因

而第二个报错则是因为你安装的torch是cpu版本的。比如你可以在你的conda环境下查找torch版本:

conda list | grep torch

结果如下:

(base) weinz@dlp01:~$ conda list | grep torch
(正常使用命令无法看到下面注释的一行,为了直观显示,我粘贴过来了)
# Name                    Version                   Build  Channel
pytorch                   1.12.1          cpu_py38he8d8e81_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
torchvision               0.13.1          cpu_py38h164cc8f_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main

其中包版本号Version后面的Build列,就能看到torch适配的是cpu版本。

发现问题后,改成gpu版本的就可以了。

具体方法就接上我最终的解决方案了:

具体解决方法

首先回顾一下我们需要的torch版本:

在镜像源中找到1.12.1以及适配gpu的pytorch、torchvision

进入镜像源https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

这就是我们要找的。右键-复制链接地址,直接用这个包安装pytorch:

conda install https://mirrors.bfsu.edu.cn/anaconda/cloud/pytorch/linux-64/pytorch-1.12.1-py3.8_cuda10.2_cudnn7.6.5_0.tar.bz2

可以查看一下,这样下载的是否是gpu版本的。输入查找命令:

conda list | grep torch

得到结果:

可以看到,显示的是cuda,这个就是gpu版本的了。

然后用以下命令,自动安装适配的依赖包:

conda install pytorch

这样就安装成功了,再次调用以上python命令测试CUDA:

print("是否可用:", torch.cuda.is_available())        # 查看GPU是否可用
print("GPU数量:", torch.cuda.device_count())        # 查看GPU数量
print("torch方法查看CUDA版本:", torch.version.cuda)  # torch方法查看CUDA版本
print("GPU索引号:", torch.cuda.current_device())    # 查看GPU索引号
print("GPU名称:", torch.cuda.get_device_name(1))    # 根据索引号得到GPU名称

得到结果:

True
2
10.2
0
Tesla T4

torchvision也是同理:

conda install https://mirrors.bfsu.edu.cn/anaconda/cloud/pytorch/linux-64/torchvision-0.13.1-py38_cu102.tar.bz2
conda install torchvision

总结

看着网上的文章,还有问chatGPT,一步步解决了这个问题,还是值得的。

有的方法在选择gpu版本的时候,选择这样:

conda install -c 镜像源 后面调试版本
conda install -c 'http...' pytorch=1.12.1 torchvision=0.13.0 cudatoolkit=10.2
conda install -c 'http...' pytorch=1.9.1 torchvision=0.8.0 cudatoolkit=10.2
...

一步步调版本,我在调的时候发现太搞了,而且试出来下好配套的了,可能下的也是cpu版本的。

具体可参考这篇文章:

https://blog.csdn.net/u013468614/article/details/125910538

可能更了解镜像源内部选择的方式,会有更好的解决方法。不过在此之前,这种直接下载你需要的版本然后让镜像源给你补依赖包才是最高效的方法。

Guess you like

Origin blog.csdn.net/m0_46948660/article/details/129205116