Pytorch source code compilation Libtorch

Create a virtual environment:

conda create -n build-libtorch python=3.8
cd build-libtorch

Install related dependencies:

conda install astunparse numpy ninja pyyaml mkl mkl-include setuptools cmake cffi typing_extensions future six requests dataclasses

Download Pytorch, you can download it through or HTTPS:SSH

git clone https://github.com/pytorch/pytorch.git  # HTTPS
git clone [email protected]:pytorch/pytorch.git  # SSH

The cloned one is the latest version, you can switch to different versions as needed:

cd pytorch
git tag  # 查看标签
git checkout v1.4.0  # 根据标签切换版本
git submodule sync  # 根据父仓库中的配置,将子模块的URL进行更新,以保证与父仓库中记录的URL一致。这样,当执行git submodule update命令时,Git就能正确地从指定的URL下载子模块的更新或特定版本的代码
git submodule update --init --recursive  # 初始化和更新父仓库中的子模块,并递归地初始化和更新所有嵌套的子模块

If the submodule cannot be updated through HTTPS, you can use sudo gedit .gitmodulesthe command to open .gitmodulesthe file, which records the storage path and download address of the submodule. Modify the submodule after opening the file url, for example, url = https://github.com/pybind/pybind11.gitmodify to url = [email protected]:pybind/pybind11.git. After all submodules urlare modified, execute git submodule syncand in sequence git submodule update --init --recursive.

You can also download it in one step:

git clone -b v1.4.0 https://github.com/pytorch/pytorch.git
git clone -b v1.4.0 [email protected]:pytorch/pytorch.git

Compilation options can be set as needed:

export USE_CUDA=False
export BUILD_TEST=False
export USE_NINJA=OFF

Execute compilation:

方式一:
# 在pytorch目录下
mkdir build_libtorch && cd build_libtorch
python ../tools/build_libtorch.py

方式二:
# 在pytorch的父目录下
mkdir pytorch-build && cd pytorch-build
cmake -DBUILD_SHARED_LIBS:BOOL=ON -DCMAKE_BUILD_TYPE:STRING=Release -DPYTHON_EXECUTABLE:PATH=`which python3` -DCMAKE_INSTALL_PREFIX:PATH=../pytorch-install ../pytorch
cmake --build . --target install

Create a new libtorch directory, copy the pytorch/torch/include directory to the libtorch directory, and copy the build/lib directory to the libtorch directory.

Possible problems:

  1. When executing the cmake command, the operation Performing Test COMPILER_SUPPORTS_LONG_DOUBLEstops at this step and there is no response for a long time. This is probably a problem caused by the GCC version. You can try to change the GCC version to solve the problem. The replaced version can be higher than the previous one, or you can Lower, you can try more
  2. If a problem occurs , you can try to solve it by Could not run a simple program built with your compiler. If you are trying to use -fsanitize=address, make sure libasan is properly installed on your system (you can confirm if the problem is this by attempting to build and run a small program.)setting it in CMakeLists.txt in the pytorch directory.set(INTERN_BUILD_MOBILE ON)

Guess you like

Origin blog.csdn.net/weixin_48158964/article/details/133128106