1*da0073e9SAndroid Build Coastguard Workerlibtorch (C++-only) 2*da0073e9SAndroid Build Coastguard Worker=================== 3*da0073e9SAndroid Build Coastguard Worker 4*da0073e9SAndroid Build Coastguard WorkerThe core of pytorch does not depend on Python. A 5*da0073e9SAndroid Build Coastguard WorkerCMake-based build system compiles the C++ source code into a shared 6*da0073e9SAndroid Build Coastguard Workerobject, libtorch.so. 7*da0073e9SAndroid Build Coastguard Worker 8*da0073e9SAndroid Build Coastguard WorkerBuilding libtorch using Python 9*da0073e9SAndroid Build Coastguard Worker------------------------------ 10*da0073e9SAndroid Build Coastguard Worker 11*da0073e9SAndroid Build Coastguard WorkerYou can use a python script/module located in tools package to build libtorch 12*da0073e9SAndroid Build Coastguard Worker:: 13*da0073e9SAndroid Build Coastguard Worker cd <pytorch_root> 14*da0073e9SAndroid Build Coastguard Worker 15*da0073e9SAndroid Build Coastguard Worker # Make a new folder to build in to avoid polluting the source directories 16*da0073e9SAndroid Build Coastguard Worker mkdir build_libtorch && cd build_libtorch 17*da0073e9SAndroid Build Coastguard Worker 18*da0073e9SAndroid Build Coastguard Worker # You might need to export some required environment variables here. 19*da0073e9SAndroid Build Coastguard Worker Normally setup.py sets good default env variables, but you'll have to do 20*da0073e9SAndroid Build Coastguard Worker that manually. 21*da0073e9SAndroid Build Coastguard Worker python ../tools/build_libtorch.py 22*da0073e9SAndroid Build Coastguard Worker 23*da0073e9SAndroid Build Coastguard Worker 24*da0073e9SAndroid Build Coastguard WorkerAlternatively, you can call setup.py normally and then copy the built cpp libraries. This method may have side effects to your active Python installation. 25*da0073e9SAndroid Build Coastguard Worker:: 26*da0073e9SAndroid Build Coastguard Worker cd <pytorch_root> 27*da0073e9SAndroid Build Coastguard Worker python setup.py build 28*da0073e9SAndroid Build Coastguard Worker 29*da0073e9SAndroid Build Coastguard Worker ls torch/lib/tmp_install # output is produced here 30*da0073e9SAndroid Build Coastguard Worker ls torch/lib/tmp_install/lib/libtorch.so # of particular interest 31*da0073e9SAndroid Build Coastguard Worker 32*da0073e9SAndroid Build Coastguard WorkerTo produce libtorch.a rather than libtorch.so, set the environment variable `BUILD_SHARED_LIBS=OFF`. 33*da0073e9SAndroid Build Coastguard Worker 34*da0073e9SAndroid Build Coastguard WorkerTo use ninja rather than make, set `CMAKE_GENERATOR="-GNinja" CMAKE_INSTALL="ninja install"`. 35*da0073e9SAndroid Build Coastguard Worker 36*da0073e9SAndroid Build Coastguard WorkerNote that we are working on eliminating tools/build_pytorch_libs.sh in favor of a unified cmake build. 37*da0073e9SAndroid Build Coastguard Worker 38*da0073e9SAndroid Build Coastguard WorkerBuilding libtorch using CMake 39*da0073e9SAndroid Build Coastguard Worker-------------------------------------- 40*da0073e9SAndroid Build Coastguard Worker 41*da0073e9SAndroid Build Coastguard WorkerYou can build C++ libtorch.so directly with cmake. For example, to build a Release version from the main branch and install it in the directory specified by CMAKE_INSTALL_PREFIX below, you can use 42*da0073e9SAndroid Build Coastguard Worker:: 43*da0073e9SAndroid Build Coastguard Worker git clone -b main --recurse-submodule https://github.com/pytorch/pytorch.git 44*da0073e9SAndroid Build Coastguard Worker mkdir pytorch-build 45*da0073e9SAndroid Build Coastguard Worker cd pytorch-build 46*da0073e9SAndroid Build Coastguard Worker cmake -DBUILD_SHARED_LIBS:BOOL=ON -DCMAKE_BUILD_TYPE:STRING=Release -DPYTHON_EXECUTABLE:PATH=`which python3` -DCMAKE_INSTALL_PREFIX:PATH=../pytorch-install ../pytorch 47*da0073e9SAndroid Build Coastguard Worker cmake --build . --target install 48*da0073e9SAndroid Build Coastguard Worker 49*da0073e9SAndroid Build Coastguard WorkerTo use release branch v1.6.0, for example, replace ``master`` with ``v1.6.0``. You will get errors if you do not have needed dependencies such as Python3's PyYAML package. 50