Skip to content

Add install_requirements.{sh,py} that only install requirements #7715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 41 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
da1df94
Update
swolchok Jan 16, 2025
e411a1e
Update
swolchok Jan 16, 2025
6e9745c
Update
swolchok Jan 16, 2025
6d65549
Update
swolchok Jan 16, 2025
ac71f8d
Update
swolchok Jan 16, 2025
00fd17d
Update
swolchok Jan 16, 2025
9a1ddde
Update
swolchok Jan 16, 2025
2063432
Update
swolchok Jan 16, 2025
cd79afd
Update
swolchok Jan 16, 2025
e76d22f
Update
swolchok Jan 16, 2025
a893391
Update
swolchok Jan 16, 2025
84b8e5f
Update
swolchok Jan 16, 2025
fd0a5b1
Update
swolchok Jan 16, 2025
bf753a8
Update
swolchok Jan 16, 2025
9158d0e
Update
swolchok Jan 16, 2025
400ec37
Update
swolchok Jan 16, 2025
cab1ad3
Update
swolchok Jan 16, 2025
1238f24
Update
swolchok Jan 16, 2025
e089ac5
Update
swolchok Jan 16, 2025
2dcdbcd
Update
swolchok Jan 16, 2025
a89792a
Update
swolchok Jan 16, 2025
a95cc8d
Update
swolchok Jan 16, 2025
27a3d64
Update
swolchok Jan 16, 2025
1d4dfab
Update
swolchok Jan 16, 2025
ff3a43d
Update
swolchok Jan 16, 2025
f98d073
Update
swolchok Jan 16, 2025
23c60d7
Update
swolchok Jan 16, 2025
5e8be76
Update
swolchok Jan 16, 2025
641ca89
Update
swolchok Jan 17, 2025
57dab67
Update
swolchok Jan 17, 2025
238d145
Update
swolchok Jan 17, 2025
3f2c188
Update
swolchok Jan 17, 2025
4561c6b
Update
swolchok Jan 17, 2025
c208b1f
Update
swolchok Jan 17, 2025
f2719b2
Update
swolchok Jan 17, 2025
4b3f654
Update
swolchok Jan 17, 2025
a4f50f8
Update
swolchok Jan 17, 2025
86e9c74
Update
swolchok Jan 17, 2025
87a4c2d
Update
swolchok Jan 17, 2025
5ca60ad
Update
swolchok Jan 17, 2025
b6da309
Update
swolchok Jan 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 10 additions & 121 deletions install_executorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,15 @@
import glob
import itertools
import os
import platform
import re
import shutil
import subprocess
import sys

# Before doing anything, cd to the directory containing this script.
os.chdir(os.path.dirname(os.path.abspath(__file__)))


def python_is_compatible():
# Scrape the version range from pyproject.toml, which should be in the current directory.
version_specifier = None
with open("pyproject.toml", "r") as file:
for line in file:
if line.startswith("requires-python"):
match = re.search(r'"([^"]*)"', line)
if match:
version_specifier = match.group(1)
break

if not version_specifier:
print(
"WARNING: Skipping python version check: version range not found",
file=sys.stderr,
)
return False

# Install the packaging module if necessary.
try:
import packaging
except ImportError:
subprocess.run(
[sys.executable, "-m", "pip", "install", "packaging"], check=True
)
# Compare the current python version to the range in version_specifier. Exits
# with status 1 if the version is not compatible, or with status 0 if the
# version is compatible or the logic itself fails.
try:
import packaging.specifiers
import packaging.version

python_version = packaging.version.parse(platform.python_version())
version_range = packaging.specifiers.SpecifierSet(version_specifier)
if python_version not in version_range:
print(
f'ERROR: ExecuTorch does not support python version {python_version}: must satisfy "{version_specifier}"',
file=sys.stderr,
)
return False
except Exception as e:
print(f"WARNING: Skipping python version check: {e}", file=sys.stderr)
return True
from install_requirements import (
install_requirements,
python_is_compatible,
TORCH_NIGHTLY_URL,
)


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


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


# Since ExecuTorch often uses main-branch features of pytorch, only the nightly
# pip versions will have the required features.
#
# NOTE: If a newly-fetched version of the executorch repo changes the value of
# NIGHTLY_VERSION, you should re-run this script to install the necessary
# package versions.
NIGHTLY_VERSION = "dev20250104"


def install_requirements(use_pytorch_nightly):
# pip packages needed by exir.
EXIR_REQUIREMENTS = [
# Setting use_pytorch_nightly to false to test the pinned PyTorch commit. Note
# that we don't need to set any version number there because they have already
# been installed on CI before this step, so pip won't reinstall them
f"torch==2.6.0.{NIGHTLY_VERSION}" if use_pytorch_nightly else "torch",
(
f"torchvision==0.22.0.{NIGHTLY_VERSION}"
if use_pytorch_nightly
else "torchvision"
), # For testing.
]

EXAMPLES_REQUIREMENTS = [
f"torchaudio==2.6.0.{NIGHTLY_VERSION}" if use_pytorch_nightly else "torchaudio",
]

# Assemble the list of requirements to actually install.
# TODO: Add options for reducing the number of requirements.
REQUIREMENTS_TO_INSTALL = EXIR_REQUIREMENTS + EXAMPLES_REQUIREMENTS

# Install the requirements. `--extra-index-url` tells pip to look for package
# versions on the provided URL if they aren't available on the default URL.
subprocess.run(
[
sys.executable,
"-m",
"pip",
"install",
"-r",
"requirements-examples.txt",
*REQUIREMENTS_TO_INSTALL,
"--extra-index-url",
TORCH_NIGHTLY_URL,
],
check=True,
)

LOCAL_REQUIREMENTS = [
"third-party/ao", # We need the latest kernels for fast iteration, so not relying on pypi.
]

# Install packages directly from local copy instead of pypi.
# This is usually not recommended.
subprocess.run(
[
sys.executable,
"-m",
"pip",
"install",
# Without --no-build-isolation, setup.py can't find the torch module.
"--no-build-isolation",
*LOCAL_REQUIREMENTS,
],
check=True,
)


def main(args):
if not python_is_compatible():
sys.exit(1)
Expand Down Expand Up @@ -252,4 +136,9 @@ def main(args):


if __name__ == "__main__":
# Before doing anything, cd to the directory containing this script.
os.chdir(os.path.dirname(os.path.abspath(__file__)))
if not python_is_compatible():
sys.exit(1)

main(sys.argv[1:])
17 changes: 1 addition & 16 deletions install_executorch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,4 @@

# Before doing anything, cd to the directory containing this script.
cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null || /bin/true

# Find the names of the python tools to use.
if [[ -z $PYTHON_EXECUTABLE ]];
then
if [[ -z $CONDA_DEFAULT_ENV ]] || [[ $CONDA_DEFAULT_ENV == "base" ]] || [[ ! -x "$(command -v python)" ]];
then
PYTHON_EXECUTABLE=python3
else
PYTHON_EXECUTABLE=python
fi
fi

$PYTHON_EXECUTABLE ./install_executorch.py "$@"

# Exit with the same status as the python script.
exit $?
./run_python_script.sh ./install_executorch.py "$@"
150 changes: 150 additions & 0 deletions install_requirements.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# Copyright 2024-25 Arm Limited and/or its affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

import argparse
import platform
import re
import subprocess
import sys


def python_is_compatible():
# Scrape the version range from pyproject.toml, which should be in the current directory.
version_specifier = None
with open("pyproject.toml", "r") as file:
for line in file:
if line.startswith("requires-python"):
match = re.search(r'"([^"]*)"', line)
if match:
version_specifier = match.group(1)
break

if not version_specifier:
print(
"WARNING: Skipping python version check: version range not found",
file=sys.stderr,
)
return False

# Install the packaging module if necessary.
try:
import packaging
except ImportError:
subprocess.run(
[sys.executable, "-m", "pip", "install", "packaging"], check=True
)
# Compare the current python version to the range in version_specifier. Exits
# with status 1 if the version is not compatible, or with status 0 if the
# version is compatible or the logic itself fails.
try:
import packaging.specifiers
import packaging.version

python_version = packaging.version.parse(platform.python_version())
version_range = packaging.specifiers.SpecifierSet(version_specifier)
if python_version not in version_range:
print(
f'ERROR: ExecuTorch does not support python version {python_version}: must satisfy "{version_specifier}"',
file=sys.stderr,
)
return False
except Exception as e:
print(f"WARNING: Skipping python version check: {e}", file=sys.stderr)
return True


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


# Since ExecuTorch often uses main-branch features of pytorch, only the nightly
# pip versions will have the required features.
#
# NOTE: If a newly-fetched version of the executorch repo changes the value of
# NIGHTLY_VERSION, you should re-run this script to install the necessary
# package versions.
NIGHTLY_VERSION = "dev20250104"


def install_requirements(use_pytorch_nightly):
# pip packages needed by exir.
EXIR_REQUIREMENTS = [
# Setting use_pytorch_nightly to false to test the pinned PyTorch commit. Note
# that we don't need to set any version number there because they have already
# been installed on CI before this step, so pip won't reinstall them
f"torch==2.6.0.{NIGHTLY_VERSION}" if use_pytorch_nightly else "torch",
(
f"torchvision==0.22.0.{NIGHTLY_VERSION}"
if use_pytorch_nightly
else "torchvision"
), # For testing.
]

EXAMPLES_REQUIREMENTS = [
f"torchaudio==2.6.0.{NIGHTLY_VERSION}" if use_pytorch_nightly else "torchaudio",
]

# Assemble the list of requirements to actually install.
# TODO: Add options for reducing the number of requirements.
REQUIREMENTS_TO_INSTALL = EXIR_REQUIREMENTS + EXAMPLES_REQUIREMENTS

# Install the requirements. `--extra-index-url` tells pip to look for package
# versions on the provided URL if they aren't available on the default URL.
subprocess.run(
[
sys.executable,
"-m",
"pip",
"install",
"-r",
"requirements-examples.txt",
*REQUIREMENTS_TO_INSTALL,
"--extra-index-url",
TORCH_NIGHTLY_URL,
],
check=True,
)

LOCAL_REQUIREMENTS = [
"third-party/ao", # We need the latest kernels for fast iteration, so not relying on pypi.
]

# Install packages directly from local copy instead of pypi.
# This is usually not recommended.
subprocess.run(
[
sys.executable,
"-m",
"pip",
"install",
# Without --no-build-isolation, setup.py can't find the torch module.
"--no-build-isolation",
*LOCAL_REQUIREMENTS,
],
check=True,
)


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np: don't you wanna do the other way around?

move the install_requirements() function into this file instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

def main(args):
parser = argparse.ArgumentParser()
parser.add_argument(
"--use-pt-pinned-commit",
action="store_true",
help="build from the pinned PyTorch commit instead of nightly",
)
args = parser.parse_args(args)
install_requirements(use_pytorch_nightly=not bool(args.use_pt_pinned_commit))


if __name__ == "__main__":
import os

# Before doing anything, cd to the directory containing this script.
os.chdir(os.path.dirname(os.path.abspath(__file__)))
if not python_is_compatible():
sys.exit(1)
main(sys.argv[1:])
10 changes: 10 additions & 0 deletions install_requirements.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

# Before doing anything, cd to the directory containing this script.
cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null || /bin/true
./run_python_script.sh ./install_requirements.py "$@"
26 changes: 26 additions & 0 deletions run_python_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

# Before doing anything, cd to the directory containing this script.
cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null || /bin/true

# Find the names of the python tools to use.
if [[ -z $PYTHON_EXECUTABLE ]];
then
if [[ -z $CONDA_DEFAULT_ENV ]] || [[ $CONDA_DEFAULT_ENV == "base" ]] || [[ ! -x "$(command -v python)" ]];
then
PYTHON_EXECUTABLE=python3
else
PYTHON_EXECUTABLE=python
fi
fi

SCRIPT="$1"; shift
$PYTHON_EXECUTABLE $SCRIPT "$@"

# Exit with the same status as the python script.
exit $?
Loading