|
2 | 2 | import pytest
|
3 | 3 | import scipy.sparse
|
4 | 4 |
|
| 5 | + |
| 6 | +jax = pytest.importorskip("jax") |
| 7 | +from jax.experimental.sparse import BCOO |
| 8 | + |
5 | 9 | import pytensor.sparse as ps
|
6 | 10 | import pytensor.tensor as pt
|
7 | 11 | from pytensor import function
|
8 |
| -from pytensor.graph import FunctionGraph |
| 12 | +from pytensor.graph import Constant, FunctionGraph |
| 13 | +from pytensor.tensor.type import DenseTensorType |
9 | 14 | from tests.link.jax.test_basic import compare_jax_and_py
|
10 | 15 |
|
11 | 16 |
|
| 17 | +def assert_bcoo_arrays_allclose(a1, a2): |
| 18 | + assert isinstance(a1, BCOO) |
| 19 | + assert isinstance(a1, BCOO) |
| 20 | + np.testing.assert_allclose(a1.todense(), a2.todense()) |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize("sparse_type", ("csc", "csr")) |
| 24 | +def test_sparse_io(sparse_type): |
| 25 | + """Test explicit (non-shared) input and output sparse types in JAX.""" |
| 26 | + sparse_mat = ps.matrix(format=sparse_type, name="csc", dtype="float32") |
| 27 | + sparse_mat_out = sparse_mat.T |
| 28 | + |
| 29 | + with pytest.warns( |
| 30 | + UserWarning, |
| 31 | + match="SparseTypes are implicitly converted to sparse BCOO arrays", |
| 32 | + ): |
| 33 | + fn = function([sparse_mat], sparse_mat_out, mode="JAX") |
| 34 | + |
| 35 | + sp_sparse_mat = scipy.sparse.random( |
| 36 | + 5, 40, density=0.25, format=sparse_type, dtype="float32" |
| 37 | + ) |
| 38 | + jx_sparse_mat = BCOO.from_scipy_sparse(sp_sparse_mat) |
| 39 | + |
| 40 | + sp_res = fn(sp_sparse_mat) |
| 41 | + jx_res = fn(jx_sparse_mat) |
| 42 | + assert_bcoo_arrays_allclose(sp_res, jx_sparse_mat.T) |
| 43 | + assert_bcoo_arrays_allclose(jx_res, jx_sparse_mat.T) |
| 44 | + |
| 45 | + # Chained applications |
| 46 | + assert_bcoo_arrays_allclose(fn(fn(sp_sparse_mat)), jx_sparse_mat) |
| 47 | + assert_bcoo_arrays_allclose(fn(fn(jx_sparse_mat)), jx_sparse_mat) |
| 48 | + |
| 49 | + |
12 | 50 | @pytest.mark.parametrize(
|
13 | 51 | "op, x_type, y_type",
|
14 | 52 | [
|
|
19 | 57 | # structured_dot only allows matrix @ matrix
|
20 | 58 | (ps.structured_dot, pt.matrix, ps.matrix),
|
21 | 59 | (ps.structured_dot, ps.matrix, pt.matrix),
|
| 60 | + (ps.structured_dot, ps.matrix, ps.matrix), |
22 | 61 | ],
|
23 | 62 | )
|
24 |
| -def test_sparse_dot_constant_sparse(x_type, y_type, op): |
| 63 | +@pytest.mark.parametrize("x_constant", (False, True)) |
| 64 | +@pytest.mark.parametrize("y_constant", (False, True)) |
| 65 | +def test_sparse_dot(x_type, y_type, op, x_constant, y_constant): |
25 | 66 | inputs = []
|
26 | 67 | test_values = []
|
27 | 68 |
|
28 | 69 | if x_type is ps.matrix:
|
29 |
| - x_sp = scipy.sparse.random(5, 40, density=0.25, format="csr", dtype="float32") |
30 |
| - x_pt = ps.as_sparse_variable(x_sp, name="x") |
| 70 | + x_test = scipy.sparse.random(5, 40, density=0.25, format="csr", dtype="float32") |
| 71 | + x_pt = ps.as_sparse_variable(x_test, name="x") |
31 | 72 | else:
|
32 |
| - x_pt = x_type("x", dtype="float32") |
33 |
| - if x_pt.ndim == 1: |
| 73 | + if x_type is pt.vector: |
34 | 74 | x_test = np.arange(40, dtype="float32")
|
35 | 75 | else:
|
36 | 76 | x_test = np.arange(5 * 40, dtype="float32").reshape(5, 40)
|
| 77 | + x_pt = pt.as_tensor_variable(x_test, name="x") |
| 78 | + assert isinstance(x_pt, Constant) |
| 79 | + |
| 80 | + if not x_constant: |
| 81 | + x_pt = x_pt.type(name="x") |
37 | 82 | inputs.append(x_pt)
|
38 | 83 | test_values.append(x_test)
|
39 | 84 |
|
40 | 85 | if y_type is ps.matrix:
|
41 |
| - y_sp = scipy.sparse.random(40, 3, density=0.25, format="csc", dtype="float32") |
42 |
| - y_pt = ps.as_sparse_variable(y_sp, name="y") |
| 86 | + y_test = scipy.sparse.random(40, 3, density=0.25, format="csc", dtype="float32") |
| 87 | + y_pt = ps.as_sparse_variable(y_test, name="y") |
43 | 88 | else:
|
44 |
| - y_pt = y_type("y", dtype="float32") |
45 |
| - if y_pt.ndim == 1: |
| 89 | + if y_type is pt.vector: |
46 | 90 | y_test = np.arange(40, dtype="float32")
|
47 | 91 | else:
|
48 | 92 | y_test = np.arange(40 * 3, dtype="float32").reshape(40, 3)
|
| 93 | + y_pt = pt.as_tensor_variable(y_test, name="y") |
| 94 | + assert isinstance(y_pt, Constant) |
| 95 | + |
| 96 | + if not y_constant: |
| 97 | + y_pt = y_pt.type(name="y") |
49 | 98 | inputs.append(y_pt)
|
50 | 99 | test_values.append(y_test)
|
51 | 100 |
|
52 | 101 | dot_pt = op(x_pt, y_pt)
|
53 | 102 | fgraph = FunctionGraph(inputs, [dot_pt])
|
54 |
| - compare_jax_and_py(fgraph, test_values) |
55 |
| - |
56 |
| - |
57 |
| -def test_sparse_dot_non_const_raises(): |
58 |
| - x_pt = pt.vector("x") |
59 |
| - |
60 |
| - y_sp = scipy.sparse.random(40, 3, density=0.25, format="csc", dtype="float32") |
61 |
| - y_pt = ps.as_sparse_variable(y_sp, name="y").type() |
62 |
| - |
63 |
| - out = ps.dot(x_pt, y_pt) |
64 |
| - |
65 |
| - msg = "JAX sparse dot only implemented for constant sparse inputs" |
66 |
| - |
67 |
| - with pytest.raises(NotImplementedError, match=msg): |
68 |
| - function([x_pt, y_pt], out, mode="JAX") |
69 |
| - |
70 |
| - y_pt_shared = ps.shared(y_sp, name="y") |
71 | 103 |
|
72 |
| - out = ps.dot(x_pt, y_pt_shared) |
| 104 | + def assert_fn(x, y): |
| 105 | + [x] = x |
| 106 | + [y] = y |
| 107 | + if hasattr(x, "todense"): |
| 108 | + x = x.todense() |
| 109 | + if hasattr(y, "todense"): |
| 110 | + y = y.todense() |
| 111 | + np.testing.assert_allclose(x, y) |
73 | 112 |
|
74 |
| - with pytest.raises(NotImplementedError, match=msg): |
75 |
| - function([x_pt], out, mode="JAX") |
| 113 | + compare_jax_and_py( |
| 114 | + fgraph, |
| 115 | + test_values, |
| 116 | + must_be_device_array=isinstance(dot_pt.type, DenseTensorType), |
| 117 | + assert_fn=assert_fn, |
| 118 | + ) |
0 commit comments