问题描述
想使用GPU加快程序运行速度,pip安装完torch后,检测时候报错。
pip install torch
安装时成功了,
但是测试时候报错:
进入python环境
(cat) C:\Users\asus>python
导入torch
>>> import torch
>>> print(torch.cuda.is_available()) #cuda是否可用
>>>print(torch.cuda.get_device_name(0)) #返回设备索引
>>>print(torch.cuda.device_count()) # 返回GPU的数量
>>>print(torch.cuda.current_device()) # 返回当前设备索引
>>> print(torch.rand(3,3).cuda())
从报错来看,发现还不能用。
(cat) C:\Users\asus>python
Python 3.7.10 | packaged by conda-forge | (default, Feb 19 2021, 15:37:01) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> print(torch.cuda.is_available())
False
解决方案
应该是版本问题,cuda和torch版本不对应。
NVIDIA-SMI 516.94 Driver Version: 516.94 CUDA Version: 11.7
torch安装的版本应该是torch-1.13.1
我的cuda是11.7从这个网站上可以看到他们的具体对应关系:
https://download.pytorch.org/whl/torch_stable.html
python版本3.7.10,cuda11.7,torch1.13.1。
有一个说法是返回false说明使用的cpu版本torch,不是GPU版本的。
因此需要找GPU版本的,地址如下:
https://download.pytorch.org/whl/torch_stable.html
选好版本,选好使用pip安装还是conda安装,选好什么系统,选好平台cuda版本,然后命令复制过来就行:
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117
然后就可以了。只不过这个包有点大,大致需要十几分钟才能安装完毕。
(cat) C:\Users\asus>pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117
Looking in indexes: https://pypi.org/simple, https://download.pytorch.org/whl/cu117
Collecting torch
Downloading https://download.pytorch.org/whl/cu117/torch-1.13.1%2Bcu117-cp37-cp37m-win_amd64.whl (2255.6 MB)
---------------------------------------- 2.3/2.3 GB ? eta 0:00:00
Collecting torchvision
Downloading https://download.pytorch.org/whl/cu117/torchvision-0.14.1%2Bcu117-cp37-cp37m-win_amd64.whl (4.8 MB)
---------------------------------------- 4.8/4.8 MB 2.6 MB/s eta 0:00:00
Collecting torchaudio
Downloading https://download.pytorch.org/whl/cu117/torchaudio-0.13.1%2Bcu117-cp37-cp37m-win_amd64.whl (2.3 MB)
---------------------------------------- 2.3/2.3 MB 2.8 MB/s eta 0:00:00
Requirement already satisfied: typing-extensions in d:\program\anaconda3\envs\cat\lib\site-packages (from torch) (4.4.0)
Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in d:\program\anaconda3\envs\cat\lib\site-packages (from torchvision) (9.4.0)
Requirement already satisfied: requests in d:\program\anaconda3\envs\cat\lib\site-packages (from torchvision) (2.28.1)
Requirement already satisfied: numpy in d:\program\anaconda3\envs\cat\lib\site-packages (from torchvision) (1.21.6)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in d:\program\anaconda3\envs\cat\lib\site-packages (from requests->torchvision) (1.26.4)
Requirement already satisfied: idna<4,>=2.5 in d:\program\anaconda3\envs\cat\lib\site-packages (from requests->torchvision) (3.4)
Requirement already satisfied: certifi>=2017.4.17 in d:\program\anaconda3\envs\cat\lib\site-packages (from requests->torchvision) (2022.12.7)
Requirement already satisfied: charset-normalizer<3,>=2 in d:\program\anaconda3\envs\cat\lib\site-packages (from requests->torchvision) (2.1.1)
Installing collected packages: torch, torchvision, torchaudio
Successfully installed torch-1.13.1+cu117 torchaudio-0.13.1+cu117 torchvision-0.14.1+cu117
(cat) C:\Users\asus>
测试
这样写的目的是便于对应看清楚每个参数的值:
import torch
print("torch.cuda.is_available()",torch.cuda.is_available()) # cuda是否可用
print("torch.cuda.current_device()",torch.cuda.current_device()) # 返回当前设备索引
print("torch.cuda.device_count()",torch.cuda.device_count()) # 返回GPU的数量
print("torch.cuda.get_device_name(0)",torch.cuda.get_device_name(0))
下面的看起来清爽一些:
import torch
print(torch.cuda.is_available()) # cuda是否可用
print(torch.cuda.current_device()) # 返回当前设备索引
print(torch.cuda.device_count()) # 返回GPU的数量
print(torch.cuda.get_device_name(0))
结果展示:
在我没有安装好gpu版本的时候,海报过这个错误:
但是都随着最终的版本安装完毕而解决掉了。
感谢博主Java并发编程的艺术的文章: