2
2
from collections .abc import Callable
3
3
from typing import cast
4
4
5
- import pytensor .tensor as pt
6
5
from pytensor import Variable
7
6
from pytensor import tensor as pt
8
7
from pytensor .graph import Apply , FunctionGraph
@@ -893,7 +892,7 @@ def rewrite_slogdet_kronecker(fgraph, node):
893
892
@register_canonicalize
894
893
@register_stabilize
895
894
@node_rewriter ([Blockwise ])
896
- def rewrite_cholesky_eye_to_eye (fgraph , node ):
895
+ def rewrite_remove_useless_cholesky (fgraph , node ):
897
896
"""
898
897
This rewrite takes advantage of the fact that the cholesky decomposition of an identity matrix is the matrix itself
899
898
@@ -916,14 +915,15 @@ def rewrite_cholesky_eye_to_eye(fgraph, node):
916
915
return None
917
916
918
917
# Check whether input to Cholesky is Eye and the 1's are on main diagonal
919
- eye_check = node .inputs [0 ]
918
+ potential_eye = node .inputs [0 ]
920
919
if not (
921
- eye_check .owner
922
- and isinstance (eye_check .owner .op , Eye )
923
- and getattr (eye_check .owner .inputs [- 1 ], "data" , - 1 ).item () == 0
920
+ potential_eye .owner
921
+ and isinstance (potential_eye .owner .op , Eye )
922
+ and hasattr (potential_eye .owner .inputs [- 1 ], "data" )
923
+ and potential_eye .owner .inputs [- 1 ].data .item () == 0
924
924
):
925
925
return None
926
- return [eye_check ]
926
+ return [potential_eye ]
927
927
928
928
929
929
@register_canonicalize
@@ -941,10 +941,9 @@ def rewrite_cholesky_diag_to_sqrt_diag(fgraph, node):
941
941
and isinstance (inputs .owner .op , AllocDiag )
942
942
and AllocDiag .is_offset_zero (inputs .owner )
943
943
):
944
- cholesky_input = inputs .owner .inputs [0 ]
945
- if cholesky_input .type .ndim == 1 :
946
- cholesky_val = pt .diag (cholesky_input ** 0.5 )
947
- return [cholesky_val ]
944
+ diag_input = inputs .owner .inputs [0 ]
945
+ cholesky_val = pt .diag (diag_input ** 0.5 )
946
+ return [cholesky_val ]
948
947
949
948
# Check if the input is an elemwise multiply with identity matrix -- this also results in a diagonal matrix
950
949
inputs_or_none = _find_diag_from_eye_mul (inputs )
@@ -962,8 +961,6 @@ def rewrite_cholesky_diag_to_sqrt_diag(fgraph, node):
962
961
# Now, we can simply return the matrix consisting of sqrt values of the original diagonal elements
963
962
# For a matrix, we have to first extract the diagonal (non-zero values) and then only use those
964
963
if non_eye_input .type .broadcastable [- 2 :] == (False , False ):
965
- # For Matrix
966
- return [eye_input * (non_eye_input .diagonal (axis1 = - 1 , axis2 = - 2 ) ** 0.5 )]
967
- else :
968
- # For Vector or Scalar
969
- return [eye_input * (non_eye_input ** 0.5 )]
964
+ non_eye_input = non_eye_input .diagonal (axis1 = - 1 , axis2 = - 2 )
965
+
966
+ return [eye_input * (non_eye_input ** 0.5 )]
0 commit comments