|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | +import scipy.sparse |
| 4 | + |
| 5 | +import pytensor.sparse as ps |
| 6 | +import pytensor.tensor as pt |
| 7 | +from pytensor import function |
| 8 | +from pytensor.graph import FunctionGraph |
| 9 | +from tests.link.jax.test_basic import compare_jax_and_py |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.parametrize( |
| 13 | + "op, x_type, y_type", |
| 14 | + [ |
| 15 | + (ps.dot, pt.vector, ps.matrix), |
| 16 | + (ps.dot, pt.matrix, ps.matrix), |
| 17 | + (ps.dot, ps.matrix, pt.vector), |
| 18 | + (ps.dot, ps.matrix, pt.matrix), |
| 19 | + # structured_dot only allows matrix @ matrix |
| 20 | + (ps.structured_dot, pt.matrix, ps.matrix), |
| 21 | + (ps.structured_dot, ps.matrix, pt.matrix), |
| 22 | + ], |
| 23 | +) |
| 24 | +def test_sparse_dot_constant_sparse(x_type, y_type, op): |
| 25 | + inputs = [] |
| 26 | + test_values = [] |
| 27 | + |
| 28 | + 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") |
| 31 | + else: |
| 32 | + x_pt = x_type("x", dtype="float32") |
| 33 | + if x_pt.ndim == 1: |
| 34 | + x_test = np.arange(40, dtype="float32") |
| 35 | + else: |
| 36 | + x_test = np.arange(5 * 40, dtype="float32").reshape(5, 40) |
| 37 | + inputs.append(x_pt) |
| 38 | + test_values.append(x_test) |
| 39 | + |
| 40 | + 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") |
| 43 | + else: |
| 44 | + y_pt = y_type("y", dtype="float32") |
| 45 | + if y_pt.ndim == 1: |
| 46 | + y_test = np.arange(40, dtype="float32") |
| 47 | + else: |
| 48 | + y_test = np.arange(40 * 3, dtype="float32").reshape(40, 3) |
| 49 | + inputs.append(y_pt) |
| 50 | + test_values.append(y_test) |
| 51 | + |
| 52 | + dot_pt = op(x_pt, y_pt) |
| 53 | + 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 | + |
| 72 | + out = ps.dot(x_pt, y_pt_shared) |
| 73 | + |
| 74 | + with pytest.raises(NotImplementedError, match=msg): |
| 75 | + function([x_pt], out, mode="JAX") |
0 commit comments