|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +import pytensor.tensor as pt |
| 5 | +from pytensor.configdefaults import config |
| 6 | +from pytensor.graph.fg import FunctionGraph |
| 7 | +from pytensor.tensor import slinalg as pt_slinalg |
| 8 | +from pytensor.tensor.type import matrix, vector |
| 9 | +from tests.link.pytorch.test_basic import compare_pytorch_and_py |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.parametrize("lower", [False, True]) |
| 13 | +def test_pytorch_eigvalsh(lower): |
| 14 | + A = matrix("A") |
| 15 | + B = matrix("B") |
| 16 | + |
| 17 | + out = pt_slinalg.eigvalsh(A, B, lower=lower) |
| 18 | + out_fg = FunctionGraph([A, B], [out]) |
| 19 | + |
| 20 | + with pytest.raises(NotImplementedError): |
| 21 | + compare_pytorch_and_py( |
| 22 | + out_fg, |
| 23 | + [ |
| 24 | + np.array( |
| 25 | + [[6, 3, 1, 5], [3, 0, 5, 1], [1, 5, 6, 2], [5, 1, 2, 2]] |
| 26 | + ).astype(config.floatX), |
| 27 | + np.array( |
| 28 | + [[10, 0, 1, 3], [0, 12, 7, 8], [1, 7, 14, 2], [3, 8, 2, 16]] |
| 29 | + ).astype(config.floatX), |
| 30 | + ], |
| 31 | + ) |
| 32 | + compare_pytorch_and_py( |
| 33 | + out_fg, |
| 34 | + [ |
| 35 | + np.array([[6, 3, 1, 5], [3, 0, 5, 1], [1, 5, 6, 2], [5, 1, 2, 2]]).astype( |
| 36 | + config.floatX |
| 37 | + ), |
| 38 | + None, |
| 39 | + ], |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +def test_pytorch_basic(): |
| 44 | + rng = np.random.default_rng(28494) |
| 45 | + |
| 46 | + x = matrix("x") |
| 47 | + b = vector("b") |
| 48 | + |
| 49 | + out = pt_slinalg.cholesky(x) |
| 50 | + out_fg = FunctionGraph([x], [out]) |
| 51 | + compare_pytorch_and_py( |
| 52 | + out_fg, |
| 53 | + [ |
| 54 | + (np.eye(10) + rng.standard_normal(size=(10, 10)) * 0.01).astype( |
| 55 | + config.floatX |
| 56 | + ) |
| 57 | + ], |
| 58 | + ) |
| 59 | + |
| 60 | + out = pt_slinalg.Cholesky(lower=False)(x) |
| 61 | + out_fg = FunctionGraph([x], [out]) |
| 62 | + compare_pytorch_and_py( |
| 63 | + out_fg, |
| 64 | + [ |
| 65 | + (np.eye(10) + rng.standard_normal(size=(10, 10)) * 0.01).astype( |
| 66 | + config.floatX |
| 67 | + ) |
| 68 | + ], |
| 69 | + ) |
| 70 | + |
| 71 | + out = pt_slinalg.solve(x, b) |
| 72 | + out_fg = FunctionGraph([x, b], [out]) |
| 73 | + compare_pytorch_and_py( |
| 74 | + out_fg, |
| 75 | + [ |
| 76 | + np.eye(10).astype(config.floatX), |
| 77 | + np.arange(10).astype(config.floatX), |
| 78 | + ], |
| 79 | + ) |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.xfail(reason="Blockwise not implemented") |
| 83 | +@pytest.mark.parametrize( |
| 84 | + "check_finite", |
| 85 | + (False, pytest.param(True, marks=pytest.mark.xfail(raises=NotImplementedError))), |
| 86 | +) |
| 87 | +@pytest.mark.parametrize("lower", [False, True]) |
| 88 | +@pytest.mark.parametrize("trans", [0, 1, 2, "S"]) |
| 89 | +def test_pytorch_SolveTriangular(trans, lower, check_finite): |
| 90 | + x = matrix("x") |
| 91 | + b = vector("b") |
| 92 | + |
| 93 | + out = pt_slinalg.solve_triangular( |
| 94 | + x, |
| 95 | + b, |
| 96 | + trans=trans, |
| 97 | + lower=lower, |
| 98 | + check_finite=check_finite, |
| 99 | + ) |
| 100 | + out_fg = FunctionGraph([x, b], [out]) |
| 101 | + compare_pytorch_and_py( |
| 102 | + out_fg, |
| 103 | + [ |
| 104 | + np.eye(10).astype(config.floatX), |
| 105 | + np.arange(10).astype(config.floatX), |
| 106 | + ], |
| 107 | + ) |
| 108 | + |
| 109 | + |
| 110 | +def test_pytorch_block_diag(): |
| 111 | + A = matrix("A") |
| 112 | + B = matrix("B") |
| 113 | + C = matrix("C") |
| 114 | + D = matrix("D") |
| 115 | + |
| 116 | + out = pt_slinalg.block_diag(A, B, C, D) |
| 117 | + out_fg = FunctionGraph([A, B, C, D], [out]) |
| 118 | + |
| 119 | + compare_pytorch_and_py( |
| 120 | + out_fg, |
| 121 | + [ |
| 122 | + np.random.normal(size=(5, 5)).astype(config.floatX), |
| 123 | + np.random.normal(size=(3, 3)).astype(config.floatX), |
| 124 | + np.random.normal(size=(2, 2)).astype(config.floatX), |
| 125 | + np.random.normal(size=(4, 4)).astype(config.floatX), |
| 126 | + ], |
| 127 | + ) |
| 128 | + |
| 129 | + |
| 130 | +@pytest.mark.xfail(reason="Blockwise not implemented") |
| 131 | +def test_pytorch_block_diag_blockwise(): |
| 132 | + A = pt.tensor3("A") |
| 133 | + B = pt.tensor3("B") |
| 134 | + out = pt_slinalg.block_diag(A, B) |
| 135 | + out_fg = FunctionGraph([A, B], [out]) |
| 136 | + compare_pytorch_and_py( |
| 137 | + out_fg, |
| 138 | + [ |
| 139 | + np.random.normal(size=(5, 5, 5)).astype(config.floatX), |
| 140 | + np.random.normal(size=(5, 3, 3)).astype(config.floatX), |
| 141 | + ], |
| 142 | + ) |
0 commit comments