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