Skip to content

Rewrite determinant of diagonal matrix as product of diagonal #797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 34 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
82b7e98
Added det-diag rewrite
tanish1729 Jun 2, 2024
e661ba4
fixed pt.diagonal error
tanish1729 Jun 2, 2024
a1823e9
Added test for rewrite
tanish1729 Jun 5, 2024
ac21b9e
Added test for rewrite
tanish1729 Jun 5, 2024
01e47d7
fixed test
tanish1729 Jun 5, 2024
89f209f
added check for verifying rewrite
tanish1729 Jun 6, 2024
0bd2e26
fixed other failing test
tanish1729 Jun 6, 2024
170a860
added docstring
tanish1729 Jun 7, 2024
0191b97
updated docstring
tanish1729 Jun 7, 2024
7f7c803
fixed mypy error
tanish1729 Jun 7, 2024
8d7f9f7
added det_diag_from_diag and test
tanish1729 Jun 9, 2024
9ad0606
fixed node rewriter name
tanish1729 Jun 9, 2024
c897296
added row/col tests
tanish1729 Jun 11, 2024
6c3cdae
updated check for eye
tanish1729 Jun 11, 2024
6b58cfb
updated rewrite and tests
tanish1729 Jun 15, 2024
4316e75
added check for eye_input and new test for cases where not to apply r…
tanish1729 Jun 21, 2024
20c8505
Merge branch 'pymc-devs:main' into det-diag-rewrite
tanish1729 Jun 22, 2024
6743e0a
does not apply rewrite to specific cases
tanish1729 Jun 22, 2024
ebca339
typecasted test variable
tanish1729 Jun 22, 2024
58212b6
typecast variables
tanish1729 Jun 23, 2024
3ef186d
removed shape known check; fails for rectangle eye
tanish1729 Jun 24, 2024
2c96faf
added new tests for (1,1) eye and rectangle eye
tanish1729 Jun 24, 2024
9a48a72
added helper function for diag from eye_mul
tanish1729 Jun 25, 2024
ffc0f81
updated case for no rewrite which was failing tests
tanish1729 Jun 25, 2024
9f661e3
cleaned code; updated rectangle_eye test which is an invalid rewrite
tanish1729 Jun 26, 2024
b02818d
add check for k in pt.eye
tanish1729 Jun 26, 2024
0dbc28e
Update pytensor/tensor/rewriting/linalg.py
tanish1729 Jun 26, 2024
d4ffb7f
typecasted det_val
tanish1729 Jun 26, 2024
060863a
Merge branch 'det-diag-rewrite' of https://github.com/tanish1729/pyte…
tanish1729 Jun 26, 2024
557fea1
fixed final typecasting
tanish1729 Jun 26, 2024
12ffb8f
Merge branch 'main' into det-diag-rewrite
tanish1729 Jun 30, 2024
27a9864
fixed merge
tanish1729 Jul 1, 2024
6831069
fixed failing rectangle eye test
tanish1729 Jul 1, 2024
9811b88
fixed typo
tanish1729 Jul 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions pytensor/tensor/rewriting/linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
from pytensor import Variable
from pytensor.graph import Apply, FunctionGraph
from pytensor.graph.rewriting.basic import copy_stack_trace, node_rewriter
from pytensor.tensor.basic import TensorVariable, diagonal
from pytensor.scalar.basic import Mul
from pytensor.tensor.basic import Eye, TensorVariable, diagonal
from pytensor.tensor.blas import Dot22
from pytensor.tensor.blockwise import Blockwise
from pytensor.tensor.elemwise import DimShuffle
from pytensor.tensor.elemwise import DimShuffle, Elemwise
from pytensor.tensor.math import Dot, Prod, _matrix_matrix_matmul, log, prod
from pytensor.tensor.nlinalg import (
Det,
KroneckerProduct,
MatrixInverse,
MatrixPinv,
Expand Down Expand Up @@ -377,3 +379,54 @@ def local_lift_through_linalg(
return [block_diag(*inner_matrices)]
else:
raise NotImplementedError # pragma: no cover


# Det Diag Rewrite
@register_canonicalize
@register_stabilize
@node_rewriter([det])
def det_diag_rewrite(fgraph, node):
# Find if we have Blockwise Op
if not (isinstance(node.op, Blockwise) and isinstance(node.op.core_op, Det)):
return None

node_input_op = node.inputs[0]
# Check if the op is Elemwise and mul
if not (
isinstance(node_input_op.owner.op, Elemwise)
and isinstance(node_input_op.owner.op.scalar_op, Mul)
):
return None

# Find whether any of the inputs to mul is Eye
inputs_to_mul = node_input_op.owner.inputs
eye_check = False
eye_input = []
for mul_input in inputs_to_mul:
if isinstance(mul_input.owner.op, Eye):
eye_check = True
eye_input.append(mul_input)
break # This is so that we only find the first one? Can be changed
if not (eye_check):
return None

# Get all non Eye inputs (scalars/matrices/vectors)
non_eye_inputs = list(set(inputs_to_mul) - set(eye_input))

# Dealing with only one other input
if len(non_eye_inputs) >= 2:
raise NotImplementedError

# Checking if original x was matrix
if not non_eye_inputs[0].owner:
# It was a matrix
det_val = non_eye_inputs[0].diagonal().prod()
else:
# Check for scalar (ndim = 0) or vector (ndim = 1)
sca_vec_input = non_eye_inputs[0].owner.inputs[0]
if sca_vec_input.ndim == 0:
det_val = sca_vec_input ** eye_input[0].shape[0]
else:
det_val = sca_vec_input.prod()

return [det_val]
34 changes: 34 additions & 0 deletions tests/tensor/rewriting/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,37 @@ def test_local_lift_through_linalg(constructor, f_op, f, g_op, g):
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,), (7, 7)], ids=["scalar", "vector", "matrix"]
)
def test_det_diag_rewrite(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) for node in nodes)

# NUMERIC VALUE TEST
f_det = function([x], z_det)
if len(shape) == 0:
x_test = np.random.rand()
elif len(shape) == 1:
x_test = np.random.rand(7)
else:
x_test = np.random.rand(7, 7)
det_val = f_det(x_test)
rewritten_val = f_rewritten(x_test)

assert_allclose(
det_val,
rewritten_val,
atol=1e-4 if config.floatX == "float32" else 1e-8,
rtol=1e-4 if config.floatX == "float32" else 1e-8,
)
Loading