|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +from pytensor.compile.function import function |
| 5 | +from pytensor.configdefaults import config |
| 6 | +from pytensor.graph.fg import FunctionGraph |
| 7 | +from pytensor.tensor import nlinalg as pt_nla |
| 8 | +from pytensor.tensor.type import matrix |
| 9 | +from tests.link.pytorch.test_basic import compare_pytorch_and_py |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def matrix_test(): |
| 14 | + rng = np.random.default_rng(213234) |
| 15 | + |
| 16 | + M = rng.normal(size=(3, 3)) |
| 17 | + test_value = M.dot(M.T).astype(config.floatX) |
| 18 | + |
| 19 | + x = matrix("x") |
| 20 | + return (x, test_value) |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize( |
| 24 | + "func", |
| 25 | + (pt_nla.eig, pt_nla.eigh, pt_nla.slogdet, pt_nla.inv, pt_nla.det), |
| 26 | +) |
| 27 | +def test_lin_alg_no_params(func, matrix_test): |
| 28 | + x, test_value = matrix_test |
| 29 | + |
| 30 | + out = func(x) |
| 31 | + out_fg = FunctionGraph([x], out if isinstance(out, list) else [out]) |
| 32 | + |
| 33 | + def assert_fn(x, y): |
| 34 | + np.testing.assert_allclose(x, y, rtol=1e-3) |
| 35 | + |
| 36 | + compare_pytorch_and_py(out_fg, [test_value], assert_fn=assert_fn) |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.parametrize( |
| 40 | + "mode", |
| 41 | + ( |
| 42 | + "complete", |
| 43 | + "reduced", |
| 44 | + "r", |
| 45 | + pytest.param("raw", marks=pytest.mark.xfail(raises=NotImplementedError)), |
| 46 | + ), |
| 47 | +) |
| 48 | +def test_qr(mode, matrix_test): |
| 49 | + x, test_value = matrix_test |
| 50 | + outs = pt_nla.qr(x, mode=mode) |
| 51 | + out_fg = FunctionGraph([x], outs if isinstance(outs, list) else [outs]) |
| 52 | + compare_pytorch_and_py(out_fg, [test_value]) |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.parametrize("compute_uv", [True, False]) |
| 56 | +@pytest.mark.parametrize("full_matrices", [True, False]) |
| 57 | +def test_svd(compute_uv, full_matrices, matrix_test): |
| 58 | + x, test_value = matrix_test |
| 59 | + |
| 60 | + out = pt_nla.svd(x, full_matrices=full_matrices, compute_uv=compute_uv) |
| 61 | + out_fg = FunctionGraph([x], out if isinstance(out, list) else [out]) |
| 62 | + |
| 63 | + compare_pytorch_and_py(out_fg, [test_value]) |
| 64 | + |
| 65 | + |
| 66 | +def test_pinv(): |
| 67 | + x = matrix("x") |
| 68 | + x_inv = pt_nla.pinv(x) |
| 69 | + |
| 70 | + fgraph = FunctionGraph([x], [x_inv]) |
| 71 | + x_np = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=config.floatX) |
| 72 | + compare_pytorch_and_py(fgraph, [x_np]) |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.parametrize("hermitian", [False, True]) |
| 76 | +def test_pinv_hermitian(hermitian): |
| 77 | + A = matrix("A", dtype="complex128") |
| 78 | + A_h_test = np.c_[[3, 3 + 2j], [3 - 2j, 2]] |
| 79 | + A_not_h_test = A_h_test + 0 + 1j |
| 80 | + |
| 81 | + A_inv = pt_nla.pinv(A, hermitian=hermitian) |
| 82 | + torch_fn = function([A], A_inv, mode="PYTORCH") |
| 83 | + |
| 84 | + assert np.allclose(torch_fn(A_h_test), np.linalg.pinv(A_h_test, hermitian=False)) |
| 85 | + assert np.allclose(torch_fn(A_h_test), np.linalg.pinv(A_h_test, hermitian=True)) |
| 86 | + |
| 87 | + assert ( |
| 88 | + np.allclose( |
| 89 | + torch_fn(A_not_h_test), np.linalg.pinv(A_not_h_test, hermitian=False) |
| 90 | + ) |
| 91 | + is not hermitian |
| 92 | + ) |
| 93 | + |
| 94 | + assert ( |
| 95 | + np.allclose( |
| 96 | + torch_fn(A_not_h_test), np.linalg.pinv(A_not_h_test, hermitian=True) |
| 97 | + ) |
| 98 | + is hermitian |
| 99 | + ) |
| 100 | + |
| 101 | + |
| 102 | +def test_kron(): |
| 103 | + x = matrix("x") |
| 104 | + y = matrix("y") |
| 105 | + z = pt_nla.kron(x, y) |
| 106 | + |
| 107 | + fgraph = FunctionGraph([x, y], [z]) |
| 108 | + x_np = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=config.floatX) |
| 109 | + y_np = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=config.floatX) |
| 110 | + |
| 111 | + compare_pytorch_and_py(fgraph, [x_np, y_np]) |
0 commit comments