-
Notifications
You must be signed in to change notification settings - Fork 137
Rewrite solves involving kron to eliminate kron #1559
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR introduces a rewrite optimization that eliminates Kronecker products from linear solve operations by transforming solve(kron(A, B), x)
to an equivalent expression using nested solves. This provides significant performance improvements, especially for larger matrices, by avoiding the computational cost of forming the Kronecker product.
Key changes:
- Adds
rewrite_solve_kron_to_solve
function that rewrites solve operations involving Kronecker products - Includes comprehensive tests covering different matrix shapes, solve types, and batch operations
- Handles both regular and batched Kronecker products through Blockwise operations
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
File | Description |
---|---|
pytensor/tensor/rewriting/linalg.py | Implements the core rewrite logic to transform solve(kron(A,B), x) to nested solve operations |
tests/tensor/rewriting/test_linalg.py | Adds comprehensive test coverage for the new rewrite functionality |
Comments suppressed due to low confidence (1)
pytensor/tensor/rewriting/linalg.py:896
- Missing parentheses around the condition. The line should be
(isinstance(A.owner.op, Blockwise)
to properly group with theand
operator on the next line.
or isinstance(A.owner.op, Blockwise)
solve_op = node.op | ||
props_dict = solve_op.core_op._props_dict() | ||
|
||
if props_dict["b_ndim"] != 1: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
b_ndim=2 is just a batched vector solve, could you move it to a batch dimension of b?
import numpy as np
import pytensor.tensor as pt
a = pt.matrix("a", shape=(3, 3))
b = pt.matrix("b", shape=(3, 5))
out1 = pt.linalg.solve(a, b, b_ndim=2)
out2 = pt.moveaxis(pt.linalg.solve(a, pt.moveaxis(b, -1, 0), b_ndim=1), 0, -1)
rng = np.random.default_rng(20)
test_dict = {
a: rng.normal(size=(a.type.shape)),
b: rng.normal(size=(b.type.shape))
}
np.testing.assert_allclose(out1.eval(test_dict), out2.eval(test_dict))
Description
Rewrite graphs of the form
solve(kron(A, B), x)
tosolve(A, solve(B, x.reshape).mT).mT.reshape
. This eliminates the kronecker product, and provides significant speedup.Important limitation is that it only covers the case when
b_ndim=1
, because the math underpinning the rewrite requires thatx
is a vector. This is still an important case, however, because it's what arises in the logp of a multivariate normal when the covariance matrix is kronecker.Also I hit what appears to be a numerical bug in the batch case when
assume_a = 'pos'
. There is disagreement, but only in the 2nd row of the outputs. No matter the batch size, it's always the 2nd batch that has a numerical problem -- all other batches agree. I've left in the failing test for now. We don't even vectorize kron by default, so if I can't figure it out I might just disable the rewrite for the Blockwise(Kron) case for now.Benchmarks follow, with:
Related Issue
Solve
involvingKron
#1557Checklist
Type of change
📚 Documentation preview 📚: https://pytensor--1559.org.readthedocs.build/en/1559/