Skip to content

Commit 1e64fa2

Browse files
authored
A short term fix for editable mode install failed to import root level module such as exir (#9818)
Summary: Fixes #9558. The `src/executorch/exir` file exists primarily due to the limitations of `pip install` in editable mode. Specifically, `pip install -e .` does not recognize `<executorch root>/exir` (or any root level directory with a `__init__.py`) as a valid package module because of the presence of `<executorch root>/exir/__init__.py`. See the following GitHub issue for details: [Issue #9558](#9558). To work around this limitation, a symlink is used. With this symlink and this package entry in `pyproject.toml`: ```toml [tool.setuptools.package-dir] # ... "executorch" = "src/executorch" ``` We are telling `pip install -e .` to treat `src/executorch` as the root of the `executorch` package and hence mapping `executorch.exir` to `src/executorch/exir`. This effectively gets `exir` out from the root level package. This allows us to perform `pip install -e .` successfully and enables the execution of the following command: ```bash python -c "from executorch.exir import CaptureConfig" ``` Test Plan: ```bash ./install_executorch.sh --pybind --editable python -c "from executorch.exir import CaptureConfig" ``` Reviewers: Subscribers: Tasks: Tags:
1 parent 40443a9 commit 1e64fa2

File tree

16 files changed

+49
-19
lines changed

16 files changed

+49
-19
lines changed

pyproject.toml

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,23 +88,15 @@ flatc = "executorch.data.bin:flatc"
8888
# package, and package_data to restrict the set up non-python files we
8989
# include. See also setuptools/discovery.py for custom finders.
9090
[tool.setuptools.package-dir]
91-
"executorch.backends" = "backends"
92-
"executorch.codegen" = "codegen"
93-
"executorch.data.bin" = "data/bin"
94-
# TODO(mnachin T180504136): Do not put examples/models
95-
# into core pip packages. Refactor out the necessary utils
96-
# or core models files into a separate package.
97-
"executorch.examples.apple.coreml.llama" = "examples/apple/coreml/llama"
98-
"executorch.examples.llm_pte_finetuning" = "examples/llm_pte_finetuning"
99-
"executorch.examples.models" = "examples/models"
100-
"executorch.exir" = "exir"
101-
"executorch.extension" = "extension"
102-
"executorch.kernels.quantized" = "kernels/quantized"
103-
"executorch.schema" = "schema"
104-
"executorch.devtools" = "devtools"
105-
"executorch.devtools.bundled_program" = "devtools/bundled_program"
106-
"executorch.runtime" = "runtime"
107-
"executorch.util" = "util"
91+
# Tell setuptools to follow the symlink: src/executorch/* -> * for all first level
92+
# modules such as src/executorch/exir -> exir. This helps us to semi-compliant with
93+
# the "src layout" convention for python packages, which is also discussed in
94+
# https://packaging.python.org/en/latest/discussions/src-layout-vs-flat-layout/.
95+
# In the long term we should move all the modules under the src/executorch/ folder.
96+
#
97+
# Doing this also allows us to import from executorch.exir directly in
98+
# editable mode.
99+
"executorch" = "src/executorch"
108100

109101
[tool.setuptools.package-data]
110102
# TODO(dbort): Prune /test[s]/ dirs, /third-party/ dirs, yaml files that we

setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,8 +575,7 @@ def run(self):
575575
# In editable mode, the package directory is the original source directory
576576
dst_root = self.get_package_dir(".")
577577
else:
578-
dst_root = os.path.join(self.build_lib, self.get_package_dir("executorch"))
579-
578+
dst_root = os.path.join(self.build_lib, "executorch")
580579
# Create the version file.
581580
Version.write_to_python_file(os.path.join(dst_root, "version.py"))
582581

src/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Why Do We Have These Symlinks
2+
3+
The `src/executorch/*` files exist primarily due to the limitations of `pip install` in editable mode. Specifically, `pip install -e .` does not recognize `<executorch root>/exir` (or any root level directory with a `__init__.py`) as a valid package module because of the presence of `<executorch root>/exir/__init__.py`. See the following GitHub issue for details: [Issue #9558](https://github.com/pytorch/executorch/issues/9558).
4+
5+
## The Symlink Solution
6+
7+
To work around this limitation, a symlink is used. With this symlink and this package entry in `pyproject.toml`:
8+
9+
```toml
10+
[tool.setuptools.package-dir]
11+
# ...
12+
"executorch" = "src/executorch"
13+
```
14+
We are telling `pip install -e .` to treat `src/executorch` as the root of the `executorch` package and hence mapping `executorch.*.*` to `src/executorch/*/*`. This effectively gets modules like `exir` out from the root level package.
15+
16+
This allows us to perform `pip install -e .` successfully and enables the execution of the following command:
17+
18+
```bash
19+
python -c "from executorch.exir import CaptureConfig"
20+
```
21+
22+
## Long Term Solution
23+
24+
We should start to move directories from <executorch root>/ to <executorch root>/src/ and remove the symlinks. Issue [#8699](https://github.com/pytorch/executorch/issues/8699) to track this effort. This will require a lot of work internally.
25+
26+
TODO(mnachin T180504136): Do not put examples/models into core pip packages. Refactor out the necessary utils or core models files into a separate package.

src/executorch/backends

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../backends

src/executorch/codegen

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../codegen

src/executorch/data

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../data

src/executorch/devtools

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../devtools

src/executorch/examples/apple

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../examples/apple
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../examples/llm_pte_finetuning

src/executorch/examples/models

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../examples/models

src/executorch/exir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../exir

src/executorch/extension

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../extension

src/executorch/kernels/quantized

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../kernels/quantized

src/executorch/runtime

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../runtime

src/executorch/schema

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../schema

src/executorch/util

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../util

0 commit comments

Comments
 (0)