Skip to content

Commit

Permalink
Add oneshot release snapshot for test/ondemand (llvm#768)
Browse files Browse the repository at this point in the history
* Add oneshot release snapshot for test/ondemand

Add some build scripts to test new release flow based on IREE.
Wont affect current builds, once this works well we can plumb it
in.

Build with manylinux docker

* Fixes a few issues found when debugging powderluv's setup.

* It is optional to link against Python3_LIBRARIES. Check that and don't do it if they don't exist for this config.
* Clean and auditwheel need to operate on sanitized package names. So "torch_mlir" vs "torch-mlir".
* Adds a pyproject.toml file that pins the build dependencies needed to detect both Torch and Python (the MLIR Python build was failing to detect because Numpy wasn't in the pip venv).
* Commented out auditwheel: These wheels are not PyPi compliant since they weak link to libtorch at runtime. However, they should be fine to deploy to users.
* Adds the --extra-index-url to the pip wheel command, allowing PyTorch to be found.
* Hack setup.py to remove the _mlir_libs dir before building. This keeps back-to-back versions from accumulating in the wheels for subsequent versions. IREE has a more principled way of doing this, but what I have here should work.

Co-authored-by: Stella Laurenzo <stellaraccident@gmail.com>
  • Loading branch information
powderluv and stellaraccident authored Apr 21, 2022
1 parent 33c9d25 commit cc3a4a5
Show file tree
Hide file tree
Showing 10 changed files with 442 additions and 5 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/buildManylinux.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Manylinux Build

on:
push:
branches:
- main
pull_request:
workflow_dispatch:
inputs:
release_id:
description: 'Release id to upload artifacts to'
default: ''
python_package_version:
description: 'Version to use for creating the Python package'
default: ''

jobs:
build:
name: Manylinux Build
runs-on: ubuntu-latest
steps:
- name: Get torch-mlir
uses: actions/checkout@v2
with:
submodules: 'true'
- uses: ./.github/actions/setup-build
with:
cache-suffix: ''
- name: Build Python wheels and smoke test.
run: |
cd $GITHUB_WORKSPACE
python -m pip install wheel
./build_tools/python_deploy/build_linux_packages.sh
# If we were given a release_id, then upload the package we just built
# to the github releases page.
- name: Upload Release Assets (if requested)
if: github.event.inputs.release_id != ''
id: upload-release-assets
uses: dwenegar/upload-release-assets@v1
env:
GITHUB_TOKEN: ${{ secrets.WORKFLOW_INVOCATION_TOKEN }}
with:
release_id: ${{ github.event.inputs.release_id }}
assets_path: ./wheelhouse/*.whl
# Publishing is necessary to make the release visible to `pip`
# on the github releases page.
- name: Publish Release (if requested)
if: github.event.inputs.release_id != ''
id: publish_release
uses: eregon/publish-release@v1
env:
GITHUB_TOKEN: ${{ secrets.WORKFLOW_INVOCATION_TOKEN }}
with:
release_id: ${{ github.event.inputs.release_id }}
64 changes: 64 additions & 0 deletions .github/workflows/oneshotSnapshotPackage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Release oneshot snapshot package

on:
workflow_dispatch:

jobs:
release_snapshot_package:
name: "Tag snapshot release"
runs-on: ubuntu-latest
# Don't run this in everyone's forks.
if: github.repository == 'llvm/torch-mlir'
steps:
- name: Checking out repository
uses: actions/checkout@v2
with:
token: ${{ secrets.WORKFLOW_INVOCATION_TOKEN }}

- name: Compute version
run: |
git fetch --depth=1 origin +refs/tags/*:refs/tags/*
package_version="$(printf '%(%Y%m%d)T.${{ github.run_number }}')"
tag_name="snapshot-${package_version}"
echo "package_version=${package_version}" >> $GITHUB_ENV
echo "tag_name=${tag_name}" >> $GITHUB_ENV
- name: Updating snapshot tag
run: |
git tag "${tag_name}"
- name: Pushing changes
uses: ad-m/github-push-action@v0.6.0
with:
github_token: ${{ secrets.WORKFLOW_INVOCATION_TOKEN }}
branch: main
tags: true

- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.WORKFLOW_INVOCATION_TOKEN }}
with:
tag_name: ${{ env.tag_name }}
release_name: torch-mlir snapshot ${{ env.tag_name }}
body: |
Automatic snapshot release of torch-mlir.
draft: true
prerelease: false

- name: "Invoke workflow :: Build and Test"
uses: benc-uk/workflow-dispatch@v1
with:
workflow: Build and Test
token: ${{ secrets.WORKFLOW_INVOCATION_TOKEN }}
ref: "${{ env.tag_name }}"
inputs: '{"release_id": "", "python_package_version": "${{ env.package_version }}"}'

- name: "Invoke workflow :: Manylinux Build"
uses: benc-uk/workflow-dispatch@v1
with:
workflow: Manylinux Build
token: ${{ secrets.WORKFLOW_INVOCATION_TOKEN }}
ref: "${{ env.tag_name }}"
inputs: '{"release_id": "${{ steps.create_release.outputs.id }}", "python_package_version": "${{ env.package_version }}"}'
1 change: 1 addition & 0 deletions build_tools/python_deploy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
manylinux2014-x64
124 changes: 124 additions & 0 deletions build_tools/python_deploy/build_linux_packages.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#!/bin/bash
# Copyright 2022 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

# build_linux_packages.sh
# One stop build of IREE Python packages for Linux. The Linux build is
# complicated because it has to be done via a docker container that has
# an LTS glibc version, all Python packages and other deps.
# This script handles all of those details.
#
# Usage:
# Build everything (all packages, all python versions):
# ./build_tools/python_deploy/build_linux_packages.sh
#
# Build specific Python versions and packages to custom directory:
# python_versions="cp38-cp38 cp39-cp39" \
# packages="torch-mlir" \
# output_dir="/tmp/wheelhouse" \
# ./build_tools/python_deploy/build_linux_packages.sh
#
# Valid Python versions match a subdirectory under /opt/python in the docker
# image. Typically:
# cp37-cp37m cp38-cp38 cp39-cp39 cp310-cp310
#
# Valid packages:
# torch-mlir
#
# Note that this script is meant to be run on CI and it will pollute both the
# output directory and in-tree build/ directories (under runtime/ and
# iree/compiler/) with docker created, root owned builds. Sorry - there is
# no good way around it.
#
# It can be run on a workstation but recommend using a git worktree dedicated
# to packaging to avoid stomping on development artifacts.
set -eu -o errtrace

this_dir="$(cd $(dirname $0) && pwd)"
script_name="$(basename $0)"
repo_root="$(cd $this_dir/../../ && pwd)"
script_name="$(basename $0)"
manylinux_docker_image="${manylinux_docker_image:-stellaraccident/manylinux2014_x86_64-bazel-5.1.0:latest}"
python_versions="${python_versions:-cp37-cp37m cp38-cp38 cp39-cp39 cp310-cp310}"
output_dir="${output_dir:-${this_dir}/wheelhouse}"
packages="${packages:-torch-mlir}"

function run_on_host() {
echo "Running on host"
echo "Launching docker image ${manylinux_docker_image}"
echo "Outputting to ${output_dir}"
rm -rf "${output_dir}"
mkdir -p "${output_dir}"
docker run --rm \
-v "${repo_root}:/main_checkout/torch-mlir" \
-v "${output_dir}:/wheelhouse" \
-e __MANYLINUX_BUILD_WHEELS_IN_DOCKER=1 \
-e "python_versions=${python_versions}" \
-e "packages=${packages}" \
${manylinux_docker_image} \
-- bash /main_checkout/torch-mlir/build_tools/python_deploy/build_linux_packages.sh
}

function run_in_docker() {
echo "Running in docker"
echo "Using python versions: ${python_versions}"

local orig_path="$PATH"

# Build phase.
for package in $packages; do
echo "******************** BUILDING PACKAGE ${package} ********************"
for python_version in $python_versions; do
python_dir="/opt/python/$python_version"
if ! [ -x "$python_dir/bin/python" ]; then
echo "ERROR: Could not find python: $python_dir (skipping)"
continue
fi
export PATH=$python_dir/bin:$orig_path
echo ":::: Python version $(python --version)"
case "$package" in
torch-mlir)
clean_wheels torch_mlir $python_version
build_torch_mlir
#run_audit_wheel torch_mlir $python_version
;;
*)
echo "Unrecognized package '$package'"
exit 1
;;
esac
done
done
}

function build_torch_mlir() {
CMAKE_GENERATOR=Ninja \
python -m pip wheel -v -w /wheelhouse /main_checkout/torch-mlir/ \
--extra-index-url https://download.pytorch.org/whl/nightly/cpu
}

function run_audit_wheel() {
local wheel_basename="$1"
local python_version="$2"
generic_wheel="/wheelhouse/${wheel_basename}-*-${python_version}-linux_x86_64.whl"
echo ":::: Auditwheel $generic_wheel"
auditwheel repair -w /wheelhouse $generic_wheel
rm $generic_wheel
}

function clean_wheels() {
local wheel_basename="$1"
local python_version="$2"
echo ":::: Clean wheels $wheel_basename $python_version"
rm -f /wheelhouse/${wheel_basename}-*-${python_version}-*.whl
}

# Trampoline to the docker container if running on the host.
if [ -z "${__MANYLINUX_BUILD_WHEELS_IN_DOCKER-}" ]; then
run_on_host "$@"
else
run_in_docker "$@"
fi
77 changes: 77 additions & 0 deletions build_tools/python_deploy/build_macos_packages.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/bash
# Copyright 2022 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

# build_macos_packages.sh
# One stop build of IREE Python packages for MacOS. This presumes that
# dependencies are installed from install_macos_deps.sh. This will build
# for a list of Python versions synchronized with that script and corresponding
# with directory names under:
# /Library/Frameworks/Python.framework/Versions
#
# MacOS convention is to refer to this as major.minor (i.e. "3.9", "3.10").
# Valid packages:
# torch-mlir

set -eu -o errtrace

this_dir="$(cd $(dirname $0) && pwd)"
repo_root="$(cd $this_dir/../../ && pwd)"
python_versions="${python_versions:3.9 3.10}"
output_dir="${output_dir:-${this_dir}/wheelhouse}"
packages="${packages:-torch-mlir}"

# Note that this typically is selected to match the version that the official
# Python distributed is built at.
#export MACOSX_DEPLOYMENT_TARGET=11.0
#export CMAKE_OSX_ARCHITECTURES="arm64;x86_64"

function run() {
echo "Using python versions: ${python_versions}"

local orig_path="$PATH"

# Build phase.
for package in $packages; do
echo "******************** BUILDING PACKAGE ${package} ********************"
for python_version in $python_versions; do
python_dir="/Library/Frameworks/Python.framework/Versions/$python_version"
if ! [ -x "$python_dir/bin/python3" ]; then
echo "ERROR: Could not find python3: $python_dir (skipping)"
continue
fi
export PATH=$python_dir/bin:$orig_path
echo ":::: Python version $(python3 --version)"
case "$package" in
torch-mlir)
clean_wheels torch-mlir $python_version
build_torch_mlir
;;
*)
echo "Unrecognized package '$package'"
exit 1
;;
esac
done
done
}

function build_torch_mlir() {
python -m pip install -r /main_checkout/torch-mlir/requirements.txt
CMAKE_GENERATOR=Ninja \
MACOSX_DEPLOYMENT_TARGET=11.0 \
CMAKE_OSX_ARCHITECTURES="arm64;x86_64" \
python -m pip wheel -v -w /wheelhouse /main_checkout/torch-mlir/
}

function clean_wheels() {
local wheel_basename="$1"
local python_version="$2"
echo ":::: Clean wheels $wheel_basename $python_version"
rm -f /wheelhouse/${wheel_basename}-*-${python_version}-*.whl
}

run
61 changes: 61 additions & 0 deletions build_tools/python_deploy/install_macos_deps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/zsh
# Copyright 2022 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

# Installs dependencies on MacOS necessary to build IREE.
# Additional dependencies (i.e MoltenVK) may be needed to use all functionality.
#
# Usage:
# sudo install_macos_deps.sh

set -e -o pipefail

if [[ "$(whoami)" != "root" ]]; then
echo "ERROR: Must setup deps as root"
exit 1
fi

PYTHON_INSTALLER_URLS=(
"https://www.python.org/ftp/python/3.10.4/python-3.10.4-macos11.pkg"
"https://www.python.org/ftp/python/3.9.12/python-3.9.12-macos11.pkg"
)

PYTHON_SPECS=(
3.10@https://www.python.org/ftp/python/3.10.4/python-3.10.4-macos11.pkg
3.9@https://www.python.org/ftp/python/3.9.12/python-3.9.12-macos11.pkg
)

for python_spec in $PYTHON_SPECS; do
python_version="${python_spec%%@*}"
url="${python_spec##*@}"
echo "-- Installing Python $python_version from $url"
python_path="/Library/Frameworks/Python.framework/Versions/$python_version"
python_exe="$python_path/bin/python3"

# Install Python.
if ! [ -x "$python_exe" ]; then
package_basename="$(basename $url)"
download_path="/tmp/torch_mlir_python_install/$package_basename"
mkdir -p "$(dirname $download_path)"
echo "Downloading $url -> $download_path"
curl $url -o "$download_path"

echo "Installing $download_path"
installer -pkg "$download_path" -target /
else
echo ":: Python version already installed. Not reinstalling."
fi

echo ":: Python version $python_version installed:"
$python_exe --version
$python_exe -m pip --version

echo ":: Installing system pip packages"
$python_exe -m pip install --upgrade pip
$python_exe -m pip install --upgrade delocate
done

echo "*** All done ***"
Loading

0 comments on commit cc3a4a5

Please sign in to comment.