Skip to content

Commit ff09af0

Browse files
authored
Stop installing test-only packages with the executorch package (pytorch#24143)
### Summary `pip install executorch` installs four packages that are only there for ExecuTorch's own tests: `expecttest`, `hypothesis`, `kgb` and `parameterized`. `hypothesis` also brings `sortedcontainers`. So every user installs five packages they never need. This moves the four from the package's runtime dependencies (`_base_dependencies()` in `setup.py`) to `requirements-dev.txt`. The developer setup and every CI job that runs tests already install that file, so tests still get them. Nothing a user imports needs them. Three shipped files do, but only tests or a code generator use them: `backends/cadence/aot/typing_stubs.py`, `examples/models/llama/source_transformation/test_attention_sink.py`, and `exir/dialects/edge/spec/gen.py`. Effect, resolving the declared dependencies for Python 3.12 (without `torch`): 55 packages before, 50 after on macOS arm64 and Linux x86_64. The same five go away on Linux aarch64 and Windows. Kept on purpose: `coremltools` and `scikit-learn` (the Core ML backend needs both), and `hydra-core`, `omegaconf`, `pandas`, `tabulate` and `pytorch-tokenizers`, which shipped modules import. The first commit fixes the wheel's clean-install check. It hid undeclared packages so that `importlib.util.find_spec` raised instead of returning `None`. `torch._dynamo` probes for `hypothesis` with `find_spec` when it loads, so the check failed once `hypothesis` was no longer declared, although a real install works. ### Test plan On macOS arm64, in a fresh environment with only the trimmed dependencies, `torch` 2.14.0 and a nightly `executorch` installed with `--no-deps`: - Tried to import all ~3,000 modules in the package. Only the three files above failed for lack of a removed package. The rest imported, or stopped on unrelated optional packages such as vendor SDKs. - Exported a small model with XNNPACK and with Core ML, ran both, and matched eager PyTorch. - The clean-install check fails before the first commit and passes after it, and still fails when a checked module imports an undeclared package. Tests that use the moved packages pass with `requirements-dev.txt` installed.
1 parent 9944b02 commit ff09af0

3 files changed

Lines changed: 25 additions & 20 deletions

File tree

‎.ci/scripts/wheel/test_clean_install.py‎

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,10 @@ def run_tests(work_dir: Path) -> None:
215215

216216

217217
# Reads its arguments from stdin, blocks the named modules, then imports. A blocked module
218-
# raises ModuleNotFoundError exactly as it would be absent, so the traceback shows the import
219-
# chain that wanted it.
218+
# behaves like a missing one to import and find_spec, so the traceback shows the import chain
219+
# that wanted it.
220220
_IMPORT_PROBE = """
221-
import importlib.abc, importlib.util, json, os, sys
221+
import importlib, json, os, sys
222222
223223
# Drop the working directory, which `python -c` puts first. The equivalent flag, -P, is only
224224
# available from 3.11.
@@ -229,19 +229,23 @@ def run_tests(work_dir: Path) -> None:
229229
request = json.load(sys.stdin)
230230
blocked = set(request["blocked"])
231231
232-
233-
class _Blocker(importlib.abc.MetaPathFinder):
234-
def find_spec(self, fullname, path=None, target=None):
235-
if fullname.split(".")[0] in blocked:
236-
raise ModuleNotFoundError(
237-
f"No module named {fullname!r} (not declared by the executorch wheel)",
238-
name=fullname,
239-
)
240-
return None
241-
242-
243-
sys.meta_path.insert(0, _Blocker())
244-
importlib.import_module(request["module"])
232+
# A None entry in sys.modules makes an import raise ModuleNotFoundError while
233+
# importlib.util.find_spec returns None, which is how a missing package behaves. A finder that
234+
# raises from find_spec instead breaks code that probes for optional packages, such as
235+
# torch._dynamo, which looks up hypothesis that way.
236+
for name in blocked:
237+
sys.modules[name] = None
238+
239+
try:
240+
importlib.import_module(request["module"])
241+
except ModuleNotFoundError as error:
242+
package = (error.name or "").split(".")[0]
243+
if package in blocked:
244+
raise ModuleNotFoundError(
245+
f"No module named {package!r} (not declared by the executorch wheel)",
246+
name=package,
247+
) from error
248+
raise
245249
"""
246250

247251

‎requirements-dev.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ pytest-xdist
2121
pytest-rerunfailures==15.1
2222
pytest-json-report
2323
gguf # For extension/llm/export/test/test_gguf.py (GGUF Q4_K/Q6_K dequant tests).
24+
# Needed only by tests and development tools, so the executorch package does not depend on them.
25+
expecttest
26+
hypothesis
27+
kgb
28+
parameterized

‎setup.py‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,15 +1154,11 @@ def _base_dependencies() -> List[str]:
11541154
project's runtime needs.
11551155
"""
11561156
return [
1157-
"expecttest",
11581157
"flatbuffers",
1159-
"hypothesis",
1160-
"kgb",
11611158
"mpmath==1.3.0",
11621159
"numpy>=2.0.0; python_version >= '3.10'",
11631160
"packaging",
11641161
"pandas>=2.2.2; python_version >= '3.10'",
1165-
"parameterized",
11661162
# The Qualcomm backend needs both, on different paths. Any lowering reads the CPU vendor
11671163
# to disable an mkldnn path that crashes on AMD, whether or not an SDK is already set up.
11681164
# Fetching an SDK additionally needs requests, which its downloader imports at module

0 commit comments

Comments
 (0)