Skip to content

Commit ec8b1c9

Browse files
bombs-kimfacebook-github-bot
authored andcommitted
Fix possible importing errors in build_libtorch.py (pytorch#15471)
Summary: 1. I fixed the importing process, which had some problems - **I think `setup_helpers` should not be imported as the top level module. It can lead to many future errors. For example, what if `setup_helpers` imports another module from the upper level?** So we need to change it. - The code is not consistent with other modules in `tools` package. For example, other modules in the package imports `from tools.setuptools...` not `from setuptools...`. - **It should be able to run with `python -m tools.build_libtorch` command** because this module is a part of the tools package. Currently, you cannot do that and I think it's simply wrong. ~~2. I Added platform specific warning messages. - I constantly forgot that I needed to define some environment variables in advance specific to my platform to build libtorch, especially when I'm working at a non pytorch root directory. So I thought adding warnings for common options would be helpful .~~ ~~3. Made the build output path configurable. And a few other changes.~~ orionr ebetica Pull Request resolved: pytorch#15471 Differential Revision: D13709607 Pulled By: ezyang fbshipit-source-id: 950d5727aa09f857d973538c50b1ab169d88da38
1 parent fcb4b4f commit ec8b1c9

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

docs/libtorch.rst

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
libtorch (C++-only)
22
===================
33

4-
The core of pytorch can be built and used without Python. A
4+
The core of pytorch does not depend on Python. A
55
CMake-based build system compiles the C++ source code into a shared
66
object, libtorch.so.
77

88
Building libtorch
99
-----------------
1010

11-
There is a script which wraps the CMake build. Invoke it with
11+
You can use a python script/module located in tools package to build libtorch
12+
::
13+
cd <pytorch_root>
14+
# export some required environment variables
15+
python -m tools.build_libtorch
16+
1217

18+
Alternatively, you can invoke a shell script in the same directory to achieve the same goal
1319
::
14-
cd pytorch
20+
cd <pytorch_root>
1521
BUILD_TORCH=ON ONNX_NAMESPACE=onnx_torch bash tools/build_pytorch_libs.sh --use-nnpack caffe2
1622
ls torch/lib/tmp_install # output is produced here
1723
ls torch/lib/tmp_install/lib/libtorch.so # of particular interest
@@ -20,4 +26,4 @@ To produce libtorch.a rather than libtorch.so, set the environment variable `BUI
2026

2127
To use ninja rather than make, set `CMAKE_GENERATOR="-GNinja" CMAKE_INSTALL="ninja install"`.
2228

23-
Future work will simplify this further.
29+
Note that we are working on eliminating tools/build_pytorch_libs.sh in favor of a unified cmake build.

tools/build_libtorch.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
import argparse
22
import os
3+
from os.path import dirname, abspath
34
import shlex
45
import subprocess
56
import sys
67

8+
# By appending pytorch_root to sys.path, this module can import other torch
9+
# modules even when run as a standalone script. i.e., it's okay either you
10+
# do `python build_libtorch.py` or `python -m tools.build_libtorch`.
11+
pytorch_root = dirname(dirname(abspath(__file__)))
12+
sys.path.append(pytorch_root)
13+
714
# If you want to modify flags or environmental variables that is set when
815
# building torch, you should do it in tools/setup_helpers/configure.py.
916
# Please don't add it here unless it's only used in LibTorch.
10-
from setup_helpers.configure import get_libtorch_env_with_flags
17+
from tools.setup_helpers.configure import get_libtorch_env_with_flags, IS_WINDOWS
1118

1219
if __name__ == '__main__':
1320
# Placeholder for future interface. For now just gives a nice -h.
1421
parser = argparse.ArgumentParser(description='Build libtorch')
1522
options = parser.parse_args()
1623

1724
tools_path = os.path.dirname(os.path.abspath(__file__))
18-
if sys.platform == 'win32':
25+
if IS_WINDOWS:
1926
build_pytorch_libs = os.path.join(tools_path, 'build_pytorch_libs.bat')
2027
else:
2128
build_pytorch_libs = os.path.join(tools_path, 'build_pytorch_libs.sh')

0 commit comments

Comments
 (0)