cuda 9.0 安装torch 0.4_一次不太顺畅的Pytorch编译安装的后记

4de393f30d0bdaba9a0931b6b4ec390a.png

一,起因

学校主力机没有带回家,本来打算在家随便看点东西开学返校继续搞,因为众所周知的原因,计划破产了。家里电脑比较旧,E3 2130V2,8G内存还是可以凑活用一下。Win10下换好新驱动,装完CUDA,官网找好安装命令,装完打开跑了一下就发现一个不太对的问题:

>>> import torch
>>> torch.cuda.is_available()
True
>>> x = torch.Tensor([1.0])
>>> xx = x.cuda()
E:Python37Libsite-packagestorchcuda__init__.py:134: UserWarning:
    Found GPU0 GeForce GTX 770 which is of cuda capability 3.0.
    PyTorch no longer supports this GPU because it is too old.
    The minimum cuda capability that we support is 3.5.

  warnings.warn(old_gpu_warn % (d, name, major, capability[1]))

实际上如果进行一些操作会直接爆error,CUDA是用不了的。

CUDA没啥问题,但是GTX 770太旧了跑不起来,查了一下这个问题其实挺多人都有,解决方案也不难,源码安装或者换显卡。贫穷的我觉得还是编译安装吧(然后就掉坑里了)

二,编译遇到的一些坑

Win下编译来说,可能有坑,而且比较玄学。主要参考

https://github.com/pytorch/pytorch#from-source​github.com

具体步骤链接里有,暂且不说了。说几点需要注意的吧,官网给的命令

cmd
:: [Optional] Only add the next two lines if you need Python 2.7. If you use Python 3, ignore these two lines.
set MSSdk=1
set FORCE_PY27_BUILD=1

:: [Optional] If you want to build with VS 2019 generator, please change the value in the next line to `Visual Studio 16 2019`.
:: Note: This value is useless if Ninja is detected. However, you can force that by using `set USE_NINJA=OFF`.
set CMAKE_GENERATOR=Visual Studio 15 2017

:: Read the content in the previous section carefully before you proceed.
:: [Optional] If you want to override the underlying toolset used by Ninja and Visual Studio with CUDA, please run the following script block.
:: "Visual Studio 2017 Developer Command Prompt" will be run automatically.
:: Make sure you have CMake >= 3.12 before you do this when you use the Visual Studio generator.
:: It's an essential step if you use Python 3.5.
set CMAKE_GENERATOR_TOOLSET_VERSION=14.11
set DISTUTILS_USE_SDK=1
for /f "usebackq tokens=*" %i in (`"%ProgramFiles(x86)%Microsoft Visual StudioInstallervswhere.exe" -version [15^,16^) -products * -latest -property installationPath`) do call "%iVCAuxiliaryBuildvcvarsall.bat" x64 -vcvars_ver=%CMAKE_GENERATOR_TOOLSET_VERSION%

:: [Optional] If you want to override the cuda host compiler
set CUDAHOSTCXX=C:Program Files (x86)Microsoft Visual Studio2017EnterpriseVCToolsMSVC14.11.25503binHostX64x64cl.exe

python setup.py install

CMAKE_GENERATOR这里的值,如果确定不用Ninja一定要有`set USE_NINJA=OFF`,否则就应该是CMAKE_GENERATOR=Ninja

如果编译需要CUDA,务必确认MSVC版本和CUDA版本能对上(参考链接里有对应表)

虽然是可选项,CUDAHOSTCXX如果没有出错,不要定义这行,VS2019 MSVC14.24下编译CUDA组件时会报错

务必保证编译文件夹所在硬盘剩余空间够大,编译结束之后文件夹有27G。内存不大的虚拟内存设置大一点。

最重要的一点,仔细看官方的说明,普通小问题基本都可以解决,真有Bug可以去社区发帖。(U1S1,win下玄学bug确实多)

三,一次成功的编译

环境:

  • cmake 3.14.0
  • ninja 1.9.0.post1
  • python 3.7.4
  • Visual Studio 2019 16.4.5
  • MSVC 19.24.28316
  • CUDA 10.2
  • cudnn 7.6.5

从CMD开始如下

E:>for /f "usebackq tokens=*" %i in (`"%ProgramFiles(x86)%Microsoft Visual StudioInstallervswhere.exe" -version [16^,17^) -products * -latest -property installationPath`) do call "%iVCAuxiliaryBuildvcvarsall.bat" x64 -vcvars_ver=%CMAKE_GENERATOR_TOOLSET_VERSION%

E:>call "C:Program Files (x86)Microsoft Visual Studio2019CommunityVCAuxiliaryBuildvcvarsall.bat" x64 -vcvars_ver=%CMAKE_GENERATOR_TOOLSET_VERSION%
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.4.5
** Copyright (c) 2019 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

E:>set DISTUTILS_USE_SDK=1

E:>set CMAKE_GENERATOR=Ninja

E:>cd pytorch

E:pytorch>python setup.py build

电脑内存不大,CPU也渣,等了大概三个小时,终于看见这个画面

27a8781029d2a6e36a30fce634e0773f.png

python setup.py install安装一下

跑个小测试

>>> import torch
>>>
>>> x=torch.randn(1)
>>> device=torch.device("cuda")
>>> y=torch.ones_like(x,device=device)
E:Python37Libsite-packagestorchcuda__init__.py:132: UserWarning:
    Found GPU0 GeForce GTX 770 which is of cuda capability 3.0.
    PyTorch no longer supports this GPU because it is too old.
    The minimum cuda capability that we support is 3.5.

  warnings.warn(old_gpu_warn % (d, name, major, capability[1]))
>>> x=x.to(device)
>>> z=x+y
>>> print(z)
tensor([1.4036], device='cuda:0')

warning还是有,但不爆error了,凑活用吧。(再折腾一次可真受不了)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值