Skip to content

Commit

Permalink
Use actual Solve Op to infer output dtype
Browse files Browse the repository at this point in the history
CholSolve outputs a different dtype than basic Solve in Scipy==1.15
  • Loading branch information
ricardoV94 committed Jan 10, 2025
1 parent 33c9400 commit 15c06ff
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 4 additions & 3 deletions pytensor/tensor/slinalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,10 @@ def make_node(self, A, b):
raise ValueError(f"`b` must have {self.b_ndim} dims; got {b.type} instead.")

# Infer dtype by solving the most simple case with 1x1 matrices
o_dtype = scipy.linalg.solve(
np.eye(1).astype(A.dtype), np.eye(1).astype(b.dtype)
).dtype
inp_arr = [np.eye(1).astype(A.dtype), np.eye(1).astype(b.dtype)]
out_arr = [[None]]
self.perform(None, inp_arr, out_arr)
o_dtype = out_arr[0][0].dtype
x = tensor(dtype=o_dtype, shape=b.type.shape)
return Apply(self, [A, b], [x])

Expand Down
2 changes: 1 addition & 1 deletion tests/tensor/test_slinalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ def test_solve_dtype(self):
fn = function([A, b], x)
x_result = fn(A_val.astype(A_dtype), b_val.astype(b_dtype))

assert x.dtype == x_result.dtype
assert x.dtype == x_result.dtype, (A_dtype, b_dtype)


def test_cho_solve():
Expand Down

0 comments on commit 15c06ff

Please sign in to comment.