From 9b410afbaa6443a3e405a7541e6da9adce4d063b Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Tue, 14 Nov 2023 16:47:15 -0500 Subject: [PATCH 1/5] Build release wheels for tier 1 platforms with PGO This commit adds the infrastructure to build the wheels we publish to PyPI at release time with PGO. Profile guided optimization (PGO) is a compiler technique that uses collected data about a typical execution to inform optimizations the compiler can make about building the output binary. For more details on PGO you can refer to the Rust [1] and Clang [2] documention on the topic. To start the only data collected is from running the qiskit unittests. This should give us some broad coverage as we'll exercise everything as we do in the unittests. This also means as we grow our Rust usage in Qiskit we'll continue to cover that code in the unittests and ensure we have coverage in PGO too. However, longer term we'll potentially want to look at dedicated PGO scripts that exercise Qiskit at larger scale than what we can do in the unittests. [1] https://doc.rust-lang.org/rustc/profile-guided-optimization.html [2] https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization --- .github/workflows/wheels.yml | 34 +++++++++++++++++++++++++++++++++- pyproject.toml | 2 +- tools/build_pgo.sh | 29 +++++++++++++++++++++++++++++ tools/install_rust.sh | 2 +- 4 files changed, 64 insertions(+), 3 deletions(-) create mode 100755 tools/build_pgo.sh diff --git a/.github/workflows/wheels.yml b/.github/workflows/wheels.yml index 99a46a97b63e..1006bb4dcd5f 100644 --- a/.github/workflows/wheels.yml +++ b/.github/workflows/wheels.yml @@ -20,8 +20,40 @@ jobs: with: python-version: '3.10' - uses: dtolnay/rust-toolchain@stable + with: + components: llvm-tools-preview - name: Build wheels uses: pypa/cibuildwheel@v2.16.2 + env: + CIBW_BEFORE_BUILD: 'bash ./tools/build_pgo.sh' + CIBW_BEFORE_BUILD_WINDOWS: 'bash ./tools/build_pgo.sh && cp /tmp/pgo-data/merged.profdata ~/.' + CIBW_ENVIRONMENT: 'RUSTUP_TOOLCHAIN="stable" RUSTFLAGS="-Cprofile-use=/tmp/pgo-data/merged.profdata -Cllvm-args=-pgo-warn-missing-function"' + CIBW_ENVIRONMENT_LINUX: 'PATH="$PATH:$HOME/.cargo/bin" CARGO_NET_GIT_FETCH_WITH_CLI="true" RUSTUP_TOOLCHAIN="stable" RUSTFLAGS="-Cprofile-use=/tmp/pgo-data/merged.profdata -Cllvm-args=-pgo-warn-missing-function"' + CIBW_ENVIRONMENT_WINDOWS: 'RUSTUP_TOOLCHAIN="stable" RUSTFLAGS="-Cprofile-use=c:\\Users\\runneradmin\\merged.profdata -Cllvm-args=-pgo-warn-missing-function"' + - uses: actions/upload-artifact@v3 + with: + path: ./wheelhouse/*.whl + name: shared-wheel-builds + build_wheels_32bit: + name: Build wheels 32bit + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest] + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + name: Install Python + with: + python-version: '3.10' + - uses: dtolnay/rust-toolchain@stable + with: + components: llvm-tools-preview + - name: Build wheels + uses: pypa/cibuildwheel@v2.16.2 + env: + CIBW_SKIP: 'pp* cp36-* cp37-* *musllinux* *amd64 *x86_64' - uses: actions/upload-artifact@v3 with: path: ./wheelhouse/*.whl @@ -59,7 +91,7 @@ jobs: environment: release permissions: id-token: write - needs: ["build_wheels", "build_wheels_macos_arm"] + needs: ["build_wheels", "build_wheels_macos_arm", "build_wheels_32bit"] steps: - uses: actions/download-artifact@v3 with: diff --git a/pyproject.toml b/pyproject.toml index 04bafbf3f995..69f7d826d11b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -136,7 +136,7 @@ target-version = ['py38', 'py39', 'py310', 'py311'] [tool.cibuildwheel] manylinux-x86_64-image = "manylinux2014" manylinux-i686-image = "manylinux2014" -skip = "pp* cp36-* cp37-* *musllinux*" +skip = "pp* cp36-* cp37-* *musllinux* *win32 *i686" test-skip = "*win32 *linux_i686" test-command = "python {project}/examples/python/stochastic_swap.py" # We need to use pre-built versions of Numpy and Scipy in the tests; they have a diff --git a/tools/build_pgo.sh b/tools/build_pgo.sh new file mode 100755 index 000000000000..af413089dc5a --- /dev/null +++ b/tools/build_pgo.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +set -x + +python -c 'import sys;assert sys.platform == "win32"' +is_win=$? + +set -e +# Create venv for instrumented build and test +python -m venv build_pgo + +if [[ $is_win -eq 0 ]]; then + source build_pgo/Scripts/activate +else + source build_pgo/bin/activate +fi + +# Build with instrumentation +pip install --prefer-binary -c constraints.txt -r requirements.txt setuptools-rust wheel +RUSTFLAGS="-Cprofile-generate=/tmp/pgo-data" pip install --prefer-binary -c constraints.txt -e . +RUSTFLAGS="-Cprofile-generate=/tmp/pgo-data" python setup.py build_rust --release --inplace +pip install -c constraints.txt --prefer-binary -r requirements-dev.txt +# Run profile data generation + +QISKIT_PARALLEL=FALSE stestr run --abbreviate + +deactivate + +${HOME}/.rustup/toolchains/*x86_64*/lib/rustlib/x86_64*/bin/llvm-profdata merge -o /tmp/pgo-data/merged.profdata /tmp/pgo-data diff --git a/tools/install_rust.sh b/tools/install_rust.sh index d86416207240..9ea8a394eb85 100755 --- a/tools/install_rust.sh +++ b/tools/install_rust.sh @@ -2,5 +2,5 @@ if [ ! -d rust-installer ]; then mkdir rust-installer wget https://sh.rustup.rs -O rust-installer/rustup.sh - sh rust-installer/rustup.sh -y + sh rust-installer/rustup.sh -y -c llvm-tools-preview fi From 856dd53059a65a5f9f1f7bf643dc1b3e7a0c455b Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Tue, 9 Jan 2024 17:27:56 -0500 Subject: [PATCH 2/5] Add comment about per job overrides --- pyproject.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 69f7d826d11b..3481af2cca6a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -144,6 +144,9 @@ test-command = "python {project}/examples/python/stochastic_swap.py" # Numpy 1.22 there are no i686 wheels, so we force pip to use older ones without # restricting any dependencies that Numpy and Scipy might have. before-test = "pip install --only-binary=numpy,scipy numpy scipy" +# Some jobs locally override the before-build and environment configuration if a +# specific job override is needed. For example tier 1 platforms locally override +# see: .github/workflows/wheels.yml for the jobs where this is done environment = 'RUSTUP_TOOLCHAIN="stable"' [tool.cibuildwheel.linux] From f3ddf276ba260f546369110733f0e4509d56348c Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Tue, 9 Jan 2024 17:39:59 -0500 Subject: [PATCH 3/5] Make merged path configurable --- .github/workflows/wheels.yml | 4 ++-- tools/build_pgo.sh | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/wheels.yml b/.github/workflows/wheels.yml index 1006bb4dcd5f..e95323bcfe8a 100644 --- a/.github/workflows/wheels.yml +++ b/.github/workflows/wheels.yml @@ -25,8 +25,8 @@ jobs: - name: Build wheels uses: pypa/cibuildwheel@v2.16.2 env: - CIBW_BEFORE_BUILD: 'bash ./tools/build_pgo.sh' - CIBW_BEFORE_BUILD_WINDOWS: 'bash ./tools/build_pgo.sh && cp /tmp/pgo-data/merged.profdata ~/.' + CIBW_BEFORE_BUILD: 'bash ./tools/build_pgo.sh /tmp/pgo-data/merged.profdata' + CIBW_BEFORE_BUILD_WINDOWS: 'bash ./tools/build_pgo.sh /tmp/pgo-data/merged.profdata && cp /tmp/pgo-data/merged.profdata ~/.' CIBW_ENVIRONMENT: 'RUSTUP_TOOLCHAIN="stable" RUSTFLAGS="-Cprofile-use=/tmp/pgo-data/merged.profdata -Cllvm-args=-pgo-warn-missing-function"' CIBW_ENVIRONMENT_LINUX: 'PATH="$PATH:$HOME/.cargo/bin" CARGO_NET_GIT_FETCH_WITH_CLI="true" RUSTUP_TOOLCHAIN="stable" RUSTFLAGS="-Cprofile-use=/tmp/pgo-data/merged.profdata -Cllvm-args=-pgo-warn-missing-function"' CIBW_ENVIRONMENT_WINDOWS: 'RUSTUP_TOOLCHAIN="stable" RUSTFLAGS="-Cprofile-use=c:\\Users\\runneradmin\\merged.profdata -Cllvm-args=-pgo-warn-missing-function"' diff --git a/tools/build_pgo.sh b/tools/build_pgo.sh index af413089dc5a..3bf41bd51c4e 100755 --- a/tools/build_pgo.sh +++ b/tools/build_pgo.sh @@ -2,6 +2,8 @@ set -x +merged_path=$1 + python -c 'import sys;assert sys.platform == "win32"' is_win=$? @@ -26,4 +28,4 @@ QISKIT_PARALLEL=FALSE stestr run --abbreviate deactivate -${HOME}/.rustup/toolchains/*x86_64*/lib/rustlib/x86_64*/bin/llvm-profdata merge -o /tmp/pgo-data/merged.profdata /tmp/pgo-data +${HOME}/.rustup/toolchains/*x86_64*/lib/rustlib/x86_64*/bin/llvm-profdata merge -o $merged_path /tmp/pgo-data From b7eeb24e9b5a874966112f815c33ac2b1c57e6ca Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Wed, 10 Jan 2024 10:32:19 -0500 Subject: [PATCH 4/5] Update pyproject.toml --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 3481af2cca6a..4a5ad91fd881 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -146,6 +146,7 @@ test-command = "python {project}/examples/python/stochastic_swap.py" before-test = "pip install --only-binary=numpy,scipy numpy scipy" # Some jobs locally override the before-build and environment configuration if a # specific job override is needed. For example tier 1 platforms locally override +# the before-build and environment configuration to enable PGO, # see: .github/workflows/wheels.yml for the jobs where this is done environment = 'RUSTUP_TOOLCHAIN="stable"' From 688c791e465ace855ee02fb154a55d6290264c28 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Tue, 23 Jan 2024 13:56:20 -0500 Subject: [PATCH 5/5] Update tools/build_pgo.sh --- tools/build_pgo.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tools/build_pgo.sh b/tools/build_pgo.sh index 3bf41bd51c4e..8d0a18787398 100755 --- a/tools/build_pgo.sh +++ b/tools/build_pgo.sh @@ -18,10 +18,9 @@ else fi # Build with instrumentation -pip install --prefer-binary -c constraints.txt -r requirements.txt setuptools-rust wheel -RUSTFLAGS="-Cprofile-generate=/tmp/pgo-data" pip install --prefer-binary -c constraints.txt -e . +pip install -U -c constraints.txt setuptools-rust wheel setuptools +RUSTFLAGS="-Cprofile-generate=/tmp/pgo-data" pip install --prefer-binary -c constraints.txt -r requirements-dev.txt -e . RUSTFLAGS="-Cprofile-generate=/tmp/pgo-data" python setup.py build_rust --release --inplace -pip install -c constraints.txt --prefer-binary -r requirements-dev.txt # Run profile data generation QISKIT_PARALLEL=FALSE stestr run --abbreviate