From 70685b51b4b7ebe98966091c490b501f9532d5ca Mon Sep 17 00:00:00 2001 From: DHANUSH N Date: Thu, 17 Aug 2023 21:39:45 +0530 Subject: [PATCH] paddle frontend cholesky_solve added (#21291) Co-authored-by: @AnnaTz --- .../frontends/paddle/tensor/linalg.py | 10 ++++ .../test_paddle/test_tensor/test_linalg.py | 48 ++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/ivy/functional/frontends/paddle/tensor/linalg.py b/ivy/functional/frontends/paddle/tensor/linalg.py index 82fbcefb51ac8..126961573e701 100644 --- a/ivy/functional/frontends/paddle/tensor/linalg.py +++ b/ivy/functional/frontends/paddle/tensor/linalg.py @@ -122,6 +122,16 @@ def solve(x1, x2, name=None): return ivy.solve(x1, x2) +# cholesky_solve +@with_supported_dtypes({"2.5.1 and below": ("float32", "float64")}, "paddle") +@to_ivy_arrays_and_back +def cholesky_solve(x, y, /, *, upper=False, name=None): + if upper: + y = ivy.matrix_transpose(y) + Y = ivy.solve(y, x) + return ivy.solve(ivy.matrix_transpose(y), Y) + + # cholesky @with_supported_dtypes({"2.5.1 and below": ("float32", "float64")}, "paddle") @to_ivy_arrays_and_back diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_linalg.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_linalg.py index ffed896c2469d..bd55ed23c4ccd 100644 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_linalg.py +++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_linalg.py @@ -7,7 +7,10 @@ import ivy_tests.test_ivy.helpers as helpers from ivy_tests.test_ivy.helpers import assert_all_close from ivy_tests.test_ivy.helpers import handle_frontend_test, matrix_is_stable - +from ivy_tests.test_ivy.test_frontends.test_tensorflow.test_linalg import ( + _get_second_matrix, + _get_cholesky_matrix, +) # Helpers # # ------ # @@ -528,6 +531,49 @@ def test_paddle_solve( ) +# cholesky_solve +@st.composite +def _get_paddle_cholesky_matrix(draw): + input_dtype, spd_chol = draw(_get_cholesky_matrix()) + probability = draw(st.floats(min_value=0, max_value=1)) + if probability > 0.5: + spd_chol = spd_chol.T # randomly transpose the matrix + return input_dtype, spd_chol + + +@handle_frontend_test( + fn_tree="paddle.tensor.linalg.cholesky_solve", + x=_get_second_matrix(), + y=_get_paddle_cholesky_matrix(), + test_with_out=st.just(False), +) +def test_paddle_cholesky_solve( + *, + x, + y, + frontend, + backend_fw, + test_flags, + fn_tree, + on_device, +): + input_dtype1, x1 = x + input_dtype2, x2 = y + helpers.test_frontend_function( + input_dtypes=[input_dtype1, input_dtype2], + frontend=frontend, + backend_to_test=backend_fw, + test_flags=test_flags, + fn_tree=fn_tree, + on_device=on_device, + rtol=1e-3, + atol=1e-3, + x=x1, + y=x2, + upper=np.array_equal(x2, np.triu(x2)), # check whether the matrix is upper + ) + + # cholesky @handle_frontend_test( fn_tree="paddle.tensor.linalg.cholesky",