-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathtest_linalg.py
1103 lines (907 loc) · 34.4 KB
/
test_linalg.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from functools import partial
import numpy as np
import pytest
import scipy.linalg
from numpy.testing import assert_allclose
import pytensor
from pytensor import function
from pytensor import tensor as pt
from pytensor.compile import get_default_mode
from pytensor.configdefaults import config
from pytensor.graph.rewriting.utils import rewrite_graph
from pytensor.tensor import swapaxes
from pytensor.tensor.blockwise import Blockwise
from pytensor.tensor.elemwise import DimShuffle
from pytensor.tensor.math import _allclose, dot, matmul
from pytensor.tensor.nlinalg import (
SVD,
Det,
Eig,
KroneckerProduct,
MatrixInverse,
MatrixPinv,
SLogDet,
matrix_inverse,
svd,
)
from pytensor.tensor.rewriting.linalg import inv_as_solve
from pytensor.tensor.slinalg import (
BlockDiagonal,
Cholesky,
Solve,
SolveBase,
SolveTriangular,
cho_solve,
cholesky,
solve,
solve_triangular,
)
from pytensor.tensor.type import dmatrix, matrix, tensor, vector
from tests import unittest_tools as utt
from tests.test_rop import break_op
ATOL = RTOL = 1e-3 if config.floatX == "float32" else 1e-8
def test_rop_lop():
mx = matrix("mx")
mv = matrix("mv")
v = vector("v")
y = MatrixInverse()(mx).sum(axis=0)
yv = pytensor.gradient.Rop(y, mx, mv)
rop_f = function([mx, mv], yv)
sy, _ = pytensor.scan(
lambda i, y, x, v: (pytensor.gradient.grad(y[i], x) * v).sum(),
sequences=pt.arange(y.shape[0]),
non_sequences=[y, mx, mv],
)
scan_f = function([mx, mv], sy)
rng = np.random.default_rng(utt.fetch_seed())
vx = np.asarray(rng.standard_normal((4, 4)), pytensor.config.floatX)
vv = np.asarray(rng.standard_normal((4, 4)), pytensor.config.floatX)
v1 = rop_f(vx, vv)
v2 = scan_f(vx, vv)
assert _allclose(v1, v2), f"ROP mismatch: {v1} {v2}"
raised = False
try:
pytensor.gradient.Rop(
pytensor.clone_replace(y, replace={mx: break_op(mx)}), mx, mv
)
except ValueError:
raised = True
if not raised:
raise Exception(
"Op did not raised an error even though the function"
" is not differentiable"
)
vv = np.asarray(rng.uniform(size=(4,)), pytensor.config.floatX)
yv = pytensor.gradient.Lop(y, mx, v)
lop_f = function([mx, v], yv)
sy = pytensor.gradient.grad((v * y).sum(), mx)
scan_f = function([mx, v], sy)
v1 = lop_f(vx, vv)
v2 = scan_f(vx, vv)
assert _allclose(v1, v2), f"LOP mismatch: {v1} {v2}"
def test_transinv_to_invtrans():
X = matrix("X")
Y = matrix_inverse(X)
Z = Y.transpose()
f = pytensor.function([X], Z)
if config.mode != "FAST_COMPILE":
for node in f.maker.fgraph.toposort():
if isinstance(node.op, MatrixInverse):
assert isinstance(node.inputs[0].owner.op, DimShuffle)
if isinstance(node.op, DimShuffle):
assert node.inputs[0].name == "X"
def test_generic_solve_to_solve_triangular():
A = matrix("A")
x = matrix("x")
L = cholesky(A, lower=True)
U = cholesky(A, lower=False)
b1 = solve(L, x)
b2 = solve(U, x)
f = pytensor.function([A, x], b1)
rng = np.random.default_rng(97)
X = rng.normal(size=(10, 10)).astype(config.floatX)
X = X @ X.T
X_chol = np.linalg.cholesky(X)
eye = np.eye(10, dtype=config.floatX)
if config.mode != "FAST_COMPILE":
toposort = f.maker.fgraph.toposort()
op_list = [node.op for node in toposort]
assert not any(isinstance(op, Solve) for op in op_list)
assert any(isinstance(op, SolveTriangular) for op in op_list)
assert_allclose(
f(X, eye) @ X_chol, eye, atol=1e-8 if config.floatX.endswith("64") else 1e-4
)
f = pytensor.function([A, x], b2)
if config.mode != "FAST_COMPILE":
toposort = f.maker.fgraph.toposort()
op_list = [node.op for node in toposort]
assert not any(isinstance(op, Solve) for op in op_list)
assert any(isinstance(op, SolveTriangular) for op in op_list)
assert_allclose(
f(X, eye).T @ X_chol,
eye,
atol=1e-8 if config.floatX.endswith("64") else 1e-4,
)
def test_matrix_inverse_solve():
A = dmatrix("A")
b = dmatrix("b")
node = matrix_inverse(A).dot(b).owner
[out] = inv_as_solve.transform(None, node)
assert isinstance(out.owner.op, Blockwise) and isinstance(
out.owner.op.core_op, Solve
)
@pytest.mark.parametrize("tag", ("lower", "upper", None))
@pytest.mark.parametrize("cholesky_form", ("lower", "upper"))
@pytest.mark.parametrize("product", ("lower", "upper", None))
@pytest.mark.parametrize("op", (dot, matmul))
def test_cholesky_ldotlt(tag, cholesky_form, product, op):
transform_removes_chol = tag is not None and product == tag
transform_transposes = transform_removes_chol and cholesky_form != tag
ndim = 2 if op == dot else 3
A = tensor("L", shape=(None,) * ndim)
if tag:
setattr(A.tag, tag + "_triangular", True)
if product == "lower":
M = op(A, swapaxes(A, -1, -2))
elif product == "upper":
M = op(swapaxes(A, -1, -2), A)
else:
M = A
C = cholesky(M, lower=(cholesky_form == "lower"))
f = pytensor.function([A], C, mode=get_default_mode().including("cholesky_ldotlt"))
no_cholesky_in_graph = not any(
isinstance(node.op, Cholesky)
or (isinstance(node.op, Blockwise) and isinstance(node.op.core_op, Cholesky))
for node in f.maker.fgraph.apply_nodes
)
assert no_cholesky_in_graph == transform_removes_chol
if transform_transposes:
expected_order = (1, 0) if ndim == 2 else (0, 2, 1)
assert any(
isinstance(node.op, DimShuffle) and node.op.new_order == expected_order
for node in f.maker.fgraph.apply_nodes
)
# Test some concrete value through f
# there must be lower triangular (f assumes they are)
Avs = [
np.eye(1, dtype=pytensor.config.floatX),
np.eye(10, dtype=pytensor.config.floatX),
np.array([[2, 0], [1, 4]], dtype=pytensor.config.floatX),
]
if not tag:
# these must be positive def
Avs.extend(
[
np.ones((4, 4), dtype=pytensor.config.floatX)
+ np.eye(4, dtype=pytensor.config.floatX),
]
)
cholesky_vect_fn = np.vectorize(
partial(scipy.linalg.cholesky, lower=(cholesky_form == "lower")),
signature="(a, a)->(a, a)",
)
for Av in Avs:
if tag == "upper":
Av = Av.T
if product == "lower":
Mv = Av.dot(Av.T)
elif product == "upper":
Mv = Av.T.dot(Av)
else:
Mv = Av
if ndim == 3:
Av = np.broadcast_to(Av, (5, *Av.shape))
Mv = np.broadcast_to(Mv, (5, *Mv.shape))
np.testing.assert_allclose(
cholesky_vect_fn(Mv),
f(Av),
)
def test_local_det_chol():
X = matrix("X")
L = pt.linalg.cholesky(X)
det_X = pt.linalg.det(X)
f = function([X], [L, det_X])
nodes = f.maker.fgraph.toposort()
assert not any(isinstance(node, Det) for node in nodes)
# This previously raised an error (issue #392)
f = function([X], [L, det_X, X])
nodes = f.maker.fgraph.toposort()
assert not any(isinstance(node, Det) for node in nodes)
def test_psd_solve_with_chol():
X = matrix("X")
X.tag.psd = True
X_inv = pt.linalg.solve(X, pt.identity_like(X))
f = function([X], X_inv, mode="FAST_RUN")
nodes = f.maker.fgraph.apply_nodes
assert not any(isinstance(node.op, Solve) for node in nodes)
assert any(isinstance(node.op, Cholesky) for node in nodes)
assert any(isinstance(node.op, SolveTriangular) for node in nodes)
# Numeric test
rng = np.random.default_rng(sum(map(ord, "test_psd_solve_with_chol")))
L = rng.normal(size=(5, 5)).astype(config.floatX)
X_psd = L @ L.T
X_psd_inv = f(X_psd)
assert_allclose(
X_psd_inv,
np.linalg.inv(X_psd),
atol=1e-4 if config.floatX == "float32" else 1e-8,
rtol=1e-4 if config.floatX == "float32" else 1e-8,
)
class TestBatchedVectorBSolveToMatrixBSolve:
rewrite_name = "batched_vector_b_solve_to_matrix_b_solve"
@staticmethod
def any_vector_b_solve(fn):
return any(
(
isinstance(node.op, Blockwise)
and isinstance(node.op.core_op, SolveBase)
and node.op.core_op.b_ndim == 1
)
for node in fn.maker.fgraph.apply_nodes
)
@pytest.mark.parametrize("solve_op", (solve, solve_triangular, cho_solve))
def test_valid_cases(self, solve_op):
rng = np.random.default_rng(sum(map(ord, solve_op.__name__)))
a = tensor(shape=(None, None))
b = tensor(shape=(None, None, None))
if solve_op is cho_solve:
# cho_solves expects a tuple (a, lower) as the first input
out = solve_op((a, True), b, b_ndim=1)
else:
out = solve_op(a, b, b_ndim=1)
mode = get_default_mode().excluding(self.rewrite_name)
ref_fn = pytensor.function([a, b], out, mode=mode)
assert self.any_vector_b_solve(ref_fn)
mode = get_default_mode().including(self.rewrite_name)
opt_fn = pytensor.function([a, b], out, mode=mode)
assert not self.any_vector_b_solve(opt_fn)
test_a = rng.normal(size=(3, 3)).astype(config.floatX)
test_b = rng.normal(size=(7, 5, 3)).astype(config.floatX)
np.testing.assert_allclose(
opt_fn(test_a, test_b),
ref_fn(test_a, test_b),
rtol=1e-7 if config.floatX == "float64" else 1e-5,
)
def test_invalid_batched_a(self):
rng = np.random.default_rng(sum(map(ord, self.rewrite_name)))
# Rewrite is not applicable if a has batched dims
a = tensor(shape=(None, None, None))
b = tensor(shape=(None, None, None))
out = solve(a, b, b_ndim=1)
mode = get_default_mode().including(self.rewrite_name)
opt_fn = pytensor.function([a, b], out, mode=mode)
assert self.any_vector_b_solve(opt_fn)
ref_fn = np.vectorize(np.linalg.solve, signature="(m,m),(m)->(m)")
test_a = rng.normal(size=(5, 3, 3)).astype(config.floatX)
test_b = rng.normal(size=(7, 5, 3)).astype(config.floatX)
np.testing.assert_allclose(
opt_fn(test_a, test_b),
ref_fn(test_a, test_b),
rtol=1e-7 if config.floatX == "float64" else 1e-5,
)
@pytest.mark.parametrize(
"constructor", [pt.dmatrix, pt.tensor3], ids=["not_batched", "batched"]
)
@pytest.mark.parametrize(
"f_op, f",
[
(MatrixInverse, pt.linalg.inv),
(Cholesky, pt.linalg.cholesky),
(MatrixPinv, pt.linalg.pinv),
],
ids=["inv", "cholesky", "pinv"],
)
@pytest.mark.parametrize(
"g_op, g",
[(BlockDiagonal, pt.linalg.block_diag), (KroneckerProduct, pt.linalg.kron)],
ids=["block_diag", "kron"],
)
def test_local_lift_through_linalg(constructor, f_op, f, g_op, g):
rng = np.random.default_rng(sum(map(ord, "lift_through_linalg")))
if pytensor.config.floatX.endswith("32"):
pytest.skip("Test is flaky at half precision")
A, B = list(map(constructor, "ab"))
X = f(g(A, B))
f1 = pytensor.function(
[A, B], X, mode=get_default_mode().including("local_lift_through_linalg")
)
f2 = pytensor.function(
[A, B], X, mode=get_default_mode().excluding("local_lift_through_linalg")
)
all_apply_nodes = f1.maker.fgraph.apply_nodes
f_ops = [
x for x in all_apply_nodes if isinstance(getattr(x.op, "core_op", x.op), f_op)
]
g_ops = [
x for x in all_apply_nodes if isinstance(getattr(x.op, "core_op", x.op), g_op)
]
assert len(f_ops) == 2
assert len(g_ops) == 1
test_vals = [rng.normal(size=(3,) * A.ndim).astype(config.floatX) for _ in range(2)]
test_vals = [x @ np.swapaxes(x, -1, -2) for x in test_vals]
np.testing.assert_allclose(f1(*test_vals), f2(*test_vals), atol=1e-8)
@pytest.mark.parametrize(
"shape",
[(), (7,), (1, 7), (7, 1), (7, 7), (3, 7, 7)],
ids=["scalar", "vector", "row_vec", "col_vec", "matrix", "batched_input"],
)
def test_det_diag_from_eye_mul(shape):
# Initializing x based on scalar/vector/matrix
x = pt.tensor("x", shape=shape)
y = pt.eye(7) * x
# Calculating determinant value using pt.linalg.det
z_det = pt.linalg.det(y)
# REWRITE TEST
f_rewritten = function([x], z_det, mode="FAST_RUN")
nodes = f_rewritten.maker.fgraph.apply_nodes
assert not any(
isinstance(node.op, Det) or isinstance(getattr(node.op, "core_op", None), Det)
for node in nodes
)
# NUMERIC VALUE TEST
if len(shape) == 0:
x_test = np.array(np.random.rand()).astype(config.floatX)
elif len(shape) == 1:
x_test = np.random.rand(*shape).astype(config.floatX)
else:
x_test = np.random.rand(*shape).astype(config.floatX)
x_test_matrix = np.eye(7) * x_test
det_val = np.linalg.det(x_test_matrix)
rewritten_val = f_rewritten(x_test)
assert_allclose(
det_val,
rewritten_val,
atol=1e-3 if config.floatX == "float32" else 1e-8,
rtol=1e-3 if config.floatX == "float32" else 1e-8,
)
def test_det_diag_from_diag():
x = pt.tensor("x", shape=(None,))
x_diag = pt.diag(x)
y = pt.linalg.det(x_diag)
# REWRITE TEST
f_rewritten = function([x], y, mode="FAST_RUN")
nodes = f_rewritten.maker.fgraph.apply_nodes
assert not any(isinstance(node.op, Det) for node in nodes)
# NUMERIC VALUE TEST
x_test = np.random.rand(7).astype(config.floatX)
x_test_matrix = np.eye(7) * x_test
det_val = np.linalg.det(x_test_matrix)
rewritten_val = f_rewritten(x_test)
assert_allclose(
det_val,
rewritten_val,
atol=1e-3 if config.floatX == "float32" else 1e-8,
rtol=1e-3 if config.floatX == "float32" else 1e-8,
)
def test_dont_apply_det_diag_rewrite_for_1_1():
x = pt.matrix("x")
x_diag = pt.eye(1, 1) * x
y = pt.linalg.det(x_diag)
f_rewritten = function([x], y, mode="FAST_RUN")
nodes = f_rewritten.maker.fgraph.apply_nodes
assert any(isinstance(node.op, Det) for node in nodes)
# Numeric Value test
x_test = np.random.normal(size=(3, 3)).astype(config.floatX)
x_test_matrix = np.eye(1, 1) * x_test
det_val = np.linalg.det(x_test_matrix)
rewritten_val = f_rewritten(x_test)
assert_allclose(
det_val,
rewritten_val,
atol=1e-3 if config.floatX == "float32" else 1e-8,
rtol=1e-3 if config.floatX == "float32" else 1e-8,
)
def test_det_diag_incorrect_for_rectangle_eye():
x = pt.matrix("x")
x_diag = pt.eye(7, 5) * x
with pytest.raises(ValueError, match="Determinant not defined"):
pt.linalg.det(x_diag)
def test_svd_uv_merge():
a = matrix("a")
s_1 = svd(a, full_matrices=False, compute_uv=False)
_, s_2, _ = svd(a, full_matrices=False, compute_uv=True)
_, s_3, _ = svd(a, full_matrices=True, compute_uv=True)
u_4, s_4, v_4 = svd(a, full_matrices=True, compute_uv=True)
# `grad` will introduces an SVD Op with compute_uv=True
# full_matrices = True is not supported for grad of svd
gs = pt.grad(pt.sum(s_1), a)
# 1. compute_uv=False needs rewriting with compute_uv=True
f_1 = pytensor.function([a], gs)
nodes = f_1.maker.fgraph.apply_nodes
svd_counter = 0
for node in nodes:
if isinstance(node.op, SVD):
assert node.op.compute_uv
svd_counter += 1
assert svd_counter == 1
# 2. compute_uv=True needs rewriting with compute=False, reuse node
f_2 = pytensor.function([a], [s_1, s_2])
nodes = f_2.maker.fgraph.apply_nodes
svd_counter = 0
for node in nodes:
if isinstance(node.op, SVD):
assert not node.op.compute_uv
svd_counter += 1
assert svd_counter == 1
# 3. compute_uv=True needs rewriting with compute=False, create new node
# full_matrices needs to retain the value
f_3 = pytensor.function([a], [s_2])
nodes = f_3.maker.fgraph.apply_nodes
svd_counter = 0
for node in nodes:
if isinstance(node.op, SVD):
assert not node.op.compute_uv
svd_counter += 1
assert svd_counter == 1
# Case 2 of 3. for a different full_matrices
f_4 = pytensor.function([a], [s_3])
nodes = f_4.maker.fgraph.apply_nodes
svd_counter = 0
for node in nodes:
if isinstance(node.op, SVD):
assert not node.op.compute_uv
assert node.op.full_matrices
svd_counter += 1
assert svd_counter == 1
# 4. No rewrite should happen
f_5 = pytensor.function([a], [u_4])
nodes = f_5.maker.fgraph.apply_nodes
svd_counter = 0
for node in nodes:
if isinstance(node.op, SVD):
assert node.op.full_matrices
assert node.op.compute_uv
svd_counter += 1
assert svd_counter == 1
def get_pt_function(x, op_name):
return getattr(pt.linalg, op_name)(x)
@pytest.mark.parametrize("inv_op_1", ["inv", "pinv"])
@pytest.mark.parametrize("inv_op_2", ["inv", "pinv"])
def test_inv_inv_rewrite(inv_op_1, inv_op_2):
x = pt.matrix("x")
op1 = get_pt_function(x, inv_op_1)
op2 = get_pt_function(op1, inv_op_2)
rewritten_out = rewrite_graph(op2)
assert rewritten_out == x
@pytest.mark.parametrize("inv_op", ["inv", "pinv"])
def test_inv_eye_to_eye(inv_op):
x = pt.eye(10)
x_inv = get_pt_function(x, inv_op)
f_rewritten = function([], x_inv, mode="FAST_RUN")
nodes = f_rewritten.maker.fgraph.apply_nodes
# Rewrite Test
valid_inverses = (MatrixInverse, MatrixPinv)
assert not any(isinstance(node.op, valid_inverses) for node in nodes)
# Value Test
x_test = np.eye(10)
x_inv_val = np.linalg.inv(x_test)
rewritten_val = f_rewritten()
assert_allclose(
x_inv_val,
rewritten_val,
atol=1e-3 if config.floatX == "float32" else 1e-8,
rtol=1e-3 if config.floatX == "float32" else 1e-8,
)
@pytest.mark.parametrize(
"shape",
[(), (7,), (7, 7), (5, 7, 7)],
ids=["scalar", "vector", "matrix", "batched"],
)
@pytest.mark.parametrize("inv_op", ["inv", "pinv"])
def test_inv_diag_from_eye_mul(shape, inv_op):
# Initializing x based on scalar/vector/matrix
x = pt.tensor("x", shape=shape)
x_diag = pt.eye(7) * x
# Calculating inverse using pt.linalg.inv
x_inv = get_pt_function(x_diag, inv_op)
# REWRITE TEST
f_rewritten = function([x], x_inv, mode="FAST_RUN")
nodes = f_rewritten.maker.fgraph.apply_nodes
valid_inverses = (MatrixInverse, MatrixPinv)
assert not any(isinstance(node.op, valid_inverses) for node in nodes)
# NUMERIC VALUE TEST
if len(shape) == 0:
x_test = np.array(np.random.rand()).astype(config.floatX)
elif len(shape) == 1:
x_test = np.random.rand(*shape).astype(config.floatX)
else:
x_test = np.random.rand(*shape).astype(config.floatX)
x_test_matrix = np.eye(7) * x_test
inverse_matrix = np.linalg.inv(x_test_matrix)
rewritten_inverse = f_rewritten(x_test)
assert_allclose(
inverse_matrix,
rewritten_inverse,
atol=ATOL,
rtol=RTOL,
)
@pytest.mark.parametrize("inv_op", ["inv", "pinv"])
def test_inv_diag_from_diag(inv_op):
x = pt.dvector("x")
x_diag = pt.diag(x)
x_inv = get_pt_function(x_diag, inv_op)
# REWRITE TEST
f_rewritten = function([x], x_inv, mode="FAST_RUN")
nodes = f_rewritten.maker.fgraph.apply_nodes
valid_inverses = (MatrixInverse, MatrixPinv)
assert not any(isinstance(node.op, valid_inverses) for node in nodes)
# NUMERIC VALUE TEST
x_test = np.random.rand(10)
x_test_matrix = np.eye(10) * x_test
inverse_matrix = np.linalg.inv(x_test_matrix)
rewritten_inverse = f_rewritten(x_test)
assert_allclose(
inverse_matrix,
rewritten_inverse,
atol=ATOL,
rtol=RTOL,
)
def test_diag_blockdiag_rewrite():
n_matrices = 10
matrix_size = (5, 5)
sub_matrices = pt.tensor("sub_matrices", shape=(n_matrices, *matrix_size))
bd_output = pt.linalg.block_diag(*[sub_matrices[i] for i in range(n_matrices)])
diag_output = pt.diag(bd_output)
f_rewritten = function([sub_matrices], diag_output, mode="FAST_RUN")
# Rewrite Test
nodes = f_rewritten.maker.fgraph.apply_nodes
assert not any(isinstance(node.op, BlockDiagonal) for node in nodes)
# Value Test
sub_matrices_test = np.random.rand(n_matrices, *matrix_size).astype(config.floatX)
bd_output_test = scipy.linalg.block_diag(
*[sub_matrices_test[i] for i in range(n_matrices)]
)
diag_output_test = np.diag(bd_output_test)
rewritten_val = f_rewritten(sub_matrices_test)
assert_allclose(
diag_output_test,
rewritten_val,
atol=1e-3 if config.floatX == "float32" else 1e-8,
rtol=1e-3 if config.floatX == "float32" else 1e-8,
)
def test_det_blockdiag_rewrite():
n_matrices = 100
matrix_size = (5, 5)
sub_matrices = pt.tensor("sub_matrices", shape=(n_matrices, *matrix_size))
bd_output = pt.linalg.block_diag(*[sub_matrices[i] for i in range(n_matrices)])
det_output = pt.linalg.det(bd_output)
f_rewritten = function([sub_matrices], det_output, mode="FAST_RUN")
# Rewrite Test
nodes = f_rewritten.maker.fgraph.apply_nodes
assert not any(isinstance(node.op, BlockDiagonal) for node in nodes)
# Value Test
sub_matrices_test = np.random.rand(n_matrices, *matrix_size).astype(config.floatX)
bd_output_test = scipy.linalg.block_diag(
*[sub_matrices_test[i] for i in range(n_matrices)]
)
det_output_test = np.linalg.det(bd_output_test)
rewritten_val = f_rewritten(sub_matrices_test)
assert_allclose(
det_output_test,
rewritten_val,
atol=1e-3 if config.floatX == "float32" else 1e-8,
rtol=1e-3 if config.floatX == "float32" else 1e-8,
)
def test_slogdet_blockdiag_rewrite():
n_matrices = 10
matrix_size = (5, 5)
sub_matrices = pt.tensor("sub_matrices", shape=(n_matrices, *matrix_size))
bd_output = pt.linalg.block_diag(*[sub_matrices[i] for i in range(n_matrices)])
sign_output, logdet_output = pt.linalg.slogdet(bd_output)
f_rewritten = function(
[sub_matrices], [sign_output, logdet_output], mode="FAST_RUN"
)
# Rewrite Test
nodes = f_rewritten.maker.fgraph.apply_nodes
assert not any(isinstance(node.op, BlockDiagonal) for node in nodes)
# Value Test
sub_matrices_test = np.random.rand(n_matrices, *matrix_size).astype(config.floatX)
bd_output_test = scipy.linalg.block_diag(
*[sub_matrices_test[i] for i in range(n_matrices)]
)
sign_output_test, logdet_output_test = np.linalg.slogdet(bd_output_test)
rewritten_sign_val, rewritten_logdet_val = f_rewritten(sub_matrices_test)
assert_allclose(
sign_output_test,
rewritten_sign_val,
atol=1e-3 if config.floatX == "float32" else 1e-8,
rtol=1e-3 if config.floatX == "float32" else 1e-8,
)
assert_allclose(
logdet_output_test,
rewritten_logdet_val,
atol=1e-3 if config.floatX == "float32" else 1e-8,
rtol=1e-3 if config.floatX == "float32" else 1e-8,
)
def test_diag_kronecker_rewrite():
a, b = pt.dmatrices("a", "b")
kron_prod = pt.linalg.kron(a, b)
diag_kron_prod = pt.diag(kron_prod)
f_rewritten = function([a, b], diag_kron_prod, mode="FAST_RUN")
# Rewrite Test
nodes = f_rewritten.maker.fgraph.apply_nodes
assert not any(isinstance(node.op, KroneckerProduct) for node in nodes)
# Value Test
a_test, b_test = np.random.rand(2, 20, 20)
kron_prod_test = np.kron(a_test, b_test)
diag_kron_prod_test = np.diag(kron_prod_test)
rewritten_val = f_rewritten(a_test, b_test)
assert_allclose(
diag_kron_prod_test,
rewritten_val,
atol=1e-3 if config.floatX == "float32" else 1e-8,
rtol=1e-3 if config.floatX == "float32" else 1e-8,
)
def test_det_kronecker_rewrite():
a, b = pt.dmatrices("a", "b")
kron_prod = pt.linalg.kron(a, b)
det_output = pt.linalg.det(kron_prod)
f_rewritten = function([a, b], [det_output], mode="FAST_RUN")
# Rewrite Test
nodes = f_rewritten.maker.fgraph.apply_nodes
assert not any(isinstance(node.op, KroneckerProduct) for node in nodes)
# Value Test
a_test, b_test = np.random.rand(2, 20, 20)
kron_prod_test = np.kron(a_test, b_test)
det_output_test = np.linalg.det(kron_prod_test)
rewritten_det_val = f_rewritten(a_test, b_test)
assert_allclose(
det_output_test,
rewritten_det_val,
atol=1e-3 if config.floatX == "float32" else 1e-8,
rtol=1e-3 if config.floatX == "float32" else 1e-8,
)
def test_slogdet_kronecker_rewrite():
a, b = pt.dmatrices("a", "b")
kron_prod = pt.linalg.kron(a, b)
sign_output, logdet_output = pt.linalg.slogdet(kron_prod)
f_rewritten = function([a, b], [sign_output, logdet_output], mode="FAST_RUN")
# Rewrite Test
nodes = f_rewritten.maker.fgraph.apply_nodes
assert not any(isinstance(node.op, KroneckerProduct) for node in nodes)
# Value Test
a_test, b_test = np.random.rand(2, 20, 20)
kron_prod_test = np.kron(a_test, b_test)
sign_output_test, logdet_output_test = np.linalg.slogdet(kron_prod_test)
rewritten_sign_val, rewritten_logdet_val = f_rewritten(a_test, b_test)
assert_allclose(
sign_output_test,
rewritten_sign_val,
atol=1e-3 if config.floatX == "float32" else 1e-8,
rtol=1e-3 if config.floatX == "float32" else 1e-8,
)
assert_allclose(
logdet_output_test,
rewritten_logdet_val,
atol=1e-3 if config.floatX == "float32" else 1e-8,
rtol=1e-3 if config.floatX == "float32" else 1e-8,
)
def test_cholesky_eye_rewrite():
x = pt.eye(10)
L = pt.linalg.cholesky(x)
f_rewritten = function([], L, mode="FAST_RUN")
nodes = f_rewritten.maker.fgraph.apply_nodes
# Rewrite Test
assert not any(isinstance(node.op, Cholesky) for node in nodes)
# Value Test
x_test = np.eye(10)
L = np.linalg.cholesky(x_test)
rewritten_val = f_rewritten()
assert_allclose(
L,
rewritten_val,
atol=1e-3 if config.floatX == "float32" else 1e-8,
rtol=1e-3 if config.floatX == "float32" else 1e-8,
)
@pytest.mark.parametrize(
"shape",
[(), (7,), (7, 7), (5, 7, 7)],
ids=["scalar", "vector", "matrix", "batched"],
)
def test_cholesky_diag_from_eye_mul(shape):
# Initializing x based on scalar/vector/matrix
x = pt.tensor("x", shape=shape)
y = pt.eye(7) * x
# Performing cholesky decomposition using pt.linalg.cholesky
z_cholesky = pt.linalg.cholesky(y)
# REWRITE TEST
f_rewritten = function([x], z_cholesky, mode="FAST_RUN")
nodes = f_rewritten.maker.fgraph.apply_nodes
assert not any(isinstance(node.op, Cholesky) for node in nodes)
# NUMERIC VALUE TEST
if len(shape) == 0:
x_test = np.array(np.random.rand()).astype(config.floatX)
elif len(shape) == 1:
x_test = np.random.rand(*shape).astype(config.floatX)
else:
x_test = np.random.rand(*shape).astype(config.floatX)
x_test_matrix = np.eye(7) * x_test
cholesky_val = np.linalg.cholesky(x_test_matrix)
rewritten_val = f_rewritten(x_test)
assert_allclose(
cholesky_val,
rewritten_val,
atol=1e-3 if config.floatX == "float32" else 1e-8,
rtol=1e-3 if config.floatX == "float32" else 1e-8,
)
def test_cholesky_diag_from_diag():
x = pt.dvector("x")
x_diag = pt.diag(x)
x_cholesky = pt.linalg.cholesky(x_diag)
# REWRITE TEST
f_rewritten = function([x], x_cholesky, mode="FAST_RUN")
nodes = f_rewritten.maker.fgraph.apply_nodes
assert not any(isinstance(node.op, Cholesky) for node in nodes)
# NUMERIC VALUE TEST
x_test = np.random.rand(10)
x_test_matrix = np.eye(10) * x_test
cholesky_val = np.linalg.cholesky(x_test_matrix)
rewritten_cholesky = f_rewritten(x_test)
assert_allclose(
cholesky_val,
rewritten_cholesky,
atol=1e-3 if config.floatX == "float32" else 1e-8,
rtol=1e-3 if config.floatX == "float32" else 1e-8,
)
def test_rewrite_cholesky_diag_to_sqrt_diag_not_applied():
# Case 1 : y is not a diagonal matrix because of k = -1
x = pt.tensor("x", shape=(7, 7))
y = pt.eye(7, k=-1) * x
z_cholesky = pt.linalg.cholesky(y)
# REWRITE TEST (should not be applied)
f_rewritten = function([x], z_cholesky, mode="FAST_RUN")
nodes = f_rewritten.maker.fgraph.apply_nodes
assert any(isinstance(node.op, Cholesky) for node in nodes)
# Case 2 : eye is degenerate
x = pt.scalar("x")
y = pt.eye(1) * x
z_cholesky = pt.linalg.cholesky(y)
f_rewritten = function([x], z_cholesky, mode="FAST_RUN")
nodes = f_rewritten.maker.fgraph.apply_nodes
assert any(isinstance(node.op, Cholesky) for node in nodes)
def test_slogdet_specialization():
x, a = pt.dmatrix("x"), np.random.rand(20, 20)
det_x, det_a = pt.linalg.det(x), np.linalg.det(a)
log_abs_det_x, log_abs_det_a = pt.log(pt.abs(det_x)), np.log(np.abs(det_a))
log_det_x, log_det_a = pt.log(det_x), np.log(det_a)
sign_det_x, sign_det_a = pt.sign(det_x), np.sign(det_a)
exp_det_x = pt.exp(det_x)
# REWRITE TESTS
# sign(det(x))
f = function([x], [sign_det_x], mode="FAST_RUN")
nodes = f.maker.fgraph.apply_nodes
assert len([node for node in nodes if isinstance(node.op, SLogDet)]) == 1
assert not any(isinstance(node.op, Det) for node in nodes)
rw_sign_det_a = f(a)
assert_allclose(
sign_det_a,
rw_sign_det_a,
atol=1e-3 if config.floatX == "float32" else 1e-8,
rtol=1e-3 if config.floatX == "float32" else 1e-8,
)
# log(abs(det(x)))
f = function([x], [log_abs_det_x], mode="FAST_RUN")
nodes = f.maker.fgraph.apply_nodes
assert len([node for node in nodes if isinstance(node.op, SLogDet)]) == 1
assert not any(isinstance(node.op, Det) for node in nodes)
rw_log_abs_det_a = f(a)
assert_allclose(
log_abs_det_a,
rw_log_abs_det_a,
atol=1e-3 if config.floatX == "float32" else 1e-8,
rtol=1e-3 if config.floatX == "float32" else 1e-8,
)
# log(det(x))
f = function([x], [log_det_x], mode="FAST_RUN")
nodes = f.maker.fgraph.apply_nodes
assert len([node for node in nodes if isinstance(node.op, SLogDet)]) == 1
assert not any(isinstance(node.op, Det) for node in nodes)
rw_log_det_a = f(a)
assert_allclose(
log_det_a,
rw_log_det_a,
atol=1e-3 if config.floatX == "float32" else 1e-8,
rtol=1e-3 if config.floatX == "float32" else 1e-8,
)
# More than 1 valid function
f = function([x], [sign_det_x, log_abs_det_x], mode="FAST_RUN")
nodes = f.maker.fgraph.apply_nodes
assert len([node for node in nodes if isinstance(node.op, SLogDet)]) == 1
assert not any(isinstance(node.op, Det) for node in nodes)
# Other functions (rewrite shouldnt be applied to these)
# Only invalid functions
f = function([x], [exp_det_x], mode="FAST_RUN")
nodes = f.maker.fgraph.apply_nodes
assert not any(isinstance(node.op, SLogDet) for node in nodes)
# Invalid + Valid function
f = function([x], [exp_det_x, sign_det_x], mode="FAST_RUN")
nodes = f.maker.fgraph.apply_nodes
assert not any(isinstance(node.op, SLogDet) for node in nodes)