|
1 | 1 | import sparse
|
2 | 2 |
|
3 |
| -import pytest |
4 |
| - |
5 | 3 | import numpy as np
|
6 | 4 | import scipy.sparse as sp
|
| 5 | +from numpy.testing import assert_equal |
7 | 6 |
|
8 | 7 |
|
9 | 8 | def test_backend_contex_manager(backend):
|
| 9 | + rng = np.random.default_rng(0) |
| 10 | + x = sparse.random((100, 10, 100), density=0.01, random_state=rng) |
| 11 | + y = sparse.random((100, 10, 100), density=0.01, random_state=rng) |
| 12 | + |
10 | 13 | if backend == sparse.BackendType.Finch:
|
11 |
| - with pytest.raises(NotImplementedError): |
12 |
| - sparse.COO.from_numpy(np.eye(5)) |
| 14 | + import finch |
| 15 | + |
| 16 | + def storage(): |
| 17 | + return finch.Storage(finch.Dense(finch.SparseList(finch.SparseList(finch.Element(0.0)))), order="C") |
| 18 | + |
| 19 | + x = x.to_device(storage()) |
| 20 | + y = y.to_device(storage()) |
13 | 21 | else:
|
14 |
| - sparse.COO.from_numpy(np.eye(5)) |
| 22 | + x.asformat("gcxs") |
| 23 | + y.asformat("gcxs") |
| 24 | + |
| 25 | + z = x + y |
| 26 | + result = sparse.sum(z) |
| 27 | + assert result.shape == () |
15 | 28 |
|
16 | 29 |
|
17 | 30 | def test_finch_backend():
|
18 | 31 | np_eye = np.eye(5)
|
19 | 32 | sp_arr = sp.csr_matrix(np_eye)
|
20 | 33 |
|
21 | 34 | with sparse.Backend(backend=sparse.BackendType.Finch):
|
22 |
| - finch_dense = sparse.Tensor(np_eye) |
| 35 | + import finch |
| 36 | + |
| 37 | + finch_dense = finch.Tensor(np_eye) |
23 | 38 |
|
24 | 39 | assert np.shares_memory(finch_dense.todense(), np_eye)
|
25 | 40 |
|
26 |
| - finch_arr = sparse.Tensor(sp_arr) |
| 41 | + finch_arr = finch.Tensor(sp_arr) |
27 | 42 |
|
28 |
| - np.testing.assert_equal(finch_arr.todense(), np_eye) |
| 43 | + assert_equal(finch_arr.todense(), np_eye) |
29 | 44 |
|
30 | 45 | transposed = sparse.permute_dims(finch_arr, (1, 0))
|
31 | 46 |
|
32 |
| - np.testing.assert_equal(transposed.todense(), np_eye.T) |
| 47 | + assert_equal(transposed.todense(), np_eye.T) |
| 48 | + |
| 49 | + @sparse.compiled |
| 50 | + def my_fun(tns1, tns2): |
| 51 | + tmp = sparse.add(tns1, tns2) |
| 52 | + return sparse.sum(tmp, axis=0) |
| 53 | + |
| 54 | + result = my_fun(finch_dense, finch_arr) |
| 55 | + |
| 56 | + assert_equal(result.todense(), np.sum(2 * np_eye, axis=0)) |
0 commit comments