Skip to content

Conversation

@ricardoV94
Copy link
Member

@ricardoV94 ricardoV94 commented Dec 10, 2025

The old local_det_chol rewrite is extended to cover more cases of a matrix that is factorized elsewhere, not just with Cholesky, but also LU, LUFactor, or SVD, QR (the latter two only if the sign isn't needed)

A new rewrite is added for the determinant of a factorization itself. The logic is slightly different, for instance det(LUFactor) is non-sensical, and the determinant for some outputs of SVD/ QR can always be computed even if the determinant of the whole factorization cannot.

Also extended the rewrite of log(prod(x)) to sum(log(x)), which should increase the stability of many of these when we want the log determinant (or log(abs(determintant))).

Still missing tests

Closes #1679
Related to #573

@ricardoV94 ricardoV94 added linalg Linear algebra enhancement New feature or request graph rewriting labels Dec 10, 2025
match core_op:
case Cholesky():
L = client.outputs[0]
new_det = matrix_diagonal_product(L) ** 2
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Add the positive tag here.

Possibly also rewrite for log(x ** 2) -> log(x) * 2, when we know x is positive

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all seemed out of scope so I didn't address it. Positive tagging isn't used systematically and I'd rather we have a plan for doing that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

@ricardoV94
Copy link
Member Author

Still missing tests and a couple other things. I wanted to first get #1786 in (which I think is still not ready either)

@jessegrabowski
Copy link
Member

I approved to poke you towards finishing it :)

@jessegrabowski
Copy link
Member

I used claude to add tests. I think it did a reasonable job.

@jessegrabowski
Copy link
Member

I also rebased, so if you want to keep working on it be aware of that.

@jessegrabowski jessegrabowski marked this pull request as ready for review January 4, 2026 19:11
assert_equal_computations([rewritten], [expected])


def test_local_log_prod_to_sum_log_no_rewrite():
Copy link
Member Author

@ricardoV94 ricardoV94 Jan 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: this can be part of the first test above, as another parametrization where the expected graph is the same as the original one

]
assert len(det_nodes_no_opt) == 1

fn_opt = function(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where these tests hard to pattern match with the rewrite_graph -> assert_equal_computations mold?

assert_equal_computations([rewritten], [expected])


def test_det_of_factorized_matrix_no_rewrite_without_abs():
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same nit about being a parametrization above. I am not sure myself I prefer that way, just raising if you have a clear preference


# Test graph that only has det_X
f = function([X], [det_X])
f.dprint()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably added by me

Suggested change
f.dprint()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request graph rewriting linalg Linear algebra

Projects

None yet

Development

Successfully merging this pull request may close these issues.

local_det_chol doesn't work

2 participants