Skip to content

Commit cd0e584

Browse files
authored
Add install_requirements.{sh,py} that only install requirements (#7715)
Now that the previous PR renamed install_requirements to install_executorch, we can use those names for scripts that actually just install requirements.
1 parent ee1d7c3 commit cd0e584

5 files changed

+197
-137
lines changed

install_executorch.py

+10-121
Original file line numberDiff line numberDiff line change
@@ -10,59 +10,15 @@
1010
import glob
1111
import itertools
1212
import os
13-
import platform
14-
import re
1513
import shutil
1614
import subprocess
1715
import sys
1816

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
17+
from install_requirements import (
18+
install_requirements,
19+
python_is_compatible,
20+
TORCH_NIGHTLY_URL,
21+
)
6622

6723

6824
def clean():
@@ -79,78 +35,6 @@ def clean():
7935
VALID_PYBINDS = ["coreml", "mps", "xnnpack"]
8036

8137

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-
15438
def main(args):
15539
if not python_is_compatible():
15640
sys.exit(1)
@@ -252,4 +136,9 @@ def main(args):
252136

253137

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

install_executorch.sh

+1-16
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

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

install_requirements.sh

+10
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

+26
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)