From 27809f9bb7a6745eb05452228e6660130e84c8b8 Mon Sep 17 00:00:00 2001 From: David Hewitt <1939362+davidhewitt@users.noreply.github.com> Date: Tue, 20 Jun 2023 17:15:08 +0100 Subject: [PATCH 1/4] try pgo for benchmarks run --- .github/workflows/codspeed.yml | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/.github/workflows/codspeed.yml b/.github/workflows/codspeed.yml index 9da7c2f3b..25ace6292 100644 --- a/.github/workflows/codspeed.yml +++ b/.github/workflows/codspeed.yml @@ -41,18 +41,37 @@ jobs: if: steps.cache-py.outputs.cache-hit != 'true' - name: install rust stable + id: rust-toolchain uses: dtolnay/rust-toolchain@stable + with: + components: llvm-tools - name: cache rust uses: Swatinem/rust-cache@v2 - - name: Install pydantic-core + - name: Compile pydantic-core for profiling + # --no-default-features to avoid using mimalloc + run: | + pip install -e . --config-settings=build-args='--no-default-features --verbose' -v + python -c 'import pydantic_core; assert pydantic_core._pydantic_core.__pydantic_core_default_allocator__' + env: + CONST_RANDOM_SEED: 0 # Fix the compile time RNG seed + RUSTFLAGS: "-Cprofile-generate=${{ github.workspace }}/profdata" + + - name: Gather pgo data + run: pytest + + - name: Prepare merged pgo data + run: rustup run stable bash -c '$RUSTUP_HOME/toolchains/$RUSTUP_TOOLCHAIN/lib/rustlib/x86_64-unknown-linux-gnu/bin/llvm-profdata merge -o ${{ github.workspace }}/merged.profdata ${{ github.workspace }}/profdata' + + - name: Compile pydantic-core for benchmarking # --no-default-features to avoid using mimalloc run: | pip install -e . --config-settings=build-args='--no-default-features --verbose' -v python -c 'import pydantic_core; assert pydantic_core._pydantic_core.__pydantic_core_default_allocator__' env: CONST_RANDOM_SEED: 0 # Fix the compile time RNG seed + RUSTFLAGS: "-Cprofile-use=${{ github.workspace }}/merged.profdata" - name: Run CodSpeed benchmarks uses: CodSpeedHQ/action@v1 From ca68039140d99e1660697cdb76e8c6f2c66e65e4 Mon Sep 17 00:00:00 2001 From: David Hewitt <1939362+davidhewitt@users.noreply.github.com> Date: Wed, 28 Jun 2023 16:07:47 +0100 Subject: [PATCH 2/4] add pgo builds for release artifacts on ubuntu --- .github/workflows/ci.yml | 113 ++++++++++++++++++++++++++++++++++----- 1 file changed, 100 insertions(+), 13 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0cb72ee50..88b8cbab5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -287,10 +287,25 @@ jobs: jobs: ${{ toJSON(needs) }} allowed-failures: coverage + build-sdist: + name: build sdist + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: PyO3/maturin-action@v1 + with: + command: sdist + args: --out dist + rust-toolchain: stable + - uses: actions/upload-artifact@v3 + with: + name: pypi_files + path: dist + build: name: build on ${{ matrix.platform || matrix.os }} (${{ matrix.target }} - ${{ matrix.manylinux || 'auto' }}) # only run on push to main and on release - if: "success() && (startsWith(github.ref, 'refs/tags/') || github.ref == 'refs/heads/main' || contains(github.event.pull_request.labels.*.name, 'Full Build'))" + if: startsWith(github.ref, 'refs/tags/') || github.ref == 'refs/heads/main' || contains(github.event.pull_request.labels.*.name, 'Full Build') strategy: fail-fast: false matrix: @@ -345,6 +360,11 @@ jobs: container: messense/manylinux_2_24-cross:s390x interpreter: 3.7 3.8 3.9 3.10 3.11 3.12 exclude: + # Optimized PGO builds for manylinux follow a different matrix, maybe in future + # maturin-action can support this automatically + - os: ubuntu + target: x86_64 + manylinux: auto # Windows on arm64 only supports Python 3.11+ - os: windows target: aarch64 @@ -364,14 +384,6 @@ jobs: # generate self-schema now, so we don't have to do so inside docker in maturin build - run: python generate_self_schema.py - - name: build sdist - if: ${{ matrix.os == 'ubuntu' && matrix.target == 'x86_64' && matrix.manylinux == 'auto' }} - uses: PyO3/maturin-action@v1 - with: - command: sdist - args: --out dist - rust-toolchain: stable - - name: build wheels uses: PyO3/maturin-action@v1 with: @@ -391,8 +403,83 @@ jobs: name: pypi_files path: dist + build-pgo: + name: build pgo-optimized on ${{ matrix.platform || matrix.os }} (${{ matrix.interpreter}} - ${{ matrix.target }} - ${{ matrix.manylinux || 'auto' }}) + # only run on push to main and on release + if: startsWith(github.ref, 'refs/tags/') || github.ref == 'refs/heads/main' || contains(github.event.pull_request.labels.*.name, 'Full Build') + strategy: + fail-fast: false + matrix: + os: [ubuntu] + platform: [linux] + target: [x86_64] + manylinux: [auto] + interpreter: ["3.7", "3.8", "3.9", "3.10", "3.11", "pypy3.7", "pypy3.8", "pypy3.9"] + + runs-on: ${{ matrix.os }}-latest + steps: + - uses: actions/checkout@v3 + + - name: set up python + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.interpreter }} + architecture: ${{ matrix.python-architecture || 'x64' }} + + - name: install rust stable + id: rust-toolchain + uses: dtolnay/rust-toolchain@stable + with: + components: llvm-tools + + - run: pip install -U 'black>=22.3.0,<23' typing_extensions + + # generate self-schema now, so we don't have to do so inside docker in maturin build + - run: python generate_self_schema.py + + - name: build initial wheel + uses: PyO3/maturin-action@v1 + with: + target: ${{ matrix.target }} + manylinux: ${{ matrix.manylinux || 'auto' }} + args: > + --release + --out pgo-wheel + --interpreter ${{ matrix.interpreter }} + -- -Cprofile-generate=${{ github.workspace }}/profdata + rust-toolchain: stable + docker-options: -e CI + + - name: generate pgo data + run: | + pip install -U pip + pip install -r tests/requirements.txt + pip install pydantic-core --no-index --no-deps --find-links pgo-wheel --force-reinstall + pytest + rustup run stable bash -c '$RUSTUP_HOME/toolchains/$RUSTUP_TOOLCHAIN/lib/rustlib/x86_64-unknown-linux-gnu/bin/llvm-profdata merge -o ${{ github.workspace }}/merged.profdata ${{ github.workspace }}/profdata' + + - name: build pgo-optimized wheel + uses: PyO3/maturin-action@v1 + with: + target: ${{ matrix.target }} + manylinux: ${{ matrix.manylinux || 'auto' }} + args: > + --release + --out dist + --interpreter ${{ matrix.interpreter }} + -- -Cprofile-use=${{ github.workspace }}/merged.profdata + rust-toolchain: stable + docker-options: -e CI + + - run: ${{ matrix.ls || 'ls -lh' }} dist/ + + - uses: actions/upload-artifact@v3 + with: + name: pypi_files + path: dist + inspect-pypi-assets: - needs: [build] + needs: [build, build-sdist, build-pgo] runs-on: ubuntu-latest steps: @@ -474,7 +561,7 @@ jobs: test-builds-os: name: test build on ${{ matrix.os }} - needs: [build] + needs: [build, build-pgo] strategy: fail-fast: false @@ -502,8 +589,8 @@ jobs: - run: pytest --ignore=tests/test_docstrings.py release: - needs: [test-builds-arch, test-builds-os, check] - if: "success() && startsWith(github.ref, 'refs/tags/')" + needs: [test-builds-arch, test-builds-os, build-sdist, check] + if: success() && startsWith(github.ref, 'refs/tags/') runs-on: ubuntu-latest steps: From 9003451d80c32301b1fdeef77f91b41c38946d16 Mon Sep 17 00:00:00 2001 From: David Hewitt <1939362+davidhewitt@users.noreply.github.com> Date: Mon, 3 Jul 2023 11:05:30 +0100 Subject: [PATCH 3/4] use tests/benchmarks as PGO source --- .github/workflows/ci.yml | 2 +- .github/workflows/codspeed.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 88b8cbab5..e08f6b10d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -455,7 +455,7 @@ jobs: pip install -U pip pip install -r tests/requirements.txt pip install pydantic-core --no-index --no-deps --find-links pgo-wheel --force-reinstall - pytest + pytest tests/benchmarks rustup run stable bash -c '$RUSTUP_HOME/toolchains/$RUSTUP_TOOLCHAIN/lib/rustlib/x86_64-unknown-linux-gnu/bin/llvm-profdata merge -o ${{ github.workspace }}/merged.profdata ${{ github.workspace }}/profdata' - name: build pgo-optimized wheel diff --git a/.github/workflows/codspeed.yml b/.github/workflows/codspeed.yml index 25ace6292..fc33ab2db 100644 --- a/.github/workflows/codspeed.yml +++ b/.github/workflows/codspeed.yml @@ -59,7 +59,7 @@ jobs: RUSTFLAGS: "-Cprofile-generate=${{ github.workspace }}/profdata" - name: Gather pgo data - run: pytest + run: pytest tests/benchmarks - name: Prepare merged pgo data run: rustup run stable bash -c '$RUSTUP_HOME/toolchains/$RUSTUP_TOOLCHAIN/lib/rustlib/x86_64-unknown-linux-gnu/bin/llvm-profdata merge -o ${{ github.workspace }}/merged.profdata ${{ github.workspace }}/profdata' From 383f4978ee4f43a0e5796b161dbfea0360a315b5 Mon Sep 17 00:00:00 2001 From: David Hewitt <1939362+davidhewitt@users.noreply.github.com> Date: Mon, 3 Jul 2023 12:12:02 +0100 Subject: [PATCH 4/4] move windows x86_64 builds to PGO --- .github/workflows/ci.yml | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e08f6b10d..374a96012 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -360,11 +360,13 @@ jobs: container: messense/manylinux_2_24-cross:s390x interpreter: 3.7 3.8 3.9 3.10 3.11 3.12 exclude: - # Optimized PGO builds for manylinux follow a different matrix, maybe in future - # maturin-action can support this automatically + # Optimized PGO builds for x86_64 manylinux and windows follow a different matrix, + # maybe in future maturin-action can support this automatically - os: ubuntu target: x86_64 manylinux: auto + - os: windows + target: x86_64 # Windows on arm64 only supports Python 3.11+ - os: windows target: aarch64 @@ -410,11 +412,17 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu] - platform: [linux] + os: [ubuntu, windows] target: [x86_64] manylinux: [auto] - interpreter: ["3.7", "3.8", "3.9", "3.10", "3.11", "pypy3.7", "pypy3.8", "pypy3.9"] + interpreter: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12-dev", "pypy3.7", "pypy3.8", "pypy3.9"] + include: + - os: ubuntu + platform: linux + - os: windows + ls: dir + - interpreter: 3.12-dev + maturin-interpreter: "3.12" runs-on: ${{ matrix.os }}-latest steps: @@ -445,18 +453,25 @@ jobs: args: > --release --out pgo-wheel - --interpreter ${{ matrix.interpreter }} + --interpreter ${{ matrix.maturin-interpreter || matrix.interpreter }} -- -Cprofile-generate=${{ github.workspace }}/profdata rust-toolchain: stable docker-options: -e CI + - name: detect rust host + run: echo RUST_HOST=$(rustc -Vv | grep host | cut -d ' ' -f 2) >> "$GITHUB_ENV" + shell: bash + - name: generate pgo data run: | pip install -U pip pip install -r tests/requirements.txt pip install pydantic-core --no-index --no-deps --find-links pgo-wheel --force-reinstall pytest tests/benchmarks - rustup run stable bash -c '$RUSTUP_HOME/toolchains/$RUSTUP_TOOLCHAIN/lib/rustlib/x86_64-unknown-linux-gnu/bin/llvm-profdata merge -o ${{ github.workspace }}/merged.profdata ${{ github.workspace }}/profdata' + rustup run stable bash -c 'echo LLVM_PROFDATA=$RUSTUP_HOME/toolchains/$RUSTUP_TOOLCHAIN/lib/rustlib/${{ env.RUST_HOST }}/bin/llvm-profdata >> "$GITHUB_ENV"' + + - name: merge pgo data + run: ${{ env.LLVM_PROFDATA }} merge -o ${{ github.workspace }}/merged.profdata ${{ github.workspace }}/profdata - name: build pgo-optimized wheel uses: PyO3/maturin-action@v1 @@ -466,7 +481,7 @@ jobs: args: > --release --out dist - --interpreter ${{ matrix.interpreter }} + --interpreter ${{ matrix.maturin-interpreter || matrix.interpreter }} -- -Cprofile-use=${{ github.workspace }}/merged.profdata rust-toolchain: stable docker-options: -e CI