libtorch (C++-only)
===================
-The core of pytorch can be built and used without Python. A
+The core of pytorch does not depend on Python. A
CMake-based build system compiles the C++ source code into a shared
object, libtorch.so.
Building libtorch
-----------------
-There is a script which wraps the CMake build. Invoke it with
+You can use a python script/module located in tools package to build libtorch
+::
+ cd <pytorch_root>
+ # export some required environment variables
+ python -m tools.build_libtorch
+
+Alternatively, you can invoke a shell script in the same directory to achieve the same goal
::
- cd pytorch
+ cd <pytorch_root>
BUILD_TORCH=ON ONNX_NAMESPACE=onnx_torch bash tools/build_pytorch_libs.sh --use-nnpack caffe2
ls torch/lib/tmp_install # output is produced here
ls torch/lib/tmp_install/lib/libtorch.so # of particular interest
To use ninja rather than make, set `CMAKE_GENERATOR="-GNinja" CMAKE_INSTALL="ninja install"`.
-Future work will simplify this further.
+Note that we are working on eliminating tools/build_pytorch_libs.sh in favor of a unified cmake build.
import argparse
import os
+from os.path import dirname, abspath
import shlex
import subprocess
import sys
+# By appending pytorch_root to sys.path, this module can import other torch
+# modules even when run as a standalone script. i.e., it's okay either you
+# do `python build_libtorch.py` or `python -m tools.build_libtorch`.
+pytorch_root = dirname(dirname(abspath(__file__)))
+sys.path.append(pytorch_root)
+
# If you want to modify flags or environmental variables that is set when
# building torch, you should do it in tools/setup_helpers/configure.py.
# Please don't add it here unless it's only used in LibTorch.
-from setup_helpers.configure import get_libtorch_env_with_flags
+from tools.setup_helpers.configure import get_libtorch_env_with_flags, IS_WINDOWS
if __name__ == '__main__':
# Placeholder for future interface. For now just gives a nice -h.
options = parser.parse_args()
tools_path = os.path.dirname(os.path.abspath(__file__))
- if sys.platform == 'win32':
+ if IS_WINDOWS:
build_pytorch_libs = os.path.join(tools_path, 'build_pytorch_libs.bat')
else:
build_pytorch_libs = os.path.join(tools_path, 'build_pytorch_libs.sh')