PyTorch源码编译
安装conda
python最低要求 3.8版本及以上。
1 | mkdir -p ~/miniconda3 |
下载源码
从pytorch社区下载源码,并下载git的submodule。
1 | git clone --recursive https://github.com/pytorch/pytorch |
拉代码特别是拉三方的库的时候,会有访问不到的情况,可以重试,或者配置http代理以及ssh代理:
http代理:
1 | export http_proxy=host:ip |
ssh代理:
1 | cat ~/.ssh/config |
安装依赖
pip最好配置下清华源,不然会很慢。
1 | python -m pip install --upgrade pip |
安装pytorch依赖,gcc需要支持C++ 17,需要使用gcc 9.4.0及以上。
1 | conda install cmake ninja |
编译
1 | export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"} |
一些有用的编译时的环境变量:
DEBUG=1
will enable debug builds (-g -O0)REL_WITH_DEB_INFO=1
will enable debug symbols with optimizations (-g -O3)USE_DISTRIBUTED=0
will disable distributed (c10d, gloo, mpi, etc.) build.USE_MKLDNN=0
will disable using MKL-DNN.USE_CUDA=0
will disable compiling CUDA (in case you are developing on something not CUDA related), to save compile time.BUILD_TEST=0
will disable building C++ test binaries.USE_FBGEMM=0
will disable using FBGEMM (quantized 8-bit server operators).USE_NNPACK=0
will disable compiling with NNPACK.USE_QNNPACK=0
will disable QNNPACK build (quantized 8-bit operators).USE_XNNPACK=0
will disable compiling with XNNPACK.USE_FLASH_ATTENTION=0
andUSE_MEM_EFF_ATTENTION=0
will disable compiling flash attention and memory efficient kernels respectively
验证
1 | import torch |
参考: