From c370d33a5f986c4b45781fa06a63bcb94ba9af7d Mon Sep 17 00:00:00 2001 From: DHANUSH N Date: Fri, 4 Aug 2023 21:07:22 +0530 Subject: [PATCH 1/6] paddle cholesky solve added --- ivy/functional/frontends/paddle/tensor/linalg.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ivy/functional/frontends/paddle/tensor/linalg.py b/ivy/functional/frontends/paddle/tensor/linalg.py index 82fbcefb51ac8..6bf8b7f992a68 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 +@to_ivy_arrays_and_back +@with_supported_dtypes({"2.5.1 and below": ("float32", "float64")}, "paddle") +def cholesky_solve(x, y, /, *, upper=False, name=None): + if upper: + x = ivy.matrix_transpose(x) + Y = ivy.solve(x, y) + return ivy.solve(ivy.matrix_transpose(x), Y) + + # cholesky @with_supported_dtypes({"2.5.1 and below": ("float32", "float64")}, "paddle") @to_ivy_arrays_and_back From e9fd6fd08e0e0ee69828ca26ea1aaa91119c3c74 Mon Sep 17 00:00:00 2001 From: DHANUSH N Date: Fri, 4 Aug 2023 21:19:03 +0530 Subject: [PATCH 2/6] paddle cholesky_solve frontend test added --- .../test_paddle/test_tensor/test_linalg.py | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) 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..f6b72f3606c9b 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_cholesky_matrix, + _get_second_matrix, +) # Helpers # # ------ # @@ -528,6 +531,42 @@ def test_paddle_solve( ) +# cholesky_solve +@handle_frontend_test( + fn_tree="paddle.tensor.linalg.cholesky_solve", + x=_get_cholesky_matrix(), + y=_get_second_matrix(), + test_with_out=st.just(False), + upper=st.booleans(), +) +def test_paddle_cholesky_solve( + *, + x, + y, + upper, + 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=upper, + ) + + # cholesky @handle_frontend_test( fn_tree="paddle.tensor.linalg.cholesky", From a881f2370134a65743c13f46b3a9d3ff1f02843b Mon Sep 17 00:00:00 2001 From: DHANUSH N Date: Fri, 4 Aug 2023 22:27:20 +0530 Subject: [PATCH 3/6] modified test file --- .../frontends/paddle/tensor/linalg.py | 2 +- .../test_paddle/test_tensor/test_linalg.py | 53 ++++++++++++++++--- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/ivy/functional/frontends/paddle/tensor/linalg.py b/ivy/functional/frontends/paddle/tensor/linalg.py index 6bf8b7f992a68..727488d845ab0 100644 --- a/ivy/functional/frontends/paddle/tensor/linalg.py +++ b/ivy/functional/frontends/paddle/tensor/linalg.py @@ -125,7 +125,7 @@ def solve(x1, x2, name=None): # cholesky_solve @to_ivy_arrays_and_back @with_supported_dtypes({"2.5.1 and below": ("float32", "float64")}, "paddle") -def cholesky_solve(x, y, /, *, upper=False, name=None): +def cholesky_solve(y, x, /, *, upper=False, name=None): if upper: x = ivy.matrix_transpose(x) Y = ivy.solve(x, y) 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 f6b72f3606c9b..0c17c49bab02d 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 @@ -2,15 +2,12 @@ import ivy from hypothesis import strategies as st, assume import numpy as np +import sys # local 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_cholesky_matrix, - _get_second_matrix, -) # Helpers # # ------ # @@ -532,6 +529,50 @@ def test_paddle_solve( # cholesky_solve +@st.composite +def _get_cholesky_matrix(draw): + # batch_shape, random_size, shared + input_dtype = draw( + st.shared( + st.sampled_from(draw(helpers.get_dtypes("float"))), + key="shared_dtype", + ) + ) + shared_size = draw( + st.shared(helpers.ints(min_value=2, max_value=4), key="shared_size") + ) + gen = draw( + helpers.array_values( + dtype=input_dtype, + shape=tuple([shared_size, shared_size]), + min_value=2, + max_value=5, + ).filter(lambda x: np.linalg.cond(x.tolist()) < 1 / sys.float_info.epsilon) + ) + spd = np.matmul(gen.T, gen) + np.identity(gen.shape[0]) + spd_chol = np.linalg.cholesky(spd) + return input_dtype, spd_chol + + +@st.composite +def _get_second_matrix(draw): + # batch_shape, shared, random_size + input_dtype = draw( + st.shared( + st.sampled_from(draw(helpers.get_dtypes("float"))), + key="shared_dtype", + ) + ) + shared_size = draw( + st.shared(helpers.ints(min_value=2, max_value=4), key="shared_size") + ) + return input_dtype, draw( + helpers.array_values( + dtype=input_dtype, shape=tuple([shared_size, 1]), min_value=2, max_value=5 + ) + ) + + @handle_frontend_test( fn_tree="paddle.tensor.linalg.cholesky_solve", x=_get_cholesky_matrix(), @@ -561,8 +602,8 @@ def test_paddle_cholesky_solve( on_device=on_device, rtol=1e-3, atol=1e-3, - x=x1, - y=x2, + x=x2, + y=x1, upper=upper, ) From 16f570737e181ed15647e49f402ca1cbc9d8be0c Mon Sep 17 00:00:00 2001 From: DHANUSH N Date: Sat, 5 Aug 2023 02:12:00 +0530 Subject: [PATCH 4/6] fixed tests for paddle cholesky_solve --- .../test_frontends/test_paddle/test_tensor/test_linalg.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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 0c17c49bab02d..ab404739edd9a 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 @@ -551,6 +551,9 @@ def _get_cholesky_matrix(draw): ) spd = np.matmul(gen.T, gen) + np.identity(gen.shape[0]) spd_chol = np.linalg.cholesky(spd) + 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 @@ -578,13 +581,11 @@ def _get_second_matrix(draw): x=_get_cholesky_matrix(), y=_get_second_matrix(), test_with_out=st.just(False), - upper=st.booleans(), ) def test_paddle_cholesky_solve( *, x, y, - upper, frontend, backend_fw, test_flags, @@ -604,7 +605,7 @@ def test_paddle_cholesky_solve( atol=1e-3, x=x2, y=x1, - upper=upper, + upper=np.array_equal(x1, np.triu(x1)), # check whether the matrix is upper ) From c1c63dc83ab554cffd15cc37d65cb8a51b56ffa4 Mon Sep 17 00:00:00 2001 From: DHANUSH N Date: Sat, 5 Aug 2023 21:34:54 +0530 Subject: [PATCH 5/6] Refactored decorators --- ivy/functional/frontends/paddle/tensor/linalg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ivy/functional/frontends/paddle/tensor/linalg.py b/ivy/functional/frontends/paddle/tensor/linalg.py index 727488d845ab0..8f36ae930bd09 100644 --- a/ivy/functional/frontends/paddle/tensor/linalg.py +++ b/ivy/functional/frontends/paddle/tensor/linalg.py @@ -123,8 +123,8 @@ def solve(x1, x2, name=None): # cholesky_solve -@to_ivy_arrays_and_back @with_supported_dtypes({"2.5.1 and below": ("float32", "float64")}, "paddle") +@to_ivy_arrays_and_back def cholesky_solve(y, x, /, *, upper=False, name=None): if upper: x = ivy.matrix_transpose(x) From eb08a9d8463f54b09816b72be3b6bfd09b6f2b11 Mon Sep 17 00:00:00 2001 From: DHANUSH N Date: Wed, 16 Aug 2023 12:16:19 +0000 Subject: [PATCH 6/6] fixed arguments and removed redundant functions --- .../frontends/paddle/tensor/linalg.py | 8 +-- .../test_paddle/test_tensor/test_linalg.py | 57 ++++--------------- 2 files changed, 15 insertions(+), 50 deletions(-) diff --git a/ivy/functional/frontends/paddle/tensor/linalg.py b/ivy/functional/frontends/paddle/tensor/linalg.py index 8f36ae930bd09..126961573e701 100644 --- a/ivy/functional/frontends/paddle/tensor/linalg.py +++ b/ivy/functional/frontends/paddle/tensor/linalg.py @@ -125,11 +125,11 @@ def solve(x1, x2, name=None): # cholesky_solve @with_supported_dtypes({"2.5.1 and below": ("float32", "float64")}, "paddle") @to_ivy_arrays_and_back -def cholesky_solve(y, x, /, *, upper=False, name=None): +def cholesky_solve(x, y, /, *, upper=False, name=None): if upper: - x = ivy.matrix_transpose(x) - Y = ivy.solve(x, y) - return ivy.solve(ivy.matrix_transpose(x), Y) + y = ivy.matrix_transpose(y) + Y = ivy.solve(y, x) + return ivy.solve(ivy.matrix_transpose(y), Y) # cholesky 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 ab404739edd9a..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 @@ -2,12 +2,15 @@ import ivy from hypothesis import strategies as st, assume import numpy as np -import sys # local 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 # # ------ # @@ -530,56 +533,18 @@ def test_paddle_solve( # cholesky_solve @st.composite -def _get_cholesky_matrix(draw): - # batch_shape, random_size, shared - input_dtype = draw( - st.shared( - st.sampled_from(draw(helpers.get_dtypes("float"))), - key="shared_dtype", - ) - ) - shared_size = draw( - st.shared(helpers.ints(min_value=2, max_value=4), key="shared_size") - ) - gen = draw( - helpers.array_values( - dtype=input_dtype, - shape=tuple([shared_size, shared_size]), - min_value=2, - max_value=5, - ).filter(lambda x: np.linalg.cond(x.tolist()) < 1 / sys.float_info.epsilon) - ) - spd = np.matmul(gen.T, gen) + np.identity(gen.shape[0]) - spd_chol = np.linalg.cholesky(spd) +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 -@st.composite -def _get_second_matrix(draw): - # batch_shape, shared, random_size - input_dtype = draw( - st.shared( - st.sampled_from(draw(helpers.get_dtypes("float"))), - key="shared_dtype", - ) - ) - shared_size = draw( - st.shared(helpers.ints(min_value=2, max_value=4), key="shared_size") - ) - return input_dtype, draw( - helpers.array_values( - dtype=input_dtype, shape=tuple([shared_size, 1]), min_value=2, max_value=5 - ) - ) - - @handle_frontend_test( fn_tree="paddle.tensor.linalg.cholesky_solve", - x=_get_cholesky_matrix(), - y=_get_second_matrix(), + x=_get_second_matrix(), + y=_get_paddle_cholesky_matrix(), test_with_out=st.just(False), ) def test_paddle_cholesky_solve( @@ -603,9 +568,9 @@ def test_paddle_cholesky_solve( on_device=on_device, rtol=1e-3, atol=1e-3, - x=x2, - y=x1, - upper=np.array_equal(x1, np.triu(x1)), # check whether the matrix is upper + x=x1, + y=x2, + upper=np.array_equal(x2, np.triu(x2)), # check whether the matrix is upper )