|
| 1 | +name: Benchmarks |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + - v3 |
| 8 | + pull_request: |
| 9 | + branches: |
| 10 | + - main |
| 11 | + - v3 |
| 12 | + |
| 13 | +concurrency: |
| 14 | + group: benchmarks-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }} |
| 15 | + cancel-in-progress: true |
| 16 | + |
| 17 | +jobs: |
| 18 | + benchmarks: |
| 19 | + name: "Run benchmarks" |
| 20 | + if: github.event_name == 'push' |
| 21 | + runs-on: ubuntu-latest |
| 22 | + permissions: |
| 23 | + contents: write |
| 24 | + pages: write |
| 25 | + id-token: write |
| 26 | + steps: |
| 27 | + - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 28 | + with: |
| 29 | + fetch-depth: 0 |
| 30 | + persist-credentials: false |
| 31 | + |
| 32 | + - name: Set up Python |
| 33 | + uses: mamba-org/setup-micromamba@add3a49764cedee8ee24e82dfde87f5bc2914462 # v2.0.7 |
| 34 | + with: |
| 35 | + environment-name: pytensor-bench |
| 36 | + micromamba-version: "1.5.10-0" |
| 37 | + init-shell: bash |
| 38 | + post-cleanup: "all" |
| 39 | + |
| 40 | + - name: Install dependencies |
| 41 | + shell: micromamba-shell {0} |
| 42 | + run: | |
| 43 | + micromamba install --yes -q -c conda-forge "python=3.11" mkl numpy scipy pip mkl-service cython "numba>=0.57" jax jaxlib asv |
| 44 | + pip install -e ./ |
| 45 | + python -c 'import pytensor; print(pytensor.config.__str__(print_doc=False))' |
| 46 | + python -c 'import pytensor; assert pytensor.config.blas__ldflags != "", "Blas flags are empty"' |
| 47 | +
|
| 48 | + - name: Fetch previous results from asv-results branch |
| 49 | + shell: bash |
| 50 | + run: | |
| 51 | + git fetch origin asv-results:asv-results 2>/dev/null || true |
| 52 | + if git rev-parse --verify asv-results 2>/dev/null; then |
| 53 | + git worktree add /tmp/asv-results asv-results |
| 54 | + if [ -d /tmp/asv-results/results ]; then |
| 55 | + mkdir -p .asv/results |
| 56 | + cp -r /tmp/asv-results/results/* .asv/results/ |
| 57 | + fi |
| 58 | + git worktree remove /tmp/asv-results --force |
| 59 | + fi |
| 60 | +
|
| 61 | + - name: Configure ASV machine |
| 62 | + shell: micromamba-shell {0} |
| 63 | + run: asv machine --yes |
| 64 | + |
| 65 | + - name: Run benchmarks |
| 66 | + shell: micromamba-shell {0} |
| 67 | + run: | |
| 68 | + export PYTENSOR_FLAGS=warn__ignore_bug_before=all,on_opt_error=raise,on_shape_error=raise,gcc__cxxflags=-pipe |
| 69 | + asv run --python=same --set-commit-hash=$(git rev-parse HEAD) --show-stderr |
| 70 | +
|
| 71 | + - name: Push results to asv-results branch |
| 72 | + shell: bash |
| 73 | + run: | |
| 74 | + git config user.name "github-actions[bot]" |
| 75 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 76 | +
|
| 77 | + # Create or update the asv-results branch |
| 78 | + if git rev-parse --verify asv-results 2>/dev/null; then |
| 79 | + git worktree add /tmp/asv-results asv-results |
| 80 | + else |
| 81 | + git worktree add --orphan -b asv-results /tmp/asv-results |
| 82 | + cd /tmp/asv-results |
| 83 | + git rm -rf . 2>/dev/null || true |
| 84 | + cd - |
| 85 | + fi |
| 86 | +
|
| 87 | + mkdir -p /tmp/asv-results/results |
| 88 | + cp -r .asv/results/* /tmp/asv-results/results/ |
| 89 | + cd /tmp/asv-results |
| 90 | + git add results/ |
| 91 | + git commit -m "Update benchmark results for ${{ github.sha }}" || true |
| 92 | + git push origin asv-results |
| 93 | + cd - |
| 94 | + git worktree remove /tmp/asv-results --force |
| 95 | +
|
| 96 | + - name: Generate HTML |
| 97 | + shell: micromamba-shell {0} |
| 98 | + run: asv publish |
| 99 | + |
| 100 | + - name: Upload Pages artifact |
| 101 | + uses: actions/upload-pages-artifact@56afc609e74202658d3ffba0e8f6dda462b719fa # v3.0.1 |
| 102 | + with: |
| 103 | + path: .asv/html |
| 104 | + |
| 105 | + deploy-pages: |
| 106 | + name: "Deploy benchmark dashboard" |
| 107 | + if: github.event_name == 'push' |
| 108 | + needs: benchmarks |
| 109 | + runs-on: ubuntu-latest |
| 110 | + permissions: |
| 111 | + pages: write |
| 112 | + id-token: write |
| 113 | + environment: |
| 114 | + name: github-pages |
| 115 | + url: ${{ steps.deployment.outputs.page_url }} |
| 116 | + steps: |
| 117 | + - name: Deploy to GitHub Pages |
| 118 | + id: deployment |
| 119 | + uses: actions/deploy-pages@d6db90164ac5ed86f2b6aed7e0febac5b3c0c03e # v4.0.5 |
| 120 | + |
| 121 | + benchmarks-pr: |
| 122 | + name: "Benchmark comparison" |
| 123 | + if: github.event_name == 'pull_request' |
| 124 | + runs-on: ubuntu-latest |
| 125 | + permissions: |
| 126 | + pull-requests: write |
| 127 | + steps: |
| 128 | + - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 |
| 129 | + with: |
| 130 | + fetch-depth: 0 |
| 131 | + persist-credentials: false |
| 132 | + |
| 133 | + - name: Set up Python |
| 134 | + uses: mamba-org/setup-micromamba@add3a49764cedee8ee24e82dfde87f5bc2914462 # v2.0.7 |
| 135 | + with: |
| 136 | + environment-name: pytensor-bench |
| 137 | + micromamba-version: "1.5.10-0" |
| 138 | + init-shell: bash |
| 139 | + post-cleanup: "all" |
| 140 | + |
| 141 | + - name: Install dependencies |
| 142 | + shell: micromamba-shell {0} |
| 143 | + run: | |
| 144 | + micromamba install --yes -q -c conda-forge "python=3.11" mkl numpy scipy pip mkl-service cython "numba>=0.57" jax jaxlib asv |
| 145 | + pip install -e ./ |
| 146 | +
|
| 147 | + - name: Configure ASV machine |
| 148 | + shell: micromamba-shell {0} |
| 149 | + run: asv machine --yes |
| 150 | + |
| 151 | + - name: Run benchmark comparison |
| 152 | + id: bench |
| 153 | + shell: micromamba-shell {0} |
| 154 | + env: |
| 155 | + BASE_REF: ${{ github.base_ref }} |
| 156 | + run: | |
| 157 | + export PYTENSOR_FLAGS=warn__ignore_bug_before=all,on_opt_error=raise,on_shape_error=raise,gcc__cxxflags=-pipe |
| 158 | +
|
| 159 | + HEAD_SHA=$(git rev-parse HEAD) |
| 160 | + BASE_SHA=$(git merge-base "origin/$BASE_REF" HEAD) |
| 161 | +
|
| 162 | + # Benchmark the PR head (already installed) |
| 163 | + asv run --python=same --set-commit-hash="$HEAD_SHA" --show-stderr |
| 164 | +
|
| 165 | + # Checkout base, reinstall, and benchmark |
| 166 | + git checkout "$BASE_SHA" |
| 167 | + pip install -e ./ |
| 168 | + asv run --python=same --set-commit-hash="$BASE_SHA" --show-stderr |
| 169 | +
|
| 170 | + # Return to PR head so asv.conf.json is available for compare |
| 171 | + git checkout "$HEAD_SHA" |
| 172 | +
|
| 173 | + # Compare results |
| 174 | + asv compare "$BASE_SHA" "$HEAD_SHA" --factor 1.1 | tee bench_output.txt |
| 175 | +
|
| 176 | + - name: Post benchmark results as PR comment |
| 177 | + if: always() |
| 178 | + uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7.1.0 |
| 179 | + with: |
| 180 | + script: | |
| 181 | + const fs = require('fs'); |
| 182 | + let output = ''; |
| 183 | + try { |
| 184 | + output = fs.readFileSync('bench_output.txt', 'utf8'); |
| 185 | + } catch (e) { |
| 186 | + output = 'Benchmark comparison failed to produce results.'; |
| 187 | + } |
| 188 | + if (output.trim()) { |
| 189 | + const body = `## Benchmark comparison (main vs PR)\n\n\`\`\`\n${output}\n\`\`\`\n\nBenchmarks that changed by more than 10% are flagged.`; |
| 190 | + await github.rest.issues.createComment({ |
| 191 | + issue_number: context.issue.number, |
| 192 | + owner: context.repo.owner, |
| 193 | + repo: context.repo.repo, |
| 194 | + body: body |
| 195 | + }); |
| 196 | + } |
0 commit comments