PyTorch 检查GPU版本是否安装成功的操作

时间:2022-03-14 00:08:16

anaconda命令行下检查:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
(base) PS C:\Users\chenxuqi> conda deactivate
PS C:\Users\chenxuqi> conda activate ssd
(ssd) PS C:\Users\chenxuqi> python
Python 3.6.12 |Anaconda, Inc.| (default, Sep 9 2020, 00:29:25) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> print(torch.__version__)
0.4.0
>>> print(torch.cuda.is_available())
True
>>>
>>>
>>>

cmd命令行下检查cuda安装:

?
1
2
3
4
5
6
7
8
9
10
11
Microsoft Windows [版本 10.0.18363.1139]
(c) 2019 Microsoft Corporation。保留所有权利。
 
C:\Users\chenxuqi>nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2017 NVIDIA Corporation
Built on Fri_Sep__1_21:08:32_Central_Daylight_Time_2017
Cuda compilation tools, release 9.0, V9.0.176
 
C:\Users\chenxuqi>
C:\Users\chenxuqi>

执行使用GPU的代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
import time
import torch
 
##################################################
 
for i in range(1,10):
  start = time.time()
  a = torch.FloatTensor(i*100,1000,1000)
  a = a.cuda() #a = a
  a = torch.matmul(a,a)
  end = time.time() - start
  print(end)

执行结果:

注意,这里显存太小,溢出了...但是安装是成功的...

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Windows PowerShell
 
尝试新的跨平台 PowerShell https://aka.ms/pscore6
 
PS C:\Users\chenxuqi\Desktop\新建文件夹> & 'D:\Anaconda3\envs\ssd\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.10.332292344\pythonFiles\lib\python\debugpy\launcher' '50571' '--' 'c:\Users\chenxuqi\Desktop\新建文件夹\testGPU.py'
3.6260359287261963
0.6305170059204102
0.9055967330932617
1.3199987411499023
1.5979139804840088
2.0483360290527344
THCudaCheck FAIL file=c:\programdata\miniconda3\conda-bld\pytorch_1524549877902\work\aten\src\thc\generic/THCStorage.cu line=58 error=2 : out of memory
Traceback (most recent call last):
 File "c:\Users\chenxuqi\Desktop\新建文件夹\testGPU.py", line 10, in <module>
  a = torch.matmul(a,a)
RuntimeError: cuda runtime error (2) : out of memory at c:\programdata\miniconda3\conda-bld\pytorch_1524549877902\work\aten\src\thc\generic/THCStorage.cu:58
PS C:\Users\chenxuqi\Desktop\新建文件夹> conda activate ssd
PS C:\Users\chenxuqi\Desktop\新建文件夹>

补充:pytorch离线安装,验证gpu版安装成功

使用conda 命令在线安装pytorch会下载中断,添加pip清华大学镜像源https://pypi.tuna.tsinghua.edu.cn/simple/中下好torch,安装包,然后使用pip命令

?
1
pip install "下在的安装包的路径"

例如:

?
1
pip install "C:\Users\28614\Desktop\pytorch-nightly-cpu-1.0.0.dev20181222-py3.7_cpu_0.tar.bz2"

安装gpu版,需要安装cuda,和cudnn。

验证gpu版是否安装成功

?
1
2
import torch
print(torch.cuda.is_available())

返回True,则安装成功

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/m0_46653437/article/details/109575389