From 6e96567a1fdca54cdd6fdb4d87eb3d3d431fbce9 Mon Sep 17 00:00:00 2001 From: zois Date: Sat, 14 Oct 2023 04:23:03 +0300 Subject: [PATCH 1/4] Add Misc Operations pytorch frontend (cdist) --- .../frontends/torch/miscellaneous_ops.py | 40 +++++++++++++++++ .../test_torch/test_miscellaneous_ops.py | 44 +++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/ivy/functional/frontends/torch/miscellaneous_ops.py b/ivy/functional/frontends/torch/miscellaneous_ops.py index 7e1f401b5ed59..f7ca0bad2d7ef 100644 --- a/ivy/functional/frontends/torch/miscellaneous_ops.py +++ b/ivy/functional/frontends/torch/miscellaneous_ops.py @@ -92,6 +92,46 @@ def cartesian_prod(*tensors): return ret +@with_unsupported_dtypes({"2.1.0 and below": "float16"}, "torch") +@to_ivy_arrays_and_back +def cdist(x1, x2, p, compute_mode="use_mm_for_euclid_dist_if_necessary"): + if len(x1.shape) != 3 or len(x2.shape) != 3: + raise ivy.exceptions.IvyError( + "Both ivy arrays need to have 3 dimensions (BxRxM)" + ) + + if ( + compute_mode != "use_mm_for_euclid_dist_if_necessary" + and compute_mode != "use_mm_for_euclid_dist" + and compute_mode != "donot_use_mm_for_euclid_dist" + ): + raise ivy.exceptions.IvyError( + f"{compute_mode} is not a valid value for compute_mode" + ) + if p == 2: + B, P, M = x1.shape + _, R, _ = x2.shape + if ( + compute_mode == "use_mm_for_euclid_dist_if_necessary" + and (P > 25 or R > 25) + or compute_mode == "use_mm_for_euclid_dist" + ): + return ivy.vector_norm( + x1[:, :, None, :] - x2[:, None, :, :], axis=-1, ord=p + ) + else: + distances = ivy.zeros((B, P, R), dtype=x1.dtype) + for b in range(B): + for i in range(P): + for j in range(R): + distances[b, i, j] = ivy.vector_norm( + x1[b, i, :] - x2[b, j, :], ord=p + ) + return distances + else: + return ivy.vector_norm(x1[:, :, None, :] - x2[:, None, :, :], axis=-1, ord=p) + + @to_ivy_arrays_and_back def clone(input, *, memory_format=None): return ivy.copy_array(input) diff --git a/ivy_tests/test_ivy/test_frontends/test_torch/test_miscellaneous_ops.py b/ivy_tests/test_ivy/test_frontends/test_torch/test_miscellaneous_ops.py index 3d2250c78ef9c..c75ec8ec30d5c 100644 --- a/ivy_tests/test_ivy/test_frontends/test_torch/test_miscellaneous_ops.py +++ b/ivy_tests/test_ivy/test_frontends/test_torch/test_miscellaneous_ops.py @@ -510,6 +510,50 @@ def test_torch_cartesian_prod( ) +@handle_frontend_test( + fn_tree="torch.cdist", + dtypes_and_x=helpers.dtype_and_values( + shape=st.shared(helpers.get_shape(min_num_dims=3, max_num_dims=3), key="shape"), + shared_dtype=True, + num_arrays=2, + allow_inf=False, + dtype=["float32", "float32", "float64", "float64"], + ), + p=st.integers(min_value=0, max_value=1000000), + compute_mode=st.sampled_from( + [ + "use_mm_for_euclid_dist_if_necessary", + "use_mm_for_euclid_dist", + "donot_use_mm_for_euclid_dist", + ] + ), +) +def test_torch_cdist( + *, + dtypes_and_x, + p, + compute_mode, + on_device, + fn_tree, + frontend, + test_flags, + backend_fw, +): + input_dtypes, xs = dtypes_and_x + helpers.test_frontend_function( + input_dtypes=input_dtypes, + backend_to_test=backend_fw, + frontend=frontend, + test_flags=test_flags, + fn_tree=fn_tree, + on_device=on_device, + x1=xs[0], + x2=xs[1], + p=p, + compute_mode=compute_mode, + ) + + # clone @handle_frontend_test( fn_tree="torch.clone", From 87abe70439372223b25c1bc64cd31977631421ff Mon Sep 17 00:00:00 2001 From: Zois <64856990+joyios1@users.noreply.github.com> Date: Mon, 23 Oct 2023 19:37:17 +0300 Subject: [PATCH 2/4] Update ivy/functional/frontends/torch/miscellaneous_ops.py oh, yes, I forgot it. Co-authored-by: Sam Armstrong <88863522+Sam-Armstrong@users.noreply.github.com> --- ivy/functional/frontends/torch/miscellaneous_ops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ivy/functional/frontends/torch/miscellaneous_ops.py b/ivy/functional/frontends/torch/miscellaneous_ops.py index f7ca0bad2d7ef..1cb9a7200ddb4 100644 --- a/ivy/functional/frontends/torch/miscellaneous_ops.py +++ b/ivy/functional/frontends/torch/miscellaneous_ops.py @@ -94,7 +94,7 @@ def cartesian_prod(*tensors): @with_unsupported_dtypes({"2.1.0 and below": "float16"}, "torch") @to_ivy_arrays_and_back -def cdist(x1, x2, p, compute_mode="use_mm_for_euclid_dist_if_necessary"): +def cdist(x1, x2, p=2.0, compute_mode="use_mm_for_euclid_dist_if_necessary"): if len(x1.shape) != 3 or len(x2.shape) != 3: raise ivy.exceptions.IvyError( "Both ivy arrays need to have 3 dimensions (BxRxM)" From d7344ba49ed5909b9a44c64a062f0abccc742e39 Mon Sep 17 00:00:00 2001 From: Zois <64856990+joyios1@users.noreply.github.com> Date: Mon, 23 Oct 2023 19:43:41 +0300 Subject: [PATCH 3/4] Update ivy_tests/test_ivy/test_frontends/test_torch/test_miscellaneous_ops.py Yes that is better. I did the change and the tests pass, so it is fine! Co-authored-by: Sam Armstrong <88863522+Sam-Armstrong@users.noreply.github.com> --- .../test_frontends/test_torch/test_miscellaneous_ops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ivy_tests/test_ivy/test_frontends/test_torch/test_miscellaneous_ops.py b/ivy_tests/test_ivy/test_frontends/test_torch/test_miscellaneous_ops.py index c75ec8ec30d5c..08d0f26b92a4c 100644 --- a/ivy_tests/test_ivy/test_frontends/test_torch/test_miscellaneous_ops.py +++ b/ivy_tests/test_ivy/test_frontends/test_torch/test_miscellaneous_ops.py @@ -517,7 +517,7 @@ def test_torch_cartesian_prod( shared_dtype=True, num_arrays=2, allow_inf=False, - dtype=["float32", "float32", "float64", "float64"], + available_dtypes=["float32", "float64"], ), p=st.integers(min_value=0, max_value=1000000), compute_mode=st.sampled_from( From 85f65cb3c29788ce29300ffeba789704c1f19d83 Mon Sep 17 00:00:00 2001 From: NripeshN Date: Wed, 27 Dec 2023 23:28:07 +0530 Subject: [PATCH 4/4] Update unsupported dtype check for cdist function --- ivy/functional/frontends/torch/miscellaneous_ops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ivy/functional/frontends/torch/miscellaneous_ops.py b/ivy/functional/frontends/torch/miscellaneous_ops.py index 5c121aee92cab..7bd69abad45d5 100644 --- a/ivy/functional/frontends/torch/miscellaneous_ops.py +++ b/ivy/functional/frontends/torch/miscellaneous_ops.py @@ -92,7 +92,7 @@ def cartesian_prod(*tensors): return ret -@with_unsupported_dtypes({"2.1.0 and below": "float16"}, "torch") +@with_unsupported_dtypes({"2.1.2 and below": "float16"}, "torch") @to_ivy_arrays_and_back def cdist(x1, x2, p=2.0, compute_mode="use_mm_for_euclid_dist_if_necessary"): if len(x1.shape) != 3 or len(x2.shape) != 3: