From a1e8e47f5e28be345d315be05332b162e0723e3e Mon Sep 17 00:00:00 2001 From: ArsalanAli915 Date: Sun, 27 Aug 2023 16:29:55 +0500 Subject: [PATCH 1/5] add inplace log in pytorch frontend --- ivy/functional/frontends/torch/tensor.py | 2 +- .../test_frontends/test_torch/test_tensor.py | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/ivy/functional/frontends/torch/tensor.py b/ivy/functional/frontends/torch/tensor.py index 09ec33d2d5fd3..de7918aff432a 100644 --- a/ivy/functional/frontends/torch/tensor.py +++ b/ivy/functional/frontends/torch/tensor.py @@ -369,7 +369,7 @@ def arctanh_(self): @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") def log(self): return torch_frontend.log(self) - + @with_supported_dtypes({"2.0.1 and below": ("float32", "float64")}, "torch") def log2_(self): self.ivy_array = self.log2().ivy_array 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 index e45dfb5acf803..ff567032b009f 100644 --- a/ivy_tests/test_ivy/test_frontends/test_torch/test_tensor.py +++ b/ivy_tests/test_ivy/test_frontends/test_torch/test_tensor.py @@ -2127,17 +2127,17 @@ def test_torch_tensor_log( ) -# log2_ +# log_ @handle_frontend_method( class_tree=CLASS_TREE, init_tree="torch.tensor", - method_name="log2_", + method_name="log_", dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), + available_dtypes=helpers.get_dtypes("float"), allow_inf=False, ), ) -def test_torch_tensor_log2_( +def test_torch_tensor_log_( dtype_and_x, frontend_method_data, init_flags, @@ -2162,18 +2162,17 @@ def test_torch_tensor_log2_( on_device=on_device, ) - -# log_ +# log2_ @handle_frontend_method( class_tree=CLASS_TREE, init_tree="torch.tensor", - method_name="log_", + method_name="log2_", dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), + available_dtypes=helpers.get_dtypes("valid"), allow_inf=False, ), ) -def test_torch_tensor_log_( +def test_torch_tensor_log2_( dtype_and_x, frontend_method_data, init_flags, @@ -2199,6 +2198,7 @@ def test_torch_tensor_log_( ) + # log2 @handle_frontend_method( class_tree=CLASS_TREE, From 2f7b1ea0364e3922cf4fed8245314b9a75409b57 Mon Sep 17 00:00:00 2001 From: ArsalanAli915 Date: Tue, 29 Aug 2023 16:54:01 +0500 Subject: [PATCH 2/5] passed all test --- ivy/functional/frontends/torch/tensor.py | 4 ++-- ivy_tests/test_ivy/test_frontends/test_torch/test_tensor.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ivy/functional/frontends/torch/tensor.py b/ivy/functional/frontends/torch/tensor.py index 905adcce4fdd9..5b3f29c900a1b 100644 --- a/ivy/functional/frontends/torch/tensor.py +++ b/ivy/functional/frontends/torch/tensor.py @@ -369,7 +369,7 @@ def arctanh_(self): @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") def log(self): return torch_frontend.log(self) - + @with_supported_dtypes({"2.0.1 and below": ("float32", "float64")}, "torch") def log2_(self): self.ivy_array = self.log2().ivy_array @@ -387,7 +387,7 @@ def copy_(self, other, non_blocking=False): def arccosh(self): return torch_frontend.arccosh(self) - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") + @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") def log_(self): self.ivy_array = self.log().ivy_array return self 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 index 393947c007736..defb7046eaea1 100644 --- a/ivy_tests/test_ivy/test_frontends/test_torch/test_tensor.py +++ b/ivy_tests/test_ivy/test_frontends/test_torch/test_tensor.py @@ -2162,6 +2162,7 @@ def test_torch_tensor_log_( on_device=on_device, ) + # log2_ @handle_frontend_method( class_tree=CLASS_TREE, @@ -2198,7 +2199,6 @@ def test_torch_tensor_log2_( ) - # log2 @handle_frontend_method( class_tree=CLASS_TREE, From 2ade894711ddf6e86ef1bfd7e9b8d23989cbc696 Mon Sep 17 00:00:00 2001 From: ArsalanAli915 Date: Thu, 31 Aug 2023 14:04:08 +0500 Subject: [PATCH 3/5] implemented inplace reciprocal function and passed all tests --- .../frontends/paddle/tensor/math.py | 6 ++++ .../test_paddle/test_tensor/test_math.py | 28 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/ivy/functional/frontends/paddle/tensor/math.py b/ivy/functional/frontends/paddle/tensor/math.py index dd4564b44302f..54a6052a4df5c 100644 --- a/ivy/functional/frontends/paddle/tensor/math.py +++ b/ivy/functional/frontends/paddle/tensor/math.py @@ -393,6 +393,12 @@ def reciprocal(x, name=None): return ivy.reciprocal(x) +@with_unsupported_dtypes({"2.5.1 and below": ("float16", "bfloat16")}, "paddle") +@to_ivy_arrays_and_back +def reciprocal_(x, name=None): + return ivy.inplace_update(x, reciprocal(x)) + + @with_unsupported_dtypes({"2.5.1 and below": ("float16", "bfloat16")}, "paddle") @to_ivy_arrays_and_back def remainder(x, y, name=None): diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_math.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_math.py index 62ab9db541e88..d821c6f0dbce3 100644 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_math.py +++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_math.py @@ -1710,6 +1710,34 @@ def test_paddle_reciprocal( ) +# reciprocal_ +@handle_frontend_test( + fn_tree="paddle.reciprocal", + dtype_and_x=helpers.dtype_and_values( + available_dtypes=helpers.get_dtypes("float"), + ), +) +def test_paddle_reciprocal_( + *, + dtype_and_x, + on_device, + fn_tree, + frontend, + backend_fw, + test_flags, +): + input_dtype, x = dtype_and_x + helpers.test_frontend_function( + input_dtypes=input_dtype, + frontend=frontend, + backend_to_test=backend_fw, + test_flags=test_flags, + fn_tree=fn_tree, + on_device=on_device, + x=x[0], + ) + + # remainder @handle_frontend_test( fn_tree="paddle.remainder", From d60e585118d1d55b56e97aa9164ef8d562fd30f9 Mon Sep 17 00:00:00 2001 From: Arsalan ali Date: Thu, 31 Aug 2023 14:11:37 +0500 Subject: [PATCH 4/5] Delete ivy/functional/frontends/torch/tensor.py --- ivy/functional/frontends/torch/tensor.py | 1915 ---------------------- 1 file changed, 1915 deletions(-) delete mode 100644 ivy/functional/frontends/torch/tensor.py diff --git a/ivy/functional/frontends/torch/tensor.py b/ivy/functional/frontends/torch/tensor.py deleted file mode 100644 index 5e881f7208c31..0000000000000 --- a/ivy/functional/frontends/torch/tensor.py +++ /dev/null @@ -1,1915 +0,0 @@ -# global -from typing import Iterable -import math - -# local -import ivy -import ivy.functional.frontends.torch as torch_frontend -import ivy.functional.frontends.torch.nn.functional as torch_frontend_nn -from ivy.functional.frontends.numpy.creation_routines.from_existing_data import ( - array as np_frontend_array, -) -from ivy.func_wrapper import with_unsupported_dtypes -from ivy.func_wrapper import with_supported_dtypes -from ivy.functional.frontends.torch.func_wrapper import ( - _to_ivy_array, - numpy_to_torch_style_args, -) - - -class Tensor: - def __init__(self, array, device=None, _init_overload=False, requires_grad=False): - if _init_overload: - self._ivy_array = ( - ivy.array(array) if not isinstance(array, ivy.Array) else array - ) - - else: - self._ivy_array = ivy.array( - array, dtype=torch_frontend.float32, device=device - ) - self._grads = None - self._requires_grad = requires_grad - self.grad_fn = None - if not _init_overload: - self._is_leaf = True - else: - self._is_leaf = False - self._requires_grad = requires_grad - - def __len__(self): - return len(self._ivy_array) - - def __repr__(self): - return str(self.ivy_array.__repr__()).replace( - "ivy.array", "ivy.frontends.torch.Tensor" - ) - - # Properties # - # ---------- # - - @property - def ivy_array(self): - return self._ivy_array - - @property - def device(self): - return self.ivy_array.device - - @property - def dtype(self): - return self.ivy_array.dtype - - @property - def shape(self): - return Size(self.ivy_array.shape) - - @property - def real(self): - return self.ivy_array.real - - @property - def imag(self): - return self.ivy_array.imag - - @property - def ndim(self): - return self.dim() - - @property - def T(self): - if self.ndim == 1: - return self - return torch_frontend.permute(self, list(range(self.ndim))[::-1]) - - @property - def data(self): - return torch_frontend.tensor( - ivy.stop_gradient(self.ivy_array, preserve_type=False) - ) - - @property - def grad(self): - return self._grads - - @property - def requires_grad(self): - return self._requires_grad - - @property - def is_leaf(self): - return self._is_leaf - - # Setters # - # --------# - - @ivy_array.setter - def ivy_array(self, array): - self._ivy_array = ( - ivy.array(array) if not isinstance(array, ivy.Array) else array - ) - - @requires_grad.setter - def requires_grad(self, requires_grad): - self._requires_grad = requires_grad - - @is_leaf.setter - def is_leaf(self, is_leaf): - self._is_leaf = is_leaf - - # Instance Methods # - # ---------------- # - def reshape(self, *args, shape=None): - if args and shape: - raise TypeError("reshape() got multiple values for argument 'shape'") - if shape is not None: - return torch_frontend.reshape(self, shape) - if args: - if isinstance(args[0], (tuple, list, ivy.Shape)): - shape = args[0] - return torch_frontend.reshape(self, shape) - else: - return torch_frontend.reshape(self, args) - return torch_frontend.reshape(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def reshape_as(self, other): - return torch_frontend.reshape(self, other.shape) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def add(self, other, *, alpha=1): - return torch_frontend.add(self, other, alpha=alpha) - - # @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def divide(self, other, *, out=None): - return torch_frontend.divide(self, other, out=out) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def sub(self, other, *, alpha=1): - return torch_frontend.sub(self, other, alpha=alpha) - - def chunk(self, chunks, dim=0): - return torch_frontend.chunk(self, chunks, dim=dim) - - @numpy_to_torch_style_args - def any(self, dim=None, keepdim=False): - return torch_frontend.any(self, dim=dim, keepdim=keepdim) - - @numpy_to_torch_style_args - def all(self, dim=None, keepdim=False): - return torch_frontend.all(self, dim=dim, keepdim=keepdim) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def add_(self, other, *, alpha=1): - self.ivy_array = self.add(other, alpha=alpha).ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def addmm(self, mat1, mat2, *, beta=1, alpha=1): - return torch_frontend.addmm(self, mat1, mat2, beta=beta, alpha=alpha) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def addmm_(self, mat1, mat2, *, beta=1, alpha=1): - self.ivy_array = self.addmm(mat1, mat2, beta=beta, alpha=alpha).ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def addmv(self, mat, vec, *, beta=1, alpha=1): - return torch_frontend.addmv(self, mat, vec, beta=beta, alpha=alpha) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def addmv_(self, mat, vec, *, beta=1, alpha=1): - self.ivy_array = torch_frontend.addmv( - self, mat, vec, beta=beta, alpha=alpha - ).ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def addbmm(self, batch1, batch2, *, beta=1, alpha=1): - return torch_frontend.addbmm(self, batch1, batch2, beta=beta, alpha=alpha) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def addbmm_(self, batch1, batch2, *, beta=1, alpha=1): - self.ivy_array = self.addbmm(batch1, batch2, beta=beta, alpha=alpha).ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def subtract_(self, other, *, alpha=1): - self.ivy_array = self.sub(other, alpha=alpha).ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def asin(self): - return torch_frontend.asin(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def asin_(self): - self.ivy_array = self.asin().ivy_array - return self - - @numpy_to_torch_style_args - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def sum(self, dim=None, keepdim=False, *, dtype=None): - return torch_frontend.sum(self, dim=dim, keepdim=keepdim, dtype=dtype) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def sin(self): - return torch_frontend.sin(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def sin_(self): - self.ivy_array = self.sin().ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def sinh(self): - return torch_frontend.sinh(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def sinh_(self): - self.ivy_array = self.sinh().ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def cos(self): - return torch_frontend.cos(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def cos_(self): - self.ivy_array = self.cos().ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def cosh(self): - return torch_frontend.cosh(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def cosh_(self): - self.ivy_array = self.cosh().ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def arcsinh(self): - return torch_frontend.arcsinh(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def arcsinh_(self): - self.ivy_array = torch_frontend.arcsinh(self).ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def arcsin(self): - return torch_frontend.arcsin(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def arcsin_(self): - self.ivy_array = self.arcsin().ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def atan(self): - return torch_frontend.atan(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def atan_(self): - self.ivy_array = self.atan().ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def atan2(self, other): - return torch_frontend.atan2(self, other) - - def view(self, *args, size=None): - """ - Reshape Tensor. - - possible arguments are either: - - size - - tuple of ints - - list of ints - - torch.Size object - - ints - Parameters - ---------- - args:int arguments - size: optional shape - - Returns reshaped tensor - ------- - """ - if ivy.exists(size) and not args: - shape_tup = size - elif args and not ivy.exists(size): - if ( - isinstance(args[0], (tuple, list, ivy.Shape)) - or type(args[0]).__name__ == "Size" - ) and len(args) == 1: - shape_tup = args[0] - else: - shape_tup = args - else: - raise ValueError( - "View only accepts as argument ints, tuple or list of ints or " - "the keyword argument size." - ) - return torch_frontend.reshape(self, shape_tup) - - def float(self, memory_format=None): - self.ivy_array = ivy.astype(self.ivy_array, ivy.float32, copy=False) - return self - - def double(self): - return self.to(torch_frontend.float64) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def asinh(self): - return torch_frontend.asinh(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def asinh_(self): - self.ivy_array = self.asinh().ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def tan(self): - return torch_frontend.tan(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def tan_(self): - self.ivy_array = self.tan().ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def tanh(self): - return torch_frontend.tanh(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def tanh_(self): - self.ivy_array = self.tanh().ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def atanh(self): - return torch_frontend.atanh(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def atanh_(self): - self.ivy_array = self.atanh().ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def arctanh(self): - return torch_frontend.arctanh(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def arctanh_(self): - self.ivy_array = self.arctanh().ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def log(self): - return torch_frontend.log(self) - - @with_supported_dtypes({"2.0.1 and below": ("float32", "float64")}, "torch") - def log2_(self): - self.ivy_array = self.log2().ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16", "uint16")}, "torch") - def copy_(self, other, non_blocking=False): - ivy.utils.assertions.check_one_way_broadcastable( - self.ivy_array.shape, torch_frontend.tensor(other).ivy_array.shape - ) - self._ivy_array = torch_frontend.tensor(other).ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def arccosh(self): - return torch_frontend.arccosh(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def log_(self): - self.ivy_array = self.log().ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def log2(self): - return torch_frontend.log2(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def relu(self): - return torch_frontend_nn.relu(self) - - @numpy_to_torch_style_args - @with_unsupported_dtypes({"2.0.1 and below": ("complex",)}, "torch") - def amax(self, dim=None, keepdim=False): - return torch_frontend.amax(self, dim=dim, keepdim=keepdim) - - @numpy_to_torch_style_args - @with_unsupported_dtypes({"2.0.1 and below": ("complex",)}, "torch") - def amin(self, dim=None, keepdim=False): - return torch_frontend.amin(self, dim=dim, keepdim=keepdim) - - @numpy_to_torch_style_args - @with_unsupported_dtypes({"2.0.1 and below": ("complex", "float16")}, "torch") - def aminmax(self, dim=None, keepdim=False): - return torch_frontend.aminmax(self, dim=dim, keepdim=keepdim) - - def abs(self): - return torch_frontend.abs(self) - - def abs_(self): - self.ivy_array = self.abs().ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def logical_and(self, other): - return torch_frontend.logical_and(self, other) - - def logical_not(self, *, out=None): - return torch_frontend.logical_not(self, out=out) - - def logical_not_(self): - self.ivy_array = ivy.astype(self.logical_not().ivy_array, self.dtype) - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def logical_or(self, other): - return torch_frontend.logical_or(self, other) - - def bitwise_not(self): - return torch_frontend.bitwise_not(self) - - def bitwise_and(self, other): - return torch_frontend.bitwise_and(self, other) - - @with_supported_dtypes({"2.0.1 and below": ("integer",)}, "torch") - def bitwise_or(self, other): - return torch_frontend.bitwise_or(self, other) - - def bitwise_left_shift(self, other): - return torch_frontend.bitwise_left_shift(self, other) - - @with_supported_dtypes({"2.0.1 and below": ("integer",)}, "torch") - def bitwise_or_(self, other): - self.ivy_array = self.bitwise_or(other).ivy_array - return self - - def contiguous(self, memory_format=None): - return torch_frontend.tensor(self) - - def new_ones( - self, - *args, - size=None, - dtype=None, - device=None, - requires_grad=False, - layout=None, - pin_memory=False, - ): - if dtype is None: - dtype = self.dtype - if device is None: - device = self.device - if size is None: - size = args[0] if isinstance(args[0], (tuple, list, ivy.Shape)) else args - return torch_frontend.ones( - size, dtype=dtype, device=device, requires_grad=requires_grad - ) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def floor(self, *, out=None): - return torch_frontend.floor(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def not_equal(self, other, *, out=None): - return torch_frontend.not_equal(self, other, out=out) - - ne = not_equal - - def equal(self, other): - return torch_frontend.equal(self, other) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "complex")}, "torch") - def erf(self, *, out=None): - return torch_frontend.erf(self, out=out) - - @with_supported_dtypes( - {"2.0.1 and below": ("float32", "float64", "bfloat16")}, "torch" - ) - def erf_(self, *, out=None): - self.ivy_array = self.erf(out=out).ivy_array - return self - - def new_zeros( - self, - *args, - size=None, - dtype=None, - device=None, - requires_grad=False, - layout=None, - pin_memory=False, - ): - if size and args: - raise TypeError("new_zeros() got multiple values for argument 'size'") - if dtype is None: - dtype = self.dtype - if device is None: - device = self.device - if size is None: - size = args[0] if isinstance(args[0], (tuple, list, ivy.Shape)) else args - return torch_frontend.zeros( - size=size, dtype=dtype, device=device, requires_grad=requires_grad - ) - - def to(self, *args, **kwargs): - if len(args) > 0: - if hasattr(args[0], "ivy_array") or ivy.is_array(args[0]): - if self.dtype == ivy.dtype(args[0]) and self.device == ivy.dev(args[0]): - return self - else: - cast_tensor = self.clone() - cast_tensor.ivy_array = ivy.asarray( - self.ivy_array, - dtype=ivy.dtype(args[0]), - device=ivy.dev(args[0]), - ) - return cast_tensor - if ( - isinstance(args[0], (ivy.Dtype, ivy.NativeDtype)) - or args[0] in ivy._all_ivy_dtypes_str - ): - if self.dtype == ivy.as_ivy_dtype(args[0]): - return self - else: - cast_tensor = self.clone() - cast_tensor.ivy_array = ivy.asarray(self.ivy_array, dtype=args[0]) - return cast_tensor - if isinstance(args[0], (ivy.Device, ivy.NativeDevice, str)): - if isinstance(args[0], str) and not isinstance( - args[0], (ivy.Device, ivy.NativeDevice) - ): - ivy.utils.assertions.check_elem_in_list( - args[0], - [ - "cpu", - "cuda", - "mps", - "xpu", - "mkldnn", - "opengl", - "opencl", - "ideep", - "hip", - "ve", - "ort", - "mlc", - "xla", - "lazy", - "vulkan", - "meta", - "hpu", - ], - ) - if self.device == ivy.as_ivy_dev(args[0]): - return self - else: - cast_tensor = self.clone() - cast_tensor.ivy_array = ivy.asarray(self.ivy_array, device=args[0]) - return cast_tensor - else: - if ( - "dtype" in kwargs - and "device" in kwargs - and self.dtype == kwargs["dtype"] - and self.device == kwargs["device"] - ): - return self - else: - cast_tensor = self.clone() - cast_tensor.ivy_array = ivy.asarray( - self.ivy_array, - device=kwargs["device"] if "device" in kwargs else self.device, - dtype=kwargs["dtype"] if "dtype" in kwargs else self.dtype, - ) - return cast_tensor - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def arctan(self): - return torch_frontend.atan(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def arctan_(self): - self.ivy_array = self.arctan().ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def arctan2(self, other): - return torch_frontend.arctan2(self, other) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def arctan2_(self, other): - self.ivy_array = self.arctan2(other).ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def acos(self): - return torch_frontend.acos(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def acos_(self): - self.ivy_array = self.acos().ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def arccosh_(self): - self.ivy_array = self.arccosh().ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def arccos(self): - return torch_frontend.arccos(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def arccos_(self): - self.ivy_array = self.arccos().ivy_array - return self - - def new_tensor( - self, - data, - *, - dtype=None, - device=None, - requires_grad=False, - layout=None, - pin_memory=False, - ): - dtype = ivy.dtype(self.ivy_array) if dtype is None else dtype - device = ivy.dev(self.ivy_array) if device is None else device - _data = ivy.asarray(data, copy=True, dtype=dtype, device=device) - return torch_frontend.tensor(_data) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def view_as(self, other): - return self.view(size=other.shape) - - def expand(self, *args, size=None): - if args and size: - raise TypeError("expand() got multiple values for argument 'size'") - if args: - if isinstance(args[0], (tuple, list, ivy.Shape)): - size = args[0] - else: - size = args - - return torch_frontend.tensor(ivy.expand(self.ivy_array, tuple(size))) - - def expand_as(self, other): - return self.expand( - ivy.shape(other.ivy_array if isinstance(other, Tensor) else other) - ) - - def detach(self): - return torch_frontend.tensor( - ivy.stop_gradient(self.ivy_array, preserve_type=False) - ) - - def detach_(self): - self.ivy_array = self.detach().ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16", "uint16")}, "torch") - @numpy_to_torch_style_args - def unsqueeze(self, dim): - return torch_frontend.unsqueeze(self, dim) - - @numpy_to_torch_style_args - def unsqueeze_(self, dim): - self.ivy_array = self.unsqueeze(dim).ivy_array - return self - - def ravel(self): - return torch_frontend.ravel(self) - - def split(self, split_size, dim=0): - return torch_frontend.split(self, split_size, dim) - - def tensor_split(self, indices_or_sections, dim=0): - return torch_frontend.tensor_split(self, indices_or_sections, dim) - - def vsplit(self, indices_or_sections, /): - return torch_frontend.vsplit(self, indices_or_sections) - - def hsplit(self, indices_or_sections, /): - return torch_frontend.hsplit(self, indices_or_sections) - - def dsplit( - self, - indices_or_sections, - /, - ): - return torch_frontend.dsplit(self, indices_or_sections) - - def dim(self): - return self.ivy_array.ndim - - @with_supported_dtypes( - {"2.5.0 and below": ("float32", "float64", "int32", "int64")}, "paddle" - ) - def heaviside(self, values, *, out=None): - return torch_frontend.heaviside(self, values, out=out) - - def new_full( - self, - size, - fill_value, - *, - dtype=None, - device=None, - requires_grad=False, - layout=None, - pin_memory=False, - ): - dtype = ivy.dtype(self.ivy_array) if dtype is None else dtype - if ivy.is_float_dtype(dtype): - fill_value = float(fill_value) - elif ivy.is_int_dtype(dtype): - fill_value = int(fill_value) - elif ivy.is_bool_dtype(dtype): - fill_value = bool(fill_value) - device = ivy.dev(self.ivy_array) if device is None else device - _data = ivy.full(size, fill_value, dtype=dtype, device=device) - return torch_frontend.tensor(_data) - - def new_empty( - self, - size, - *, - dtype=None, - device=None, - requires_grad=False, - layout=None, - pin_memory=False, - ): - dtype = ivy.dtype(self.ivy_array) if dtype is None else dtype - device = ivy.dev(self.ivy_array) if device is None else device - _data = ivy.empty(size, dtype=dtype, device=device) - return torch_frontend.tensor(_data) - - def unfold(self, dimension, size, step): - slices = [] - for i in range(0, self.shape[dimension] - size + 1, step): - slices.append(self.ivy_array[i : i + size]) - return torch_frontend.stack(slices) - - def long(self, memory_format=None): - self.ivy_array = ivy.astype(self.ivy_array, ivy.int64, copy=False) - return self - - @numpy_to_torch_style_args - def max(self, dim=None, keepdim=False): - return torch_frontend.max(self, dim=dim, keepdim=keepdim) - - @property - def is_quantized(self): - return "q" in ivy.dtype(self.ivy_array) - - @property - def is_cuda(self): - return "gpu" in ivy.dev(self.ivy_array) - - @property - def is_meta(self): - return "meta" in ivy.dev(self.ivy_array) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def pow(self, exponent): - return torch_frontend.pow(self, exponent) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def pow_(self, exponent): - self.ivy_array = self.pow(exponent).ivy_array - return self - - def size(self, dim=None): - shape = self.shape - if dim is None: - return shape - else: - try: - return shape[dim] - except IndexError: - raise IndexError( - "Dimension out of range (expected to be in range of [{}, {}], " - "but got {}".format(len(shape), len(shape) - 1, dim) - ) - - def matmul(self, other): - return torch_frontend.matmul(self, other) - - def argwhere(self): - return torch_frontend.argwhere(self) - - @numpy_to_torch_style_args - @with_unsupported_dtypes({"2.0.1 and below": ("complex",)}, "torch") - def argmax(self, dim=None, keepdim=False): - return torch_frontend.argmax(self, dim=dim, keepdim=keepdim) - - @numpy_to_torch_style_args - @with_unsupported_dtypes({"2.0.1 and below": ("complex",)}, "torch") - def argmin(self, dim=None, keepdim=False): - return torch_frontend.argmin(self, dim=dim, keepdim=keepdim) - - @with_unsupported_dtypes({"2.0.1 and below": ("complex",)}, "torch") - def argsort(self, dim=-1, descending=False): - return torch_frontend.argsort(self, dim=dim, descending=descending) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def ceil(self): - return torch_frontend.ceil(self) - - @numpy_to_torch_style_args - def min(self, dim=None, keepdim=False): - return torch_frontend.min(self, dim=dim, keepdim=keepdim) - - def permute(self, *args, dims=None): - if args and dims: - raise TypeError("permute() got multiple values for argument 'dims'") - if dims is not None: - return torch_frontend.permute(self, dims) - if args: - if isinstance(args[0], (tuple, list, ivy.Shape)): - dims = args[0] - return torch_frontend.permute(self, dims) - else: - return torch_frontend.permute(self, args) - return torch_frontend.permute(self) - - @numpy_to_torch_style_args - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def mean(self, dim=None, keepdim=False): - return torch_frontend.mean(self, dim=dim, keepdim=keepdim) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - @numpy_to_torch_style_args - def nanmean(self, dim=None, keepdim=False): - return torch_frontend.nanmean(self, dim=dim, keepdim=keepdim) - - @numpy_to_torch_style_args - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def median(self, dim=None, keepdim=False): - return torch_frontend.median(self, dim=dim, keepdim=keepdim) - - def transpose(self, dim0, dim1): - return torch_frontend.transpose(self, dim0=dim0, dim1=dim1) - - def transpose_(self, dim0, dim1): - self.ivy_array = self.transpose(dim0, dim1).ivy_array - return self - - def t(self): - return torch_frontend.t(self) - - def flatten(self, start_dim=0, end_dim=-1): - return torch_frontend.flatten(self, start_dim, end_dim) - - @numpy_to_torch_style_args - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def cumsum(self, dim, *, dtype=None): - return torch_frontend.cumsum(self, dim, dtype=dtype) - - @numpy_to_torch_style_args - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def cumsum_(self, dim, *, dtype=None): - self.ivy_array = self.cumsum(dim, dtype).ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def inverse(self): - return torch_frontend.inverse(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("bool",)}, "torch") - def neg(self): - return torch_frontend.negative(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("bool",)}, "torch") - def neg_(self): - self.ivy_array = torch_frontend.negative(self).ivy_array - return self - - __neg__ = neg - - def int(self, memory_format=None): - self.ivy_array = ivy.astype(self.ivy_array, ivy.int32, copy=False) - return self - - def half(self, memory_format=None): - self.ivy_array = ivy.astype(self.ivy_array, ivy.float16, copy=False) - return self - - def bool(self, memory_format=None): - self.ivy_array = ivy.astype(self.ivy_array, ivy.bool, copy=False) - return self - - def type(self, dtype=None, non_blocking=False, **kwargs): - if ivy.exists(dtype): - self.ivy_array = ivy.astype(self.ivy_array, dtype) - return self - else: - return str(self.dtype) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def type_as(self, other): - if self.dtype != other.dtype: - self.ivy_array = ivy.astype(self.ivy_array, other.dtype) - return self - - def byte(self, memory_format=None): - self.ivy_array = ivy.astype(self.ivy_array, ivy.uint8, copy=False) - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def ne(self, other): - return torch_frontend.ne(self, other) - - @numpy_to_torch_style_args - def squeeze(self, dim=None): - return torch_frontend.squeeze(self, dim) - - @numpy_to_torch_style_args - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16", "uint16")}, "torch") - def squeeze_(self, dim=None): - self.ivy_array = self.squeeze(dim).ivy_array - return self - - def flip(self, dims): - return torch_frontend.flip(self, dims) - - def fliplr(self): - return torch_frontend.fliplr(self) - - def sort(self, dim=-1, descending=False): - return torch_frontend.sort(self, dim=dim, descending=descending) - - def tril(self, diagonal=0): - return torch_frontend.tril(self, diagonal=diagonal) - - def tril_(self, diagonal=0): - self.ivy_array = self.tril(diagonal=diagonal).ivy_array - return self - - def index_select(self, dim, index): - return torch_frontend.index_select(self, dim, index) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "complex")}, "torch") - def clamp(self, min=None, max=None): - return torch_frontend.clamp(self, min=min, max=max) - - @with_unsupported_dtypes({"2.0.1 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 - - @with_unsupported_dtypes( - {"2.0.1 and below": ("bool", "bfloat16", "float16", "complex")}, "torch" - ) - def clamp_min(self, min=None): - return torch_frontend.clamp(self, min=min) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def sqrt(self): - return torch_frontend.sqrt(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def rsqrt(self): - return torch_frontend.rsqrt(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def rsqrt_(self): - self.ivy_array = self.rsqrt().ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def sqrt_(self): - self.ivy_array = self.sqrt().ivy_array - return self - - def where(self, condition, other): - return torch_frontend.tensor(torch_frontend.where(condition, self, other)) - - def clone(self, memory_format=None): - return torch_frontend.tensor(ivy.array(self.ivy_array, copy=True)) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def acosh(self): - return torch_frontend.acosh(self) - - def masked_fill(self, mask, value): - return torch_frontend.tensor( - torch_frontend.where(mask, value, self), dtype=self.dtype - ) - - def masked_fill_(self, mask, value): - self.ivy_array = self.masked_fill(mask, value).ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def index_add_(self, dim, index, source, *, alpha=1): - self.ivy_array = torch_frontend.index_add( - self, dim, index, source, alpha=alpha - ).ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def index_add(self, dim, index, source, *, alpha=1): - return torch_frontend.index_add( - self._ivy_array, dim, index, source, alpha=alpha - ) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def acosh_(self): - self.ivy_array = self.acosh().ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def numpy(self): - return np_frontend_array(self.ivy_array) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def sigmoid(self): - return torch_frontend.sigmoid(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def sigmoid_(self): - self.ivy_array = self.sigmoid().ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def softmax(self, dim=None, dtype=None): - return torch_frontend.nn.functional.softmax(self, dim=dim, dtype=dtype) - - def repeat_interleave(self, repeats, dim=None, *, output_size=None): - return torch_frontend.repeat_interleave(self, repeats, dim) - - def repeat(self, *args, repeats=None): - if args and repeats: - raise ivy.utils.exceptions.IvyException( - "repeat() got multiple values for argument 'repeats'" - ) - if args: - if isinstance(args[0], (tuple, list, ivy.Shape)): - repeats = args[0] - else: - repeats = args - elif not isinstance(repeats, (tuple, list)): - raise ivy.utils.exceptions.IvyException( - "repeat(): argument 'repeats' must be tuple of ints" - ) - - return torch_frontend.tile(self, repeats) - - @numpy_to_torch_style_args - def unbind(self, dim=0): - return torch_frontend.unbind(self, dim=dim) - - def remainder(self, other, *, out=None): - return torch_frontend.remainder(self, other, out=out) - - @with_supported_dtypes( - {"2.0.1 and below": ("float16", "float32", "float64", "bfloat16")}, "torch" - ) - def reciprocal_(self): - self.ivy_array = torch_frontend.reciprocal(self).ivy_array - return self - - def remainder_(self, other, *, out=None): - self.ivy_array = torch_frontend.remainder(self, other, out=out).ivy_array - return self - - def bitwise_not_(self): - self.ivy_array = self.bitwise_not().ivy_array - return self - - def bitwise_and_(self, other): - self.ivy_array = self.bitwise_and(other).ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def atan2_(self, other): - self.ivy_array = self.atan2(other).ivy_array - return self - - def fmin(self, other): - return torch_frontend.fmin(self, other) - - def msort(self): - return torch_frontend.msort(self) - - @with_unsupported_dtypes( - {"2.0.1 and below": ("float16", "bfloat16", "complex")}, "torch" - ) - def trunc(self): - return torch_frontend.trunc(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "complex")}, "torch") - def trunc_(self): - self.ivy_array = self.trunc().ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "complex")}, "torch") - def fix(self): - return torch_frontend.fix(self) - - @with_unsupported_dtypes( - {"2.0.1 and below": ("float16", "bfloat16", "complex")}, "torch" - ) - def fix_(self): - self.ivy_array = self.fix().ivy_array - return self - - def isinf(self): - return torch_frontend.isinf(self._ivy_array) - - def is_complex(self): - return torch_frontend.is_complex(self._ivy_array) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def isreal(self): - return torch_frontend.isreal(self._ivy_array) - - def addr(self, vec1, vec2, *, beta=1, alpha=1, out=None): - return torch_frontend.addr(self, vec1, vec2, beta=beta, alpha=alpha, out=out) - - def addr_(self, vec1, vec2, *, beta=1, alpha=1): - self.ivy_array = self.addr(vec1, vec2, beta=beta, alpha=alpha).ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def dot(self, tensor): - return torch_frontend.dot(self, tensor) - - @with_supported_dtypes({"2.0.1 and below": ("float32", "float64")}, "torch") - def bernoulli(self, *, generator=None, out=None): - return torch_frontend.bernoulli(self._ivy_array, generator=generator, out=out) - - # Special Methods # - # -------------------# - - def __bool__(self): - if len(self.shape) == sum(self.shape): - return torch_frontend.tensor(self.ivy_array.to_scalar().__bool__()) - raise ValueError( - "The truth value of an array with more than one element is ambiguous. " - "Use a.any() or a.all()" - ) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def __add__(self, other): - return torch_frontend.add(self, other) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def __mod__(self, other): - return torch_frontend.remainder(self, other) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def __pow__(self, exponent): - return self.pow(exponent) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def __rpow__(self, other): - return torch_frontend.pow(other, self) - - def __long__(self, memory_format=None): - return self.long() - - def __getitem__(self, query, /): - ivy_args = ivy.nested_map([self, query], _to_ivy_array) - ret = ivy.get_item(*ivy_args) - return torch_frontend.Tensor(ret, _init_overload=True) - - def __setitem__(self, key, value, /): - key, value = ivy.nested_map([key, value], _to_ivy_array) - self.ivy_array[key] = value - - def __iter__(self): - if self.ndim == 0: - raise TypeError("iteration over a 0-d tensor not supported") - for i in range(self.shape[0]): - yield self[i] - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def __radd__(self, other): - return torch_frontend.add(other, self) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def __mul__(self, other): - return torch_frontend.mul(self, other) - - @with_unsupported_dtypes({"2.0.1 and below": "bfloat16"}, "torch") - def __matmul__(self, other): - return torch_frontend.matmul(self, other) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def __rmul__(self, other): - return torch_frontend.mul(other, self) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def __sub__(self, other): - return torch_frontend.subtract(self, other) - - def __truediv__(self, other): - return torch_frontend.div(self, other) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "complex")}, "torch") - def __floordiv__(self, other): - return torch_frontend.floor_divide(self, other) - - def __iadd__(self, other): - ret = torch_frontend.add(self, other) - self.ivy_array = ivy.inplace_update( - self.ivy_array, ivy.astype(ret.ivy_array, self.dtype) - ) - return self - - def __imod__(self, other): - ret = torch_frontend.remainder(self, other) - self.ivy_array = ivy.inplace_update( - self.ivy_array, ivy.astype(ret.ivy_array, self.dtype) - ) - return self - - def __imul__(self, other): - ret = torch_frontend.mul(self, other) - self.ivy_array = ivy.inplace_update( - self.ivy_array, ivy.astype(ret.ivy_array, self.dtype) - ) - return self - - def __isub__(self, other): - ret = torch_frontend.subtract(self, other) - self.ivy_array = ivy.inplace_update( - self.ivy_array, ivy.astype(ret.ivy_array, self.dtype) - ) - return self - - def __itruediv__(self, other): - ret = torch_frontend.div(self, other) - self.ivy_array = ivy.inplace_update( - self.ivy_array, ivy.astype(ret.ivy_array, self.dtype) - ) - return self - - def __int__(self): - item = self.item() - if isinstance(item, complex): - if item.imag != 0: - raise TypeError("can't convert complex to int without overflow") - item = item.real - return int(item) - - def __float__(self): - item = self.item() - if isinstance(item, complex): - if item.imag != 0: - raise TypeError("can't convert complex to float without overflow") - item = item.real - return float(item) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def __eq__(self, other): - return torch_frontend.eq(self, other) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def __gt__(self, other): - return torch_frontend.greater(self, other) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def __ge__(self, other): - return torch_frontend.greater_equal(self, other) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def __ne__(self, other): - return self.ne(other) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def __rsub__(self, other): - return torch_frontend.subtract(other, self) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def __lt__(self, other): - return torch_frontend.less(self, other) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def __le__(self, other): - return torch_frontend.less_equal(self, other) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def __or__(self, other): - return torch_frontend.bitwise_or(self, other) - - def __invert__(self): - return torch_frontend.bitwise_not(self) - - def __and__(self, other): - return torch_frontend.bitwise_and(self, other) - - def __array__(self, dtype=None): - if dtype is None: - return ivy.to_numpy(self.ivy_array) - else: - return ivy.to_numpy(self.ivy_array).astype(dtype, copy=False) - - def __array_wrap__(self, array): - if array.dtype == bool: - array = array.astype("uint8") - return torch_frontend.tensor(array) - - # Method aliases - absolute, absolute_ = abs, abs_ - clip, clip_ = clamp, clamp_ - ndimension = dim - subtract = sub - sub_ = subtract_ - eq = equal - - def bitwise_xor(self, other): - return torch_frontend.bitwise_xor(self, other) - - def bitwise_xor_(self, other): - self.ivy_array = self.bitwise_xor(other).ivy_array - return self - - def item(self): - if all(dim == 1 for dim in self.shape): - return self.ivy_array.to_scalar() - else: - raise ValueError( - "only one element tensors can be converted to Python scalars" - ) - - @numpy_to_torch_style_args - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def cumprod(self, dim, dtype): - return torch_frontend.cumprod(self, dim, dtype=dtype) - - @numpy_to_torch_style_args - def count_nonzero(self, dim): - return torch_frontend.count_nonzero(self, dim=dim) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16", "float16")}, "torch") - def exp(self): - return torch_frontend.exp(self) - - @with_unsupported_dtypes( - {"2.0.1 and below": ("bfloat16", "float16", "complex")}, "torch" - ) - def expm1(self): - return torch_frontend.expm1(self) - - # remove "bfloat16" from the below decorator after fixing ivy.Array.__repr__ method - @with_unsupported_dtypes( - {"2.0.1 and below": ("bfloat16", "float16", "complex")}, "torch" - ) - def expm1_(self): - self.ivy_array = torch_frontend.expm1(self).ivy_array - return self - - # fmt: off - @with_unsupported_dtypes({"2.0.1 and below": ("int8", "int16", "int32", "int64", "uint8", "bool", "float16",)},"torch",) # noqa - def exp_(self): - self.ivy_array = self.exp().ivy_array - return self - # fmt: on - - def mul(self, other): - return torch_frontend.mul(self, other) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def ceil_(self): - self.ivy_array = torch_frontend.ceil(self).ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def mul_(self, other): - self.ivy_array = self.mul(other).ivy_array - # the return dtype is the same as the input dtype - self.ivy_array = self.to(self.dtype).ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16", "float16")}, "torch") - def round(self, *, decimals=0): - return torch_frontend.round(self, decimals=decimals) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16", "float16")}, "torch") - def round_(self, *, decimals=0): - self.ivy_array = self.round(decimals=decimals).ivy_array - return self - - @numpy_to_torch_style_args - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "complex")}, "torch") - def cross(self, other, dim=-1): - return torch_frontend.cross(self, other, dim=dim) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def det(self): - return torch_frontend.det(self) - - def reciprocal(self): - return torch_frontend.reciprocal(self) - - def fill_(self, value): - self.ivy_array = torch_frontend.full_like( - self, value, dtype=self.dtype, device=self.device - ).ivy_array - return self - - def nonzero(self, as_tuple=False): - return torch_frontend.nonzero(self, as_tuple=as_tuple) - - def mm(self, mat2): - return torch_frontend.mm(self, mat2) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16", "float16")}, "torch") - def square(self): - return torch_frontend.square(self._ivy_array) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def log10(self): - return torch_frontend.log10(self._ivy_array) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def log10_(self): - self.ivy_array = self.log10().ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16", "uint16")}, "torch") - def zero_(self): - self.ivy_array = torch_frontend.zeros_like(self).ivy_array - return self - - def short(self, memory_format=None): - self.ivy_array = ivy.astype(self.ivy_array, ivy.int16, copy=False) - return self - - @numpy_to_torch_style_args - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def prod(self, dim=None, keepdim=False, *, dtype=None): - return torch_frontend.prod(self, dim=dim, keepdim=keepdim, dtype=dtype) - - def div(self, other, *, rounding_mode=None): - return torch_frontend.div(self, other, rounding_mode=rounding_mode) - - def div_(self, other, *, rounding_mode=None): - self.ivy_array = self.div(other, rounding_mode=rounding_mode).ivy_array - return self - - @with_supported_dtypes( - {"2.0.1 and below": ("float16", "float32", "float64", "bfloat16")}, "torch" - ) - def true_divide_(self, other): - self.ivy_array = self.div(other, rounding_mode=None).ivy_array - return self - - def normal_(self, mean=0, std=1, *, generator=None): - self.ivy_array = ivy.random_normal( - mean=mean, - std=std, - shape=self.ivy_array.shape, - dtype=self.dtype, - device=self.device, - ) - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def addcdiv(self, tensor1, tensor2, *, value=1): - return torch_frontend.addcdiv(self, tensor1, tensor2, value=value) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def addcmul(self, tensor1, tensor2, *, value=1): - return torch_frontend.addcmul(self, tensor1, tensor2, value=value) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def addcmul_(self, tensor1, tensor2, *, value=1): - self.ivy_array = self.addcmul(tensor1, tensor2, value=value).ivy_array - return self - - sign_decorator_dtypes = ("float16", "complex", "bool") - - @with_unsupported_dtypes({"2.0.1 and below": sign_decorator_dtypes}, "torch") - def sign(self): - return torch_frontend.sign(self._ivy_array) - - @with_unsupported_dtypes({"2.0.1 and below": sign_decorator_dtypes}, "torch") - def sign_(self): - self.ivy_array = self.sign().ivy_array - return self - - @numpy_to_torch_style_args - def std(self, dim=None, unbiased=True, keepdim=False, *, out=None): - return torch_frontend.std( - self, dim=dim, unbiased=unbiased, keepdim=keepdim, out=out - ) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def fmod(self, other, *, out=None): - return torch_frontend.fmod(self, other, out=out) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def fmod_(self, other): - self.ivy_array = self.fmod(other).ivy_array - return self - - def norm(self, p="fro", dim=None, keepdim=False, dtype=None): - return torch_frontend.norm(self, p=p, dim=dim, keepdim=keepdim, dtype=dtype) - - def tolist(self): - return self._ivy_array.to_list() - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def multiply(self, other, *, out=None): - return torch_frontend.multiply(self, other, out=out) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def multiply_(self, other, *, out=None): - self.ivy_array = torch_frontend.multiply(self, other, out=out).ivy_array - return self - - @numpy_to_torch_style_args - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "complex")}, "torch") - def topk(self, k, dim=None, largest=True, sorted=True): - return torch_frontend.topk(self, k, dim=dim, largest=largest, sorted=sorted) - - rshift_dtypes = ("float16", "bfloat16", "float32", "float64", "bool", "complex") - - @with_unsupported_dtypes({"2.0.1 and below": rshift_dtypes}, "torch") - def bitwise_right_shift(self, other, *, out=None): - return torch_frontend.bitwise_right_shift(self._ivy_array, other) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def logdet(self): - chol = torch_frontend.cholesky(self) - return 2 * torch_frontend.sum( - torch_frontend.log(torch_frontend.real(torch_frontend.diagonal(chol))) - ) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def copysign(self, other, *, out=None): - return torch_frontend.copysign(self, other, out=out) - - @with_supported_dtypes( - {"2.0.1 and below": ("float16", "float32", "float64")}, "torch" - ) - def copysign_(self, other, *, out=None): - self.ivy_array = self.copysign(other, out=out).ivy_array - return self - - @with_unsupported_dtypes( - {"2.0.1 and below": ("complex", "bfloat16", "bool")}, "torch" - ) - def greater(self, other, *, out=None): - return torch_frontend.greater(self, other, out=out) - - gt = greater - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16", "bool")}, "torch") - def greater_(self, other): - self.ivy_array = ivy.astype(self.greater(other).ivy_array, self.dtype) - return self - - gt_ = greater_ - - @with_unsupported_dtypes( - {"2.0.1 and below": ("complex", "bfloat16", "bool")}, "torch" - ) - def greater_equal(self, other, *, out=None): - return torch_frontend.greater_equal(self, other, out=out) - - ge = greater_equal - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16", "bool")}, "torch") - def greater_equal_(self, other): - self.ivy_array = ivy.astype(self.greater_equal(other).ivy_array, self.dtype) - return self - - ge_ = greater_equal_ - - @with_unsupported_dtypes( - {"2.0.1 and below": ("complex", "bfloat16", "bool")}, "torch" - ) - def less(self, other, *, out=None): - return torch_frontend.less(self, other, out=out) - - lt = less - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16", "bool")}, "torch") - def less_(self, other): - self.ivy_array = ivy.astype(self.less(other).ivy_array, self.dtype) - return self - - lt_ = less_ - - @with_unsupported_dtypes( - {"2.0.1 and below": ("complex", "bfloat16", "bool")}, "torch" - ) - def less_equal(self, other, *, out=None): - return torch_frontend.less_equal(self, other, out=out) - - le = less_equal - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16", "bool")}, "torch") - def less_equal_(self, other): - self.ivy_array = ivy.astype(self.less_equal(other).ivy_array, self.dtype) - return self - - le_ = less_equal_ - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def eq_(self, other): - self.ivy_array = ivy.astype( - torch_frontend.eq(self, other).ivy_array, self.dtype - ) - return self - - @numpy_to_torch_style_args - def var(self, dim=None, *, correction=1, keepdim=False): - return torch_frontend.var(self, dim=dim, unbiased=correction, keepdim=keepdim) - - def narrow(self, dim, start, length): - return torch_frontend.narrow(self, dim=dim, start=start, length=length) - - def as_strided(self, size, stride, storage_offset=None): - return torch_frontend.as_strided( - self, size=size, stride=stride, storage_offset=storage_offset - ) - - def stride(self, dim=None): - strides = [ - stride // math.ceil(ivy.dtype_bits(self.dtype) / 8) - for stride in self.ivy_array.strides - ] - if dim is not None: - return strides[dim] - return strides - - @with_supported_dtypes( - {"2.0.1 and below": ("float32", "float64", "bfloat16")}, "torch" - ) - def log1p(self): - promoted_type = ivy.promote_types(self.dtype, "float32") - return torch_frontend.log1p(self).to(promoted_type) - - @with_supported_dtypes({"2.0.1 and below": ("float32", "float64")}, "torch") - def log1p_(self): - promoted_type = ivy.promote_types(self.dtype, "float32") - self.ivy_array = torch_frontend.log1p(self).to(promoted_type).ivy_array - return self - - def baddbmm(self, batch1, batch2, *, beta=1, alpha=1): - return torch_frontend.baddbmm( - self, batch1=batch1, batch2=batch2, beta=beta, alpha=alpha - ) - - def baddbmm_(self, batch1, batch2, *, beta=1, alpha=1): - self.ivy_array = torch_frontend.baddbmm( - self, batch1=batch1, batch2=batch2, beta=beta, alpha=alpha - ).ivy_array - return self - - def bmm(self, mat2): - return torch_frontend.bmm(self, mat2=mat2) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def floor_(self): - self.ivy_array = self.floor().ivy_array - return self - - def diag(self, diagonal=0): - return torch_frontend.diag(self, diagonal=diagonal) - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16",)}, "torch") - def diagonal(self, offset=0, dim1=0, dim2=1): - return torch_frontend.diagonal(self, offset=offset, dim1=dim1, dim2=dim2) - - def gather(self, dim, index): - return torch_frontend.gather(self, dim=dim, index=index) - - def take_along_dim(self, indices, dim): - return torch_frontend.take_along_dim(self, indices=indices, dim=dim) - - def movedim(self, source, destination): - return torch_frontend.movedim(self, source=source, destination=destination) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch") - def addcdiv_(self, tensor1, tensor2, *, value=1): - self.ivy_array = self.addcdiv( - tensor1=tensor1, tensor2=tensor2, value=value - ).ivy_array - return self - - @with_unsupported_dtypes({"2.0.1 and below": ("bfloat16", "float16")}, "torch") - def cholesky(self, upper=False): - return torch_frontend.cholesky(self, upper=upper) - - def tile(self, *reps): - if ( - isinstance(reps, Iterable) - and len(reps) == 1 - and isinstance(reps[0], Iterable) - ): - reps = reps[0] - return torch_frontend.tile(self, reps) - - def apply_(self, callable, /): - if self.device != "cpu": - raise Exception("apply_ is only supported on cpu tensors") - self.ivy_array = callable(self.ivy_array) - return self - - def requires_grad_(self, requires_grad=True): - self._requires_grad = requires_grad - return self - - def backward(self, gradient=None, retain_graph=None, create_graph=False): - if gradient is None and int(torch_frontend.numel(self)) > 1: - raise RuntimeError("grad can be implicitly created only for scalar outputs") - if self.grad_fn is None and self._grads is None: - assert self.shape == gradient.shape, "Mismatch in shape" - self._grads = gradient - return - _grad_list = self.grad_fn( - gradient if gradient is not None else torch_frontend.tensor(1.0) - ) - for idx, next_function in enumerate(self.grad_fn.next_functions): - if next_function.__self__.grad_fn is not None: - next_function.__self__.backward(_grad_list[idx]) - else: - next_function(_grad_list[idx]) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def logaddexp(self, other): - return torch_frontend.logaddexp(self, other) - - def angle(self): - return torch_frontend.angle(self) - - @with_supported_dtypes( - { - "2.5.0 and below": ( - "int64", - "float64", - "complex128", - "float32", - "complex64", - "int32", - ) - }, - "paddle", - ) - def adjoint(self): - return torch_frontend.adjoint(self) - - @with_unsupported_dtypes( - {"2.0.1 and below": ("int16", "float16", "bfloat16")}, "torch" - ) - def conj(self): - return torch_frontend.conj(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, "torch") - def svd(self, some=True, compute_uv=True, *, out=None): - return torch_frontend.svd(self, some=some, compute_uv=compute_uv, out=out) - - @with_unsupported_dtypes( - {"2.0.1 and below": ("float16", "bfloat16", "float32", "float64", "complex")}, - "torch", - ) - def gcd(self, other, *, out=None): - return torch_frontend.gcd(self, other, out=out) - - @with_unsupported_dtypes( - { - "2.0.1 and below": ( - "float16", - "bfloat16", - "uint16", - "bool", - "complex64", - "complex128", - ) - }, - "torch", - ) - def isnan(self): - return torch_frontend.isnan(self) - - def char(self): - self.ivy_array = ivy.asarray(self.ivy_array, dtype=torch_frontend.char) - return self - - @with_unsupported_dtypes( - { - "2.0.1 and below": ( - "float16", - "bfloat16", - "float32", - "float64", - "complex", - "uint8", - "int8", - ) - }, - "torch", - ) - def lcm(self, other, *, out=None): - return torch_frontend.lcm(self, other, out=out) - - @with_unsupported_dtypes( - { - "2.0.1 and below": ( - "bfloat16", - "int8", - "uint8", - "int16", - "complex128", - "complex64", - "bool", - ) - }, - "torch", - ) - def triu_(self, diagonal=0): - self.ivy_array = torch_frontend.triu(self, diagonal).ivy_array - return self - - @with_unsupported_dtypes( - {"2.0.1 and below": ("float16", "bfloat16")}, - "torch", - ) - def quantile(self, q, dim=None, keepdim=False, *, interpolation="linear", out=None): - return torch_frontend.quantile( - self, q, axis=dim, keepdims=keepdim, interpolation=interpolation, out=out - ) - - @with_unsupported_dtypes( - { - "2.0.1 and below": ( - "float16", - "bfloat16", - ) - }, - "torch", - ) - def sinc(self): - return torch_frontend.sinc(self) - - @with_unsupported_dtypes({"2.0.1 and below": ("uint8",)}, "torch") - def index_fill(self, dim, index, value): - arr = torch_frontend.moveaxis(self, dim, 0) - arr[ivy.to_list(index)] = value - arr = torch_frontend.moveaxis(self, 0, dim) - return arr - - @with_unsupported_dtypes( - { - "2.0.1 and below": ( - "uint16", - "uint32", - "uint64", - "bfloat16", - "float16", - "complex64", - "complex128", - ) - }, - "torch", - ) - def cummax(self, dim): - return torch_frontend.cummax(self, dim) - - -class Size(tuple): - def __new__(cls, iterable=()): - new_iterable = list() - for i, item in enumerate(iterable): - if isinstance(item, int): - new_iterable.append(item) - continue - try: - new_iterable.append(int(item)) - except Exception: - raise TypeError(f"Expected int, but got {type(item)} at index {i}") - return super(Size, cls).__new__(cls, tuple(new_iterable)) - - def __init__(self, shape) -> None: - self._ivy_shape = ( - ivy.shape(shape) if not isinstance(shape, ivy.Shape) else shape - ) - - def __repr__(self): - return f'ivy.frontends.torch.Size([{", ".join(str(d) for d in self)}])' - - @property - def ivy_shape(self): - return self._ivy_shape From 258dbe0c902df78aa45c536629e614acbeeb658a Mon Sep 17 00:00:00 2001 From: Arsalan ali Date: Thu, 31 Aug 2023 14:12:15 +0500 Subject: [PATCH 5/5] Delete ivy_tests/test_ivy/test_frontends/test_torch/test_tensor.py --- .../test_frontends/test_torch/test_tensor.py | 12600 ---------------- 1 file changed, 12600 deletions(-) delete mode 100644 ivy_tests/test_ivy/test_frontends/test_torch/test_tensor.py 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 deleted file mode 100644 index 34dad481b0fd5..0000000000000 --- a/ivy_tests/test_ivy/test_frontends/test_torch/test_tensor.py +++ /dev/null @@ -1,12600 +0,0 @@ -# global -import pytest -from types import SimpleNamespace -import numpy as np - -from ivy_tests.test_ivy.test_frontends.test_torch.test_comparison_ops import ( - _topk_helper, -) -from ivy_tests.test_ivy.test_frontends.test_torch.test_creation_ops import ( - _as_strided_helper, -) -from ivy_tests.test_ivy.test_frontends.test_torch.test_indexing_slicing_joining_mutating_ops import ( # noqa: E501 - _dtype_input_dim_start_length, -) -from ivy_tests.test_ivy.test_frontends.test_torch.test_reduction_ops import ( - _get_axis_and_p, -) - -import ivy -from hypothesis import strategies as st, given, assume - -# local -import ivy_tests.test_ivy.helpers as helpers -from ivy_tests.test_ivy.test_frontends.test_torch.test_blas_and_lapack_ops import ( - _get_dtype_and_3dbatch_matrices, - _get_dtype_input_and_matrices, - _get_dtype_input_and_mat_vec, -) -from ivy.functional.frontends.torch import Tensor -from ivy_tests.test_ivy.helpers import handle_frontend_method, BackendHandler -from ivy_tests.test_ivy.test_functional.test_core.test_searching import ( - _broadcastable_trio, -) -from ivy_tests.test_ivy.test_functional.test_core.test_manipulation import ( # noqa - _get_splits, -) -from ivy_tests.test_ivy.test_frontends.test_torch.test_miscellaneous_ops import ( # noqa - dtype_value1_value2_axis, -) -from ivy_tests.test_ivy.test_frontends.test_torch.test_linalg import ( # noqa - _get_dtype_and_matrix, -) -from ivy_tests.test_ivy.test_functional.test_core.test_statistical import ( - _get_castable_dtype, - _statistical_dtype_values, -) - -from ivy_tests.test_ivy.test_functional.test_experimental.test_core.test_statistical import ( # noqa - _quantile_helper, -) - -try: - import torch -except ImportError: - torch = SimpleNamespace() - -CLASS_TREE = "ivy.functional.frontends.torch.Tensor" - - -# --- Helpers --- # -# --------------- # - - -@st.composite -def _array_idxes_n_dtype(draw, **kwargs): - num_dims = draw(helpers.ints(min_value=1, max_value=4)) - dtype, x = draw( - helpers.dtype_and_values( - **kwargs, min_num_dims=num_dims, max_num_dims=num_dims, shared_dtype=True - ) - ) - idxes = draw( - st.lists( - helpers.ints(min_value=0, max_value=num_dims - 1), - min_size=num_dims, - max_size=num_dims, - unique=True, - ) - ) - return x, idxes, dtype - - -@st.composite -def _arrays_dim_idx_n_dtypes(draw): - num_dims = draw(st.shared(helpers.ints(min_value=1, max_value=4), key="num_dims")) - num_arrays = 2 - common_shape = draw( - helpers.lists( - x=helpers.ints(min_value=2, max_value=3), - min_size=num_dims - 1, - max_size=num_dims - 1, - ) - ) - _dim = draw(helpers.ints(min_value=0, max_value=num_dims - 1)) - unique_dims = draw( - helpers.lists( - x=helpers.ints(min_value=2, max_value=3), - min_size=num_arrays, - max_size=num_arrays, - ) - ) - - min_dim = min(unique_dims) - max_dim = max(unique_dims) - _idx = draw( - helpers.array_values( - shape=min_dim, - dtype="int64", - min_value=0, - max_value=max_dim, - exclude_min=False, - ) - ) - - xs = list() - available_input_types = draw(helpers.get_dtypes("numeric")) - input_dtypes = draw( - helpers.array_dtypes( - available_dtypes=available_input_types, - num_arrays=num_arrays, - shared_dtype=True, - ) - ) - for ud, dt in zip(unique_dims, input_dtypes): - x = draw( - helpers.array_values( - shape=common_shape[:_dim] + [ud] + common_shape[_dim:], - dtype=dt, - large_abs_safety_factor=2.5, - small_abs_safety_factor=2.5, - safety_factor_scale="log", - ) - ) - xs.append(x) - return xs, input_dtypes, _dim, _idx - - -# Helper functions -@st.composite -def _dtypes(draw): - return draw( - st.shared( - helpers.list_of_size( - x=st.sampled_from( - draw(helpers.get_dtypes("numeric", prune_function=False)) - ), - size=1, - ), - key="dtype", - ) - ) - - -@st.composite -def _expand_helper(draw): - num_dims = draw(st.integers(min_value=1, max_value=10)) - shape = draw( - helpers.get_shape(min_num_dims=num_dims, max_num_dims=num_dims).filter( - lambda x: any(i == 1 for i in x) - ) - ) - new_shape = draw( - helpers.get_shape(min_num_dims=num_dims, max_num_dims=num_dims).filter( - lambda x: all(x[i] == v if v != 1 else True for i, v in enumerate(shape)) - ) - ) - dtype, x = draw( - helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - shape=shape, - ) - ) - return dtype, x, new_shape - - -@st.composite -def _fill_value_and_size( - draw, - *, - min_num_dims=1, - max_num_dims=5, - min_dim_size=1, - max_dim_size=10, -): - if isinstance(min_dim_size, st._internal.SearchStrategy): - min_dim_size = draw(min_dim_size) - if isinstance(max_dim_size, st._internal.SearchStrategy): - max_dim_size = draw(max_dim_size) - - available_dtypes = draw(helpers.get_dtypes("numeric")) - dtype = draw( - helpers.array_dtypes( - num_arrays=1, - available_dtypes=available_dtypes, - ) - ) - array = draw( - helpers.array_values( - dtype=dtype[0], - shape=(1,), - ) - ) - dtype.append("int32") - size = draw( - st.shared( - helpers.get_shape( - min_num_dims=min_num_dims, - max_num_dims=max_num_dims, - min_dim_size=min_dim_size, - max_dim_size=max_dim_size, - ), - key="shape", - ) - ) - fill_value = draw(helpers.ints()) if "int" in dtype[0] else draw(helpers.floats()) - - return dtype, [array, size, fill_value] - - -@st.composite -def _get_clamp_inputs(draw): - shape = draw( - helpers.get_shape( - min_num_dims=1, max_num_dims=5, min_dim_size=2, max_dim_size=10 - ) - ) - x_dtype, x = draw( - helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - shape=shape, - ) - ) - min = draw(st.booleans()) - if min: - max = draw(st.booleans()) - min = draw( - helpers.array_values( - 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=26, max_value=50 - ) - ) - if max - else None - ) - else: - min = None - max = draw( - helpers.array_values( - dtype=x_dtype[0], shape=shape, min_value=26, max_value=50 - ) - ) - return x_dtype, x, min, max - - -@st.composite -def _get_clip_min_inputs(draw): - shape = draw( - helpers.get_shape( - min_num_dims=1, max_num_dims=5, min_dim_size=2, max_dim_size=10 - ) - ) - x_dtype, x = draw( - helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - shape=shape, - ) - ) - - min = draw( - helpers.array_values(dtype=x_dtype[0], shape=shape, min_value=0, max_value=25) - ) - - return x_dtype, x, min - - -@st.composite -def _get_dtype_and_multiplicative_matrices(draw): - return draw( - st.one_of( - _get_dtype_input_and_matrices(), - _get_dtype_and_3dbatch_matrices(), - ) - ) - - -@st.composite -def _get_dtype_and_multiplicative_matrices(draw): - return draw( - st.one_of( - _get_dtype_input_and_matrices(), - _get_dtype_and_3dbatch_matrices(), - ) - ) - - -@st.composite -def _get_dtype_input_and_vectors(draw, with_input=False, same_size=False): - dim_size1 = draw(helpers.ints(min_value=2, max_value=5)) - dim_size2 = dim_size1 if same_size else draw(helpers.ints(min_value=2, max_value=5)) - dtype = draw(helpers.get_dtypes("float", full=True)) - dtype = [ - draw(st.sampled_from(tuple(set(dtype).difference({"bfloat16", "float16"})))) - ] - vec1 = draw( - helpers.array_values( - dtype=dtype[0], shape=(dim_size1,), min_value=2, max_value=5 - ) - ) - vec2 = draw( - helpers.array_values( - dtype=dtype[0], shape=(dim_size2,), min_value=2, max_value=5 - ) - ) - if with_input: - input = draw( - helpers.array_values( - dtype=dtype[0], shape=(dim_size1, dim_size2), min_value=2, max_value=5 - ) - ) - return dtype, input, vec1, vec2 - return dtype, vec1, vec2 - - -@st.composite -def _masked_fill_helper(draw): - cond, xs, dtypes = draw(_broadcastable_trio()) - if ivy.is_uint_dtype(dtypes[0]): - fill_value = draw(helpers.ints(min_value=0, max_value=5)) - elif ivy.is_int_dtype(dtypes[0]): - fill_value = draw(helpers.ints(min_value=-5, max_value=5)) - else: - fill_value = draw(helpers.floats(min_value=-5, max_value=5)) - return dtypes[0], xs[0], cond, fill_value - - -@st.composite -def _repeat_helper(draw): - shape = draw( - helpers.get_shape( - min_num_dims=1, max_num_dims=5, min_dim_size=2, max_dim_size=10 - ) - ) - - input_dtype, x = draw( - helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - shape=shape, - ) - ) - - repeats = draw(st.lists(st.integers(min_value=1, max_value=5), min_size=len(shape))) - return input_dtype, x, repeats - - -@st.composite -def _requires_grad(draw): - dtype = draw(_dtypes())[0] - if ivy.is_int_dtype(dtype) or ivy.is_uint_dtype(dtype): - return draw(st.just(False)) - return draw(st.booleans()) - - -@st.composite -def _to_helper(draw): - dtype_x = draw( - helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - num_arrays=2, - large_abs_safety_factor=3, - ) - ) - input_dtype, x = dtype_x - arg = draw(st.sampled_from(["tensor", "dtype", "device"])) - if arg == "tensor": - method_num_positional_args = 1 - method_all_as_kwargs_np = {"other": x[1]} - elif arg == "dtype": - method_num_positional_args = 1 - dtype = draw(helpers.get_dtypes("valid", full=False))[0] - method_all_as_kwargs_np = {"dtype": dtype} - else: - method_num_positional_args = 0 - device = draw(st.just("cpu")) - dtype = draw(helpers.get_dtypes("valid", full=False, none=True))[0] - method_all_as_kwargs_np = {"dtype": dtype, "device": device} - return input_dtype, x, method_num_positional_args, method_all_as_kwargs_np - - -@st.composite -def _unfold_args(draw): - values_dtype, values, axis, shape = draw( - helpers.dtype_values_axis( - available_dtypes=helpers.get_dtypes("float"), - force_int_axis=True, - shape=draw( - helpers.get_shape( - allow_none=False, - min_num_dims=1, - min_dim_size=1, - ) - ), - ret_shape=True, - ) - ) - size = draw( - st.integers( - min_value=1, - max_value=max(shape[axis] - 1, 1), - ) - ) - step = draw( - st.integers( - min_value=1, - max_value=size, - ) - ) - return values_dtype, values, axis, size, step - - -# diagonal -@st.composite -def dims_and_offset(draw, shape): - shape_actual = draw(shape) - dim1 = draw(helpers.get_axis(shape=shape, force_int=True)) - dim2 = draw(helpers.get_axis(shape=shape, force_int=True)) - offset = draw( - st.integers(min_value=-shape_actual[dim1], max_value=shape_actual[dim1]) - ) - return dim1, dim2, offset - - -# --- Main --- # -# ------------ # - - -# __add__ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__add__", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch___add__( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# __and__ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__and__", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=st.one_of(st.just(("bool",)), helpers.get_dtypes("integer")), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch___and__( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__array_wrap__", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - num_arrays=2, - ), -) -def test_torch___array_wrap__( - dtype_and_x, - backend_fw, - frontend, -): - input_dtypes, x = dtype_and_x - if x[1].dtype == "bfloat16": - return - if x[0].dtype == "bfloat16": - ret_gt = torch.tensor(x[0].tolist(), dtype=torch.bfloat16).__array_wrap__(x[1]) - else: - ret_gt = torch.tensor(x[0]).__array_wrap__(x[1]) - with BackendHandler.update_backend(backend_fw) as ivy_backend: - local_importer = ivy_backend.utils.dynamic_import - function_module = local_importer.import_module("ivy.functional.frontends.torch") - ret = function_module.tensor(x[0]).__array_wrap__(x[1]) - assert isinstance(ret, function_module.Tensor) - helpers.value_test( - ret_np_flat=np.array(ret.ivy_array).ravel(), - ret_np_from_gt_flat=ret_gt.numpy().ravel(), - ground_truth_backend="torch", - backend=backend_fw, - ) - - -# __bool__ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__bool__", - dtype_and_x=helpers.dtype_and_values( - max_dim_size=1, - min_value=-1e04, - max_value=1e04, - ), -) -def test_torch___bool__( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# __eq__ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__eq__", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch___eq__( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__floordiv__", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - large_abs_safety_factor=2.5, - small_abs_safety_factor=2.5, - safety_factor_scale="log", - ), -) -def test_torch___floordiv__( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - assume(not np.any(np.isclose(x[1], 0))) - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - atol_=1, - ) - - -# __getitem__ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__getitem__", - dtype_x_index=helpers.dtype_array_query( - available_dtypes=helpers.get_dtypes("valid"), - allow_neg_step=False, - ), -) -def test_torch___getitem__( - dtype_x_index, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x, index = dtype_x_index - helpers.test_frontend_method( - init_input_dtypes=[input_dtype[0]], - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x}, - method_input_dtypes=[*input_dtype[1:]], - method_all_as_kwargs_np={"query": index}, - 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, - init_tree="torch.tensor", - method_name="__gt__", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch___gt__( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# __invert__ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__invert__", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("integer"), - num_arrays=1, - ), -) -def test_torch___invert__( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# __long__ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__long__", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("integer"), - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch___long__( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# __lt__ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__lt__", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch___lt__( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# __matmul__ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__matmul__", - dtype_tensor1_tensor2=_get_dtype_and_multiplicative_matrices(), -) -def test_torch___matmul__( - dtype_tensor1_tensor2, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - dtype, tensor1, tensor2 = dtype_tensor1_tensor2 - helpers.test_frontend_method( - init_input_dtypes=dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": tensor1, - }, - method_input_dtypes=dtype, - method_all_as_kwargs_np={"other": tensor2}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# __mod__ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__mod__", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - ), -) -def test_torch___mod__( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# __mul__ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__mul__", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch___mul__( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# __ne__ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__ne__", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch___ne__( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# __neg__ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__neg__", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch___neg__( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# __or__ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__or__", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch___or__( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# __pow__ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__pow__", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - num_arrays=2, - ), -) -def test_torch___pow__( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - dtype = input_dtype[0] - if "int" in dtype: - x[1] = ivy.abs(x[1]) - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "exponent": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# __radd__ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__radd__", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch___radd__( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# __rmul__ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__rmul__", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch___rmul__( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# __rpow__ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__rpow__", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - num_arrays=2, - min_value=1, - ), -) -def test_torch___rpow__( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - dtype = input_dtype[0] - if "int" in dtype: - x[0] = ivy.abs(x[0]) - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# __rsub__ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__rsub__", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - num_arrays=2, - ), -) -def test_torch___rsub__( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# __setitem__ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__setitem__", - dtypes_x_index_val=helpers.dtype_array_query_val( - available_dtypes=helpers.get_dtypes("valid"), - allow_neg_step=False, - ).filter(lambda x: x[0][0] == x[0][-1]), -) -def test_torch___setitem__( - dtypes_x_index_val, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x, index, val = dtypes_x_index_val - helpers.test_frontend_method( - init_input_dtypes=[input_dtype[0]], - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x}, - method_input_dtypes=[*input_dtype[1:]], - method_all_as_kwargs_np={"key": index, "value": val}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# __sub__ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__sub__", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch___sub__( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# __truediv__ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__truediv__", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - shared_dtype=True, - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch___truediv__( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="__array__", - dtype_and_x=helpers.dtype_and_values(available_dtypes=helpers.get_dtypes("valid")), - dtype=helpers.get_dtypes("valid", full=False), -) -def test_torch__array__( - dtype_and_x, - dtype, - frontend, - backend_fw, -): - input_dtype, x = dtype_and_x - if x[0].dtype == "bfloat16": - return - dtype[0] = np.dtype(dtype[0]) - ret_gt = torch.tensor(x[0]).__array__(dtype[0]) - with BackendHandler.update_backend(backend_fw) as ivy_backend: - local_importer = ivy_backend.utils.dynamic_import - function_module = local_importer.import_module("ivy.functional.frontends.torch") - ret = function_module.tensor(x[0]).__array__(dtype[0]) - - helpers.value_test( - ret_np_flat=ret.ravel(), - ret_np_from_gt_flat=ret_gt.ravel(), - ground_truth_backend="torch", - backend=backend_fw, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="baddbmm_", - dtype_and_matrices=_get_dtype_and_3dbatch_matrices(with_input=True, input_3d=True), - beta=st.floats( - min_value=-5, - max_value=5, - allow_nan=False, - allow_subnormal=False, - allow_infinity=False, - ), - alpha=st.floats( - min_value=-5, - max_value=5, - allow_nan=False, - allow_subnormal=False, - allow_infinity=False, - ), -) -def test_torch_baddbmm_( - dtype_and_matrices, - beta, - alpha, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, -): - input_dtype, x, batch1, batch2 = dtype_and_matrices - 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={ - "batch1": batch1, - "batch2": batch2, - "beta": beta, - "alpha": alpha, - }, - frontend=frontend, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - on_device=on_device, - ) - - -# char -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="char", - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - min_value=-128, - max_value=127, - ), -) -def test_torch_char( - dtype_x, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# index_fill -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="index_fill", - dtype_indices_axis=helpers.array_indices_axis( - array_dtypes=helpers.get_dtypes("numeric"), - indices_dtypes=["int64"], - min_num_dims=1, - max_num_dims=5, - min_dim_size=1, - max_dim_size=10, - first_dimension_only=True, - indices_same_dims=False, - ), - value=st.floats(min_value=-100, max_value=100), -) -def test_torch_index_fill( - dtype_indices_axis, - value, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtypes, x, indices, axis, _ = dtype_indices_axis - if indices.ndim != 1: - indices = ivy.flatten(indices) - helpers.test_frontend_method( - init_input_dtypes=[input_dtypes[0]], - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x}, - method_input_dtypes=[input_dtypes[1]], - method_all_as_kwargs_np={ - "dim": axis, - "index": indices, - "value": value, - }, - frontend=frontend, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="sinc", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_instance_sinc( - *, - dtype_and_x, - frontend, - backend_fw, - frontend_method_data, - init_flags, - method_flags, - on_device, -): - input_dtype, x = dtype_and_x - 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={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - backend_to_test=backend_fw, - on_device=on_device, - ) - - -# isnan -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="isnan", - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - ), -) -def test_torch_isnan( - dtype_x, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# rsqrt_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="rsqrt_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_rsqrt_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@given( - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid", prune_function=False), - ), - requires_grad=st.booleans(), -) -def test_torch_tensor__requires_grad( - dtype_x, - requires_grad, - backend_fw, -): - ivy.set_backend(backend_fw) - _, data = dtype_x - x = Tensor(data[0]) - assert not x._requires_grad - x.requires_grad_() - assert x._requires_grad - x.requires_grad_(requires_grad) - assert x._requires_grad == requires_grad - ivy.previous_backend() - - -# abs -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="abs", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_abs( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# abs_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="abs_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_abs_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# acos -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="acos", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_acos( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# acos_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="acos_", - dtype_and_x=helpers.dtype_and_values( - min_value=-1.0, - max_value=1.0, - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_acos_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# acosh -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="acosh", - dtype_and_x=helpers.dtype_and_values( - min_value=1.0, - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_acosh( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# acosh_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="acosh_", - dtype_and_x=helpers.dtype_and_values( - min_value=1.0, - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_acosh_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# add -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="add", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), - alpha=st.floats(min_value=-1e04, max_value=1e04, allow_infinity=False), -) -def test_torch_tensor_add( - dtype_and_x, - alpha, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - "alpha": alpha, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - atol_=1e-02, - on_device=on_device, - ) - - -# add_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="add_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - num_arrays=2, - ), -) -def test_torch_tensor_add_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=[input_dtype[0]], - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# addbmm -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="addbmm", - dtype_and_matrices=_get_dtype_and_3dbatch_matrices(with_input=True), - beta=st.floats( - min_value=-5, - max_value=5, - allow_nan=False, - allow_subnormal=False, - allow_infinity=False, - ), - alpha=st.floats( - min_value=-5, - max_value=5, - allow_nan=False, - allow_subnormal=False, - allow_infinity=False, - ), -) -def test_torch_tensor_addbmm( - dtype_and_matrices, - beta, - alpha, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x, batch1, batch2 = dtype_and_matrices - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x, - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "batch1": batch1, - "batch2": batch2, - "beta": beta, - "alpha": alpha, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - atol_=1e-02, - on_device=on_device, - ) - - -# addbmm_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="addbmm_", - dtype_and_matrices=_get_dtype_and_3dbatch_matrices(with_input=True), - beta=st.floats( - min_value=-5, - max_value=5, - allow_nan=False, - allow_subnormal=False, - allow_infinity=False, - ), - alpha=st.floats( - min_value=-5, - max_value=5, - allow_nan=False, - allow_subnormal=False, - allow_infinity=False, - ), -) -def test_torch_tensor_addbmm_( - dtype_and_matrices, - beta, - alpha, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x, batch1, batch2 = dtype_and_matrices - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x, - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "batch1": batch1, - "batch2": batch2, - "beta": beta, - "alpha": alpha, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - atol_=1e-02, - on_device=on_device, - ) - - -# addcdiv -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="addcdiv", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=3, - large_abs_safety_factor=2.5, - small_abs_safety_factor=2.5, - safety_factor_scale="log", - shared_dtype=True, - ), - value=st.floats(min_value=-100, max_value=100), -) -def test_torch_tensor_addcdiv( - dtype_and_x, - value, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - assume(not np.any(np.isclose(x[2], 0))) - - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "tensor1": x[1], - "tensor2": x[2], - "value": value, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - atol_=1e-03, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="addcdiv_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=3, - large_abs_safety_factor=2.5, - small_abs_safety_factor=2.5, - safety_factor_scale="log", - shared_dtype=True, - ), - value=st.floats(min_value=-100, max_value=100), -) -def test_torch_tensor_addcdiv_( - dtype_and_x, - value, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - assume(not np.any(np.isclose(x[2], 0))) - - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "tensor1": x[1], - "tensor2": x[2], - "value": value, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - atol_=1e-03, - ) - - -# addcmul -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="addcmul", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=3, - large_abs_safety_factor=2.5, - small_abs_safety_factor=2.5, - safety_factor_scale="log", - shared_dtype=True, - ), - value=st.floats(min_value=-100, max_value=100), -) -def test_torch_tensor_addcmul( - dtype_and_x, - value, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "tensor1": x[1], - "tensor2": x[2], - "value": value, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - atol_=1e-02, - ) - - -# addcmul_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="addcmul_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=3, - large_abs_safety_factor=2.5, - small_abs_safety_factor=2.5, - safety_factor_scale="log", - shared_dtype=True, - ), - value=st.floats(min_value=-100, max_value=100), -) -def test_torch_tensor_addcmul_( - dtype_and_x, - value, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "tensor1": x[1], - "tensor2": x[2], - "value": value, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - atol_=1e-02, - ) - - -# addmm -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="addmm", - dtype_and_matrices=_get_dtype_input_and_matrices(with_input=True), - beta=st.floats( - min_value=-5, - max_value=5, - allow_nan=False, - allow_subnormal=False, - allow_infinity=False, - ), - alpha=st.floats( - min_value=-5, - max_value=5, - allow_nan=False, - allow_subnormal=False, - allow_infinity=False, - ), -) -def test_torch_tensor_addmm( - dtype_and_matrices, - beta, - alpha, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x, mat1, mat2 = dtype_and_matrices - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x, - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "mat1": mat1, - "mat2": mat2, - "beta": beta, - "alpha": alpha, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - atol_=1e-02, - on_device=on_device, - ) - - -# addmv_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="addmv_", - dtype_and_matrices=_get_dtype_input_and_mat_vec(with_input=True), - beta=st.floats( - min_value=-5, - max_value=5, - allow_nan=False, - allow_subnormal=False, - allow_infinity=False, - ), - alpha=st.floats( - min_value=-5, - max_value=5, - allow_nan=False, - allow_subnormal=False, - allow_infinity=False, - ), -) -def test_torch_tensor_addmv_( - dtype_and_matrices, - beta, - alpha, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x, mat, vec = dtype_and_matrices - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - init_all_as_kwargs_np={ - "data": x, - }, - method_input_dtypes=input_dtype, - backend_to_test=backend_fw, - method_all_as_kwargs_np={ - "mat": mat, - "vec": vec, - "beta": beta, - "alpha": alpha, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - atol_=1e-02, - on_device=on_device, - ) - - -# addr -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="addr", - dtype_and_vecs=_get_dtype_input_and_vectors(with_input=True), - beta=st.floats( - min_value=-5, - max_value=5, - allow_nan=False, - allow_subnormal=False, - allow_infinity=False, - ), - alpha=st.floats( - min_value=-5, - max_value=5, - allow_nan=False, - allow_subnormal=False, - allow_infinity=False, - ), -) -def test_torch_tensor_addr( - dtype_and_vecs, - beta, - alpha, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - dtype, input, vec1, vec2 = dtype_and_vecs - helpers.test_frontend_method( - init_input_dtypes=dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": input, - }, - method_input_dtypes=dtype, - method_all_as_kwargs_np={ - "vec1": vec1, - "vec2": vec2, - "beta": beta, - "alpha": alpha, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - atol_=1e-02, - on_device=on_device, - ) - - -# addr_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="addr_", - dtype_and_vecs=_get_dtype_input_and_vectors(with_input=True), - beta=st.floats( - min_value=-5, - max_value=5, - allow_nan=False, - allow_subnormal=False, - allow_infinity=False, - ), - alpha=st.floats( - min_value=-5, - max_value=5, - allow_nan=False, - allow_subnormal=False, - allow_infinity=False, - ), -) -def test_torch_tensor_addr_( - dtype_and_vecs, - beta, - alpha, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - dtype, input, vec1, vec2 = dtype_and_vecs - helpers.test_frontend_method( - init_input_dtypes=dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": input, - }, - method_input_dtypes=dtype, - method_all_as_kwargs_np={ - "vec1": vec1, - "vec2": vec2, - "beta": beta, - "alpha": alpha, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - atol_=1e-02, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="adjoint", - dtype_and_values=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("real_and_complex"), - min_num_dims=2, - min_dim_size=2, - ), -) -def test_torch_tensor_adjoint( - dtype_and_values, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, values = dtype_and_values - - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": values[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - init_flags=init_flags, - method_flags=method_flags, - frontend_method_data=frontend_method_data, - frontend=frontend, - on_device=on_device, - ) - - -# all -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="all", - dtype_input_axis=helpers.dtype_values_axis( - available_dtypes=helpers.get_dtypes("numeric"), - min_num_dims=1, - min_value=-1e04, - max_value=1e04, - valid_axis=True, - force_int_axis=True, - ), - keepdim=st.booleans(), -) -def test_torch_tensor_all( - dtype_input_axis, - keepdim, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x, axis = dtype_input_axis - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "dim": axis, - "keepdim": keepdim, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# amax -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="amax", - dtype_x_axis=helpers.dtype_values_axis( - available_dtypes=helpers.get_dtypes("numeric"), - valid_axis=True, - force_int_axis=True, - ), - keepdim=st.booleans(), -) -def test_torch_tensor_amax( - dtype_x_axis, - keepdim, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x, axis = dtype_x_axis - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "dim": axis, - "keepdim": keepdim, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# amin -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="amin", - dtype_x_axis=helpers.dtype_values_axis( - available_dtypes=helpers.get_dtypes("numeric"), - valid_axis=True, - force_int_axis=True, - ), - keepdim=st.booleans(), -) -def test_torch_tensor_amin( - dtype_x_axis, - keepdim, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x, axis = dtype_x_axis - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "dim": axis, - "keepdim": keepdim, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# aminmax -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="aminmax", - dtype_input_axis=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - ), -) -def test_torch_tensor_aminmax( - dtype_input_axis, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_input_axis - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# angle -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="angle", - dtype_and_values=helpers.dtype_and_values( - available_dtypes=["float64", "complex64", "complex128"], - ), -) -def test_torch_tensor_angle( - dtype_and_values, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, -): - input_dtype, values = dtype_and_values - - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - init_all_as_kwargs_np={ - "data": values[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - init_flags=init_flags, - method_flags=method_flags, - frontend_method_data=frontend_method_data, - frontend=frontend, - on_device=on_device, - ) - - -# any -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="any", - dtype_input_axis=helpers.dtype_values_axis( - available_dtypes=helpers.get_dtypes("numeric"), - min_num_dims=1, - min_value=-1e04, - max_value=1e04, - valid_axis=True, - force_int_axis=True, - ), - keepdim=st.booleans(), -) -def test_torch_tensor_any( - dtype_input_axis, - keepdim, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x, axis = dtype_input_axis - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "dim": axis, - "keepdim": keepdim, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# write test for torch instance apply_ - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="apply_", - dtype_and_values=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=1, - ), -) -def test_torch_tensor_apply_( - dtype_and_values, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - def func(x): - return x + 1 - - input_dtype, values = dtype_and_values - - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": values[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "callable": func, - }, - init_flags=init_flags, - method_flags=method_flags, - frontend_method_data=frontend_method_data, - frontend=frontend, - on_device=on_device, - ) - - -# arccos -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="arccos", - dtype_and_x=helpers.dtype_and_values( - min_value=-1.0, - max_value=1.0, - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_arccos( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# arccos_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="arccos_", - dtype_and_x=helpers.dtype_and_values( - min_value=-1.0, - max_value=1.0, - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_arccos_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# arccosh -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="arccosh", - dtype_and_x=helpers.dtype_and_values( - min_value=-1.0, - max_value=1.0, - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_arccosh( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# arccosh_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="arccosh_", - dtype_and_x=helpers.dtype_and_values( - min_value=-1.0, - max_value=1.0, - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_arccosh_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# arcsin -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="arcsin", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_arcsin( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# arcsin_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="arcsin_", - dtype_and_x=helpers.dtype_and_values( - min_value=-1.0, - max_value=1.0, - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_arcsin_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# arcsinh -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="arcsinh", - dtype_and_x=helpers.dtype_and_values( - min_value=-1.0, - max_value=1.0, - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_arcsinh( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# arcsinh_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="arcsinh_", - dtype_and_x=helpers.dtype_and_values( - min_value=-1.0, - max_value=1.0, - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_arcsinh_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# arctan -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="arctan", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_arctan( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# arctan2 -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="arctan2", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - ), -) -def test_torch_tensor_arctan2( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# arctan2_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="arctan2_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - ), -) -def test_torch_tensor_arctan2_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# arctan_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="arctan_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_arctan_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# arctanh -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="arctanh", - dtype_and_x=helpers.dtype_and_values( - min_value=-1.0, - max_value=1.0, - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_arctanh( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# arctanh_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="arctanh_", - dtype_and_x=helpers.dtype_and_values( - min_value=-1.0, - max_value=1.0, - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_arctanh_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# argmax -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="argmax", - dtype_input_axis=helpers.dtype_values_axis( - available_dtypes=helpers.get_dtypes("numeric"), - force_int_axis=True, - min_num_dims=1, - max_num_dims=3, - min_dim_size=1, - max_dim_size=3, - min_value=1, - max_value=5, - valid_axis=True, - allow_neg_axes=True, - ), - keepdim=st.booleans(), -) -def test_torch_tensor_argmax( - dtype_input_axis, - keepdim, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x, axis = dtype_input_axis - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "dim": axis, - "keepdim": keepdim, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# argmin -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="argmin", - dtype_input_axis=helpers.dtype_values_axis( - available_dtypes=helpers.get_dtypes("numeric"), - force_int_axis=True, - min_num_dims=1, - max_num_dims=3, - min_dim_size=1, - max_dim_size=3, - min_value=1, - max_value=5, - valid_axis=True, - allow_neg_axes=True, - ), - keepdim=st.booleans(), -) -def test_torch_tensor_argmin( - dtype_input_axis, - keepdim, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x, axis = dtype_input_axis - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "dim": axis, - "keepdim": keepdim, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# argsort -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="argsort", - dtype_input_axis=helpers.dtype_values_axis( - available_dtypes=helpers.get_dtypes("numeric"), - force_int_axis=True, - min_num_dims=1, - max_num_dims=3, - min_dim_size=1, - max_dim_size=3, - min_value=1, - max_value=5, - valid_axis=True, - allow_neg_axes=True, - ), - descending=st.booleans(), -) -def test_torch_tensor_argsort( - dtype_input_axis, - descending, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x, axis = dtype_input_axis - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "dim": axis, - "descending": descending, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# argwhere -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="argwhere", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - ), -) -def test_torch_tensor_argwhere( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="as_strided", - dtype_x_and_other=_as_strided_helper(), -) -def test_torch_tensor_as_strided( - dtype_x_and_other, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x, size, stride, offset = dtype_x_and_other - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "size": size, - "stride": stride, - "storage_offset": offset, - }, - frontend=frontend, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - on_device=on_device, - ) - - -# asin -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="asin", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_asin( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# asin_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="asin_", - dtype_and_x=helpers.dtype_and_values( - min_value=-1.0, - max_value=1.0, - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_asin_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# asinh -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="asinh", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_asinh( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - rtol_=1e-2, - atol_=1e-2, - on_device=on_device, - ) - - -# asinh_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="asinh_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_asinh_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - rtol_=1e-2, - atol_=1e-2, - on_device=on_device, - ) - - -# atan -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="atan", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_atan( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# atan2 -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="atan2", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - ), -) -def test_torch_tensor_atan2( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# atan2_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="atan2_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - ), -) -def test_torch_tensor_atan2_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# atan_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="atan_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_atan_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# atanh -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="atanh", - dtype_and_x=helpers.dtype_and_values( - min_value=-1.0, - max_value=1.0, - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_atanh( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# atanh_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="atanh_", - dtype_and_x=helpers.dtype_and_values( - min_value=-1.0, - max_value=1.0, - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_atanh_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@given( - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float", prune_function=False), - num_arrays=3, - min_value=-1e3, - max_value=1e3, - ).filter(lambda x: all(dt == "float32" for dt in x[0])), -) -def test_torch_tensor_backward( - dtype_x, - backend_fw, -): - ivy.set_backend(backend_fw) - if ivy.current_backend_str() == "numpy": - ivy.warnings.warn("Gradient calculation unavailable for numpy backend") - return - if ivy.current_backend_str() == "paddle": - ivy.warnings.warn("torch.Tensor.backward() unavailable for paddle backend") - return - _, values = dtype_x - x = Tensor(values[0], requires_grad=True) - y = Tensor(values[1], requires_grad=True) - z = Tensor(values[2], requires_grad=True) - a = x + y.pow(2) - b = z * a - c = b.sum() - c.backward() - x_torch = torch.tensor(values[0], requires_grad=True, dtype=torch.float32) - y_torch = torch.tensor(values[1], requires_grad=True, dtype=torch.float32) - z_torch = torch.tensor(values[2], requires_grad=True, dtype=torch.float32) - a_torch = x_torch + y_torch.pow(2) - b_torch = z_torch * a_torch - c_torch = b_torch.sum() - c_torch.backward() - helpers.assertions.value_test( - ret_np_flat=helpers.flatten_and_to_np( - ret=x._grads.ivy_array, backend=backend_fw - ), - ret_np_from_gt_flat=helpers.flatten_and_to_np( - ret=ivy.to_ivy(x_torch.grad.numpy()), backend=backend_fw - ), - rtol=1e-3, - atol=1e-3, - backend="torch", - ) - helpers.assertions.value_test( - ret_np_flat=helpers.flatten_and_to_np( - ret=y._grads.ivy_array, backend=backend_fw - ), - ret_np_from_gt_flat=helpers.flatten_and_to_np( - ret=ivy.to_ivy(y_torch.grad.numpy()), backend=backend_fw - ), - rtol=1e-3, - atol=1e-3, - backend="torch", - ) - helpers.assertions.value_test( - ret_np_flat=helpers.flatten_and_to_np( - ret=z._grads.ivy_array, backend=backend_fw - ), - ret_np_from_gt_flat=helpers.flatten_and_to_np( - ret=ivy.to_ivy(z_torch.grad.numpy()), backend=backend_fw - ), - rtol=1e-3, - atol=1e-3, - backend="torch", - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="baddbmm", - dtype_and_matrices=_get_dtype_and_3dbatch_matrices(with_input=True, input_3d=True), - beta=st.floats( - min_value=-5, - max_value=5, - allow_nan=False, - allow_subnormal=False, - allow_infinity=False, - ), - alpha=st.floats( - min_value=-5, - max_value=5, - allow_nan=False, - allow_subnormal=False, - allow_infinity=False, - ), -) -def test_torch_tensor_baddbmm( - dtype_and_matrices, - beta, - alpha, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x, batch1, batch2 = dtype_and_matrices - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "batch1": batch1, - "batch2": batch2, - "beta": beta, - "alpha": alpha, - }, - frontend=frontend, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - on_device=on_device, - ) - - -# bernoulli -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="bernoulli", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - ), - test_with_out=st.just(True), -) -def test_torch_tensor_bernoulli( - dtype_and_x, - frontend, - frontend_method_data, - init_flags, - method_flags, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "input": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={"generator": x[1], "out": x[2]}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - ) - - -# bitwise_and -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="bitwise_and", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("integer"), - num_arrays=2, - ), -) -def test_torch_tensor_bitwise_and( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# bitwise_and_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="bitwise_and_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("integer"), - num_arrays=2, - ), -) -def test_torch_tensor_bitwise_and_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# bitwise_left_shift -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="bitwise_left_shift", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("integer"), - num_arrays=2, - ), -) -def test_torch_tensor_bitwise_left_shift( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# bitwise_not -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="bitwise_not", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("integer"), - num_arrays=2, - ), -) -def test_torch_tensor_bitwise_not( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - method_all_as_kwargs_np={}, - frontend=frontend, - on_device=on_device, - ) - - -# bitwise_not_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="bitwise_not_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("integer"), - num_arrays=2, - ), -) -def test_torch_tensor_bitwise_not_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - method_all_as_kwargs_np={}, - frontend=frontend, - on_device=on_device, - ) - - -# bitwise_or -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="bitwise_or", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - num_arrays=2, - ), -) -def test_torch_tensor_bitwise_or( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# bitwise_or_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="bitwise_or_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - num_arrays=2, - ), -) -def test_torch_tensor_bitwise_or_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# bitwise right shift -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="bitwise_right_shift", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - num_arrays=2, - shared_dtype=True, - ), -) -def test_torch_tensor_bitwise_right_shift( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - # negative shifts will throw an exception - # shifts >= dtype witdth produce backend-defined behavior - x[1] = np.asarray( - np.clip(x[1], 0, np.iinfo(input_dtype[1]).bits - 1), dtype=input_dtype[1] - ) - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# bitwise_xor -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="bitwise_xor", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=st.one_of(st.just(("bool",)), helpers.get_dtypes("integer")), - num_arrays=2, - ), -) -def test_torch_tensor_bitwise_xor( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# bitwise_xor_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="bitwise_xor_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=st.one_of(st.just(("bool",)), helpers.get_dtypes("integer")), - num_arrays=2, - ), -) -def test_torch_tensor_bitwise_xor_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# bool -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="bool", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("integer"), - ), -) -def test_torch_tensor_bool( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# byte -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="byte", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - ), -) -def test_torch_tensor_byte( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# ceil -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="ceil", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_ceil( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# ceil_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="ceil_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_ceil_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="cholesky", - dtype_and_x=_get_dtype_and_matrix(square=True), - upper=st.booleans(), -) -def test_torch_tensor_cholesky( - dtype_and_x, - upper, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - x = x[0] - # make symmetric positive-definite - x = np.matmul(x.swapaxes(-1, -2), x) + np.identity(x.shape[-1]) * 1e-3 - - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x, - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "upper": upper, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - rtol_=1e-2, - ) - - -# chunk -@pytest.mark.skip("Testing takes a lot of time") -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="chunk", - dtype_x_dim=helpers.dtype_values_axis( - available_dtypes=helpers.get_dtypes("float"), - min_num_dims=1, - min_value=-1e04, - max_value=1e04, - force_int_axis=True, - valid_axis=True, - ), - chunks=st.integers( - min_value=1, - max_value=5, - ), -) -def test_torch_tensor_chunk( - dtype_x_dim, - chunks, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x, dim = dtype_x_dim - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "chunks": chunks, - "dim": dim, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# clamp -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="clamp", - dtype_and_x_min_max=_get_clamp_inputs(), -) -def test_torch_tensor_clamp( - dtype_and_x_min_max, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x, min, max = dtype_and_x_min_max - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - 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, - ) - - -# clamp_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="clamp_", - dtype_and_x_min_max=_get_clamp_inputs(), -) -def test_torch_tensor_clamp_( - dtype_and_x_min_max, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x, min, max = dtype_and_x_min_max - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - 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, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="clamp_min", - input_and_ranges=_get_clip_min_inputs(), -) -def test_torch_tensor_clamp_min( - input_and_ranges, - frontend_method_data, - init_flags, - backend_fw, - frontend, - on_device, - method_flags, -): - x_dtype, x, min = input_and_ranges - helpers.test_frontend_method( - init_input_dtypes=x_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=x_dtype, - method_all_as_kwargs_np={ - "min": min, - }, - 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_tensor_clip( - input_and_ranges, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x, min, max = input_and_ranges - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - 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_tensor_clip_( - input_and_ranges, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x, min, max = input_and_ranges - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - 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, - ) - - -# clone -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="clone", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - num_arrays=1, - ), -) -def test_torch_tensor_clone( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="conj", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float_and_complex") - ), -) -def test_torch_tensor_conj( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# contiguous -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="contiguous", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_contiguous( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# copy_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="copy_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - num_arrays=2, - ), -) -def test_torch_tensor_copy_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# copysign -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="copysign", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - min_num_dims=1, - num_arrays=2, - ), -) -def test_torch_tensor_copysign( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# copysign_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="copysign_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - min_num_dims=1, - num_arrays=2, - ), -) -def test_torch_tensor_copysign_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# cos -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="cos", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_cos( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# cos_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="cos_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_cos_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": list(x[0]) if type(x[0]) == int else x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# cosh -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="cosh", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_cosh( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# cosh_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="cosh_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_cosh_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - rtol_=1e-2, - atol_=1e-2, - ) - - -# count_nonzero -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="count_nonzero", - dtype_value=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), - ), - dim=helpers.get_axis( - shape=st.shared(helpers.get_shape(), key="shape"), - allow_neg=True, - force_int=True, - ), -) -def test_torch_tensor_count_nonzero( - dtype_value, - dim, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_value - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={"dim": dim}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# cross -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="cross", - dtype_input_other_dim=dtype_value1_value2_axis( - available_dtypes=helpers.get_dtypes("numeric"), - min_num_dims=1, - max_num_dims=10, - min_dim_size=3, - max_dim_size=3, - min_value=-1e10, - max_value=1e10, - abs_smallest_val=0.01, - large_abs_safety_factor=2, - safety_factor_scale="log", - ), -) -def test_torch_tensor_cross( - dtype_input_other_dim, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - dtype, input, other, dim = dtype_input_other_dim - helpers.test_frontend_method( - init_input_dtypes=dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": input, - }, - method_input_dtypes=dtype, - method_all_as_kwargs_np={ - "other": other, - "dim": dim, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - rtol_=1e-2, - atol_=1e-2, - ) - - -# cummax -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="cummax", - dtype_value=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), - ), - dim=helpers.get_axis( - shape=st.shared(helpers.get_shape(), key="shape"), - allow_neg=False, - force_int=True, - ), -) -def test_torch_tensor_cummax( - dtype_value, - dim, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_value - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={"dim": dim}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# cumprod -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="cumprod", - dtype_value=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), - ), - dim=helpers.get_axis( - shape=st.shared(helpers.get_shape(), key="shape"), - allow_neg=True, - force_int=True, - ), - dtypes=_dtypes(), -) -def test_torch_tensor_cumprod( - dtype_value, - dim, - dtypes, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_value - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=dtypes, - method_all_as_kwargs_np={ - "dim": dim, - "dtype": dtypes[0], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# cumsum -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="cumsum", - dtype_value=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), - ), - dim=helpers.get_axis( - shape=st.shared(helpers.get_shape(), key="shape"), - allow_neg=True, - force_int=True, - ), - dtypes=_dtypes(), -) -def test_torch_tensor_cumsum( - dtype_value, - dim, - dtypes, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_value - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=dtypes, - method_all_as_kwargs_np={ - "dim": dim, - "dtype": dtypes[0], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# cumsum_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="cumsum_", - dtype_value=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), - ), - dim=helpers.get_axis( - shape=st.shared(helpers.get_shape(), key="shape"), - allow_neg=True, - force_int=True, - ), -) -def test_torch_tensor_cumsum_( - dtype_value, - dim, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_value - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "dim": dim, - "dtype": input_dtype[0], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# det -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="det", - dtype_and_x=_get_dtype_and_matrix(square=True, batch=True), -) -def test_torch_tensor_det( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x, - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# detach -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="detach", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - ), -) -def test_torch_tensor_detach( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# detach_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="detach_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - ), -) -def test_torch_tensor_detach_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@given( - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid", prune_function=False) - ).filter(lambda x: "bfloat16" not in x[0]), -) -def test_torch_tensor_device( - dtype_x, - backend_fw, -): - ivy.set_backend(backend_fw) - _, data = dtype_x - x = Tensor(data[0]) - x.ivy_array = data[0] - ivy.utils.assertions.check_equal( - x.device, ivy.dev(ivy.array(data[0])), as_array=False - ) - ivy.previous_backend() - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="diag", - dtype_and_values=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - shape=st.shared(helpers.get_shape(min_num_dims=1, max_num_dims=2), key="shape"), - ), - diagonal=st.integers(min_value=-100, max_value=100), -) -def test_torch_tensor_diag( - dtype_and_values, - diagonal, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, values = dtype_and_values - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": values[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "diagonal": diagonal, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="diagonal", - dtype_and_values=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - shape=st.shared(helpers.get_shape(min_num_dims=2), key="shape"), - ), - dims_and_offset=dims_and_offset( - shape=st.shared(helpers.get_shape(min_num_dims=2), key="shape") - ), -) -def test_torch_tensor_diagonal( - dtype_and_values, - dims_and_offset, - frontend, - frontend_method_data, - backend_fw, - init_flags, - method_flags, - on_device, -): - input_dtype, value = dtype_and_values - dim1, dim2, offset = dims_and_offset - input = value[0] - num_dims = len(np.shape(input)) - assume(dim1 != dim2) - if dim1 < 0: - assume(dim1 + num_dims != dim2) - if dim2 < 0: - assume(dim1 != dim2 + num_dims) - helpers.test_frontend_method( - init_input_dtypes=[input_dtype[0]], - init_all_as_kwargs_np={"data": input}, - method_input_dtypes=[input_dtype[0]], - method_all_as_kwargs_np={ - "offset": offset, - "dim1": dim1, - "dim2": dim2, - }, - frontend=frontend, - frontend_method_data=frontend_method_data, - backend_to_test=backend_fw, - init_flags=init_flags, - method_flags=method_flags, - on_device=on_device, - ) - - -# dim -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="dim", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - ), -) -def test_torch_tensor_dim( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# div -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="div", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - num_arrays=2, - large_abs_safety_factor=2.5, - small_abs_safety_factor=2.5, - safety_factor_scale="log", - ), - rounding_mode=st.sampled_from(["floor", "trunc"]) | st.none(), -) -def test_torch_tensor_div( - dtype_and_x, - rounding_mode, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - assume(not np.any(np.isclose(x[1], 0))) - - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - "rounding_mode": rounding_mode, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# div_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="div_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - num_arrays=2, - large_abs_safety_factor=2.5, - small_abs_safety_factor=2.5, - safety_factor_scale="log", - ), - rounding_mode=st.sampled_from(["floor", "trunc"]) | st.none(), -) -def test_torch_tensor_div_( - dtype_and_x, - rounding_mode, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - assume(not np.any(np.isclose(x[1], 0))) - - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - "rounding_mode": rounding_mode, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# divide -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="divide", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch_tensor_divide( - dtype_and_x, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - atol_=1e-02, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="dot", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - shape=(1,), - ), -) -def test_torch_tensor_dot( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "tensor": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="double", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - ), -) -def test_torch_tensor_double( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - backend_fw, - on_device, -): - input_dtype, x = dtype_and_x - 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={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - backend_to_test=backend_fw, - on_device=on_device, - ) - - -# dsplit -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="dsplit", - dtype_value=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - shape=st.shared(helpers.get_shape(min_num_dims=3), key="value_shape"), - ), - indices_or_sections=_get_splits( - min_num_dims=3, - axis=2, - allow_none=False, - allow_array_indices=False, - is_mod_split=True, - ), -) -def test_torch_tensor_dsplit( - dtype_value, - indices_or_sections, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_value - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={"indices_or_sections": indices_or_sections}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@given( - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid", prune_function=False) - ).filter(lambda x: "bfloat16" not in x[0]), -) -def test_torch_tensor_dtype(dtype_x, backend_fw): - ivy.set_backend(backend_fw) - dtype, data = dtype_x - x = Tensor(data[0]) - x.ivy_array = data[0] - ivy.utils.assertions.check_equal(x.dtype, dtype[0], as_array=False) - ivy.previous_backend() - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="eq_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch_tensor_eq_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# equal -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="equal", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - num_arrays=2, - shared_dtype=True, - min_num_dims=1, - min_value=-1e04, - max_value=1e04, - ), -) -def test_torch_tensor_equal( - dtype_and_x, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - atol_=1e-04, - rtol_=1e-04, - on_device=on_device, - ) - - -# erf -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="erf", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_erf( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# erf_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="erf_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - ), -) -def test_torch_tensor_erf_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# exp -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="exp", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - ), -) -def test_torch_tensor_exp( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# exp_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="exp_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - ), -) -def test_torch_tensor_exp_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="expand", - dtype_x_shape=_expand_helper(), - unpack_shape=st.booleans(), -) -def test_torch_tensor_expand( - dtype_x_shape, - unpack_shape, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x, shape = dtype_x_shape - if unpack_shape: - method_flags.num_positional_args = len(shape) + 1 - size = {} - i = 0 - for x_ in shape: - size["x{}".format(i)] = x_ - i += 1 - else: - size = { - "size": shape, - } - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np=size, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# expand_as -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="expand_as", - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), num_arrays=2 - ), -) -def test_torch_tensor_expand_as( - dtype_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# expm1 -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="expm1", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - ), -) -def test_torch_tensor_expm1( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# expm1_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="expm1_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_expm1_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# fill_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="fill_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), - value=helpers.floats(min_value=1, max_value=10), -) -def test_torch_tensor_fill_( - dtype_and_x, - value, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "value": value, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# fix -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="fix", - dtype_value=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), - ), -) -def test_torch_tensor_fix( - dtype_value, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_value - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# fix_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="fix_", - dtype_value=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), - ), -) -def test_torch_tensor_fix_( - dtype_value, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_value - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# flatten -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="flatten", - dtype_value=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - shape=st.shared(helpers.get_shape(), key="shape"), - ), - axes=helpers.get_axis( - shape=st.shared(helpers.get_shape(), key="shape"), - min_size=2, - max_size=2, - unique=False, - force_tuple=True, - ), -) -def test_torch_tensor_flatten( - dtype_value, - axes, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_value - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "start_dim": axes[0], - "end_dim": axes[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# flip -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="flip", - dtype_values_axis=_array_idxes_n_dtype( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_flip( - dtype_values_axis, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - x, idxes, dtype = dtype_values_axis - helpers.test_frontend_method( - init_input_dtypes=dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=dtype, - method_all_as_kwargs_np={ - "dims": idxes, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# fliplr -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="fliplr", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - min_num_dims=2, - ), -) -def test_torch_tensor_fliplr( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="float", - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - ), -) -def test_torch_tensor_float( - dtype_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# floor -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="floor", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_floor( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="floor_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_floor_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# fmin -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="fmin", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - ), -) -def test_torch_tensor_fmin( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# fmod -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="fmod", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - shared_dtype=True, - min_num_dims=1, - min_value=-100, - max_value=100, - ), -) -def test_torch_tensor_fmod( - dtype_and_x, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={"other": x[1]}, - frontend=frontend, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - on_device=on_device, - ) - - -# fmod_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="fmod_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - shared_dtype=True, - min_num_dims=1, - min_value=-100, - max_value=100, - ), -) -def test_torch_tensor_fmod_( - dtype_and_x, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={"other": x[1]}, - frontend=frontend, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="gather", - params_indices_others=helpers.array_indices_axis( - array_dtypes=helpers.get_dtypes("valid"), - indices_dtypes=["int64"], - indices_same_dims=True, - ), -) -def test_torch_tensor_gather( - params_indices_others, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtypes, x, indices, axis, batch_dims = params_indices_others - helpers.test_frontend_method( - init_input_dtypes=[input_dtypes[0]], - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x}, - method_input_dtypes=[input_dtypes[1]], - method_all_as_kwargs_np={ - "dim": axis, - "index": indices, - }, - frontend=frontend, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - on_device=on_device, - ) - - -# gcd -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="gcd", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("integer"), - min_value=-100, - max_value=100, - min_num_dims=1, - max_num_dims=3, - min_dim_size=1, - max_dim_size=3, - num_arrays=2, - shared_dtype=True, - ), -) -def test_torch_tensor_gcd( - dtype_and_x, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend=frontend, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - on_device=on_device, - ) - - -def test_torch_tensor_grad(backend_fw): - ivy.set_backend(backend_fw) - x = Tensor(ivy.array([1.0, 2.0, 3.0])) - grads = ivy.array([1.0, 2.0, 3.0]) - x._grads = grads - assert ivy.array_equal(x.grad, grads) - ivy.previous_backend() - - -def test_torch_tensor_grad_fn(backend_fw): - ivy.set_backend(backend_fw) - x = Tensor(ivy.array([3.0]), requires_grad=True) - ivy.utils.assertions.check_equal(x.grad_fn, None, as_array=False) - y = x.pow(2) - ivy.utils.assertions.check_equal(y.grad_fn, "PowBackward", as_array=False) - ivy.utils.assertions.check_equal( - y.grad_fn.next_functions[0], "AccumulateGrad", as_array=False - ) - z = y.detach() - ivy.utils.assertions.check_equal(z.grad_fn, None, as_array=False) - ivy.previous_backend() - - -# greater -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="greater", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch_tensor_greater( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# greater_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="greater_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch_tensor_greater_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# greater_equal -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="greater_equal", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch_tensor_greater_equal( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# greater_equal_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="greater_equal_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch_tensor_greater_equal_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# half -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="half", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - ), -) -def test_torch_tensor_half( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="heaviside", - dtype_and_values=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - ), -) -def test_torch_tensor_heaviside( - dtype_and_values, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, values = dtype_and_values - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": values[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "values": values[1], - }, - init_flags=init_flags, - method_flags=method_flags, - frontend_method_data=frontend_method_data, - frontend=frontend, - on_device=on_device, - ) - - -# hsplit -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="hsplit", - dtype_value=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - shape=st.shared(helpers.get_shape(min_num_dims=2), key="value_shape"), - ), - indices_or_sections=_get_splits( - min_num_dims=1, - axis=1, - allow_none=False, - allow_array_indices=False, - is_mod_split=True, - ), -) -def test_torch_tensor_hsplit( - dtype_value, - indices_or_sections, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_value - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={"indices_or_sections": indices_or_sections}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@given( - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("complex", prune_function=False) - ), -) -def test_torch_tensor_imag(dtype_x, backend_fw): - ivy.set_backend(backend_fw) - _, data = dtype_x - x = Tensor(data[0]) - x.ivy_array = data[0] - ivy.utils.assertions.check_equal(x.imag, ivy.imag(data[0])) - ivy.previous_backend() - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="index_add", - xs_dtypes_dim_idx=_arrays_dim_idx_n_dtypes(), - alpha=st.integers(min_value=1, max_value=2), -) -def test_torch_tensor_index_add( - *, - xs_dtypes_dim_idx, - alpha, - frontend_method_data, - init_flags, - method_flags, - on_device, - frontend, - backend_fw, -): - xs, input_dtypes, axis, indices = xs_dtypes_dim_idx - if xs[0].shape[axis] < xs[1].shape[axis]: - source, input = xs - else: - input, source = xs - helpers.test_frontend_method( - init_input_dtypes=[input_dtypes[0]], - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": input, - }, - method_input_dtypes=["int64", input_dtypes[1]], - method_all_as_kwargs_np={ - "dim": axis, - "index": indices, - "source": source, - "alpha": alpha, - }, - frontend=frontend, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - on_device=on_device, - rtol_=1e-03, - ) - - -# index_add -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="index_add_", - xs_dtypes_dim_idx=_arrays_dim_idx_n_dtypes(), - alpha=st.integers(min_value=1, max_value=2), -) -def test_torch_tensor_index_add_( - *, - xs_dtypes_dim_idx, - alpha, - frontend_method_data, - init_flags, - method_flags, - on_device, - frontend, - backend_fw, -): - xs, input_dtypes, axis, indices = xs_dtypes_dim_idx - if xs[0].shape[axis] < xs[1].shape[axis]: - source, input = xs - else: - input, source = xs - helpers.test_frontend_method( - init_input_dtypes=[input_dtypes[0]], - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": input, - }, - method_input_dtypes=["int64", input_dtypes[1]], - method_all_as_kwargs_np={ - "dim": axis, - "index": indices, - "source": source, - "alpha": alpha, - }, - frontend=frontend, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - on_device=on_device, - rtol_=1e-03, - ) - - -# index_select -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="index_select", - params_indices_others=helpers.array_indices_axis( - array_dtypes=helpers.get_dtypes("valid"), - indices_dtypes=["int64"], - max_num_dims=1, - indices_same_dims=True, - ), -) -def test_torch_tensor_index_select( - params_indices_others, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtypes, input, indices, axis, batch_dims = params_indices_others - helpers.test_frontend_method( - init_input_dtypes=[input_dtypes[0]], - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": input, - }, - method_input_dtypes=[input_dtypes[1]], - method_all_as_kwargs_np={ - "dim": axis, - "index": indices, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="bmm", - dtype_and_matrices=_get_dtype_and_3dbatch_matrices(with_input=True, input_3d=True), -) -def test_torch_tensor_instance_bmm( - dtype_and_matrices, - backend_fw, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, -): - input_dtype, _, x, mat2 = dtype_and_matrices - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - init_all_as_kwargs_np={"data": x}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={"mat2": mat2}, - frontend=frontend, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - on_device=on_device, - backend_to_test=backend_fw, - ) - - -# int -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="int", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("integer"), - ), -) -def test_torch_tensor_int( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# inverse -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="inverse", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - min_num_dims=2, - ).filter(lambda s: s[1][0].shape[-1] == s[1][0].shape[-2]), -) -def test_torch_tensor_inverse( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# is_complex -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="is_complex", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - ), -) -def test_torch_tensor_is_complex( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@given( - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid", prune_function=False) - ).filter(lambda x: "bfloat16" not in x[0]), -) -def test_torch_tensor_is_cuda( - dtype_x, - backend_fw, -): - ivy.set_backend(backend_fw) - _, data = dtype_x - x = Tensor(data[0]) - x.ivy_array = data[0] - ivy.utils.assertions.check_equal( - x.is_cuda, "gpu" in ivy.dev(ivy.array(data[0])), as_array=False - ) - ivy.previous_backend() - - -@given( - requires_grad=st.booleans(), -) -def test_torch_tensor_is_leaf(requires_grad, backend_fw): - ivy.set_backend(backend_fw) - x = Tensor(ivy.array([3.0]), requires_grad=requires_grad) - ivy.utils.assertions.check_equal(x.is_leaf, True, as_array=False) - y = x.pow(2) - ivy.utils.assertions.check_equal(y.is_leaf, not requires_grad, as_array=False) - z = y.detach() - ivy.utils.assertions.check_equal(z.is_leaf, True, as_array=False) - ivy.previous_backend() - - -@given( - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid", prune_function=False) - ).filter(lambda x: "bfloat16" not in x[0]), -) -def test_torch_tensor_is_meta( - dtype_x, - backend_fw, -): - ivy.set_backend(backend_fw) - _, data = dtype_x - x = Tensor(data[0]) - x.ivy_array = data[0] - ivy.utils.assertions.check_equal( - x.is_meta, "meta" in ivy.dev(ivy.array(data[0])), as_array=False - ) - ivy.previous_backend() - - -@given( - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid", prune_function=False) - ).filter(lambda x: "bfloat16" not in x[0]), -) -def test_torch_tensor_is_quantized( - dtype_x, - backend_fw, -): - ivy.set_backend(backend_fw) - _, data = dtype_x - x = Tensor(data[0]) - x.ivy_array = data[0] - ivy.utils.assertions.check_equal( - x.is_quantized, "q" in ivy.dtype(ivy.array(data[0])), as_array=False - ) - ivy.previous_backend() - - -# isinf -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="isinf", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - ), -) -def test_torch_tensor_isinf( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# isreal -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="isreal", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - ), -) -def test_torch_tensor_isreal( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@given( - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid", prune_function=False) - ).filter(lambda x: "bfloat16" not in x[0]), -) -def test_torch_tensor_ivy_array( - dtype_x, - backend_fw, -): - _, data = dtype_x - ivy.set_backend(backend_fw) - x = Tensor(data[0]) - x.ivy_array = data[0] - ret = helpers.flatten_and_to_np(ret=x.ivy_array.data, backend=backend_fw) - ret_gt = helpers.flatten_and_to_np(ret=data[0], backend=backend_fw) - helpers.value_test( - ret_np_flat=ret, - ret_np_from_gt_flat=ret_gt, - backend="torch", - ) - - -# lcm -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="lcm", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("integer"), - num_arrays=2, - min_value=-100, - max_value=100, - min_num_dims=1, - max_num_dims=3, - min_dim_size=1, - max_dim_size=3, - shared_dtype=True, - ), -) -def test_torch_tensor_lcm( - dtype_and_x, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend=frontend, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - on_device=on_device, - ) - - -# less -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="less", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch_tensor_less( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# less_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="less_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch_tensor_less_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# less_equal -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="less_equal", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch_tensor_less_equal( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# less_equal_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="less_equal_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch_tensor_less_equal_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# log -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="log", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_log( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# log10 -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="log10", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_log10( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# log10_ tests -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="log10_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_log10_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="log1p", - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - max_value=1e37, - ), -) -def test_torch_tensor_log1p( - dtype_x, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend=frontend, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - on_device=on_device, - ) - - -# log1p_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="log1p_", - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - max_value=1e37, - ), -) -def test_torch_tensor_log1p_( - dtype_x, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend=frontend, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - on_device=on_device, - ) - - -# log2 -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="log2", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_log2( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# addmm_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="log2_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - allow_inf=False, - ), -) -def test_torch_tensor_log2_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x, mat1, mat2 = dtype_and_matrices - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x, - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "mat1": mat1, - "mat2": mat2, - "beta": beta, - "alpha": alpha, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - atol_=1e-02, - on_device=on_device, - ) - - -# log2_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="log2_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - allow_inf=False, - ), -) -def test_torch_tensor_log2_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# log_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="log_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_log_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x, mat, vec = dtype_and_matrices - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x, - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "mat": mat, - "vec": vec, - "beta": beta, - "alpha": alpha, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - atol_=1e-02, - on_device=on_device, - ) - - -# log_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="log_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_log_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# logaddexp -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="logaddexp", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - min_num_dims=1, - min_value=-100, - max_value=100, - shared_dtype=True, - ), -) -def test_torch_tensor_logaddexp( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# logdet -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="logdet", - dtype_and_x=_get_dtype_and_matrix(square=True, batch=True), -) -def test_torch_tensor_logdet( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - dtype, x = dtype_and_x - x = np.matmul(x.T, x) + np.identity(x.shape[0]) - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x, - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# logical_and -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="logical_and", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - num_arrays=2, - ), -) -def test_torch_tensor_logical_and( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# logical_not -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="logical_not", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), num_arrays=1 - ), -) -def test_torch_tensor_logical_not( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# logical_not_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="logical_not_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - num_arrays=1, - large_abs_safety_factor=12, - ), -) -def test_torch_tensor_logical_not_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# logical_or -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="logical_or", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - num_arrays=2, - ), -) -def test_torch_tensor_logical_or( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# long -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="long", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("integer"), - ), -) -def test_torch_tensor_long( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# masked_fill -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="masked_fill", - x_mask_val=_masked_fill_helper(), -) -def test_torch_tensor_masked_fill( - x_mask_val, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - dtype, x, mask, val = x_mask_val - helpers.test_frontend_method( - init_input_dtypes=[dtype], - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x, - }, - method_input_dtypes=["bool", dtype], - method_all_as_kwargs_np={ - "mask": mask, - "value": val, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# matmul -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="matmul", - dtype_tensor1_tensor2=_get_dtype_and_multiplicative_matrices(), -) -def test_torch_tensor_matmul( - dtype_tensor1_tensor2, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - dtype, tensor1, tensor2 = dtype_tensor1_tensor2 - helpers.test_frontend_method( - init_input_dtypes=dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": tensor1, - }, - method_input_dtypes=dtype, - method_all_as_kwargs_np={"other": tensor2}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# max -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="max", - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_max( - dtype_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# mean -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="mean", - dtype_and_x=_statistical_dtype_values( - function="mean", - min_value=-1e04, - max_value=1e04, - ), - keepdims=st.booleans(), -) -def test_torch_tensor_mean( - dtype_and_x, - keepdims, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x, axis = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "dim": axis, - "keepdim": keepdims, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# median -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="median", - dtype_input_axis=helpers.dtype_values_axis( - available_dtypes=helpers.get_dtypes("float"), - min_num_dims=1, - valid_axis=True, - force_int_axis=True, - ), - keepdim=st.booleans(), -) -def test_torch_tensor_median( - dtype_input_axis, - keepdim, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x, axis = dtype_input_axis - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "dim": axis, - "keepdim": keepdim, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# min -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="min", - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_min( - dtype_x, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# mm -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="mm", - dtype_xy=_get_dtype_input_and_matrices(), -) -def test_torch_tensor_mm( - dtype_xy, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - dtype, x, y = dtype_xy - helpers.test_frontend_method( - init_input_dtypes=dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x, - }, - method_input_dtypes=dtype, - method_all_as_kwargs_np={ - "mat2": y, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="movedim", - dtype_and_input=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - min_value=-100, - max_value=100, - shape=st.shared( - helpers.get_shape( - min_num_dims=1, - max_num_dims=3, - min_dim_size=1, - max_dim_size=3, - ), - key="a_s_d", - ), - ), - source=helpers.get_axis( - allow_none=False, - unique=True, - shape=st.shared( - helpers.get_shape( - min_num_dims=1, - max_num_dims=3, - min_dim_size=1, - max_dim_size=3, - ), - key="a_s_d", - ), - min_size=1, - force_int=True, - ), - destination=helpers.get_axis( - allow_none=False, - unique=True, - shape=st.shared( - helpers.get_shape( - min_num_dims=1, - max_num_dims=3, - min_dim_size=1, - max_dim_size=3, - ), - key="a_s_d", - ), - min_size=1, - force_int=True, - ), -) -def test_torch_tensor_movedim( - dtype_and_input, - source, - destination, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, value = dtype_and_input - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": value[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "source": source, - "destination": destination, - }, - frontend=frontend, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - on_device=on_device, - ) - - -# msort -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="msort", - dtype_value=helpers.dtype_and_values( - available_dtypes=["float32", "float64", "int32", "int64"], - shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), - ), -) -def test_torch_tensor_msort( - dtype_value, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_value - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# mul -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="mul", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - num_arrays=2, - ), -) -def test_torch_tensor_mul( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# mul_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="mul_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - num_arrays=2, - shared_dtype=True, - ), -) -def test_torch_tensor_mul_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# multiply -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="multiply", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - num_arrays=2, - ), -) -def test_torch_tensor_multiply( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# multiply_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="multiply_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - num_arrays=2, - ), -) -def test_torch_tensor_multiply_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# nanmean -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="nanmean", - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - min_value=-1e04, - max_value=1e04, - ), -) -def test_torch_tensor_nanmean( - dtype_x, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="narrow", - dtype_input_dim_start_length=_dtype_input_dim_start_length(), -) -def test_torch_tensor_narrow( - dtype_input_dim_start_length, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - (input_dtype, x, dim, start, length) = dtype_input_dim_start_length - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "dim": dim, - "start": start, - "length": length, - }, - frontend=frontend, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - on_device=on_device, - ) - - -@given( - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid", prune_function=False), - ret_shape=True, - ).filter(lambda x: "bfloat16" not in x[0]), -) -def test_torch_tensor_ndim(dtype_x, backend_fw): - ivy.set_backend(backend_fw) - dtype, data, shape = dtype_x - x = Tensor(data[0]) - ivy.utils.assertions.check_equal(x.ndim, data[0].ndim, as_array=False) - ivy.previous_backend() - - -# ndimension -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="ndimension", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - ), -) -def test_torch_tensor_ndimension( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# ne -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="ne", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch_tensor_ne( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# neg -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="neg", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch_tensor_neg( - dtype_and_x, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# neg_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="neg_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch_tensor_neg_( - dtype_and_x, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# new_empty (not actually intuitive for testing) -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="new_empty", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - ), - size=helpers.get_shape( - min_num_dims=1, - max_num_dims=3, - ), -) -def test_torch_tensor_new_empty( - dtype_and_x, - size, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=[input_dtype[0]], - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x, - }, - method_input_dtypes=[ivy.int32], - method_all_as_kwargs_np={ - "size": size, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# new_full -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="new_full", - dtype_and_x=_fill_value_and_size(max_num_dims=3), -) -def test_torch_tensor_new_full( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=[input_dtype[0]], - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[input_dtype[1]], - method_all_as_kwargs_np={ - "size": x[1], - "fill_value": x[2], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# new_ones -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="new_ones", - dtype_and_x=helpers.dtype_and_values(available_dtypes=helpers.get_dtypes("float")), - size=helpers.get_shape( - allow_none=False, - min_num_dims=1, - max_num_dims=5, - min_dim_size=1, - max_dim_size=10, - ), - dtypes=_dtypes(), - requires_grad=_requires_grad(), -) -def test_torch_tensor_new_ones( - dtype_and_x, - size, - dtypes, - requires_grad, - on_device, - frontend_method_data, - init_flags, - method_flags, - frontend, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=dtypes, - method_all_as_kwargs_np={ - "size": size, - "dtype": dtypes[0], - "requires_grad": requires_grad, - "device": on_device, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# new_tensor -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="new_tensor", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - num_arrays=2, - ), -) -def test_torch_tensor_new_tensor( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=[input_dtype[0]], - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[input_dtype[1]], - method_all_as_kwargs_np={ - "data": x[1], - "dtype": input_dtype[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# new_zeros -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="new_zeros", - dtype_and_x=helpers.dtype_and_values(available_dtypes=helpers.get_dtypes("valid")), - size=helpers.get_shape( - allow_none=False, - min_num_dims=1, - max_num_dims=5, - min_dim_size=1, - max_dim_size=10, - ), - dtypes=_dtypes(), - requires_grad=_requires_grad(), -) -def test_torch_tensor_new_zeros( - dtype_and_x, - size, - dtypes, - requires_grad, - on_device, - frontend_method_data, - init_flags, - method_flags, - frontend, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=dtypes, - method_all_as_kwargs_np={ - "size": size, - "dtype": dtypes[0], - "requires_grad": requires_grad, - "device": on_device, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# nonzero -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="nonzero", - dtype_and_values=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - ), -) -def test_torch_tensor_nonzero( - dtype_and_values, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_values - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# norm -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="norm", - p_dtype_x_axis=_get_axis_and_p(), - keepdim=st.booleans(), - dtype=helpers.get_dtypes("valid", full=False), -) -def test_torch_tensor_norm( - p_dtype_x_axis, - keepdim, - dtype, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - p, values = p_dtype_x_axis - input_dtype, x, axis = values - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "p": p, - "dim": axis, - "keepdim": keepdim, - "dtype": dtype[0], - }, - frontend=frontend, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - on_device=on_device, - ) - - -# normal_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="normal_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), - mean=helpers.floats(min_value=-1, max_value=1), - std=helpers.floats(min_value=0, max_value=1), -) -def test_torch_tensor_normal_( - dtype_and_x, - mean, - std, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - dtype, x = dtype_and_x - - def call(): - return helpers.test_frontend_method( - init_input_dtypes=dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=dtype, - method_all_as_kwargs_np={ - "mean": mean, - "std": std, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - test_values=False, - ) - - ret = call() - - if not ivy.exists(ret): - return - - ret_np, ret_from_np = ret - ret_np = helpers.flatten_and_to_np(ret=ret_np) - ret_from_np = helpers.flatten_and_to_np(ret=ret_from_np) - for u, v in zip(ret_np, ret_from_np): - assert u.dtype == v.dtype - assert u.shape == v.shape - - -# not_equal -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="not_equal", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - num_arrays=2, - ), -) -def test_torch_tensor_not_equal( - dtype_and_x, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - atol_=1e-02, - on_device=on_device, - ) - - -# numpy -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="numpy", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - ), -) -def test_torch_tensor_numpy( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - ret, frontend_ret = helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - test_values=False, - ) - # manual testing required as function return is numpy frontend - helpers.value_test( - ret_np_flat=helpers.flatten_and_to_np(ret=ret), - ret_np_from_gt_flat=frontend_ret[0], - ground_truth_backend="torch", - ) - - -# permute -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="permute", - dtype_values_axis=_array_idxes_n_dtype( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_permute( - dtype_values_axis, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - x, idxes, dtype = dtype_values_axis - unpack_dims = True - if unpack_dims: - method_flags.num_positional_args = len(idxes) + 1 - dims = {} - i = 0 - for x_ in idxes: - dims["x{}".format(i)] = x_ - i += 1 - else: - dims = { - "dims": tuple(idxes), - } - helpers.test_frontend_method( - init_input_dtypes=dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=dtype, - method_all_as_kwargs_np=dims, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# pow -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="pow", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch_tensor_pow( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - dtype = input_dtype[0] - if "int" in dtype: - x[1] = ivy.abs(x[1]) - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "exponent": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# pow_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="pow_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - num_arrays=2, - ), -) -def test_torch_tensor_pow_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - dtype = input_dtype[0] - if "int" in dtype: - x[1] = ivy.abs(x[1]) - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "exponent": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# prod -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="prod", - dtype_x_axis=helpers.dtype_values_axis( - available_dtypes=helpers.get_dtypes("numeric"), - min_num_dims=1, - max_num_dims=5, - valid_axis=True, - allow_neg_axes=False, - max_axes_size=1, - force_int_axis=True, - large_abs_safety_factor=10, - small_abs_safety_factor=10, - safety_factor_scale="log", - ), - dtype=helpers.get_dtypes("float", none=True, full=False), - keepdims=st.booleans(), -) -def test_torch_tensor_prod( - dtype_x_axis, - dtype, - keepdims, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x, axis = dtype_x_axis - if ivy.current_backend_str() == "torch": - init_flags.as_variable = [False] - method_flags.as_variable = [False] - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "dim": axis, - "keepdim": keepdims, - "dtype": dtype[0], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="quantile", - dtype_and_x=_quantile_helper().filter(lambda x: "bfloat16" not in x[0]), - keepdims=st.booleans(), -) -def test_torch_tensor_quantile( - dtype_and_x, - keepdims, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x, axis, interpolation, q = dtype_and_x - if type(axis) is tuple: - axis = axis[0] - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "q": q, - "dim": axis, - "keepdim": keepdims, - "interpolation": interpolation[0], - }, - frontend=frontend, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - on_device=on_device, - ) - - -# ravel -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="ravel", - dtype_value=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), - ), -) -def test_torch_tensor_ravel( - dtype_value, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_value - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@given( - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("complex", prune_function=False) - ).filter(lambda x: "bfloat16" not in x[0]), -) -def test_torch_tensor_real(dtype_x, backend_fw): - ivy.set_backend(backend_fw) - _, data = dtype_x - x = Tensor(data[0]) - x.ivy_array = data[0] - ivy.utils.assertions.check_equal(x.real, ivy.real(data[0])) - ivy.previous_backend() - - -# reciprocal -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="reciprocal", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - min_value=1, - ), -) -def test_torch_tensor_reciprocal( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# reciprocal_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="reciprocal_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - min_value=1, - ), -) -def test_torch_tensor_reciprocal_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# relu -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="relu", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_relu( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# remainder -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="remainder", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - large_abs_safety_factor=2.5, - small_abs_safety_factor=2.5, - shared_dtype=True, - num_arrays=2, - ), -) -def test_torch_tensor_remainder( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# remainder_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="remainder_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - min_value=-1e04, - max_value=1e04, - large_abs_safety_factor=2.5, - small_abs_safety_factor=2.5, - shared_dtype=True, - num_arrays=2, - ), -) -def test_torch_tensor_remainder_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# repeat -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="repeat", - dtype_x_repeats=_repeat_helper(), - unpack_repeat=st.booleans(), -) -def test_torch_tensor_repeat( - dtype_x_repeats, - unpack_repeat, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x, repeats = dtype_x_repeats - repeat = { - "repeats": repeats, - } - if unpack_repeat: - method_flags.num_positional_args = len(repeat["repeats"]) + 1 - for i, x_ in enumerate(repeat["repeats"]): - repeat["x{}".format(i)] = x_ - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np=repeat, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@given( - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid", prune_function=False), - ), - requires_grad=st.booleans(), -) -def test_torch_tensor_requires_grad(dtype_x, requires_grad, backend_fw): - ivy.set_backend(backend_fw) - _, data = dtype_x - x = Tensor(data[0], requires_grad=requires_grad) - ivy.utils.assertions.check_equal(x.requires_grad, requires_grad, as_array=False) - x.requires_grad = not requires_grad - ivy.utils.assertions.check_equal(x.requires_grad, not requires_grad, as_array=False) - ivy.previous_backend() - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="reshape", - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - shape=st.shared(helpers.get_shape(), key="value_shape"), - ), - shape=helpers.reshape_shapes( - shape=st.shared(helpers.get_shape(), key="value_shape") - ), - unpack_shape=st.booleans(), -) -def test_torch_tensor_reshape( - dtype_x, - shape, - unpack_shape, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_x - shape = { - "shape": shape, - } - if unpack_shape: - method_flags.num_positional_args = len(shape["shape"]) + 1 - i = 0 - for x_ in shape["shape"]: - shape["x{}".format(i)] = x_ - i += 1 - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np=shape, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# reshape_as -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="reshape_as", - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), num_arrays=2 - ), -) -def test_torch_tensor_reshape_as( - dtype_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# round -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="round", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), - decimals=st.integers(min_value=0, max_value=5), -) -def test_torch_tensor_round( - dtype_and_x, - decimals, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "decimals": decimals, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# round_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="round_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), - decimals=st.integers(min_value=0, max_value=5), -) -def test_torch_tensor_round_( - dtype_and_x, - decimals, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "decimals": decimals, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# rsqrt -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="rsqrt", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_rsqrt( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@given( - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid", prune_function=False), - ret_shape=True, - ).filter(lambda x: "bfloat16" not in x[0]), -) -def test_torch_tensor_shape(dtype_x, backend_fw): - ivy.set_backend(backend_fw) - dtype, data, shape = dtype_x - x = Tensor(data[0]) - ivy.utils.assertions.check_equal( - x.ivy_array.shape, ivy.Shape(shape), as_array=False - ) - ivy.previous_backend() - - -# short -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="short", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), -) -def test_torch_tensor_short( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# sigmoid -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="sigmoid", - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_sigmoid( - dtype_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# sigmoid -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="sigmoid_", - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_sigmoid_( - dtype_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# sign -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="sign", - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - ), -) -def test_torch_tensor_sign( - dtype_x, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# sign_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="sign_", - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - ), -) -def test_torch_tensor_sign_( - dtype_x, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=[input_dtype], - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - on_device=on_device, - frontend=frontend, - ) - - -# sin -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="sin", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_sin( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# sin_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="sin_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_sin_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# sinh -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="sinh", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_sinh( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# sinh_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="sinh_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_sinh_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# size -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="size", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), - ), - dim=helpers.get_axis( - shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), - force_int=True, - ), -) -def test_torch_tensor_size( - dtype_and_x, - dim, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "dim": dim, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# softmax -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="softmax", - dtype_x_and_axis=helpers.dtype_values_axis( - available_dtypes=helpers.get_dtypes("float"), - min_num_dims=1, - max_axes_size=1, - force_int_axis=True, - valid_axis=True, - ), - dtype=helpers.get_dtypes("float", full=False), -) -def test_torch_tensor_softmax( - dtype_x_and_axis, - dtype, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x, axis = dtype_x_and_axis - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "dim": axis, - "dtype": dtype[0], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# sort -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="sort", - dtype_value=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), - ), - dim=helpers.get_axis( - shape=st.shared(helpers.get_shape(), key="shape"), - allow_neg=True, - force_int=True, - ), - descending=st.booleans(), -) -def test_torch_tensor_sort( - dtype_value, - dim, - descending, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_value - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "dim": dim, - "descending": descending, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# split -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="split", - dtype_value=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - shape=st.shared(helpers.get_shape(min_num_dims=1), key="value_shape"), - ), - split_size=_get_splits(allow_none=False, min_num_dims=1, allow_array_indices=False), - dim=st.shared( - helpers.get_axis( - shape=st.shared(helpers.get_shape(min_num_dims=1), key="value_shape"), - force_int=True, - ), - key="target_axis", - ), -) -def test_torch_tensor_split( - dtype_value, - split_size, - dim, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_value - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "split_size": split_size, - "dim": dim, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# sqrt -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="sqrt", - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - ), -) -def test_torch_tensor_sqrt( - dtype_x, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# sqrt_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="sqrt_", - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_sqrt_( - dtype_x, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# square -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="square", - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_tensor_square( - dtype_x, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# squeeze -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="squeeze", - dtype_value_axis=helpers.dtype_values_axis( - available_dtypes=helpers.get_dtypes("valid"), - min_num_dims=1, - valid_axis=True, - force_int_axis=True, - shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), - ), -) -def test_torch_tensor_squeeze( - dtype_value_axis, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x, axis = dtype_value_axis - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "dim": axis, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# squeeze_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="squeeze_", - dtype_value_axis=helpers.dtype_values_axis( - available_dtypes=helpers.get_dtypes("valid"), - min_num_dims=1, - valid_axis=True, - force_int_axis=True, - shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), - ), -) -def test_torch_tensor_squeeze_( - dtype_value_axis, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x, axis = dtype_value_axis - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "dim": axis, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# std -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="std", - dtype_and_x=_statistical_dtype_values(function="std"), -) -def test_torch_tensor_std( - dtype_and_x, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x, _, _ = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="stride", - dtype_value_axis=helpers.dtype_values_axis( - available_dtypes=helpers.get_dtypes("valid"), - min_num_dims=1, - valid_axis=True, - force_int_axis=True, - ), -) -def test_torch_tensor_stride( - dtype_value_axis, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x, axis = dtype_value_axis - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={"dim": axis}, - frontend=frontend, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - on_device=on_device, - ) - - -# sub -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="sub", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=2, - min_value=-1e04, - max_value=1e04, - allow_inf=False, - ), - alpha=st.floats(min_value=-1e04, max_value=1e04, allow_infinity=False), -) -def test_torch_tensor_sub( - dtype_and_x, - alpha, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - "alpha": alpha, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - atol_=1e-02, - on_device=on_device, - ) - - -# subtract_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="subtract_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - num_arrays=2, - ), -) -def test_torch_tensor_subtract_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=[input_dtype[0]], - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# sum -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="sum", - dtype_x_dim=_get_castable_dtype( - min_value=-1e04, - max_value=1e04, - ), - keepdim=st.booleans(), -) -def test_torch_tensor_sum( - dtype_x_dim, - keepdim, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x, dim, castable_dtype = dtype_x_dim - if method_flags.as_variable: - castable_dtype = input_dtype - input_dtype = [input_dtype] - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "dim": dim, - "keepdim": keepdim, - "dtype": castable_dtype, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="svd", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - min_value=0, - max_value=10, - shape=helpers.ints(min_value=2, max_value=5).map(lambda x: tuple([x, x])), - ), - some=st.booleans(), - compute_uv=st.booleans(), -) -def test_torch_tensor_svd( - dtype_and_x, - some, - compute_uv, - frontend, - backend_fw, - frontend_method_data, - init_flags, - method_flags, - on_device, -): - input_dtype, x = dtype_and_x - x = np.asarray(x[0], dtype=input_dtype[0]) - - ret, frontend_ret = helpers.test_frontend_method( - init_input_dtypes=input_dtype, - init_all_as_kwargs_np={ - "data": x, - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "some": some, - "compute_uv": compute_uv, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - backend_to_test=backend_fw, - on_device=on_device, - test_values=False, - ) - with helpers.update_backend(backend_fw) as ivy_backend: - ret = [ivy_backend.to_numpy(x) for x in ret] - frontend_ret = [np.asarray(x) for x in frontend_ret] - - u, s, vh = ret - frontend_u, frontend_s, frontend_vh = frontend_ret - - if compute_uv: - helpers.assert_all_close( - ret_np=frontend_u @ np.diag(frontend_s) @ frontend_vh.T, - ret_from_gt_np=u @ np.diag(s) @ vh, - rtol=1e-2, - atol=1e-2, - backend=backend_fw, - ground_truth_backend=frontend, - ) - else: - helpers.assert_all_close( - ret_np=frontend_s, - ret_from_gt_np=s, - rtol=1e-2, - atol=1e-2, - backend=backend_fw, - ground_truth_backend=frontend, - ) - - -# t -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="t", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - shape=helpers.get_shape(min_num_dims=2, max_num_dims=2), - ), -) -def test_torch_tensor_t( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="take_along_dim", - dtype_indices_axis=helpers.array_indices_axis( - array_dtypes=helpers.get_dtypes("numeric"), - indices_dtypes=["int64"], - min_num_dims=1, - max_num_dims=5, - min_dim_size=1, - max_dim_size=10, - indices_same_dims=True, - ), -) -def test_torch_tensor_take_along_dim( - dtype_indices_axis, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtypes, value, indices, axis, _ = dtype_indices_axis - helpers.test_frontend_method( - init_input_dtypes=[input_dtypes[0]], - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": value, - }, - method_input_dtypes=[input_dtypes[1]], - method_all_as_kwargs_np={ - "indices": indices, - "dim": axis, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# tan -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="tan", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_tan( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# tan_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="tan_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_tan_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# tanh -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="tanh", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_tanh( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# tanh_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="tanh_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_tanh_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# tensor_split -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="tensor_split", - dtype_value=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("integer"), - shape=st.shared(helpers.get_shape(min_num_dims=1), key="value_shape"), - ), - indices_or_sections=_get_splits( - min_num_dims=1, allow_none=False, allow_array_indices=False - ), - dim=st.shared( - helpers.get_axis( - shape=st.shared(helpers.get_shape(min_num_dims=1), key="value_shape"), - force_int=True, - ), - key="target_axis", - ), - method_num_positional_args=st.just(1), -) -def test_torch_tensor_tensor_split( - dtype_value, - indices_or_sections, - dim, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_value - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={ - "indices_or_sections": indices_or_sections, - "dim": dim, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="tile", - dtype_and_values=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - shape=st.shared(helpers.get_shape(), key="shape"), - ), - reps=helpers.get_axis( - shape=st.shared(helpers.get_shape(), key="shape"), - allow_neg=False, - ), -) -def test_torch_tensor_tile( - dtype_and_values, - reps, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, values = dtype_and_values - if isinstance(reps, tuple): - method_flags.num_positional_args = len(reps) - else: - method_flags.num_positional_args = 1 - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": values[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "reps": reps, - }, - init_flags=init_flags, - method_flags=method_flags, - frontend_method_data=frontend_method_data, - frontend=frontend, - on_device=on_device, - ) - - -# to -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="to", - args_kwargs=_to_helper(), -) -def test_torch_tensor_to( - args_kwargs, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x, method_num_positional_args, method_all_as_kwargs_np = args_kwargs - method_flags.num_positional_args = method_num_positional_args - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np=method_all_as_kwargs_np, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# topk -# TODO: add value test after the stable sorting is added to torch -# https://github.com/pytorch/pytorch/issues/88184 -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="topk", - dtype_x_axis_k=_topk_helper(), - largest=st.booleans(), - sorted=st.booleans(), -) -def test_torch_tensor_topk( - dtype_x_axis_k, - largest, - sorted, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, input, axis, k = dtype_x_axis_k - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": input[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "k": k, - "dim": axis, - "largest": largest, - "sorted": sorted, - }, - frontend=frontend, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - on_device=on_device, - test_values=False, - ) - - -# transpose -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="transpose", - dtype_value=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), - ), - dim0=helpers.get_axis( - shape=st.shared(helpers.get_shape(), key="shape"), - allow_neg=True, - force_int=True, - ), - dim1=helpers.get_axis( - shape=st.shared(helpers.get_shape(), key="shape"), - allow_neg=True, - force_int=True, - ), -) -def test_torch_tensor_transpose( - dtype_value, - dim0, - dim1, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_value - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={"dim0": dim0, "dim1": dim1}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# transpose_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="transpose_", - dtype_value=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), - ), - dim0=helpers.get_axis( - shape=st.shared(helpers.get_shape(), key="shape"), - allow_neg=True, - force_int=True, - ), - dim1=helpers.get_axis( - shape=st.shared(helpers.get_shape(), key="shape"), - allow_neg=True, - force_int=True, - ), -) -def test_torch_tensor_transpose_( - dtype_value, - dim0, - dim1, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_value - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "dim0": dim0, - "dim1": dim1, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# tril -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="tril", - dtype_and_values=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - min_num_dims=2, # Torch requires this. - ), - diagonal=st.integers(min_value=-100, max_value=100), -) -def test_torch_tensor_tril( - dtype_and_values, - diagonal, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_values - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "diagonal": diagonal, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# tril_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="tril_", - dtype_and_values=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - min_num_dims=2, # Torch requires this. - ), - diagonal=st.integers(min_value=-100, max_value=100), -) -def test_torch_tensor_tril_( - dtype_and_values, - diagonal, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_values - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "diagonal": diagonal, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# true_divide_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="true_divide_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - num_arrays=2, - large_abs_safety_factor=2.5, - small_abs_safety_factor=2.5, - safety_factor_scale="log", - ), -) -def test_torch_tensor_true_divide_( - dtype_and_x, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - assume(not np.any(np.isclose(x[1], 0))) - - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# trunc -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="trunc", - dtype_value=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), - ), -) -def test_torch_tensor_trunc( - dtype_value, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_value - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# trunc_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="trunc_", - dtype_value=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - shape=st.shared(helpers.get_shape(min_num_dims=1), key="shape"), - ), -) -def test_torch_tensor_trunc_( - dtype_value, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_value - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# type -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="type", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - ), - dtype=helpers.get_dtypes("valid", full=False), -) -def test_torch_tensor_type( - dtype_and_x, - dtype, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "dtype": dtype[0], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# type_as -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="type_as", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - num_arrays=2, - ), -) -def test_torch_tensor_type_as( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# unbind -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="unbind", - dtype_value_axis=helpers.dtype_values_axis( - available_dtypes=helpers.get_dtypes("numeric"), - min_num_dims=1, - valid_axis=True, - force_int_axis=True, - ), -) -def test_torch_tensor_unbind( - dtype_value_axis, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtypes, x, axis = dtype_value_axis - helpers.test_frontend_method( - init_input_dtypes=input_dtypes, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtypes, - method_all_as_kwargs_np={ - "dim": axis, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# unfold -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="unfold", - dtype_values_args=_unfold_args(), -) -def test_torch_tensor_unfold( - dtype_values_args, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x, axis, size, step = dtype_values_args - print(axis, size, step) - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x, - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "dimension": axis, - "size": size, - "step": step, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# unsqueeze -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="unsqueeze", - dtype_value=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - shape=st.shared(helpers.get_shape(), key="shape"), - ), - dim=helpers.get_axis( - shape=st.shared(helpers.get_shape(), key="shape"), - allow_neg=True, - force_int=True, - ), -) -def test_torch_tensor_unsqueeze( - dtype_value, - dim, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_value - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "dim": dim, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# unsqueeze_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="unsqueeze_", - dtype_value=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - shape=st.shared(helpers.get_shape(), key="shape"), - ), - dim=helpers.get_axis( - shape=st.shared(helpers.get_shape(), key="shape"), - allow_neg=True, - force_int=True, - ), -) -def test_torch_tensor_unsqueeze_( - dtype_value, - dim, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_value - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "dim": dim, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="var", - dtype_and_x=_statistical_dtype_values( - function="var", - min_value=-1e04, - max_value=1e04, - ), - keepdim=st.booleans(), -) -def test_torch_tensor_var( - dtype_and_x, - keepdim, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x, axis, correction = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "dim": axis, - "correction": int(correction), - "keepdim": keepdim, - }, - frontend=frontend, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - on_device=on_device, - ) - - -# view -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="view", - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - shape=st.shared(helpers.get_shape(), key="value_shape"), - ), - shape=helpers.reshape_shapes( - shape=st.shared(helpers.get_shape(min_num_dims=1), key="value_shape") - ), -) -def test_torch_tensor_view( - dtype_x, - shape, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "size": shape, - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# view_as -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="view_as", - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - shape=st.shared(helpers.get_shape(), key="value_shape"), - num_arrays=2, - ), -) -def test_torch_tensor_view_as( - dtype_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={ - "other": x[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# vsplit -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="vsplit", - dtype_value=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - shape=st.shared(helpers.get_shape(min_num_dims=2), key="value_shape"), - ), - indices_or_sections=_get_splits( - min_num_dims=2, - axis=0, - allow_none=False, - allow_array_indices=False, - is_mod_split=True, - ), -) -def test_torch_tensor_vsplit( - dtype_value, - indices_or_sections, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_value - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=[], - method_all_as_kwargs_np={"indices_or_sections": indices_or_sections}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# where -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="where", - broadcastables=_broadcastable_trio(), -) -def test_torch_tensor_where( - broadcastables, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - cond, xs, dtypes = broadcastables - helpers.test_frontend_method( - init_input_dtypes=dtypes, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": xs[0], - }, - method_input_dtypes=["bool", dtypes[1]], - method_all_as_kwargs_np={ - "condition": cond, - "other": xs[1], - }, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# zero_ tests -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="zero_", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - allow_inf=False, - ), -) -def test_torch_tensor_zero_( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - -# triu_ -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="torch.tensor", - method_name="triu_", - dtype_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - min_num_dims=2, - max_num_dims=5, - min_dim_size=1, - max_dim_size=5, - ), - diagonal=st.integers( - min_value=-4, - max_value=4, - ), -) -def test_torch_triu_( - dtype_x, - diagonal, - frontend, - frontend_method_data, - init_flags, - method_flags, - on_device, - backend_fw, -): - input_dtype, x = dtype_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={"data": x[0]}, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={"diagonal": diagonal}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - )