1010import glob
1111import itertools
1212import os
13+ import platform
14+ import re
1315import shutil
1416import subprocess
1517import sys
1618
17- from install_requirements import (
18- install_requirements ,
19- python_is_compatible ,
20- TORCH_NIGHTLY_URL ,
21- )
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
2266
2367
2468def clean ():
@@ -35,6 +79,78 @@ def clean():
3579VALID_PYBINDS = ["coreml" , "mps" , "xnnpack" ]
3680
3781
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+
38154def main (args ):
39155 if not python_is_compatible ():
40156 sys .exit (1 )
@@ -136,9 +252,4 @@ def main(args):
136252
137253
138254if __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-
144255 main (sys .argv [1 :])
0 commit comments