|
| 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:]) |
0 commit comments