ubuntu18.04源码安装pytorch

本文安装的步骤不包含cuda

参考资料:

【1】https://pytorch.org/get-started/locally/    (官网)

【2】https://github.com/pytorch/pytorch#from-source  (pytorch github项目)

【3】https://blog.csdn.net/qq_15192373/article/details/81091098 (anaconda安装,使用的清华的镜像)

 

一.安装anaconda(实际是安装miniconda)(下面的命令选择的是清华的镜像,参考的【3】)

1.1 curl -O https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh 【命令】

(或者使用官方镜像,没有试curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh)

1.2 sh Miniconda3-latest-Linux-x86_64.sh  【命令】

     安装的时候按照提示按回车键或输入yes

注意:安装的过程中会修改~/.bashrc,要重新打开终端或source ~/.bashrc才会把conda的路径加入到PATH环境变量中

 

二.安装依赖(参考【2】中的源码安装)

conda install numpy ninja pyyaml mkl mkl-include setuptools cmake cffi typing  (mlk的时间有点长128.9M)

git clone --recursive https://github.com/pytorch/pytorch (时间较长,我用虚拟机花了10个小时左右)

cd pytorch

(# if you are updating an existing checkout

git submodule sync

git submodule update --init --recursive)

export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}

python setup.py install

 

三.遇到的问题

3.1 问题描述如下:

FAILED: caffe2/CMakeFiles/torch_cpu.dir/ideep/operators/operator_fallback_ideep.cc.o

......

c++: internal compiler error: Killed (program cc1plus)

解决办法:

在tools/build_pytorch_libs.py中将

max_jobs = os.getenv('MAX_JOBS', str(multiprocessing.cpu_count()))

改为
max_jobs = '1'

 

四.测试

from __future__ import print_function

import torch

x = torch.rand(5, 3)

print(x)

输出:

tensor([[0.3380, 0.3845, 0.3217],

           [0.8337, 0.9050, 0.2650],

           [0.2979, 0.7141, 0.9069],  

           [0.1449, 0.1132, 0.1375],

           [0.4675, 0.3947, 0.1426]])

在测试的时候遇到下面的错误:

No module named 'torch._C'

原因是在pytorch的源码目录运行的python然后输入的程序,不能再pytorch的源码目录执行,因为安装完后会在site-packages下安装torch,应该用那个。而在源码目录下运行时会用源码里的torch,里面没有_C(我的安装目录生成的是_C.cpython-37m-x86_64-linux-gnu.so)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章