Skip to content
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

Upload integrate tools for integrate transparency #2627

Merged
merged 12 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
43 changes: 43 additions & 0 deletions build_tools/integrate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Integrate Scripts

A collection of scripts used to integrate the StableHLO repository into the
rest of the OpenXLA repos. These scripts are run ~2x/wk by a StableHLO oncall
rotation at Google to ensure new changes can propagate to the rest of the
ecosystem in a reasonable amount of time.

## Integrate Process

### Bump LLVM Revision

First we bump LLVM to match that of XLA, and apply any patches the the XLA
LLVM integrate had to apply to StableHLO to build LLVM:
GleasonK marked this conversation as resolved.
Show resolved Hide resolved

```
$ ./build_tools/integrate/llvm_bump_revision.sh
```

### Integarte into OpenXLA Repositories

The StableHLO oncall then integrates the change into the google monorepo, which
propagates the new StableHLO features to XLA, Shardy, JAX, TF, etc, including
any changes or patches that were needed to build these projects with the new
feature.

_Note: this is the only step that must be carried out by a Google team member._

### Tag the integrated StableHLO commit and bump StableHLO version numbers

This step takes care of a few things:
1. Add a tag for the integrated StableHLO version
2. Bump the patch version in [Version.h](https://github.com/openxla/stablehlo/tree/main/stablehlo/dialect/Version.h#L41)
3. Bump the 4w and 12w forward compatibility requirement versions in
[Version.cpp](https://github.com/openxla/stablehlo/blob/main/stablehlo/dialect/Version.cpp#L75)

```
Usage: ./build_tools/integrate/stablehlo_tag_and_bump_version.sh [-t <COMMIT_TO_TAG>]
-t Specify a commit to tag, must be an integrated StableHLO commit
available on https://github.com/search?q=repo%3Aopenxla%2Fxla+integrate+stablehlo&type=commits
If not specifed, will only bump the 4w and 12w versions.

$ ./build_tools/integrate/stablehlo_tag_and_bump_version.sh -t 37487a8e
```
50 changes: 50 additions & 0 deletions build_tools/integrate/llvm_bump_revision.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash
# Copyright 2024 The StableHLO Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

SCRIPT_DIR="$(dirname "$(realpath "$0")")"
GleasonK marked this conversation as resolved.
Show resolved Hide resolved
GH_ACTIONS="$SCRIPT_DIR/../github_actions"
REPO_ROOT="$SCRIPT_DIR/../.."

# Update build files
bump_to_xla_llvm_version() {
LLVM_URL="https://raw.githubusercontent.com/openxla/xla/refs/heads/main/third_party/llvm/workspace.bzl"
LLVM_REV=$(curl -s LLVM_URL | grep 'LLVM_COMMIT =' | cut -d '"' -f 2)
echo "Bumping to LLVM commit: $LLVM_REV"
echo "$LLVM_REV" > ./build_tools/llvm_version.txt
"$GH_ACTIONS/lint_llvm_commit.sh" -f .
}

apply_xla_patch() {
PATCH_URL="https://raw.githubusercontent.com/openxla/xla/refs/heads/main/third_party/stablehlo/temporary.patch"
PATCH=$(curl -s "$PATCH_URL")
if (( $(echo "$PATCH" | wc -l) < 2 )); then
echo "Patch file is empty, skipping patch."
return 0
fi

TMP_DIR=$(mktemp -d)
TMP_PATCH="$TMP_DIR/temporary.patch"
echo "Cloning patch into $TMP_PATCH"
echo "$PATCH" > "$TMP_PATCH"
cd "$REPO_ROOT" || exit 1
patch -p1 < "$TMP_PATCH"
}

bump_to_xla_llvm_version
apply_xla_patch

# Print the commit message
echo "Commit changes with message:"
echo "git add ."
echo "git commit -m \"Integrate LLVM at llvm/llvm-project@${LLVM_REV:0:12}\""
209 changes: 209 additions & 0 deletions build_tools/integrate/stablehlo_tag_and_bump_version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
#!/bin/bash
# Copyright 2024 The StableHLO Authors.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

exit_with_usage() {
echo "Usage: $0 [-t <COMMIT_TO_TAG>]"
echo " -t Specify a commit to tag, must be an integrated StableHLO commit"
echo " available on https://github.com/search?q=repo%3Aopenxla%2Fxla+integrate+stablehlo&type=commits"
echo " If not specifed, will only bump the 4w and 12w versions."
exit 1
}

## Tag old version
tag_integrated_version() {
# Must be valid commit
if ! git rev-parse "$COMMIT" > /dev/null; then
echo "Could not find commit $COMMIT."
echo "Sync with upstream and pull changes to local repo."
exit 1
fi

# Ensure proper commit
# NOTE: THIS COMMIT MUST BE INTEGRATED INTO OPENXLA/XLA BY STABLEHLO ONCALL
# Valid commit hashes here:
# https://github.com/search?q=repo%3Aopenxla%2Fxla+integrate+stablehlo&type=commits
echo "Using commit:"
git log --format=%B -n 1 "$COMMIT"
echo
read -p "Is this the correct commit? [y] " -n 1 -r
echo; echo
if [[ "$REPLY" =~ ^[Nn]$ ]]; then
echo "Exiting..."
exit 1
fi

RELEASE="${VERSION[0]}.${VERSION[1]}.${VERSION[2]}"
RELEASE_TAG="v$RELEASE"
NEW_RELEASE="${VERSION[0]}.${VERSION[1]}.$((VERSION[2]+1))"
echo "Creating tagged release $RELEASE_TAG at $COMMIT"
echo "$ git tag -a $RELEASE_TAG $COMMIT -m \"StableHLO $RELEASE_TAG\""
git tag -a "$RELEASE_TAG" "$COMMIT" -m "StableHLO $RELEASE_TAG"
echo
echo "Most recent tags:"
git tag --sort=-version:refname | head -3
echo
echo "If this is incorrect, can undo using:"
echo "$ git tag -d $RELEASE_TAG"
echo
}

bump_patch_version() {
## Bump revision
echo "Bumping revision to: $NEW_VERSION_STR"
echo "$ sed -i \"s/$OLD_VERSION_STR/$NEW_VERSION_STR/\" $VERSION_H"
sed -i "s/$OLD_VERSION_STR/$NEW_VERSION_STR/" "$VERSION_H"
echo
}

## Setup version variables as globals:
## VERSION, OLD_VERSION_STR, NEW_VERSION_STR
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
VERSION_H="$SCRIPT_DIR/../../stablehlo/dialect/Version.h"
VERSION_CPP="$SCRIPT_DIR/../../stablehlo/dialect/Version.cpp"
setup_version_vars() {
# getCurrentVersion() { Version(0, X, Y); }
VERSION_STR=$(cat "$VERSION_H" | grep getCurrentVersion -A1 | grep -o 'Version([0-9], .*)')
REGEX="Version\(([0-9]+), ([0-9]+), ([0-9]+)\)"
if [[ "$VERSION_STR" =~ "$REGEX" ]]
then
VERSION=(${BASH_REMATCH[1]} ${BASH_REMATCH[2]} ${BASH_REMATCH[3]})
OLD_VERSION_STR="${BASH_REMATCH}"
NEW_VERSION_STR="Version(${VERSION[0]}, ${VERSION[1]}, $((VERSION[2]+1)))"
else
echo "Error: Could not find current version string in Version.h" >&2
exit 1
fi
}

tag_and_bump() {
COMMIT="$1"
setup_version_vars
echo "Bumping version $OLD_VERSION_STR -> $NEW_VERSION_STR"

tag_integrated_version "$COMMIT"
GleasonK marked this conversation as resolved.
Show resolved Hide resolved
bump_patch_version

## Next steps
echo "NEXT STEPS"
echo " Push tag to upstream using:"
echo " $ git push upstream $RELEASE_TAG"
echo
echo " Commit and patch bump changes:"
echo " $ git add ."
echo " $ git commit -m \"Bump patch version after integrate $RELEASE -> $NEW_RELEASE\""
}

## Update the 4w and 12w forward compatiblility versions
# This function:
# Replaces WEEK_4 and WEEK12 versions in stablehlo/dialect/Version.cpp
# return Version(a, b, c); // WEEK_4 ANCHOR: DO NOT MODIFY
# return Version(m, n, p); // WEEK_12 ANCHOR: DO NOT MODIFY
# WEEK_4 version - The most recent git tag that was created at least 28 days ago.
# WEEK_12 version - The most recent git tag that was created at least 84 days ago.

update_forward_compat_versions() {
echo "Bumping 4w and 12w compatibility window values"

UTC_TIME=$(date -u +%s)

WEEK_4_TAG=""
WEEK_12_TAG=""

git fetch --tags upstream

# Iterate over tags finding the proper tag
while IFS= read -r line; do
# split line CSV
IFS=',' read -r tag_ts tag_v <<< "$line"
ts_diff=$(( (UTC_TIME - tag_ts) / 86400 ))

if [ -z "$WEEK_4_TAG" ] && [ "$ts_diff" -ge 28 ]; then
WEEK_4_TAG=$tag_v
fi

if [ -z "$WEEK_12_TAG" ] && [ "$ts_diff" -ge 84 ]; then
WEEK_12_TAG=$tag_v
break
fi
done < <(git for-each-ref --sort=taggerdate --format '%(taggerdate:unix),%(refname:short)' refs/tags | tail -40 | tac)

if [ -z "$WEEK_4_TAG" ] || [ -z "$WEEK_12_TAG" ]; then
echo "Error: WEEK_4 or WEEK_12 tag not found." >&2
exit 1
fi

WEEK_4_TAG=$(echo "$WEEK_4_TAG" | sed -n 's/.*v\([0-9]\+\)\.\([0-9]\+\)\.\([0-9]\+\).*/\1, \2, \3/p')
WEEK_12_TAG=$(echo "$WEEK_12_TAG" | sed -n 's/.*v\([0-9]\+\)\.\([0-9]\+\)\.\([0-9]\+\).*/\1, \2, \3/p')

if [ -z "$WEEK_4_TAG" ] || [ -z "$WEEK_12_TAG" ]; then
echo "Error: Unable to parse the WEEK_4 or WEEK_12 tag" >&2
exit 1
fi

echo "New WEEK_4 Version: $WEEK_4_TAG" >&2
echo "New WEEK_12 Version: $WEEK_12_TAG" >&2

sed -i -E \
-e "s/(return Version\()([0-9]+), ([0-9]+), ([0-9]+)(\);\s+\/\/ WEEK_4 ANCHOR: DO NOT MODIFY)/\1$WEEK_4_TAG\5/" \
-e "s/(return Version\()([0-9]+), ([0-9]+), ([0-9]+)(\);\s+\/\/ WEEK_12 ANCHOR: DO NOT MODIFY)/\1$WEEK_12_TAG\5/" \
"$VERSION_CPP"


# Checking Version.cpp values
grep "WEEK_4 ANCHOR: DO NOT MODIFY" "$VERSION_CPP" >&2
grep "WEEK_12 ANCHOR: DO NOT MODIFY" "$VERSION_CPP" >&2

if [ "$(grep -c -E "return Version\($WEEK_4_TAG\);\s+\/\/ WEEK_4 ANCHOR: DO NOT MODIFY" "$VERSION_CPP")" -ne 1 ]; then
echo "ERROR: WEEK_4 version is not correct" >&2
exit 1
fi

if [ "$(grep -c -E "return Version\($WEEK_12_TAG\);\s+\/\/ WEEK_12 ANCHOR: DO NOT MODIFY" "$VERSION_CPP")" -ne 1 ]; then
echo "ERROR: WEEK_12 version is not correct" >&2
exit 1
fi
}

# ------
# MAIN
# ------

COMMIT_TO_TAG=""
while getopts 't:' flag; do
case "${flag}" in
t) COMMIT_TO_TAG="$OPTARG" ;;
*) exit_with_usage ;;
esac
done
shift $(( OPTIND - 1 ))

## Validation
if [ $# -ne 0 ]; then
exit_with_usage
fi

# Must have upstream remote - This is needed to fetch and add tags
if ! git remote | grep upstream > /dev/null; then
echo "Missing upstream remote. Use:"
echo "$ git remote add upstream https://github.com/openxla/stablehlo.git"
exit 1
fi

update_forward_compat_versions

if [ -n "$COMMIT_TO_TAG" ]; then
tag_and_bump "$COMMIT_TO_TAG"
else
echo "No commit to tag specified, only bumping 4w and 12w values."
fi
Loading
Loading