Ubuntu编译GPU版本Pytorch
摘要
两年前,疫情爆发后,呆在家中,无奈在有限的环境下编译了GPU版本Pytorch,这是当时的总结。前面当时编出来的whl包和源码没有留存。讽刺的是,现在我再按照自己的总结编译Pytorch时,依然走了很多的弯路。先前的总结没有注意所编译的pytorch版本,这次需要编译特定版本,对第三方的库git clone时也令人崩溃。希望能给到同样需要编译pytorch的人一些帮助。
环境
操作系统:Ubuntu18.04
硬件环境:
CPU: i5 3210m
RAM: 6G
显卡: GT640m
显卡驱动:470.103.01
CUDA: cuda10.0
CC: gcc7.5
CXX: g++7.5
python: 3.7.4
cmake:3.13
clang: 6.0.0
需要编译的pytorch:1.6.0
注意:
显卡型号、cuda版本和cudnn版本三者需要配合。比如CUDA11.0不支持GT640m显卡。新版本的Pytorch(v1.10.1)要求更高版本的CUDA:
CUDA10.0不支持7以上的GCC版本,而Ubuntu20.10只有8版本以上的GCC下载:PyTorch requires CUDA 10.2 or above.
cuda/include/crt/host_config.h:129:2: error: #error – unsupported GNU version! gcc versions later than 7 are not supported!
129 | #error – unsupported GNU version! gcc versions later than 7 are not supported!
源码获取
获取特定tag的pytorch源代码,并同步第三方子模块
git clone -b v1.6.0 --depth=1 /mirrors/pytorch --recursive
上面的命令会clone下pytorch代码,但第三方子模块代码不一定都能clone下来。接下来执行下面的命令:
cd pytorchgit submodule syncgit submodule update --init --recursive
可能会执行多次
git submodule syncgit submodule update --init --recursive
注意:当前版本的pytorch(1.6.0-rc7)所依赖的第三方子模块版本查看方式如下: 第三方库名字@后面的字符串就是子模块提交的id
git submodule update --init --recursive
会自动checkout对应的版本(例如FP16子模块的4dfe081):
编译
开始编译、安装:
python setup.py install
这次编译没有人为禁用功能,CMAKE检查环境后自动确定哪些功能启用。
共有4908个任务:
构建whl包:
python setup.py bdist_wheel
将在dist目录构建出whl包
使用Clang编译器编译
需要先设置环境变量:
export CMAKE_C_COMPILER=clangexport CMAKE_CPULS_COMPILER=clang++
再进行环境清理、编译、安装:
python setup.py cleanpython setup.py install