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