ubuntu18.04 source installation pytorch

The installation steps in this article do not include cuda

Reference materials:

【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 installation, used Tsinghua mirror image)

 

1. Install anaconda ( actually install miniconda ) (the following command selects the mirror image of Tsinghua, refer to [3])

1.1 curl -O https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh [Command]

(Or use the official mirror without trying curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh)

1.2 sh Miniconda3-latest-Linux-x86_64.sh 【Command】

     When installing, follow the prompts and press Enter or enter yes

Note: The ~/.bashrc will be modified during the installation process, and the conda path will be added to the PATH environment variable only after reopening the terminal or source ~/.bashrc

 

2. Installation dependencies (refer to the source code installation in [2])

conda install numpy ninja pyyaml ​​mkl mkl-include setuptools cmake cffi typing (mlk time is a bit longer 128.9M)

git clone --recursive https://github.com/pytorch/pytorch (Long time, I spent about 10 hours with a virtual machine)

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

 

Three. Problems encountered

3.1 The problem is described as follows:

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

......

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

Solution:

In tools/build_pytorch_libs.py, set

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


Change to max_jobs = '1'

 

Four. Test

from __future__ import print_function

import torch

x = torch.rand(5, 3)

print(x)

Output:

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]])

Encountered the following error during testing:

No module named 'torch._C'

The reason is that the program entered after running python in the source directory of pytorch cannot be executed in the source directory of pytorch, because torch will be installed under site-packages after installation, and that should be used. When running in the source code directory, the torch in the source code will be used, without _C (my installation directory generated _C.cpython-37m-x86_64-linux-gnu.so)

Guess you like

Origin blog.csdn.net/kh815/article/details/104411250