Skip to content

Commit c096d57

Browse files
authored
ci: WIP Benchmarks (#733)
1 parent 5bf81c9 commit c096d57

16 files changed

+400
-18
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,7 @@ jobs:
5353
if: always()
5454
with:
5555
files: ./**/coverage*.xml
56-
benchmarks:
57-
runs-on: ubuntu-latest
58-
steps:
59-
- name: Checkout Repo
60-
uses: actions/checkout@v4
61-
- name: Set up Python
62-
uses: actions/setup-python@v5
63-
with:
64-
python-version: '3.10'
65-
cache: 'pip'
66-
- name: Install asv
67-
run: |
68-
pip install asv
69-
asv machine --yes
70-
- name: Run benchmarks
71-
run: |
72-
asv run --quick
56+
7357
examples:
7458
runs-on: ubuntu-latest
7559
steps:

.github/workflows/codespeed.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: codspeed-benchmarks
2+
3+
on:
4+
push:
5+
branches:
6+
#- "main" # or "master"
7+
- "benchmarks" # or "master"
8+
pull_request:
9+
# `workflow_dispatch` allows CodSpeed to trigger backtest
10+
# performance analysis in order to generate initial data.
11+
workflow_dispatch:
12+
13+
jobs:
14+
benchmarks:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
- uses: actions/setup-python@v3
19+
with:
20+
python-version: "3.11"
21+
22+
- name: Install dependencies
23+
run: pip install ".[all]"
24+
25+
- name: Run benchmarks
26+
uses: CodSpeedHQ/action@v3
27+
with:
28+
run: pytest benchmarks/ --codspeed

benchmarks/test_benchmark_coo.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import sparse
2+
3+
import numpy as np
4+
5+
6+
def test_matmul(benchmark):
7+
rng = np.random.default_rng(seed=42)
8+
x = sparse.random((40, 40), density=0.01, random_state=rng)
9+
y = sparse.random((40, 40), density=0.01, random_state=rng)
10+
11+
x @ y # Numba compilation
12+
13+
@benchmark
14+
def test_matmul():
15+
x @ y

benchmarks_original/__init__.py

Whitespace-only changes.
File renamed without changes.
File renamed without changes.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import importlib
2+
import operator
3+
import os
4+
5+
import sparse
6+
7+
from utils import benchmark
8+
9+
import numpy as np
10+
import scipy.sparse as sps
11+
12+
LEN = 10000
13+
DENSITY = 0.001
14+
ITERS = 3
15+
rng = np.random.default_rng(0)
16+
17+
18+
if __name__ == "__main__":
19+
print("Elementwise Example:\n")
20+
21+
for func_name in ["multiply", "add", "greater_equal"]:
22+
print(f"{func_name} benchmark:\n")
23+
24+
s1_sps = sps.random(LEN, LEN, format="csr", density=DENSITY, random_state=rng) * 10
25+
s1_sps.sum_duplicates()
26+
s2_sps = sps.random(LEN, LEN, format="csr", density=DENSITY, random_state=rng) * 10
27+
s2_sps.sum_duplicates()
28+
29+
# ======= Finch =======
30+
os.environ[sparse._ENV_VAR_NAME] = "Finch"
31+
importlib.reload(sparse)
32+
33+
s1 = sparse.asarray(s1_sps.asformat("csc"), format="csc")
34+
s2 = sparse.asarray(s2_sps.asformat("csc"), format="csc")
35+
36+
func = getattr(sparse, func_name)
37+
38+
# Compile & Benchmark
39+
result_finch = benchmark(func, args=[s1, s2], info="Finch", iters=ITERS)
40+
41+
# ======= Numba =======
42+
os.environ[sparse._ENV_VAR_NAME] = "Numba"
43+
importlib.reload(sparse)
44+
45+
s1 = sparse.asarray(s1_sps)
46+
s2 = sparse.asarray(s2_sps)
47+
48+
func = getattr(sparse, func_name)
49+
50+
# Compile & Benchmark
51+
result_numba = benchmark(func, args=[s1, s2], info="Numba", iters=ITERS)
52+
53+
# ======= SciPy =======
54+
s1 = s1_sps
55+
s2 = s2_sps
56+
57+
if func_name == "multiply":
58+
func, args = s1.multiply, [s2]
59+
elif func_name == "add":
60+
func, args = operator.add, [s1, s2]
61+
elif func_name == "greater_equal":
62+
func, args = operator.ge, [s1, s2]
63+
64+
# Compile & Benchmark
65+
result_scipy = benchmark(func, args=args, info="SciPy", iters=ITERS)
66+
67+
np.testing.assert_allclose(result_numba.todense(), result_scipy.toarray())
68+
np.testing.assert_allclose(result_finch.todense(), result_numba.todense())
69+
np.testing.assert_allclose(result_finch.todense(), result_scipy.toarray())

benchmarks_original/matmul_example.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import importlib
2+
import os
3+
4+
import sparse
5+
6+
from utils import benchmark
7+
8+
import numpy as np
9+
import scipy.sparse as sps
10+
11+
LEN = 100000
12+
DENSITY = 0.00001
13+
ITERS = 3
14+
rng = np.random.default_rng(0)
15+
16+
17+
if __name__ == "__main__":
18+
print("Matmul Example:\n")
19+
20+
a_sps = sps.random(LEN, LEN - 10, format="csr", density=DENSITY, random_state=rng) * 10
21+
a_sps.sum_duplicates()
22+
b_sps = sps.random(LEN - 10, LEN, format="csr", density=DENSITY, random_state=rng) * 10
23+
b_sps.sum_duplicates()
24+
25+
# ======= Finch =======
26+
os.environ[sparse._ENV_VAR_NAME] = "Finch"
27+
importlib.reload(sparse)
28+
29+
a = sparse.asarray(a_sps)
30+
b = sparse.asarray(b_sps)
31+
32+
@sparse.compiled
33+
def sddmm_finch(a, b):
34+
return a @ b
35+
36+
# Compile & Benchmark
37+
result_finch = benchmark(sddmm_finch, args=[a, b], info="Finch", iters=ITERS)
38+
39+
# ======= Numba =======
40+
os.environ[sparse._ENV_VAR_NAME] = "Numba"
41+
importlib.reload(sparse)
42+
43+
a = sparse.asarray(a_sps)
44+
b = sparse.asarray(b_sps)
45+
46+
def sddmm_numba(a, b):
47+
return a @ b
48+
49+
# Compile & Benchmark
50+
result_numba = benchmark(sddmm_numba, args=[a, b], info="Numba", iters=ITERS)
51+
52+
# ======= SciPy =======
53+
def sddmm_scipy(a, b):
54+
return a @ b
55+
56+
a = a_sps
57+
b = b_sps
58+
59+
# Compile & Benchmark
60+
result_scipy = benchmark(sddmm_scipy, args=[a, b], info="SciPy", iters=ITERS)
61+
62+
# np.testing.assert_allclose(result_numba.todense(), result_scipy.toarray())
63+
# np.testing.assert_allclose(result_finch.todense(), result_numba.todense())
64+
# np.testing.assert_allclose(result_finch.todense(), result_scipy.toarray())

0 commit comments

Comments
 (0)