|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2025 Advanced Micro Devices, Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License v2.0 with LLVM Exceptions. |
| 6 | +# See https://llvm.org/LICENSE.txt for license information. |
| 7 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 8 | + |
| 9 | +"""Updates the pinned IREE versions in this repository. |
| 10 | +
|
| 11 | +Usage: |
| 12 | + update_iree_requirement_pins.py |
| 13 | +
|
| 14 | +Environment variable outputs (via GITHUB_ENV) for GitHub Actions: |
| 15 | + CURRENT_IREE_BASE_COMPILER_VERSION |
| 16 | + CURRENT_IREE_BASE_RUNTIME_VERSION |
| 17 | + CURRENT_IREE_TURBINE_VERSION |
| 18 | + LATEST_IREE_BASE_COMPILER_VERSION |
| 19 | + LATEST_IREE_BASE_RUNTIME_VERSION |
| 20 | + LATEST_IREE_TURBINE_VERSION |
| 21 | +""" |
| 22 | + |
| 23 | +from pathlib import Path |
| 24 | +import os |
| 25 | +import re |
| 26 | +import subprocess |
| 27 | +import sys |
| 28 | +import textwrap |
| 29 | + |
| 30 | +REPO_ROOT = Path(__file__).parent.parent |
| 31 | +REQUIREMENTS_IREE_PINNED_PATH = REPO_ROOT / "requirements-iree-pinned.txt" |
| 32 | +SHORTFIN_CMAKELISTS_PATH = REPO_ROOT / "shortfin" / "CMakeLists.txt" |
| 33 | + |
| 34 | + |
| 35 | +def get_current_version(package_name): |
| 36 | + with open(REQUIREMENTS_IREE_PINNED_PATH, "r") as f: |
| 37 | + text = f.read() |
| 38 | + return re.findall(f"{package_name}==(.*)", text)[0] |
| 39 | + |
| 40 | + |
| 41 | +def get_latest_version(package_name, extra_pip_args=[]): |
| 42 | + print("\n-------------------------------------------------------------------------") |
| 43 | + print(f"Finding latest available package version for package '{package_name}'\n") |
| 44 | + |
| 45 | + # This queries the pip index to get the latest version. |
| 46 | + # |
| 47 | + # This doesn't require downloading or installing, so it should be more |
| 48 | + # portable across operating systems than other approaches. |
| 49 | + # |
| 50 | + # Note: the `index` subcommand is experimental. We could instead: |
| 51 | + # * Install (into a venv) then check what was installed with `--report`, |
| 52 | + # `pip freeze`, or regex parsing (as in `get_current_version()` above) |
| 53 | + # * Download then check what was downloaded |
| 54 | + # * Scrape the package index and/or release page (https://iree.dev/pip-release-links.html) |
| 55 | + subprocess_args = [ |
| 56 | + sys.executable, |
| 57 | + "-m", |
| 58 | + "pip", |
| 59 | + "index", |
| 60 | + "versions", |
| 61 | + package_name, |
| 62 | + "--disable-pip-version-check", |
| 63 | + ] |
| 64 | + subprocess_args.extend(extra_pip_args) |
| 65 | + |
| 66 | + print(f"Running command:\n {subprocess.list2cmdline(subprocess_args)}\n") |
| 67 | + result = subprocess.run(subprocess_args, stdout=subprocess.PIPE) |
| 68 | + output = result.stdout.decode("utf-8") |
| 69 | + print(f"Command output:\n{textwrap.indent(output, ' ')}") |
| 70 | + |
| 71 | + # Search for text like `iree-base-compiler (3.2.0rc20250109)` within the |
| 72 | + # multiple lines of output from the command. |
| 73 | + # WARNING: The output from `pip index` is UNSTABLE and UNSTRUCTURED, but |
| 74 | + # this seems to work using Python 3.11.2 and pip 22.3.1. |
| 75 | + version_search_regex = re.compile(f"{package_name}\s\((.*)\)") |
| 76 | + matches = version_search_regex.match(output) |
| 77 | + if not matches: |
| 78 | + raise RuntimeError("Failed to find a package version using regex") |
| 79 | + version = matches.groups()[0] |
| 80 | + print( |
| 81 | + f"Found package version for '{package_name}' in output using regex: '{version}'" |
| 82 | + ) |
| 83 | + return version |
| 84 | + |
| 85 | + |
| 86 | +def get_current_git_tag(): |
| 87 | + with open(SHORTFIN_CMAKELISTS_PATH, "r") as f: |
| 88 | + text = f.read() |
| 89 | + return re.findall('SHORTFIN_IREE_GIT_TAG "(.*)"', text)[0] |
| 90 | + |
| 91 | + |
| 92 | +def main(): |
| 93 | + print("Updating IREE version pins!") |
| 94 | + |
| 95 | + current_compiler_version = get_current_version("iree-base-compiler") |
| 96 | + current_runtime_version = get_current_version("iree-base-runtime") |
| 97 | + current_turbine_version = get_current_version("iree-turbine") |
| 98 | + current_git_tag = get_current_git_tag() |
| 99 | + |
| 100 | + nightly_pip_args = [ |
| 101 | + "--pre", |
| 102 | + "--find-links", |
| 103 | + "https://iree.dev/pip-release-links.html", |
| 104 | + ] |
| 105 | + latest_compiler_version = get_latest_version("iree-base-compiler", nightly_pip_args) |
| 106 | + latest_runtime_version = get_latest_version("iree-base-runtime", nightly_pip_args) |
| 107 | + latest_turbine_version = get_latest_version("iree-turbine", nightly_pip_args) |
| 108 | + # TODO(scotttodd): Get this from git? It should generally be in sync with |
| 109 | + # the python packages and follow a naming convention. If that isn't |
| 110 | + # true, such as right after a stable release, then this may break. |
| 111 | + latest_git_tag = f"iree-{latest_runtime_version}" |
| 112 | + |
| 113 | + print("\n-------------------------------------------------------------------------") |
| 114 | + print("Current versions:") |
| 115 | + print(f" iree-base-compiler=={current_compiler_version}") |
| 116 | + print(f" iree-base-runtime=={current_runtime_version}") |
| 117 | + print(f" iree-turbine=={current_turbine_version}") |
| 118 | + print(f' SHORTFIN_IREE_GIT_TAG "{current_git_tag}"') |
| 119 | + print("Latest versions:") |
| 120 | + print(f" iree-base-compiler=={latest_compiler_version}") |
| 121 | + print(f" iree-base-runtime=={latest_runtime_version}") |
| 122 | + print(f" iree-turbine=={latest_turbine_version}") |
| 123 | + print(f' SHORTFIN_IREE_GIT_TAG "{latest_git_tag}"') |
| 124 | + |
| 125 | + # Write to GitHub Actions environment variables for future steps to use if they want. |
| 126 | + github_env = os.getenv("GITHUB_ENV") |
| 127 | + if github_env: |
| 128 | + with open(github_env, "a") as fh: |
| 129 | + print( |
| 130 | + f"CURRENT_IREE_BASE_COMPILER_VERSION={current_compiler_version}", |
| 131 | + file=fh, |
| 132 | + ) |
| 133 | + print( |
| 134 | + f"CURRENT_IREE_BASE_RUNTIME_VERSION={current_runtime_version}", file=fh |
| 135 | + ) |
| 136 | + print(f"CURRENT_IREE_TURBINE_VERSION={current_turbine_version}", file=fh) |
| 137 | + print(f"CURRENT_SHORTFIN_IREE_GIT_TAG={current_git_tag}", file=fh) |
| 138 | + print( |
| 139 | + f"LATEST_IREE_BASE_COMPILER_VERSION={latest_compiler_version}", file=fh |
| 140 | + ) |
| 141 | + print(f"LATEST_IREE_BASE_RUNTIME_VERSION={latest_runtime_version}", file=fh) |
| 142 | + print(f"LATEST_IREE_TURBINE_VERSION={latest_turbine_version}", file=fh) |
| 143 | + print(f"LATEST_SHORTFIN_IREE_GIT_TAG={latest_git_tag}", file=fh) |
| 144 | + |
| 145 | + if ( |
| 146 | + current_compiler_version == latest_compiler_version |
| 147 | + and current_runtime_version == latest_runtime_version |
| 148 | + and current_turbine_version == latest_turbine_version |
| 149 | + and current_git_tag == latest_git_tag |
| 150 | + ): |
| 151 | + print("Already using the latest versions, exiting") |
| 152 | + return |
| 153 | + |
| 154 | + print("\n-------------------------------------------------------------------------") |
| 155 | + print(f"Editing version pins in '{REQUIREMENTS_IREE_PINNED_PATH}'") |
| 156 | + with open(REQUIREMENTS_IREE_PINNED_PATH, "r") as f: |
| 157 | + text = f.read() |
| 158 | + print(f"Original text:\n{textwrap.indent(text, ' ')}\n") |
| 159 | + |
| 160 | + text = re.sub( |
| 161 | + "iree-base-compiler==.*", |
| 162 | + f"iree-base-compiler=={latest_compiler_version}", |
| 163 | + text, |
| 164 | + ) |
| 165 | + text = re.sub( |
| 166 | + "iree-base-runtime==.*", |
| 167 | + f"iree-base-runtime=={latest_runtime_version}", |
| 168 | + text, |
| 169 | + ) |
| 170 | + text = re.sub( |
| 171 | + "iree-turbine==.*", |
| 172 | + f"iree-turbine=={latest_turbine_version}", |
| 173 | + text, |
| 174 | + ) |
| 175 | + print(f"New text:\n{textwrap.indent(text, ' ')}\n") |
| 176 | + with open(REQUIREMENTS_IREE_PINNED_PATH, "w") as f: |
| 177 | + f.write(text) |
| 178 | + |
| 179 | + print(f"Editing git tag in '{SHORTFIN_CMAKELISTS_PATH}'") |
| 180 | + with open(SHORTFIN_CMAKELISTS_PATH, "r") as f: |
| 181 | + text = f.read() |
| 182 | + text = re.sub( |
| 183 | + 'SHORTFIN_IREE_GIT_TAG ".*"', |
| 184 | + f'SHORTFIN_IREE_GIT_TAG "{latest_git_tag}"', |
| 185 | + text, |
| 186 | + ) |
| 187 | + with open(SHORTFIN_CMAKELISTS_PATH, "w") as f: |
| 188 | + f.write(text) |
| 189 | + |
| 190 | + print("-------------------------------------------------------------------------") |
| 191 | + print("Edits complete") |
| 192 | + |
| 193 | + |
| 194 | +if __name__ == "__main__": |
| 195 | + main() |
0 commit comments