Skip to content

Commit 465ff70

Browse files
committed
Add install_requirements.{sh,py} that only install requirements
Now that the previous PR renamed install_requirements to install_executorch, we can use those names for scripts that actually just install requirements. ghstack-source-id: 0f178c5 ghstack-comment-id: 2597040494 Pull Request resolved: #7715
1 parent 12f08fd commit 465ff70

File tree

5 files changed

+196
-136
lines changed

5 files changed

+196
-136
lines changed

install_executorch.py

Lines changed: 10 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -11,58 +11,15 @@
1111
import itertools
1212
import os
1313
import platform
14-
import re
1514
import shutil
1615
import subprocess
1716
import sys
1817

19-
# Before doing anything, cd to the directory containing this script.
20-
os.chdir(os.path.dirname(os.path.abspath(__file__)))
21-
22-
23-
def python_is_compatible():
24-
# Scrape the version range from pyproject.toml, which should be in the current directory.
25-
version_specifier = None
26-
with open("pyproject.toml", "r") as file:
27-
for line in file:
28-
if line.startswith("requires-python"):
29-
match = re.search(r'"([^"]*)"', line)
30-
if match:
31-
version_specifier = match.group(1)
32-
break
33-
34-
if not version_specifier:
35-
print(
36-
"WARNING: Skipping python version check: version range not found",
37-
file=sys.stderr,
38-
)
39-
return False
40-
41-
# Install the packaging module if necessary.
42-
try:
43-
import packaging
44-
except ImportError:
45-
subprocess.run(
46-
[sys.executable, "-m", "pip", "install", "packaging"], check=True
47-
)
48-
# Compare the current python version to the range in version_specifier. Exits
49-
# with status 1 if the version is not compatible, or with status 0 if the
50-
# version is compatible or the logic itself fails.
51-
try:
52-
import packaging.specifiers
53-
import packaging.version
54-
55-
python_version = packaging.version.parse(platform.python_version())
56-
version_range = packaging.specifiers.SpecifierSet(version_specifier)
57-
if python_version not in version_range:
58-
print(
59-
f'ERROR: ExecuTorch does not support python version {python_version}: must satisfy "{version_specifier}"',
60-
file=sys.stderr,
61-
)
62-
return False
63-
except Exception as e:
64-
print(f"WARNING: Skipping python version check: {e}", file=sys.stderr)
65-
return True
18+
from install_requirements import (
19+
install_requirements,
20+
python_is_compatible,
21+
TORCH_NIGHTLY_URL,
22+
)
6623

6724

6825
def clean():
@@ -79,78 +36,6 @@ def clean():
7936
VALID_PYBINDS = ["coreml", "mps", "xnnpack"]
8037

8138

82-
# The pip repository that hosts nightly torch packages.
83-
TORCH_NIGHTLY_URL = "https://download.pytorch.org/whl/nightly/cpu"
84-
85-
86-
# Since ExecuTorch often uses main-branch features of pytorch, only the nightly
87-
# pip versions will have the required features.
88-
#
89-
# NOTE: If a newly-fetched version of the executorch repo changes the value of
90-
# NIGHTLY_VERSION, you should re-run this script to install the necessary
91-
# package versions.
92-
NIGHTLY_VERSION = "dev20250104"
93-
94-
95-
def install_requirements(use_pytorch_nightly):
96-
# pip packages needed by exir.
97-
EXIR_REQUIREMENTS = [
98-
# Setting use_pytorch_nightly to false to test the pinned PyTorch commit. Note
99-
# that we don't need to set any version number there because they have already
100-
# been installed on CI before this step, so pip won't reinstall them
101-
f"torch==2.6.0.{NIGHTLY_VERSION}" if use_pytorch_nightly else "torch",
102-
(
103-
f"torchvision==0.22.0.{NIGHTLY_VERSION}"
104-
if use_pytorch_nightly
105-
else "torchvision"
106-
), # For testing.
107-
]
108-
109-
EXAMPLES_REQUIREMENTS = [
110-
f"torchaudio==2.6.0.{NIGHTLY_VERSION}" if use_pytorch_nightly else "torchaudio",
111-
]
112-
113-
# Assemble the list of requirements to actually install.
114-
# TODO: Add options for reducing the number of requirements.
115-
REQUIREMENTS_TO_INSTALL = EXIR_REQUIREMENTS + EXAMPLES_REQUIREMENTS
116-
117-
# Install the requirements. `--extra-index-url` tells pip to look for package
118-
# versions on the provided URL if they aren't available on the default URL.
119-
subprocess.run(
120-
[
121-
sys.executable,
122-
"-m",
123-
"pip",
124-
"install",
125-
"-r",
126-
"requirements-examples.txt",
127-
*REQUIREMENTS_TO_INSTALL,
128-
"--extra-index-url",
129-
TORCH_NIGHTLY_URL,
130-
],
131-
check=True,
132-
)
133-
134-
LOCAL_REQUIREMENTS = [
135-
"third-party/ao", # We need the latest kernels for fast iteration, so not relying on pypi.
136-
]
137-
138-
# Install packages directly from local copy instead of pypi.
139-
# This is usually not recommended.
140-
subprocess.run(
141-
[
142-
sys.executable,
143-
"-m",
144-
"pip",
145-
"install",
146-
# Without --no-build-isolation, setup.py can't find the torch module.
147-
"--no-build-isolation",
148-
*LOCAL_REQUIREMENTS,
149-
],
150-
check=True,
151-
)
152-
153-
15439
def main(args):
15540
if not python_is_compatible():
15641
sys.exit(1)
@@ -252,4 +137,9 @@ def main(args):
252137

253138

254139
if __name__ == "__main__":
140+
# Before doing anything, cd to the directory containing this script.
141+
os.chdir(os.path.dirname(os.path.abspath(__file__)))
142+
if not python_is_compatible():
143+
sys.exit(1)
144+
255145
main(sys.argv[1:])

install_executorch.sh

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,4 @@
77

88
# Before doing anything, cd to the directory containing this script.
99
cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null || /bin/true
10-
11-
# Find the names of the python tools to use.
12-
if [[ -z $PYTHON_EXECUTABLE ]];
13-
then
14-
if [[ -z $CONDA_DEFAULT_ENV ]] || [[ $CONDA_DEFAULT_ENV == "base" ]] || [[ ! -x "$(command -v python)" ]];
15-
then
16-
PYTHON_EXECUTABLE=python3
17-
else
18-
PYTHON_EXECUTABLE=python
19-
fi
20-
fi
21-
22-
$PYTHON_EXECUTABLE ./install_executorch.py "$@"
23-
24-
# Exit with the same status as the python script.
25-
exit $?
10+
./run_python_script.sh ./install_executorch.py "$@"

install_requirements.py

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# Copyright 2024-25 Arm Limited and/or its 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+
import argparse
9+
import re
10+
import subprocess
11+
import sys
12+
13+
14+
def python_is_compatible():
15+
# Scrape the version range from pyproject.toml, which should be in the current directory.
16+
version_specifier = None
17+
with open("pyproject.toml", "r") as file:
18+
for line in file:
19+
if line.startswith("requires-python"):
20+
match = re.search(r'"([^"]*)"', line)
21+
if match:
22+
version_specifier = match.group(1)
23+
break
24+
25+
if not version_specifier:
26+
print(
27+
"WARNING: Skipping python version check: version range not found",
28+
file=sys.stderr,
29+
)
30+
return False
31+
32+
# Install the packaging module if necessary.
33+
try:
34+
import packaging
35+
except ImportError:
36+
subprocess.run(
37+
[sys.executable, "-m", "pip", "install", "packaging"], check=True
38+
)
39+
# Compare the current python version to the range in version_specifier. Exits
40+
# with status 1 if the version is not compatible, or with status 0 if the
41+
# version is compatible or the logic itself fails.
42+
try:
43+
import packaging.specifiers
44+
import packaging.version
45+
46+
python_version = packaging.version.parse(platform.python_version())
47+
version_range = packaging.specifiers.SpecifierSet(version_specifier)
48+
if python_version not in version_range:
49+
print(
50+
f'ERROR: ExecuTorch does not support python version {python_version}: must satisfy "{version_specifier}"',
51+
file=sys.stderr,
52+
)
53+
return False
54+
except Exception as e:
55+
print(f"WARNING: Skipping python version check: {e}", file=sys.stderr)
56+
return True
57+
58+
59+
# The pip repository that hosts nightly torch packages.
60+
TORCH_NIGHTLY_URL = "https://download.pytorch.org/whl/nightly/cpu"
61+
62+
63+
# Since ExecuTorch often uses main-branch features of pytorch, only the nightly
64+
# pip versions will have the required features.
65+
#
66+
# NOTE: If a newly-fetched version of the executorch repo changes the value of
67+
# NIGHTLY_VERSION, you should re-run this script to install the necessary
68+
# package versions.
69+
NIGHTLY_VERSION = "dev20250104"
70+
71+
72+
def install_requirements(use_pytorch_nightly):
73+
# pip packages needed by exir.
74+
EXIR_REQUIREMENTS = [
75+
# Setting use_pytorch_nightly to false to test the pinned PyTorch commit. Note
76+
# that we don't need to set any version number there because they have already
77+
# been installed on CI before this step, so pip won't reinstall them
78+
f"torch==2.6.0.{NIGHTLY_VERSION}" if use_pytorch_nightly else "torch",
79+
(
80+
f"torchvision==0.22.0.{NIGHTLY_VERSION}"
81+
if use_pytorch_nightly
82+
else "torchvision"
83+
), # For testing.
84+
]
85+
86+
EXAMPLES_REQUIREMENTS = [
87+
f"torchaudio==2.6.0.{NIGHTLY_VERSION}" if use_pytorch_nightly else "torchaudio",
88+
]
89+
90+
# Assemble the list of requirements to actually install.
91+
# TODO: Add options for reducing the number of requirements.
92+
REQUIREMENTS_TO_INSTALL = EXIR_REQUIREMENTS + EXAMPLES_REQUIREMENTS
93+
94+
# Install the requirements. `--extra-index-url` tells pip to look for package
95+
# versions on the provided URL if they aren't available on the default URL.
96+
subprocess.run(
97+
[
98+
sys.executable,
99+
"-m",
100+
"pip",
101+
"install",
102+
"-r",
103+
"requirements-examples.txt",
104+
*REQUIREMENTS_TO_INSTALL,
105+
"--extra-index-url",
106+
TORCH_NIGHTLY_URL,
107+
],
108+
check=True,
109+
)
110+
111+
LOCAL_REQUIREMENTS = [
112+
"third-party/ao", # We need the latest kernels for fast iteration, so not relying on pypi.
113+
]
114+
115+
# Install packages directly from local copy instead of pypi.
116+
# This is usually not recommended.
117+
subprocess.run(
118+
[
119+
sys.executable,
120+
"-m",
121+
"pip",
122+
"install",
123+
# Without --no-build-isolation, setup.py can't find the torch module.
124+
"--no-build-isolation",
125+
*LOCAL_REQUIREMENTS,
126+
],
127+
check=True,
128+
)
129+
130+
131+
def main(args):
132+
parser = argparse.ArgumentParser()
133+
parser.add_argument(
134+
"--use-pt-pinned-commit",
135+
action="store_true",
136+
help="build from the pinned PyTorch commit instead of nightly",
137+
)
138+
args = parser.parse_args(args)
139+
install_requirements(use_pytorch_nightly=not bool(args.use_pt_pinned_commit))
140+
141+
142+
if __name__ == "__main__":
143+
import os
144+
145+
# Before doing anything, cd to the directory containing this script.
146+
os.chdir(os.path.dirname(os.path.abspath(__file__)))
147+
if not python_is_compatible():
148+
sys.exit(1)
149+
main(sys.argv[1:])

install_requirements.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
# Before doing anything, cd to the directory containing this script.
9+
cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null || /bin/true
10+
./run_python_script.sh ./install_requirements.py "$@"

run_python_script.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
# Before doing anything, cd to the directory containing this script.
9+
cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null || /bin/true
10+
11+
# Find the names of the python tools to use.
12+
if [[ -z $PYTHON_EXECUTABLE ]];
13+
then
14+
if [[ -z $CONDA_DEFAULT_ENV ]] || [[ $CONDA_DEFAULT_ENV == "base" ]] || [[ ! -x "$(command -v python)" ]];
15+
then
16+
PYTHON_EXECUTABLE=python3
17+
else
18+
PYTHON_EXECUTABLE=python
19+
fi
20+
fi
21+
22+
SCRIPT="$1"; shift
23+
$PYTHON_EXECUTABLE $SCRIPT "$@"
24+
25+
# Exit with the same status as the python script.
26+
exit $?

0 commit comments

Comments
 (0)