Repository: ivy-llc/ivy

Issue:
clip_



Discussion:

Resolving patch:
diff --git a/ivy/functional/frontends/torch/pointwise_ops.py b/ivy/functional/frontends/torch/pointwise_ops.py
--- a/ivy/functional/frontends/torch/pointwise_ops.py
+++ b/ivy/functional/frontends/torch/pointwise_ops.py
@@ -257,7 +257,7 @@ def ceil(input, *, out=None):
     return ivy.ceil(input, out=out)
 
 
-@with_unsupported_dtypes({"1.11.0 and below": ("float16",)}, "torch")
+@with_unsupported_dtypes({"1.11.0 and below": ("float16", "complex")}, "torch")
 @to_ivy_arrays_and_back
 def clamp(input, min=None, max=None, *, out=None):
     ivy.utils.assertions.check_all_or_any_fn(
@@ -268,7 +268,6 @@ def clamp(input, min=None, max=None, *, out=None):
         limit=[1, 2],
         message="at most one of min or max can be None",
     )
-    input = ivy.array(input)
     if min is None:
         return ivy.minimum(input, max, out=out)
     if max is None:
@@ -276,23 +275,7 @@ def clamp(input, min=None, max=None, *, out=None):
     return ivy.clip(input, min, max, out=out)
 
 
-@with_unsupported_dtypes({"1.11.0 and below": ("float16",)}, "torch")
-@to_ivy_arrays_and_back
-def clip(input, min=None, max=None, *, out=None):
-    ivy.utils.assertions.check_all_or_any_fn(
-        min,
-        max,
-        fn=ivy.exists,
-        type="any",
-        limit=[1, 2],
-        message="at most one of min or max can be None",
-    )
-    input = ivy.array(input)
-    if min is None:
-        return ivy.minimum(input, max, out=out)
-    if max is None:
-        return ivy.maximum(input, min, out=out)
-    return ivy.clip(input, min, max, out=out)
+clip = clamp
 
 
 @to_ivy_arrays_and_back
diff --git a/ivy/functional/frontends/torch/tensor.py b/ivy/functional/frontends/torch/tensor.py
--- a/ivy/functional/frontends/torch/tensor.py
+++ b/ivy/functional/frontends/torch/tensor.py
@@ -702,13 +702,11 @@ def tril(self, diagonal=0):
     def index_select(self, dim, index):
         return torch_frontend.index_select(self._ivy_array, dim, index)
 
-    @with_unsupported_dtypes({"1.11.0 and below": ("bfloat16", "float16")}, "torch")
+    @with_unsupported_dtypes({"1.11.0 and below": ("float16", "complex")}, "torch")
     def clamp(self, min=None, max=None):
-        if min is not None and max is not None and ivy.all(min > max):
-            return torch_frontend.tensor(ivy.array(self._ivy_array).full_like(max))
         return torch_frontend.clamp(self._ivy_array, min=min, max=max)
 
-    @with_unsupported_dtypes({"1.11.0 and below": ("bfloat16", "float16")}, "torch")
+    @with_unsupported_dtypes({"1.11.0 and below": ("float16", "complex")}, "torch")
     def clamp_(self, min=None, max=None):
         self._ivy_array = self.clamp(min=min, max=max).ivy_array
         return self
@@ -916,6 +914,7 @@ def __and__(self, other):
 
     # Method aliases
     absolute, absolute_ = abs, abs_
+    clip, clip_ = clamp, clamp_
     ndimension = dim
 
     def bitwise_xor(self, other):


Test patch:
diff --git a/ivy_tests/test_ivy/test_frontends/test_torch/test_pointwise_ops.py b/ivy_tests/test_ivy/test_frontends/test_torch/test_pointwise_ops.py
--- a/ivy_tests/test_ivy/test_frontends/test_torch/test_pointwise_ops.py
+++ b/ivy_tests/test_ivy/test_frontends/test_torch/test_pointwise_ops.py
@@ -1185,7 +1185,7 @@ def _get_clip_inputs(draw):
     )
     x_dtype, x = draw(
         helpers.dtype_and_values(
-            available_dtypes=helpers.get_dtypes("float"),
+            available_dtypes=helpers.get_dtypes("numeric"),
             shape=shape,
         )
     )
@@ -1194,13 +1194,13 @@ def _get_clip_inputs(draw):
         max = draw(st.booleans())
         min = draw(
             helpers.array_values(
-                dtype=x_dtype[0], shape=shape, min_value=-25, max_value=0
+                dtype=x_dtype[0], shape=shape, min_value=0, max_value=25
             )
         )
         max = (
             draw(
                 helpers.array_values(
-                    dtype=x_dtype[0], shape=shape, min_value=1, max_value=25
+                    dtype=x_dtype[0], shape=shape, min_value=26, max_value=50
                 )
             )
             if max
@@ -1210,7 +1210,7 @@ def _get_clip_inputs(draw):
         min = None
         max = draw(
             helpers.array_values(
-                dtype=x_dtype[0], shape=shape, min_value=1, max_value=25
+                dtype=x_dtype[0], shape=shape, min_value=26, max_value=50
             )
         )
     return x_dtype, x, min, max
@@ -1219,6 +1219,7 @@ def _get_clip_inputs(draw):
 # clamp
 @handle_frontend_test(
     fn_tree="torch.clamp",
+    aliases=["torch.clip"],
     input_and_ranges=_get_clip_inputs(),
 )
 def test_torch_clamp(
@@ -1242,32 +1243,6 @@ def test_torch_clamp(
     )
 
 
-# clip
-@handle_frontend_test(
-    fn_tree="torch.clip",
-    input_and_ranges=_get_clip_inputs(),
-)
-def test_torch_clip(
-    *,
-    input_and_ranges,
-    on_device,
-    fn_tree,
-    frontend,
-    test_flags,
-):
-    x_dtype, x, min, max = input_and_ranges
-    helpers.test_frontend_function(
-        input_dtypes=x_dtype,
-        frontend=frontend,
-        test_flags=test_flags,
-        fn_tree=fn_tree,
-        on_device=on_device,
-        input=x[0],
-        min=min,
-        max=max,
-    )
-
-
 # mul
 @handle_frontend_test(
     fn_tree="torch.mul",
diff --git a/ivy_tests/test_ivy/test_frontends/test_torch/test_tensor.py b/ivy_tests/test_ivy/test_frontends/test_torch/test_tensor.py
--- a/ivy_tests/test_ivy/test_frontends/test_torch/test_tensor.py
+++ b/ivy_tests/test_ivy/test_frontends/test_torch/test_tensor.py
@@ -5678,6 +5678,68 @@ def test_torch_instance_clamp_(
     )
 
 
+# clip
+@handle_frontend_method(
+    class_tree=CLASS_TREE,
+    init_tree="torch.tensor",
+    method_name="clip",
+    input_and_ranges=_get_clamp_inputs(),
+)
+def test_torch_instance_clip(
+    input_and_ranges,
+    frontend,
+    frontend_method_data,
+    init_flags,
+    method_flags,
+    on_device,
+):
+    input_dtype, x, min, max = input_and_ranges
+    helpers.test_frontend_method(
+        init_input_dtypes=input_dtype,
+        init_all_as_kwargs_np={
+            "data": x[0],
+        },
+        method_input_dtypes=input_dtype,
+        method_all_as_kwargs_np={"min": min, "max": max},
+        frontend_method_data=frontend_method_data,
+        init_flags=init_flags,
+        method_flags=method_flags,
+        frontend=frontend,
+        on_device=on_device,
+    )
+
+
+# clip_
+@handle_frontend_method(
+    class_tree=CLASS_TREE,
+    init_tree="torch.tensor",
+    method_name="clip_",
+    input_and_ranges=_get_clamp_inputs(),
+)
+def test_torch_instance_clip_(
+    input_and_ranges,
+    frontend,
+    frontend_method_data,
+    init_flags,
+    method_flags,
+    on_device,
+):
+    input_dtype, x, min, max = input_and_ranges
+    helpers.test_frontend_method(
+        init_input_dtypes=input_dtype,
+        init_all_as_kwargs_np={
+            "data": x[0],
+        },
+        method_input_dtypes=input_dtype,
+        method_all_as_kwargs_np={"min": min, "max": max},
+        frontend_method_data=frontend_method_data,
+        init_flags=init_flags,
+        method_flags=method_flags,
+        frontend=frontend,
+        on_device=on_device,
+    )
+
+
 # __gt__
 @handle_frontend_method(
     class_tree=CLASS_TREE,