Skip to content

Commit bd8ef0c

Browse files
tupuirgommerstylerjereddy
authored
ENH: add machinery to support Array API (scipy#18668)
See api-dev doc updates in this commit for details. Co-authored-by: Ralf Gommers <[email protected]> Co-authored-by: Tyler Reddy <[email protected]>
1 parent 2bf959a commit bd8ef0c

23 files changed

+1607
-576
lines changed

.github/workflows/array_api.yml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
name: Array API
2+
3+
on:
4+
push:
5+
branches:
6+
- maintenance/**
7+
pull_request:
8+
branches:
9+
- main
10+
- maintenance/**
11+
12+
permissions:
13+
contents: read # to fetch code (actions/checkout)
14+
15+
env:
16+
CCACHE_DIR: "${{ github.workspace }}/.ccache"
17+
INSTALLDIR: "build-install"
18+
19+
concurrency:
20+
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
21+
cancel-in-progress: true
22+
23+
jobs:
24+
pytorch_cpu:
25+
name: Linux PyTorch CPU
26+
if: "github.repository == 'scipy/scipy' || github.repository == ''"
27+
runs-on: ubuntu-22.04
28+
strategy:
29+
matrix:
30+
python-version: ['3.11']
31+
maintenance-branch:
32+
- ${{ contains(github.ref, 'maintenance/') || contains(github.base_ref, 'maintenance/') }}
33+
exclude:
34+
- maintenance-branch: true
35+
36+
steps:
37+
- uses: actions/checkout@v3
38+
with:
39+
submodules: recursive
40+
41+
- name: Setup Python
42+
uses: actions/setup-python@v4
43+
with:
44+
python-version: ${{ matrix.python-version }}
45+
cache: 'pip' # not using a path to also cache pytorch
46+
47+
- name: Install Ubuntu dependencies
48+
run: |
49+
sudo apt-get update
50+
sudo apt-get install -y libopenblas-dev libatlas-base-dev liblapack-dev gfortran libgmp-dev libmpfr-dev libsuitesparse-dev ccache libmpc-dev
51+
52+
- name: Install Python packages
53+
run: |
54+
python -m pip install numpy cython pytest pytest-xdist pytest-timeout pybind11 mpmath gmpy2 pythran ninja meson click rich-click doit pydevtool pooch
55+
56+
- name: Install PyTorch CPU
57+
run: |
58+
python -m pip install "torch<2.1" --index-url https://download.pytorch.org/whl/cpu
59+
60+
- name: Prepare compiler cache
61+
id: prep-ccache
62+
shell: bash
63+
run: |
64+
mkdir -p "${CCACHE_DIR}"
65+
echo "dir=$CCACHE_DIR" >> $GITHUB_OUTPUT
66+
NOW=$(date -u +"%F-%T")
67+
echo "timestamp=${NOW}" >> $GITHUB_OUTPUT
68+
69+
- name: Setup compiler cache
70+
uses: actions/cache@v3
71+
id: cache-ccache
72+
with:
73+
path: ${{ steps.prep-ccache.outputs.dir }}
74+
key: ${{ github.workflow }}-${{ matrix.python-version }}-ccache-linux-${{ steps.prep-ccache.outputs.timestamp }}
75+
restore-keys: |
76+
${{ github.workflow }}-${{ matrix.python-version }}-ccache-linux-
77+
78+
- name: Setup build and install scipy
79+
run: |
80+
python dev.py build
81+
82+
- name: Test SciPy
83+
run: |
84+
export OMP_NUM_THREADS=2
85+
export SCIPY_USE_PROPACK=1
86+
# expand as new modules are added
87+
# --array-api-backend == -b
88+
python dev.py --no-build test -b pytorch -b numpy -b numpy.array_api -s cluster -- --durations 10 --timeout=60
89+
python dev.py --no-build test --array-api-backend all --tests scipy/_lib/tests/test_array_api.py

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@
1717
path = scipy/_lib/boost_math
1818
url = https://github.com/boostorg/math.git
1919
shallow = true
20+
[submodule "scipy/_lib/array_api_compat"]
21+
path = scipy/_lib/array_api_compat
22+
url = https://github.com/data-apis/array-api-compat.git

LICENSES_bundled.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,8 @@ Files: scipy/stats/_rcont/[logfactorial.h,logfactorial.c]
256256
License 3-Clause BSD
257257
For details, see header inside scipy/stats/_rcont/logfactorial.h
258258
and scipy/stats/_rcont/logfactorial.c
259+
260+
Name: array-api-compat
261+
Files: scipy/_lib/array-api-compat/*
262+
License: MIT
263+
For details, see scipy/_lib/array-api-compat/LICENCE

benchmarks/benchmarks/cluster.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def time_kmeans2(self, k, init):
5050

5151

5252
class VQ(Benchmark):
53-
params = [[2, 10, 50], ['float32', 'float64', 'float128']]
53+
params = [[2, 10, 50], ['float32', 'float64']]
5454
param_names = ['k', 'dtype']
5555

5656
def __init__(self):

dev.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ class Test(Task):
685685
$ python dev.py test -t scipy.optimize.tests.test_minimize_constrained
686686
$ python dev.py test -s cluster -m full --durations 20
687687
$ python dev.py test -s stats -- --tb=line # `--` passes next args to pytest
688+
$ python dev.py test -b numpy -b pytorch -s cluster
688689
```
689690
""" # noqa: E501
690691
ctx = CONTEXT
@@ -716,6 +717,13 @@ class Test(Task):
716717
['--parallel', '-j'], default=1, metavar='N_JOBS',
717718
help="Number of parallel jobs for testing"
718719
)
720+
array_api_backend = Option(
721+
['--array-api-backend', '-b'], default=None, metavar='ARRAY_BACKEND',
722+
multiple=True,
723+
help=(
724+
"Array API backend ('all', 'numpy', 'pytorch', 'cupy', 'numpy.array_api')."
725+
)
726+
)
719727
# Argument can't have `help=`; used to consume all of `-- arg1 arg2 arg3`
720728
pytest_args = Argument(
721729
['pytest_args'], nargs=-1, metavar='PYTEST-ARGS', required=False
@@ -756,6 +764,9 @@ def scipy_tests(cls, args, pytest_args):
756764
else:
757765
tests = None
758766

767+
if len(args.array_api_backend) != 0:
768+
os.environ['SCIPY_ARRAY_API'] = json.dumps(list(args.array_api_backend))
769+
759770
runner, version, mod_path = get_test_runner(PROJECT_MODULE)
760771
# FIXME: changing CWD is not a good practice
761772
with working_dir(dirs.site):

doc/source/dev/api-dev/api-dev-toc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ SciPy API Development Guide
99

1010
nan_policy
1111
special_ufuncs
12+
array_api

0 commit comments

Comments
 (0)