Skip to content

Commit

Permalink
Add CI support for benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonwillard committed Dec 9, 2022
1 parent fe3e76d commit c5d0a88
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 13 deletions.
57 changes: 55 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ jobs:
- name: Install dependencies
shell: bash -l {0}
run: |
mamba install --yes -q "python~=${PYTHON_VERSION}=*_cpython" mkl numpy scipy pip mkl-service graphviz cython pytest coverage pytest-cov sympy
mamba install --yes -q "python~=${PYTHON_VERSION}=*_cpython" mkl numpy scipy pip mkl-service graphviz cython pytest coverage pytest-cov pytest-benchmark sympy
if [[ $INSTALL_NUMBA == "1" ]]; then mamba install --yes -q -c conda-forge "python~=${PYTHON_VERSION}=*_cpython" "numba>=0.55" numba-scipy; fi
mamba install --yes -q -c conda-forge "python~=${PYTHON_VERSION}=*_cpython" jax jaxlib
pip install -e ./
Expand All @@ -132,7 +132,8 @@ jobs:
if [[ $FAST_COMPILE == "1" ]]; then export AESARA_FLAGS=$AESARA_FLAGS,mode=FAST_COMPILE; fi
if [[ $FLOAT32 == "1" ]]; then export AESARA_FLAGS=$AESARA_FLAGS,floatX=float32; fi
export AESARA_FLAGS=$AESARA_FLAGS,warn__ignore_bug_before=all,on_opt_error=raise,on_shape_error=raise,gcc__cxxflags=-pipe
python -m pytest -x -r A --verbose --runslow --cov=aesara/ --cov-report=xml:coverage/coverage-${MATRIX_ID}.xml --no-cov-on-fail $PART
python -m pytest -x -r A --verbose --runslow --cov=aesara/ --cov-report=xml:coverage/coverage-${MATRIX_ID}.xml --no-cov-on-fail $PART --benchmark-skip
env:
MATRIX_ID: ${{ steps.matrix-id.outputs.id }}
MKL_THREADING_LAYER: GNU
Expand All @@ -148,6 +149,58 @@ jobs:
name: coverage
path: coverage/coverage-${{ steps.matrix-id.outputs.id }}.xml

benchmarks:
name: "Benchmarks"
needs:
- changes
- style
runs-on: ubuntu-latest
if: ${{ needs.changes.outputs.changes == 'true' && needs.style.result == 'success' }}
strategy:
fail-fast: true
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up Python 3.9
uses: conda-incubator/setup-miniconda@v2
with:
mamba-version: "*"
channels: conda-forge,defaults
channel-priority: true
python-version: 3.9
auto-update-conda: true
- name: Install dependencies
shell: bash -l {0}
run: |
mamba install --yes -q -c conda-forge "python~=${PYTHON_VERSION}=*_cpython" mkl numpy scipy pip mkl-service cython pytest "numba>=0.55" numba-scipy jax jaxlib pytest-benchmark
pip install -e ./
mamba list && pip freeze
python -c 'import aesara; print(aesara.config.__str__(print_doc=False))'
python -c 'import aesara; assert(aesara.config.blas__ldflags != "")'
env:
PYTHON_VERSION: 3.9
- name: Download previous benchmark data
uses: actions/cache@v1
with:
path: ./cache
key: ${{ runner.os }}-benchmark
- name: Run benchmarks
shell: bash -l {0}
run: |
python -m pytest --benchmark-only --benchmark-json output.json
- name: Store benchmark result
uses: benchmark-action/github-action-benchmark@v1
with:
name: Python Benchmark with pytest-benchmark
tool: 'pytest'
output-file-path: output.json
external-data-json-path: ./cache/benchmark-data.json
alert-threshold: '200%'
github-token: ${{ secrets.GITHUB_TOKEN }}
comment-on-alert: true
fail-on-alert: true

all-checks:
if: ${{ always() }}
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions environment-arm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies:
- pytest
- pytest-cov
- pytest-xdist
- pytest-benchmark
# For building docs
- sphinx>=1.3
- sphinx_rtd_theme
Expand Down
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies:
- pytest
- pytest-cov
- pytest-xdist
- pytest-benchmark
# For building docs
- sphinx>=1.3
- sphinx_rtd_theme
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ pre-commit
isort
packaging
typing_extensions
pytest-benchmark
13 changes: 2 additions & 11 deletions tests/scan/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import pickle
import shutil
import sys
import timeit
from collections import OrderedDict
from tempfile import mkdtemp

Expand Down Expand Up @@ -2179,15 +2178,13 @@ def scan_fn():
@pytest.mark.skipif(
not config.cxx, reason="G++ not available, so we need to skip this test."
)
def test_cython_performance():
def test_cython_performance(benchmark):

# This implicitly confirms that the Cython version is being used
from aesara.scan import scan_perform_ext # noqa: F401

# Python usually out-performs Aesara below 100 iterations
N = 200
n_timeit = 50

M = -1 / np.arange(1, 11).astype(config.floatX)
r = np.arange(N * 10).astype(config.floatX).reshape(N, 10)

Expand Down Expand Up @@ -2216,17 +2213,11 @@ def f_py():
# Make sure we're actually computing a `Scan`
assert any(isinstance(node.op, Scan) for node in f_cvm.maker.fgraph.apply_nodes)

cvm_res = f_cvm()
cvm_res = benchmark(f_cvm)

# Make sure the results are the same between the two implementations
assert np.allclose(cvm_res, py_res)

python_duration = timeit.timeit(lambda: f_py(), number=n_timeit)
cvm_duration = timeit.timeit(lambda: f_cvm(), number=n_timeit)
print(f"python={python_duration}, cvm={cvm_duration}")

assert cvm_duration <= python_duration


@config.change_flags(mode="FAST_COMPILE", compute_test_value="raise")
def test_compute_test_values():
Expand Down

0 comments on commit c5d0a88

Please sign in to comment.