|
| 1 | +#!/bin/bash |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | + |
| 8 | +set -euxo pipefail |
| 9 | + |
| 10 | +PYTHON_EXECUTABLE="${PYTHON_EXECUTABLE:-python}" |
| 11 | +REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" |
| 12 | +BUILD_VENV="${REPO_ROOT}/.venv-minimal-build" |
| 13 | +TEST_VENV="${REPO_ROOT}/.venv-minimal-test" |
| 14 | + |
| 15 | +rm -rf "${BUILD_VENV}" "${TEST_VENV}" "${REPO_ROOT}/dist" "${REPO_ROOT}/pip-out" |
| 16 | + |
| 17 | +"${PYTHON_EXECUTABLE}" -m venv "${BUILD_VENV}" |
| 18 | +source "${BUILD_VENV}/bin/activate" |
| 19 | +python -m pip install --upgrade pip |
| 20 | +python -m pip install \ |
| 21 | + "cmake>=3.24,<4.0.0" \ |
| 22 | + "numpy>=2.0.0" \ |
| 23 | + packaging \ |
| 24 | + pyyaml \ |
| 25 | + setuptools \ |
| 26 | + wheel \ |
| 27 | + zstd \ |
| 28 | + certifi \ |
| 29 | + torch \ |
| 30 | + torchvision \ |
| 31 | + --index-url https://download.pytorch.org/whl/cpu \ |
| 32 | + --extra-index-url https://pypi.org/simple |
| 33 | + |
| 34 | +( |
| 35 | + cd "${REPO_ROOT}" |
| 36 | + EXECUTORCH_BUILD_MINIMAL=1 python setup.py bdist_wheel |
| 37 | +) |
| 38 | + |
| 39 | +WHEEL_FILE="$(find "${REPO_ROOT}/dist" -maxdepth 1 -name 'executorch-*.whl' | head -1)" |
| 40 | +test -n "${WHEEL_FILE}" |
| 41 | + |
| 42 | +python - "${WHEEL_FILE}" <<'PY' |
| 43 | +import re |
| 44 | +import sys |
| 45 | +import zipfile |
| 46 | +
|
| 47 | +wheel_file = sys.argv[1] |
| 48 | +with zipfile.ZipFile(wheel_file) as wheel: |
| 49 | + names = wheel.namelist() |
| 50 | + metadata_name = next( |
| 51 | + (name for name in names if name.endswith(".dist-info/METADATA")), None |
| 52 | + ) |
| 53 | + if metadata_name is None: |
| 54 | + raise AssertionError(f"{wheel_file} has no METADATA") |
| 55 | + metadata_text = wheel.read(metadata_name).decode("utf-8") |
| 56 | +
|
| 57 | +for forbidden in ( |
| 58 | + "executorch/backends/", |
| 59 | + "executorch/examples/", |
| 60 | + "executorch/kernels/", |
| 61 | + "executorch/runtime/", |
| 62 | + "executorch/devtools/", |
| 63 | + "executorch/extension/pybindings/", |
| 64 | +): |
| 65 | + matches = [name for name in names if name.startswith(forbidden)] |
| 66 | + if matches: |
| 67 | + raise AssertionError(f"{wheel_file} unexpectedly contains {matches[:5]}") |
| 68 | +
|
| 69 | +extensions = [ |
| 70 | + name |
| 71 | + for name in names |
| 72 | + if name.endswith((".so", ".dylib", ".dll", ".pyd")) and "flatc" not in name |
| 73 | +] |
| 74 | +if extensions: |
| 75 | + raise AssertionError(f"{wheel_file} unexpectedly contains extensions: {extensions}") |
| 76 | +
|
| 77 | +
|
| 78 | +def _dist_name(requirement): |
| 79 | + name = re.split(r"[ ;\[<>=!~(]", requirement.strip(), maxsplit=1)[0] |
| 80 | + return re.sub(r"[-_.]+", "-", name).lower() |
| 81 | +
|
| 82 | +
|
| 83 | +# Only the core (non-extra) Requires-Dist entries define what a plain |
| 84 | +# "pip install" pulls; ignore the optional extras (cortex_m, vgf, ...). |
| 85 | +declared = { |
| 86 | + _dist_name(line.split(":", 1)[1]) |
| 87 | + for line in metadata_text.splitlines() |
| 88 | + if line.startswith("Requires-Dist:") and "extra==" not in line.replace(" ", "") |
| 89 | +} |
| 90 | +# The minimal wheel must declare EXACTLY this core set and nothing else -- the |
| 91 | +# same names as `keep` in setup.py:_minimal_dependencies(). Exact match catches |
| 92 | +# both a heavy full-wheel dep leaking in (coremltools, pandas, or a re-added |
| 93 | +# mpmath/torch) and a required dep going missing. |
| 94 | +expected = { |
| 95 | + "flatbuffers", |
| 96 | + "numpy", |
| 97 | + "packaging", |
| 98 | + "pyyaml", |
| 99 | + "ruamel-yaml", |
| 100 | + "sympy", |
| 101 | + "tabulate", |
| 102 | + "typing-extensions", |
| 103 | +} |
| 104 | +if declared != expected: |
| 105 | + raise AssertionError( |
| 106 | + f"{wheel_file} minimal core deps mismatch: " |
| 107 | + f"unexpected={sorted(declared - expected)} missing={sorted(expected - declared)}" |
| 108 | + ) |
| 109 | +PY |
| 110 | + |
| 111 | +deactivate |
| 112 | + |
| 113 | +"${PYTHON_EXECUTABLE}" -m venv "${TEST_VENV}" |
| 114 | +source "${TEST_VENV}/bin/activate" |
| 115 | +python -m pip install --upgrade pip |
| 116 | +# torch and torchvision are needed to export a model but are intentionally not |
| 117 | +# declared as wheel dependencies (consumers are expected to bring their own). |
| 118 | +python -m pip install \ |
| 119 | + "torch" \ |
| 120 | + "torchvision" \ |
| 121 | + --index-url https://download.pytorch.org/whl/cpu \ |
| 122 | + --extra-index-url https://pypi.org/simple |
| 123 | +# Install the minimal wheel WITHOUT --no-deps so pip resolves its declared |
| 124 | +# dependencies, confirming the slim set is correct and resolvable. (That no heavy |
| 125 | +# deps sneak in is guaranteed by the METADATA exact-match check above, which |
| 126 | +# covers the wheel's direct Requires-Dist.) |
| 127 | +python -m pip install \ |
| 128 | + "${WHEEL_FILE}" \ |
| 129 | + --index-url https://download.pytorch.org/whl/cpu \ |
| 130 | + --extra-index-url https://pypi.org/simple |
| 131 | + |
| 132 | +# flatc is the only compiled artifact in the minimal wheel and the reason it is |
| 133 | +# platform specific. Confirm it ships, resolves through _get_flatc_path() (the |
| 134 | +# executorch.data.bin lookup added for this build mode), and actually runs. |
| 135 | +python - <<'PY' |
| 136 | +import subprocess |
| 137 | +
|
| 138 | +from executorch.exir._serialize._flatbuffer import _get_flatc_path |
| 139 | +
|
| 140 | +flatc_path = _get_flatc_path() |
| 141 | +print(f"flatc resolved to: {flatc_path}") |
| 142 | +subprocess.run([flatc_path, "--version"], check=True) |
| 143 | +PY |
| 144 | + |
| 145 | +python - <<'PY' |
| 146 | +from pathlib import Path |
| 147 | +
|
| 148 | +import torch |
| 149 | +from torch.export import export |
| 150 | +from torchvision.models import mobilenet_v2 |
| 151 | +
|
| 152 | +from executorch.exir import to_edge_transform_and_lower |
| 153 | +
|
| 154 | +model = mobilenet_v2(weights=None).eval() |
| 155 | +example_inputs = (torch.randn(1, 3, 224, 224),) |
| 156 | +
|
| 157 | +edge_program = to_edge_transform_and_lower(export(model, example_inputs)) |
| 158 | +executorch_program = edge_program.to_executorch() |
| 159 | +
|
| 160 | +output_path = Path("mv2_minimal.pte") |
| 161 | +with output_path.open("wb") as output_file: |
| 162 | + executorch_program.write_to_file(output_file) |
| 163 | +
|
| 164 | +assert output_path.stat().st_size > 0 |
| 165 | +PY |
0 commit comments