-
Notifications
You must be signed in to change notification settings - Fork 149
Fix lu_solve with batch inputs #1394
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -6,7 +6,6 @@ | |||||
| import pytest | ||||||
| import scipy | ||||||
|
|
||||||
| import pytensor | ||||||
| from pytensor import function, grad | ||||||
| from pytensor import tensor as pt | ||||||
| from pytensor.configdefaults import config | ||||||
|
|
@@ -130,7 +129,7 @@ def test_cholesky_grad_indef(): | |||||
|
|
||||||
| def test_cholesky_infer_shape(): | ||||||
| x = matrix() | ||||||
| f_chol = pytensor.function([x], [cholesky(x).shape, cholesky(x, lower=False).shape]) | ||||||
| f_chol = function([x], [cholesky(x).shape, cholesky(x, lower=False).shape]) | ||||||
| if config.mode != "FAST_COMPILE": | ||||||
| topo_chol = f_chol.maker.fgraph.toposort() | ||||||
| f_chol.dprint() | ||||||
|
|
@@ -313,7 +312,7 @@ def test_solve_correctness( | |||||
| b_ndim=len(b_size), | ||||||
| ) | ||||||
|
|
||||||
| solve_func = pytensor.function([A, b], y) | ||||||
| solve_func = function([A, b], y) | ||||||
| X_np = solve_func(A_val.copy(), b_val.copy()) | ||||||
|
|
||||||
| ATOL = 1e-8 if config.floatX.endswith("64") else 1e-4 | ||||||
|
|
@@ -444,7 +443,7 @@ def test_correctness(self, b_shape: tuple[int], lower, trans, unit_diagonal): | |||||
| b_ndim=len(b_shape), | ||||||
| ) | ||||||
|
|
||||||
| f = pytensor.function([A, b], x) | ||||||
| f = function([A, b], x) | ||||||
|
|
||||||
| x_pt = f(A_val, b_val) | ||||||
| x_sp = scipy.linalg.solve_triangular( | ||||||
|
|
@@ -508,8 +507,8 @@ def test_infer_shape(self): | |||||
| A = matrix() | ||||||
| b = matrix() | ||||||
| self._compile_and_check( | ||||||
| [A, b], # pytensor.function inputs | ||||||
| [self.op_class(b_ndim=2)(A, b)], # pytensor.function outputs | ||||||
| [A, b], # function inputs | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These comments are silly anyway, just use a keyword argument?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was a find replace spill over to comments
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comments are still stupid >:(
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But I didn't add them :D |
||||||
| [self.op_class(b_ndim=2)(A, b)], # function outputs | ||||||
| # A must be square | ||||||
| [ | ||||||
| np.asarray(rng.random((5, 5)), dtype=config.floatX), | ||||||
|
|
@@ -522,8 +521,8 @@ def test_infer_shape(self): | |||||
| A = matrix() | ||||||
| b = vector() | ||||||
| self._compile_and_check( | ||||||
| [A, b], # pytensor.function inputs | ||||||
| [self.op_class(b_ndim=1)(A, b)], # pytensor.function outputs | ||||||
| [A, b], # function inputs | ||||||
| [self.op_class(b_ndim=1)(A, b)], # function outputs | ||||||
| # A must be square | ||||||
| [ | ||||||
| np.asarray(rng.random((5, 5)), dtype=config.floatX), | ||||||
|
|
@@ -538,10 +537,10 @@ def test_solve_correctness(self): | |||||
| A = matrix() | ||||||
| b = matrix() | ||||||
| y = self.op_class(lower=True, b_ndim=2)(A, b) | ||||||
| cho_solve_lower_func = pytensor.function([A, b], y) | ||||||
| cho_solve_lower_func = function([A, b], y) | ||||||
|
|
||||||
| y = self.op_class(lower=False, b_ndim=2)(A, b) | ||||||
| cho_solve_upper_func = pytensor.function([A, b], y) | ||||||
| cho_solve_upper_func = function([A, b], y) | ||||||
|
|
||||||
| b_val = np.asarray(rng.random((5, 1)), dtype=config.floatX) | ||||||
|
|
||||||
|
|
@@ -603,7 +602,7 @@ def test_lu_decomposition( | |||||
| A = tensor("A", shape=shape, dtype=dtype) | ||||||
| out = lu(A, permute_l=permute_l, p_indices=p_indices) | ||||||
|
|
||||||
| f = pytensor.function([A], out) | ||||||
| f = function([A], out) | ||||||
|
|
||||||
| rng = np.random.default_rng(utt.fetch_seed()) | ||||||
| x = rng.normal(size=shape).astype(config.floatX) | ||||||
|
|
@@ -706,7 +705,7 @@ def test_lu_solve(self, b_shape: tuple[int], trans): | |||||
|
|
||||||
| x = self.factor_and_solve(A, b, trans=trans, sum=False) | ||||||
|
|
||||||
| f = pytensor.function([A, b], x) | ||||||
| f = function([A, b], x) | ||||||
| x_pt = f(A_val.copy(), b_val.copy()) | ||||||
| x_sp = scipy.linalg.lu_solve( | ||||||
| scipy.linalg.lu_factor(A_val.copy()), b_val.copy(), trans=trans | ||||||
|
|
@@ -738,13 +737,29 @@ def test_lu_solve_gradient(self, b_shape: tuple[int], trans: bool): | |||||
| test_fn = functools.partial(self.factor_and_solve, sum=True, trans=trans) | ||||||
| utt.verify_grad(test_fn, [A_val, b_val], 3, rng) | ||||||
|
|
||||||
| def test_lu_solve_batch_dims(self): | ||||||
| A = pt.tensor("A", shape=(3, 1, 5, 5)) | ||||||
| b = pt.tensor("b", shape=(1, 4, 5)) | ||||||
| lu_and_pivots = lu_factor(A) | ||||||
| x = lu_solve(lu_and_pivots, b, b_ndim=1) | ||||||
| assert x.type.shape in {(3, 4, None), (3, 4, 5)} | ||||||
|
||||||
| assert x.type.shape in {(3, 4, None), (3, 4, 5)} | |
| assert x.type.shape == (3, 4, 5) or (x.type.shape[:2] == (3, 4) and x.type.shape[2] is None) |
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.
This was on purpose, right now it returns None, but if we improve static type it should return 5 and won't make the test fail
Uh oh!
There was an error while loading. Please reload this page.