Skip to content

Commit adfdf04

Browse files
API: Initial finch_backend (#649)
* API: Initial finch backend Co-authored-by: Hameer Abbasi <[email protected]>
1 parent 32e6e0b commit adfdf04

File tree

6 files changed

+33
-3
lines changed

6 files changed

+33
-3
lines changed

.github/workflows/ci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ jobs:
3434
key:
3535
test-${{ matrix.os }}-conda-py${{ matrix.python }}-${{ env.CACHE_NUMBER }}-${{
3636
hashFiles('ci/environment.yml') }}
37+
- uses: julia-actions/setup-julia@v1
38+
with:
39+
version: '1.10.0'
3740
- uses: conda-incubator/setup-miniconda@v3
3841
with:
3942
activate-environment: sparse-dev

ci/environment.yml

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ name: sparse-dev
22
channels:
33
- conda-forge
44
dependencies:
5+
- python
56
- pip

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ tests = [
3434
"pytest-cov",
3535
"pre-commit",
3636
"scipy",
37+
"sparse[finch]",
3738
]
3839
tox = ["sparse[tests]", "tox"]
3940
all = ["sparse[docs,tox]", "matrepr"]

sparse/__init__.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ def __init__(self, backend=BackendType.PyData):
2626
self.token = None
2727

2828
def __enter__(self):
29-
token = backend_var.set(self.backend)
30-
self.token = token
29+
self.token = backend_var.set(self.backend)
3130

3231
def __exit__(self, exc_type, exc_value, traceback):
3332
backend_var.reset(self.token)

sparse/finch_backend/__init__.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
Tensor = ()
1+
try:
2+
import finch # noqa: F401
3+
except ModuleNotFoundError as e:
4+
raise ImportError("Finch not installed. Run `pip install sparse[finch]` to enable Finch backend") from e
5+
6+
from finch import Tensor, astype, permute_dims
7+
8+
__all__ = ["Tensor", "astype", "permute_dims"]
29

310

411
class COO:

sparse/tests/test_backends.py

+19
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44

55
import numpy as np
6+
import scipy.sparse as sp
67

78

89
def test_backend_contex_manager(backend):
@@ -11,3 +12,21 @@ def test_backend_contex_manager(backend):
1112
sparse.COO.from_numpy(np.eye(5))
1213
else:
1314
sparse.COO.from_numpy(np.eye(5))
15+
16+
17+
def test_finch_backend():
18+
np_eye = np.eye(5)
19+
sp_arr = sp.csr_matrix(np_eye)
20+
21+
with sparse.Backend(backend=sparse.BackendType.Finch):
22+
finch_dense = sparse.Tensor(np_eye)
23+
24+
assert np.shares_memory(finch_dense.todense(), np_eye)
25+
26+
finch_arr = sparse.Tensor(sp_arr)
27+
28+
np.testing.assert_equal(finch_arr.todense(), np_eye)
29+
30+
transposed = sparse.permute_dims(finch_arr, (1, 0))
31+
32+
np.testing.assert_equal(transposed.todense(), np_eye.T)

0 commit comments

Comments
 (0)