2019-01-08 23:54:20 +00:00
libtorch (C++-only)
===================
2019-01-17 07:52:37 +00:00
The core of pytorch does not depend on Python. A
2019-01-08 23:54:20 +00:00
CMake-based build system compiles the C++ source code into a shared
object, libtorch.so.
2024-09-25 02:30:54 +00:00
AMD ROCm Support
------------------------------
If you're compiling for AMD ROCm then first run this command:
::
cd <pytorch_root>
# Only run this if you're compiling for ROCm
python tools/amd_build/build_amd.py
Additional information about ROCm support can be found in the top-level
`README <https://github.com/pytorch/pytorch/blob/main/README.md> `_ .
2020-10-21 21:27:27 +00:00
Building libtorch using Python
------------------------------
2019-01-08 23:54:20 +00:00
2019-01-17 07:52:37 +00:00
You can use a python script/module located in tools package to build libtorch
::
cd <pytorch_root>
2019-05-30 19:38:36 +00:00
# Make a new folder to build in to avoid polluting the source directories
mkdir build_libtorch && cd build_libtorch
2019-01-08 23:54:20 +00:00
2019-05-30 19:38:36 +00:00
# You might need to export some required environment variables here.
Normally setup.py sets good default env variables, but you'll have to do
that manually.
python ../tools/build_libtorch.py
Alternatively, you can call setup.py normally and then copy the built cpp libraries. This method may have side effects to your active Python installation.
2019-01-08 23:54:20 +00:00
::
2019-01-17 07:52:37 +00:00
cd <pytorch_root>
2019-05-30 19:38:36 +00:00
python setup.py build
2019-01-08 23:54:20 +00:00
ls torch/lib/tmp_install # output is produced here
ls torch/lib/tmp_install/lib/libtorch.so # of particular interest
To produce libtorch.a rather than libtorch.so, set the environment variable `BUILD_SHARED_LIBS=OFF` .
To use ninja rather than make, set `CMAKE_GENERATOR="-GNinja" CMAKE_INSTALL="ninja install"` .
2019-01-17 07:52:37 +00:00
Note that we are working on eliminating tools/build_pytorch_libs.sh in favor of a unified cmake build.
2020-10-21 21:27:27 +00:00
Building libtorch using CMake
--------------------------------------
2023-05-02 18:20:27 +00:00
You 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
2020-10-21 21:27:27 +00:00
::
2023-05-02 18:20:27 +00:00
git clone -b main --recurse-submodule https://github.com/pytorch/pytorch.git
2020-10-21 21:27:27 +00:00
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
To 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.