Skip to content

Commit 250f681

Browse files
dbortfacebook-github-bot
authored andcommitted
New pip build system (pytorch#2499)
Summary: Compatible with `python -m build -wheel` instead of using the deprecated `python setup.py bdist_wheel`. A new custom build command builds all CMake targets, and a new custom build_ext command installs built files into the wheel. A new custom build_py command copies platform-independent files into the wheel instead of copying them into the source tree. Puts all generated files under `pip-out` instead of mixing with files in the tree. Uses Command methods like `mkpath` and `copy_file` to benefit from setuptools concepts like dry_run, and to get logging for free. Adds `flatc` to the wheel and puts it on the user's PATH. Removes the call to `install_flatc.sh` in `install_requirements.sh`; but, leaves the script in place since someone might want to run it on their own. Also: - Enables runtime logging for pybindings even in release mode - Fixes another `-fPIC` requirement when building the `flatcc` host tools - Picks a number of build jobs based on the cores in the system, instead of defaulting to 9 - Removes now-unnecessary steps from the Getting Started docs - Removes a use of `${arg^^}`, which doesn't work with older versions of bash Pull Request resolved: pytorch#2499 Test Plan: Iteration command to test wheel building without completely rebuilding the system: ``` rm -rf \ /tmp/wheel-out \ pip-out/lib* \ pip-out/bdist.* \ pip-out/temp.*/cmake-out/CMake* \ executorch.egg-info \ third-party/flatcc/{bin,lib} \ ; \ python setup.py bdist_wheel --dist-dir /tmp/wheel-out ``` To fully rebuild the system, also `rm -rf pip-out`. The wheel file is written to (e.g.) ``` /tmp/wheel-out/executorch-0.1.0-cp310-cp310-linux_x86_64.whl ``` Examine it with `unzip -l`, or install it with `pip install /tmp/wheel-out/executorch*.whl`. Also tested creating the wheel via `install_requirements.sh` and creating a simple model file. ``` ./install_requirements.sh python3 -m examples.portable.scripts.export --model_name="add" <creates add.pte file> ``` Also tested that it can build pybindings with xnnpack when invoked via `install_requirements.sh`: ``` ./install_requirements.sh --pybind xnnpack python -m unittest backends/xnnpack/test/models/mobilenet_v2.py ``` Builds successfully, but trying to run the test currently fails during xnnpack init() due to T184024365. But it fails in the same way that the main branch does, so this new diff does not regress. Reviewed By: GregoryComer Differential Revision: D55436863 Pulled By: dbort fbshipit-source-id: 06fd9b151a1aba1010b0da9571cd27116782125e
1 parent db7c057 commit 250f681

File tree

7 files changed

+446
-201
lines changed

7 files changed

+446
-201
lines changed

.gitignore

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@ cmake-android-out/
55
cmake-ios-out/
66
ethos-u-scratch/
77
executorch.egg-info
8+
pip-out/
89
__pycache__/
9-
build/lib/
10-
exir/_serialize/scalar_type.fbs
11-
exir/_serialize/program.fbs
12-
sdk/bundled_program/serialize/bundled_program_schema.fbs
13-
sdk/bundled_program/serialize/scalar_type.fbs
1410

1511
# Any exported models and profiling outputs
1612
*.pte

build/pip_data_bin_init.py.in

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# This file should be written to the wheel package as
2+
# `executorch/data/bin/__init__.py`.
3+
#
4+
# Setuptools will expect to be able to say something like `from
5+
# executorch.data.bin import mybin; mybin()` for each entry listed in the
6+
# [project.scripts] section of pyproject.toml. This file makes the `mybin()`
7+
# function execute the binary at `executorch/data/bin/mybin` and exit with that
8+
# binary's exit status.
9+
10+
import subprocess
11+
import os
12+
import sys
13+
import types
14+
15+
# This file should live in the target `bin` directory.
16+
_bin_dir = os.path.join(os.path.dirname(__file__))
17+
18+
def _find_executable_files_under(dir):
19+
"""Lists all executable files in the given directory."""
20+
bin_names = []
21+
for filename in os.listdir(dir):
22+
filepath = os.path.join(dir, filename)
23+
if os.path.isfile(filepath) and os.access(filepath, os.X_OK):
24+
bin_names.append(filename)
25+
return bin_names
26+
27+
# The list of binaries to create wrapper functions for.
28+
_bin_names = _find_executable_files_under(_bin_dir)
29+
30+
# We'll define functions named after each binary. Make them importable.
31+
__all__ = _bin_names
32+
33+
def _run(name):
34+
"""Runs the named binary, which should live under _bin_dir.
35+
36+
Exits the current process with the return code of the subprocess.
37+
"""
38+
raise SystemExit(subprocess.call([os.path.join(_bin_dir, name)] + sys.argv[1:], close_fds=False))
39+
40+
# Define a function named after each of the binaries.
41+
for bin_name in _bin_names:
42+
exec(f"def {bin_name}(): _run('{bin_name}')")

docs/source/getting-started-setup.md

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -111,37 +111,12 @@ Follow these steps:
111111
source .executorch/bin/activate
112112
```
113113

114-
1. Install [Cmake](https://cmake.org/download)
115-
116-
```bash
117-
conda install cmake
118-
```
119-
120-
Alternatively:
121-
122-
```bash
123-
pip install cmake
124-
```
125-
126114
1. Install ExecuTorch and dependencies:
127115

128116
```bash
129117
./install_requirements.sh
130118
```
131119

132-
**Optional:** Install ExecuTorch as an editable installation:
133-
```bash
134-
pip install --editable . --config-settings editable_mode=strict --no-build-isolation
135-
```
136-
137-
1. Expose FlatBuffers compiler:
138-
139-
ExecuTorch uses `flatc` to export models and builds it from sources at
140-
`third-party/flatbuffers`. Make it available by adding it to the `$PATH` environment variable,
141-
as prompted by the previous step, or exporting as `$FLATC_EXECUTABLE`
142-
enironment variable.
143-
Run `./build/install_flatc.sh` to make sure `flatc` is installed correctly.
144-
145120
You have successfully set up your environment to work with ExecuTorch. The next
146121
step is to generate a sample ExecuTorch program.
147122

install_requirements.sh

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ for arg in "$@"; do
2828
;;
2929
coreml|mps|xnnpack)
3030
if [[ "$EXECUTORCH_BUILD_PYBIND" == "ON" ]]; then
31-
CMAKE_ARGS="$CMAKE_ARGS -DEXECUTORCH_BUILD_${arg^^}=ON"
31+
arg_upper="$(echo "${arg}" | tr '[:lower:]' '[:upper:]')"
32+
CMAKE_ARGS="$CMAKE_ARGS -DEXECUTORCH_BUILD_${arg_upper}=ON"
3233
else
3334
echo "Error: $arg must follow --pybind"
3435
exit 1
@@ -65,6 +66,7 @@ EXIR_REQUIREMENTS=(
6566

6667
# pip packages needed for development.
6768
DEVEL_REQUIREMENTS=(
69+
cmake # For building binary targets.
6870
setuptools # For building the pip package.
6971
tomli # Imported by extract_sources.py when using python < 3.11.
7072
wheel # For building the pip package archive.
@@ -94,13 +96,9 @@ pip install --extra-index-url "${TORCH_NIGHTLY_URL}" \
9496
"${REQUIREMENTS_TO_INSTALL[@]}"
9597

9698
#
97-
# Install executorch pip package.
99+
# Install executorch pip package. This also makes `flatc` available on the path.
98100
#
99101

100-
EXECUTORCH_BUILD_PYBIND="$EXECUTORCH_BUILD_PYBIND" \
101-
CMAKE_ARGS="$CMAKE_ARGS" \
102-
CMAKE_BUILD_PARALLEL_LEVEL=9 \
103-
pip install . --no-build-isolation -v
104-
105-
# Install flatc dependency
106-
bash build/install_flatc.sh
102+
EXECUTORCH_BUILD_PYBIND="${EXECUTORCH_BUILD_PYBIND}" \
103+
CMAKE_ARGS="${CMAKE_ARGS}" \
104+
pip install . --no-build-isolation -v

pyproject.toml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,27 @@ dependencies=[
2020
"ruamel.yaml",
2121
"sympy",
2222
"tabulate",
23-
"tomli",
24-
"zstd",
2523
]
2624

25+
# Tell setuptools to generate commandline wrappers for tools that we install
26+
# under data/bin in the pip package. This will put these commands on the user's
27+
# path.
28+
[project.scripts]
29+
flatc = "executorch.data.bin:flatc"
30+
2731
[tool.setuptools.package-data]
28-
"*" = ["*.fbs", "*.yaml"]
32+
# TODO(dbort): Prune /test[s]/ dirs, /third-party/ dirs, yaml files that we
33+
# don't need.
34+
"*" = [
35+
# Some backends like XNNPACK need their .fbs files.
36+
"*.fbs",
37+
# Some kernel libraries need their .yaml files.
38+
"*.yaml",
39+
]
2940

3041
[tool.setuptools.exclude-package-data]
3142
"*" = ["*.pyc"]
3243

3344
[tool.usort]
45+
# Do not try to put "first-party" imports in their own section.
3446
first_party_detection = false

sdk/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ ExternalProject_Add(
7777
SOURCE_DIR ${CMAKE_SOURCE_DIR}/third-party/flatcc
7878
BINARY_DIR ${CMAKE_BINARY_DIR}/_host_build
7979
CMAKE_CACHE_ARGS -DFLATCC_TEST:BOOL=OFF -DFLATCC_REFLECTION:BOOL=OFF
80+
# See above comment about POSITION_INDEPENDENT_CODE.
81+
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON
8082
INSTALL_COMMAND "" # Prevent the install step, modify as needed
8183
)
8284

0 commit comments

Comments
 (0)