Skip to content

Commit 79058cd

Browse files
committed
install_requirements.py: refactor: extract install_requirements() function
More preparation for separating installation of requirements from installation of ExecuTorch. Test Plan: ./install_requirements.sh in a fresh venv succeeded and reported installing executorch ghstack-source-id: bd6cc93 ghstack-comment-id: 2596313423 Pull Request resolved: #7704
1 parent 98f8ade commit 79058cd

File tree

1 file changed

+85
-79
lines changed

1 file changed

+85
-79
lines changed

install_requirements.py

Lines changed: 85 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -76,94 +76,29 @@ def clean():
7676
print("Done cleaning build artifacts.")
7777

7878

79-
def main(args):
80-
if not python_is_compatible():
81-
sys.exit(1)
79+
# The pip repository that hosts nightly torch packages.
80+
TORCH_NIGHTLY_URL = "https://download.pytorch.org/whl/nightly/cpu"
8281

83-
# Parse options.
8482

85-
EXECUTORCH_BUILD_PYBIND = ""
86-
CMAKE_ARGS = os.getenv("CMAKE_ARGS", "")
87-
CMAKE_BUILD_ARGS = os.getenv("CMAKE_BUILD_ARGS", "")
88-
USE_PYTORCH_NIGHTLY = True
89-
90-
parser = argparse.ArgumentParser(prog="install_requirements")
91-
parser.add_argument(
92-
"--pybind",
93-
action="append",
94-
nargs="+",
95-
help="one or more of coreml/mps/xnnpack, or off",
96-
)
97-
parser.add_argument(
98-
"--clean",
99-
action="store_true",
100-
help="clean build artifacts and pip-out instead of installing",
101-
)
102-
parser.add_argument(
103-
"--use-pt-pinned-commit",
104-
action="store_true",
105-
help="build from the pinned PyTorch commit instead of nightly",
106-
)
107-
args = parser.parse_args(args)
108-
if args.pybind:
109-
# Flatten list of lists.
110-
args.pybind = list(itertools.chain(*args.pybind))
111-
if "off" in args.pybind:
112-
if len(args.pybind) != 1:
113-
raise Exception(
114-
f"Cannot combine `off` with other pybinds: {args.pybind}"
115-
)
116-
EXECUTORCH_BUILD_PYBIND = "OFF"
117-
else:
118-
for pybind_arg in args.pybind:
119-
if pybind_arg not in ["coreml", "mps", "xnnpack"]:
120-
continue
121-
EXECUTORCH_BUILD_PYBIND = "ON"
122-
CMAKE_ARGS += f" -DEXECUTORCH_BUILD_{pybind_arg.upper()}=ON"
123-
124-
if args.clean:
125-
clean()
126-
return
127-
128-
if args.use_pt_pinned_commit:
129-
# This option is used in CI to make sure that PyTorch build from the pinned commit
130-
# is used instead of nightly. CI jobs wouldn't be able to catch regression from the
131-
# latest PT commit otherwise
132-
USE_PYTORCH_NIGHTLY = False
133-
134-
# If --pybind is not set explicitly for backends (e.g., --pybind xnnpack)
135-
# or is not turned off explicitly (--pybind off)
136-
# then install XNNPACK by default.
137-
if EXECUTORCH_BUILD_PYBIND == "":
138-
EXECUTORCH_BUILD_PYBIND = "ON"
139-
CMAKE_ARGS += " -DEXECUTORCH_BUILD_XNNPACK=ON"
140-
141-
# Use ClangCL on Windows.
142-
# ClangCL is an alias to Clang that configures it to work in an MSVC-compatible
143-
# mode. Using it on Windows to avoid compiler compatibility issues for MSVC.
144-
if os.name == "nt":
145-
CMAKE_ARGS += " -T ClangCL"
146-
147-
# Since ExecuTorch often uses main-branch features of pytorch, only the nightly
148-
# pip versions will have the required features.
149-
#
150-
# NOTE: If a newly-fetched version of the executorch repo changes the value of
151-
# NIGHTLY_VERSION, you should re-run this script to install the necessary
152-
# package versions.
153-
NIGHTLY_VERSION = "dev20250104"
83+
# Since ExecuTorch often uses main-branch features of pytorch, only the nightly
84+
# pip versions will have the required features.
85+
#
86+
# NOTE: If a newly-fetched version of the executorch repo changes the value of
87+
# NIGHTLY_VERSION, you should re-run this script to install the necessary
88+
# package versions.
89+
NIGHTLY_VERSION = "dev20250104"
15490

155-
# The pip repository that hosts nightly torch packages.
156-
TORCH_NIGHTLY_URL = "https://download.pytorch.org/whl/nightly/cpu"
15791

92+
def install_requirements(use_pytorch_nightly):
15893
# pip packages needed by exir.
15994
EXIR_REQUIREMENTS = [
160-
# Setting USE_PYTORCH_NIGHTLY to false to test the pinned PyTorch commit. Note
95+
# Setting use_pytorch_nightly to false to test the pinned PyTorch commit. Note
16196
# that we don't need to set any version number there because they have already
16297
# been installed on CI before this step, so pip won't reinstall them
163-
f"torch==2.6.0.{NIGHTLY_VERSION}" if USE_PYTORCH_NIGHTLY else "torch",
98+
f"torch==2.6.0.{NIGHTLY_VERSION}" if use_pytorch_nightly else "torch",
16499
(
165100
f"torchvision==0.22.0.{NIGHTLY_VERSION}"
166-
if USE_PYTORCH_NIGHTLY
101+
if use_pytorch_nightly
167102
else "torchvision"
168103
), # For testing.
169104
"typing-extensions",
@@ -173,7 +108,7 @@ def main(args):
173108
# TODO: Make each example publish its own requirements.txt
174109
EXAMPLES_REQUIREMENTS = [
175110
"timm==1.0.7",
176-
f"torchaudio==2.6.0.{NIGHTLY_VERSION}" if USE_PYTORCH_NIGHTLY else "torchaudio",
111+
f"torchaudio==2.6.0.{NIGHTLY_VERSION}" if use_pytorch_nightly else "torchaudio",
177112
"torchsr==1.0.4",
178113
"transformers==4.47.1",
179114
]
@@ -228,6 +163,77 @@ def main(args):
228163
check=True,
229164
)
230165

166+
167+
def main(args):
168+
if not python_is_compatible():
169+
sys.exit(1)
170+
171+
# Parse options.
172+
173+
EXECUTORCH_BUILD_PYBIND = ""
174+
CMAKE_ARGS = os.getenv("CMAKE_ARGS", "")
175+
CMAKE_BUILD_ARGS = os.getenv("CMAKE_BUILD_ARGS", "")
176+
use_pytorch_nightly = True
177+
178+
parser = argparse.ArgumentParser(prog="install_requirements")
179+
parser.add_argument(
180+
"--pybind",
181+
action="append",
182+
nargs="+",
183+
help="one or more of coreml/mps/xnnpack, or off",
184+
)
185+
parser.add_argument(
186+
"--clean",
187+
action="store_true",
188+
help="clean build artifacts and pip-out instead of installing",
189+
)
190+
parser.add_argument(
191+
"--use-pt-pinned-commit",
192+
action="store_true",
193+
help="build from the pinned PyTorch commit instead of nightly",
194+
)
195+
args = parser.parse_args(args)
196+
if args.pybind:
197+
# Flatten list of lists.
198+
args.pybind = list(itertools.chain(*args.pybind))
199+
if "off" in args.pybind:
200+
if len(args.pybind) != 1:
201+
raise Exception(
202+
f"Cannot combine `off` with other pybinds: {args.pybind}"
203+
)
204+
EXECUTORCH_BUILD_PYBIND = "OFF"
205+
else:
206+
for pybind_arg in args.pybind:
207+
if pybind_arg not in ["coreml", "mps", "xnnpack"]:
208+
continue
209+
EXECUTORCH_BUILD_PYBIND = "ON"
210+
CMAKE_ARGS += f" -DEXECUTORCH_BUILD_{pybind_arg.upper()}=ON"
211+
212+
if args.clean:
213+
clean()
214+
return
215+
216+
if args.use_pt_pinned_commit:
217+
# This option is used in CI to make sure that PyTorch build from the pinned commit
218+
# is used instead of nightly. CI jobs wouldn't be able to catch regression from the
219+
# latest PT commit otherwise
220+
use_pytorch_nightly = False
221+
222+
install_requirements(use_pytorch_nightly)
223+
224+
# If --pybind is not set explicitly for backends (e.g., --pybind xnnpack)
225+
# or is not turned off explicitly (--pybind off)
226+
# then install XNNPACK by default.
227+
if EXECUTORCH_BUILD_PYBIND == "":
228+
EXECUTORCH_BUILD_PYBIND = "ON"
229+
CMAKE_ARGS += " -DEXECUTORCH_BUILD_XNNPACK=ON"
230+
231+
# Use ClangCL on Windows.
232+
# ClangCL is an alias to Clang that configures it to work in an MSVC-compatible
233+
# mode. Using it on Windows to avoid compiler compatibility issues for MSVC.
234+
if os.name == "nt":
235+
CMAKE_ARGS += " -T ClangCL"
236+
231237
#
232238
# Install executorch pip package. This also makes `flatc` available on the path.
233239
# The --extra-index-url may be necessary if pyproject.toml has a dependency on a

0 commit comments

Comments
 (0)