- CPU版本的MKL加速;
- 从源码构建pip版本的安装包;
CPU版本
默认版本,使用pip,直接安装torch和torchvision两个包,即可:
pip3 install torch torchvision
当需要使用MKL加速时,则需要源码编译,必须使用conda环境。同时,添加环境变量到~/.bashrc
。
vim ~/.bashrc
export NO_CUDA=1
export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}
编译源码,注意下载和编译时间较长,执行,直接编译至conda环境中:
git submodule update --init --recursive
cd pytorch
python setup.py install
如果,需要生成pip版本,参考,则需要使用命令:
python setup.py bdist_wheel
注意Python版本,由3.7版本生成的whl,只能装入3.7版本的环境中,位于dist中:
pytorch/dist/torch-1.1.0a0+7e73783-cp37-cp37m-linux_x86_64.whl
GPU版本
在https://pytorch.org/get-started/locally/中,直接选择对应的环境+CUDA版本,安装即可,如Python3.6+CUDA8.0:
下载:
wget https://download.pytorch.org/whl/cu80/torch-1.0.1.post2-cp37-cp37m-linux_x86_64.whl
# Python 3.6
pip3 install https://download.pytorch.org/whl/cu80/torch-1.0.1.post2-cp36-cp36m-linux_x86_64.whl
pip3 install torchvision
或 源码编译
export CMAKE_PREFIX_PATH="$(dirname $(which conda))/../" # [anaconda root directory]
# Install basic dependencies
conda install numpy pyyaml mkl mkl-include setuptools cmake cffi typing
# Add LAPACK support for the GPU
conda install -c pytorch magma-cuda80 # or magma-cuda90 if CUDA 9
git clone --recursive https://github.com/pytorch/pytorch
cd pytorch
python setup.py install
大约构建25m左右,输出
running install_egg_info
running egg_info
creating torch.egg-info
writing torch.egg-info/PKG-INFO
writing dependency_links to torch.egg-info/dependency_links.txt
writing entry points to torch.egg-info/entry_points.txt
writing top-level names to torch.egg-info/top_level.txt
writing manifest file 'torch.egg-info/SOURCES.txt'
reading manifest file 'torch.egg-info/SOURCES.txt'
writing manifest file 'torch.egg-info/SOURCES.txt'
Copying torch.egg-info to /home/xxx/anaconda3/lib/python3.7/site-packages/torch-1.1.0a0+7e73783-py3.7.egg-info
running install_scripts
Installing convert-caffe2-to-onnx script to /home/xxx/anaconda3/bin
Installing convert-onnx-to-caffe2 script to /home/xxx/anaconda3/bin
可以直接下载magma-cuda80,网址,安装:
conda install magma-cuda80-2.3.0-1.tar.bz2
验证
注意:torch.cuda.is_available()
用于验证GPU是否启用,不要位于torch的同名文件夹,参考。
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 26 2018, 19:50:54)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import print_function
>>> import torch
>>> x = torch.rand(5, 3)
>>> print(x)
tensor([[0.8321, 0.2015, 0.5998],
[0.0934, 0.2769, 0.3742],
[0.4468, 0.8644, 0.1492],
[0.1357, 0.4677, 0.1423],
[0.6983, 0.7396, 0.7163]])
>>> torch.cuda.is_available()
False
>>>
其他
可以将conda的激活命令,由.bashrc
移入一个脚本,避免每次启动,都会默认激活,从而影响其他工程。
# conda_activate.sh
# added by Anaconda3 5.3.1 installer
# >>> conda init >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$(CONDA_REPORT_ERRORS=false '/home/wcl1/anaconda3/bin/conda' shell.bash hook 2> /dev/null)"
if [ $? -eq 0 ]; then
\eval "$__conda_setup"
else
if [ -f "/home/wcl1/anaconda3/etc/profile.d/conda.sh" ]; then
. "/home/wcl1/anaconda3/etc/profile.d/conda.sh"
CONDA_CHANGEPS1=false conda activate base
else
\export PATH="$PATH:/home/wcl1/anaconda3/bin"
fi
fi
unset __conda_setup
# <<< conda init <<<
Python升级
- 更新apt-get的源,参考
- 更新环境,
sudo apt-get install -f
- 解决
/boot
空间不足,参考,find /boot -type f -regex "^.*-generic"
和sudo find /boot -type f -regex "^.*XX-generic" -delete
- 安装python3.7,参考,
sudo apt-get install python3.7
- 使用,输入python3.7,即可。
注意:conda环境仅支持3.6+,如果需要,必须升级Python版本。
OK, that's all!