From 2b25d4e13a1cf88741e205eaed1fb6349e0bd4ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Tue, 10 Sep 2024 16:20:57 +0200 Subject: [PATCH 1/3] wip --- .github/workflows/codspeed.yml | 7 ++++++- benchmarks/conftest.py | 17 ++++++++++++++++- benchmarks/test_benchmark_coo.py | 17 ++++++++++++----- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/.github/workflows/codspeed.yml b/.github/workflows/codspeed.yml index fed68f03..4f0aee45 100644 --- a/.github/workflows/codspeed.yml +++ b/.github/workflows/codspeed.yml @@ -25,4 +25,9 @@ jobs: - name: Run benchmarks uses: CodSpeedHQ/action@v3 with: - run: pytest benchmarks/ --codspeed + run: SPARSE_BACKEND=Numba pytest benchmarks/ --codspeed + + - name: Run benchmarks + uses: CodSpeedHQ/action@v3 + with: + run: SPARSE_BACKEND=Finch pytest benchmarks/ --codspeed diff --git a/benchmarks/conftest.py b/benchmarks/conftest.py index b80d9b88..f1d8d3bc 100644 --- a/benchmarks/conftest.py +++ b/benchmarks/conftest.py @@ -1,11 +1,26 @@ import pytest - +import os +import sparse @pytest.fixture def seed(scope="session"): return 42 +def get_backend_id(param): + backend = param + return f"{backend=}" + +@pytest.fixture(params=[sparse._BACKEND], autouse=True, ids=get_backend_id) +def backend(request): + + return request.param + + +@pytest.fixture +def min_size(scope="session"): + return 100 + @pytest.fixture def max_size(scope="session"): return 2**26 diff --git a/benchmarks/test_benchmark_coo.py b/benchmarks/test_benchmark_coo.py index 108ac075..80112f46 100644 --- a/benchmarks/test_benchmark_coo.py +++ b/benchmarks/test_benchmark_coo.py @@ -13,17 +13,21 @@ def format_id(format): return f"{format=}" - @pytest.mark.parametrize("format", ["coo", "gcxs"]) -def test_matmul(benchmark, sides, format, seed, max_size, ids=format_id): +def test_matmul(benchmark, sides, seed, format, backend, min_size, max_size, ids=format_id): + #if backend == sparse._BackendType.Finch: + # pytest.skip() + m, n, p = sides - - if m * n >= max_size or n * p >= max_size: + + if m * n >= max_size or n * p >= max_size or m * n <= min_size or n * p <= min_size: pytest.skip() - + rng = np.random.default_rng(seed=seed) x = sparse.random((m, n), density=DENSITY, format=format, random_state=rng) y = sparse.random((n, p), density=DENSITY, format=format, random_state=rng) + + if hasattr(sparse, "compiled"): operator.matmul = sparse.compiled(operator.matmul) x @ y # Numba compilation @@ -52,6 +56,9 @@ def elemwise_args(request, seed, max_size): @pytest.mark.parametrize("f", [operator.add, operator.mul]) def test_elemwise(benchmark, f, elemwise_args): x, y = elemwise_args + + if hasattr(sparse, "compiled"): f = sparse.compiled(f) + f(x, y) @benchmark From ab2108274d02f52cc34cd8dbb8cba26af1c31418 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 10 Sep 2024 14:32:36 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- benchmarks/conftest.py | 10 ++++++---- benchmarks/test_benchmark_coo.py | 19 +++++++++++-------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/benchmarks/conftest.py b/benchmarks/conftest.py index f1d8d3bc..64e3c185 100644 --- a/benchmarks/conftest.py +++ b/benchmarks/conftest.py @@ -1,7 +1,8 @@ -import pytest -import os import sparse +import pytest + + @pytest.fixture def seed(scope="session"): return 42 @@ -11,16 +12,17 @@ def get_backend_id(param): backend = param return f"{backend=}" + @pytest.fixture(params=[sparse._BACKEND], autouse=True, ids=get_backend_id) def backend(request): - return request.param - + @pytest.fixture def min_size(scope="session"): return 100 + @pytest.fixture def max_size(scope="session"): return 2**26 diff --git a/benchmarks/test_benchmark_coo.py b/benchmarks/test_benchmark_coo.py index 80112f46..cc90d5ac 100644 --- a/benchmarks/test_benchmark_coo.py +++ b/benchmarks/test_benchmark_coo.py @@ -13,21 +13,23 @@ def format_id(format): return f"{format=}" + @pytest.mark.parametrize("format", ["coo", "gcxs"]) def test_matmul(benchmark, sides, seed, format, backend, min_size, max_size, ids=format_id): - #if backend == sparse._BackendType.Finch: + # if backend == sparse._BackendType.Finch: # pytest.skip() - + m, n, p = sides - + if m * n >= max_size or n * p >= max_size or m * n <= min_size or n * p <= min_size: pytest.skip() - + rng = np.random.default_rng(seed=seed) x = sparse.random((m, n), density=DENSITY, format=format, random_state=rng) y = sparse.random((n, p), density=DENSITY, format=format, random_state=rng) - - if hasattr(sparse, "compiled"): operator.matmul = sparse.compiled(operator.matmul) + + if hasattr(sparse, "compiled"): + operator.matmul = sparse.compiled(operator.matmul) x @ y # Numba compilation @@ -57,8 +59,9 @@ def elemwise_args(request, seed, max_size): def test_elemwise(benchmark, f, elemwise_args): x, y = elemwise_args - if hasattr(sparse, "compiled"): f = sparse.compiled(f) - + if hasattr(sparse, "compiled"): + f = sparse.compiled(f) + f(x, y) @benchmark From e6eed2ed34dbacabbd0d8d893dbb6dd3b1d7f157 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Mon, 16 Sep 2024 15:22:15 +0200 Subject: [PATCH 3/3] Correcting conftest and adding Finch compilation --- benchmarks/conftest.py | 4 ++-- benchmarks/test_benchmark_coo.py | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/benchmarks/conftest.py b/benchmarks/conftest.py index f1d8d3bc..5814bf6b 100644 --- a/benchmarks/conftest.py +++ b/benchmarks/conftest.py @@ -11,7 +11,7 @@ def get_backend_id(param): backend = param return f"{backend=}" -@pytest.fixture(params=[sparse._BACKEND], autouse=True, ids=get_backend_id) +@pytest.fixture(params=[sparse._BACKEND.value], autouse=True, ids=get_backend_id) def backend(request): return request.param @@ -19,7 +19,7 @@ def backend(request): @pytest.fixture def min_size(scope="session"): - return 100 + return 50 @pytest.fixture def max_size(scope="session"): diff --git a/benchmarks/test_benchmark_coo.py b/benchmarks/test_benchmark_coo.py index 80112f46..770f3002 100644 --- a/benchmarks/test_benchmark_coo.py +++ b/benchmarks/test_benchmark_coo.py @@ -15,8 +15,6 @@ def format_id(format): @pytest.mark.parametrize("format", ["coo", "gcxs"]) def test_matmul(benchmark, sides, seed, format, backend, min_size, max_size, ids=format_id): - #if backend == sparse._BackendType.Finch: - # pytest.skip() m, n, p = sides @@ -54,7 +52,7 @@ def elemwise_args(request, seed, max_size): @pytest.mark.parametrize("f", [operator.add, operator.mul]) -def test_elemwise(benchmark, f, elemwise_args): +def test_elemwise(benchmark, f, elemwise_args, backend): x, y = elemwise_args if hasattr(sparse, "compiled"): f = sparse.compiled(f) @@ -85,6 +83,9 @@ def elemwise_broadcast_args(request, seed, max_size): @pytest.mark.parametrize("f", [operator.add, operator.mul]) def test_elemwise_broadcast(benchmark, f, elemwise_broadcast_args): x, y = elemwise_broadcast_args + + if hasattr(sparse, "compiled"): f = sparse.compiled(f) + f(x, y) @benchmark @@ -108,6 +109,8 @@ def test_index_scalar(benchmark, indexing_args): side = x.shape[0] rank = x.ndim + if hasattr(sparse, "compiled"): operator.getitem = sparse.compiled(operator.getitem) + x[(side // 2,) * rank] # Numba compilation @benchmark @@ -120,6 +123,8 @@ def test_index_slice(benchmark, indexing_args): side = x.shape[0] rank = x.ndim + if hasattr(sparse, "compiled"): operator.getitem = sparse.compiled(operator.getitem) + x[(slice(side // 2),) * rank] # Numba compilation @benchmark @@ -133,6 +138,8 @@ def test_index_fancy(benchmark, indexing_args, seed): rng = np.random.default_rng(seed=seed) index = rng.integers(0, side, size=(side // 2,)) + if hasattr(sparse, "compiled"): operator.getitem = sparse.compiled(operator.getitem) + x[index] # Numba compilation @benchmark @@ -172,6 +179,8 @@ def densemul_args(request, sides, seed, max_size): def test_gcxs_dot_ndarray(benchmark, densemul_args): x, t = densemul_args + if hasattr(sparse, "compiled"): operator.matmul = sparse.compiled(operator.matmul) + # Numba compilation x @ t