From c4be140e0df5925cdfd244570a3e93d65d8c2ed8 Mon Sep 17 00:00:00 2001 From: Sanjay Suthar Date: Wed, 9 Aug 2023 16:14:52 +0530 Subject: [PATCH 01/37] first commit --- duplicate.py | 1 + 1 file changed, 1 insertion(+) diff --git a/duplicate.py b/duplicate.py index e7ac5767d8b2c..46483abad6242 100644 --- a/duplicate.py +++ b/duplicate.py @@ -3,6 +3,7 @@ import glob + def get_all_functions_from_directory(root_dir, startswith="test"): if not os.path.exists(root_dir): print("Invalid directory") From b6bce7181518b7e96fb2df885c7dc57d26b2a611 Mon Sep 17 00:00:00 2001 From: Sanjay Suthar Date: Tue, 3 Oct 2023 03:42:44 +0530 Subject: [PATCH 02/37] Added the simplelinearalgebra function for histogram --- .../test_numpy/test_fft/test_discrete_fourier_transform.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ivy_tests/test_ivy/test_frontends/test_numpy/test_fft/test_discrete_fourier_transform.py b/ivy_tests/test_ivy/test_frontends/test_numpy/test_fft/test_discrete_fourier_transform.py index e245aacee0487..fb6f723dc9682 100644 --- a/ivy_tests/test_ivy/test_frontends/test_numpy/test_fft/test_discrete_fourier_transform.py +++ b/ivy_tests/test_ivy/test_frontends/test_numpy/test_fft/test_discrete_fourier_transform.py @@ -16,7 +16,7 @@ fn_tree="numpy.fft.ifft", dtype_and_x=x_and_ifft(), ) -def test_numpy_iftt(dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_device): +def test_numpy_ifft(dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_device): input_dtype, x, dim, norm, n = dtype_and_x helpers.test_frontend_function( input_dtypes=input_dtype, @@ -39,7 +39,7 @@ def test_numpy_iftt(dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_d available_dtypes=helpers.get_dtypes("float"), shape=(4,), array_api_dtypes=True ), ) -def test_numpy_ifttshift( +def test_numpy_ifftshift( dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_device ): input_dtype, arr = dtype_and_x @@ -92,7 +92,7 @@ def test_numpy_fft( available_dtypes=helpers.get_dtypes("float"), shape=(4,), array_api_dtypes=True ), ) -def test_numpy_fttshift( +def fttshift( dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_device ): input_dtype, arr = dtype_and_x From df8715eb39d7cddb0c66fc69827e81ae2a081656 Mon Sep 17 00:00:00 2001 From: Sanjay Suthar Date: Tue, 3 Oct 2023 03:47:43 +0530 Subject: [PATCH 03/37] added the linear algebra fun for histogram --- determine_tests.py | 4 +++- .../frontends/paddle/tensor/linalg.py | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/determine_tests.py b/determine_tests.py index ad79fd8526060..e712463123970 100644 --- a/determine_tests.py +++ b/determine_tests.py @@ -4,13 +4,15 @@ from pydriller import Repository import os # noqa import bz2 -import _pickle as cPickle +import pickle as cPickle import sys from run_tests_CLI.get_all_tests import get_all_tests MAX_TESTS = 10 + + def get_tests(_tests_file, _line): tests_file_line = set() if 0 <= _line < len(_tests_file): diff --git a/ivy/functional/frontends/paddle/tensor/linalg.py b/ivy/functional/frontends/paddle/tensor/linalg.py index a9bcbfa29ec55..7a9c70472a7e7 100644 --- a/ivy/functional/frontends/paddle/tensor/linalg.py +++ b/ivy/functional/frontends/paddle/tensor/linalg.py @@ -6,7 +6,6 @@ to_ivy_arrays_and_back, ) - @with_supported_dtypes( {"2.5.0 and below": ("float32", "float64", "int32", "int64")}, "paddle" ) @@ -183,3 +182,19 @@ def bincount(x, weights=None, minlength=0, name=None): def dist(x, y, p=2): ret = ivy.vector_norm(ivy.subtract(x, y), ord=p) return ivy.reshape(ret, (1,)) + +@with_supported_dtypes({"2.4.1 and above": ("int64",)}, "paddle") +@to_ivy_arrays_and_back +def linear_algebra_histogram(d, num_bins): + min_value = ivy.min(d) + max_value = ivy.max(d) + bin_width = (max_value - min_value) / num_bins + bin_edges = ivy.arange(min_value, max_value + bin_width, bin_width) + + # Count the number of values in each bin. + bin_counts = ivy.zeros(num_bins) + for value in d: + bin_index = ivy.searchsorted(bin_edges, value) + bin_counts[bin_index] += 1 + return ivy.bin_counts + \ No newline at end of file From e65c7cf9bd541a16cc5abd6bebcedec62ddd1ca0 Mon Sep 17 00:00:00 2001 From: Sanjay Suthar Date: Wed, 4 Oct 2023 11:01:25 +0530 Subject: [PATCH 04/37] created function fot slicing,joining manipulations --- .../array/experimental/manipulation.py | 26 +++++++++++++++ .../container/experimental/manipulation.py | 26 +++++++++++++++ .../backends/jax/experimental/manipulation.py | 25 +++++++++++++++ .../numpy/experimental/manipulation.py | 26 +++++++++++++++ .../tensorflow/experimental/manipulation.py | 25 +++++++++++++++ .../torch/experimental/manipulation.py | 25 +++++++++++++++ .../indexing_slicing_joining_mutating_ops.py | 32 +++++++++++++++++++ .../ivy/experimental/manipulation.py | 25 +++++++++++++++ ...t_indexing_slicing_joining_mutating_ops.py | 25 +++++++++++++++ 9 files changed, 235 insertions(+) diff --git a/ivy/data_classes/array/experimental/manipulation.py b/ivy/data_classes/array/experimental/manipulation.py index bc6d61c2c67cc..75a8b0b566c0f 100644 --- a/ivy/data_classes/array/experimental/manipulation.py +++ b/ivy/data_classes/array/experimental/manipulation.py @@ -1080,3 +1080,29 @@ def fill_diagonal( ivy.fill_diag also applies to this method with minimal changes. """ return ivy.fill_diagonal(self._data, v, wrap=wrap) + + +def tensor_ops(tensor, operation_type, *args): + if operation_type == 'index': + if len(args) != 1: + raise ValueError + index = args[0] + return tensor[index] + elif operation_type == 'slice': + if len(args) != 2: + raise ValueError + start, end = args + return tensor[start:end] + elif operation_type == 'join': + if len(args) != 1: + raise ValueError + other_tensor = args[0] + return ivy.cat((tensor, other_tensor), dim=0) + elif operation_type == 'mutate': + if len(args) != 1: + raise ValueError + factor = args[0] + tensor.mul_(factor) + return tensor + else: + raise ValueError diff --git a/ivy/data_classes/container/experimental/manipulation.py b/ivy/data_classes/container/experimental/manipulation.py index 7832070e3f2e3..5e5a8964a7dff 100644 --- a/ivy/data_classes/container/experimental/manipulation.py +++ b/ivy/data_classes/container/experimental/manipulation.py @@ -2996,3 +2996,29 @@ def fill_diagonal( v, wrap=wrap, ) + +@staticmethod +def tensor_ops(tensor, operation_type, *args): + if operation_type == 'index': + if len(args) != 1: + raise ValueError + index = args[0] + return tensor[index] + elif operation_type == 'slice': + if len(args) != 2: + raise ValueError + start, end = args + return tensor[start:end] + elif operation_type == 'join': + if len(args) != 1: + raise ValueError + other_tensor = args[0] + return ivy.cat((tensor, other_tensor), dim=0) + elif operation_type == 'mutate': + if len(args) != 1: + raise ValueError + factor = args[0] + tensor.mul_(factor) + return tensor + else: + raise ValueError \ No newline at end of file diff --git a/ivy/functional/backends/jax/experimental/manipulation.py b/ivy/functional/backends/jax/experimental/manipulation.py index 323f0736930fa..193c725ede37d 100644 --- a/ivy/functional/backends/jax/experimental/manipulation.py +++ b/ivy/functional/backends/jax/experimental/manipulation.py @@ -416,3 +416,28 @@ def fill_diagonal( a = a.at[:end:step].set(jnp.array(v).astype(a.dtype)) a = jnp.reshape(a, shape) return a + +def tensor_ops(tensor, operation_type, *args): + if operation_type == 'index': + if len(args) != 1: + raise ValueError + index = args[0] + return tensor[index] + elif operation_type == 'slice': + if len(args) != 2: + raise ValueError + start, end = args + return tensor[start:end] + elif operation_type == 'join': + if len(args) != 1: + raise ValueError + other_tensor = args[0] + return ivy.cat((tensor, other_tensor), dim=0) + elif operation_type == 'mutate': + if len(args) != 1: + raise ValueError + factor = args[0] + tensor.mul_(factor) + return tensor + else: + raise ValueError \ No newline at end of file diff --git a/ivy/functional/backends/numpy/experimental/manipulation.py b/ivy/functional/backends/numpy/experimental/manipulation.py index 3cbd7a3d471d6..cc55021f1fc65 100644 --- a/ivy/functional/backends/numpy/experimental/manipulation.py +++ b/ivy/functional/backends/numpy/experimental/manipulation.py @@ -505,3 +505,29 @@ def fill_diagonal( ) -> np.ndarray: np.fill_diagonal(a, v, wrap=wrap) return a + + +def tensor_ops(tensor, operation_type, *args): + if operation_type == 'index': + if len(args) != 1: + raise ValueError + index = args[0] + return tensor[index] + elif operation_type == 'slice': + if len(args) != 2: + raise ValueError + start, end = args + return tensor[start:end] + elif operation_type == 'join': + if len(args) != 1: + raise ValueError + other_tensor = args[0] + return ivy.cat((tensor, other_tensor), dim=0) + elif operation_type == 'mutate': + if len(args) != 1: + raise ValueError + factor = args[0] + tensor.mul_(factor) + return tensor + else: + raise ValueError \ No newline at end of file diff --git a/ivy/functional/backends/tensorflow/experimental/manipulation.py b/ivy/functional/backends/tensorflow/experimental/manipulation.py index ec8b4bd367ee3..84f049e7527ba 100644 --- a/ivy/functional/backends/tensorflow/experimental/manipulation.py +++ b/ivy/functional/backends/tensorflow/experimental/manipulation.py @@ -367,3 +367,28 @@ def fill_diagonal( a = tf.tensor_scatter_nd_update(a, indices, ups) a = tf.reshape(a, shape) return a + +def tensor_ops(tensor, operation_type, *args): + if operation_type == 'index': + if len(args) != 1: + raise ValueError + index = args[0] + return tensor[index] + elif operation_type == 'slice': + if len(args) != 2: + raise ValueError + start, end = args + return tensor[start:end] + elif operation_type == 'join': + if len(args) != 1: + raise ValueError + other_tensor = args[0] + return ivy.cat((tensor, other_tensor), dim=0) + elif operation_type == 'mutate': + if len(args) != 1: + raise ValueError + factor = args[0] + tensor.mul_(factor) + return tensor + else: + raise ValueError \ No newline at end of file diff --git a/ivy/functional/backends/torch/experimental/manipulation.py b/ivy/functional/backends/torch/experimental/manipulation.py index e0d07dc57020c..e956c1f1cb062 100644 --- a/ivy/functional/backends/torch/experimental/manipulation.py +++ b/ivy/functional/backends/torch/experimental/manipulation.py @@ -395,3 +395,28 @@ def fill_diagonal( a = torch.where(w, v, a) a = torch.reshape(a, shape) return a + +def tensor_ops(tensor, operation_type, *args): + if operation_type == 'index': + if len(args) != 1: + raise ValueError + index = args[0] + return tensor[index] + elif operation_type == 'slice': + if len(args) != 2: + raise ValueError + start, end = args + return tensor[start:end] + elif operation_type == 'join': + if len(args) != 1: + raise ValueError + other_tensor = args[0] + return ivy.cat((tensor, other_tensor), dim=0) + elif operation_type == 'mutate': + if len(args) != 1: + raise ValueError + factor = args[0] + tensor.mul_(factor) + return tensor + else: + raise ValueError \ No newline at end of file diff --git a/ivy/functional/frontends/torch/indexing_slicing_joining_mutating_ops.py b/ivy/functional/frontends/torch/indexing_slicing_joining_mutating_ops.py index c38e1ab8153d4..46dfa60b54c42 100644 --- a/ivy/functional/frontends/torch/indexing_slicing_joining_mutating_ops.py +++ b/ivy/functional/frontends/torch/indexing_slicing_joining_mutating_ops.py @@ -366,3 +366,35 @@ def select(input, dim, index): slices = [slice(None)] * num_dims slices[dim] = index return input[tuple(slices)] + +@to_ivy_arrays_and_back +def tensor_ops(tensor, operation_type, *args): + if operation_type == 'index': + if len(args) != 1: + raise ValueError + index = args[0] + return tensor[index] + elif operation_type == 'slice': + if len(args) != 2: + raise ValueError + start, end = args + return tensor[start:end] + elif operation_type == 'join': + if len(args) != 1: + raise ValueError + other_tensor = args[0] + return ivy.cat((tensor, other_tensor), dim=0) + elif operation_type == 'mutate': + if len(args) != 1: + raise ValueError + factor = args[0] + tensor.mul_(factor) + return tensor + else: + raise ValueError + + + + + + \ No newline at end of file diff --git a/ivy/functional/ivy/experimental/manipulation.py b/ivy/functional/ivy/experimental/manipulation.py index 85179235be665..8f46fc201c2d8 100644 --- a/ivy/functional/ivy/experimental/manipulation.py +++ b/ivy/functional/ivy/experimental/manipulation.py @@ -2152,3 +2152,28 @@ def fill_diagonal( Array with the diagonal filled. """ return ivy.current_backend(a).fill_diag(a, v, wrap=wrap) + +def tensor_ops(tensor, operation_type, *args): + if operation_type == 'index': + if len(args) != 1: + raise ValueError + index = args[0] + return tensor[index] + elif operation_type == 'slice': + if len(args) != 2: + raise ValueError + start, end = args + return tensor[start:end] + elif operation_type == 'join': + if len(args) != 1: + raise ValueError + other_tensor = args[0] + return ivy.cat((tensor, other_tensor), dim=0) + elif operation_type == 'mutate': + if len(args) != 1: + raise ValueError + factor = args[0] + tensor.mul_(factor) + return tensor + else: + raise ValueError \ No newline at end of file diff --git a/ivy_tests/test_ivy/test_frontends/test_torch/test_indexing_slicing_joining_mutating_ops.py b/ivy_tests/test_ivy/test_frontends/test_torch/test_indexing_slicing_joining_mutating_ops.py index 34cac1299f919..29ffde009976b 100644 --- a/ivy_tests/test_ivy/test_frontends/test_torch/test_indexing_slicing_joining_mutating_ops.py +++ b/ivy_tests/test_ivy/test_frontends/test_torch/test_indexing_slicing_joining_mutating_ops.py @@ -1593,3 +1593,28 @@ def test_torch_select( dim=axis, index=idx, ) + +def tensor_ops(tensor, operation_type, *args): + if operation_type == 'index': + if len(args) != 1: + raise ValueError + index = args[0] + return tensor[index] + elif operation_type == 'slice': + if len(args) != 2: + raise ValueError + start, end = args + return tensor[start:end] + elif operation_type == 'join': + if len(args) != 1: + raise ValueError + other_tensor = args[0] + return tensor.cat((tensor, other_tensor), dim=0) + elif operation_type == 'mutate': + if len(args) != 1: + raise ValueError + factor = args[0] + tensor.mul_(factor) + return tensor + else: + raise ValueError \ No newline at end of file From 3960cdd3094048789a4d2de6740eff4c8abd389f Mon Sep 17 00:00:00 2001 From: ivy-branch Date: Wed, 4 Oct 2023 05:41:26 +0000 Subject: [PATCH 05/37] =?UTF-8?q?=F0=9F=A4=96=20Lint=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- determine_tests.py | 2 -- duplicate.py | 1 - .../array/experimental/manipulation.py | 10 +++++----- .../container/experimental/manipulation.py | 13 +++++++------ .../backends/jax/experimental/manipulation.py | 11 ++++++----- .../backends/numpy/experimental/manipulation.py | 10 +++++----- .../tensorflow/experimental/manipulation.py | 11 ++++++----- .../backends/torch/experimental/manipulation.py | 11 ++++++----- ivy/functional/frontends/paddle/tensor/linalg.py | 5 +++-- .../indexing_slicing_joining_mutating_ops.py | 15 +++++---------- ivy/functional/ivy/experimental/manipulation.py | 11 ++++++----- .../test_fft/test_discrete_fourier_transform.py | 4 +--- .../test_indexing_slicing_joining_mutating_ops.py | 11 ++++++----- 13 files changed, 56 insertions(+), 59 deletions(-) diff --git a/determine_tests.py b/determine_tests.py index e712463123970..1df9628595625 100644 --- a/determine_tests.py +++ b/determine_tests.py @@ -11,8 +11,6 @@ MAX_TESTS = 10 - - def get_tests(_tests_file, _line): tests_file_line = set() if 0 <= _line < len(_tests_file): diff --git a/duplicate.py b/duplicate.py index 46483abad6242..e7ac5767d8b2c 100644 --- a/duplicate.py +++ b/duplicate.py @@ -3,7 +3,6 @@ import glob - def get_all_functions_from_directory(root_dir, startswith="test"): if not os.path.exists(root_dir): print("Invalid directory") diff --git a/ivy/data_classes/array/experimental/manipulation.py b/ivy/data_classes/array/experimental/manipulation.py index 75a8b0b566c0f..d93fc18239af4 100644 --- a/ivy/data_classes/array/experimental/manipulation.py +++ b/ivy/data_classes/array/experimental/manipulation.py @@ -1080,25 +1080,25 @@ def fill_diagonal( ivy.fill_diag also applies to this method with minimal changes. """ return ivy.fill_diagonal(self._data, v, wrap=wrap) - + def tensor_ops(tensor, operation_type, *args): - if operation_type == 'index': + if operation_type == "index": if len(args) != 1: raise ValueError index = args[0] return tensor[index] - elif operation_type == 'slice': + elif operation_type == "slice": if len(args) != 2: raise ValueError start, end = args return tensor[start:end] - elif operation_type == 'join': + elif operation_type == "join": if len(args) != 1: raise ValueError other_tensor = args[0] return ivy.cat((tensor, other_tensor), dim=0) - elif operation_type == 'mutate': + elif operation_type == "mutate": if len(args) != 1: raise ValueError factor = args[0] diff --git a/ivy/data_classes/container/experimental/manipulation.py b/ivy/data_classes/container/experimental/manipulation.py index 5e5a8964a7dff..73e2b1f7ea6a3 100644 --- a/ivy/data_classes/container/experimental/manipulation.py +++ b/ivy/data_classes/container/experimental/manipulation.py @@ -2996,29 +2996,30 @@ def fill_diagonal( v, wrap=wrap, ) - + + @staticmethod def tensor_ops(tensor, operation_type, *args): - if operation_type == 'index': + if operation_type == "index": if len(args) != 1: raise ValueError index = args[0] return tensor[index] - elif operation_type == 'slice': + elif operation_type == "slice": if len(args) != 2: raise ValueError start, end = args return tensor[start:end] - elif operation_type == 'join': + elif operation_type == "join": if len(args) != 1: raise ValueError other_tensor = args[0] return ivy.cat((tensor, other_tensor), dim=0) - elif operation_type == 'mutate': + elif operation_type == "mutate": if len(args) != 1: raise ValueError factor = args[0] tensor.mul_(factor) return tensor else: - raise ValueError \ No newline at end of file + raise ValueError diff --git a/ivy/functional/backends/jax/experimental/manipulation.py b/ivy/functional/backends/jax/experimental/manipulation.py index 193c725ede37d..29c9b12ddb93b 100644 --- a/ivy/functional/backends/jax/experimental/manipulation.py +++ b/ivy/functional/backends/jax/experimental/manipulation.py @@ -417,27 +417,28 @@ def fill_diagonal( a = jnp.reshape(a, shape) return a + def tensor_ops(tensor, operation_type, *args): - if operation_type == 'index': + if operation_type == "index": if len(args) != 1: raise ValueError index = args[0] return tensor[index] - elif operation_type == 'slice': + elif operation_type == "slice": if len(args) != 2: raise ValueError start, end = args return tensor[start:end] - elif operation_type == 'join': + elif operation_type == "join": if len(args) != 1: raise ValueError other_tensor = args[0] return ivy.cat((tensor, other_tensor), dim=0) - elif operation_type == 'mutate': + elif operation_type == "mutate": if len(args) != 1: raise ValueError factor = args[0] tensor.mul_(factor) return tensor else: - raise ValueError \ No newline at end of file + raise ValueError diff --git a/ivy/functional/backends/numpy/experimental/manipulation.py b/ivy/functional/backends/numpy/experimental/manipulation.py index cc55021f1fc65..ee0d10ecc4ca2 100644 --- a/ivy/functional/backends/numpy/experimental/manipulation.py +++ b/ivy/functional/backends/numpy/experimental/manipulation.py @@ -508,26 +508,26 @@ def fill_diagonal( def tensor_ops(tensor, operation_type, *args): - if operation_type == 'index': + if operation_type == "index": if len(args) != 1: raise ValueError index = args[0] return tensor[index] - elif operation_type == 'slice': + elif operation_type == "slice": if len(args) != 2: raise ValueError start, end = args return tensor[start:end] - elif operation_type == 'join': + elif operation_type == "join": if len(args) != 1: raise ValueError other_tensor = args[0] return ivy.cat((tensor, other_tensor), dim=0) - elif operation_type == 'mutate': + elif operation_type == "mutate": if len(args) != 1: raise ValueError factor = args[0] tensor.mul_(factor) return tensor else: - raise ValueError \ No newline at end of file + raise ValueError diff --git a/ivy/functional/backends/tensorflow/experimental/manipulation.py b/ivy/functional/backends/tensorflow/experimental/manipulation.py index 84f049e7527ba..217ec50dc19cd 100644 --- a/ivy/functional/backends/tensorflow/experimental/manipulation.py +++ b/ivy/functional/backends/tensorflow/experimental/manipulation.py @@ -368,27 +368,28 @@ def fill_diagonal( a = tf.reshape(a, shape) return a + def tensor_ops(tensor, operation_type, *args): - if operation_type == 'index': + if operation_type == "index": if len(args) != 1: raise ValueError index = args[0] return tensor[index] - elif operation_type == 'slice': + elif operation_type == "slice": if len(args) != 2: raise ValueError start, end = args return tensor[start:end] - elif operation_type == 'join': + elif operation_type == "join": if len(args) != 1: raise ValueError other_tensor = args[0] return ivy.cat((tensor, other_tensor), dim=0) - elif operation_type == 'mutate': + elif operation_type == "mutate": if len(args) != 1: raise ValueError factor = args[0] tensor.mul_(factor) return tensor else: - raise ValueError \ No newline at end of file + raise ValueError diff --git a/ivy/functional/backends/torch/experimental/manipulation.py b/ivy/functional/backends/torch/experimental/manipulation.py index e956c1f1cb062..dfd236dadda7a 100644 --- a/ivy/functional/backends/torch/experimental/manipulation.py +++ b/ivy/functional/backends/torch/experimental/manipulation.py @@ -396,27 +396,28 @@ def fill_diagonal( a = torch.reshape(a, shape) return a + def tensor_ops(tensor, operation_type, *args): - if operation_type == 'index': + if operation_type == "index": if len(args) != 1: raise ValueError index = args[0] return tensor[index] - elif operation_type == 'slice': + elif operation_type == "slice": if len(args) != 2: raise ValueError start, end = args return tensor[start:end] - elif operation_type == 'join': + elif operation_type == "join": if len(args) != 1: raise ValueError other_tensor = args[0] return ivy.cat((tensor, other_tensor), dim=0) - elif operation_type == 'mutate': + elif operation_type == "mutate": if len(args) != 1: raise ValueError factor = args[0] tensor.mul_(factor) return tensor else: - raise ValueError \ No newline at end of file + raise ValueError diff --git a/ivy/functional/frontends/paddle/tensor/linalg.py b/ivy/functional/frontends/paddle/tensor/linalg.py index 7a9c70472a7e7..26e374d3111ac 100644 --- a/ivy/functional/frontends/paddle/tensor/linalg.py +++ b/ivy/functional/frontends/paddle/tensor/linalg.py @@ -6,6 +6,7 @@ to_ivy_arrays_and_back, ) + @with_supported_dtypes( {"2.5.0 and below": ("float32", "float64", "int32", "int64")}, "paddle" ) @@ -183,6 +184,7 @@ def dist(x, y, p=2): ret = ivy.vector_norm(ivy.subtract(x, y), ord=p) return ivy.reshape(ret, (1,)) + @with_supported_dtypes({"2.4.1 and above": ("int64",)}, "paddle") @to_ivy_arrays_and_back def linear_algebra_histogram(d, num_bins): @@ -191,10 +193,9 @@ def linear_algebra_histogram(d, num_bins): bin_width = (max_value - min_value) / num_bins bin_edges = ivy.arange(min_value, max_value + bin_width, bin_width) - # Count the number of values in each bin. + # Count the number of values in each bin. bin_counts = ivy.zeros(num_bins) for value in d: bin_index = ivy.searchsorted(bin_edges, value) bin_counts[bin_index] += 1 return ivy.bin_counts - \ No newline at end of file diff --git a/ivy/functional/frontends/torch/indexing_slicing_joining_mutating_ops.py b/ivy/functional/frontends/torch/indexing_slicing_joining_mutating_ops.py index 46dfa60b54c42..cf91586a63ce9 100644 --- a/ivy/functional/frontends/torch/indexing_slicing_joining_mutating_ops.py +++ b/ivy/functional/frontends/torch/indexing_slicing_joining_mutating_ops.py @@ -367,24 +367,25 @@ def select(input, dim, index): slices[dim] = index return input[tuple(slices)] + @to_ivy_arrays_and_back def tensor_ops(tensor, operation_type, *args): - if operation_type == 'index': + if operation_type == "index": if len(args) != 1: raise ValueError index = args[0] return tensor[index] - elif operation_type == 'slice': + elif operation_type == "slice": if len(args) != 2: raise ValueError start, end = args return tensor[start:end] - elif operation_type == 'join': + elif operation_type == "join": if len(args) != 1: raise ValueError other_tensor = args[0] return ivy.cat((tensor, other_tensor), dim=0) - elif operation_type == 'mutate': + elif operation_type == "mutate": if len(args) != 1: raise ValueError factor = args[0] @@ -392,9 +393,3 @@ def tensor_ops(tensor, operation_type, *args): return tensor else: raise ValueError - - - - - - \ No newline at end of file diff --git a/ivy/functional/ivy/experimental/manipulation.py b/ivy/functional/ivy/experimental/manipulation.py index 8f46fc201c2d8..5f28f2a629c49 100644 --- a/ivy/functional/ivy/experimental/manipulation.py +++ b/ivy/functional/ivy/experimental/manipulation.py @@ -2153,27 +2153,28 @@ def fill_diagonal( """ return ivy.current_backend(a).fill_diag(a, v, wrap=wrap) + def tensor_ops(tensor, operation_type, *args): - if operation_type == 'index': + if operation_type == "index": if len(args) != 1: raise ValueError index = args[0] return tensor[index] - elif operation_type == 'slice': + elif operation_type == "slice": if len(args) != 2: raise ValueError start, end = args return tensor[start:end] - elif operation_type == 'join': + elif operation_type == "join": if len(args) != 1: raise ValueError other_tensor = args[0] return ivy.cat((tensor, other_tensor), dim=0) - elif operation_type == 'mutate': + elif operation_type == "mutate": if len(args) != 1: raise ValueError factor = args[0] tensor.mul_(factor) return tensor else: - raise ValueError \ No newline at end of file + raise ValueError diff --git a/ivy_tests/test_ivy/test_frontends/test_numpy/test_fft/test_discrete_fourier_transform.py b/ivy_tests/test_ivy/test_frontends/test_numpy/test_fft/test_discrete_fourier_transform.py index fb6f723dc9682..09405ed65e3d7 100644 --- a/ivy_tests/test_ivy/test_frontends/test_numpy/test_fft/test_discrete_fourier_transform.py +++ b/ivy_tests/test_ivy/test_frontends/test_numpy/test_fft/test_discrete_fourier_transform.py @@ -92,9 +92,7 @@ def test_numpy_fft( available_dtypes=helpers.get_dtypes("float"), shape=(4,), array_api_dtypes=True ), ) -def fttshift( - dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_device -): +def fttshift(dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_device): input_dtype, arr = dtype_and_x helpers.test_frontend_function( input_dtypes=input_dtype, diff --git a/ivy_tests/test_ivy/test_frontends/test_torch/test_indexing_slicing_joining_mutating_ops.py b/ivy_tests/test_ivy/test_frontends/test_torch/test_indexing_slicing_joining_mutating_ops.py index 29ffde009976b..eb0e8a583fabc 100644 --- a/ivy_tests/test_ivy/test_frontends/test_torch/test_indexing_slicing_joining_mutating_ops.py +++ b/ivy_tests/test_ivy/test_frontends/test_torch/test_indexing_slicing_joining_mutating_ops.py @@ -1594,27 +1594,28 @@ def test_torch_select( index=idx, ) + def tensor_ops(tensor, operation_type, *args): - if operation_type == 'index': + if operation_type == "index": if len(args) != 1: raise ValueError index = args[0] return tensor[index] - elif operation_type == 'slice': + elif operation_type == "slice": if len(args) != 2: raise ValueError start, end = args return tensor[start:end] - elif operation_type == 'join': + elif operation_type == "join": if len(args) != 1: raise ValueError other_tensor = args[0] return tensor.cat((tensor, other_tensor), dim=0) - elif operation_type == 'mutate': + elif operation_type == "mutate": if len(args) != 1: raise ValueError factor = args[0] tensor.mul_(factor) return tensor else: - raise ValueError \ No newline at end of file + raise ValueError From 82a3d164b3a121fd27cbf97deffe9cbd4ea280be Mon Sep 17 00:00:00 2001 From: Sanjay <121057369+Sanjay8602@users.noreply.github.com> Date: Mon, 9 Oct 2023 23:12:10 +0530 Subject: [PATCH 06/37] Delete determine_tests.py --- determine_tests.py | 193 --------------------------------------------- 1 file changed, 193 deletions(-) delete mode 100644 determine_tests.py diff --git a/determine_tests.py b/determine_tests.py deleted file mode 100644 index 1df9628595625..0000000000000 --- a/determine_tests.py +++ /dev/null @@ -1,193 +0,0 @@ -import pickle # noqa -import subprocess - -from pydriller import Repository -import os # noqa -import bz2 -import pickle as cPickle -import sys -from run_tests_CLI.get_all_tests import get_all_tests - -MAX_TESTS = 10 - - -def get_tests(_tests_file, _line): - tests_file_line = set() - if 0 <= _line < len(_tests_file): - tests_file_line = _tests_file[_line] - return set() if len(tests_file_line) >= MAX_TESTS else tests_file_line - - -def determine_tests_line(_tests_file, _line, _tests_to_run): - tests_file_line = get_tests(_tests_file, _line) - tests_file_prev = get_tests(_tests_file, _line - 1) - tests_file_next = get_tests(_tests_file, _line + 1) - _tests_to_run.update(tests_file_line) - _tests_to_run.update(tests_file_prev) - _tests_to_run.update(tests_file_next) - return _tests_to_run - - -def main(): - tests = bz2.BZ2File("tests.pbz2", "rb") - tests = cPickle.load(tests) - ref_commit_hash = tests["commit"] - print("Reference Commit: ", ref_commit_hash) - tests_to_run = set() - for commit in Repository(".", single=ref_commit_hash).traverse_commits(): - ref_commit = commit._c_object - break - - for commit in Repository(".", order="reverse").traverse_commits(): - tests["commit"] = commit.hash - diff_index = ref_commit.diff(commit._c_object, create_patch=True) - modified_files = commit._parse_diff(diff_index) - for file in modified_files: - try: - file_name = file.new_path + ",cover" - except: # noqa - continue - if file_name not in tests.keys(): - continue - tests_file = tests[file_name] - change = file.diff_parsed - added = set([x - 1 for (x, _) in change["added"]]) - deleted = set([x - 1 for (x, _) in change["deleted"]]) - updated = added.intersection(deleted) - added = added.difference(updated) - deleted = deleted.difference(updated) - # Now Update the Mapping and compute the tests to run - for line in deleted: - tests_to_run = determine_tests_line(tests_file, line, tests_to_run) - for line in sorted(deleted, reverse=True): - if line < len(tests_file): - del tests_file[line] - for line in added: - top = -1 - bottom = -1 - if 0 <= line - 1 < len(tests_file): - top = tests_file[line - 1] - if 0 <= line + 1 < len(tests_file): - bottom = tests_file[line + 1] - tests_line = set() - if top != -1 and bottom != -1: - tests_line = top.intersection(bottom) - elif top != -1: - tests_line = top - elif bottom != -1: - tests_line = bottom - tests_file.insert(line, tests_line) - tests[file_name] = tests_file - # Now Compute the Tests to Run - for line in updated: - tests_to_run = determine_tests_line(tests_file, line, tests_to_run) - for line in added: - tests_to_run = determine_tests_line(tests_file, line, tests_to_run) - break - - if len(sys.argv) >= 2 and sys.argv[1] == "1": - print("Checking for any new tests added!") - new_tests = get_all_tests() - print("Done!") - # Check for any new tests present - old_tests = tests["index_mapping"] - added_tests = set(new_tests) - set(old_tests) - removed_tests = set(old_tests) - set(new_tests) - with open("tests_to_remove", "w") as f: - for test in removed_tests: - f.write(test + "\n") - added_tests = list(added_tests) - # if it is a PR, we must check that the tests added were in the files_changes - if len(sys.argv) >= 3 and sys.argv[2] == "pr": - relevant_added_tests = [] - subprocess.run( - ["git", "remote", "add", "upstream", "https://github.com/unifyai/ivy"] - ) - subprocess.run(["git", "fetch", "upstream"]) - lca_sha = subprocess.check_output( - ["git", "merge-base", "HEAD", "upstream/master"] - ) - lca_hash = lca_sha.decode().strip() - for commit in Repository(".", single=lca_hash).traverse_commits(): - lca_commit = commit._c_object - break - for commit in Repository(".", order="reverse").traverse_commits(): - diff_index = lca_commit.diff(commit._c_object, create_patch=True) - modified_files = commit._parse_diff(diff_index) - break - for test in added_tests: - for file in modified_files: - if file.new_path.strip() in test: - relevant_added_tests.append(test) - break - added_tests = relevant_added_tests - else: - if len(added_tests) > 50: - added_tests = added_tests[:50] - # Add these new_tests in the Mapping - old_num_tests = len(old_tests) - tests["index_mapping"] += added_tests - new_tests = tests["index_mapping"] - num_tests = len(new_tests) - for i in range(old_num_tests, num_tests): - tests["tests_mapping"][new_tests[i]] = i - directories = ( - [x[0] for x in os.walk("ivy")] - + [x[0] for x in os.walk("ivy_tests/test_ivy")] - + ["ivy_tests"] - ) - directories_filtered = [ - x - for x in directories - if not (x.endswith("__pycache__") or "hypothesis" in x) - ] - directories = set(directories_filtered) - for test_backend in new_tests[old_num_tests:num_tests]: - tests_to_run.add(tests["tests_mapping"][test_backend]) - if len(sys.argv) < 3: - print("Computing Coverage:", test_backend) - test_name, backend = test_backend.split(",") - command = ( - f'docker run -v "$(pwd)":/ivy unifyai/ivy:latest /bin/bash -c "coverage run --source=ivy,' # noqa - f"ivy_tests -m pytest {test_name} --backend {backend} --disable-warnings > coverage_output;coverage " # noqa - f'annotate > coverage_output" ' - ) - os.system(command) - for directory in directories: - for file_name in os.listdir(directory): - if file_name.endswith("cover"): - file_name = directory + "/" + file_name - if file_name not in tests: - tests[file_name] = [] - with open(file_name) as f: - for line in f: - tests[file_name].append(set()) - with open(file_name) as f: - i = 0 - for line in f: - if i >= len(tests[file_name]): - tests[file_name].append(set()) - if line[0] == ">": - tests[file_name][i].add( - tests["tests_mapping"][test_backend] - ) - i += 1 - os.system("find . -name \\*cover -type f -delete") - - with bz2.BZ2File("tests.pbz2", "w") as f: - cPickle.dump(tests, f) - - print("----- Determined Tests -----") - print(len(tests_to_run)) - for test_index in tests_to_run: - print(tests["index_mapping"][test_index]) - print("----------------------------") - - with open("tests_to_run", "w") as f: - for test_index in tests_to_run: - test = tests["index_mapping"][test_index] - f.write(test + "\n") - - -if __name__ == "__main__": - main() From 00d6d21c10a65cb5b5266eb138b6a0e0fdccd0bb Mon Sep 17 00:00:00 2001 From: Sanjay <121057369+Sanjay8602@users.noreply.github.com> Date: Mon, 9 Oct 2023 23:12:56 +0530 Subject: [PATCH 07/37] Delete ivy_tests/test_ivy/test_frontends/test_numpy/test_fft/test_discrete_fourier_transform.py --- .../test_discrete_fourier_transform.py | 252 ------------------ 1 file changed, 252 deletions(-) delete mode 100644 ivy_tests/test_ivy/test_frontends/test_numpy/test_fft/test_discrete_fourier_transform.py diff --git a/ivy_tests/test_ivy/test_frontends/test_numpy/test_fft/test_discrete_fourier_transform.py b/ivy_tests/test_ivy/test_frontends/test_numpy/test_fft/test_discrete_fourier_transform.py deleted file mode 100644 index 09405ed65e3d7..0000000000000 --- a/ivy_tests/test_ivy/test_frontends/test_numpy/test_fft/test_discrete_fourier_transform.py +++ /dev/null @@ -1,252 +0,0 @@ -# global -from hypothesis import strategies as st - -# local -import ivy_tests.test_ivy.helpers as helpers -from ivy_tests.test_ivy.helpers import handle_frontend_test -from ivy_tests.test_ivy.test_functional.test_experimental.test_nn.test_layers import ( - x_and_ifft, - x_and_rfftn, -) - -# ivy_tests/test_ivy/test_functional/test_experimental/test_nn/test_layers.py - - -@handle_frontend_test( - fn_tree="numpy.fft.ifft", - dtype_and_x=x_and_ifft(), -) -def test_numpy_ifft(dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_device): - input_dtype, x, dim, norm, n = 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, - test_values=True, - a=x, - n=n, - axis=dim, - norm=norm, - ) - - -@handle_frontend_test( - fn_tree="numpy.fft.ifftshift", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), shape=(4,), array_api_dtypes=True - ), -) -def test_numpy_ifftshift( - dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_device -): - input_dtype, arr = 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, - test_values=True, - x=arr[0], - axes=None, - ) - - -@handle_frontend_test( - fn_tree="numpy.fft.fft", - dtype_input_axis=helpers.dtype_values_axis( - available_dtypes=helpers.get_dtypes("float_and_complex"), - shape=(2,), - min_axis=-1, - force_int_axis=True, - ), - norm=st.sampled_from(["backward", "ortho", "forward"]), - n=st.integers(min_value=2, max_value=10), -) -def test_numpy_fft( - dtype_input_axis, norm, n, backend_fw, frontend, test_flags, fn_tree, on_device -): - input_dtype, x, axis = dtype_input_axis - 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, - test_values=True, - a=x[0], - n=n, - axis=axis, - norm=norm, - ) - - -@handle_frontend_test( - fn_tree="numpy.fft.fftshift", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), shape=(4,), array_api_dtypes=True - ), -) -def fttshift(dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_device): - input_dtype, arr = 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, - test_values=True, - x=arr[0], - axes=None, - ) - - -@handle_frontend_test( - fn_tree="numpy.fft.rfft", - dtype_input_axis=helpers.dtype_values_axis( - available_dtypes=helpers.get_dtypes("float_and_complex"), - shape=(2,), - min_axis=-1, - force_int_axis=True, - ), - norm=st.sampled_from(["backward", "ortho", "forward"]), - n=st.integers(min_value=2, max_value=5), -) -def test_numpy_rfft( - dtype_input_axis, norm, n, backend_fw, frontend, test_flags, fn_tree, on_device -): - input_dtype, x, axis = dtype_input_axis - 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, - test_values=True, - a=x[0], - n=n, - axis=axis, - norm=norm, - ) - - -@handle_frontend_test( - fn_tree="numpy.fft.ihfft", - dtype_input_axis=helpers.dtype_values_axis( - available_dtypes=helpers.get_dtypes("float_and_complex"), - shape=(2,), - min_axis=-1, - force_int_axis=True, - ), - norm=st.sampled_from(["backward", "ortho", "forward"]), - n=st.integers(min_value=2, max_value=5), -) -def test_numpy_ihfft( - dtype_input_axis, norm, n, backend_fw, frontend, test_flags, fn_tree, on_device -): - input_dtype, x, axis = dtype_input_axis - 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, - test_values=True, - a=x[0], - n=n, - axis=axis, - norm=norm, - ) - - -@handle_frontend_test( - fn_tree="numpy.fft.fftfreq", - n=st.integers(min_value=10, max_value=100), - sample_rate=st.integers(min_value=1, max_value=10), -) -def test_numpy_fftfreq( - n, sample_rate, backend_fw, frontend, test_flags, fn_tree, on_device -): - d = 1 / sample_rate - helpers.test_frontend_function( - input_dtypes=[int], - frontend=frontend, - backend_to_test=backend_fw, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - test_values=True, - n=n, - d=d, - ) - - -@handle_frontend_test( - fn_tree="numpy.fft.rfftfreq", - n=st.integers(min_value=10, max_value=100), - sample_rate=st.integers(min_value=1, max_value=10), -) -def test_numpy_rfftfreq( - n, sample_rate, backend_fw, frontend, test_flags, fn_tree, on_device -): - d = 1 / sample_rate - helpers.test_frontend_function( - input_dtypes=[int], - frontend=frontend, - backend_to_test=backend_fw, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - test_values=True, - n=n, - d=d, - ) - - -@handle_frontend_test( - fn_tree="numpy.fft.ifftn", - dtype_and_x=x_and_ifft(), -) -def test_numpy_ifftn(dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_device): - input_dtype, x, dim, norm, n = 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, - test_values=True, - a=x, - s=None, - axes=None, - norm=norm, - ) - - -@handle_frontend_test( - fn_tree="numpy.fft.rfftn", - dtype_and_x=x_and_rfftn(), -) -def test_numpy_rfftn(dtype_and_x, frontend, test_flags, fn_tree, on_device): - dtype, x, s, axes, norm = dtype_and_x - helpers.test_frontend_function( - input_dtypes=dtype, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - test_values=True, - a=x, - s=s, - axes=axes, - norm=norm, - ) From 7337c7f069c0fa2cb036917de0ad022411616d0d Mon Sep 17 00:00:00 2001 From: Sanjay <121057369+Sanjay8602@users.noreply.github.com> Date: Mon, 9 Oct 2023 23:41:56 +0530 Subject: [PATCH 08/37] Delete duplicate.py --- duplicate.py | 43 ------------------------------------------- 1 file changed, 43 deletions(-) delete mode 100644 duplicate.py diff --git a/duplicate.py b/duplicate.py deleted file mode 100644 index e7ac5767d8b2c..0000000000000 --- a/duplicate.py +++ /dev/null @@ -1,43 +0,0 @@ -import importlib -import os -import glob - - -def get_all_functions_from_directory(root_dir, startswith="test"): - if not os.path.exists(root_dir): - print("Invalid directory") - exit(1) - functions_names = [] - for filename in glob.iglob(root_dir + "/**/*.py", recursive=True): - if len(filename) >= 2 and filename[:2] == "./": - filename = filename[2:] - filename = filename.replace(".py", "") - filename = filename.replace("/", ".") - module = importlib.import_module(filename) - module_functions_names = [ - obj for obj in dir(module) if obj.startswith(startswith) - ] - functions_names.extend(module_functions_names) - return functions_names - - -def check_duplicate(): - fn_test_core = get_all_functions_from_directory( - "ivy_tests/test_ivy/test_functional/test_core" - ) - fn_test_nn = get_all_functions_from_directory( - "ivy_tests/test_ivy/test_functional/test_nn" - ) - fn_test_experimental = get_all_functions_from_directory( - "ivy_tests/test_ivy/test_functional/test_experimental" - ) - fn_ivy_test = set(fn_test_core).union(set(fn_test_nn)) - common_list = fn_ivy_test.intersection(set(fn_test_experimental)) - return common_list - - -if __name__ == "__main__": - common_set = check_duplicate() - if len(common_set) != 0: - print("This function already exists in the functional API.") - exit(1) From 0061c011a86db23ac4ca5ee0ff19f1aad16cf3d3 Mon Sep 17 00:00:00 2001 From: ivy-branch Date: Wed, 4 Oct 2023 05:41:26 +0000 Subject: [PATCH 09/37] =?UTF-8?q?=F0=9F=A4=96=20Lint=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit feat: added the tensor operations --- determine_tests.py | 2 -- duplicate.py | 1 - .../array/experimental/manipulation.py | 10 ++++---- .../container/experimental/manipulation.py | 13 +++++----- .../backends/jax/experimental/manipulation.py | 11 ++++---- .../numpy/experimental/manipulation.py | 10 ++++---- .../tensorflow/experimental/manipulation.py | 11 ++++---- .../torch/experimental/manipulation.py | 11 ++++---- .../frontends/paddle/tensor/linalg.py | 5 ++-- .../indexing_slicing_joining_mutating_ops.py | 25 ++++++++----------- .../ivy/experimental/manipulation.py | 11 ++++---- .../test_discrete_fourier_transform.py | 4 +-- ...t_indexing_slicing_joining_mutating_ops.py | 11 ++++---- 13 files changed, 61 insertions(+), 64 deletions(-) diff --git a/determine_tests.py b/determine_tests.py index e712463123970..1df9628595625 100644 --- a/determine_tests.py +++ b/determine_tests.py @@ -11,8 +11,6 @@ MAX_TESTS = 10 - - def get_tests(_tests_file, _line): tests_file_line = set() if 0 <= _line < len(_tests_file): diff --git a/duplicate.py b/duplicate.py index 46483abad6242..e7ac5767d8b2c 100644 --- a/duplicate.py +++ b/duplicate.py @@ -3,7 +3,6 @@ import glob - def get_all_functions_from_directory(root_dir, startswith="test"): if not os.path.exists(root_dir): print("Invalid directory") diff --git a/ivy/data_classes/array/experimental/manipulation.py b/ivy/data_classes/array/experimental/manipulation.py index 75a8b0b566c0f..d93fc18239af4 100644 --- a/ivy/data_classes/array/experimental/manipulation.py +++ b/ivy/data_classes/array/experimental/manipulation.py @@ -1080,25 +1080,25 @@ def fill_diagonal( ivy.fill_diag also applies to this method with minimal changes. """ return ivy.fill_diagonal(self._data, v, wrap=wrap) - + def tensor_ops(tensor, operation_type, *args): - if operation_type == 'index': + if operation_type == "index": if len(args) != 1: raise ValueError index = args[0] return tensor[index] - elif operation_type == 'slice': + elif operation_type == "slice": if len(args) != 2: raise ValueError start, end = args return tensor[start:end] - elif operation_type == 'join': + elif operation_type == "join": if len(args) != 1: raise ValueError other_tensor = args[0] return ivy.cat((tensor, other_tensor), dim=0) - elif operation_type == 'mutate': + elif operation_type == "mutate": if len(args) != 1: raise ValueError factor = args[0] diff --git a/ivy/data_classes/container/experimental/manipulation.py b/ivy/data_classes/container/experimental/manipulation.py index 5e5a8964a7dff..73e2b1f7ea6a3 100644 --- a/ivy/data_classes/container/experimental/manipulation.py +++ b/ivy/data_classes/container/experimental/manipulation.py @@ -2996,29 +2996,30 @@ def fill_diagonal( v, wrap=wrap, ) - + + @staticmethod def tensor_ops(tensor, operation_type, *args): - if operation_type == 'index': + if operation_type == "index": if len(args) != 1: raise ValueError index = args[0] return tensor[index] - elif operation_type == 'slice': + elif operation_type == "slice": if len(args) != 2: raise ValueError start, end = args return tensor[start:end] - elif operation_type == 'join': + elif operation_type == "join": if len(args) != 1: raise ValueError other_tensor = args[0] return ivy.cat((tensor, other_tensor), dim=0) - elif operation_type == 'mutate': + elif operation_type == "mutate": if len(args) != 1: raise ValueError factor = args[0] tensor.mul_(factor) return tensor else: - raise ValueError \ No newline at end of file + raise ValueError diff --git a/ivy/functional/backends/jax/experimental/manipulation.py b/ivy/functional/backends/jax/experimental/manipulation.py index 193c725ede37d..29c9b12ddb93b 100644 --- a/ivy/functional/backends/jax/experimental/manipulation.py +++ b/ivy/functional/backends/jax/experimental/manipulation.py @@ -417,27 +417,28 @@ def fill_diagonal( a = jnp.reshape(a, shape) return a + def tensor_ops(tensor, operation_type, *args): - if operation_type == 'index': + if operation_type == "index": if len(args) != 1: raise ValueError index = args[0] return tensor[index] - elif operation_type == 'slice': + elif operation_type == "slice": if len(args) != 2: raise ValueError start, end = args return tensor[start:end] - elif operation_type == 'join': + elif operation_type == "join": if len(args) != 1: raise ValueError other_tensor = args[0] return ivy.cat((tensor, other_tensor), dim=0) - elif operation_type == 'mutate': + elif operation_type == "mutate": if len(args) != 1: raise ValueError factor = args[0] tensor.mul_(factor) return tensor else: - raise ValueError \ No newline at end of file + raise ValueError diff --git a/ivy/functional/backends/numpy/experimental/manipulation.py b/ivy/functional/backends/numpy/experimental/manipulation.py index cc55021f1fc65..ee0d10ecc4ca2 100644 --- a/ivy/functional/backends/numpy/experimental/manipulation.py +++ b/ivy/functional/backends/numpy/experimental/manipulation.py @@ -508,26 +508,26 @@ def fill_diagonal( def tensor_ops(tensor, operation_type, *args): - if operation_type == 'index': + if operation_type == "index": if len(args) != 1: raise ValueError index = args[0] return tensor[index] - elif operation_type == 'slice': + elif operation_type == "slice": if len(args) != 2: raise ValueError start, end = args return tensor[start:end] - elif operation_type == 'join': + elif operation_type == "join": if len(args) != 1: raise ValueError other_tensor = args[0] return ivy.cat((tensor, other_tensor), dim=0) - elif operation_type == 'mutate': + elif operation_type == "mutate": if len(args) != 1: raise ValueError factor = args[0] tensor.mul_(factor) return tensor else: - raise ValueError \ No newline at end of file + raise ValueError diff --git a/ivy/functional/backends/tensorflow/experimental/manipulation.py b/ivy/functional/backends/tensorflow/experimental/manipulation.py index 84f049e7527ba..217ec50dc19cd 100644 --- a/ivy/functional/backends/tensorflow/experimental/manipulation.py +++ b/ivy/functional/backends/tensorflow/experimental/manipulation.py @@ -368,27 +368,28 @@ def fill_diagonal( a = tf.reshape(a, shape) return a + def tensor_ops(tensor, operation_type, *args): - if operation_type == 'index': + if operation_type == "index": if len(args) != 1: raise ValueError index = args[0] return tensor[index] - elif operation_type == 'slice': + elif operation_type == "slice": if len(args) != 2: raise ValueError start, end = args return tensor[start:end] - elif operation_type == 'join': + elif operation_type == "join": if len(args) != 1: raise ValueError other_tensor = args[0] return ivy.cat((tensor, other_tensor), dim=0) - elif operation_type == 'mutate': + elif operation_type == "mutate": if len(args) != 1: raise ValueError factor = args[0] tensor.mul_(factor) return tensor else: - raise ValueError \ No newline at end of file + raise ValueError diff --git a/ivy/functional/backends/torch/experimental/manipulation.py b/ivy/functional/backends/torch/experimental/manipulation.py index e956c1f1cb062..dfd236dadda7a 100644 --- a/ivy/functional/backends/torch/experimental/manipulation.py +++ b/ivy/functional/backends/torch/experimental/manipulation.py @@ -396,27 +396,28 @@ def fill_diagonal( a = torch.reshape(a, shape) return a + def tensor_ops(tensor, operation_type, *args): - if operation_type == 'index': + if operation_type == "index": if len(args) != 1: raise ValueError index = args[0] return tensor[index] - elif operation_type == 'slice': + elif operation_type == "slice": if len(args) != 2: raise ValueError start, end = args return tensor[start:end] - elif operation_type == 'join': + elif operation_type == "join": if len(args) != 1: raise ValueError other_tensor = args[0] return ivy.cat((tensor, other_tensor), dim=0) - elif operation_type == 'mutate': + elif operation_type == "mutate": if len(args) != 1: raise ValueError factor = args[0] tensor.mul_(factor) return tensor else: - raise ValueError \ No newline at end of file + raise ValueError diff --git a/ivy/functional/frontends/paddle/tensor/linalg.py b/ivy/functional/frontends/paddle/tensor/linalg.py index 7a9c70472a7e7..26e374d3111ac 100644 --- a/ivy/functional/frontends/paddle/tensor/linalg.py +++ b/ivy/functional/frontends/paddle/tensor/linalg.py @@ -6,6 +6,7 @@ to_ivy_arrays_and_back, ) + @with_supported_dtypes( {"2.5.0 and below": ("float32", "float64", "int32", "int64")}, "paddle" ) @@ -183,6 +184,7 @@ def dist(x, y, p=2): ret = ivy.vector_norm(ivy.subtract(x, y), ord=p) return ivy.reshape(ret, (1,)) + @with_supported_dtypes({"2.4.1 and above": ("int64",)}, "paddle") @to_ivy_arrays_and_back def linear_algebra_histogram(d, num_bins): @@ -191,10 +193,9 @@ def linear_algebra_histogram(d, num_bins): bin_width = (max_value - min_value) / num_bins bin_edges = ivy.arange(min_value, max_value + bin_width, bin_width) - # Count the number of values in each bin. + # Count the number of values in each bin. bin_counts = ivy.zeros(num_bins) for value in d: bin_index = ivy.searchsorted(bin_edges, value) bin_counts[bin_index] += 1 return ivy.bin_counts - \ No newline at end of file diff --git a/ivy/functional/frontends/torch/indexing_slicing_joining_mutating_ops.py b/ivy/functional/frontends/torch/indexing_slicing_joining_mutating_ops.py index 46dfa60b54c42..c15e2d13439f0 100644 --- a/ivy/functional/frontends/torch/indexing_slicing_joining_mutating_ops.py +++ b/ivy/functional/frontends/torch/indexing_slicing_joining_mutating_ops.py @@ -367,34 +367,29 @@ def select(input, dim, index): slices[dim] = index return input[tuple(slices)] + @to_ivy_arrays_and_back -def tensor_ops(tensor, operation_type, *args): - if operation_type == 'index': +def tensor_ops(tensor, operations_type, *args): + if operations_type == "index": if len(args) != 1: raise ValueError index = args[0] return tensor[index] - elif operation_type == 'slice': + elif operations_type == "slice": if len(args) != 2: raise ValueError start, end = args return tensor[start:end] - elif operation_type == 'join': - if len(args) != 1: - raise ValueError - other_tensor = args[0] - return ivy.cat((tensor, other_tensor), dim=0) - elif operation_type == 'mutate': + elif operations_type == "mutate": if len(args) != 1: raise ValueError factor = args[0] tensor.mul_(factor) + elif operations_type == "join": + if len(args) != 1: + raise ValueError + other_tensor = args[0] + return ivy.cat((tensor, other_tensor), dim=0) return tensor else: raise ValueError - - - - - - \ No newline at end of file diff --git a/ivy/functional/ivy/experimental/manipulation.py b/ivy/functional/ivy/experimental/manipulation.py index 8f46fc201c2d8..5f28f2a629c49 100644 --- a/ivy/functional/ivy/experimental/manipulation.py +++ b/ivy/functional/ivy/experimental/manipulation.py @@ -2153,27 +2153,28 @@ def fill_diagonal( """ return ivy.current_backend(a).fill_diag(a, v, wrap=wrap) + def tensor_ops(tensor, operation_type, *args): - if operation_type == 'index': + if operation_type == "index": if len(args) != 1: raise ValueError index = args[0] return tensor[index] - elif operation_type == 'slice': + elif operation_type == "slice": if len(args) != 2: raise ValueError start, end = args return tensor[start:end] - elif operation_type == 'join': + elif operation_type == "join": if len(args) != 1: raise ValueError other_tensor = args[0] return ivy.cat((tensor, other_tensor), dim=0) - elif operation_type == 'mutate': + elif operation_type == "mutate": if len(args) != 1: raise ValueError factor = args[0] tensor.mul_(factor) return tensor else: - raise ValueError \ No newline at end of file + raise ValueError diff --git a/ivy_tests/test_ivy/test_frontends/test_numpy/test_fft/test_discrete_fourier_transform.py b/ivy_tests/test_ivy/test_frontends/test_numpy/test_fft/test_discrete_fourier_transform.py index fb6f723dc9682..09405ed65e3d7 100644 --- a/ivy_tests/test_ivy/test_frontends/test_numpy/test_fft/test_discrete_fourier_transform.py +++ b/ivy_tests/test_ivy/test_frontends/test_numpy/test_fft/test_discrete_fourier_transform.py @@ -92,9 +92,7 @@ def test_numpy_fft( available_dtypes=helpers.get_dtypes("float"), shape=(4,), array_api_dtypes=True ), ) -def fttshift( - dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_device -): +def fttshift(dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_device): input_dtype, arr = dtype_and_x helpers.test_frontend_function( input_dtypes=input_dtype, diff --git a/ivy_tests/test_ivy/test_frontends/test_torch/test_indexing_slicing_joining_mutating_ops.py b/ivy_tests/test_ivy/test_frontends/test_torch/test_indexing_slicing_joining_mutating_ops.py index 29ffde009976b..eb0e8a583fabc 100644 --- a/ivy_tests/test_ivy/test_frontends/test_torch/test_indexing_slicing_joining_mutating_ops.py +++ b/ivy_tests/test_ivy/test_frontends/test_torch/test_indexing_slicing_joining_mutating_ops.py @@ -1594,27 +1594,28 @@ def test_torch_select( index=idx, ) + def tensor_ops(tensor, operation_type, *args): - if operation_type == 'index': + if operation_type == "index": if len(args) != 1: raise ValueError index = args[0] return tensor[index] - elif operation_type == 'slice': + elif operation_type == "slice": if len(args) != 2: raise ValueError start, end = args return tensor[start:end] - elif operation_type == 'join': + elif operation_type == "join": if len(args) != 1: raise ValueError other_tensor = args[0] return tensor.cat((tensor, other_tensor), dim=0) - elif operation_type == 'mutate': + elif operation_type == "mutate": if len(args) != 1: raise ValueError factor = args[0] tensor.mul_(factor) return tensor else: - raise ValueError \ No newline at end of file + raise ValueError From f280b52fe42b4aafc38ef83b82ca70172196d05f Mon Sep 17 00:00:00 2001 From: Sanjay <121057369+Sanjay8602@users.noreply.github.com> Date: Mon, 16 Oct 2023 15:23:15 +0530 Subject: [PATCH 10/37] Delete ivy_tests/test_ivy/test_frontends/test_numpy/test_fft/test_discrete_fourier_transform.py --- .../test_discrete_fourier_transform.py | 252 ------------------ 1 file changed, 252 deletions(-) delete mode 100644 ivy_tests/test_ivy/test_frontends/test_numpy/test_fft/test_discrete_fourier_transform.py diff --git a/ivy_tests/test_ivy/test_frontends/test_numpy/test_fft/test_discrete_fourier_transform.py b/ivy_tests/test_ivy/test_frontends/test_numpy/test_fft/test_discrete_fourier_transform.py deleted file mode 100644 index 09405ed65e3d7..0000000000000 --- a/ivy_tests/test_ivy/test_frontends/test_numpy/test_fft/test_discrete_fourier_transform.py +++ /dev/null @@ -1,252 +0,0 @@ -# global -from hypothesis import strategies as st - -# local -import ivy_tests.test_ivy.helpers as helpers -from ivy_tests.test_ivy.helpers import handle_frontend_test -from ivy_tests.test_ivy.test_functional.test_experimental.test_nn.test_layers import ( - x_and_ifft, - x_and_rfftn, -) - -# ivy_tests/test_ivy/test_functional/test_experimental/test_nn/test_layers.py - - -@handle_frontend_test( - fn_tree="numpy.fft.ifft", - dtype_and_x=x_and_ifft(), -) -def test_numpy_ifft(dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_device): - input_dtype, x, dim, norm, n = 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, - test_values=True, - a=x, - n=n, - axis=dim, - norm=norm, - ) - - -@handle_frontend_test( - fn_tree="numpy.fft.ifftshift", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), shape=(4,), array_api_dtypes=True - ), -) -def test_numpy_ifftshift( - dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_device -): - input_dtype, arr = 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, - test_values=True, - x=arr[0], - axes=None, - ) - - -@handle_frontend_test( - fn_tree="numpy.fft.fft", - dtype_input_axis=helpers.dtype_values_axis( - available_dtypes=helpers.get_dtypes("float_and_complex"), - shape=(2,), - min_axis=-1, - force_int_axis=True, - ), - norm=st.sampled_from(["backward", "ortho", "forward"]), - n=st.integers(min_value=2, max_value=10), -) -def test_numpy_fft( - dtype_input_axis, norm, n, backend_fw, frontend, test_flags, fn_tree, on_device -): - input_dtype, x, axis = dtype_input_axis - 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, - test_values=True, - a=x[0], - n=n, - axis=axis, - norm=norm, - ) - - -@handle_frontend_test( - fn_tree="numpy.fft.fftshift", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), shape=(4,), array_api_dtypes=True - ), -) -def fttshift(dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_device): - input_dtype, arr = 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, - test_values=True, - x=arr[0], - axes=None, - ) - - -@handle_frontend_test( - fn_tree="numpy.fft.rfft", - dtype_input_axis=helpers.dtype_values_axis( - available_dtypes=helpers.get_dtypes("float_and_complex"), - shape=(2,), - min_axis=-1, - force_int_axis=True, - ), - norm=st.sampled_from(["backward", "ortho", "forward"]), - n=st.integers(min_value=2, max_value=5), -) -def test_numpy_rfft( - dtype_input_axis, norm, n, backend_fw, frontend, test_flags, fn_tree, on_device -): - input_dtype, x, axis = dtype_input_axis - 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, - test_values=True, - a=x[0], - n=n, - axis=axis, - norm=norm, - ) - - -@handle_frontend_test( - fn_tree="numpy.fft.ihfft", - dtype_input_axis=helpers.dtype_values_axis( - available_dtypes=helpers.get_dtypes("float_and_complex"), - shape=(2,), - min_axis=-1, - force_int_axis=True, - ), - norm=st.sampled_from(["backward", "ortho", "forward"]), - n=st.integers(min_value=2, max_value=5), -) -def test_numpy_ihfft( - dtype_input_axis, norm, n, backend_fw, frontend, test_flags, fn_tree, on_device -): - input_dtype, x, axis = dtype_input_axis - 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, - test_values=True, - a=x[0], - n=n, - axis=axis, - norm=norm, - ) - - -@handle_frontend_test( - fn_tree="numpy.fft.fftfreq", - n=st.integers(min_value=10, max_value=100), - sample_rate=st.integers(min_value=1, max_value=10), -) -def test_numpy_fftfreq( - n, sample_rate, backend_fw, frontend, test_flags, fn_tree, on_device -): - d = 1 / sample_rate - helpers.test_frontend_function( - input_dtypes=[int], - frontend=frontend, - backend_to_test=backend_fw, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - test_values=True, - n=n, - d=d, - ) - - -@handle_frontend_test( - fn_tree="numpy.fft.rfftfreq", - n=st.integers(min_value=10, max_value=100), - sample_rate=st.integers(min_value=1, max_value=10), -) -def test_numpy_rfftfreq( - n, sample_rate, backend_fw, frontend, test_flags, fn_tree, on_device -): - d = 1 / sample_rate - helpers.test_frontend_function( - input_dtypes=[int], - frontend=frontend, - backend_to_test=backend_fw, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - test_values=True, - n=n, - d=d, - ) - - -@handle_frontend_test( - fn_tree="numpy.fft.ifftn", - dtype_and_x=x_and_ifft(), -) -def test_numpy_ifftn(dtype_and_x, backend_fw, frontend, test_flags, fn_tree, on_device): - input_dtype, x, dim, norm, n = 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, - test_values=True, - a=x, - s=None, - axes=None, - norm=norm, - ) - - -@handle_frontend_test( - fn_tree="numpy.fft.rfftn", - dtype_and_x=x_and_rfftn(), -) -def test_numpy_rfftn(dtype_and_x, frontend, test_flags, fn_tree, on_device): - dtype, x, s, axes, norm = dtype_and_x - helpers.test_frontend_function( - input_dtypes=dtype, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - test_values=True, - a=x, - s=s, - axes=axes, - norm=norm, - ) From 7bae7a2ca647a90e1a2cc337a84cfbc9144d0a72 Mon Sep 17 00:00:00 2001 From: Sanjay <121057369+Sanjay8602@users.noreply.github.com> Date: Mon, 16 Oct 2023 15:24:06 +0530 Subject: [PATCH 11/37] Delete ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py --- .../test_nn/test_functional/test_norm.py | 49 ------------------- 1 file changed, 49 deletions(-) delete mode 100644 ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py deleted file mode 100644 index 67bf5a6a4ff6e..0000000000000 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py +++ /dev/null @@ -1,49 +0,0 @@ -# global -from typing import Literal -from hypothesis import strategies as st - -# local -import ivy_tests.test_ivy.helpers as helpers -from ivy_tests.test_ivy.helpers.testing_helpers import handle_frontend_test - -from ivy_tests.test_ivy.test_functional.test_nn.test_norms import ( - _generate_data_layer_norm, -) - -from ivy import ivy - - -# layer_norm -@handle_frontend_test( - fn_tree="paddle.nn.functional.layer_norm", - values_tuple=_generate_data_layer_norm( - available_dtypes=helpers.get_dtypes("float"), - ), - eps=st.floats(min_value=0.01, max_value=0.1), -) -def test_paddle_layer_norm( - *, - values_tuple, - normalized_shape, - eps, - test_flags, - frontend: Literal['paddle'], - on_device, - fn_tree, -): - (dtype, x, normalized_shape, scale, offset) = values_tuple - helpers.test_frontend_function( - input_dtypes=dtype, - frontend=frontend, - test_flags=test_flags, - on_device=on_device, - fn_tree=fn_tree, - x=x[0], - normalized_shape=normalized_shape, - weight=scale[0], - bias=offset[0], - epsilon=eps, - ) - -def batch_norm(x, gamma, beta, moving_mean, moving_var, epsilon=1e-5): - return ivy.batch_norm(x, gamma, beta, moving_mean, moving_var, epsilon) From c3d51e1ce1a5a31e6e5d2d1f8af7ff21d798ea95 Mon Sep 17 00:00:00 2001 From: Sanjay <121057369+Sanjay8602@users.noreply.github.com> Date: Mon, 16 Oct 2023 15:24:37 +0530 Subject: [PATCH 12/37] Delete determine_tests.py --- determine_tests.py | 193 --------------------------------------------- 1 file changed, 193 deletions(-) delete mode 100644 determine_tests.py diff --git a/determine_tests.py b/determine_tests.py deleted file mode 100644 index 1df9628595625..0000000000000 --- a/determine_tests.py +++ /dev/null @@ -1,193 +0,0 @@ -import pickle # noqa -import subprocess - -from pydriller import Repository -import os # noqa -import bz2 -import pickle as cPickle -import sys -from run_tests_CLI.get_all_tests import get_all_tests - -MAX_TESTS = 10 - - -def get_tests(_tests_file, _line): - tests_file_line = set() - if 0 <= _line < len(_tests_file): - tests_file_line = _tests_file[_line] - return set() if len(tests_file_line) >= MAX_TESTS else tests_file_line - - -def determine_tests_line(_tests_file, _line, _tests_to_run): - tests_file_line = get_tests(_tests_file, _line) - tests_file_prev = get_tests(_tests_file, _line - 1) - tests_file_next = get_tests(_tests_file, _line + 1) - _tests_to_run.update(tests_file_line) - _tests_to_run.update(tests_file_prev) - _tests_to_run.update(tests_file_next) - return _tests_to_run - - -def main(): - tests = bz2.BZ2File("tests.pbz2", "rb") - tests = cPickle.load(tests) - ref_commit_hash = tests["commit"] - print("Reference Commit: ", ref_commit_hash) - tests_to_run = set() - for commit in Repository(".", single=ref_commit_hash).traverse_commits(): - ref_commit = commit._c_object - break - - for commit in Repository(".", order="reverse").traverse_commits(): - tests["commit"] = commit.hash - diff_index = ref_commit.diff(commit._c_object, create_patch=True) - modified_files = commit._parse_diff(diff_index) - for file in modified_files: - try: - file_name = file.new_path + ",cover" - except: # noqa - continue - if file_name not in tests.keys(): - continue - tests_file = tests[file_name] - change = file.diff_parsed - added = set([x - 1 for (x, _) in change["added"]]) - deleted = set([x - 1 for (x, _) in change["deleted"]]) - updated = added.intersection(deleted) - added = added.difference(updated) - deleted = deleted.difference(updated) - # Now Update the Mapping and compute the tests to run - for line in deleted: - tests_to_run = determine_tests_line(tests_file, line, tests_to_run) - for line in sorted(deleted, reverse=True): - if line < len(tests_file): - del tests_file[line] - for line in added: - top = -1 - bottom = -1 - if 0 <= line - 1 < len(tests_file): - top = tests_file[line - 1] - if 0 <= line + 1 < len(tests_file): - bottom = tests_file[line + 1] - tests_line = set() - if top != -1 and bottom != -1: - tests_line = top.intersection(bottom) - elif top != -1: - tests_line = top - elif bottom != -1: - tests_line = bottom - tests_file.insert(line, tests_line) - tests[file_name] = tests_file - # Now Compute the Tests to Run - for line in updated: - tests_to_run = determine_tests_line(tests_file, line, tests_to_run) - for line in added: - tests_to_run = determine_tests_line(tests_file, line, tests_to_run) - break - - if len(sys.argv) >= 2 and sys.argv[1] == "1": - print("Checking for any new tests added!") - new_tests = get_all_tests() - print("Done!") - # Check for any new tests present - old_tests = tests["index_mapping"] - added_tests = set(new_tests) - set(old_tests) - removed_tests = set(old_tests) - set(new_tests) - with open("tests_to_remove", "w") as f: - for test in removed_tests: - f.write(test + "\n") - added_tests = list(added_tests) - # if it is a PR, we must check that the tests added were in the files_changes - if len(sys.argv) >= 3 and sys.argv[2] == "pr": - relevant_added_tests = [] - subprocess.run( - ["git", "remote", "add", "upstream", "https://github.com/unifyai/ivy"] - ) - subprocess.run(["git", "fetch", "upstream"]) - lca_sha = subprocess.check_output( - ["git", "merge-base", "HEAD", "upstream/master"] - ) - lca_hash = lca_sha.decode().strip() - for commit in Repository(".", single=lca_hash).traverse_commits(): - lca_commit = commit._c_object - break - for commit in Repository(".", order="reverse").traverse_commits(): - diff_index = lca_commit.diff(commit._c_object, create_patch=True) - modified_files = commit._parse_diff(diff_index) - break - for test in added_tests: - for file in modified_files: - if file.new_path.strip() in test: - relevant_added_tests.append(test) - break - added_tests = relevant_added_tests - else: - if len(added_tests) > 50: - added_tests = added_tests[:50] - # Add these new_tests in the Mapping - old_num_tests = len(old_tests) - tests["index_mapping"] += added_tests - new_tests = tests["index_mapping"] - num_tests = len(new_tests) - for i in range(old_num_tests, num_tests): - tests["tests_mapping"][new_tests[i]] = i - directories = ( - [x[0] for x in os.walk("ivy")] - + [x[0] for x in os.walk("ivy_tests/test_ivy")] - + ["ivy_tests"] - ) - directories_filtered = [ - x - for x in directories - if not (x.endswith("__pycache__") or "hypothesis" in x) - ] - directories = set(directories_filtered) - for test_backend in new_tests[old_num_tests:num_tests]: - tests_to_run.add(tests["tests_mapping"][test_backend]) - if len(sys.argv) < 3: - print("Computing Coverage:", test_backend) - test_name, backend = test_backend.split(",") - command = ( - f'docker run -v "$(pwd)":/ivy unifyai/ivy:latest /bin/bash -c "coverage run --source=ivy,' # noqa - f"ivy_tests -m pytest {test_name} --backend {backend} --disable-warnings > coverage_output;coverage " # noqa - f'annotate > coverage_output" ' - ) - os.system(command) - for directory in directories: - for file_name in os.listdir(directory): - if file_name.endswith("cover"): - file_name = directory + "/" + file_name - if file_name not in tests: - tests[file_name] = [] - with open(file_name) as f: - for line in f: - tests[file_name].append(set()) - with open(file_name) as f: - i = 0 - for line in f: - if i >= len(tests[file_name]): - tests[file_name].append(set()) - if line[0] == ">": - tests[file_name][i].add( - tests["tests_mapping"][test_backend] - ) - i += 1 - os.system("find . -name \\*cover -type f -delete") - - with bz2.BZ2File("tests.pbz2", "w") as f: - cPickle.dump(tests, f) - - print("----- Determined Tests -----") - print(len(tests_to_run)) - for test_index in tests_to_run: - print(tests["index_mapping"][test_index]) - print("----------------------------") - - with open("tests_to_run", "w") as f: - for test_index in tests_to_run: - test = tests["index_mapping"][test_index] - f.write(test + "\n") - - -if __name__ == "__main__": - main() From 857e841bc8018a4602336bb862c4d8688057b520 Mon Sep 17 00:00:00 2001 From: Sanjay <121057369+Sanjay8602@users.noreply.github.com> Date: Mon, 16 Oct 2023 15:25:40 +0530 Subject: [PATCH 13/37] Delete ivy/functional/frontends/paddle/tensor/linalg.py --- .../frontends/paddle/tensor/linalg.py | 201 ------------------ 1 file changed, 201 deletions(-) delete mode 100644 ivy/functional/frontends/paddle/tensor/linalg.py diff --git a/ivy/functional/frontends/paddle/tensor/linalg.py b/ivy/functional/frontends/paddle/tensor/linalg.py deleted file mode 100644 index 26e374d3111ac..0000000000000 --- a/ivy/functional/frontends/paddle/tensor/linalg.py +++ /dev/null @@ -1,201 +0,0 @@ -# global -import ivy -from ivy.func_wrapper import with_unsupported_dtypes, with_supported_dtypes -from ivy.functional.frontends.paddle import promote_types_of_paddle_inputs -from ivy.functional.frontends.paddle.func_wrapper import ( - to_ivy_arrays_and_back, -) - - -@with_supported_dtypes( - {"2.5.0 and below": ("float32", "float64", "int32", "int64")}, "paddle" -) -@to_ivy_arrays_and_back -def cross(x, y, /, *, axis=9, name=None): - x, y = promote_types_of_paddle_inputs(x, y) - return ivy.cross(x, y, axis=axis) - - -# matmul -@with_unsupported_dtypes({"2.5.0 and below": ("float16", "bfloat16")}, "paddle") -@to_ivy_arrays_and_back -def matmul(x, y, transpose_x=False, transpose_y=False, name=None): - x, y = promote_types_of_paddle_inputs(x, y) - return ivy.matmul(x, y, transpose_a=transpose_x, transpose_b=transpose_y) - - -# norm -@with_supported_dtypes({"2.5.0 and below": ("float32", "float64")}, "paddle") -@to_ivy_arrays_and_back -def norm(x, p="fro", axis=None, keepdim=False, name=None): - if axis is None and p is not None: - if p == "fro": - p = 2 - ret = ivy.vector_norm(x.flatten(), ord=p, axis=-1) - if keepdim: - ret = ret.reshape([1] * len(x.shape)) - if len(ret.shape) == 0: - return ivy.array([ret]) - return ret - - if isinstance(axis, tuple): - axis = list(axis) - if isinstance(axis, list) and len(axis) == 1: - axis = axis[0] - - if isinstance(axis, int): - if p == "fro": - p = 2 - if p in [0, 1, 2, ivy.inf, -ivy.inf]: - ret = ivy.vector_norm(x, ord=p, axis=axis, keepdims=keepdim) - elif isinstance(p, (int, float)): - ret = ivy.pow( - ivy.sum(ivy.pow(ivy.abs(x), p), axis=axis, keepdims=keepdim), - float(1.0 / p), - ) - - elif isinstance(axis, list) and len(axis) == 2: - if p == 0: - raise ValueError - elif p == 1: - ret = ivy.sum(ivy.abs(x), axis=axis, keepdims=keepdim) - elif p == 2 or p == "fro": - ret = ivy.matrix_norm(x, ord="fro", axis=axis, keepdims=keepdim) - elif p == ivy.inf: - ret = ivy.max(ivy.abs(x), axis=axis, keepdims=keepdim) - elif p == -ivy.inf: - ret = ivy.min(ivy.abs(x), axis=axis, keepdims=keepdim) - elif isinstance(p, (int, float)) and p > 0: - ret = ivy.pow( - ivy.sum(ivy.pow(ivy.abs(x), p), axis=axis, keepdims=keepdim), - float(1.0 / p), - ) - else: - raise ValueError - - else: - raise ValueError - - if len(ret.shape) == 0: - ret = ivy.array( - [ret] - ) # this is done so as to match shape of output from paddle - return ret - - -# eig -@to_ivy_arrays_and_back -def eig(x, name=None): - return ivy.eig(x) - - -# eigvals -@to_ivy_arrays_and_back -def eigvals(x, name=None): - return ivy.eigvals(x) - - -# eigvalsh -@to_ivy_arrays_and_back -def eigvalsh(x, UPLO="L", name=None): - return ivy.eigvalsh(x, UPLO=UPLO) - - -# eigh -@to_ivy_arrays_and_back -def eigh(x, UPLO="L", name=None): - return ivy.eigh(x, UPLO=UPLO) - - -# pinv -@with_unsupported_dtypes({"2.5.0 and below": ("float16", "bfloat16")}, "paddle") -@to_ivy_arrays_and_back -def pinv(x, rcond=1e-15, hermitian=False, name=None): - # TODO: Add hermitian functionality - return ivy.pinv(x, rtol=rcond) - - -# solve -@with_unsupported_dtypes({"2.5.0 and below": ("float16", "bfloat16")}, "paddle") -@to_ivy_arrays_and_back -def solve(x1, x2, name=None): - return ivy.solve(x1, x2) - - -# cholesky -@with_supported_dtypes({"2.5.0 and below": ("float32", "float64")}, "paddle") -@to_ivy_arrays_and_back -def cholesky(x, /, *, upper=False, name=None): - return ivy.cholesky(x, upper=upper) - - -# bmm -@with_unsupported_dtypes({"2.5.0 and below": ("float16", "bfloat16")}, "paddle") -@to_ivy_arrays_and_back -def bmm(x, y, transpose_x=False, transpose_y=False, name=None): - if len(ivy.shape(x)) != 3 or len(ivy.shape(y)) != 3: - raise RuntimeError("input must be 3D matrices") - x, y = promote_types_of_paddle_inputs(x, y) - return ivy.matmul(x, y, transpose_a=transpose_x, transpose_b=transpose_y) - - -# matrix_power -@with_unsupported_dtypes({"2.5.0 and below": ("float16", "bfloat16")}, "paddle") -@to_ivy_arrays_and_back -def matrix_power(x, n, name=None): - return ivy.matrix_power(x, n) - - -# cond -@with_supported_dtypes({"2.5.0 and below": ("float32", "float64")}, "paddle") -@to_ivy_arrays_and_back -def cond(x, p=None, name=None): - ret = ivy.cond(x, p=p, out=name) - if ret.shape == (): - ret = ret.reshape((1,)) - return ret - - -# dot -@with_supported_dtypes({"2.5.0 and below": ("float32", "float64")}, "paddle") -@to_ivy_arrays_and_back -def dot(x, y, name=None): - x, y = promote_types_of_paddle_inputs(x, y) - out = ivy.multiply(x, y) - return ivy.sum(out, axis=ivy.get_num_dims(x) - 1, keepdims=False) - - -# transpose -@with_unsupported_dtypes({"2.5.0 and below": ("uint8", "int8", "int16")}, "paddle") -@to_ivy_arrays_and_back -def transpose(x, perm, name=None): - return ivy.permute_dims(x, axes=perm) - - -@with_supported_dtypes({"2.4.1 and above": ("int64",)}, "paddle") -@to_ivy_arrays_and_back -def bincount(x, weights=None, minlength=0, name=None): - return ivy.bincount(x, weights=weights, minlength=minlength) - - -@with_supported_dtypes({"2.4.1 and above": ("float64", "float32")}, "paddle") -@to_ivy_arrays_and_back -def dist(x, y, p=2): - ret = ivy.vector_norm(ivy.subtract(x, y), ord=p) - return ivy.reshape(ret, (1,)) - - -@with_supported_dtypes({"2.4.1 and above": ("int64",)}, "paddle") -@to_ivy_arrays_and_back -def linear_algebra_histogram(d, num_bins): - min_value = ivy.min(d) - max_value = ivy.max(d) - bin_width = (max_value - min_value) / num_bins - bin_edges = ivy.arange(min_value, max_value + bin_width, bin_width) - - # Count the number of values in each bin. - bin_counts = ivy.zeros(num_bins) - for value in d: - bin_index = ivy.searchsorted(bin_edges, value) - bin_counts[bin_index] += 1 - return ivy.bin_counts From 6b88b046b54ffbd9b7ac0c36f605644694c17ff0 Mon Sep 17 00:00:00 2001 From: Sanjay <121057369+Sanjay8602@users.noreply.github.com> Date: Mon, 16 Oct 2023 15:27:27 +0530 Subject: [PATCH 14/37] Delete ivy/functional/frontends/paddle/nn/functional/norm.py --- .../frontends/paddle/nn/functional/norm.py | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 ivy/functional/frontends/paddle/nn/functional/norm.py diff --git a/ivy/functional/frontends/paddle/nn/functional/norm.py b/ivy/functional/frontends/paddle/nn/functional/norm.py deleted file mode 100644 index 8e9decfe31b4b..0000000000000 --- a/ivy/functional/frontends/paddle/nn/functional/norm.py +++ /dev/null @@ -1,16 +0,0 @@ -# local -import ivy -from ivy.func_wrapper import with_supported_dtypes -from ivy.functional.frontends.paddle.func_wrapper import to_ivy_arrays_and_back - - -@to_ivy_arrays_and_back -@with_supported_dtypes({"2.5.0 and below": ("float32", "float64")}, "paddle") -def layer_norm(x, normalized_shape, weight=None, bias=None, epsilon=1e-05, name=None): - return ivy.layer_norm(x, normalized_shape, weight, bias, epsilon) - -@to_ivy_arrays_and_back -@with_supported_dtypes({"2.5.0 and below": ("float32", "float64")}, "paddle") -def batch_norm(x, gamma, beta, moving_mean, moving_var, epsilon=1e-5): - return ivy.batch_norm(x, gamma, beta, moving_mean, moving_var, epsilon) - \ No newline at end of file From 03db265bb4fd0d239df8ed33d039c2f8178b87e3 Mon Sep 17 00:00:00 2001 From: Sanjay Suthar Date: Mon, 16 Oct 2023 16:01:12 +0530 Subject: [PATCH 15/37] Feat: Added NN Norm Functions to PaddlePaddle Frontend - [https://github.com/unifyai/ivy/issues/23300] --- ivy/functional/frontends/paddle/nn/functional/norm.py | 1 - .../test_paddle/test_nn/test_functional/test_norm.py | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/ivy/functional/frontends/paddle/nn/functional/norm.py b/ivy/functional/frontends/paddle/nn/functional/norm.py index 8e9decfe31b4b..affacbb918161 100644 --- a/ivy/functional/frontends/paddle/nn/functional/norm.py +++ b/ivy/functional/frontends/paddle/nn/functional/norm.py @@ -13,4 +13,3 @@ def layer_norm(x, normalized_shape, weight=None, bias=None, epsilon=1e-05, name= @with_supported_dtypes({"2.5.0 and below": ("float32", "float64")}, "paddle") def batch_norm(x, gamma, beta, moving_mean, moving_var, epsilon=1e-5): return ivy.batch_norm(x, gamma, beta, moving_mean, moving_var, epsilon) - \ No newline at end of file diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py index 67bf5a6a4ff6e..3d489bd898008 100644 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py +++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py @@ -45,5 +45,7 @@ def test_paddle_layer_norm( epsilon=eps, ) -def batch_norm(x, gamma, beta, moving_mean, moving_var, epsilon=1e-5): + +def batch_normalisation(x, gamma, beta, moving_mean, moving_var, epsilon=1e-5): return ivy.batch_norm(x, gamma, beta, moving_mean, moving_var, epsilon) + From 15b00d8c61528550b46d52c39a05531a3aa5be29 Mon Sep 17 00:00:00 2001 From: Sanjay Suthar Date: Mon, 16 Oct 2023 16:28:20 +0530 Subject: [PATCH 16/37] Added the batch normalisation in NN Paddle test --- ivy/functional/frontends/paddle/nn/functional/norm.py | 4 ++-- .../test_paddle/test_nn/test_functional/test_norm.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ivy/functional/frontends/paddle/nn/functional/norm.py b/ivy/functional/frontends/paddle/nn/functional/norm.py index affacbb918161..046a339217f4e 100644 --- a/ivy/functional/frontends/paddle/nn/functional/norm.py +++ b/ivy/functional/frontends/paddle/nn/functional/norm.py @@ -11,5 +11,5 @@ def layer_norm(x, normalized_shape, weight=None, bias=None, epsilon=1e-05, name= @to_ivy_arrays_and_back @with_supported_dtypes({"2.5.0 and below": ("float32", "float64")}, "paddle") -def batch_norm(x, gamma, beta, moving_mean, moving_var, epsilon=1e-5): - return ivy.batch_norm(x, gamma, beta, moving_mean, moving_var, epsilon) +def batch_normalisation(x, gamma, beta, moving_mean, moving_var, epsilon=1e-5): + return ivy.batch_normalisation(x, gamma, beta, moving_mean, moving_var, epsilon) diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py index 3d489bd898008..1578d27aef26d 100644 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py +++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py @@ -47,5 +47,5 @@ def test_paddle_layer_norm( def batch_normalisation(x, gamma, beta, moving_mean, moving_var, epsilon=1e-5): - return ivy.batch_norm(x, gamma, beta, moving_mean, moving_var, epsilon) + return ivy.batch_normalisation(x, gamma, beta, moving_mean, moving_var, epsilon) From 573bdd06a1da690a6f3175bf5659fbe0a4e5d8f2 Mon Sep 17 00:00:00 2001 From: Sanjay Suthar Date: Wed, 18 Oct 2023 00:28:22 +0530 Subject: [PATCH 17/37] batch normalization --- .../test_nn/test_functional/test_norm.py | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py index 1578d27aef26d..dd63ef769b0bb 100644 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py +++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py @@ -10,7 +10,7 @@ _generate_data_layer_norm, ) -from ivy import ivy +from ivy_tests.test_ivy.test_functional.test_nn.test_norms import (_generate_data_batch_norm,) # layer_norm @@ -44,8 +44,27 @@ def test_paddle_layer_norm( bias=offset[0], epsilon=eps, ) - - -def batch_normalisation(x, gamma, beta, moving_mean, moving_var, epsilon=1e-5): - return ivy.batch_normalisation(x, gamma, beta, moving_mean, moving_var, epsilon) - +@handle_frontend_test( + fn_tree="paddle.nn.functional.batch_norm", + values_tuple=_generate_data_batch_norm( + available_dtypes=helpers.get_dtypes("float"), + ), + eps=st.floats(min_value=0.01, max_value=0.1), +) +def test_batch_norm( + *, values_tuple, test_flags, backend_fw, fn_name, on_device +): + (dtype, x, gamma, beta, moving_mean, moving_var, epsilon) = values_tuple + helpers.test_function( + input_dtypes=dtype, + test_flags=test_flags, + backend_to_test=backend_fw, + fn_name=fn_name, + on_device=on_device, + x=x, + gamma=gamma, + beta=beta, + moving_mean=moving_mean, + moving_var=moving_var, + epsilon=epsilon, + ) \ No newline at end of file From 81b17d1ad1bae4fb13e6916c8737e63956be8d12 Mon Sep 17 00:00:00 2001 From: Sanjay Suthar Date: Wed, 18 Oct 2023 00:35:58 +0530 Subject: [PATCH 18/37] batch norm --- .../frontends/paddle/nn/functional/norm.py | 6 +- .../test_functional/test_nn/test_norms.py | 64 +++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/ivy/functional/frontends/paddle/nn/functional/norm.py b/ivy/functional/frontends/paddle/nn/functional/norm.py index 046a339217f4e..1cab45ee61a42 100644 --- a/ivy/functional/frontends/paddle/nn/functional/norm.py +++ b/ivy/functional/frontends/paddle/nn/functional/norm.py @@ -9,7 +9,9 @@ def layer_norm(x, normalized_shape, weight=None, bias=None, epsilon=1e-05, name=None): return ivy.layer_norm(x, normalized_shape, weight, bias, epsilon) + + @to_ivy_arrays_and_back @with_supported_dtypes({"2.5.0 and below": ("float32", "float64")}, "paddle") -def batch_normalisation(x, gamma, beta, moving_mean, moving_var, epsilon=1e-5): - return ivy.batch_normalisation(x, gamma, beta, moving_mean, moving_var, epsilon) +def batch_norm(x, gamma, beta, moving_mean, moving_var, epsilon=1e-5): + return ivy.batch_norm(x, gamma, beta, moving_mean, moving_var, epsilon) \ No newline at end of file diff --git a/ivy_tests/test_ivy/test_functional/test_nn/test_norms.py b/ivy_tests/test_ivy/test_functional/test_nn/test_norms.py index ba2eb62e73eb8..b939646ee58b1 100644 --- a/ivy_tests/test_ivy/test_functional/test_nn/test_norms.py +++ b/ivy_tests/test_ivy/test_functional/test_nn/test_norms.py @@ -112,3 +112,67 @@ def test_layer_norm( offset=offset[0], new_std=new_std, ) + +# created function for batch normalization +def _generate_data_batch_norm( + draw, + *, + available_dtypes, + min_value, + max_value, + min_gamma=0.1, + max_gamma=2.0, + min_beta=-1.0, + max_beta=1.0, + min_moving_mean=0.0, + max_moving_mean=1.0, + min_moving_var=0.1, + max_moving_var=1.0, + epsilon=1e-5, + min_num_dims=1, + max_num_dims=5, + ret_shape=True, +): + results = draw( + helpers.dtype_values( + available_dtypes=available_dtypes, + min_value=min_value, + max_value=max_value, + min_num_dims=min_num_dims, + max_num_dims=max_num_dims, + ret_shape=ret_shape, + ) + ) + + dtype, x, shape = results + + gamma = draw(st.floats(min_value=min_gamma, max_value=max_gamma)) + beta = draw(st.floats(min_value=min_beta, max_value=max_beta)) + moving_mean = draw(st.floats(min_value=min_moving_mean, max_value=max_moving_mean)) + moving_var = draw(st.floats(min_value=min_moving_var, max_value=max_moving_var)) + + return dtype, x, gamma, beta, moving_mean, moving_var, epsilon + +@handle_test( + fn_tree="functional.batch_norm", + values_tuple=_generate_data_batch_norm( + available_dtypes=helpers.get_dtypes("float"), + ), +) +def test_batch_norm( + *, values_tuple, test_flags, backend_fw, fn_name, on_device +): + (dtype, x, gamma, beta, moving_mean, moving_var, epsilon) = values_tuple + helpers.test_function( + input_dtypes=dtype, + test_flags=test_flags, + backend_to_test=backend_fw, + fn_name=fn_name, + on_device=on_device, + x=x, + gamma=gamma, + beta=beta, + moving_mean=moving_mean, + moving_var=moving_var, + epsilon=epsilon, + ) From b544e8f92e17b02dedcabc5a47efb8f62d6df822 Mon Sep 17 00:00:00 2001 From: ivy-branch Date: Tue, 17 Oct 2023 19:11:01 +0000 Subject: [PATCH 19/37] =?UTF-8?q?=F0=9F=A4=96=20Lint=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../frontends/paddle/nn/functional/norm.py | 3 +-- .../test_nn/test_functional/test_norm.py | 14 ++++++++------ .../test_ivy/test_functional/test_nn/test_norms.py | 8 ++++---- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/ivy/functional/frontends/paddle/nn/functional/norm.py b/ivy/functional/frontends/paddle/nn/functional/norm.py index 1cab45ee61a42..8853f204df49d 100644 --- a/ivy/functional/frontends/paddle/nn/functional/norm.py +++ b/ivy/functional/frontends/paddle/nn/functional/norm.py @@ -10,8 +10,7 @@ def layer_norm(x, normalized_shape, weight=None, bias=None, epsilon=1e-05, name= return ivy.layer_norm(x, normalized_shape, weight, bias, epsilon) - @to_ivy_arrays_and_back @with_supported_dtypes({"2.5.0 and below": ("float32", "float64")}, "paddle") def batch_norm(x, gamma, beta, moving_mean, moving_var, epsilon=1e-5): - return ivy.batch_norm(x, gamma, beta, moving_mean, moving_var, epsilon) \ No newline at end of file + return ivy.batch_norm(x, gamma, beta, moving_mean, moving_var, epsilon) diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py index dd63ef769b0bb..e858999c7bcba 100644 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py +++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py @@ -10,7 +10,9 @@ _generate_data_layer_norm, ) -from ivy_tests.test_ivy.test_functional.test_nn.test_norms import (_generate_data_batch_norm,) +from ivy_tests.test_ivy.test_functional.test_nn.test_norms import ( + _generate_data_batch_norm, +) # layer_norm @@ -27,7 +29,7 @@ def test_paddle_layer_norm( normalized_shape, eps, test_flags, - frontend: Literal['paddle'], + frontend: Literal["paddle"], on_device, fn_tree, ): @@ -44,6 +46,8 @@ def test_paddle_layer_norm( bias=offset[0], epsilon=eps, ) + + @handle_frontend_test( fn_tree="paddle.nn.functional.batch_norm", values_tuple=_generate_data_batch_norm( @@ -51,9 +55,7 @@ def test_paddle_layer_norm( ), eps=st.floats(min_value=0.01, max_value=0.1), ) -def test_batch_norm( - *, values_tuple, test_flags, backend_fw, fn_name, on_device -): +def test_batch_norm(*, values_tuple, test_flags, backend_fw, fn_name, on_device): (dtype, x, gamma, beta, moving_mean, moving_var, epsilon) = values_tuple helpers.test_function( input_dtypes=dtype, @@ -67,4 +69,4 @@ def test_batch_norm( moving_mean=moving_mean, moving_var=moving_var, epsilon=epsilon, - ) \ No newline at end of file + ) diff --git a/ivy_tests/test_ivy/test_functional/test_nn/test_norms.py b/ivy_tests/test_ivy/test_functional/test_nn/test_norms.py index b939646ee58b1..8c13ecc6dcf71 100644 --- a/ivy_tests/test_ivy/test_functional/test_nn/test_norms.py +++ b/ivy_tests/test_ivy/test_functional/test_nn/test_norms.py @@ -113,7 +113,8 @@ def test_layer_norm( new_std=new_std, ) -# created function for batch normalization + +# created function for batch normalization def _generate_data_batch_norm( draw, *, @@ -153,15 +154,14 @@ def _generate_data_batch_norm( return dtype, x, gamma, beta, moving_mean, moving_var, epsilon + @handle_test( fn_tree="functional.batch_norm", values_tuple=_generate_data_batch_norm( available_dtypes=helpers.get_dtypes("float"), ), ) -def test_batch_norm( - *, values_tuple, test_flags, backend_fw, fn_name, on_device -): +def test_batch_norm(*, values_tuple, test_flags, backend_fw, fn_name, on_device): (dtype, x, gamma, beta, moving_mean, moving_var, epsilon) = values_tuple helpers.test_function( input_dtypes=dtype, From 85ef210751bb4c4275f7693582f23d0ac26422eb Mon Sep 17 00:00:00 2001 From: Sanjay <121057369+Sanjay8602@users.noreply.github.com> Date: Wed, 18 Oct 2023 00:42:03 +0530 Subject: [PATCH 20/37] Delete ivy_tests/test_ivy/test_frontends/test_torch/test_indexing_slicing_joining_mutating_ops.py --- ...t_indexing_slicing_joining_mutating_ops.py | 1621 ----------------- 1 file changed, 1621 deletions(-) delete mode 100644 ivy_tests/test_ivy/test_frontends/test_torch/test_indexing_slicing_joining_mutating_ops.py diff --git a/ivy_tests/test_ivy/test_frontends/test_torch/test_indexing_slicing_joining_mutating_ops.py b/ivy_tests/test_ivy/test_frontends/test_torch/test_indexing_slicing_joining_mutating_ops.py deleted file mode 100644 index eb0e8a583fabc..0000000000000 --- a/ivy_tests/test_ivy/test_frontends/test_torch/test_indexing_slicing_joining_mutating_ops.py +++ /dev/null @@ -1,1621 +0,0 @@ -# global -from hypothesis import strategies as st -import math - - -# local -import ivy_tests.test_ivy.helpers as helpers -from ivy_tests.test_ivy.helpers import handle_frontend_test -from ivy_tests.test_ivy.test_functional.test_core.test_manipulation import _get_splits -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, -) - - -# noinspection DuplicatedCode -@st.composite -def _arrays_idx_n_dtypes(draw): - num_dims = draw(st.shared(helpers.ints(min_value=1, max_value=4), key="num_dims")) - num_arrays = draw( - st.shared(helpers.ints(min_value=2, max_value=4), key="num_arrays") - ) - common_shape = draw( - helpers.list_of_size( - x=helpers.ints(min_value=2, max_value=3), - size=num_dims - 1, - ) - ) - unique_idx = draw(helpers.ints(min_value=0, max_value=num_dims - 1)) - unique_dims = draw( - helpers.list_of_size( - x=helpers.ints(min_value=2, max_value=3), - size=num_arrays, - ) - ) - xs = list() - input_dtypes = draw( - helpers.array_dtypes(available_dtypes=draw(helpers.get_dtypes("float"))) - ) - for ud, dt in zip(unique_dims, input_dtypes): - x = draw( - helpers.array_values( - shape=common_shape[:unique_idx] + [ud] + common_shape[unique_idx:], - dtype=dt, - ) - ) - xs.append(x) - return xs, input_dtypes, unique_idx - - -# noinspection DuplicatedCode -@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 - - -# adjoint -@handle_frontend_test( - fn_tree="torch.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_adjoint( - *, - dtype_and_values, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtype, value = dtype_and_values - helpers.test_frontend_function( - input_dtypes=input_dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=value[0], - ) - - -# cat -@handle_frontend_test( - fn_tree="torch.cat", - xs_n_input_dtypes_n_unique_idx=_arrays_idx_n_dtypes(), -) -def test_torch_cat( - *, - xs_n_input_dtypes_n_unique_idx, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - xs, input_dtypes, unique_idx = xs_n_input_dtypes_n_unique_idx - helpers.test_frontend_function( - input_dtypes=input_dtypes, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - tensors=xs, - dim=unique_idx, - ) - - -# concat -@handle_frontend_test( - fn_tree="torch.concat", - xs_n_input_dtypes_n_unique_idx=_arrays_idx_n_dtypes(), -) -def test_torch_concat( - *, - xs_n_input_dtypes_n_unique_idx, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - xs, input_dtypes, unique_idx = xs_n_input_dtypes_n_unique_idx - helpers.test_frontend_function( - input_dtypes=input_dtypes, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - tensors=xs, - dim=unique_idx, - ) - - -# gather -@handle_frontend_test( - fn_tree="torch.gather", - params_indices_others=helpers.array_indices_axis( - array_dtypes=helpers.get_dtypes("valid"), - indices_dtypes=["int64"], - indices_same_dims=True, - ), -) -def test_torch_gather( - *, - params_indices_others, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtypes, input, indices, axis, batch_dims = params_indices_others - helpers.test_frontend_function( - input_dtypes=input_dtypes, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=input, - dim=axis, - index=indices, - ) - - -# nonzero -@handle_frontend_test( - fn_tree="torch.nonzero", - dtype_and_values=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - ), - as_tuple=st.booleans(), -) -def test_torch_nonzero( - *, - dtype_and_values, - as_tuple, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - dtype, input = dtype_and_values - helpers.test_frontend_function( - input_dtypes=dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=input[0], - as_tuple=as_tuple, - ) - - -# permute -@handle_frontend_test( - fn_tree="torch.permute", - dtype_values_axis=_array_idxes_n_dtype( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_permute( - *, - dtype_values_axis, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - x, idxes, dtype = dtype_values_axis - helpers.test_frontend_function( - input_dtypes=dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=x[0], - dims=tuple(idxes), - ) - - -# swapdims -@handle_frontend_test( - fn_tree="torch.swapdims", - dtype_and_values=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - shape=st.shared(helpers.get_shape(min_num_dims=2), key="shape"), - ), - dim0=helpers.get_axis( - shape=st.shared(helpers.get_shape(min_num_dims=2), key="shape"), - force_int=True, - ), - dim1=helpers.get_axis( - shape=st.shared(helpers.get_shape(min_num_dims=2), key="shape"), - force_int=True, - ), -) -def test_torch_swapdims( - *, - dtype_and_values, - dim0, - dim1, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtype, value = dtype_and_values - helpers.test_frontend_function( - input_dtypes=input_dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=value[0], - dim0=dim0, - dim1=dim1, - ) - - -# reshape -@st.composite -def dtypes_x_reshape(draw): - shape = draw(helpers.get_shape(min_num_dims=1)) - dtypes, x = draw( - helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - shape=shape, - ) - ) - shape = draw( - helpers.get_shape(min_num_dims=1).filter( - lambda s: math.prod(s) == math.prod(shape) - ) - ) - return dtypes, x, shape - - -@handle_frontend_test( - fn_tree="torch.reshape", - dtypes_x_reshape=dtypes_x_reshape(), -) -def test_torch_reshape( - *, - dtypes_x_reshape, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtype, x, shape = dtypes_x_reshape - helpers.test_frontend_function( - input_dtypes=input_dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=x[0], - shape=shape, - ) - - -# stack -@handle_frontend_test( - fn_tree="torch.stack", - dtype_value_shape=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=st.shared(helpers.ints(min_value=2, max_value=4), key="num_arrays"), - 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"), - ).filter(lambda axis: isinstance(axis, int)), -) -def test_torch_stack( - *, - dtype_value_shape, - dim, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtype, value = dtype_value_shape - helpers.test_frontend_function( - input_dtypes=input_dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - tensors=value, - dim=dim, - ) - - -# transpose -@handle_frontend_test( - fn_tree="torch.transpose", - dtype_and_values=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - shape=st.shared(helpers.get_shape(min_num_dims=2), key="shape"), - ), - dim0=helpers.get_axis( - shape=st.shared(helpers.get_shape(min_num_dims=2), key="shape"), - force_int=True, - ), - dim1=helpers.get_axis( - shape=st.shared(helpers.get_shape(min_num_dims=2), key="shape"), - force_int=True, - ), -) -def test_torch_transpose( - *, - dtype_and_values, - dim0, - dim1, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtype, value = dtype_and_values - helpers.test_frontend_function( - input_dtypes=input_dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=value[0], - dim0=dim0, - dim1=dim1, - ) - - -# t -@handle_frontend_test( - fn_tree="torch.t", - dtype_and_values=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - shape=st.shared(helpers.get_shape(max_num_dims=2), key="shape"), - ), -) -def test_torch_t( - *, - dtype_and_values, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtype, value = dtype_and_values - helpers.test_frontend_function( - input_dtypes=input_dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=value[0], - ) - - -# squeeze -@handle_frontend_test( - fn_tree="torch.squeeze", - dtype_and_values=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - 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"), - max_size=1, - ).filter(lambda axis: isinstance(axis, int)), -) -def test_torch_squeeze( - *, - dtype_and_values, - dim, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtype, value = dtype_and_values - helpers.test_frontend_function( - input_dtypes=input_dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=value[0], - dim=dim, - ) - - -# swapaxes -@handle_frontend_test( - fn_tree="torch.swapaxes", - dtype_and_values=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - shape=st.shared(helpers.get_shape(min_num_dims=2), key="shape"), - ), - axis0=helpers.get_axis( - shape=st.shared(helpers.get_shape(min_num_dims=2), key="shape"), - force_int=True, - ), - axis1=helpers.get_axis( - shape=st.shared(helpers.get_shape(min_num_dims=2), key="shape"), - force_int=True, - ), -) -def test_torch_swapaxes( - *, - dtype_and_values, - axis0, - axis1, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtype, value = dtype_and_values - helpers.test_frontend_function( - input_dtypes=input_dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=value[0], - axis0=axis0, - axis1=axis1, - ) - - -@st.composite -def _chunk_helper(draw): - dtype, x, shape = draw( - helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - min_num_dims=1, - ret_shape=True, - ) - ) - axis = draw(helpers.get_axis(shape=shape, force_int=True)) - if shape[axis] == 0: - chunks = 0 - else: - factors = [] - for i in range(1, shape[axis] + 1): - if shape[axis] % i == 0: - factors.append(i) - chunks = draw(st.sampled_from(factors)) - return dtype, x, axis, chunks - - -# chunk -@handle_frontend_test( - fn_tree="torch.chunk", - x_dim_chunks=_chunk_helper(), - test_with_out=st.just(False), -) -def test_torch_chunk( - *, - x_dim_chunks, - fn_tree, - on_device, - frontend, - test_flags, - backend_fw, -): - dtype, x, axis, chunks = x_dim_chunks - helpers.test_frontend_function( - input_dtypes=dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=x[0], - chunks=chunks, - dim=axis, - ) - - -# tile -@handle_frontend_test( - fn_tree="torch.tile", - 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=False, - force_tuple=True, - ), -) -def test_torch_tile( - *, - dtype_value, - dim, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtype, value = dtype_value - helpers.test_frontend_function( - input_dtypes=input_dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=value[0], - dims=dim, - ) - - -# unsqueeze -@handle_frontend_test( - fn_tree="torch.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_unsqueeze( - *, - dtype_value, - dim, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtype, value = dtype_value - helpers.test_frontend_function( - input_dtypes=input_dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=value[0], - dim=dim, - ) - - -@handle_frontend_test( - fn_tree="torch.argwhere", - dtype_and_values=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - ), -) -def test_torch_argwhere( - *, - dtype_and_values, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - dtype, input = dtype_and_values - helpers.test_frontend_function( - input_dtypes=dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=input[0], - ) - - -# movedim -@handle_frontend_test( - fn_tree="torch.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, - ), - test_with_out=st.just(False), -) -def test_torch_movedim( - *, - dtype_and_input, - source, - destination, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtype, value = dtype_and_input - helpers.test_frontend_function( - input_dtypes=input_dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=value[0], - source=source, - destination=destination, - ) - - -# moveaxis -@handle_frontend_test( - fn_tree="torch.moveaxis", - 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, - ), - test_with_out=st.just(False), -) -def test_torch_moveaxis( - *, - dtype_and_input, - source, - destination, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtype, value = dtype_and_input - helpers.test_frontend_function( - input_dtypes=input_dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=value[0], - source=source, - destination=destination, - ) - - -# hstack -@handle_frontend_test( - fn_tree="torch.hstack", - dtype_value_shape=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_hstack( - *, - dtype_value_shape, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtype, value = dtype_value_shape - helpers.test_frontend_function( - input_dtypes=input_dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - tensors=value, - ) - - -# dstack -@handle_frontend_test( - fn_tree="torch.dstack", - dtype_value_shape=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_dstack( - *, - dtype_value_shape, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtype, value = dtype_value_shape - helpers.test_frontend_function( - input_dtypes=input_dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - tensors=value, - ) - - -# index_select -@handle_frontend_test( - fn_tree="torch.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_index_select( - *, - params_indices_others, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtypes, input, indices, axis, batch_dims = params_indices_others - helpers.test_frontend_function( - input_dtypes=input_dtypes, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=input, - dim=axis, - index=indices, - ) - - -# take_along_dim -@handle_frontend_test( - fn_tree="torch.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_take_along_dim( - *, - dtype_indices_axis, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtypes, value, indices, axis, _ = dtype_indices_axis - helpers.test_frontend_function( - input_dtypes=input_dtypes, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=value, - indices=indices, - dim=axis, - ) - - -# vstack -@handle_frontend_test( - fn_tree="torch.vstack", - dtype_value_shape=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_torch_vstack( - *, - dtype_value_shape, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtype, value = dtype_value_shape - helpers.test_frontend_function( - input_dtypes=input_dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - tensors=value, - ) - - -# split -@handle_frontend_test( - fn_tree="torch.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_or_sections=_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_split( - *, - dtype_value, - split_size_or_sections, - dim, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtype, value = dtype_value - helpers.test_frontend_function( - input_dtypes=input_dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - tensor=value[0], - split_size_or_sections=split_size_or_sections, - dim=dim, - ) - - -# tensor_split -@handle_frontend_test( - fn_tree="torch.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 - ), - axis=st.shared( - helpers.get_axis( - shape=st.shared(helpers.get_shape(min_num_dims=1), key="value_shape"), - force_int=True, - ), - key="target_axis", - ), - number_positional_args=st.just(2), - test_with_out=st.just(False), -) -def test_torch_tensor_split( - *, - dtype_value, - indices_or_sections, - axis, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtype, value = dtype_value - helpers.test_frontend_function( - input_dtypes=input_dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=value[0], - indices_or_sections=indices_or_sections, - dim=axis, - ) - - -# unbind -@handle_frontend_test( - fn_tree="torch.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_unbind( - *, - dtype_value_axis, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtypes, value, axis = dtype_value_axis - helpers.test_frontend_function( - input_dtypes=input_dtypes, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=value[0], - dim=axis, - ) - - -# dsplit -@handle_frontend_test( - fn_tree="torch.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_dsplit( - *, - dtype_value, - indices_or_sections, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtype, value = dtype_value - helpers.test_frontend_function( - input_dtypes=input_dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=value[0], - indices_or_sections=indices_or_sections, - ) - - -# hsplit -@handle_frontend_test( - fn_tree="torch.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_hsplit( - *, - dtype_value, - indices_or_sections, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtype, value = dtype_value - helpers.test_frontend_function( - input_dtypes=input_dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=value[0], - indices_or_sections=indices_or_sections, - ) - - -# vsplit -@handle_frontend_test( - fn_tree="torch.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_vsplit( - *, - dtype_value, - indices_or_sections, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtype, value = dtype_value - helpers.test_frontend_function( - input_dtypes=input_dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=value[0], - indices_or_sections=indices_or_sections, - ) - - -# row_stack -@handle_frontend_test( - fn_tree="torch.row_stack", - dtype_value_shape=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - num_arrays=st.integers(1, 5), - ), -) -def test_torch_row_stack( - *, - dtype_value_shape, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtype, value = dtype_value_shape - helpers.test_frontend_function( - input_dtypes=input_dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - tensors=value, - ) - - -@handle_frontend_test( - fn_tree="torch.where", - broadcastables=_broadcastable_trio(), - only_cond=st.booleans(), -) -def test_torch_where( - *, - broadcastables, - only_cond, - frontend, - test_flags, - fn_tree, - backend_fw, - on_device, -): - cond, xs, dtypes = broadcastables - - if only_cond: - helpers.test_frontend_function( - input_dtypes=[dtypes[0]], - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - condition=xs[0], - ) - - else: - helpers.test_frontend_function( - input_dtypes=["bool"] + dtypes, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - condition=cond, - input=xs[0], - other=xs[1], - ) - - -@handle_frontend_test( - fn_tree="torch.conj", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float_and_complex"), - ), -) -def test_torch_conj( - on_device, - frontend, - *, - dtype_and_x, - fn_tree, - test_flags, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_function( - input_dtypes=input_dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=x[0], - ) - - -@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")) - available_input_types.remove("float16") # half summation unstable in backends - 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 - - -# index_add -@handle_frontend_test( - fn_tree="torch.index_add", - xs_dtypes_dim_idx=_arrays_dim_idx_n_dtypes(), - alpha=st.integers(min_value=1, max_value=2), -) -def test_torch_index_add( - *, - xs_dtypes_dim_idx, - alpha, - on_device, - fn_tree, - frontend, - test_flags, - 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_function( - input_dtypes=[input_dtypes[0], "int64", input_dtypes[1]], - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - rtol=1e-03, - input=input, - dim=axis, - index=indices, - source=source, - alpha=alpha, - ) - - -# index_copy -@handle_frontend_test( - fn_tree="torch.index_copy", - xs_dtypes_dim_idx=_arrays_dim_idx_n_dtypes(), -) -def test_torch_index_copy( - *, - xs_dtypes_dim_idx, - on_device, - fn_tree, - frontend, - test_flags, - 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_function( - input_dtypes=[input_dtypes[0], "int64", input_dtypes[1]], - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=input, - dim=axis, - index=indices, - source=source, - ) - - -@st.composite -def _dtypes_input_mask(draw): - _shape = draw(helpers.get_shape(min_num_dims=1, min_dim_size=1)) - _mask = draw(helpers.array_values(dtype=helpers.get_dtypes("bool"), shape=_shape)) - _dtype, _x = draw( - helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - num_arrays=1, - shape=_shape, - ) - ) - - return _dtype, _x, _mask - - -@handle_frontend_test( - fn_tree="torch.masked_select", - dtype_input_mask=_dtypes_input_mask(), -) -def test_torch_masked_select( - *, - dtype_input_mask, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - ( - input_dtype, - x, - mask, - ) = dtype_input_mask - - helpers.test_frontend_function( - input_dtypes=input_dtype + ["bool"], - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=x[0], - mask=mask, - ) - - -@handle_frontend_test( - fn_tree="torch.take", - dtype_and_x=helpers.array_indices_axis( - array_dtypes=helpers.get_dtypes(), indices_dtypes=["int64"] - ), -) -def test_torch_take( - *, - dtype_and_x, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - dtypes, xs, indices, _, _ = dtype_and_x - helpers.test_frontend_function( - input_dtypes=dtypes, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=xs, - index=indices, - ) - - -@st.composite -def _dtype_input_dim_start_length(draw): - _shape = draw(helpers.get_shape(min_num_dims=1, min_dim_size=1)) - _dtype, _x = draw( - helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("numeric"), - num_arrays=1, - shape=_shape, - ) - ) - _dim = draw( - helpers.get_axis( - shape=_shape, - force_int=True, - ), - ) - _start = draw(helpers.ints(min_value=1, max_value=_shape[_dim])) - - _length = draw(helpers.ints(min_value=0, max_value=_shape[_dim] - _start)) - - return _dtype, _x, _dim, _start, _length - - -@handle_frontend_test( - fn_tree="torch.narrow", - dtype_input_dim_start_length=_dtype_input_dim_start_length(), -) -def test_torch_narrow( - *, - dtype_input_dim_start_length, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - (input_dtype, x, dim, start, length) = dtype_input_dim_start_length - - helpers.test_frontend_function( - input_dtypes=input_dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=x[0], - dim=dim, - start=start, - length=length, - ) - - -@st.composite -def _dtype_input_idx_axis(draw): - dtype_x_axis_shape = draw( - helpers.dtype_values_axis( - available_dtypes=helpers.get_dtypes("valid"), - force_int_axis=True, - ret_shape=True, - valid_axis=True, - min_num_dims=2, - ) - ) - - input_dtype, x, axis, shape = dtype_x_axis_shape - max_idx = 0 - if shape: - max_idx = shape[axis] - 1 - idx = draw(helpers.ints(min_value=0, max_value=max_idx)) - x = x[0] - - return input_dtype, x, idx, axis - - -@handle_frontend_test( - fn_tree="torch.select", - dtype_x_idx_axis=_dtype_input_idx_axis(), -) -def test_torch_select( - *, - dtype_x_idx_axis, - on_device, - fn_tree, - frontend, - test_flags, - backend_fw, -): - input_dtype, x, idx, axis = dtype_x_idx_axis - - helpers.test_frontend_function( - input_dtypes=input_dtype, - backend_to_test=backend_fw, - frontend=frontend, - test_flags=test_flags, - fn_tree=fn_tree, - on_device=on_device, - input=x, - dim=axis, - index=idx, - ) - - -def tensor_ops(tensor, operation_type, *args): - if operation_type == "index": - if len(args) != 1: - raise ValueError - index = args[0] - return tensor[index] - elif operation_type == "slice": - if len(args) != 2: - raise ValueError - start, end = args - return tensor[start:end] - elif operation_type == "join": - if len(args) != 1: - raise ValueError - other_tensor = args[0] - return tensor.cat((tensor, other_tensor), dim=0) - elif operation_type == "mutate": - if len(args) != 1: - raise ValueError - factor = args[0] - tensor.mul_(factor) - return tensor - else: - raise ValueError From 7e52b746af24b7c89d28b78fbebc24841f5e8bdb Mon Sep 17 00:00:00 2001 From: Sanjay <121057369+Sanjay8602@users.noreply.github.com> Date: Wed, 18 Oct 2023 00:42:35 +0530 Subject: [PATCH 21/37] Delete ivy/functional/ivy/experimental/manipulation.py --- .../ivy/experimental/manipulation.py | 2180 ----------------- 1 file changed, 2180 deletions(-) delete mode 100644 ivy/functional/ivy/experimental/manipulation.py diff --git a/ivy/functional/ivy/experimental/manipulation.py b/ivy/functional/ivy/experimental/manipulation.py deleted file mode 100644 index 5f28f2a629c49..0000000000000 --- a/ivy/functional/ivy/experimental/manipulation.py +++ /dev/null @@ -1,2180 +0,0 @@ -# global -from typing import ( - Optional, - Union, - Tuple, - Iterable, - Sequence, - Callable, - Any, - Literal, - List, -) -from numbers import Number -from functools import partial -import math - -# local -import ivy -from ivy.func_wrapper import ( - handle_out_argument, - handle_partial_mixed_function, - to_native_arrays_and_back, - inputs_to_native_shapes, - handle_nestable, - handle_array_like_without_promotion, - handle_view, - inputs_to_ivy_arrays, - handle_array_function, - handle_device_shifting, -) -from ivy.utils.backend import current_backend -from ivy.utils.exceptions import handle_exceptions - - -@handle_exceptions -@handle_nestable -@handle_partial_mixed_function -@handle_array_like_without_promotion -@handle_view -@inputs_to_ivy_arrays -@handle_array_function -def flatten( - x: Union[ivy.Array, ivy.NativeArray], - /, - *, - copy: Optional[bool] = None, - start_dim: Optional[int] = 0, - end_dim: Optional[int] = -1, - order: Optional[str] = "C", - out: Optional[ivy.Array] = None, -) -> ivy.Array: - """ - Flattens input by reshaping it into a one-dimensional tensor. If start_dim or - end_dim are passed, only dimensions starting with start_dim and ending with end_dim - are flattened. The order of elements in input is unchanged. - - Parameters - ---------- - x - input array to flatten. - copy - boolean indicating whether to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - start_dim - first dim to flatten. If not set, defaults to 0. - end_dim - last dim to flatten. If not set, defaults to -1. - order - Read the elements of the input container using this index order, - and place the elements into the reshaped array using this index order. - ‘C’ means to read / write the elements using C-like index order, - with the last axis index changing fastest, back to the first axis index - changing slowest. - ‘F’ means to read / write the elements using Fortran-like index order, with - the first index changing fastest, and the last index changing slowest. - Note that the ‘C’ and ‘F’ options take no account of the memory layout - of the underlying array, and only refer to the order of indexing. - Default order is 'C' - out - optional output array, for writing the result to. - - Returns - ------- - ret - the flattened array over the specified dimensions. - - Examples - -------- - With :class:`ivy.Array` input: - - >>> x = ivy.array([[1,2], [3,4]]) - >>> ivy.flatten(x) - ivy.array([1, 2, 3, 4]) - - >>> x = ivy.array([[1,2], [3,4]]) - >>> ivy.flatten(x, order='F') - ivy.array([1, 3, 2, 4]) - - >>> x = ivy.array( - [[[[ 5, 5, 0, 6], - [17, 15, 11, 16], - [ 6, 3, 13, 12]], - - [[ 6, 18, 10, 4], - [ 5, 1, 17, 3], - [14, 14, 18, 6]]], - - - [[[12, 0, 1, 13], - [ 8, 7, 0, 3], - [19, 12, 6, 17]], - - [[ 4, 15, 6, 15], - [ 0, 5, 17, 9], - [ 9, 3, 6, 19]]], - - - [[[17, 13, 11, 16], - [ 4, 18, 17, 4], - [10, 10, 9, 1]], - - [[19, 17, 13, 10], - [ 4, 19, 16, 17], - [ 2, 12, 8, 14]]]] - ) - >>> ivy.flatten(x, start_dim = 1, end_dim = 2) - ivy.array( - [[[ 5, 5, 0, 6], - [17, 15, 11, 16], - [ 6, 3, 13, 12], - [ 6, 18, 10, 4], - [ 5, 1, 17, 3], - [14, 14, 18, 6]], - - [[12, 0, 1, 13], - [ 8, 7, 0, 3], - [19, 12, 6, 17], - [ 4, 15, 6, 15], - [ 0, 5, 17, 9], - [ 9, 3, 6, 19]], - - [[17, 13, 11, 16], - [ 4, 18, 17, 4], - [10, 10, 9, 1], - [19, 17, 13, 10], - [ 4, 19, 16, 17], - [ 2, 12, 8, 14]]])) - """ - if copy: - x = ivy.copy_array(x) - if x.shape == (): - x = ivy.reshape(x, (1, -1))[0, :] - if start_dim == end_dim: - return ivy.inplace_update(out, x) if ivy.exists(out) else x - if start_dim not in range(-len(x.shape), len(x.shape)): - raise IndexError( - "Dimension out of range (expected to be in range of" - f" {[-len(x.shape), len(x.shape) - 1]}, but got {start_dim}" - ) - if end_dim not in range(-len(x.shape), len(x.shape)): - raise IndexError( - "Dimension out of range (expected to be in range of" - f" {[-len(x.shape), len(x.shape) - 1]}, but got {end_dim}" - ) - if start_dim < 0: - start_dim = len(x.shape) + start_dim - if end_dim < 0: - end_dim = len(x.shape) + end_dim - c = 1 - for i in range(start_dim, end_dim + 1): - c *= x.shape[i] - lst = [c] - if start_dim != 0: - for i in range(0, start_dim): - lst.insert(i, x.shape[i]) - for i in range(end_dim + 1, len(x.shape)): - lst.insert(i, x.shape[i]) - return ivy.reshape(x, tuple(lst), order=order, out=out) - - -flatten.mixed_backend_wrappers = { - "to_add": ( - "handle_out_argument", - "inputs_to_native_arrays", - "outputs_to_ivy_arrays", - "handle_device_shifting", - ), - "to_skip": ("inputs_to_ivy_arrays", "handle_partial_mixed_function"), -} - - -@handle_nestable -@handle_array_like_without_promotion -@handle_view -@handle_out_argument -@to_native_arrays_and_back -@handle_device_shifting -def moveaxis( - a: Union[ivy.Array, ivy.NativeArray], - source: Union[int, Sequence[int]], - destination: Union[int, Sequence[int]], - /, - *, - copy: Optional[bool] = None, - out: Optional[Union[ivy.Array, ivy.NativeArray]] = None, -) -> Union[ivy.Array, ivy.NativeArray]: - """ - Move axes of an array to new positions.. - - Parameters - ---------- - a - The array whose axes should be reordered. - source - Original positions of the axes to move. These must be unique. - destination - Destination positions for each of the original axes. - These must also be unique. - copy - boolean indicating whether to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - out - optional output array, for writing the result to. - - Returns - ------- - ret - Array with moved axes. This array is a view of the input array. - - Examples - -------- - With :class:`ivy.Array` input: - >>> x = ivy.zeros((3, 4, 5)) - >>> ivy.moveaxis(x, 0, -1).shape - (4, 5, 3) - >>> ivy.moveaxis(x, -1, 0).shape - (5, 3, 4) - """ - return ivy.current_backend().moveaxis(a, source, destination, copy=copy, out=out) - - -@handle_nestable -@handle_array_like_without_promotion -@handle_out_argument -@to_native_arrays_and_back -@handle_device_shifting -def heaviside( - x1: Union[ivy.Array, ivy.NativeArray], - x2: Union[ivy.Array, ivy.NativeArray], - /, - *, - out: Optional[ivy.Array] = None, -) -> ivy.Array: - """ - Compute the Heaviside step function for each element in x1. - - Parameters - ---------- - x1 - input array. - x2 - values to use where x1 is zero. - out - optional output array, for writing the result to. - - Returns - ------- - ret - output array with element-wise Heaviside step function of x1. - This is a scalar if both x1 and x2 are scalars. - - Examples - -------- - With :class:`ivy.Array` input: - - >>> x1 = ivy.array([-1.5, 0, 2.0]) - >>> x2 = ivy.array([0.5]) - >>> ivy.heaviside(x1, x2) - ivy.array([0.0000, 0.5000, 1.0000]) - - >>> x1 = ivy.array([-1.5, 0, 2.0]) - >>> x2 = ivy.array([1.2, -2.0, 3.5]) - >>> ivy.heaviside(x1, x2) - ivy.array([0., -2., 1.]) - """ - return ivy.current_backend().heaviside(x1, x2, out=out) - - -@handle_nestable -@handle_array_like_without_promotion -@handle_view -@handle_out_argument -@to_native_arrays_and_back -@handle_device_shifting -def flipud( - m: Union[ivy.Array, ivy.NativeArray], - /, - *, - copy: Optional[bool] = None, - out: Optional[Union[ivy.Array, ivy.NativeArray]] = None, -) -> Union[ivy.Array, ivy.NativeArray]: - """ - Flip array in the up/down direction. Flip the entries in each column in the up/down - direction. Rows are preserved, but appear in a different order than before. - - Parameters - ---------- - m - The array to be flipped. - copy - boolean indicating whether to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - out - optional output array, for writing the result to. - - Returns - ------- - ret - Array corresponding to input array with elements - order reversed along axis 0. - - Examples - -------- - >>> m = ivy.diag([1, 2, 3]) - >>> ivy.flipud(m) - ivy.array([[ 0., 0., 3.], - [ 0., 2., 0.], - [ 1., 0., 0.]]) - """ - return ivy.current_backend().flipud(m, copy=copy, out=out) - - -@handle_nestable -@handle_out_argument -@to_native_arrays_and_back -@handle_device_shifting -def vstack( - arrays: Sequence[ivy.Array], - /, - *, - out: Optional[Union[ivy.Array, ivy.NativeArray]] = None, -) -> ivy.Array: - """ - Stack arrays in sequence vertically (row wise). - - Parameters - ---------- - arrays - Sequence of arrays to be stacked. - - Returns - ------- - ret - The array formed by stacking the given arrays. - - Examples - -------- - >>> x = ivy.array([1, 2, 3]) - >>> y = ivy.array([2, 3, 4]) - >>> ivy.vstack((x, y)) - ivy.array([[1, 2, 3], - [2, 3, 4]]) - >>> ivy.vstack((x, y, x, y)) - ivy.array([[1, 2, 3], - [2, 3, 4], - [1, 2, 3], - [2, 3, 4]]) - - >>> y = [ivy.array([[5, 6]]), ivy.array([[7, 8]])] - >>> print(ivy.vstack(y)) - ivy.array([[5, 6], - [7, 8]]) - """ - return ivy.current_backend().vstack(arrays, out=out) - - -@handle_nestable -@handle_out_argument -@to_native_arrays_and_back -@handle_device_shifting -def hstack( - arrays: Sequence[ivy.Array], - /, - *, - out: Optional[Union[ivy.Array, ivy.NativeArray]] = None, -) -> ivy.Array: - """ - Stack arrays in sequence horizotally (column wise). - - Parameters - ---------- - arrays - Sequence of arrays to be stacked. - - Returns - ------- - ret - The array formed by stacking the given arrays. - - Examples - -------- - >>> x = ivy.array([1, 2, 3]) - >>> y = ivy.array([2, 3, 4]) - >>> ivy.hstack((x, y)) - ivy.array([1, 2, 3, 2, 3, 4]) - >>> x = ivy.array([1, 2, 3]) - >>> y = ivy.array([0, 0, 0]) - >>> ivy.hstack((x, y, x)) - ivy.array([1, 2, 3, 0, 0, 0, 1, 2, 3]) - >>> y = [ivy.array([[5, 6]]), ivy.array([[7, 8]])] - >>> print(ivy.hstack(y)) - ivy.array([[5, 6, 7, 8]]) - """ - return ivy.current_backend().hstack(arrays, out=out) - - -@handle_exceptions -@handle_nestable -@handle_array_like_without_promotion -@handle_view -@handle_out_argument -@to_native_arrays_and_back -@handle_device_shifting -def rot90( - m: Union[ivy.Array, ivy.NativeArray], - /, - *, - copy: Optional[bool] = None, - k: int = 1, - axes: Tuple[int, int] = (0, 1), - out: Optional[ivy.Array] = None, -) -> ivy.Array: - """ - Rotate an array by 90 degrees in the plane specified by axes. Rotation direction is - from the first towards the second axis. - - Parameters - ---------- - m - Input array of two or more dimensions. - copy - boolean indicating whether to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - k - Number of times the array is rotated by 90 degrees. - axes - The array is rotated in the plane defined by the axes. Axes must be - different. - out - optional output container, for writing the result to. It must have a shape - that the inputs broadcast to. - - Returns - ------- - ret - A rotated view of m. - - Examples - -------- - With :code:`ivy.Array` input: - >>> m = ivy.array([[1,2], [3,4]]) - >>> ivy.rot90(m) - ivy.array([[2, 4], - [1, 3]]) - >>> m = ivy.array([[1,2], [3,4]]) - >>> ivy.rot90(m, k=2) - ivy.array([[4, 3], - [2, 1]]) - >>> m = ivy.array([[[0, 1],\ - [2, 3]],\ - [[4, 5],\ - [6, 7]]]) - >>> ivy.rot90(m, k=2, axes=(1,2)) - ivy.array([[[3, 2], - [1, 0]], - - [[7, 6], - [5, 4]]]) - With :code:`ivy.NativeArray` input: - >>> m = ivy.native_array([[1,2], [3,4]]) - >>> ivy.rot90(m) - ivy.array([[2, 4], - [1, 3]]) - >>> m = ivy.native_array([[1,2], [3,4]]) - >>> ivy.rot90(m, k=2) - ivy.array([[4, 3], - [2, 1]]) - >>> m = ivy.native_array([[[0, 1],\ - [2, 3]],\ - [[4, 5],\ - [6, 7]]]) - >>> ivy.rot90(m, k=2, axes=(1,2)) - ivy.array([[[3, 2], - [1, 0]], - - [[7, 6], - [5, 4]]]) - """ - return ivy.current_backend(m).rot90(m, copy=copy, k=k, axes=axes, out=out) - - -@handle_exceptions -@handle_nestable -@handle_array_like_without_promotion -@handle_out_argument -@to_native_arrays_and_back -@handle_device_shifting -def top_k( - x: Union[ivy.Array, ivy.NativeArray], - k: int, - /, - *, - axis: int = -1, - largest: bool = True, - sorted: bool = True, - out: Optional[tuple] = None, -) -> Tuple[ivy.Array, ivy.NativeArray]: - """ - Return the `k` largest elements of the given input array along a given axis. - - Parameters - ---------- - x - The array to compute top_k for. - k - Number of top elements to retun must not exceed the array size. - axis - The axis along which we must return the top elements default value is 1. - largest - If largest is set to False we return k smallest elements of the array. - sorted - If sorted is set to True we return the elements in sorted order. - out: - Optional output tuple, for writing the result to. Must have two arrays inside, - with a shape that the returned tuple broadcast to. - - Returns - ------- - ret - A named tuple with values and indices of top k elements. - - Examples - -------- - With :class:`ivy.Array` input: - - >>> x = ivy.array([2., 1., -3., 5., 9., 0., -4]) - >>> y = ivy.top_k(x, 2) - >>> print(y) - top_k(values=ivy.array([9., 5.]), indices=ivy.array([4, 3])) - - >>> x = ivy.array([[-2., 3., 4., 0.], [-8., 0., -1., 2.]]) - >>> y = ivy.top_k(x, 2, axis=1, largest=False) - >>> print(y) - top_k(values=ivy.array([[-2., 0.],[-8., -1.]]), - ... indices=ivy.array([[0, 3],[0, 2]])) - - With :class:`ivy.NativeArray` input: - - >>> x = ivy.native_array([2., 1., -3., 5., 9., 0., -4]) - >>> y = ivy.top_k(x, 3) - >>> print(y) - top_k(values=ivy.array([9., 5., 2.]), indices=ivy.array([4, 3, 0])) - - With :class:`ivy.Container` input: - - >>> x = ivy.Container(a=ivy.array([-1, 2, -4]), b=ivy.array([4., 5., 0.])) - >>> y = ivy.top_k(2) - >>> print(y) - { - a: [ - values = ivy.array([ 2, -1]), - indices = ivy.array([1, 0]) - ], - b: [ - values = ivy.array([5., 4.]), - indices = ivy.array([1, 0]) - ] - } - """ - return current_backend(x).top_k( - x, k, axis=axis, largest=largest, sorted=sorted, out=out - ) - - -@handle_nestable -@handle_array_like_without_promotion -@handle_view -@handle_out_argument -@to_native_arrays_and_back -@handle_device_shifting -def fliplr( - m: Union[ivy.Array, ivy.NativeArray], - /, - *, - copy: Optional[bool] = None, - out: Optional[Union[ivy.Array, ivy.NativeArray]] = None, -) -> Union[ivy.Array, ivy.NativeArray]: - """ - Flip array in the left/right direction. Flip the entries in each column in the - left/right direction. Columns are preserved, but appear in a different order than - before. - - Parameters - ---------- - m - The array to be flipped. Must be at least 2-D. - copy - boolean indicating whether to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - out - optional output array, for writing the result to. - - Returns - ------- - ret - Array corresponding to input array with elements - order reversed along axis 1. - - Examples - -------- - >>> m = ivy.diag([1, 2, 3]) - >>> ivy.fliplr(m) - ivy.array([[0, 0, 1], - [0, 2, 0], - [3, 0, 0]]) - """ - return ivy.current_backend().fliplr(m, copy=copy, out=out) - - -@handle_nestable -@handle_array_like_without_promotion -@handle_out_argument -@to_native_arrays_and_back -@handle_device_shifting -def i0( - x: Union[ivy.Array, ivy.NativeArray], - /, - *, - out: Optional[ivy.Array] = None, -) -> ivy.Array: - """ - Compute the Bessel i0 function of x element-wise. - - Parameters - ---------- - x - Array input. - out - optional output array, for writing the result to. - - Returns - ------- - ret - Array with the modified Bessel function - evaluated at each of the elements of x. - - Examples - -------- - >>> x = ivy.array([1, 2, 3]) - >>> ivy.i0(x) - ivy.array([1.26606588, 2.2795853 , 4.88079259]) - """ - return ivy.current_backend(x).i0(x, out=out) - - -def _slice_at_axis(sl, axis): - return (slice(None),) * axis + (sl,) + (...,) - - -def _set_pad_area(padded, axis, width_pair, value_pair): - if width_pair[0] > 0: - left_slice = _slice_at_axis(slice(None, width_pair[0]), axis) - padded[left_slice] = value_pair[0] - if width_pair[1] > 0: - right_slice = _slice_at_axis( - slice(padded.shape[axis] - width_pair[1], None), axis - ) - padded[right_slice] = value_pair[1] - return padded - - -def _get_edges(padded, axis, width_pair): - left_index = width_pair[0] - left_slice = _slice_at_axis(slice(left_index, left_index + 1), axis) - left_edge = padded[left_slice] - right_index = padded.shape[axis] - width_pair[1] - right_slice = _slice_at_axis(slice(right_index - 1, right_index), axis) - right_edge = padded[right_slice] - return left_edge, right_edge - - -def _get_linear_ramps(padded, axis, width_pair, end_value_pair): - edge_pair = _get_edges(padded, axis, width_pair) - if width_pair[0] > 0: - left_ramp = ivy.linspace( - end_value_pair[0], - ivy.array(edge_pair[0].squeeze(axis)), - num=width_pair[0], - endpoint=False, - dtype=ivy.Dtype(str(padded.dtype)), - axis=axis, - ) - else: - left_ramp = ivy.empty((0,)) - if width_pair[1] > 0: - right_ramp = ivy.flip( - ivy.linspace( - end_value_pair[1], - ivy.array(edge_pair[1].squeeze(axis)), - num=width_pair[1], - endpoint=False, - dtype=ivy.Dtype(str(padded.dtype)), - axis=axis, - ), - axis=axis, - ) - else: - right_ramp = ivy.empty((0,)) - return left_ramp, right_ramp - - -def _get_stats(padded, axis, width_pair, length_pair, stat_func): - left_index = width_pair[0] - right_index = padded.shape[axis] - width_pair[1] - max_length = right_index - left_index - left_length, right_length = length_pair - if left_length is None or max_length < left_length: - left_length = max_length - if right_length is None or max_length < right_length: - right_length = max_length - left_slice = _slice_at_axis(slice(left_index, left_index + left_length), axis) - left_chunk = padded[left_slice] - left_stat = stat_func(left_chunk, axis=axis, keepdims=True) - left_stat = ivy.round(left_stat) if "int" in left_chunk.dtype else left_stat - if left_length == right_length == max_length: - return left_stat, left_stat - right_slice = _slice_at_axis(slice(right_index - right_length, right_index), axis) - right_chunk = padded[right_slice] - right_stat = stat_func(right_chunk, axis=axis, keepdims=True) - right_stat = ivy.round(right_stat) if "int" in right_chunk.dtype else right_stat - return left_stat, right_stat - - -def _set_reflect_both(padded, axis, width_pair, method, include_edge=False): - left_pad, right_pad = width_pair - old_length = padded.shape[axis] - right_pad - left_pad - if include_edge: - edge_offset = 1 - else: - edge_offset = 0 - old_length -= 1 - if left_pad > 0: - chunk_length = min(old_length, left_pad) - stop = left_pad - edge_offset - start = stop + chunk_length - left_slice = _slice_at_axis(slice(start, stop, -1), axis) - left_chunk = padded[left_slice] - if method == "odd": - edge_slice = _slice_at_axis(slice(left_pad, left_pad + 1), axis) - left_chunk = 2 * padded[edge_slice] - left_chunk - start = left_pad - chunk_length - stop = left_pad - pad_area = _slice_at_axis(slice(start, stop), axis) - padded[pad_area] = left_chunk - left_pad -= chunk_length - if right_pad > 0: - chunk_length = min(old_length, right_pad) - start = -right_pad + edge_offset - 2 - stop = start - chunk_length - right_slice = _slice_at_axis(slice(start, stop, -1), axis) - right_chunk = padded[right_slice] - if method == "odd": - edge_slice = _slice_at_axis(slice(-right_pad - 1, -right_pad), axis) - right_chunk = 2 * padded[edge_slice] - right_chunk - start = padded.shape[axis] - right_pad - stop = start + chunk_length - pad_area = _slice_at_axis(slice(start, stop), axis) - padded[pad_area] = right_chunk - right_pad -= chunk_length - return left_pad, right_pad, padded - - -def _set_wrap_both(padded, axis, width_pair): - left_pad, right_pad = width_pair - period = padded.shape[axis] - right_pad - left_pad - new_left_pad = 0 - new_right_pad = 0 - if left_pad > 0: - right_slice = _slice_at_axis( - slice( - -right_pad - min(period, left_pad), - -right_pad if right_pad != 0 else None, - ), - axis, - ) - right_chunk = padded[right_slice] - if left_pad > period: - pad_area = _slice_at_axis(slice(left_pad - period, left_pad), axis) - new_left_pad = left_pad - period - else: - pad_area = _slice_at_axis(slice(None, left_pad), axis) - padded[pad_area] = right_chunk - if right_pad > 0: - left_slice = _slice_at_axis( - slice( - left_pad, - left_pad + min(period, right_pad), - ), - axis, - ) - left_chunk = padded[left_slice] - if right_pad > period: - pad_area = _slice_at_axis(slice(-right_pad, -right_pad + period), axis) - new_right_pad = right_pad - period - else: - pad_area = _slice_at_axis(slice(-right_pad, None), axis) - padded[pad_area] = left_chunk - return new_left_pad, new_right_pad, padded - - -def _pad_simple(array, pad_width, fill_value=None): - new_shape = tuple( - left + size + right for size, (left, right) in zip(array.shape, pad_width) - ) - padded = ivy.zeros(new_shape, dtype=array.dtype) - if fill_value is not None: - padded = ivy.ones_like(padded) * fill_value - original_area_slice = tuple( - slice(left, left + size) for size, (left, right) in zip(array.shape, pad_width) - ) - padded[original_area_slice] = array - return padded, original_area_slice - - -def _to_pairs(x, n): - if ivy.isscalar(x): - return ((x, x),) * n - elif len(x) == 2 and ivy.isscalar(x[0]): - return ((x[0], x[1]),) * n - elif len(x) != n: - ivy.utils.assertions.check_equal( - ivy.asarray(list(x)).shape, - (n, 2), - message=( - "tuple argument should contain " - "ndim pairs where ndim is the number of " - "the input's dimensions" - ), - as_array=False, - ) - return x - - -def _to_dilated(x, n): - if ivy.isscalar(x): - return ((x, x, x),) * n - elif len(x) == 3 and ivy.isscalar(x[0]): - return ((x[0], x[1], x[2]),) * n - elif len(x) != n: - ivy.utils.assertions.check_equal( - ivy.asarray(list(x)).shape, - (n, 3), - message=( - "tuple argument should contain " - "ndim groups where ndim is the number of " - "the input's dimensions" - ), - as_array=False, - ) - return x - - -def _check_tuple_arg(arg, name, force_integer=True): - is_scalar = ivy.isscalar if not force_integer else ivy.is_int_dtype - flag_assert = False - if isinstance(arg, (tuple, list)): - for nested in arg: - if isinstance(nested, (tuple, list)): - for sub_nested in nested: - if not is_scalar(sub_nested): - flag_assert = True - break - elif not is_scalar(nested): - flag_assert = True - elif not is_scalar(arg): - flag_assert = True - if flag_assert: - if not force_integer: - raise ivy.utils.exceptions.IvyException( - name + " should be scalar, tuple of scalars or tuple of scalar tuples" - ) - else: - raise ivy.utils.exceptions.IvyException( - name + " should be int, tuple of ints or tuple of int tuples" - ) - - -def _check_arguments( - mode, - pad_width, - stat_length, - constant_values, - end_values, - reflect_type, -): - ivy.utils.assertions.check_true( - callable(mode) - or mode - in [ - "constant", - "dilated", - "edge", - "linear_ramp", - "maximum", - "mean", - "median", - "minimum", - "reflect", - "symmetric", - "wrap", - "empty", - ], - message="the provided mode is not supported", - ) - _check_tuple_arg(pad_width, "pad_width") - if mode not in ["dilated"]: - ivy.utils.assertions.check_true( - all(element[1] >= 0 for element in ivy.ndenumerate(pad_width)), - message="the pad_widths must be greater or equal to zero", - ) - if mode in ["maximum", "mean", "median", "minimum"]: - _check_tuple_arg(stat_length, "stat_length") - ivy.utils.assertions.check_true( - all(element[1] > 0 for element in ivy.ndenumerate(stat_length)), - message="the stat lengths must be greater than zero", - ) - elif mode == "constant": - _check_tuple_arg(constant_values, "constant_values", force_integer=False) - elif mode == "linear_ramp": - _check_tuple_arg(end_values, "end_values", force_integer=False) - ivy.utils.assertions.check_true( - reflect_type in ["even", "odd"], - message="the provided reflect_type is not supported", - ) - - -@handle_exceptions -@handle_nestable -@handle_array_like_without_promotion -@inputs_to_ivy_arrays -@handle_array_function -def pad( - input: Union[ivy.Array, ivy.NativeArray], - pad_width: Union[Iterable[Tuple[int]], int], - /, - *, - mode: Union[ - Literal[ - "constant", - "dilated", - "edge", - "linear_ramp", - "maximum", - "mean", - "median", - "minimum", - "reflect", - "symmetric", - "wrap", - "empty", - ], - Callable, - ] = "constant", - stat_length: Union[Iterable[Tuple[int]], int] = 1, - constant_values: Union[Iterable[Tuple[Number]], Number] = 0, - end_values: Union[Iterable[Tuple[Number]], Number] = 0, - reflect_type: Literal["even", "odd"] = "even", - **kwargs: Optional[Any], -) -> ivy.Array: - """ - Pad an array. - - Parameters - ---------- - input - Input array to pad. - pad_width - Number of values padded to the edges of each axis. - - ((before_1, after_1), … (before_N, after_N)) yields unique pad widths - for each axis. - - ((before, after),) yields same before and after pad for each axis. - - pad (integer) is shortcut for before = after = pad width for all axes. - mode - One of the following string values or a user-supplied function. - - "constant": Pads with a constant value. - - "edge": Pads with the input's edge values. - - "linear_ramp": Pads with the linear ramp between end_value - and the input's edge value. - - "maximum": Pads with the maximum value of all or part of the vector - along each axis. - - "mean": Pads with the mean value of all or part of the vector along - each axis. - - "median": Pads with the median value of all or part of the vector - along each axis. - - "minimum": Pads with the minimum value of all or part of the vector - along each axis. - - "reflect": Pads with the reflection mirrored on the first and last - values of the vector along each axis. - - "symmetric": Pads with the reflection of the vector mirrored along - the edge of the input. - - "wrap": Pads with the wrap of the vector along the axis. - The first values are used to pad the end and the end values are used - to pad the beginning. - - "empty": Pads with undefined values. - - : Pads with a user-defined padding function. The padding - function should modify a rank 1 array following the signature - `padding_func(vector, iaxis_pad_width, iaxis, kwargs)`, where: - - `vector` is a rank 1 array already padded with zeros. Padded - values are `vector[:iaxis_pad_width[0]]` and - `vector[-iaxis_pad_width[1]:]`. - - `iaxis_pad_width` is a 2-tuple of ints, where - `iaxis_pad_width[0]` represents the number of values padded at - the beginning of `vector` and `iaxis_pad_width[1]` represents - the number of values padded at the end of `vector`. - - `iaxis` is the axis currently being calculated. - - `kwargs` is a dict of keyword arguments the function requires. - stat_length - Used in "maximum", "mean", "median", and "minimum". Number of values at edge - of each axis used to calculate the statistic value. - - ((before_1, after_1), … (before_N, after_N)) yields unique statistic - lengths for each axis. - - ((before, after),) yields same before and after statistic lengths for - each axis. - - stat_length (integer) is a shortcut for before = after = stat_length - length for all axes. - - None uses the entire axis. - constant_values - Used in "constant". The values to set the padded values for each axis. - - ((before_1, after_1), ... (before_N, after_N)) yields unique pad - constants for each axis. - - ((before, after),) yields same before and after constants for each axis. - - constant (integer) is a shortcut for before = after = constant for - all axes. - end_values - Used in "linear_ramp". The values used for the ending value of the linear_ramp - and that will form the edge of the padded array. - - ((before_1, after_1), ... (before_N, after_N)) yields unique end values - for each axis. - - ((before, after),) yields same before and after end values for each axis - - end (integer) is a shortcut for before = after = end for all axes. - reflect_type - Used in "reflect", and "symmetric". The "even" style is the default with an - unaltered reflection around the edge value. For the "odd" style, the extended - part of the array is created by subtracting the reflected values from two - times the edge value. - - Returns - ------- - ret - Padded array of the same rank as the input but with shape increased according - to pad_width. - - - Both the description and the type hints above assume an array input for simplicity, - but this function is *nestable*, and therefore also accepts :class:`ivy.Container` - instances in place of any of the arguments. - - Examples - -------- - With :class:`ivy.Array` input: - - >>> x = ivy.array([[1, 2, 3], [4, 5, 6]]) - >>> padding = ((1, 1), (2, 2)) - >>> y = ivy.pad(x, padding, mode="constant", constant_values=0) - >>> print(y) - ivy.array([[0, 0, 0, 0, 0, 0, 0], - [0, 0, 1, 2, 3, 0, 0], - [0, 0, 4, 5, 6, 0, 0], - [0, 0, 0, 0, 0, 0, 0]]) - - >>> x = ivy.array([[1, 2, 3], [4, 5, 6]]) - >>> padding = ((1, 1), (2, 2)) - >>> y = ivy.pad(x, padding, mode="reflect") - >>> print(y) - ivy.array([[6, 5, 4, 5, 6, 5, 4], - [3, 2, 1, 2, 3, 2, 1], - [6, 5, 4, 5, 6, 5, 4], - [3, 2, 1, 2, 3, 2, 1]]) - - >>> x = ivy.array([[1, 2, 3], [4, 5, 6]]) - >>> padding = ((1, 1), (2, 2)) - >>> y = ivy.pad(x, padding, mode="symmetric") - >>> print(y) - ivy.array([[2, 1, 1, 2, 3, 3, 2], - [2, 1, 1, 2, 3, 3, 2], - [5, 4, 4, 5, 6, 6, 5], - [5, 4, 4, 5, 6, 6, 5]]) - - With :class:`ivy.NativeArray` input: - - >>> x = ivy.native_array([[1, 2, 3], [4, 5, 6]]) - >>> padding = ((1, 1), (2, 2)) - >>> y = ivy.pad(x, padding, mode="constant", constant_values=7) - >>> print(y) - ivy.array([[7, 7, 7, 7, 7, 7, 7], - [7, 7, 1, 2, 3, 7, 7], - [7, 7, 4, 5, 6, 7, 7], - [7, 7, 7, 7, 7, 7, 7]]) - - With :class:`ivy.Container` input: - - >>> x = ivy.Container(a=ivy.array([0, 1, 2]), b=ivy.array([4, 5, 6])) - >>> padding = (1, 1) - >>> y = ivy.pad(x, padding, mode="constant") - >>> print(y) - { - a: ivy.array([0, 0, 1, 2, 0]), - b: ivy.array([0, 4, 5, 6, 0]) - } - """ - _check_arguments( - mode, - pad_width, - stat_length, - constant_values, - end_values, - reflect_type, - ) - if mode == "dilated": - pad_width = _to_dilated(pad_width, input.ndim) - if ivy.as_ivy_dtype(type(constant_values)) != input.dtype: - padding_value = ivy.native_array(constant_values, dtype=input.dtype) - else: - padding_value = constant_values - padded = _interior_pad(input, padding_value, pad_width) - return padded - pad_width = _to_pairs(pad_width, len(input.shape)) - if callable(mode): - func = mode - padded, _ = _pad_simple(input, pad_width, fill_value=0) - for axis in range(padded.ndim): - padded = ivy.moveaxis(padded, axis, -1) - inds = ivy.ndindex(padded.shape[:-1]) - for ind in inds: - padded[ind] = func(padded[ind], pad_width[axis], axis, kwargs) - return padded - padded, original_area_slice = _pad_simple(input, pad_width) - axes = range(padded.ndim) - stat_functions = { - "maximum": ivy.max, - "minimum": ivy.min, - "mean": ivy.mean, - "median": ivy.median, - } - if mode == "constant": - constant_values = _to_pairs(constant_values, padded.ndim) - constant_values = tuple(tuple(map(ivy.array, pair)) for pair in constant_values) - for axis, width_pair, value_pair in zip(axes, pad_width, constant_values): - padded = _set_pad_area(padded, axis, width_pair, value_pair) - elif mode == "empty": - pass - elif mode == "edge": - for axis, width_pair in zip(axes, pad_width): - edge_pair = _get_edges(padded, axis, width_pair) - padded = _set_pad_area(padded, axis, width_pair, edge_pair) - elif mode == "linear_ramp": - end_values = _to_pairs(end_values, padded.ndim) - for axis, width_pair, value_pair in zip(axes, pad_width, end_values): - ramp_pair = _get_linear_ramps(padded, axis, width_pair, value_pair) - padded = _set_pad_area(padded, axis, width_pair, ramp_pair) - elif mode in stat_functions: - func = stat_functions[mode] - stat_length = _to_pairs(stat_length, padded.ndim) - if mode == "median": - ivy.utils.assertions.check_true( - ivy.is_float_dtype(input), - message="median interpolation is only supported for floats", - ) - for axis, width_pair, length_pair in zip(axes, pad_width, stat_length): - stat_pair = _get_stats(padded, axis, width_pair, length_pair, func) - padded = _set_pad_area(padded, axis, width_pair, stat_pair) - elif mode in {"reflect", "symmetric"}: - include_edge = True if mode == "symmetric" else False - for axis, (left_index, right_index) in zip(axes, pad_width): - if input.shape[axis] == 1 and (left_index > 0 or right_index > 0): - edge_pair = _get_edges(padded, axis, (left_index, right_index)) - padded = _set_pad_area( - padded, axis, (left_index, right_index), edge_pair - ) - continue - while left_index > 0 or right_index > 0: - left_index, right_index, padded = _set_reflect_both( - padded, axis, (left_index, right_index), reflect_type, include_edge - ) - elif mode == "wrap": - for axis, (left_index, right_index) in zip(axes, pad_width): - while left_index > 0 or right_index > 0: - left_index, right_index, padded = _set_wrap_both( - padded, axis, (left_index, right_index) - ) - return padded - - -pad.mixed_backend_wrappers = { - "to_add": ( - "inputs_to_native_arrays", - "outputs_to_ivy_arrays", - "handle_device_shifting", - ), - "to_skip": ("inputs_to_ivy_arrays",), -} - - -@handle_exceptions -@handle_nestable -@handle_array_like_without_promotion -@handle_view -@to_native_arrays_and_back -@handle_array_function -@handle_device_shifting -def vsplit( - ary: Union[ivy.Array, ivy.NativeArray], - indices_or_sections: Union[int, Sequence[int], ivy.Array, ivy.NativeArray], - /, - *, - copy: Optional[bool] = None, -) -> List[ivy.Array]: - """ - Split an array vertically into multiple sub-arrays. - - Parameters - ---------- - ary - Array input. - indices_or_sections - If indices_or_sections is an integer n, the array is split into n - equal sections, provided that n must be a divisor of the split axis. - If indices_or_sections is a sequence of ints or 1-D array, - then input is split at each of the indices. - copy - boolean indicating whether to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - - Returns - ------- - ret - input array split vertically. - - Examples - -------- - >>> ary = ivy.array( - [[[0., 1.], - [2., 3.]], - [[4., 5.], - [6., 7.]]] - ) - >>> ivy.vsplit(ary, 2) - [ivy.array([[[0., 1.], [2., 3.]]]), ivy.array([[[4., 5.], [6., 7.]]])]) - """ - return ivy.current_backend(ary).vsplit(ary, indices_or_sections, copy=copy) - - -@handle_exceptions -@handle_nestable -@handle_array_like_without_promotion -@handle_view -@to_native_arrays_and_back -@handle_device_shifting -def dsplit( - ary: Union[ivy.Array, ivy.NativeArray], - indices_or_sections: Union[int, Sequence[int], ivy.Array, ivy.NativeArray], - /, - *, - copy: Optional[bool] = None, -) -> List[ivy.Array]: - """ - Split an array into multiple sub-arrays along the 3rd axis. - - Parameters - ---------- - ary - Array input. - indices_or_sections - If indices_or_sections is an integer n, the array is split into n sections. - If the array is divisible by n along the 3rd axis, each section will be of - equal size. If input is not divisible by n, the sizes of the first - int(ary.size(0) % n) sections will have size int(ary.size(0) / n) + 1, and - the rest will have size int(ary.size(0) / n). - If indices_or_sections is a sequence of ints or 1-D array, - then input is split at each of the indices. - copy - boolean indicating whether to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - - Returns - ------- - ret - input array split along the 3rd axis. - - Examples - -------- - >>> ary = ivy.array( - [[[ 0., 1., 2., 3.], - [ 4., 5., 6., 7.]], - [[ 8., 9., 10., 11.], - [12., 13., 14., 15.]]] - ) - >>> ivy.dsplit(ary, 2) - [ivy.array([[[ 0., 1.], [ 4., 5.]], [[ 8., 9.], [12., 13.]]]), - ivy.array([[[ 2., 3.], [ 6., 7.]], [[10., 11.], [14., 15.]]])] - """ - return ivy.current_backend(ary).dsplit(ary, indices_or_sections, copy=copy) - - -@handle_nestable -@handle_array_like_without_promotion -@handle_view -@to_native_arrays_and_back -@handle_device_shifting -def atleast_1d( - *arys: Union[ivy.Array, ivy.NativeArray, bool, Number], - copy: Optional[bool] = None, -) -> List[ivy.Array]: - """ - Convert inputs to arrays with at least one dimension. Scalar inputs are converted to - 1-dimensional arrays, whilst higher-dimensional inputs are preserved. - - Parameters - ---------- - arys - One or more input arrays. - copy - boolean indicating whether to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - - Returns - ------- - ret - An array, or list of arrays, each with atleast 1D. - Copies are made only if necessary. - - Examples - -------- - >>> ary1 = ivy.array(5) - >>> ivy.atleast_1d(ary1) - ivy.array([5]) - >>> ary2 = ivy.array([[3,4]]) - >>> ivy.atleast_1d(ary2) - ivy.array([[3, 4]]) - >>> ivy.atleast_1d(6,7,8) - [ivy.array([6]), ivy.array([7]), ivy.array([8])] - """ - return ivy.current_backend().atleast_1d(*arys, copy=copy) - - -@handle_nestable -@handle_out_argument -@to_native_arrays_and_back -@handle_device_shifting -def dstack( - arrays: Sequence[ivy.Array], - /, - *, - out: Optional[ivy.Array] = None, -) -> ivy.Array: - """ - Stack arrays in sequence depth wise (along third axis). - - Parameters - ---------- - arrays - Sequence of arrays to be stacked. - - Returns - ------- - ret - The array formed by stacking the given arrays. - - Examples - -------- - >>> x = ivy.array([1, 2, 3]) - >>> y = ivy.array([2, 3, 4]) - >>> ivy.dstack((x, y)) - ivy.array([[[1, 2], - [2, 3], - [3, 4]]]) - >>> x = ivy.array([[1], [2], [3]]) - >>> y = ivy.array([[2], [3], [4]]) - >>> ivy.dstack((x, y)) - ivy.array([[[1, 2]], - [[2, 3]], - [[3, 4]]]) - """ - return ivy.current_backend().dstack(arrays, out=out) - - -@handle_nestable -@handle_array_like_without_promotion -@handle_view -@to_native_arrays_and_back -@handle_device_shifting -def atleast_2d( - *arys: Union[ivy.Array, ivy.NativeArray], - copy: Optional[bool] = None, -) -> List[ivy.Array]: - """ - Convert inputs to arrays with at least two dimension. Scalar inputs are converted to - 2-dimensional arrays, whilst higher-dimensional inputs are preserved. - - Parameters - ---------- - arys - One or more array-like sequences. Non-array inputs are - converted to arrays. Arrays that already have two or more - dimensions are preserved. - copy - boolean indicating whether to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - - Returns - ------- - ret - An array, or list of arrays, each with atleast 2D. - Copies are made only if necessary. - - Examples - -------- - >>> ary1 = ivy.array(5) - >>> ivy.atleast_2d(ary1) - ivy.array([[5]]) - >>> ary2 = ivy.array([[[3,4]]]) - >>> ivy.atleast_2d(ary2) - ivy.array([[[3, 4]]]) - >>> ivy.atleast_2d(6,7,8) - [ivy.array([[6]]), ivy.array([[7]]), ivy.array([[8]])] - """ - return ivy.current_backend().atleast_2d(*arys, copy=copy) - - -@handle_nestable -@handle_array_like_without_promotion -@handle_view -@to_native_arrays_and_back -@handle_device_shifting -def atleast_3d( - *arys: Union[ivy.Array, ivy.NativeArray, bool, Number], - copy: Optional[bool] = None, -) -> List[ivy.Array]: - """ - Convert inputs to arrays with at least three dimension. Scalar inputs are converted - to 3-dimensional arrays, whilst higher-dimensional inputs are preserved. - - Parameters - ---------- - arys - One or more array-like sequences. Non-array inputs are - converted to arrays. Arrays that already have three or more - dimensions are preserved. - copy - boolean indicating whether to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - - Returns - ------- - ret - An array, or list of arrays, each with a.ndim >= 3. Copies - are avoided where possible, and views with three or more - dimensions are returned. For example, a 1-D array of shape - (N,) becomes a view of shape (1, N, 1), and a 2-D array of - shape (M, N) becomes a view of shape (M, N, 1). - - Examples - -------- - >>> ary1 = ivy.array([5,6]) - >>> ivy.atleast_3d(ary1) - ivy.array([[[5], - [6]]]) - >>> ary2 = ivy.array([[[3,4]]]) - >>> ivy.atleast_3d(ary2) - ivy.array([[[3, 4]]]) - >>> ary3 = ivy.array([[3,4],[9,10]]) - >>> ivy.atleast_3d(6,7,ary3) - [ivy.array([[[6]]]), ivy.array([[[7]]]), ivy.array([[[ 3], - [ 4]], - - [[ 9], - [10]]])] - """ - return ivy.current_backend().atleast_3d(*arys, copy=copy) - - -@handle_exceptions -@handle_nestable -@handle_array_like_without_promotion -@handle_out_argument -@to_native_arrays_and_back -@handle_device_shifting -def take_along_axis( - arr: Union[ivy.Array, ivy.NativeArray], - indices: Union[ivy.Array, ivy.NativeArray], - axis: int, - /, - *, - mode: str = "fill", - out: Optional[ivy.Array] = None, -) -> ivy.Array: - """ - Take values from the input array by matching 1d index and data slices. - - Parameters - ---------- - arr - The source array. - indices - The indices of the values to extract. - axis - The axis over which to select values. - If axis is None, arr is treated as a flattened 1D array. - mode - One of: 'clip', 'fill', 'drop'. Parameter controlling how out-of-bounds indices - will be handled. - out - The output array. - - Returns - ------- - ret - The returned array has the same shape as `indices`. - - Examples - -------- - >>> arr = ivy.array([[4, 3, 5], [1, 2, 1]]) - >>> indices = ivy.array([[0, 1, 1], [2, 0, 0]]) - >>> y = ivy.take_along_axis(arr, indices, 1) - >>> print(y) - ivy.array([[4, 3, 3], [1, 1, 1]]) - """ - return ivy.current_backend(arr).take_along_axis( - arr, indices, axis, mode=mode, out=out - ) - - -@handle_exceptions -@handle_nestable -@handle_array_like_without_promotion -@handle_view -@to_native_arrays_and_back -@handle_array_function -@handle_device_shifting -def hsplit( - ary: Union[ivy.Array, ivy.NativeArray], - indices_or_sections: Union[int, Sequence[int], ivy.Array, ivy.NativeArray], - /, - *, - copy: Optional[bool] = None, -) -> List[ivy.Array]: - """ - Split an array into multiple sub-arrays horizontally. - - Parameters - ---------- - ary - Array input. - indices_or_sections - If indices_or_sections is an integer n, the array is split into n - equal sections, provided that n must be a divisor of the split axis. - If indices_or_sections is a tuple of ints, then input is split at each of - the indices in the tuple. - copy - boolean indicating whether to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - - Returns - ------- - ret - input array split horizontally. - - Examples - -------- - >>> ary = ivy.array( - [[0., 1., 2., 3.], - [4., 5., 6, 7.], - [8., 9., 10., 11.], - [12., 13., 14., 15.]] - ) - >>> ivy.hsplit(ary, 2) - [ivy.array([[ 0., 1.], - [ 4., 5.], - [ 8., 9.], - [12., 13.]]), - ivy.array([[ 2., 3.], - [ 6., 7.], - [10., 11.], - [14., 15.]])] - """ - return ivy.current_backend(ary).hsplit(ary, indices_or_sections, copy=copy) - - -@handle_exceptions -@inputs_to_native_shapes -def broadcast_shapes(*shapes: Union[List[int], List[Tuple]]) -> Tuple[int]: - """ - Broadcasts shapes. - - Parameters - ---------- - shapes - The shapes to broadcast. - - Returns - ------- - ret - The broadcasted shape. - - Examples - -------- - >>> x = [(3, 3), (3, 1)] - >>> print(ivy.broadcast_shapes(x)) - (3, 3) - - >>> print(ivy.broadcast_shapes([(3, 3),(3, 1),(1, 3)])) - (3, 3) - """ - return ivy.current_backend().broadcast_shapes(*shapes) - - -@handle_exceptions -@handle_nestable -@handle_array_like_without_promotion -@handle_view -@handle_out_argument -@inputs_to_native_shapes -@to_native_arrays_and_back -@handle_device_shifting -def expand( - x: Union[ivy.Array, ivy.NativeArray], - shape: Union[ivy.Shape, ivy.NativeShape], - /, - *, - copy: Optional[bool] = None, - out: Optional[ivy.Array] = None, -) -> ivy.Array: - """ - Broadcast the input Array following the given shape and the broadcast rule. - - Parameters - ---------- - x - Array input. - shape - A 1-D Array indicates the shape you want to expand to, - following the broadcast rule. - copy - boolean indicating whether to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - out - optional output array, for writing the result to. - - Returns - ------- - ret - Output Array - """ - return ivy.current_backend(x).expand(x, shape, out=out, copy=copy) - - -@handle_nestable -@handle_exceptions -@handle_array_like_without_promotion -@inputs_to_ivy_arrays -def put_along_axis( - arr: Union[ivy.Array, ivy.NativeArray], - indices: Union[ivy.Array, ivy.NativeArray], - values: Union[ivy.Array, ivy.NativeArray], - axis: int, - /, - *, - mode: str = "raise", - out: Optional[ivy.Array] = None, -) -> None: - """ - Put values into the input array by matching 1d index and data slices along a - specified axis. - - Parameters - ---------- - arr : array_like - The input array to modify. - indices : array_like - The indices of the values to put into `arr`. - values : array_like - The values to put into `arr`. - axis : int - The axis over which to put the `values`. - mode : {'raise', 'wrap', 'clip'}, optional - Specifies how out-of-bounds indices will be handled. - The following modes are available: - - - 'raise': a `ValueError` is raised when an index is out of bounds. - - 'wrap': the index is wrapped around to the corresponding index - at the other end of the axis. - - 'clip': the index is clipped to the closest in-bounds index. - out : ndarray, optional - Output array in which to place the result. - If not specified, a new array is created. - - Returns - ------- - None - - Examples - -------- - >>> arr = ivy.array([[4, 3, 5], [1, 2, 1]]) - >>> indices = ivy.array([[0, 1, 1], [2, 0, 0]]) - >>> values = ivy.array([[9, 8, 7], [6, 5, 4]]) - >>> put_along_axis(arr, indices, values, 1, mode='clip') - >>> print(arr) - ivy.array([[3, 7, 5], - [6, 4, 1]]) - """ - if out is None: - out = ivy.zeros_like(arr) - - indices = ivy.expand_dims(indices, axis=axis) - values = ivy.expand_dims(values, axis=axis) - - stacked = ivy.concat((arr, values), axis=axis) - - sorted_indices = ivy.argsort(indices, axis=axis) - sorted_stacked = ivy.take_along_axis(stacked, sorted_indices, axis=axis) - - arr = ivy.where( - ivy.expand_dims(sorted_indices < arr.shape[axis], axis=axis), - sorted_stacked, - arr, - ) - - if mode == "clip": - indices = ivy.clip(indices, 0, arr.shape[axis] - 1) - elif mode == "wrap": - indices = ivy.mod(indices, arr.shape[axis]) - - arr = ivy.where( - ivy.expand_dims(sorted_indices < arr.shape[axis], axis=axis), arr, values - ) - - ivy.assign(out, arr) - - -def _check_bounds(shape0, shape1, strides1, itemsize): - numel0 = math.prod(shape0) - ndim1 = len(shape1) - return ( - sum((shape1[i] - 1) * strides1[i] for i in range(ndim1)) + itemsize - <= numel0 * itemsize - ) - - -@handle_exceptions -@handle_nestable -@handle_array_like_without_promotion -@inputs_to_native_shapes -@inputs_to_ivy_arrays -def as_strided( - x: Union[ivy.Array, ivy.NativeArray], - shape: Union[ivy.Shape, ivy.NativeShape, Sequence[int]], - strides: Sequence[int], - /, -) -> ivy.Array: - """ - Create a copy of the input array with the given shape and strides. - - Parameters - ---------- - x - Input Array. - shape - The shape of the new array. - strides - The strides of the new array (specified in bytes). - - Returns - ------- - ret - Output Array - - Examples - -------- - >>> x = ivy.array([1, 2, 3, 4, 5, 6]) - >>> ivy.as_strided(x, (4, 3), (8, 8)) - ivy.array([[1, 2, 3], - [2, 3, 4], - [3, 4, 5], - [4, 5, 6]]) - """ - itemsize = x.itemsize - if not _check_bounds(x.shape, shape, strides, itemsize): - raise ivy.exceptions.IvyException("attempted unsafe memory access") - if any(strides[i] % itemsize != 0 for i in range(len(strides))): - raise ivy.exceptions.IvyException("strides must be multiple of itemsize") - - src = memoryview(ivy.to_numpy(x)).cast("b") - - src_ind = ivy.inner( - ivy.indices(shape).reshape((len(shape), -1)).T, - ivy.array(strides), - ) - src_ind = ivy.expand_dims(src_ind, axis=-1) - src_ind = src_ind + ivy.arange(itemsize) - src_ind = ivy.reshape(src_ind, (-1,)).to_numpy() - - temp_list = [src[i] for i in src_ind] - temp_array = ivy.asarray(temp_list, dtype=ivy.int8) - result = bytearray(temp_array.to_numpy()) - - return ivy.reshape( - ivy.frombuffer(result, dtype=x.dtype, count=math.prod(shape)), - shape, - ) - - -as_strided.unsupported_dtypes = ("bfloat16",) -as_strided.mixed_backend_wrappers = { - "to_add": ( - "inputs_to_native_arrays", - "outputs_to_ivy_arrays", - "handle_device_shifting", - ), - "to_skip": ("inputs_to_ivy_arrays",), -} - - -@handle_exceptions -@handle_nestable -@handle_out_argument -@to_native_arrays_and_back -@handle_array_function -@handle_device_shifting -def concat_from_sequence( - input_sequence: Union[ - Tuple[Union[ivy.Array, ivy.NativeArray]], - List[Union[ivy.Array, ivy.NativeArray]], - ], - /, - *, - new_axis: int = 0, - axis: int = 0, - out: Optional[ivy.Array] = None, -) -> ivy.Array: - """ - Concatenate a sequence of arrays along a new or an existing axis. - - Parameters - ---------- - input_sequence - A sequence of arrays. - new_axis - Insert and concatenate on a new axis or not, - default 0 means do not insert new axis. - new_axis = 0: concatenate - new_axis = 1: stack - axis - axis along which the arrays will be concatenated. - - out - optional output array, for writing the result to. - - Returns - ------- - ret - Output Array - """ - return current_backend(input_sequence).concat_from_sequence( - input_sequence, new_axis=new_axis, axis=axis, out=out - ) - - -def _slice(operand, start_indices, limit_indices, strides=None): - strides = [1] * len(operand.shape) if strides is None else strides - - full_slice = () - for i, _ in enumerate(operand.shape): - strides_i = int(strides[i]) - start_i = int(start_indices[i]) - limit_i = int(limit_indices[i]) - full_slice += (slice(start_i, limit_i, strides_i),) - return operand[full_slice] - - -def _slice_along_axis(x, start=0, stop=None, stride=1, axis=0): - if axis >= 0: - slices = [slice(None)] * axis + [slice(start, stop, stride)] - else: - slices = [Ellipsis, slice(start, stop, stride)] + [slice(None)] * (-1 - axis) - return x[tuple(slices)] - - -def _interior_pad(operand, padding_value, padding_config): - for axis, (_, _, interior) in enumerate(padding_config): - if interior > 0: - new_shape = list(operand.shape) - new_shape[axis] = new_shape[axis] + (new_shape[axis] - 1) * interior - new_array = ivy.full(new_shape, padding_value) - src_indices = ivy.arange(operand.shape[axis]) - dst_indices = src_indices * (interior + 1) - index_tuple = [slice(None)] * operand.ndim - index_tuple[axis] = dst_indices - new_array[tuple(index_tuple)] = operand - operand = new_array - - start_indices = [0] * operand.ndim - limit_indices = [0] * operand.ndim - for axis, (low, high, _) in enumerate(padding_config): - if low < 0: - start_indices[axis] = abs(low) - if high < 0: - limit_indices[axis] = high - else: - limit_indices[axis] = operand.shape[axis] + 1 - padded = _slice(operand, start_indices, limit_indices) - - pad_width = [(0, 0)] * operand.ndim - for axis, (low, high, _) in enumerate(padding_config): - if low > 0 and high > 0: - pad_width[axis] = (low, high) - elif low > 0 and not high > 0: - pad_width[axis] = (low, 0) - elif high > 0 and not low > 0: - pad_width[axis] = (0, high) - padded = ivy.constant_pad(padded, pad_width, value=padding_value) - return padded - - -def _interleave(a, b, axis): - assert a.shape[axis] == b.shape[axis] or a.shape[axis] == b.shape[axis] + 1 - a_pad = [(0, 0, 0)] * a.ndim - b_pad = [(0, 0, 0)] * b.ndim - a_pad[axis] = (0, 1 if a.shape[axis] == b.shape[axis] else 0, 1) - b_pad[axis] = (1, 0 if a.shape[axis] == b.shape[axis] else 1, 1) - a = _interior_pad(a, 0.0, a_pad) - b = _interior_pad(b, 0.0, b_pad) - return ivy.add(a, b) - - -@handle_exceptions -@handle_nestable -@inputs_to_ivy_arrays -@handle_array_function -def associative_scan( - x: Union[ivy.Array, ivy.NativeArray], - fn: Callable, - /, - *, - reverse: bool = False, - axis: int = 0, -) -> ivy.Array: - """ - Perform an associative scan over the given array. - - Parameters - ---------- - x - The array to scan over. - fn - The associative function to apply. - reverse - Whether to scan in reverse with respect to the given axis. - axis - The axis to scan over. - - Returns - ------- - ret - The result of the scan. - """ - elems = [x] - - if reverse: - elems = [ivy.flip(elem, axis=[axis]) for elem in elems] - - def _combine(a, b): - a = a[0] - b = b[0] - if a.shape[axis] == 0: - return [a] - c = fn(a, b) - return [c] - - def _scan(elems): - num_elems = elems[0].shape[axis] - - if num_elems < 2: - return elems - - reduced_elems = _combine( - [_slice_along_axis(elem, 0, -1, stride=2, axis=axis) for elem in elems], - [_slice_along_axis(elem, 1, None, stride=2, axis=axis) for elem in elems], - ) - - odd_elems = _scan(reduced_elems) - - if num_elems % 2 == 0: - even_elems = _combine( - [_slice_along_axis(e, 0, -1, axis=axis) for e in odd_elems], - [_slice_along_axis(e, 2, None, stride=2, axis=axis) for e in elems], - ) - else: - even_elems = _combine( - odd_elems, - [_slice_along_axis(e, 2, None, stride=2, axis=axis) for e in elems], - ) - even_elems = [ - ivy.concat([_slice_along_axis(elem, 0, 1, axis=axis), result], axis=axis) - for (elem, result) in zip(elems, even_elems) - ] - return list(map(partial(_interleave, axis=axis), even_elems, odd_elems)) - - scans = _scan(elems) - - if reverse: - scans = [ivy.flip(scanned, axis=[axis]) for scanned in scans] - - return ivy.reshape(ivy.asarray(scans), elems[0].shape) - - -@handle_exceptions -@handle_nestable -@handle_array_like_without_promotion -@to_native_arrays_and_back -@handle_array_function -@handle_device_shifting -def unique_consecutive( - x: Union[ivy.Array, ivy.NativeArray], - /, - *, - axis: Optional[int] = None, -) -> Tuple[ - Union[ivy.Array, ivy.NativeArray], - Union[ivy.Array, ivy.NativeArray], - Union[ivy.Array, ivy.NativeArray], -]: - """Eliminates all but the first element from every consecutive group of equivalent - elements in ``x``. - - Parameters - ---------- - x - input array. - - axis - the axis to apply unique on. If None, unique is applied on flattened ``x``. - - Returns - ------- - ret - a namedtuple ``(output, inverse_indices, counts)`` whose - - first element has the field name ``output`` and is an array - containing ``x`` with its equivalent consecutive elements eliminated. - - second element has the field name ``inverse_indices`` and is an - array containing the indices of ``output`` that reconstruct ``x``. - - third element has the field name ``counts`` and is an array - containing the number of occurrences for each unique value or array in ``x``. - - - Examples - -------- - With :class:`ivy.Array` input: - >>> x = ivy.array([1, 1, 2, 2, 3, 1, 1, 2]) - >>> ivy..unique_consecutive(x) - Results(values=ivy.array([1, 2, 3, 1, 2]), - inverse_indices=ivy.array([0, 0, 1, 1, 2, 3, 3, 4]), - counts=ivy.array([2, 2, 1, 2, 1])) - """ - return ivy.current_backend(x).unique_consecutive(x, axis=axis) - - -@handle_exceptions -@handle_nestable -@handle_array_like_without_promotion -@to_native_arrays_and_back -@handle_array_function -def fill_diagonal( - a: Union[ivy.Array, ivy.NativeArray], - v: Union[int, float], - /, - *, - wrap: bool = False, -) -> Union[ivy.Array, ivy.NativeArray]: - """ - Fill the main diagonal of the given array of any dimensionality.. - - Parameters - ---------- - a - Array at least 2D. - v - The value to write on the diagonal. - wrap - The diagonal ‘wrapped’ after N columns for tall matrices. - - Returns - ------- - ret - Array with the diagonal filled. - """ - return ivy.current_backend(a).fill_diag(a, v, wrap=wrap) - - -def tensor_ops(tensor, operation_type, *args): - if operation_type == "index": - if len(args) != 1: - raise ValueError - index = args[0] - return tensor[index] - elif operation_type == "slice": - if len(args) != 2: - raise ValueError - start, end = args - return tensor[start:end] - elif operation_type == "join": - if len(args) != 1: - raise ValueError - other_tensor = args[0] - return ivy.cat((tensor, other_tensor), dim=0) - elif operation_type == "mutate": - if len(args) != 1: - raise ValueError - factor = args[0] - tensor.mul_(factor) - return tensor - else: - raise ValueError From 8ffe7657066981db7ab8db1f38fdc5e07ac51f34 Mon Sep 17 00:00:00 2001 From: Sanjay <121057369+Sanjay8602@users.noreply.github.com> Date: Wed, 18 Oct 2023 00:43:21 +0530 Subject: [PATCH 22/37] Delete ivy/functional/frontends/torch/indexing_slicing_joining_mutating_ops.py --- .../indexing_slicing_joining_mutating_ops.py | 413 ------------------ 1 file changed, 413 deletions(-) delete mode 100644 ivy/functional/frontends/torch/indexing_slicing_joining_mutating_ops.py diff --git a/ivy/functional/frontends/torch/indexing_slicing_joining_mutating_ops.py b/ivy/functional/frontends/torch/indexing_slicing_joining_mutating_ops.py deleted file mode 100644 index 89be00ef0dbcd..0000000000000 --- a/ivy/functional/frontends/torch/indexing_slicing_joining_mutating_ops.py +++ /dev/null @@ -1,413 +0,0 @@ -# local -import ivy -from ivy.functional.frontends.torch.func_wrapper import ( - to_ivy_arrays_and_back, - numpy_to_torch_style_args, - to_ivy_shape, -) - - -@to_ivy_arrays_and_back -def adjoint(input): - return ivy.adjoint(input) - - -@to_ivy_arrays_and_back -def cat(tensors, dim=0, *, out=None): - return ivy.concat(tensors, axis=dim, out=out) - - -@to_ivy_arrays_and_back -def chunk(input, chunks, dim=0): - if ivy.shape(input) == (): - return [input] - else: - dim_size = ivy.shape(input)[dim] - chunk_size = dim_size // chunks - if chunk_size == 0: - return ivy.split(input, num_or_size_splits=dim_size, axis=dim) - else: - remainder = dim_size % chunks - if remainder == 0: - return ivy.split(input, num_or_size_splits=chunks, axis=dim) - else: - return ivy.split( - input, - num_or_size_splits=tuple( - [chunk_size + remainder] + [chunk_size] * (chunks - 1) - ), - axis=dim, - ) - - -@to_ivy_arrays_and_back -def concat(tensors, dim=0, *, out=None): - return ivy.concat(tensors, axis=dim, out=out) - - -@to_ivy_arrays_and_back -def gather(input, dim, index, *, sparse_grad=False, out=None): - if sparse_grad: - raise ivy.utils.exceptions.IvyException( - "Gather does not yet support the sparse grad functionality" - ) - - dim = dim % len(input.shape) - all_indices = ivy.argwhere(ivy.full(index.shape, True)) - gather_locations = ivy.reshape(index, [ivy.prod(ivy.array(index.shape))]) - - gather_indices = [] - for axis in range(len(index.shape)): - if axis == dim: - gather_indices.append(ivy.array(gather_locations, dtype=index.dtype)) - else: - gather_indices.append(ivy.array(all_indices[:, axis], dtype=index.dtype)) - - gather_indices = ivy.stack(gather_indices, axis=-1) - gathered = ivy.gather_nd(input, gather_indices) - reshaped = ivy.reshape(gathered, index.shape) - return reshaped - - -@to_ivy_arrays_and_back -def nonzero(input, *, out=None, as_tuple=False): - ret = ivy.nonzero(input) - if as_tuple is False: - ret = ivy.matrix_transpose(ivy.stack(ret)) - - if ivy.exists(out): - return ivy.inplace_update(out, ret) - return ret - - -@to_ivy_arrays_and_back -def permute(input, dims): - return ivy.permute_dims(input, axes=dims) - - -@to_ivy_shape -@to_ivy_arrays_and_back -def reshape(input, shape): - return ivy.reshape(input, shape) - - -@numpy_to_torch_style_args -@to_ivy_arrays_and_back -def squeeze(input, dim=None): - if isinstance(dim, int) and input.ndim > 0: - if input.shape[dim] > 1: - return input - return ivy.squeeze(input, axis=dim) - - -@to_ivy_arrays_and_back -def stack(tensors, dim=0, *, out=None): - return ivy.stack(tensors, axis=dim, out=out) - - -@to_ivy_arrays_and_back -def swapaxes(input, axis0, axis1): - return ivy.swapaxes(input, axis0, axis1) - - -@to_ivy_arrays_and_back -def swapdims(input, dim0, dim1): - return ivy.swapaxes(input, dim0, dim1) - - -@to_ivy_arrays_and_back -def transpose(input, dim0, dim1): - return ivy.swapaxes(input, dim0, dim1) - - -@to_ivy_arrays_and_back -def t(input): - if input.ndim > 2: - raise ivy.utils.exceptions.IvyException( - "t(input) expects a tensor with <= 2 dimensions, but self is %dD" - % input.ndim - ) - if input.ndim == 2: - return ivy.swapaxes(input, 0, 1) - else: - return input - - -@to_ivy_arrays_and_back -def tile(input, dims): - try: - tup = tuple(dims) - except TypeError: - tup = (dims,) - d = len(tup) - res = 0 - if len(input.shape) > len([dims]) - 1: - res = input - if d < input.ndim: - tup = (1,) * (input.ndim - d) + tup - res = ivy.tile(input, tup) - - else: - res = ivy.tile(input, repeats=dims, out=None) - return res - - -@to_ivy_arrays_and_back -def unsqueeze(input, dim=0): - return ivy.expand_dims(input, axis=dim) - - -@to_ivy_arrays_and_back -def argwhere(input): - return ivy.argwhere(input) - - -@to_ivy_arrays_and_back -def movedim(input, source, destination): - return ivy.moveaxis(input, source, destination) - - -@to_ivy_arrays_and_back -def moveaxis(input, source, destination): - return ivy.moveaxis(input, source, destination) - - -@to_ivy_arrays_and_back -def hstack(tensors, *, out=None): - return ivy.hstack(tensors, out=out) - - -@to_ivy_arrays_and_back -def index_select(input, dim, index, *, out=None): - return ivy.gather(input, index, axis=dim, out=out) - - -@to_ivy_arrays_and_back -def dstack(tensors, *, out=None): - return ivy.dstack(tensors, out=out) - - -@to_ivy_arrays_and_back -def take_along_dim(input, indices, dim, *, out=None): - return ivy.take_along_axis(input, indices, dim, out=out) - - -@to_ivy_arrays_and_back -def vstack(tensors, *, out=None): - return ivy.vstack(tensors, out=out) - - -@to_ivy_arrays_and_back -def split(tensor, split_size_or_sections, dim=0): - if isinstance(split_size_or_sections, int): - split_size = split_size_or_sections - split_size_or_sections = [split_size] * (tensor.shape[dim] // split_size) - if tensor.shape[dim] % split_size: - split_size_or_sections.append(tensor.shape[dim] % split_size) - return tuple( - ivy.split( - tensor, - num_or_size_splits=split_size_or_sections, - axis=dim, - with_remainder=True, - ) - ) - - -@to_ivy_arrays_and_back -def tensor_split(input, indices_or_sections, dim=0): - if isinstance(indices_or_sections, (list, tuple, ivy.Array)): - indices_or_sections = ( - ivy.diff(indices_or_sections, prepend=[0], append=[input.shape[dim]]) - .astype(ivy.int8) - .to_list() - ) - return ivy.split( - input, num_or_size_splits=indices_or_sections, axis=dim, with_remainder=True - ) - - -@to_ivy_arrays_and_back -def unbind(input, dim=0): - shape = list(input.shape) - shape.pop(dim) - return tuple([x.reshape(tuple(shape)) for x in split(input, 1, dim=dim)]) - - -@to_ivy_arrays_and_back -def dsplit(input, indices_or_sections, /): - if isinstance(indices_or_sections, (list, tuple, ivy.Array)): - indices_or_sections = ( - ivy.diff(indices_or_sections, prepend=[0], append=[input.shape[2]]) - .astype(ivy.int8) - .to_list() - ) - return tuple(ivy.dsplit(input, indices_or_sections)) - - -@to_ivy_arrays_and_back -def hsplit(input, indices_or_sections=None, /): - if isinstance(indices_or_sections, (list, tuple, ivy.Array)): - if input.ndim == 1: - indices_or_sections = ( - ivy.diff(indices_or_sections, prepend=[0], append=[input.shape[0]]) - .astype(ivy.int8) - .to_list() - ) - else: - indices_or_sections = ( - ivy.diff(indices_or_sections, prepend=[0], append=[input.shape[1]]) - .astype(ivy.int8) - .to_list() - ) - return tuple(ivy.hsplit(input, indices_or_sections)) - - -@to_ivy_arrays_and_back -def vsplit(input, indices_or_sections=None, /): - if isinstance(indices_or_sections, (list, tuple, ivy.Array)): - indices_or_sections = ( - ivy.diff(indices_or_sections, prepend=[0], append=[input.shape[0]]) - .astype(ivy.int8) - .to_list() - ) - return tuple(ivy.vsplit(input, indices_or_sections)) - - -@to_ivy_arrays_and_back -def row_stack(tensors, *, out=None): - return ivy.vstack(tensors, out=out) - - -@to_ivy_arrays_and_back -def where(condition, input=None, other=None): - if not ivy.exists(input) and not ivy.exists(other): - return nonzero(condition, as_tuple=True) - return ivy.where(condition, input, other) - - -@to_ivy_arrays_and_back -def conj(input): - return ivy.conj(input) - - -@to_ivy_arrays_and_back -def index_add(input, dim, index, source, *, alpha=1, out=None): - input = ivy.swapaxes(input, dim, 0) - source = ivy.swapaxes(source, dim, 0) - _to_adds = [] - index = sorted(zip(ivy.to_list(index), range(len(index))), key=(lambda x: x[0])) - while index: - _curr_idx = index[0][0] - while len(_to_adds) < _curr_idx: - _to_adds.append(ivy.zeros_like(source[0])) - _to_add_cum = ivy.get_item(source, index[0][1]) - while (1 < len(index)) and (index[0][0] == index[1][0]): - _to_add_cum = _to_add_cum + ivy.get_item(source, index.pop(1)[1]) - index.pop(0) - _to_adds.append(_to_add_cum) - while len(_to_adds) < input.shape[0]: - _to_adds.append(ivy.zeros_like(source[0])) - _to_adds = ivy.stack(_to_adds) - if len(input.shape) < 2: - # Added this line due to the paddle backend treating scalars as 1-d arrays - _to_adds = ivy.flatten(_to_adds) - - ret = ivy.add(input, _to_adds, alpha=alpha) - ret = ivy.swapaxes(ret, 0, dim, out=out) - return ret - - -@to_ivy_arrays_and_back -def index_copy(input, dim, index, source, *, out=None): - input = ivy.swapaxes(input, dim, 0) - source = ivy.swapaxes(source, dim, 0) - index = sorted(zip(ivy.to_list(index), range(len(index))), key=(lambda x: x[0])) - res = [] - while index: - _curr_idx = index[0][0] - for i in range(len(res), _curr_idx): - res.append(ivy.get_item(input, i)) - while (1 < len(index)) and (index[0][0] == index[1][0]): - index.pop(0) - res.append(ivy.get_item(source, index[0][1])) - index.pop(0) - for i in range(len(res), input.shape[0]): - res.append(ivy.get_item(input, i)) - res = ivy.stack(res) - if len(input.shape) < 2: - res = ivy.flatten(res) - - return ivy.swapaxes(res, 0, dim, out=out) - - -@to_ivy_arrays_and_back -def masked_select(input, mask, out=None): - return ivy.flatten(input[mask], out=out) - - -@to_ivy_arrays_and_back -def take(input, index): - input = ivy.reshape(input, (-1,)) - return ivy.gather(input, index, axis=0) - - -@to_ivy_arrays_and_back -def narrow(input, dim, start, length): - num_dims = ivy.get_num_dims(input) - slices = [slice(None)] * num_dims - slices[dim] = slice(start, start + length) - return input[tuple(slices)] - - -@to_ivy_arrays_and_back -def select(input, dim, index): - num_dims = ivy.get_num_dims(input) - slices = [slice(None)] * num_dims - slices[dim] = index - return input[tuple(slices)] - - -@to_ivy_arrays_and_back -<<<<<<< HEAD -def tensor_ops(tensor, operations_type, *args): - if operations_type == "index": - -def tensor_ops(tensor, operation_type, *args): - if operation_type == "index": - - if len(args) != 1: - raise ValueError - index = args[0] - return tensor[index] - - elif operations_type == "slice": -======= - elif operation_type == "slice": ->>>>>>> 7337c7f069c0fa2cb036917de0ad022411616d0d - if len(args) != 2: - raise ValueError - start, end = args - return tensor[start:end] -<<<<<<< HEAD - elif operations_type == "mutate": -======= - elif operation_type == "join": - if len(args) != 1: - raise ValueError - other_tensor = args[0] - return ivy.cat((tensor, other_tensor), dim=0) - elif operation_type == "mutate": ->>>>>>> 7337c7f069c0fa2cb036917de0ad022411616d0d - if len(args) != 1: - raise ValueError - factor = args[0] - tensor.mul_(factor) - elif operations_type == "join": - if len(args) != 1: - raise ValueError - other_tensor = args[0] - return ivy.cat((tensor, other_tensor), dim=0) - return tensor - else: - raise ValueError From 1112d042de3eb3bb3fd0df17635e9ffd86c165a3 Mon Sep 17 00:00:00 2001 From: Sanjay <121057369+Sanjay8602@users.noreply.github.com> Date: Wed, 18 Oct 2023 00:44:29 +0530 Subject: [PATCH 23/37] Delete ivy/functional/backends/torch/experimental/manipulation.py --- .../torch/experimental/manipulation.py | 423 ------------------ 1 file changed, 423 deletions(-) delete mode 100644 ivy/functional/backends/torch/experimental/manipulation.py diff --git a/ivy/functional/backends/torch/experimental/manipulation.py b/ivy/functional/backends/torch/experimental/manipulation.py deleted file mode 100644 index dfd236dadda7a..0000000000000 --- a/ivy/functional/backends/torch/experimental/manipulation.py +++ /dev/null @@ -1,423 +0,0 @@ -# global -from typing import Optional, Union, Sequence, Tuple, NamedTuple, List -from numbers import Number -from collections import namedtuple -import torch - -# local -from ivy.func_wrapper import with_unsupported_dtypes -from .. import backend_version -import ivy - - -def moveaxis( - a: torch.Tensor, - source: Union[int, Sequence[int]], - destination: Union[int, Sequence[int]], - /, - *, - copy: Optional[bool] = None, - out: Optional[torch.Tensor] = None, -) -> torch.Tensor: - if copy: - a = torch.clone(a) - return torch.moveaxis(a, source, destination) - - -moveaxis.support_native_out = False - - -def heaviside( - x1: torch.tensor, - x2: torch.tensor, - /, - *, - out: Optional[torch.tensor] = None, -) -> torch.tensor: - return torch.heaviside( - x1, - x2, - out=out, - ) - - -heaviside.support_native_out = True - - -def flipud( - m: torch.Tensor, - /, - *, - copy: Optional[bool] = None, - out: Optional[torch.tensor] = None, -) -> torch.tensor: - if copy: - m = torch.clone(m) - return torch.flipud(m) - - -flipud.support_native_out = False - - -def vstack( - arrays: Sequence[torch.Tensor], - /, - *, - out: Optional[torch.Tensor] = None, -) -> torch.Tensor: - if not isinstance(arrays, tuple): - arrays = tuple(arrays) - return torch.vstack(arrays, out=None) - - -def hstack( - arrays: Sequence[torch.Tensor], - /, - *, - out: Optional[torch.Tensor] = None, -) -> torch.Tensor: - if not isinstance(arrays, tuple): - arrays = tuple(arrays) - return torch.hstack(arrays, out=None) - - -def rot90( - m: torch.Tensor, - /, - *, - copy: Optional[bool] = None, - k: int = 1, - axes: Tuple[int, int] = (0, 1), - out: Optional[torch.Tensor] = None, -) -> torch.Tensor: - if copy: - m = torch.clone(m) - return torch.rot90(m, k, axes) - - -def top_k( - x: torch.Tensor, - k: int, - /, - *, - axis: int = -1, - largest: bool = True, - sorted: bool = True, - out: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, -) -> Tuple[torch.Tensor, torch.Tensor]: - k = min(k, x.shape[axis]) - topk_res = NamedTuple( - "top_k", [("values", torch.Tensor), ("indices", torch.Tensor)] - ) - if not largest: - indices = torch.argsort(x, dim=axis) - indices = torch.index_select(indices, axis, torch.arange(k)) - else: - indices = torch.argsort(-x, dim=axis) - indices = torch.index_select(indices, axis, torch.arange(k)) - if not sorted: - indices = torch.sort(indices, dim=axis)[0] - val = torch.gather(x, axis, indices) - return topk_res(val, indices) - - -def fliplr( - m: torch.Tensor, - /, - *, - copy: Optional[bool] = None, - out: Optional[torch.tensor] = None, -) -> torch.tensor: - if copy: - m = torch.clone(m) - return torch.fliplr(m) - - -fliplr.support_native_out = False - - -@with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, backend_version) -def i0( - x: torch.Tensor, - /, - *, - out: Optional[torch.Tensor] = None, -) -> torch.Tensor: - return torch.i0(x, out=out) - - -i0.support_native_out = True - - -def flatten( - x: torch.Tensor, - /, - *, - copy: Optional[bool] = None, - start_dim: Optional[int] = 0, - end_dim: Optional[int] = -1, - order: Optional[str] = "C", - out: Optional[torch.Tensor] = None, -) -> torch.Tensor: - if copy: - x = torch.clone(x) - return torch.flatten(x, start_dim=start_dim, end_dim=end_dim) - - -flatten.partial_mixed_handler = ( - lambda *args, copy=None, start_dim=0, end_dim=1, order="C", **kwargs: order == "C" -) - - -def vsplit( - ary: torch.Tensor, - indices_or_sections: Union[int, Sequence[int], torch.Tensor], - /, - *, - copy: Optional[bool] = None, -) -> List[torch.Tensor]: - if len(ary.shape) < 2: - raise ivy.utils.exceptions.IvyError( - "vsplit only works on arrays of 2 or more dimensions" - ) - if copy: - ary = torch.clone(ary) - return ivy.split(ary, num_or_size_splits=indices_or_sections, axis=0) - - -def dsplit( - ary: torch.Tensor, - indices_or_sections: Union[int, Sequence[int], torch.Tensor], - /, - *, - copy: Optional[bool] = None, -) -> List[torch.Tensor]: - if len(ary.shape) < 2: - raise ivy.utils.exceptions.IvyError( - "dsplit only works on arrays of 3 or more dimensions" - ) - if copy: - ary = torch.clone(ary) - return ivy.split(ary, num_or_size_splits=indices_or_sections, axis=2) - - -def atleast_1d(*arys: torch.Tensor, copy: Optional[bool] = None) -> List[torch.Tensor]: - if copy: - arys = ivy.nested_map(arys, torch.clone) - transformed = torch.atleast_1d(*arys) - if isinstance(transformed, tuple): - return list(transformed) - return transformed - - -def dstack( - arrays: Sequence[torch.Tensor], - /, - *, - out: Optional[torch.Tensor] = None, -) -> torch.Tensor: - if not isinstance(arrays, tuple): - arrays = tuple(arrays) - return torch.dstack(arrays, out=out) - - -def atleast_2d(*arys: torch.Tensor, copy: Optional[bool] = None) -> List[torch.Tensor]: - if copy: - arys = ivy.nested_map(arys, torch.clone) - transformed = torch.atleast_2d(*arys) - if isinstance(transformed, tuple): - return list(transformed) - return transformed - - -def atleast_3d( - *arys: Union[torch.Tensor, bool, Number], copy: Optional[bool] = None -) -> List[torch.Tensor]: - if copy: - arys = ivy.nested_map(arys, torch.clone) - transformed = torch.atleast_3d(*arys) - if isinstance(transformed, tuple): - return list(transformed) - return transformed - - -@with_unsupported_dtypes({"2.0.1 and below": ("float16", "bfloat16")}, backend_version) -def take_along_axis( - arr: torch.Tensor, - indices: torch.Tensor, - axis: int, - /, - *, - mode: str = "fill", - out: Optional[torch.Tensor] = None, -) -> torch.Tensor: - if arr.ndim != indices.ndim: - raise ivy.utils.exceptions.IvyException( - "arr and indices must have the same number of dimensions;" - + f" got {arr.ndim} vs {indices.ndim}" - ) - indices = indices.long() - if mode not in ["clip", "fill", "drop"]: - raise ValueError( - f"Invalid mode '{mode}'. Valid modes are 'clip', 'fill', 'drop'." - ) - arr_shape = arr.shape - if axis < 0: - axis += arr.ndim - if mode == "clip": - max_index = arr.shape[axis] - 1 - indices = torch.clamp(indices, 0, max_index) - elif mode == "fill" or mode == "drop": - if "float" in str(arr.dtype) or "complex" in str(arr.dtype): - fill_value = float("nan") - elif "uint" in str(arr.dtype): - fill_value = torch.iinfo(arr.dtype).max - elif "int" in str(arr.dtype): - fill_value = -torch.iinfo(arr.dtype).max - 1 - indices = torch.where((indices < 0) | (indices >= arr.shape[axis]), -1, indices) - arr_shape = list(arr_shape) - arr_shape[axis] = 1 - fill_arr = torch.full(arr_shape, fill_value, dtype=arr.dtype) - arr = torch.cat([arr, fill_arr], dim=axis) - indices = torch.where(indices < 0, arr.shape[axis] + indices, indices) - return torch.take_along_dim(arr, indices, axis, out=out) - - -def hsplit( - ary: torch.Tensor, - indices_or_sections: Union[int, Tuple[int, ...]], - /, - *, - copy: Optional[bool] = None, -) -> List[torch.Tensor]: - if copy: - ary = torch.clone(ary) - if len(ary.shape) == 1: - return ivy.split(ary, num_or_size_splits=indices_or_sections, axis=0) - return ivy.split(ary, num_or_size_splits=indices_or_sections, axis=1) - - -take_along_axis.support_native_out = True - - -def broadcast_shapes(*shapes: Union[List[int], List[Tuple]]) -> Tuple[int]: - return tuple(torch.broadcast_shapes(*shapes)) - - -broadcast_shapes.support_native_out = False - - -def expand( - x: torch.Tensor, - shape: Union[List[int], List[Tuple]], - /, - *, - copy: Optional[bool] = None, - out: Optional[torch.Tensor] = None, -) -> torch.Tensor: - if copy: - x = torch.clone(x) - return x.expand(shape) - - -expand.support_native_out = False - - -def concat_from_sequence( - input_sequence: Union[Tuple[torch.Tensor], List[torch.Tensor]], - /, - *, - new_axis: int = 0, - axis: int = 0, - out: Optional[torch.Tensor] = None, -) -> torch.Tensor: - is_tuple = type(input_sequence) is tuple - if is_tuple: - input_sequence = list(input_sequence) - if new_axis == 0: - ret = torch.cat(input_sequence, dim=axis) - return ret - elif new_axis == 1: - ret = torch.stack(input_sequence, dim=axis) - return ret - - -@with_unsupported_dtypes({"2.0.1 and below": ("complex", "float16")}, backend_version) -def unique_consecutive( - x: torch.Tensor, - /, - *, - axis: Optional[int] = None, -) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: - Results = namedtuple( - "Results", - ["output", "inverse_indices", "counts"], - ) - output, inverse_indices, counts = torch.unique_consecutive( - x, - return_inverse=True, - return_counts=True, - dim=axis, - ) - return Results( - output.to(x.dtype), - inverse_indices, - counts, - ) - - -def fill_diagonal( - a: torch.Tensor, - v: Union[int, float], - /, - *, - wrap: bool = False, -) -> torch.Tensor: - shape = a.shape - max_end = torch.prod(torch.tensor(shape)) - end = max_end - if len(shape) == 2: - step = shape[1] + 1 - if not wrap: - end = shape[1] * shape[1] - else: - step = 1 + (torch.cumprod(torch.tensor(shape[:-1]), 0)).sum() - - end = max_end if end > max_end else end - a = torch.reshape(a, (-1,)) - w = torch.zeros(a.shape, dtype=bool).to(a.device) - ins = torch.arange(0, max_end).to(a.device) - steps = torch.arange(0, end, step).to(a.device) - - for i in steps: - i = ins == i - w = torch.logical_or(w, i) - a = torch.where(w, v, a) - a = torch.reshape(a, shape) - return a - - -def tensor_ops(tensor, operation_type, *args): - if operation_type == "index": - if len(args) != 1: - raise ValueError - index = args[0] - return tensor[index] - elif operation_type == "slice": - if len(args) != 2: - raise ValueError - start, end = args - return tensor[start:end] - elif operation_type == "join": - if len(args) != 1: - raise ValueError - other_tensor = args[0] - return ivy.cat((tensor, other_tensor), dim=0) - elif operation_type == "mutate": - if len(args) != 1: - raise ValueError - factor = args[0] - tensor.mul_(factor) - return tensor - else: - raise ValueError From bbdc28653f9682665c64ba7bc2057b5a51355747 Mon Sep 17 00:00:00 2001 From: Sanjay <121057369+Sanjay8602@users.noreply.github.com> Date: Wed, 18 Oct 2023 00:45:06 +0530 Subject: [PATCH 24/37] Delete ivy/functional/backends/tensorflow/experimental/manipulation.py --- .../tensorflow/experimental/manipulation.py | 395 ------------------ 1 file changed, 395 deletions(-) delete mode 100644 ivy/functional/backends/tensorflow/experimental/manipulation.py diff --git a/ivy/functional/backends/tensorflow/experimental/manipulation.py b/ivy/functional/backends/tensorflow/experimental/manipulation.py deleted file mode 100644 index 217ec50dc19cd..0000000000000 --- a/ivy/functional/backends/tensorflow/experimental/manipulation.py +++ /dev/null @@ -1,395 +0,0 @@ -# global -from collections import namedtuple -from typing import Union, Optional, Sequence, Tuple, NamedTuple, List -from numbers import Number -import tensorflow as tf - -# local -from ivy.func_wrapper import with_unsupported_dtypes -from .. import backend_version -import ivy - - -def moveaxis( - a: Union[tf.Tensor, tf.Variable], - source: Union[int, Sequence[int]], - destination: Union[int, Sequence[int]], - /, - *, - copy: Optional[bool] = None, - out: Optional[Union[tf.Tensor, tf.Variable]] = None, -) -> Union[tf.Tensor, tf.Variable]: - return tf.experimental.numpy.moveaxis(a, source, destination) - - -@with_unsupported_dtypes({"2.13.0 and below": ("bfloat16",)}, backend_version) -def heaviside( - x1: Union[tf.Tensor, tf.Variable], - x2: Union[tf.Tensor, tf.Variable], - /, - *, - out: Optional[Union[tf.Tensor, tf.Variable]] = None, -) -> Union[tf.Tensor, tf.Variable]: - return tf.cast(tf.experimental.numpy.heaviside(x1, x2), x1.dtype) - - -def flipud( - m: Union[tf.Tensor, tf.Variable], - /, - *, - copy: Optional[bool] = None, - out: Optional[Union[tf.Tensor, tf.Variable]] = None, -) -> Union[tf.Tensor, tf.Variable]: - return tf.experimental.numpy.flipud(m) - - -def vstack( - arrays: Union[Sequence[tf.Tensor], Sequence[tf.Variable]], - /, - *, - out: Optional[Union[tf.Tensor, tf.Variable]] = None, -) -> Union[tf.Tensor, tf.Variable]: - return tf.experimental.numpy.vstack(arrays) - - -def hstack( - arrays: Union[Sequence[tf.Tensor], Sequence[tf.Variable]], - /, - *, - out: Optional[Union[tf.Tensor, tf.Variable]] = None, -) -> Union[tf.Tensor, tf.Variable]: - return tf.experimental.numpy.hstack(arrays) - - -def rot90( - m: Union[tf.Tensor, tf.Variable], - /, - *, - copy: Optional[bool] = None, - k: int = 1, - axes: Tuple[int, int] = (0, 1), - out: Union[tf.Tensor, tf.Variable] = None, -) -> Union[tf.Tensor, tf.Variable]: - return tf.experimental.numpy.rot90(m, k, axes) - - -@with_unsupported_dtypes({"2.13.0 and below": ("unsigned", "complex")}, backend_version) -def top_k( - x: tf.Tensor, - k: int, - /, - *, - axis: int = -1, - largest: bool = True, - sorted: bool = True, - out: Optional[Tuple[tf.Tensor, tf.Tensor]] = None, -) -> Tuple[tf.Tensor, tf.Tensor]: - k = min(k, x.shape[axis]) - if not largest: - indices = tf.experimental.numpy.argsort(x, axis=axis) - indices = tf.experimental.numpy.take( - indices, tf.experimental.numpy.arange(k), axis=axis - ) - indices = tf.dtypes.cast(indices, tf.int32) - else: - indices = tf.experimental.numpy.argsort(-x, axis=axis) - indices = tf.experimental.numpy.take( - indices, tf.experimental.numpy.arange(k), axis=axis - ) - indices = tf.dtypes.cast(indices, tf.int32) - if not sorted: - indices = tf.sort(indices, axis=axis) - topk_res = NamedTuple("top_k", [("values", tf.Tensor), ("indices", tf.Tensor)]) - val = tf.experimental.numpy.take_along_axis(x, indices, axis=axis) - indices = tf.dtypes.cast(indices, tf.int64) - return topk_res(val, indices) - - -def fliplr( - m: Union[tf.Tensor, tf.Variable], - /, - *, - copy: Optional[bool] = None, - out: Optional[Union[tf.Tensor, tf.Variable]] = None, -) -> Union[tf.Tensor, tf.Variable]: - return tf.experimental.numpy.fliplr(m) - - -@with_unsupported_dtypes({"2.13.0 and below": ("bfloat16",)}, backend_version) -def i0( - x: Union[tf.Tensor, tf.Variable], - /, - *, - out: Optional[Union[tf.Tensor, tf.Variable]] = None, -) -> Union[tf.Tensor, tf.Variable]: - return tf.math.bessel_i0(x, name=None) - - -def vsplit( - ary: Union[tf.Tensor, tf.Variable], - indices_or_sections: Union[int, Sequence[int], tf.Tensor, tf.Variable], - /, - *, - copy: Optional[bool] = None, -) -> List[Union[tf.Tensor, tf.Variable]]: - if len(ary.shape) < 2: - raise ivy.utils.exceptions.IvyError( - "vsplit only works on arrays of 2 or more dimensions" - ) - return ivy.split(ary, num_or_size_splits=indices_or_sections, axis=0) - - -def dsplit( - ary: Union[tf.Tensor, tf.Variable], - indices_or_sections: Union[int, Sequence[int], tf.Tensor, tf.Variable], - /, - *, - copy: Optional[bool] = None, -) -> List[Union[tf.Tensor, tf.Variable]]: - if len(ary.shape) < 3: - raise ivy.utils.exceptions.IvyError( - "dsplit only works on arrays of 3 or more dimensions" - ) - return ivy.split(ary, num_or_size_splits=indices_or_sections, axis=2) - - -def atleast_1d( - *arys: Union[tf.Tensor, tf.Variable, bool, Number], - copy: Optional[bool] = None, -) -> List[Union[tf.Tensor, tf.Variable]]: - return tf.experimental.numpy.atleast_1d(*arys) - - -def dstack( - arrays: Union[Sequence[tf.Tensor], Sequence[tf.Variable]], - /, - *, - out: Optional[Union[tf.Tensor, tf.Variable]] = None, -) -> Union[tf.Tensor, tf.Variable]: - return tf.experimental.numpy.dstack(arrays) - - -def atleast_2d( - *arys: Union[tf.Tensor, tf.Variable], - copy: Optional[bool] = None, -) -> List[Union[tf.Tensor, tf.Variable]]: - return tf.experimental.numpy.atleast_2d(*arys) - - -def atleast_3d( - *arys: Union[tf.Tensor, tf.Variable, bool, Number], - copy: Optional[bool] = None, -) -> List[Union[tf.Tensor, tf.Variable]]: - return tf.experimental.numpy.atleast_3d(*arys) - - -def take_along_axis( - arr: Union[tf.Tensor, tf.Variable], - indices: Union[tf.Tensor, tf.Variable], - axis: int, - /, - *, - mode: str = "fill", - out: Optional[Union[tf.Tensor, tf.Variable]] = None, -) -> Union[tf.Tensor, tf.Variable]: - if len(arr.shape) != len(indices.shape): - raise ivy.utils.exceptions.IvyException( - "arr and indices must have the same number of dimensions;" - + f" got {len(arr.shape)} vs {len(indices.shape)}" - ) - indices = tf.dtypes.cast(indices, tf.int32) - if mode not in ["clip", "fill", "drop"]: - raise ValueError( - f"Invalid mode '{mode}'. Valid modes are 'clip', 'fill', 'drop'." - ) - arr_shape = arr.shape - if axis < 0: - axis += len(arr.shape) - if mode == "clip": - max_index = arr.shape[axis] - 1 - indices = tf.clip_by_value(indices, 0, max_index) - elif mode in ("fill", "drop"): - if "float" in str(arr.dtype) or "complex" in str(arr.dtype): - fill_value = tf.constant(float("nan"), dtype=arr.dtype) - elif "uint" in str(arr.dtype): - fill_value = tf.constant(arr.dtype.max, dtype=arr.dtype) - elif "int" in str(arr.dtype): - fill_value = tf.constant(-arr.dtype.max - 1, dtype=arr.dtype) - else: - raise TypeError( - f"Invalid dtype '{arr.dtype}'. Valid dtypes are 'float', 'complex'," - " 'uint', 'int'." - ) - indices = tf.where((indices < 0) | (indices >= arr.shape[axis]), -1, indices) - arr_shape = list(arr_shape) - arr_shape[axis] = 1 - fill_arr = tf.fill(arr_shape, fill_value) - arr = tf.concat([arr, fill_arr], axis=axis) - return tf.experimental.numpy.take_along_axis(arr, indices, axis) - - -def hsplit( - ary: Union[tf.Tensor, tf.Variable], - indices_or_sections: Union[int, Tuple[int, ...]], - /, - *, - copy: Optional[bool] = None, -) -> List[Union[tf.Tensor, tf.Variable]]: - if len(ary.shape) == 1: - return ivy.split(ary, num_or_size_splits=indices_or_sections, axis=0) - return ivy.split(ary, num_or_size_splits=indices_or_sections, axis=1) - - -def broadcast_shapes( - *shapes: Union[List[int], List[Tuple]], -) -> Tuple[int, ...]: - if len(shapes) > 1: - desired_shape = tf.broadcast_dynamic_shape(shapes[0], shapes[1]) - if len(shapes) > 2: - for i in range(2, len(shapes)): - desired_shape = tf.broadcast_dynamic_shape(desired_shape, shapes[i]) - else: - return [shapes[0]] - return tuple(desired_shape.numpy().tolist()) - - -def expand( - x: Union[tf.Tensor, tf.Variable], - shape: Union[List[int], List[Tuple]], - /, - *, - copy: Optional[bool] = None, - out: Optional[Union[tf.Tensor, tf.Variable]] = None, -) -> Union[tf.Tensor, tf.Variable]: - shape = list(shape) - for i, dim in enumerate(shape): - if dim < 0: - shape[i] = x.shape[i] - return tf.broadcast_to(x, shape) - - -def concat_from_sequence( - input_sequence: Union[Tuple[tf.Tensor], List[tf.Tensor]], - /, - *, - new_axis: int = 0, - axis: int = 0, - out: Optional[Union[tf.Tensor, tf.Variable]] = None, -) -> Union[tf.Tensor, tf.Variable]: - is_tuple = type(input_sequence) is tuple - if is_tuple: - input_sequence = list(input_sequence) - highest_dtype = input_sequence[0].dtype - for i in input_sequence: - highest_dtype = ivy.as_native_dtype(ivy.promote_types(highest_dtype, i.dtype)) - - if new_axis == 0: - ret = tf.concat(input_sequence, axis=axis) - return ret - elif new_axis == 1: - ret = tf.stack(input_sequence, axis=axis) - return ret - - -def unique_consecutive( - x: Union[tf.Tensor, tf.Variable], - /, - *, - axis: Optional[int] = None, -) -> Tuple[ - Union[tf.Tensor, tf.Variable], - Union[tf.Tensor, tf.Variable], - Union[tf.Tensor, tf.Variable], -]: - Results = namedtuple( - "Results", - ["output", "inverse_indices", "counts"], - ) - x_shape = None - if axis is None: - x_shape = x.shape - x = tf.reshape(x, -1) - axis = -1 - ndim = len(x.shape) - if axis < 0: - axis += ndim - splits = ( - tf.where( - tf.math.reduce_any( - tf.experimental.numpy.diff(x, axis=axis) != 0, - axis=tuple(i for i in tf.range(ndim) if i != axis), - ) - ) - + 1 - ) - if tf.size(splits) > 0: - sub_arrays = tf.experimental.numpy.split(x, tf.reshape(splits, -1), axis=axis) - else: - sub_arrays = [x] - output = tf.concat( - [ - tf.raw_ops.UniqueV2(x=sub_array, axis=tf.constant([axis]))[0] - for sub_array in sub_arrays - ], - axis=axis, - ) - counts = tf.convert_to_tensor([sub_array.shape[axis] for sub_array in sub_arrays]) - inverse_indices = tf.repeat(tf.range(len(counts)), counts) - if x_shape: - inverse_indices = tf.reshape(inverse_indices, x_shape) - return Results( - tf.cast(output, x.dtype), - tf.cast(inverse_indices, tf.int64), - tf.cast(counts, tf.int64), - ) - - -def fill_diagonal( - a: tf.Tensor, - v: Union[int, float], - /, - *, - wrap: bool = False, -): - shape = tf.shape(a) - max_end = tf.math.reduce_prod(shape) - end = max_end - if len(shape) == 2: - step = shape[1] + 1 - if not wrap: - end = shape[1] * shape[1] - else: - step = 1 + tf.reduce_sum(tf.math.cumprod(shape[:-1])) - a = tf.reshape(a, (-1,)) - end = min(end, max_end) - indices = [[i] for i in range(0, end, step)] - ups = tf.convert_to_tensor([v] * len(indices), dtype=a.dtype) - a = tf.tensor_scatter_nd_update(a, indices, ups) - a = tf.reshape(a, shape) - return a - - -def tensor_ops(tensor, operation_type, *args): - if operation_type == "index": - if len(args) != 1: - raise ValueError - index = args[0] - return tensor[index] - elif operation_type == "slice": - if len(args) != 2: - raise ValueError - start, end = args - return tensor[start:end] - elif operation_type == "join": - if len(args) != 1: - raise ValueError - other_tensor = args[0] - return ivy.cat((tensor, other_tensor), dim=0) - elif operation_type == "mutate": - if len(args) != 1: - raise ValueError - factor = args[0] - tensor.mul_(factor) - return tensor - else: - raise ValueError From 180d5dd08b8c03b539f853b6d2e726bb93e35831 Mon Sep 17 00:00:00 2001 From: Sanjay <121057369+Sanjay8602@users.noreply.github.com> Date: Wed, 18 Oct 2023 00:45:50 +0530 Subject: [PATCH 25/37] Delete ivy/functional/backends/numpy/experimental/manipulation.py --- .../numpy/experimental/manipulation.py | 533 ------------------ 1 file changed, 533 deletions(-) delete mode 100644 ivy/functional/backends/numpy/experimental/manipulation.py diff --git a/ivy/functional/backends/numpy/experimental/manipulation.py b/ivy/functional/backends/numpy/experimental/manipulation.py deleted file mode 100644 index ee0d10ecc4ca2..0000000000000 --- a/ivy/functional/backends/numpy/experimental/manipulation.py +++ /dev/null @@ -1,533 +0,0 @@ -# global -from typing import ( - Optional, - Union, - Sequence, - Tuple, - NamedTuple, - Literal, - Callable, - Any, - List, -) -from numbers import Number -from collections import namedtuple -import numpy as np - -# local -import ivy -from ivy.functional.backends.numpy.helpers import _scalar_output_to_0d_array - - -def moveaxis( - a: np.ndarray, - source: Union[int, Sequence[int]], - destination: Union[int, Sequence[int]], - /, - *, - copy: Optional[bool] = None, - out: Optional[np.ndarray] = None, -) -> np.ndarray: - if copy: - a = a.copy() - return np.moveaxis(a, source, destination) - - -moveaxis.support_native_out = False - - -def heaviside( - x1: np.ndarray, - x2: np.ndarray, - /, - *, - out: Optional[np.ndarray] = None, -) -> np.ndarray: - return np.heaviside( - x1, - x2, - out=out, - ) - - -heaviside.support_native_out = True - - -def flipud( - m: np.ndarray, - /, - *, - copy: Optional[bool] = None, - out: Optional[np.ndarray] = None, -) -> np.ndarray: - if copy: - m = m.copy() - return np.flipud(m) - - -flipud.support_native_out = False - - -def vstack( - arrays: Sequence[np.ndarray], - /, - *, - out: Optional[np.ndarray] = None, -) -> np.ndarray: - return np.vstack(arrays) - - -def hstack( - arrays: Sequence[np.ndarray], - /, - *, - out: Optional[np.ndarray] = None, -) -> np.ndarray: - return np.hstack(arrays) - - -def rot90( - m: np.ndarray, - /, - *, - copy: Optional[bool] = None, - k: int = 1, - axes: Tuple[int, int] = (0, 1), - out: Optional[np.ndarray] = None, -) -> np.ndarray: - if copy: - m = m.copy() - return np.rot90(m, k, axes) - - -def top_k( - x: np.ndarray, - k: int, - /, - *, - axis: int = -1, - largest: bool = True, - sorted: bool = True, - out: Optional[Tuple[np.ndarray, np.ndarray]] = None, -) -> Tuple[np.ndarray, np.ndarray]: - k = min(k, x.shape[axis]) - if not largest: - indices = np.argsort(x, axis=axis) - indices = np.take(indices, np.arange(k), axis=axis) - else: - indices = np.argsort(-x, axis=axis) - indices = np.take(indices, np.arange(k), axis=axis) - if not sorted: - indices = np.sort(indices, axis=axis) - topk_res = NamedTuple("top_k", [("values", np.ndarray), ("indices", np.ndarray)]) - val = np.take_along_axis(x, indices, axis=axis) - return topk_res(val, indices) - - -def fliplr( - m: np.ndarray, - /, - *, - copy: Optional[bool] = None, - out: Optional[np.ndarray] = None, -) -> np.ndarray: - if copy: - m = m.copy() - return np.fliplr(m) - - -fliplr.support_native_out = False - - -def i0( - x: np.ndarray, - /, - *, - out: Optional[np.ndarray] = None, -) -> np.ndarray: - return np.i0(x) - - -i0.support_native_out = False - - -def _flat_array_to_1_dim_array(x): - return x.reshape((1,)) if x.shape == () else x - - -def _slice(operand, start_indices, limit_indices, strides=None): - strides = [1] * len(operand.shape) if strides is None else strides - - full_slice = () - for i, _ in enumerate(operand.shape): - strides_i = int(strides[i]) - start_i = int(start_indices[i]) - limit_i = int(limit_indices[i]) - full_slice += (slice(start_i, limit_i, strides_i),) - return operand[full_slice] - - -def _interior_pad(operand, padding_value, padding_config): - for axis, (_, _, interior) in enumerate(padding_config): - if interior > 0: - new_shape = list(operand.shape) - new_shape[axis] = new_shape[axis] + (new_shape[axis] - 1) * interior - new_array = np.full(new_shape, padding_value) - src_indices = np.arange(operand.shape[axis]) - dst_indices = src_indices * (interior + 1) - index_tuple = [slice(None)] * operand.ndim - index_tuple[axis] = dst_indices - new_array[tuple(index_tuple)] = operand - operand = new_array - - start_indices = [0] * operand.ndim - limit_indices = [0] * operand.ndim - for axis, (low, high, _) in enumerate(padding_config): - if low < 0: - start_indices[axis] = abs(low) - if high < 0: - limit_indices[axis] = high - else: - limit_indices[axis] = operand.shape[axis] + 1 - padded = _slice(operand, start_indices, limit_indices) - - pad_width = [(0, 0)] * operand.ndim - for axis, (low, high, _) in enumerate(padding_config): - if low > 0 and high > 0: - pad_width[axis] = (low, high) - elif low > 0 and not high > 0: - pad_width[axis] = (low, 0) - elif high > 0 and not low > 0: - pad_width[axis] = (0, high) - padded = np.pad(padded, pad_width, constant_values=padding_value) - return padded - - -def pad( - input: np.ndarray, - pad_width: Union[Sequence[Sequence[int]], np.ndarray, int], - /, - *, - mode: Union[ - Literal[ - "constant", - "dilated", - "edge", - "linear_ramp", - "maximum", - "mean", - "median", - "minimum", - "reflect", - "symmetric", - "wrap", - "empty", - ], - Callable, - ] = "constant", - stat_length: Union[Sequence[Sequence[int]], int] = 1, - constant_values: Union[Sequence[Sequence[Number]], Number] = 0, - end_values: Union[Sequence[Sequence[Number]], Number] = 0, - reflect_type: Literal["even", "odd"] = "even", - **kwargs: Optional[Any], -) -> np.ndarray: - if mode == "dilated": - if ivy.as_ivy_dtype(type(constant_values)) != input.dtype: - padding_value = ivy.native_array(constant_values, dtype=input.dtype) - else: - padding_value = constant_values - padded = _interior_pad(input, padding_value, pad_width) - return ivy.native_array(padded) - - if callable(mode): - return np.pad( - _flat_array_to_1_dim_array(input), - pad_width, - mode=mode, - **kwargs, - ) - if mode in ["maximum", "mean", "median", "minimum"]: - return np.pad( - _flat_array_to_1_dim_array(input), - pad_width, - mode=mode, - stat_length=stat_length, - ) - elif mode == "constant": - return np.pad( - _flat_array_to_1_dim_array(input), - pad_width, - mode=mode, - constant_values=constant_values, - ) - elif mode == "linear_ramp": - return np.pad( - _flat_array_to_1_dim_array(input), - pad_width, - mode=mode, - end_values=end_values, - ) - elif mode in ["reflect", "symmetric"]: - return np.pad( - _flat_array_to_1_dim_array(input), - pad_width, - mode=mode, - reflect_type=reflect_type, - ) - else: - return np.pad( - _flat_array_to_1_dim_array(input), - pad_width, - mode=mode, - ) - - -def vsplit( - ary: np.ndarray, - indices_or_sections: Union[int, Sequence[int], np.ndarray], - /, - *, - copy: Optional[bool] = None, -) -> List[np.ndarray]: - if ary.ndim < 2: - raise ivy.exceptions.IvyError( - "vsplit only works on arrays of 2 or more dimensions" - ) - if copy: - ary = ary.copy() - return ivy.split(ary, num_or_size_splits=indices_or_sections, axis=0) - - -def dsplit( - ary: np.ndarray, - indices_or_sections: Union[int, Tuple[int, ...]], - /, - *, - copy: Optional[bool] = None, -) -> List[np.ndarray]: - if ary.ndim < 3: - raise ivy.utils.exceptions.IvyError( - "dsplit only works on arrays of 3 or more dimensions" - ) - if copy: - ary = ary.copy() - return ivy.split(ary, num_or_size_splits=indices_or_sections, axis=2) - - -def atleast_1d( - *arys: Union[np.ndarray, bool, Number], copy: Optional[bool] = None -) -> List[np.ndarray]: - if copy: - arys = ivy.nested_map(arys, np.copy) - return np.atleast_1d(*arys) - - -def dstack( - arrays: Sequence[np.ndarray], - /, - *, - out: Optional[np.ndarray] = None, -) -> np.ndarray: - return np.dstack(arrays) - - -def atleast_2d(*arys: np.ndarray, copy: Optional[bool] = None) -> List[np.ndarray]: - if copy: - arys = ivy.nested_map(arys, np.copy) - return np.atleast_2d(*arys) - - -def atleast_3d( - *arys: Union[np.ndarray, bool, Number], copy: Optional[bool] = None -) -> List[np.ndarray]: - if copy: - arys = ivy.nested_map(arys, np.copy) - return np.atleast_3d(*arys) - - -@_scalar_output_to_0d_array -def take_along_axis( - arr: np.ndarray, - indices: np.ndarray, - axis: int, - /, - *, - mode: str = "fill", - out: Optional[np.ndarray] = None, -) -> np.ndarray: - if arr.ndim != indices.ndim: - raise ivy.utils.exceptions.IvyException( - "arr and indices must have the same number of dimensions;" - + f" got {arr.ndim} vs {indices.ndim}" - ) - if mode not in ["clip", "fill", "drop"]: - raise ValueError( - f"Invalid mode '{mode}'. Valid modes are 'clip', 'fill', 'drop'." - ) - arr_shape = arr.shape - if axis < 0: - axis += arr.ndim - if mode == "clip": - max_index = arr.shape[axis] - 1 - indices = np.clip(indices, 0, max_index) - elif mode in ("fill", "drop"): - if "float" in str(arr.dtype) or "complex" in str(arr.dtype): - fill_value = np.NAN - elif "uint" in str(arr.dtype): - fill_value = np.iinfo(arr.dtype).max - elif "int" in str(arr.dtype): - fill_value = -np.iinfo(arr.dtype).max - 1 - else: - raise TypeError( - f"Invalid dtype '{arr.dtype}'. Valid dtypes are 'float', 'complex'," - " 'uint', 'int'." - ) - indices = np.where((indices < 0) | (indices >= arr.shape[axis]), -1, indices) - arr_shape = list(arr_shape) - arr_shape[axis] = 1 - fill_arr = np.full(arr_shape, fill_value, dtype=arr.dtype) - arr = np.concatenate([arr, fill_arr], axis=axis) - return np.take_along_axis(arr, indices, axis) - - -def hsplit( - ary: np.ndarray, - indices_or_sections: Union[int, Tuple[int, ...]], - /, - *, - copy: Optional[bool] = None, -) -> List[np.ndarray]: - if copy: - ary = ary.copy() - if ary.ndim == 1: - return ivy.split(ary, num_or_size_splits=indices_or_sections, axis=0) - return ivy.split(ary, num_or_size_splits=indices_or_sections, axis=1) - - -take_along_axis.support_native_out = False - - -def broadcast_shapes(*shapes: Union[List[int], List[Tuple]]) -> Tuple[int]: - return np.broadcast_shapes(*shapes) - - -broadcast_shapes.support_native_out = False - - -def expand( - x: np.ndarray, - shape: Union[List[int], List[Tuple]], - /, - *, - copy: Optional[bool] = None, - out: Optional[np.ndarray] = None, -) -> np.ndarray: - if copy: - x = x.copy() - shape = list(shape) - for i, dim in enumerate(shape): - if dim < 0: - shape[i] = x.shape[i] - return np.broadcast_to(x, tuple(shape)) - - -expand.support_native_out = False - - -def concat_from_sequence( - input_sequence: Union[Tuple[np.ndarray], List[np.ndarray]], - /, - *, - new_axis: int = 0, - axis: int = 0, - out: Optional[np.ndarray] = None, -) -> np.ndarray: - is_tuple = type(input_sequence) is tuple - if is_tuple: - input_sequence = list(input_sequence) - if new_axis == 0: - ret = np.concatenate(input_sequence, axis=axis) - return ret - elif new_axis == 1: - ret = np.stack(input_sequence, axis=axis) - return ret - - -def unique_consecutive( - x: np.ndarray, - /, - *, - axis: Optional[int] = None, -) -> Tuple[np.ndarray, np.ndarray, np.ndarray]: - Results = namedtuple( - "Results", - ["output", "inverse_indices", "counts"], - ) - x_shape = None - if axis is None: - x_shape = x.shape - x = x.flatten() - axis = -1 - if axis < 0: - axis += x.ndim - sub_arrays = np.split( - x, - np.where( - np.any( - np.diff(x, axis=axis) != 0, - axis=tuple(i for i in np.arange(x.ndim) if i != axis), - ) - )[0] - + 1, - axis=axis, - ) - output = np.concatenate( - [np.unique(sub_array, axis=axis) for sub_array in sub_arrays], - axis=axis, - ) - counts = np.array([sub_array.shape[axis] for sub_array in sub_arrays]) - inverse_indices = np.repeat(np.arange(len(counts)), counts) - if x_shape: - inverse_indices = np.reshape(inverse_indices, x_shape) - return Results( - output.astype(x.dtype), - inverse_indices, - counts, - ) - - -def fill_diagonal( - a: np.ndarray, - v: Union[int, float], - /, - *, - wrap: bool = False, -) -> np.ndarray: - np.fill_diagonal(a, v, wrap=wrap) - return a - - -def tensor_ops(tensor, operation_type, *args): - if operation_type == "index": - if len(args) != 1: - raise ValueError - index = args[0] - return tensor[index] - elif operation_type == "slice": - if len(args) != 2: - raise ValueError - start, end = args - return tensor[start:end] - elif operation_type == "join": - if len(args) != 1: - raise ValueError - other_tensor = args[0] - return ivy.cat((tensor, other_tensor), dim=0) - elif operation_type == "mutate": - if len(args) != 1: - raise ValueError - factor = args[0] - tensor.mul_(factor) - return tensor - else: - raise ValueError From 3ba25cf4228f26f8c5078c076bd868693e75afcf Mon Sep 17 00:00:00 2001 From: Sanjay <121057369+Sanjay8602@users.noreply.github.com> Date: Wed, 18 Oct 2023 00:46:39 +0530 Subject: [PATCH 26/37] Delete ivy/functional/backends/jax/experimental/manipulation.py --- .../backends/jax/experimental/manipulation.py | 444 ------------------ 1 file changed, 444 deletions(-) delete mode 100644 ivy/functional/backends/jax/experimental/manipulation.py diff --git a/ivy/functional/backends/jax/experimental/manipulation.py b/ivy/functional/backends/jax/experimental/manipulation.py deleted file mode 100644 index 29c9b12ddb93b..0000000000000 --- a/ivy/functional/backends/jax/experimental/manipulation.py +++ /dev/null @@ -1,444 +0,0 @@ -# local -from typing import ( - Optional, - Union, - Sequence, - Tuple, - NamedTuple, - Literal, - Callable, - Any, - List, -) -import jax.numpy as jnp -import jax.lax as jlax -from numbers import Number -from collections import namedtuple - -# local -import ivy -from ivy.functional.backends.jax import JaxArray - - -def moveaxis( - a: JaxArray, - source: Union[int, Sequence[int]], - destination: Union[int, Sequence[int]], - /, - *, - copy: Optional[bool] = None, - out: Optional[JaxArray] = None, -) -> JaxArray: - return jnp.moveaxis(a, source, destination) - - -def heaviside( - x1: JaxArray, - x2: JaxArray, - /, - *, - out: Optional[JaxArray] = None, -) -> JaxArray: - return jnp.heaviside(x1, x2) - - -def flipud( - m: JaxArray, - /, - *, - copy: Optional[bool] = None, - out: Optional[JaxArray] = None, -) -> JaxArray: - return jnp.flipud(m) - - -def vstack( - arrays: Sequence[JaxArray], - /, - *, - out: Optional[JaxArray] = None, -) -> JaxArray: - return jnp.vstack(arrays) - - -def hstack( - arrays: Sequence[JaxArray], - /, - *, - out: Optional[JaxArray] = None, -) -> JaxArray: - return jnp.hstack(arrays) - - -def rot90( - m: JaxArray, - /, - *, - copy: Optional[bool] = None, - k: int = 1, - axes: Tuple[int, int] = (0, 1), - out: Optional[JaxArray] = None, -) -> JaxArray: - if isinstance(axes, list): - axes = tuple(axes) - return jnp.rot90(m, k, axes) - - -def top_k( - x: JaxArray, - k: int, - /, - *, - axis: int = -1, - largest: bool = True, - sorted: bool = True, - out: Optional[Tuple[JaxArray, JaxArray]] = None, -) -> Tuple[JaxArray, JaxArray]: - k = min(k, x.shape[axis]) - if not largest: - indices = jnp.argsort(x, axis=axis) - indices = jnp.take(indices, jnp.arange(k), axis=axis) - else: - indices = jnp.argsort(-x, axis=axis) - indices = jnp.take(indices, jnp.arange(k), axis=axis) - if not sorted: - indices = jnp.sort(indices, axis=axis) - topk_res = NamedTuple("top_k", [("values", JaxArray), ("indices", JaxArray)]) - val = jnp.take_along_axis(x, indices, axis=axis) - return topk_res(val, indices) - - -def fliplr( - m: JaxArray, - /, - *, - copy: Optional[bool] = None, - out: Optional[JaxArray] = None, -) -> JaxArray: - return jnp.fliplr(m) - - -def i0( - x: JaxArray, - /, - *, - out: Optional[JaxArray] = None, -) -> JaxArray: - return jnp.i0(x) - - -def _flat_array_to_1_dim_array(x): - return x.reshape((1,)) if x.shape == () else x - - -def _to_nested_tuple(nested_list): - ret = () - if hasattr(nested_list, "__iter__"): - for inner_list in nested_list: - if hasattr(inner_list, "__iter__"): - ret += (tuple(inner_list),) - else: - ret += (inner_list,) - return ret - if ret == (): - return nested_list - - -def pad( - input: JaxArray, - pad_width: Union[Sequence[Sequence[int]], JaxArray, int], - /, - *, - mode: Union[ - Literal[ - "constant", - "dilated", - "edge", - "linear_ramp", - "maximum", - "mean", - "median", - "minimum", - "reflect", - "symmetric", - "wrap", - "empty", - ], - Callable, - ] = "constant", - stat_length: Union[Sequence[Sequence[int]], int] = 1, - constant_values: Union[Sequence[Sequence[Number]], Number] = 0, - end_values: Union[Sequence[Sequence[Number]], Number] = 0, - reflect_type: Literal["even", "odd"] = "even", - **kwargs: Optional[Any], -) -> JaxArray: - pad_width = _to_nested_tuple(pad_width) - stat_length = _to_nested_tuple(stat_length) - constant_values = _to_nested_tuple(constant_values) - end_values = _to_nested_tuple(end_values) - input_dtype = input.dtype - - if mode == "dilated": - if ivy.as_ivy_dtype(type(constant_values)) != input_dtype: - padding_value = ivy.native_array(constant_values, dtype=input_dtype) - else: - padding_value = constant_values - padded = jlax.pad(input, padding_value, pad_width) - return padded - - if callable(mode): - ret = jnp.pad( - _flat_array_to_1_dim_array(input), - pad_width, - mode=mode, - **kwargs, - ) - elif mode in ["maximum", "mean", "median", "minimum"]: - ret = jnp.pad( - _flat_array_to_1_dim_array(input), - pad_width, - mode=mode, - stat_length=stat_length, - ) - elif mode == "constant": - ret = jnp.pad( - _flat_array_to_1_dim_array(input), - pad_width, - mode=mode, - constant_values=constant_values, - ) - elif mode == "linear_ramp": - ret = jnp.pad( - _flat_array_to_1_dim_array(input), - pad_width, - mode=mode, - end_values=end_values, - ) - elif mode in ["reflect", "symmetric"]: - ret = jnp.pad( - _flat_array_to_1_dim_array(input), - pad_width, - mode=mode, - reflect_type=reflect_type, - ) - else: - ret = jnp.pad( - _flat_array_to_1_dim_array(input), - pad_width, - mode=mode, - ) - if jnp.issubdtype(input_dtype, jnp.integer) and mode in ["mean", "median"]: - ret = jnp.round(ret).astype(input_dtype) - return ret - - -def vsplit( - ary: JaxArray, - indices_or_sections: Union[int, Sequence[int], JaxArray], - /, - *, - copy: Optional[bool] = None, -) -> List[JaxArray]: - if ary.ndim < 2: - raise ivy.exceptions.IvyError( - "vsplit only works on arrays of 2 or more dimensions" - ) - return ivy.split(ary, num_or_size_splits=indices_or_sections, axis=0) - - -def dsplit( - ary: JaxArray, - indices_or_sections: Union[int, Sequence[int], JaxArray], - /, - *, - copy: Optional[bool] = None, -) -> List[JaxArray]: - if ary.ndim < 3: - raise ivy.utils.exceptions.IvyError( - "dsplit only works on arrays of 3 or more dimensions" - ) - return ivy.split(ary, num_or_size_splits=indices_or_sections, axis=2) - - -def atleast_1d( - *arys: Union[JaxArray, bool, Number], copy: Optional[bool] = None -) -> List[JaxArray]: - return jnp.atleast_1d(*arys) - - -def dstack( - arrays: Sequence[JaxArray], - /, - *, - out: Optional[JaxArray] = None, -) -> JaxArray: - return jnp.dstack(arrays) - - -def atleast_2d(*arys: JaxArray, copy: Optional[bool] = None) -> List[JaxArray]: - return jnp.atleast_2d(*arys) - - -def atleast_3d( - *arys: Union[JaxArray, bool, Number], copy: Optional[bool] = None -) -> List[JaxArray]: - return jnp.atleast_3d(*arys) - - -def take_along_axis( - arr: JaxArray, - indices: JaxArray, - axis: int, - /, - *, - mode: str = "fill", - out: Optional[JaxArray] = None, -) -> JaxArray: - if arr.ndim != indices.ndim and axis is not None: - raise ivy.utils.exceptions.IvyException( - "arr and indices must have the same number of dimensions;" - + f" got {arr.ndim} vs {indices.ndim}" - ) - return jnp.take_along_axis(arr, indices, axis, mode=mode) - - -def hsplit( - ary: JaxArray, - indices_or_sections: Union[int, Tuple[int, ...]], - /, - *, - copy: Optional[bool] = None, -) -> List[JaxArray]: - if ary.ndim == 1: - return ivy.split(ary, num_or_size_splits=indices_or_sections, axis=0) - return ivy.split(ary, num_or_size_splits=indices_or_sections, axis=1) - - -def broadcast_shapes(*shapes: Union[List[int], List[Tuple]]) -> Tuple[int]: - return jnp.broadcast_shapes(*shapes) - - -def expand( - x: JaxArray, - shape: Union[List[int], List[Tuple]], - /, - *, - copy: Optional[bool] = None, - out: Optional[JaxArray] = None, -) -> JaxArray: - shape = list(shape) - if len(shape) > len(x.shape): - x = jnp.expand_dims(x, range(len(shape) - len(x.shape))) - for i, dim in enumerate(shape): - if dim < 0: - shape[i] = x.shape[i] - return jnp.broadcast_to(x, tuple(shape)) - - -def concat_from_sequence( - input_sequence: Union[Tuple[JaxArray], List[JaxArray]], - /, - *, - new_axis: int = 0, - axis: int = 0, - out: Optional[JaxArray] = None, -) -> JaxArray: - is_tuple = type(input_sequence) is tuple - if is_tuple: - input_sequence = list(input_sequence) - if new_axis == 0: - ret = jnp.concatenate(input_sequence, axis=axis) - return ret - elif new_axis == 1: - ret = jnp.stack(input_sequence, axis=axis) - return ret - - -def unique_consecutive( - x: JaxArray, - /, - *, - axis: Optional[int] = None, -) -> Tuple[JaxArray, JaxArray, JaxArray]: - Results = namedtuple( - "Results", - ["output", "inverse_indices", "counts"], - ) - x_shape = None - if axis is None: - x_shape = x.shape - x = x.flatten() - axis = -1 - if axis < 0: - axis += x.ndim - sub_arrays = jnp.split( - x, - jnp.where( - jnp.any( - jnp.diff(x, axis=axis) != 0, - axis=tuple(i for i in jnp.arange(x.ndim) if i != axis), - ) - )[0] - + 1, - axis=axis, - ) - output = jnp.concatenate( - [jnp.unique(sub_array, axis=axis) for sub_array in sub_arrays], - axis=axis, - ) - counts = jnp.array([sub_array.shape[axis] for sub_array in sub_arrays]) - inverse_indices = jnp.repeat(jnp.arange(len(counts)), counts) - if x_shape: - inverse_indices = jnp.reshape(inverse_indices, x_shape) - return Results( - output.astype(x.dtype), - inverse_indices, - counts, - ) - - -def fill_diagonal( - a: JaxArray, - v: Union[int, float], - /, - *, - wrap: bool = False, -) -> JaxArray: - shape = jnp.array(a.shape) - end = None - if len(shape) == 2: - step = shape[1] + 1 - if not wrap: - end = shape[1] * shape[1] - else: - step = 1 + (jnp.cumprod(shape[:-1])).sum() - a = jnp.reshape(a, (-1,)) - a = a.at[:end:step].set(jnp.array(v).astype(a.dtype)) - a = jnp.reshape(a, shape) - return a - - -def tensor_ops(tensor, operation_type, *args): - if operation_type == "index": - if len(args) != 1: - raise ValueError - index = args[0] - return tensor[index] - elif operation_type == "slice": - if len(args) != 2: - raise ValueError - start, end = args - return tensor[start:end] - elif operation_type == "join": - if len(args) != 1: - raise ValueError - other_tensor = args[0] - return ivy.cat((tensor, other_tensor), dim=0) - elif operation_type == "mutate": - if len(args) != 1: - raise ValueError - factor = args[0] - tensor.mul_(factor) - return tensor - else: - raise ValueError From 8eb456c1b05e07320f0cc45566ceb4c48be94b0b Mon Sep 17 00:00:00 2001 From: Sanjay <121057369+Sanjay8602@users.noreply.github.com> Date: Wed, 18 Oct 2023 00:47:01 +0530 Subject: [PATCH 27/37] Delete ivy/data_classes/array/experimental/manipulation.py --- .../array/experimental/manipulation.py | 1108 ----------------- 1 file changed, 1108 deletions(-) delete mode 100644 ivy/data_classes/array/experimental/manipulation.py diff --git a/ivy/data_classes/array/experimental/manipulation.py b/ivy/data_classes/array/experimental/manipulation.py deleted file mode 100644 index d93fc18239af4..0000000000000 --- a/ivy/data_classes/array/experimental/manipulation.py +++ /dev/null @@ -1,1108 +0,0 @@ -# global -import abc -from typing import ( - Optional, - Union, - Sequence, - Tuple, - List, - Iterable, - Callable, - Literal, - Any, -) -from numbers import Number - -# local -import ivy -from ivy import handle_view - - -class _ArrayWithManipulationExperimental(abc.ABC): - @handle_view - def moveaxis( - self: ivy.Array, - source: Union[int, Sequence[int]], - destination: Union[int, Sequence[int]], - /, - *, - copy: Optional[bool] = None, - out: Optional[ivy.Array] = None, - ) -> ivy.Array: - """ - ivy.Array instance method variant of ivy.moveaxis. This method simply wraps the - function, and so the docstring for ivy.unstack also applies to this method with - minimal changes. - - Parameters - ---------- - a - The array whose axes should be reordered. - source - Original positions of the axes to move. These must be unique. - destination - Destination positions for each of the original axes. - These must also be unique. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - out - optional output array, for writing the result to. - - Returns - ------- - ret - Array with moved axes. This array is a view of the input array. - - Examples - -------- - >>> x = ivy.zeros((3, 4, 5)) - >>> x.moveaxis(0, -1).shape - (4, 5, 3) - >>> x.moveaxis(-1, 0).shape - (5, 3, 4) - """ - return ivy.moveaxis(self._data, source, destination, copy=copy, out=out) - - def heaviside( - self: ivy.Array, - x2: ivy.Array, - /, - *, - out: Optional[ivy.Array] = None, - ) -> ivy.Array: - """ - ivy.Array instance method variant of ivy.heaviside. This method simply wraps the - function, and so the docstring for ivy.heaviside also applies to this method - with minimal changes. - - Parameters - ---------- - self - input array. - x2 - values to use where x1 is zero. - out - optional output array, for writing the result to. - - Returns - ------- - ret - output array with element-wise Heaviside step function of x1. - This is a scalar if both x1 and x2 are scalars. - - Examples - -------- - >>> x1 = ivy.array([-1.5, 0, 2.0]) - >>> x2 = ivy.array([0.5]) - >>> ivy.heaviside(x1, x2) - ivy.array([0.0000, 0.5000, 1.0000]) - - >>> x1 = ivy.array([-1.5, 0, 2.0]) - >>> x2 = ivy.array([1.2, -2.0, 3.5]) - >>> ivy.heaviside(x1, x2) - ivy.array([0., -2., 1.]) - """ - return ivy.heaviside(self._data, x2, out=out) - - @handle_view - def flipud( - self: ivy.Array, - /, - *, - copy: Optional[bool] = None, - out: Optional[ivy.Array] = None, - ) -> ivy.Array: - """ - ivy.Array instance method variant of ivy.flipud. This method simply wraps the - function, and so the docstring for ivy.flipud also applies to this method with - minimal changes. - - Parameters - ---------- - self - The array to be flipped. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - out - optional output array, for writing the result to. - - Returns - ------- - ret - Array corresponding to input array with elements - order reversed along axis 0. - - Examples - -------- - >>> m = ivy.diag([1, 2, 3]) - >>> m.flipud() - ivy.array([[ 0., 0., 3.], - [ 0., 2., 0.], - [ 1., 0., 0.]]) - """ - return ivy.flipud(self._data, copy=copy, out=out) - - def vstack( - self: ivy.Array, - arrays: Union[ - Tuple[Union[ivy.Array, ivy.NativeArray]], - List[Union[ivy.Array, ivy.NativeArray]], - ], - /, - *, - out: Optional[ivy.Array] = None, - ) -> ivy.Array: - """ - ivy.Array instance method variant of ivy.vstack. This method simply wraps the - function, and so the docstring for ivy.vstack also applies to this method with - minimal changes. - - Examples - -------- - >>> x = ivy.array([[1, 2]]) - >>> y = [ivy.array([[5, 6]]), ivy.array([[7, 8]])] - >>> print(x.vstack(y)) - ivy.array([[1, 2], - [5, 6], - [7, 8]]) - """ - if not isinstance(arrays, (list, tuple)): - arrays = [arrays] - if isinstance(arrays, tuple): - x = (self._data) + arrays - else: - x = [self._data] + arrays - return ivy.vstack(x, out=out) - - def hstack( - self: ivy.Array, - arrays: Union[ - Tuple[Union[ivy.Array, ivy.NativeArray]], - List[Union[ivy.Array, ivy.NativeArray]], - ], - /, - *, - out: Optional[ivy.Array] = None, - ) -> ivy.Array: - """ - ivy.Array instance method variant of ivy.hstack. This method simply wraps the - function, and so the docstring for ivy.hstack also applies to this method with - minimal changes. - - Examples - -------- - >>> x = ivy.array([[1, 2]]) - >>> y = [ivy.array([[5, 6]]), ivy.array([[7, 8]])] - >>> print(x.vstack(y)) - ivy.array([1, 2, 5, 6, 7, 8]) - """ - if not isinstance(arrays, (list, tuple)): - arrays = [arrays] - if isinstance(arrays, tuple): - x = (self._data,) + arrays - else: - x = [self._data] + arrays - return ivy.hstack(x, out=out) - - @handle_view - def rot90( - self: ivy.Array, - /, - *, - copy: bool = None, - k: int = 1, - axes: Tuple[int, int] = (0, 1), - out: Optional[ivy.Array] = None, - ) -> ivy.Array: - """ - ivy.Array instance method variant of ivy.rot90. This method simply wraps the - function, and so the docstring for ivy.rot90 also applies to this method with - minimal changes. - - Parameters - ---------- - self - Input array of two or more dimensions. - k - Number of times the array is rotated by 90 degrees. - axes - The array is rotated in the plane defined by the axes. Axes must be - different. - out - Optional output, for writing the result to. It must have a shape that the - inputs broadcast to. - - Returns - ------- - ret - Array with a rotated view of input array. - - Examples - -------- - >>> m = ivy.array([[1,2], [3,4]]) - >>> m.rot90() - ivy.array([[2, 4], - [1, 3]]) - >>> m = ivy.array([[1,2], [3,4]]) - >>> m.rot90(k=2) - ivy.array([[4, 3], - [2, 1]]) - >>> m = ivy.array([[[0, 1],\ - [2, 3]],\ - [[4, 5],\ - [6, 7]]]) - >>> m.rot90(k=2, axes=(1,2)) - ivy.array([[[3, 2], - [1, 0]], - - [[7, 6], - [5, 4]]]) - """ - return ivy.rot90(self._data, copy=copy, k=k, axes=axes, out=out) - - def top_k( - self: ivy.Array, - k: int, - /, - *, - axis: int = -1, - largest: bool = True, - sorted: bool = True, - out: Optional[tuple] = None, - ) -> Tuple[ivy.Array, ivy.NativeArray]: - """ - ivy.Array instance method variant of ivy.top_k. This method simply wraps the - function, and so the docstring for ivy.top_k also applies to this method with - minimal changes. - - Parameters - ---------- - self - The array to compute top_k for. - k - Number of top elements to retun must not exceed the array size. - axis - The axis along which we must return the top elements default value is 1. - largest - If largest is set to False we return k smallest elements of the array. - sorted - If sorted is set to True we return the elements in sorted order. - out: - Optional output tuple, for writing the result to. Must have two arrays, - with a shape that the returned tuple broadcast to. - - Returns - ------- - ret - A named tuple with values and indices of top k elements. - - Examples - -------- - With :class:`ivy.Array` input: - - >>> x = ivy.array([2., 1., -3., 5., 9., 0., -4]) - >>> y = x.top_k(2) - >>> print(y) - top_k(values=ivy.array([9., 5.]), indices=ivy.array([4, 3])) - """ - return ivy.top_k(self, k, axis=axis, largest=largest, sorted=sorted, out=out) - - @handle_view - def fliplr( - self: ivy.Array, - /, - *, - copy: Optional[bool] = None, - out: Optional[ivy.Array] = None, - ) -> ivy.Array: - """ - ivy.Array instance method variant of ivy.fliplr. This method simply wraps the - function, and so the docstring for ivy.fliplr also applies to this method with - minimal changes. - - Parameters - ---------- - self - The array to be flipped. Must be at least 2-D. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - out - optional output array, for writing the result to. - - Returns - ------- - ret - Array corresponding to input array with elements - order reversed along axis 1. - - Examples - -------- - >>> m = ivy.diag([1, 2, 3]) - >>> m.fliplr() - ivy.array([[0, 0, 1], - [0, 2, 0], - [3, 0, 0]]) - """ - return ivy.fliplr(self._data, copy=copy, out=out) - - def i0( - self: ivy.Array, - /, - *, - out: Optional[ivy.Array] = None, - ) -> ivy.Array: - """ - ivy.Array instance method variant of ivy.i0. This method simply wraps the - function, and so the docstring for ivy.i0 also applies to this method with - minimal changes. - - Parameters - ---------- - self - Input array. - out - Optional output, for writing the result to. - - Returns - ------- - ret - Array with modified Bessel function of the first kind, order 0. - - Examples - -------- - >>> x = ivy.array([[1, 2, 3]]) - >>> x.i0() - ivy.array([1.26606588, 2.2795853 , 4.88079259]) - """ - return ivy.i0(self._data, out=out) - - @handle_view - def flatten( - self: ivy.Array, - *, - copy: Optional[bool] = None, - start_dim: int = 0, - end_dim: int = -1, - order: str = "C", - out: Optional[ivy.Array] = None, - ) -> ivy.Array: - """ - ivy.Array instance method variant of ivy.flatten. This method simply wraps the - function, and so the docstring for ivy.flatten also applies to this method with - minimal changes. - - Parameters - ---------- - self - input array to flatten. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - start_dim - first dim to flatten. If not set, defaults to 0. - end_dim - last dim to flatten. If not set, defaults to -1. - order - Read the elements of the input container using this index order, - and place the elements into the reshaped array using this index order. - ‘C’ means to read / write the elements using C-like index order, - with the last axis index changing fastest, back to the first axis index - changing slowest. - ‘F’ means to read / write the elements using Fortran-like index order, with - the first index changing fastest, and the last index changing slowest. - Note that the ‘C’ and ‘F’ options take no account of the memory layout - of the underlying array, and only refer to the order of indexing. - Default order is 'C'. - out - Optional output, for writing the result to. - - Returns - ------- - ret - the flattened array over the specified dimensions. - - Examples - -------- - >>> x = ivy.array([[1,2], [3,4]]) - >>> x.flatten() - ivy.array([1, 2, 3, 4]) - - >>> x = ivy.array([[1,2], [3,4]]) - >>> x.flatten(order='F') - ivy.array([1, 3, 2, 4]) - - >>> x = ivy.array( - [[[[ 5, 5, 0, 6], - [17, 15, 11, 16], - [ 6, 3, 13, 12]], - - [[ 6, 18, 10, 4], - [ 5, 1, 17, 3], - [14, 14, 18, 6]]], - - - [[[12, 0, 1, 13], - [ 8, 7, 0, 3], - [19, 12, 6, 17]], - - [[ 4, 15, 6, 15], - [ 0, 5, 17, 9], - [ 9, 3, 6, 19]]], - - - [[[17, 13, 11, 16], - [ 4, 18, 17, 4], - [10, 10, 9, 1]], - - [[19, 17, 13, 10], - [ 4, 19, 16, 17], - [ 2, 12, 8, 14]]]] - ) - >>> x.flatten(start_dim = 1, end_dim = 2) - ivy.array( - [[[ 5, 5, 0, 6], - [17, 15, 11, 16], - [ 6, 3, 13, 12], - [ 6, 18, 10, 4], - [ 5, 1, 17, 3], - [14, 14, 18, 6]], - - [[12, 0, 1, 13], - [ 8, 7, 0, 3], - [19, 12, 6, 17], - [ 4, 15, 6, 15], - [ 0, 5, 17, 9], - [ 9, 3, 6, 19]], - - [[17, 13, 11, 16], - [ 4, 18, 17, 4], - [10, 10, 9, 1], - [19, 17, 13, 10], - [ 4, 19, 16, 17], - [ 2, 12, 8, 14]]])) - """ - return ivy.flatten( - self._data, - copy=copy, - start_dim=start_dim, - end_dim=end_dim, - order=order, - out=out, - ) - - def pad( - self: ivy.Array, - pad_width: Union[Iterable[Tuple[int]], int], - /, - *, - mode: Union[ - Literal[ - "constant", - "dilated", - "edge", - "linear_ramp", - "maximum", - "mean", - "median", - "minimum", - "reflect", - "symmetric", - "wrap", - "empty", - ], - Callable, - ] = "constant", - stat_length: Union[Iterable[Tuple[int]], int] = 1, - constant_values: Union[Iterable[Tuple[Number]], Number] = 0, - end_values: Union[Iterable[Tuple[Number]], Number] = 0, - reflect_type: Literal["even", "odd"] = "even", - out: Optional[ivy.Array] = None, - **kwargs: Optional[Any], - ) -> ivy.Array: - """ - ivy.Array instance method variant of ivy.pad. - - This method simply wraps the function, and so the docstring for - ivy.pad also applies to this method with minimal changes. - """ - return ivy.pad( - self._data, - pad_width, - mode=mode, - stat_length=stat_length, - constant_values=constant_values, - end_values=end_values, - reflect_type=reflect_type, - out=out, - **kwargs, - ) - - @handle_view - def vsplit( - self: ivy.Array, - indices_or_sections: Union[int, Sequence[int], ivy.Array], - /, - *, - copy: Optional[bool] = None, - ) -> List[ivy.Array]: - """ - ivy.Array instance method variant of ivy.vsplit. This method simply wraps the - function, and so the docstring for ivy.vsplit also applies to this method with - minimal changes. - - Parameters - ---------- - self - Input array. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - indices_or_sections - If indices_or_sections is an integer n, the array is split into n - equal sections, provided that n must be a divisor of the split axis. - If indices_or_sections is a sequence of ints or 1-D array, - then input is split at each of the indices. - - Returns - ------- - ret - input array split vertically. - - Examples - -------- - >>> ary = ivy.array( - [[[0., 1.], - [2., 3.]], - [[4., 5.], - [6., 7.]]] - ) - >>> ary.vsplit(2) - [ivy.array([[[0., 1.], [2., 3.]]]), ivy.array([[[4., 5.], [6., 7.]]])]) - """ - return ivy.vsplit(self._data, indices_or_sections, copy=copy) - - @handle_view - def dsplit( - self: ivy.Array, - indices_or_sections: Union[int, Sequence[int], ivy.Array], - /, - *, - copy: Optional[bool] = None, - ) -> List[ivy.Array]: - """ - ivy.Array instance method variant of ivy.dsplit. This method simply wraps the - function, and so the docstring for ivy.dsplit also applies to this method with - minimal changes. - - Parameters - ---------- - self - Input array. - indices_or_sections - If indices_or_sections is an integer n, the array is split into n - equal sections, provided that n must be a divisor of the split axis. - If indices_or_sections is a sequence of ints or 1-D array, - then input is split at each of the indices. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - - Returns - ------- - ret - input array split along the 3rd axis. - - Examples - -------- - >>> ary = ivy.array( - [[[ 0., 1., 2., 3.], - [ 4., 5., 6., 7.]], - [[ 8., 9., 10., 11.], - [12., 13., 14., 15.]]] - ) - >>> ary.dsplit(2) - [ivy.array([[[ 0., 1.], [ 4., 5.]], [[ 8., 9.], [12., 13.]]]), - ivy.array([[[ 2., 3.], [ 6., 7.]], [[10., 11.], [14., 15.]]])] - """ - return ivy.dsplit(self._data, indices_or_sections, copy=copy) - - @handle_view - def atleast_1d( - self: ivy.Array, - *arys: Union[ivy.Array, bool, Number], - copy: Optional[bool] = None, - ) -> List[ivy.Array]: - """ - ivy.Array instance method variant of ivy.atleast_1d. This method simply wraps - the function, and so the docstring for ivy.atleast_1d also applies to this - method with minimal changes. - - Parameters - ---------- - self - Input array. Cannot be a scalar input. - arys - An arbitrary number of input arrays. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - - Returns - ------- - ret - List of arrays, each with a.ndim >= 1. Copies are made - only if necessary. - - Examples - -------- - >>> a1 = ivy.array([[1,2,3]]) - >>> a2 = ivy.array(4) - >>> a1.atleast_1d(a2,5,6) - [ivy.array([[1, 2, 3]]), ivy.array([4]), ivy.array([5]), ivy.array([6])] - """ - return ivy.atleast_1d(self._data, *arys, copy=copy) - - def dstack( - self: ivy.Array, - arrays: Union[ - Tuple[Union[ivy.Array, ivy.NativeArray]], - List[Union[ivy.Array, ivy.NativeArray]], - ], - /, - *, - out: Optional[ivy.Array] = None, - ) -> ivy.Array: - """ - ivy.Array instance method variant of ivy.dstack. This method simply wraps the - function, and so the docstring for ivy.dstack also applies to this method with - minimal changes. - - Examples - -------- - >>> x = ivy.array([1, 2, 3]) - >>> y = ivy.array([2, 3, 4]) - >>> x.dstack(y) - ivy.array([[[1, 2], - [2, 3], - [3, 4]]]) - """ - if not isinstance(arrays, (list, tuple)): - arrays = [arrays] - if isinstance(arrays, tuple): - x = (self._data,) + arrays - else: - x = [self._data] + arrays - return ivy.dstack(x, out=out) - - @handle_view - def atleast_2d( - self: ivy.Array, - *arys: ivy.Array, - copy: Optional[bool] = None, - ) -> List[ivy.Array]: - """ - ivy.Array instance method variant of ivy.atleast_2d. This method simply wraps - the function, and so the docstring for ivy.atleast_2d also applies to this - method with minimal changes. - - Parameters - ---------- - self - Input array. Cannot be a scalar input. - arys - An arbitrary number of input arrays. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - - Returns - ------- - ret - List of arrays, each with a.ndim >= 2. Copies are made - only if necessary. - - Examples - -------- - >>> a1 = ivy.array([[1,2,3]]) - >>> a2 = ivy.array(4) - >>> a1.atleast_2d(a2,5,6) - [ivy.array([[1, 2, 3]]), ivy.array([[4]]), ivy.array([[5]]), ivy.array([[6]])] - """ - return ivy.atleast_2d(self._data, *arys, copy=copy) - - @handle_view - def atleast_3d( - self: ivy.Array, - *arys: Union[ivy.Array, bool, Number], - copy: Optional[bool] = None, - ) -> List[ivy.Array]: - """ - ivy.Array instance method variant of ivy.atleast_3d. This method simply wraps - the function, and so the docstring for ivy.atleast_3d also applies to this - method with minimal changes. - - Parameters - ---------- - self - Input array. Cannot be a scalar input. - arys - An arbitrary number of input arrays. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - - Returns - ------- - ret - List of arrays, each with a.ndim >= 3. Copies are made only if necessary - and views with three or more dimensions are returned. For example, a 1-D - array of shape (N,) becomes a view of shape (1, N, 1), and a 2-D array - of shape (M, N) becomes a view of shape (M, N, 1). - - Examples - -------- - >>> a1 = ivy.array([[1,2,3]]) - >>> a2 = ivy.array([4,8]) - >>> a1.atleast_3d(a2,5,6) - [ivy.array([[[1], - [2], - [3]]]), ivy.array([[[4], - [8]]]), ivy.array([[[5]]]), ivy.array([[[6]]])] - """ - return ivy.atleast_3d(self._data, *arys, copy=copy) - - def take_along_axis( - self: ivy.Array, - indices: ivy.Array, - axis: int, - /, - *, - mode: str = "fill", - out: Optional[ivy.Array] = None, - ) -> ivy.Array: - """ - ivy.Array instance method variant of ivy.take_along_axis. This method simply - wraps the function, and so the docstring for ivy.take_along_axis also applies to - this method with minimal changes. - - Parameters - ---------- - self - The source array. - indices - The indices of the values to extract. - axis - The axis over which to select values. - mode - One of: 'clip', 'fill', 'drop'. Parameter controlling how out-of-bounds - indices will be handled. - out - Optional output, for writing the result to. - - Returns - ------- - ret - The returned array has the same shape as indices. - - Examples - -------- - >>> arr = ivy.array([[4, 3, 5], [1, 2, 1]]) - >>> indices = ivy.array([[0, 1, 1], [2, 0, 0]]) - >>> y = arr.take_along_axis(indices, 1) - >>> print(y) - ivy.array([[4, 3, 3], [1, 1, 1]]) - """ - return ivy.take_along_axis(self._data, indices, axis, mode=mode, out=out) - - @handle_view - def hsplit( - self: ivy.Array, - indices_or_sections: Union[int, Tuple[int, ...]], - /, - *, - copy: Optional[bool] = None, - ) -> List[ivy.Array]: - """ - ivy.Array instance method variant of ivy.hsplit. This method simply wraps the - function, and so the docstring for ivy.hsplit also applies to this method with - minimal changes. - - Parameters - ---------- - self - Input array. - indices_or_sections - If indices_or_sections is an integer n, the array is split into n - equal sections, provided that n must be a divisor of the split axis. - If indices_or_sections is a sequence of ints or 1-D array, - then input is split at each of the indices. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - - Returns - ------- - ret - list of arrays split horizontally from input array. - - Examples - -------- - >>> ary = ivy.array( - [[0., 1., 2., 3.], - [4., 5., 6, 7.], - [8., 9., 10., 11.], - [12., 13., 14., 15.]] - ) - >>> ary.hsplit(2) - [ivy.array([[ 0., 1.], - [ 4., 5.], - [ 8., 9.], - [12., 13.]]), - ivy.array([[ 2., 3.], - [ 6., 7.], - [10., 11.], - [14., 15.]])) - """ - return ivy.hsplit(self._data, indices_or_sections, copy=copy) - - @handle_view - def expand( - self: ivy.Array, - shape: Union[ivy.Shape, ivy.NativeShape], - /, - *, - copy: Optional[bool] = None, - out: Optional[ivy.Array] = None, - ) -> ivy.Array: - """ - Broadcast the input Array following the given shape and the broadcast rule. - - Parameters - ---------- - self - Array input. - shape - A 1-D Array indicates the shape you want to expand to, - following the broadcast rule - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - out - optional output array, for writing the result to. - - Returns - ------- - ret - Output Array - """ - return ivy.expand(self._data, shape, copy=copy, out=out) - - def as_strided( - self: ivy.Array, - shape: Union[ivy.Shape, ivy.NativeShape, Sequence[int]], - strides: Sequence[int], - /, - ) -> ivy.Array: - """ - Create a copy of the input array with the given shape and strides. - - Parameters - ---------- - self - Input Array. - shape - The shape of the new array. - strides - The strides of the new array (specified in bytes). - - Returns - ------- - ret - Output Array - """ - return ivy.as_strided(self._data, shape, strides) - - @handle_view - def concat_from_sequence( - self: ivy.Array, - /, - input_sequence: Union[ - Tuple[Union[ivy.Array, ivy.NativeArray]], - List[Union[ivy.Array, ivy.NativeArray]], - ], - *, - new_axis: int = 0, - axis: int = 0, - out: Optional[ivy.Array] = None, - ) -> ivy.Array: - """ - Concatenate a sequence of arrays along a new or an existing axis. - - Parameters - ---------- - self - Array input. - input_sequence - A sequence of arrays. - new_axis - Insert and concatenate on a new axis or not, - default 0 means do not insert new axis. - new_axis = 0: concatenate - new_axis = 1: stack - axis - The axis along which the arrays will be concatenated. - out - Optional output array, for writing the result to. - - Returns - ------- - ret - Output Array - """ - if new_axis == 0: - return ivy.concat_from_sequence( - [self._data] + input_sequence, new_axis=new_axis, axis=axis, out=out - ) - elif new_axis == 1: - if not isinstance(input_sequence, (tuple, list)): - input_sequence = [input_sequence] - if isinstance(input_sequence, tuple): - input_sequence = (self._data,) + input_sequence - else: - input_sequence = [self._data] + input_sequence - return ivy.concat_from_sequence( - input_sequence, new_axis=new_axis, axis=axis, out=out - ) - - @handle_view - def associative_scan( - self: ivy.Array, - fn: Callable, - /, - *, - reverse: bool = False, - axis: int = 0, - ) -> ivy.Array: - """ - Perform an associative scan over the given array. - - Parameters - ---------- - self - The array to scan over. - fn - The associative function to apply. - reverse - Whether to scan in reverse with respect to the given axis. - axis - The axis to scan over. - - Returns - ------- - ret - The result of the scan. - """ - return ivy.associative_scan(self._data, fn, reverse=reverse, axis=axis) - - def unique_consecutive( - self: ivy.Array, - /, - *, - axis: Optional[int] = None, - ) -> Tuple[ivy.Array, ivy.Array, ivy.Array]: - """ - ivy.Array instance method variant of ivy.unique_consecutive. - - This method simply wraps the function, and so the docstring for - ivy.unique_consecutive also applies to this method with minimal - changes. - """ - return ivy.unique_consecutive(self._data, axis=axis) - - def fill_diagonal( - self: ivy.Array, - v: Union[int, float], - /, - *, - wrap: bool = False, - ) -> ivy.Array: - """ - ivy.Array instance method variant of ivy.fill_diag. - - This method simply wraps the function, and so the docstring for - ivy.fill_diag also applies to this method with minimal changes. - """ - return ivy.fill_diagonal(self._data, v, wrap=wrap) - - -def tensor_ops(tensor, operation_type, *args): - if operation_type == "index": - if len(args) != 1: - raise ValueError - index = args[0] - return tensor[index] - elif operation_type == "slice": - if len(args) != 2: - raise ValueError - start, end = args - return tensor[start:end] - elif operation_type == "join": - if len(args) != 1: - raise ValueError - other_tensor = args[0] - return ivy.cat((tensor, other_tensor), dim=0) - elif operation_type == "mutate": - if len(args) != 1: - raise ValueError - factor = args[0] - tensor.mul_(factor) - return tensor - else: - raise ValueError From e04b013f476f32dec43312ae8e990b1497e5ec52 Mon Sep 17 00:00:00 2001 From: Sanjay <121057369+Sanjay8602@users.noreply.github.com> Date: Wed, 18 Oct 2023 00:48:12 +0530 Subject: [PATCH 28/37] Delete ivy/data_classes/container/experimental/manipulation.py --- .../container/experimental/manipulation.py | 3025 ----------------- 1 file changed, 3025 deletions(-) delete mode 100644 ivy/data_classes/container/experimental/manipulation.py diff --git a/ivy/data_classes/container/experimental/manipulation.py b/ivy/data_classes/container/experimental/manipulation.py deleted file mode 100644 index 73e2b1f7ea6a3..0000000000000 --- a/ivy/data_classes/container/experimental/manipulation.py +++ /dev/null @@ -1,3025 +0,0 @@ -# global -from typing import ( - Optional, - Union, - List, - Dict, - Sequence, - Tuple, - Literal, - Any, - Callable, - Iterable, -) -from numbers import Number - -# local -import ivy -from ivy.data_classes.container.base import ContainerBase - - -class _ContainerWithManipulationExperimental(ContainerBase): - @staticmethod - def static_moveaxis( - a: Union[ivy.Array, ivy.NativeArray, ivy.Container], - source: Union[int, Sequence[int], ivy.Container], - destination: Union[int, Sequence[int], ivy.Container], - /, - *, - copy: Optional[Union[bool, ivy.Container]] = None, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container static method variant of ivy.moveaxis. This method simply wraps - the function, and so the docstring for ivy.moveaxis also applies to this method - with minimal changes. - - Parameters - ---------- - a - The container with the arrays whose axes should be reordered. - source - Original positions of the axes to move. These must be unique. - destination - Destination positions for each of the original axes. - These must also be unique. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - out - optional output container, for writing the result to. - - Returns - ------- - ret - Container including arrays with moved axes. - - Examples - -------- - With one :class:`ivy.Container` input: - >>> x = ivy.Container(a=ivy.zeros((3, 4, 5)), b=ivy.zeros((2,7,6))) - >>> ivy.Container.static_moveaxis(x, 0, -1).shape - { - a: (4, 5, 3) - b: (7, 6, 2) - } - """ - return ContainerBase.cont_multi_map_in_function( - "moveaxis", - a, - source, - destination, - copy=copy, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - out=out, - ) - - def moveaxis( - self: ivy.Container, - source: Union[int, Sequence[int], ivy.Container], - destination: Union[int, Sequence[int], ivy.Container], - /, - *, - copy: Optional[Union[bool, ivy.Container]] = None, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container instance method variant of ivy.moveaxis. This method simply wraps - the function, and so the docstring for ivy.flatten also applies to this method - with minimal changes. - - Parameters - ---------- - self - The container with the arrays whose axes should be reordered. - source - Original positions of the axes to move. These must be unique. - destination - Destination positions for each of the original axes. - These must also be unique. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - out - optional output container, for writing the result to. - - Returns - ------- - ret - Container including arrays with moved axes. - - Examples - -------- - With one :class:`ivy.Container` input: - >>> x = ivy.Container(a=ivy.zeros((3, 4, 5)), b=ivy.zeros((2,7,6))) - >>> x.moveaxis(, 0, -1).shape - { - a: (4, 5, 3) - b: (7, 6, 2) - } - """ - return self.static_moveaxis(self, source, destination, copy=copy, out=out) - - @staticmethod - def static_heaviside( - x1: Union[ivy.Array, ivy.NativeArray, ivy.Container], - x2: Union[ivy.Array, ivy.NativeArray, ivy.Container], - /, - *, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container static method variant of ivy.heaviside. This method simply wraps - the function, and so the docstring for ivy.heaviside also applies to this method - with minimal changes. - - Parameters - ---------- - x1 - input container including the arrays. - x2 - values to use where the array is zero. - out - optional output container array, for writing the result to. - - Returns - ------- - ret - output container with element-wise Heaviside step function of each array. - - Examples - -------- - With :class:`ivy.Array` input: - >>> x1 = ivy.Container(a=ivy.array([-1.5, 0, 2.0]), b=ivy.array([3.0, 5.0]) - >>> x2 = ivy.Container(a=0.5, b=[1.0, 2.0]) - >>> ivy.Container.static_heaviside(x1, x2) - { - a: ivy.array([ 0. , 0.5, 1. ]) - b: ivy.array([1.0, 1.0]) - } - """ - return ContainerBase.cont_multi_map_in_function( - "heaviside", - x1, - x2, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - out=out, - ) - - def heaviside( - self: ivy.Container, - x2: ivy.Container, - /, - *, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container instance method variant of ivy.heaviside. This method simply wraps - the function, and so the docstring for ivy.heaviside also applies to this method - with minimal changes. - - Parameters - ---------- - self - input container including the arrays. - x2 - values to use where the array is zero. - out - optional output container array, for writing the result to. - - Returns - ------- - ret - output container with element-wise Heaviside step function of each array. - - Examples - -------- - With :class:`ivy.Array` input: - >>> x1 = ivy.Container(a=ivy.array([-1.5, 0, 2.0]), b=ivy.array([3.0, 5.0]) - >>> x2 = ivy.Container(a=0.5, b=[1.0, 2.0]) - >>> x1.heaviside(x2) - { - a: ivy.array([ 0. , 0.5, 1. ]) - b: ivy.array([1.0, 1.0]) - } - """ - return self.static_heaviside(self, x2, out=out) - - @staticmethod - def static_flipud( - m: Union[ivy.Array, ivy.NativeArray, ivy.Container], - /, - *, - copy: Optional[Union[bool, ivy.Container]] = None, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container static method variant of ivy.flipud. This method simply wraps the - function, and so the docstring for ivy.flipud also applies to this method with - minimal changes. - - Parameters - ---------- - m - the container with arrays to be flipped. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - out - optional output container, for writing the result to. - - Returns - ------- - ret - container including arrays corresponding to the input container's array - with elements order reversed along axis 0. - - Examples - -------- - With one :class:`ivy.Container` input: - - >>> m = ivy.Container(a=ivy.diag([1, 2, 3]), b=ivy.arange(4)) - >>> ivy.Container.static_flipud(m) - { - a: ivy.array( - [[ 0., 0., 3.], - [ 0., 2., 0.], - [ 1., 0., 0.]] - ) - b: ivy.array([3, 2, 1, 0]) - } - """ - return ContainerBase.cont_multi_map_in_function( - "flipud", - m, - copy=copy, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - out=out, - ) - - def flipud( - self: ivy.Container, - /, - *, - copy: Optional[Union[bool, ivy.Container]] = None, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container instance method variant of ivy.flipud. This method simply wraps - the function, and so the docstring for ivy.flipud also applies to this method - with minimal changes. - - Parameters - ---------- - self - the container with arrays to be flipped. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - out - optional output container, for writing the result to. - - Returns - ------- - ret - container including arrays corresponding to the input container's array - with elements order reversed along axis 0. - - Examples - -------- - With one :class:`ivy.Container` input: - - >>> m = ivy.Container(a=ivy.diag([1, 2, 3]), b=ivy.arange(4)) - >>> m.flipud() - { - a: ivy.array( - [[ 0., 0., 3.], - [ 0., 2., 0.], - [ 1., 0., 0.]] - ) - b: ivy.array([3, 2, 1, 0]) - } - """ - return self.static_flipud(self, copy=copy, out=out) - - def vstack( - self: ivy.Container, - /, - xs: Union[ - Tuple[Union[ivy.Array, ivy.NativeArray, ivy.Container]], - List[Union[ivy.Array, ivy.NativeArray, ivy.Container]], - ], - *, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container instance method variant of ivy.stack. This method simply wraps the - function, and so the docstring for ivy.stack also applies to this method with - minimal changes. - - Examples - -------- - >>> x = ivy.Container(a=ivy.array([[0, 1], [2,3]]), b=ivy.array([[4, 5]])) - >>> y = ivy.Container(a=ivy.array([[3, 2], [1,0]]), b=ivy.array([[1, 0]])) - >>> x.vstack([y]) - { - a: ivy.array([[[0, 1], - [2, 3]], - [[3, 2], - [1, 0]]]), - b: ivy.array([[[4, 5]], - [[1, 0]]]) - } - """ - new_xs = xs.cont_copy() if ivy.is_ivy_container(xs) else xs.copy() - new_xs.insert(0, self.cont_copy()) - return self.static_vstack( - new_xs, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - out=out, - ) - - @staticmethod - def static_vstack( - xs: Union[ - Tuple[Union[ivy.Array, ivy.NativeArray, ivy.Container]], - List[Union[ivy.Array, ivy.NativeArray, ivy.Container]], - ], - /, - *, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container static method variant of ivy.stack. This method simply wraps the - function, and so the docstring for ivy.vstack also applies to this method with - minimal changes. - - Examples - -------- - With one :class:`ivy.Container` input: - - >>> c = ivy.Container(a=[ivy.array([1,2,3]), ivy.array([0,0,0])], - b=ivy.arange(3)) - >>> y = ivy.Container.static_vstack(c) - >>> print(y) - { - a: ivy.array([[1, 2, 3], - [0, 0, 0]]), - b: ivy.array([[0], - [1], - [2]]) - } - """ - return ContainerBase.cont_multi_map_in_function( - "vstack", - xs, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - out=out, - ) - - def hstack( - self: ivy.Container, - /, - xs: Union[ - Tuple[Union[ivy.Array, ivy.NativeArray, ivy.Container]], - List[Union[ivy.Array, ivy.NativeArray, ivy.Container]], - ], - *, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container instance method variant of ivy.hstack. This method simply wraps - the function, and so the docstring for ivy.hstack also applies to this method - with minimal changes. - - Examples - -------- - >>> x = ivy.Container(a=ivy.array([[0, 1], [2,3]]), b=ivy.array([[4, 5]])) - >>> y = ivy.Container(a=ivy.array([[3, 2], [1,0]]), b=ivy.array([[1, 0]])) - >>> z = x.hstack([y]) - >>> print(z) - { - a: ivy.array([[0, 1, 3, 2], - [2, 3, 1, 0]]), - b: ivy.array([[4, 5, 1, 0]]) - } - """ - new_xs = xs.cont_copy() if ivy.is_ivy_container(xs) else xs.copy() - new_xs.insert(0, self.cont_copy()) - return self.static_hstack( - new_xs, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - out=out, - ) - - @staticmethod - def static_hstack( - xs: Union[ - Tuple[Union[ivy.Array, ivy.NativeArray, ivy.Container]], - List[Union[ivy.Array, ivy.NativeArray, ivy.Container]], - ], - /, - *, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container static method variant of ivy.hstack. This method simply wraps the - function, and so the docstring for ivy.hstack also applies to this method with - minimal changes. - - Examples - -------- - With one :class:`ivy.Container` input: - >>> c = ivy.Container(a=[ivy.array([1,2,3]), ivy.array([0,0,0])]) - >>> ivy.Container.static_hstack(c) - { - a: ivy.array([1, 2, 3, 0, 0, 0]) - } - """ - return ContainerBase.cont_multi_map_in_function( - "hstack", - xs, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - out=out, - ) - - @staticmethod - def static_rot90( - m: Union[ivy.Container, ivy.Array, ivy.NativeArray], - /, - *, - copy: Union[bool, ivy.Container] = None, - k: Union[int, ivy.Container] = 1, - axes: Union[Tuple[int, int], ivy.Container] = (0, 1), - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container static method variant of ivy.rot90. This method simply wraps the - function, and so the docstring for ivy.rot90 also applies to this method with - minimal changes. - - Parameters - ---------- - m - Input array of two or more dimensions. - k - Number of times the array is rotated by 90 degrees. - axes - The array is rotated in the plane defined by the axes. Axes must be - different. - key_chains - The key-chains to apply or not apply the method to. Default is None. - to_apply - If True, the method will be applied to key_chains, otherwise key_chains - will be skipped. Default is True. - prune_unapplied - Whether to prune key_chains for which the function was not applied. - Default is False. - map_sequences - Whether to also map method to sequences (lists, tuples). Default is False. - out - optional output container, for writing the result to. It must have a shape - that the inputs broadcast to. - - Returns - ------- - ret - Container with a rotated view of m. - - Examples - -------- - >>> m = ivy.Container(a=ivy.array([[1,2], [3,4]]),\ - b=ivy.array([[1,2,3,4],\ - [7,8,9,10]])) - >>> n = ivy.Container.static_rot90(m) - >>> print(n) - { - a: ivy.array([[2, 4], - [1, 3]]), - b: ivy.array([[4, 10], - [3, 9], - [2, 8], - [1, 7]]) - } - """ - return ContainerBase.cont_multi_map_in_function( - "rot90", - m, - copy=copy, - k=k, - axes=axes, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - out=out, - ) - - def rot90( - self: Union[ivy.Container, ivy.Array, ivy.NativeArray], - /, - *, - copy: Union[bool, ivy.Container] = None, - k: Union[int, ivy.Container] = 1, - axes: Union[Tuple[int, int], ivy.Container] = (0, 1), - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container static method variant of ivy.rot90. This method simply wraps the - function, and so the docstring for ivy.rot90 also applies to this method with - minimal changes. - - Parameters - ---------- - self - Input array of two or more dimensions. - k - Number of times the array is rotated by 90 degrees. - axes - The array is rotated in the plane defined by the axes. Axes must be - different. - key_chains - The key-chains to apply or not apply the method to. Default is None. - to_apply - If True, the method will be applied to key_chains, otherwise key_chains - will be skipped. Default is True. - prune_unapplied - Whether to prune key_chains for which the function was not applied. - Default is False. - map_sequences - Whether to also map method to sequences (lists, tuples). Default is False. - out - optional output container, for writing the result to. It must have a shape - that the inputs broadcast to. - - Returns - ------- - ret - Container with a rotated view of input array. - - Examples - -------- - >>> m = ivy.Container(a=ivy.array([[1,2], [3,4]]),\ - ... b=ivy.array([[1,2,3,4],[7,8,9,10]])) - >>> n = m.rot90() - >>> print(n) - { - a: ivy.array([[2, 4], - [1, 3]]), - b: ivy.array([[4, 10], - [3, 9], - [2, 8], - [1, 7]]) - } - """ - return self.static_rot90( - self, - copy=copy, - k=k, - axes=axes, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - out=out, - ) - - @staticmethod - def static_top_k( - x: Union[ivy.Container, ivy.Array, ivy.NativeArray], - k: Union[int, ivy.Container], - /, - *, - axis: Union[int, ivy.Container] = -1, - largest: Union[bool, ivy.Container] = True, - sorted: Union[bool, ivy.Container] = True, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - out: Optional[Union[Tuple[ivy.Container, ivy.Container], ivy.Container]] = None, - ) -> Tuple[ivy.Container, ivy.Container]: - """ - ivy.Container static method variant of ivy.top_k. This method simply wraps the - function, and so the docstring for ivy.top_k also applies to this method with - minimal changes. - - Parameters - ---------- - x - The container to compute top_k for. - k - Number of top elements to retun must not exceed the array size. - axis - The axis along which we must return the top elements default value is 1. - largest - If largest is set to False we return k smallest elements of the array. - sorted - If sorted is set to True we return the elements in sorted order. - key_chains - The key-chains to apply or not apply the method to. Default is ``None``. - to_apply - If True, the method will be applied to key_chains, otherwise key_chains - will be skipped. Default is ``True``. - prune_unapplied - Whether to prune key_chains for which the function was not applied. - Default is ``False``. - map_sequences - Whether to also map method to sequences (lists, tuples). - Default is ``False`` - out: - Optional output tuple, for writing the result to. Must have two Container, - with a shape that the returned tuple broadcast to. - - Returns - ------- - ret - a container with indices and values. - - Examples - -------- - With :class:`ivy.Container` input: - - >>> x = ivy.Container(a=ivy.array([-1, 2, -4]), b=ivy.array([4., 5., 0.])) - >>> y = ivy.Container.static_top_k(x, 2) - >>> print(y) - { - a: [ - values = ivy.array([ 2, -1]), - indices = ivy.array([1, 0]) - ], - b: [ - values = ivy.array([5., 4.]), - indices = ivy.array([1, 0]) - ] - } - """ - return ContainerBase.cont_multi_map_in_function( - "top_k", - x, - k, - axis=axis, - largest=largest, - sorted=sorted, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - out=out, - ) - - def top_k( - self: ivy.Container, - k: Union[int, ivy.Container], - /, - *, - axis: Union[int, ivy.Container] = -1, - largest: Union[bool, ivy.Container] = True, - sorted: Union[bool, ivy.Container] = True, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - out: Optional[Tuple[ivy.Container, ivy.Container]] = None, - ) -> Tuple[ivy.Container, ivy.Container]: - """ - ivy.Container instance method variant of ivy.top_k. This method simply wraps the - function, and so the docstring for ivy.top_k also applies to this method with - minimal changes. - - Parameters - ---------- - self - The container to compute top_k for. - k - Number of top elements to retun must not exceed the array size. - axis - The axis along which we must return the top elements default value is 1. - largest - If largest is set to False we return k smallest elements of the array. - sorted - If sorted is set to True we return the elements in sorted order. - key_chains - The key-chains to apply or not apply the method to. Default is ``None``. - to_apply - If True, the method will be applied to key_chains, otherwise key_chains - will be skipped. Default is ``True``. - prune_unapplied - Whether to prune key_chains for which the function was not applied. - Default is ``False``. - map_sequences - Whether to also map method to sequences (lists, tuples). - Default is ``False`` - out: - Optional output tuple, for writing the result to. Must have two Container, - with a shape that the returned tuple broadcast to. - - Returns - ------- - ret - a container with indices and values. - - Examples - -------- - With :class:`ivy.Container` input: - - >>> x = ivy.Container(a=ivy.array([-1, 2, -4]), b=ivy.array([4., 5., 0.])) - >>> y = x.top_k(2) - >>> print(y) - { - a: [ - values = ivy.array([ 2, -1]), - indices = ivy.array([1, 0]) - ], - b: [ - values = ivy.array([5., 4.]), - indices = ivy.array([1, 0]) - ] - } - """ - return self.static_top_k( - self, - k, - axis=axis, - largest=largest, - sorted=sorted, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - out=out, - ) - - @staticmethod - def static_fliplr( - m: Union[ivy.Array, ivy.NativeArray, ivy.Container], - /, - *, - copy: Optional[Union[bool, ivy.Container]] = None, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container static method variant of ivy.fliplr. This method simply wraps the - function, and so the docstring for ivy.fliplr also applies to this method with - minimal changes. - - Parameters - ---------- - m - the container with arrays to be flipped. Arrays must be at least 2-D. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - key_chains - The key-chains to apply or not apply the method to. Default is ``None``. - to_apply - If True, the method will be applied to key_chains, otherwise key_chains - will be skipped. Default is ``True``. - prune_unapplied - Whether to prune key_chains for which the function was not applied. - Default is ``False``. - map_sequences - Whether to also map method to sequences (lists, tuples). - Default is ``False`` - out - optional output container, for writing the result to. - - Returns - ------- - ret - container including arrays corresponding to the input container's array - with elements order reversed along axis 1. - - Examples - -------- - With one :class:`ivy.Container` input: - >>> m = ivy.Container(a=ivy.diag([1, 2, 3]),\ - ... b=ivy.array([[1, 2, 3],[4, 5, 6]])) - >>> ivy.Container.static_fliplr(m) - { - a: ivy.array([[0, 0, 1], - [0, 2, 0], - [3, 0, 0]]), - b: ivy.array([[3, 2, 1], - [6, 5, 4]]) - } - """ - return ContainerBase.cont_multi_map_in_function( - "fliplr", - m, - copy=copy, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - out=out, - ) - - def fliplr( - self: ivy.Container, - /, - *, - copy: Optional[Union[bool, ivy.Container]] = None, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container instance method variant of ivy.fliplr. This method simply wraps - the function, and so the docstring for ivy.fliplr also applies to this method - with minimal changes. - - Parameters - ---------- - self - the container with arrays to be flipped. Arrays must be at least 2-D. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - out - optional output container, for writing the result to. - - Returns - ------- - ret - container including arrays corresponding to the input container's array - with elements order reversed along axis 1. - - Examples - -------- - With one :class:`ivy.Container` input: - - >>> m = ivy.Container(a=ivy.diag([1, 2, 3]),\ - ... b=ivy.array([[1, 2, 3],[4, 5, 6]])) - >>> m.fliplr() - { - a: ivy.array([[0, 0, 1], - [0, 2, 0], - [3, 0, 0]]), - b: ivy.array([[3, 2, 1], - [6, 5, 4]]) - } - """ - return self.static_fliplr(self, copy=copy, out=out) - - @staticmethod - def static_i0( - x: Union[ivy.Array, ivy.NativeArray, ivy.Container], - /, - *, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container static method variant of ivy.i0. This method simply wraps the - function, and so the docstring for ivy.i0 also applies to this method with - minimal changes. - - Parameters - ---------- - x - the container with array inputs. - out - optional output container, for writing the result to. - - Returns - ------- - ret - container including arrays with the modified Bessel - function evaluated at each of the elements of x. - - Examples - -------- - With one :class:`ivy.Container` input: - - >>> x = ivy.Container(a=ivy.array([1, 2, 3]), b=ivy.array(4)) - >>> ivy.Container.static_i0(x) - { - a: ivy.array([1.26606588, 2.2795853 , 4.88079259]) - b: ivy.array(11.30192195) - } - """ - return ContainerBase.cont_multi_map_in_function( - "i0", - x, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - out=out, - ) - - def i0( - self: ivy.Container, - /, - *, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container instance method variant of ivy.i0. This method simply wraps the - function, and so the docstring for ivy.i0 also applies to this method with - minimal changes. - - Parameters - ---------- - self - the container with array inputs. - out - optional output container, for writing the result to. - - Returns - ------- - ret - container including arrays with the modified Bessel - function evaluated at each of the elements of x. - - Examples - -------- - With one :class:`ivy.Container` input: - - >>> x = ivy.Container(a=ivy.array([1, 2, 3]), b=ivy.array(4)) - >>> x.i0() - { - a: ivy.array([1.26606588, 2.2795853 , 4.88079259]) - b: ivy.array(11.30192195) - } - """ - return self.static_i0(self, out=out) - - @staticmethod - def static_flatten( - x: Union[ivy.Array, ivy.NativeArray, ivy.Container], - /, - *, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - copy: Optional[Union[bool, ivy.Container]] = None, - start_dim: Union[int, ivy.Container] = 0, - end_dim: Union[int, ivy.Container] = -1, - order: Union[str, ivy.Container] = "C", - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container static method variant of ivy.flatten. This method simply wraps the - function, and so the docstring for ivy.flatten also applies to this method with - minimal changes. - - Parameters - ---------- - x - input container to flatten at leaves. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - start_dim - first dim to flatten. If not set, defaults to 0. - end_dim - last dim to flatten. If not set, defaults to -1. - order - Read the elements of the input container using this index order, - and place the elements into the reshaped array using this index order. - ‘C’ means to read / write the elements using C-like index order, - with the last axis index changing fastest, back to the first axis index - changing slowest. - ‘F’ means to read / write the elements using Fortran-like index order, with - the first index changing fastest, and the last index changing slowest. - Note that the ‘C’ and ‘F’ options take no account of the memory layout - of the underlying array, and only refer to the order of indexing. - Default order is 'C' - - Returns - ------- - ret - Container with arrays flattened at leaves. - - Examples - -------- - With one :class:`ivy.Container` input: - - >>> x = ivy.Container(a=ivy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]), - ... b=ivy.array([[[9, 10], [11, 12]], [[13, 14], [15, 16]]])) - >>> ivy.flatten(x) - [{ - a: ivy.array([1, 2, 3, 4, 5, 6, 7, 8]) - b: ivy.array([9, 10, 11, 12, 13, 14, 15, 16]) - }] - - >>> x = ivy.Container(a=ivy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]), - ... b=ivy.array([[[9, 10], [11, 12]], [[13, 14], [15, 16]]])) - >>> ivy.flatten(x, order="F") - [{ - a: ivy.array([1, 5, 3, 7, 2, 6, 4, 8]) - b: ivy.array([9, 13, 11, 15, 10, 14, 12, 16]) - }] - """ - return ContainerBase.cont_multi_map_in_function( - "flatten", - x, - copy=copy, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - start_dim=start_dim, - end_dim=end_dim, - order=order, - out=out, - ) - - def flatten( - self: ivy.Container, - *, - copy: Optional[Union[bool, ivy.Container]] = None, - start_dim: Union[int, ivy.Container] = 0, - end_dim: Union[int, ivy.Container] = -1, - order: Union[str, ivy.Container] = "C", - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container instance method variant of ivy.flatten. This method simply wraps - the function, and so the docstring for ivy.flatten also applies to this method - with minimal changes. - - Parameters - ---------- - self - input container to flatten at leaves. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - start_dim - first dim to flatten. If not set, defaults to 0. - end_dim - last dim to flatten. If not set, defaults to -1. - order - Read the elements of the input container using this index order, - and place the elements into the reshaped array using this index order. - ‘C’ means to read / write the elements using C-like index order, - with the last axis index changing fastest, back to the first axis index - changing slowest. - ‘F’ means to read / write the elements using Fortran-like index order, with - the first index changing fastest, and the last index changing slowest. - Note that the ‘C’ and ‘F’ options take no account of the memory layout - of the underlying array, and only refer to the order of indexing. - Default order is 'C' - - Returns - ------- - ret - Container with arrays flattened at leaves. - - Examples - -------- - With one :class:`ivy.Container` input: - - >>> x = ivy.Container(a=ivy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]), - ... b=ivy.array([[[9, 10], [11, 12]], [[13, 14], [15, 16]]])) - >>> x.flatten() - [{ - a: ivy.array([1, 2, 3, 4, 5, 6, 7, 8]) - b: ivy.array([9, 10, 11, 12, 13, 14, 15, 16]) - }] - - >>> x = ivy.Container(a=ivy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]), - ... b=ivy.array([[[9, 10], [11, 12]], [[13, 14], [15, 16]]])) - >>> x.flatten(order="F") - [{ - a: ivy.array([1, 5, 3, 7, 2, 6, 4, 8]) - b: ivy.array([9, 13, 11, 15, 10, 14, 12, 16]) - }] - """ - return self.static_flatten( - self, copy=copy, start_dim=start_dim, end_dim=end_dim, out=out, order=order - ) - - @staticmethod - def static_pad( - input: ivy.Container, - pad_width: Union[Iterable[Tuple[int]], int, ivy.Container], - /, - *, - mode: Union[ - Literal[ - "constant", - "dilated", - "edge", - "linear_ramp", - "maximum", - "mean", - "median", - "minimum", - "reflect", - "symmetric", - "wrap", - "empty", - ], - Callable, - ivy.Container, - ] = "constant", - stat_length: Union[Iterable[Tuple[int]], int, ivy.Container] = 1, - constant_values: Union[Iterable[Tuple[Number]], Number, ivy.Container] = 0, - end_values: Union[Iterable[Tuple[Number]], Number, ivy.Container] = 0, - reflect_type: Union[Literal["even", "odd"], ivy.Container] = "even", - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - out: Optional[ivy.Container] = None, - **kwargs: Optional[Union[Any, ivy.Container]], - ) -> ivy.Container: - """ - ivy.Container static method variant of ivy.pad. - - This method simply wraps the function, and so the docstring for - ivy.pad also applies to this method with minimal changes. - """ - return ContainerBase.cont_multi_map_in_function( - "pad", - input, - pad_width, - mode=mode, - stat_length=stat_length, - constant_values=constant_values, - end_values=end_values, - reflect_type=reflect_type, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - out=out, - **kwargs, - ) - - def pad( - self: ivy.Container, - pad_width: Union[Iterable[Tuple[int]], int, ivy.Container], - /, - *, - mode: Union[ - Literal[ - "constant", - "dilated", - "edge", - "linear_ramp", - "maximum", - "mean", - "median", - "minimum", - "reflect", - "symmetric", - "wrap", - "empty", - ], - Callable, - ivy.Container, - ] = "constant", - stat_length: Union[Iterable[Tuple[int]], int, ivy.Container] = 1, - constant_values: Union[Iterable[Tuple[Number]], Number, ivy.Container] = 0, - end_values: Union[Iterable[Tuple[Number]], Number, ivy.Container] = 0, - reflect_type: Union[Literal["even", "odd"], ivy.Container] = "even", - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - out: Optional[ivy.Container] = None, - **kwargs: Optional[Union[Any, ivy.Container]], - ) -> ivy.Container: - """ - ivy.Container instance method variant of ivy.pad. - - This method simply wraps the function, and so the docstring for - ivy.pad also applies to this method with minimal changes. - """ - return self.static_pad( - self, - pad_width, - mode=mode, - stat_length=stat_length, - constant_values=constant_values, - end_values=end_values, - reflect_type=reflect_type, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - out=out, - **kwargs, - ) - - @staticmethod - def static_vsplit( - ary: Union[ivy.Array, ivy.NativeArray, ivy.Container], - indices_or_sections: Union[ - int, Sequence[int], ivy.Array, ivy.NativeArray, ivy.Container - ], - /, - *, - copy: Optional[Union[bool, ivy.Container]] = None, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - ) -> List[ivy.Container]: - """ - ivy.Container static method variant of ivy.vsplit. This method simply wraps the - function, and so the docstring for ivy.vsplit also applies to this method with - minimal changes. - - Parameters - ---------- - ary - the container with array inputs. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - indices_or_sections - If indices_or_sections is an integer n, the array is split into n - equal sections, provided that n must be a divisor of the split axis. - If indices_or_sections is a sequence of ints or 1-D array, - then input is split at each of the indices. - key_chains - The key-chains to apply or not apply the method to. Default is None. - to_apply - If True, the method will be applied to key_chains, otherwise key_chains - will be skipped. Default is True. - prune_unapplied - Whether to prune key_chains for which the function was not applied. - Default is False. - map_sequences - Whether to also map method to sequences (lists, tuples). Default is False. - - Returns - ------- - ret - list of containers holding arrays split vertically from the input - - Examples - -------- - >>> ary = ivy.Container( - a = ivy.array( - [[[0., 1.], - [2., 3.]], - [[4., 5.], - [6., 7.]]] - ), - b=ivy.array( - [[ 0., 1., 2., 3.], - [ 4., 5., 6., 7.], - [ 8., 9., 10., 11.], - [12., 13., 14., 15.]] - ) - ) - >>> ivy.Container.static_vsplit(ary, 2) - [{ - a: ivy.array([[[0., 1.], - [2., 3.]]]), - b: ivy.array([[0., 1., 2., 3.], - [4., 5., 6., 7.]]) - }, { - a: ivy.array([[[4., 5.], - [6., 7.]]]), - b: ivy.array([[8., 9., 10., 11.], - [12., 13., 14., 15.]]) - }] - """ - return ContainerBase.cont_multi_map_in_function( - "vsplit", - ary, - indices_or_sections, - copy=copy, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - ) - - def vsplit( - self: ivy.Container, - indices_or_sections: Union[ - int, Sequence[int], ivy.Array, ivy.NativeArray, ivy.Container - ], - /, - *, - copy: Optional[Union[bool, ivy.Container]] = None, - ) -> List[ivy.Container]: - """ - ivy.Container instance method variant of ivy.vsplit. This method simply wraps - the function, and so the docstring for ivy.vsplit also applies to this method - with minimal changes. - - Parameters - ---------- - self - the container with array inputs. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - indices_or_sections - If indices_or_sections is an integer n, the array is split into n - equal sections, provided that n must be a divisor of the split axis. - If indices_or_sections is a sequence of ints or 1-D array, - then input is split at each of the indices. - - Returns - ------- - ret - list of containers holding arrays split vertically from the input - - Examples - -------- - >>> ary = ivy.Container( - a = ivy.array( - [[[0., 1.], - [2., 3.]], - [[4., 5.], - [6., 7.]]] - ), - b=ivy.array( - [[ 0., 1., 2., 3.], - [ 4., 5., 6., 7.], - [ 8., 9., 10., 11.], - [12., 13., 14., 15.]] - ) - ) - >>> ary.vsplit(2) - [{ - a: ivy.array([[[0., 1.], - [2., 3.]]]), - b: ivy.array([[0., 1., 2., 3.], - [4., 5., 6., 7.]]) - }, { - a: ivy.array([[[4., 5.], - [6., 7.]]]), - b: ivy.array([[8., 9., 10., 11.], - [12., 13., 14., 15.]]) - }] - """ - return self.static_vsplit(self, indices_or_sections, copy=copy) - - @staticmethod - def static_dsplit( - ary: Union[ivy.Array, ivy.NativeArray, ivy.Container], - indices_or_sections: Union[ - int, Sequence[int], ivy.Array, ivy.NativeArray, ivy.Container - ], - /, - *, - copy: Optional[Union[bool, ivy.Container]] = None, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - ) -> List[ivy.Container]: - """ - ivy.Container static method variant of ivy.dsplit. This method simply wraps the - function, and so the docstring for ivy.dsplit also applies to this method with - minimal changes. - - Parameters - ---------- - ary - the container with array inputs. - indices_or_sections - If indices_or_sections is an integer n, the array is split into n - equal sections, provided that n must be a divisor of the split axis. - If indices_or_sections is a sequence of ints or 1-D array, - then input is split at each of the indices. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - key_chains - The key-chains to apply or not apply the method to. Default is None. - to_apply - If True, the method will be applied to key_chains, otherwise key_chains - will be skipped. Default is True. - prune_unapplied - Whether to prune key_chains for which the function was not applied. - Default is False. - map_sequences - Whether to also map method to sequences (lists, tuples). Default is False. - - Returns - ------- - ret - list of containers holding arrays split from the input at the 3rd axis - - Examples - -------- - >>> ary = ivy.Container( - a = ivy.array( - [[[0., 1.], - [2., 3.]], - [[4., 5.], - [6., 7.]]] - ), - b=ivy.array( - [[[ 0., 1., 2., 3.], - [ 4., 5., 6., 7.], - [ 8., 9., 10., 11.], - [12., 13., 14., 15.]]] - ) - ) - >>> ivy.Container.static_dsplit(ary, 2) - [{ - a: ivy.array([[[0.], [2.]], - [[4.], [6.]]]), - b: ivy.array([[[0., 1.], [4., 5.], [8., 9.], [12., 13.]]]) - }, { - a: ivy.array([[[1.], [3.]], - [[5.], [7.]]]), - b: ivy.array([[[2., 3.], [6., 7.], [10., 11.], [14., 15.]]]) - }] - """ - return ContainerBase.cont_multi_map_in_function( - "dsplit", - ary, - indices_or_sections, - copy=copy, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - ) - - def dsplit( - self: ivy.Container, - indices_or_sections: Union[ - int, Sequence[int], ivy.Array, ivy.NativeArray, ivy.Container - ], - /, - *, - copy: Optional[Union[bool, ivy.Container]] = None, - ) -> List[ivy.Container]: - """ - ivy.Container instance method variant of ivy.dsplit. This method simply wraps - the function, and so the docstring for ivy.dsplit also applies to this method - with minimal changes. - - Parameters - ---------- - self - the container with array inputs. - indices_or_sections - If indices_or_sections is an integer n, the array is split into n - equal sections, provided that n must be a divisor of the split axis. - If indices_or_sections is a sequence of ints or 1-D array, - then input is split at each of the indices. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - - Returns - ------- - ret - list of containers holding arrays split from the input at the 3rd axis - - Examples - -------- - >>> ary = ivy.Container( - a = ivy.array( - [[[0., 1.], - [2., 3.]], - [[4., 5.], - [6., 7.]]] - ), - b=ivy.array( - [[[ 0., 1., 2., 3.], - [ 4., 5., 6., 7.], - [ 8., 9., 10., 11.], - [12., 13., 14., 15.]]] - ) - ) - >>> ary.dsplit(2) - [{ - a: ivy.array([[[0.], [2.]], - [[4.], [6.]]]), - b: ivy.array([[[0., 1.], [4., 5.], [8., 9.], [12., 13.]]]) - }, { - a: ivy.array([[[1.], [3.]], - [[5.], [7.]]]), - b: ivy.array([[[2., 3.], [6., 7.], [10., 11.], [14., 15.]]]) - }] - """ - return self.static_dsplit(self, indices_or_sections, copy=copy) - - @staticmethod - def static_atleast_1d( - *arys: Union[ivy.Array, ivy.NativeArray, ivy.Container], - copy: Optional[Union[bool, ivy.Container]] = None, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - ) -> List[ivy.Container]: - """ - ivy.Container static method variant of ivy.atleast_1d. This method simply wraps - the function, and so the docstring for ivy.atleast_1d also applies to this - method with minimal changes. - - Parameters - ---------- - arys - one or more container with array inputs. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - key_chains - The keychains to apply or not apply the method to. Default is ``None``. - to_apply - If True, the method will be applied to key_chains, otherwise key_chains - will be skipped. Default is ``True``. - prune_unapplied - Whether to prune key_chains for which the function was not applied. - Default is ``False``. - map_sequences - Whether to also map method to sequences (lists, tuples). - Default is ``False``. - - Returns - ------- - ret - container or list of container where each elements within container is - atleast 1d. Copies are made only if necessary. - - Examples - -------- - >>> ary = ivy.Container(a=ivy.array(1), b=ivy.array([3,4,5]),\ - c=ivy.array([[3]])) - >>> ivy.Container.static_atleast_1d(ary) - { - a: ivy.array([1]), - b: ivy.array([3, 4, 5]), - c: ivy.array([[3]]), - } - """ - return ContainerBase.cont_multi_map_in_function( - "atleast_1d", - *arys, - copy=copy, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - ) - - def atleast_1d( - self: Union[ivy.Container, ivy.Array, ivy.NativeArray], - *arys: Union[ivy.Container, ivy.Array, ivy.NativeArray, bool, Number], - copy: Optional[Union[bool, ivy.Container]] = None, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - ) -> List[ivy.Container]: - """ - ivy.Container instance method variant of ivy.atleast_1d. This method simply - wraps the function, and so the docstring for ivy.atleast_1d also applies to this - method with minimal changes. - - Parameters - ---------- - self - the container with array inputs. - arys - one or more container with array inputs. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - key_chains - The keychains to apply or not apply the method to. Default is ``None``. - to_apply - If True, the method will be applied to key_chains, otherwise key_chains - will be skipped. Default is ``True``. - prune_unapplied - Whether to prune key_chains for which the function was not applied. - Default is ``False``. - map_sequences - Whether to also map method to sequences (lists, tuples). - Default is ``False``. - - Returns - ------- - ret - container or list of container where each elements within container is - atleast 1d. Copies are made only if necessary. - - Examples - -------- - >>> ary1 = ivy.Container(a=ivy.array(1), b=ivy.array([3,4]),\ - c=ivy.array([[5]])) - >>> ary2 = ivy.Container(a=ivy.array(9), b=ivy.array(2),\ - c=ivy.array(3)) - >>> ary1.atleast_1d(ary2) - [{ - a: ivy.array([1]), - b: ivy.array([3, 4]), - c: ivy.array([[5]]) - }, { - a: ivy.array([9]), - b: ivy.array([2]), - c: ivy.array([3]) - }] - """ - return self.static_atleast_1d( - self, - *arys, - copy=copy, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - ) - - def dstack( - self: ivy.Container, - /, - xs: Union[ - Tuple[Union[ivy.Array, ivy.NativeArray, ivy.Container]], - List[Union[ivy.Array, ivy.NativeArray, ivy.Container]], - ], - *, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container instance method variant of ivy.stack. This method simply wraps the - function, and so the docstring for ivy.stack also applies to this method with - minimal changes. - - Examples - -------- - >>> x = ivy.Container(a=ivy.array([[0, 1], [2,3]]), b=ivy.array([[4, 5]])) - >>> y = ivy.Container(a=ivy.array([[3, 2], [1,0]]), b=ivy.array([[1, 0]])) - >>> x.dstack([y]) - { - a: ivy.array([[[0, 3], - [1, 2]], - [[2, 1], - [3, 0]]]), - b: ivy.array([[[4, 1]], - [[5, 0]]]) - } - """ - new_xs = xs.cont_copy() if ivy.is_ivy_container(xs) else xs.copy() - new_xs.insert(0, self.cont_copy()) - return self.static_dstack( - new_xs, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - out=out, - ) - - @staticmethod - def static_dstack( - xs: Union[ - Tuple[Union[ivy.Array, ivy.NativeArray, ivy.Container]], - List[Union[ivy.Array, ivy.NativeArray, ivy.Container]], - ], - /, - *, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container static method variant of ivy.stack. This method simply wraps the - function, and so the docstring for ivy.dstack also applies to this method with - minimal changes. - - Examples - -------- - With one :class:`ivy.Container` input: - >>> c = ivy.Container(a=[ivy.array([1,2,3]), ivy.array([0,0,0])], - b=ivy.arange(3)) - >>> ivy.Container.static_dstack(c) - { - a: ivy.array([[1, 0], - [2, 0] - [3,0]]), - b: ivy.array([[0, 1, 2]) - } - """ - return ContainerBase.cont_multi_map_in_function( - "dstack", - xs, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - out=out, - ) - - @staticmethod - def static_atleast_2d( - *arys: Union[ivy.Array, ivy.NativeArray, ivy.Container], - copy: Optional[Union[bool, ivy.Container]] = None, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - ) -> List[ivy.Container]: - """ - ivy.Container static method variant of ivy.atleast_2d. This method simply wraps - the function, and so the docstring for ivy.atleast_2d also applies to this - method with minimal changes. - - Parameters - ---------- - arys - one or more container with array inputs. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - key_chains - The keychains to apply or not apply the method to. Default is ``None``. - to_apply - If True, the method will be applied to key_chains, otherwise key_chains - will be skipped. Default is ``True``. - prune_unapplied - Whether to prune key_chains for which the function was not applied. - Default is ``False``. - map_sequences - Whether to also map method to sequences (lists, tuples). - Default is ``False``. - - Returns - ------- - ret - container or list of container where each elements within container is - atleast 2D. Copies are made only if necessary. - - Examples - -------- - >>> ary = ivy.Container(a=ivy.array(1), b=ivy.array([3,4,5]),\ - c=ivy.array([[3]])) - >>> ivy.Container.static_atleast_2d(ary) - { - a: ivy.array([[1]]), - b: ivy.array([[3, 4, 5]]), - c: ivy.array([[3]]) - } - """ - return ContainerBase.cont_multi_map_in_function( - "atleast_2d", - *arys, - copy=copy, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - ) - - def atleast_2d( - self: Union[ivy.Container, ivy.Array, ivy.NativeArray], - *arys: Union[ivy.Container, ivy.Array, ivy.NativeArray], - copy: Optional[Union[bool, ivy.Container]] = None, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - ) -> List[ivy.Container]: - """ - ivy.Container instance method variant of ivy.atleast_2d. This method simply - wraps the function, and so the docstring for ivy.atleast_2d also applies to this - method with minimal changes. - - Parameters - ---------- - self - container with array inputs. - arys - one or more container with array inputs. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - key_chains - The keychains to apply or not apply the method to. Default is ``None``. - to_apply - If True, the method will be applied to key_chains, otherwise key_chains - will be skipped. Default is ``True``. - prune_unapplied - Whether to prune key_chains for which the function was not applied. - Default is ``False``. - map_sequences - Whether to also map method to sequences (lists, tuples). - Default is ``False``. - - Returns - ------- - ret - container or list of container where each elements within container is - atleast 2D. Copies are made only if necessary. - - Examples - -------- - >>> ary1 = ivy.Container(a=ivy.array(1), b=ivy.array([3,4]),\ - c=ivy.array([[5]])) - >>> ary2 = ivy.Container(a=ivy.array(9), b=ivy.array(2),\ - c=ivy.array(3)) - >>> ary1.atleast_2d(ary2) - [{ - a: ivy.array([[1]]), - b: ivy.array([[3, 4]]), - c: ivy.array([[5]]) - }, { - a: ivy.array([[9]]), - b: ivy.array([[2]]), - c: ivy.array([[3]]) - }] - """ - return self.static_atleast_2d( - self, - *arys, - copy=copy, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - ) - - @staticmethod - def static_atleast_3d( - *arys: Union[ivy.Array, ivy.NativeArray, ivy.Container], - copy: Optional[Union[bool, ivy.Container]] = None, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - ) -> List[ivy.Container]: - """ - ivy.Container static method variant of ivy.atleast_3d. This method simply wraps - the function, and so the docstring for ivy.atleast_3d also applies to this - method with minimal changes. - - Parameters - ---------- - arys - one or more container with array inputs. - copy - boolean indicating whether or not to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - key_chains - The keychains to apply or not apply the method to. Default is ``None``. - to_apply - If True, the method will be applied to key_chains, otherwise key_chains - will be skipped. Default is ``True``. - prune_unapplied - Whether to prune key_chains for which the function was not applied. - Default is ``False``. - map_sequences - Whether to also map method to sequences (lists, tuples). - Default is ``False``. - - Returns - ------- - ret - container or list of container where each elements within container is - atleast 3D. Copies are made only if necessary. For example, a 1-D array - of shape (N,) becomes a view of shape (1, N, 1), and a 2-D array of shape - (M, N) becomes a view of shape (M, N, 1). - - Examples - -------- - >>> ary = ivy.Container(a=ivy.array(1), b=ivy.array([3,4,5]),\ - c=ivy.array([[3]])) - >>> ivy.Container.static_atleast_3d(ary) - { - a: ivy.array([[[1]]]), - b: ivy.array([[[3], - [4], - [5]]]), - c: ivy.array([[[3]]]) - } - """ - return ContainerBase.cont_multi_map_in_function( - "atleast_3d", - *arys, - copy=copy, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - ) - - def atleast_3d( - self: Union[ivy.Container, ivy.Array, ivy.NativeArray], - *arys: Union[ivy.Container, ivy.Array, ivy.NativeArray, bool, Number], - copy: Optional[Union[bool, ivy.Container]] = None, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - ) -> List[ivy.Container]: - """ - ivy.Container instance method variant of ivy.atleast_3d. This method simply - wraps the function, and so the docstring for ivy.atleast_3d also applies to this - method with minimal changes. - - Parameters - ---------- - self - container with array inputs. - arys - one or more container with array inputs. - - key_chains - The keychains to apply or not apply the method to. Default is ``None``. - to_apply - If True, the method will be applied to key_chains, otherwise key_chains - will be skipped. Default is ``True``. - prune_unapplied - Whether to prune key_chains for which the function was not applied. - Default is ``False``. - map_sequences - Whether to also map method to sequences (lists, tuples). - Default is ``False``. - - Returns - ------- - ret - container or list of container where each elements within container is - atleast 3D. Copies are made only if necessary. For example, a 1-D array - of shape (N,) becomes a view of shape (1, N, 1), and a 2-D array of shape - (M, N) becomes a view of shape (M, N, 1). - - Examples - -------- - >>> ary1 = ivy.Container(a=ivy.array(1), b=ivy.array([3,4]),\ - c=ivy.array([[5]])) - >>> ary2 = ivy.Container(a=ivy.array(9), b=ivy.array(2),\ - c=ivy.array(3)) - >>> ary1.atleast_3d(ary2) - [{ - a: ivy.array([[[1]]]), - b: ivy.array([[[3], - [4]]]), - c: ivy.array([[[5]]]) - }, { - a: ivy.array([[[9]]]), - b: ivy.array([[[2]]]), - c: ivy.array([[[3]]]) - }] - """ - return self.static_atleast_3d( - self, - *arys, - copy=copy, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - ) - - @staticmethod - def static_take_along_axis( - arr: Union[ivy.Array, ivy.NativeArray, ivy.Container], - indices: Union[ivy.Array, ivy.NativeArray, ivy.Container], - axis: Union[int, ivy.Container], - mode: Union[str, ivy.Container] = "fill", - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container static method variant of ivy.take_along_axis. This method simply - wraps the function, and so the docstring for ivy.take_along_axis also applies to - this method with minimal changes. - - Parameters - ---------- - arr - container with array inputs. - indices - container with indices of the values to extract. - axis - The axis over which to select values. If axis is None, then arr and indices - must be 1-D sequences of the same length. - mode - One of: 'clip', 'fill', 'drop'. Parameter controlling how out-of-bounds - indices will be handled. - key_chains - The keychains to apply or not apply the method to. Default is ``None``. - to_apply - If True, the method will be applied to key_chains, otherwise key_chains - will be skipped. Default is ``True``. - prune_unapplied - Whether to prune key_chains for which the function was not applied. - Default is ``False``. - map_sequences - Whether to also map method to sequences (lists, tuples). - Default is ``False``. - out - optional output container, for writing the result to. - - Returns - ------- - ret - a container with arrays of the same shape as those in indices. - - Examples - -------- - >>> arr = ivy.Container(a=ivy.array([[1, 2], [3, 4]]),\ - b=ivy.array([[5, 6], [7, 8]])) - >>> indices = ivy.Container(a=ivy.array([[0, 0], [1, 1]]),\ - b=ivy.array([[1, 0], [1, 0]])) - >>> ivy.Container.static_take_along_axis(arr, indices, axis=1) - { - a: ivy.array([[1, 1], - [4, 4]]), - b: ivy.array([[6, 5], - [8, 7]]) - } - """ - return ContainerBase.cont_multi_map_in_function( - "take_along_axis", - arr, - indices, - axis, - mode=mode, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - out=out, - ) - - def take_along_axis( - self: Union[ivy.Container, ivy.Array, ivy.NativeArray], - indices: Union[ivy.Container, ivy.Array, ivy.NativeArray], - axis: Union[int, ivy.Container], - mode: Union[str, ivy.Container] = "fill", - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container instance method variant of ivy.take_along_axis. This method simply - wraps the function, and so the docstring for ivy.take_along_axis also applies to - this method with minimal changes. - - Parameters - ---------- - self - container with array inputs. - indices - container with indices of the values to extract. - axis - The axis over which to select values. If axis is None, then arr and indices - must be 1-D sequences of the same length. - mode - One of: 'clip', 'fill', 'drop'. Parameter controlling how out-of-bounds - indices will be handled. - key_chains - The keychains to apply or not apply the method to. Default is ``None``. - to_apply - If True, the method will be applied to key_chains, otherwise key_chains - will be skipped. Default is ``True``. - prune_unapplied - Whether to prune key_chains for which the function was not applied. - Default is ``False``. - map_sequences - Whether to also map method to sequences (lists, tuples). - Default is ``False``. - out - optional output container, for writing the result to. - - Returns - ------- - ret - a container with arrays of the same shape as those in indices. - - Examples - -------- - >>> arr = ivy.Container(a=ivy.array([[1, 2], [3, 4]]),\ - b=ivy.array([[5, 6], [7, 8]])) - >>> indices = ivy.Container(a=ivy.array([[0, 0], [1, 1]]),\ - b=ivy.array([[1, 0], [1, 0]])) - >>> arr.take_along_axis(indices, axis=1) - [{ - a: ivy.array([[1, 1], - [4, 4]]), - b: ivy.array([[6, 5], - [8, 7]]) - }] - """ - return self.static_take_along_axis( - self, - indices, - axis, - mode=mode, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - out=out, - ) - - @staticmethod - def static_hsplit( - ary: Union[ivy.Array, ivy.NativeArray, ivy.Container], - indices_or_sections: Union[ - int, Sequence[int], ivy.Array, ivy.NativeArray, ivy.Container - ], - /, - *, - copy: Optional[Union[bool, ivy.Container]] = None, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - ) -> List[ivy.Container]: - """ - ivy.Container static method variant of ivy.hsplit. This method simply wraps the - function, and so the docstring for ivy.hsplit also applies to this method with - minimal changes. - - Parameters - ---------- - ary - the container with array inputs. - indices_or_sections - If indices_or_sections is an integer n, the array is split into n - equal sections, provided that n must be a divisor of the split axis. - If indices_or_sections is a sequence of ints or 1-D array, - then input is split at each of the indices. - key_chains - The keychains to apply or not apply the method to. Default is ``None``. - to_apply - If True, the method will be applied to key_chains, otherwise key_chains - will be skipped. Default is ``True``. - prune_unapplied - Whether to prune key_chains for which the function was not applied. - Default is ``False``. - map_sequences - Whether to also map method to sequences (lists, tuples). - Default is ``False``. - - Returns - ------- - ret - list of containers split horizontally from input array. - - Examples - -------- - >>> ary = ivy.Container( - a = ivy.array( - [[[0., 1.], - [2., 3.]], - [[4., 5.], - [6., 7.]]] - ), - b=ivy.array( - [0., 1., 2., 3., - 4., 5., 6., 7., - 8., 9., 10., 11., - 12., 13., 14., 15.] - ) - ) - >>> ivy.Container.static_hsplit(ary, 2) - [{ - a: ivy.array([[[0., 1.]], - [[4., 5.]]]), - b: ivy.array([0., 1., 2., 3., 4., 5., 6., 7.]) - }, { - a: ivy.array([[[2., 3.]], - [[6., 7.]]]), - b: ivy.array([8., 9., 10., 11., 12., 13., 14., 15.]) - }] - """ - return ContainerBase.cont_multi_map_in_function( - "hsplit", - ary, - indices_or_sections, - copy=copy, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - ) - - def hsplit( - self: ivy.Container, - indices_or_sections: Union[ - int, Sequence[int], ivy.Array, ivy.NativeArray, ivy.Container - ], - copy: Optional[Union[bool, ivy.Container]] = None, - /, - ) -> List[ivy.Container]: - """ - ivy.Container instance method variant of ivy.hsplit. This method simply wraps - the function, and so the docstring for ivy.hsplit also applies to this method - with minimal changes. - - Parameters - ---------- - self - the container with array inputs. - indices_or_sections - If indices_or_sections is an integer n, the array is split into n - equal sections, provided that n must be a divisor of the split axis. - If indices_or_sections is a sequence of ints or 1-D array, - then input is split at each of the indices. - - Returns - ------- - ret - list of containers split horizontally from input container - - Examples - -------- - >>> ary = ivy.Container( - a = ivy.array( - [[[0., 1.], - [2., 3.]], - [[4., 5.], - [6., 7.]]] - ), - b=ivy.array( - [0., 1., 2., 3., - 4., 5., 6., 7., - 8., 9., 10., 11., - 12., 13., 14., 15.] - ) - ) - >>> ary.hsplit(2) - [{ - a: ivy.array([[[0., 1.]], - [[4., 5.]]]), - b: ivy.array([0., 1., 2., 3., 4., 5., 6., 7.]) - }, { - a: ivy.array([[[2., 3.]], - [[6., 7.]]]), - b: ivy.array([8., 9., 10., 11., 12., 13., 14., 15.]) - }] - """ - return self.static_hsplit(self, indices_or_sections, copy=copy) - - @staticmethod - def static_broadcast_shapes( - shapes: Union[ivy.Container, List[Tuple[int]]], - /, - *, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container static method variant of ivy.broadcast_shapes. This method simply - wraps the function, and so the docstring for ivy.hsplit also applies to this - method with minimal changes. - - Parameters - ---------- - shapes - the container with shapes to broadcast. - key_chains - The keychains to apply or not apply the method to. Default is ``None``. - to_apply - If True, the method will be applied to key_chains, otherwise key_chains - will be skipped. Default is ``True``. - prune_unapplied - Whether to prune key_chains for which the function was not applied. - Default is ``False``. - map_sequences - Whether to also map method to sequences (lists, tuples). - Default is ``False``. - - Returns - ------- - ret - Container with broadcasted shapes. - - Examples - -------- - >>> shapes = ivy.Container(a = [(2, 3), (2, 1)], - ... b = [(2, 3), (1, 3)], - ... c = [(2, 3), (2, 3)], - ... d = [(2, 3), (2, 1), (1, 3), (2, 3)]) - >>> z = ivy.Container.static_broadcast_shapes(shapes) - >>> print(z) - { - a: (2, 3), - b: (2, 3), - c: (2, 3), - d: (2, 3) - } - """ - return ContainerBase.cont_multi_map_in_function( - "broadcast_shapes", - shapes, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - out=out, - ) - - def broadcast_shapes( - self: ivy.Container, - /, - *, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container instance method variant of ivy.broadcast_shapes. This method - simply wraps the function, and so the docstring for ivy.broadcast_shapes also - applies to this method with minimal changes. - - Parameters - ---------- - self - the container with shapes to broadcast. - - Returns - ------- - ret - Container with broadcasted shapes. - - Examples - -------- - >>> shapes = ivy.Container(a = [(2, 3), (2, 1)], - ... b = [(2, 3), (1, 3)], - ... c = [(2, 3), (2, 3)], - ... d = [(2, 3), (2, 1), (1, 3), (2, 3)]) - >>> z = shapes.broadcast_shapes() - >>> print(z) - { - a: (2, 3), - b: (2, 3), - c: (2, 3), - d: (2, 3) - } - """ - return self.static_broadcast_shapes(self, out=out) - - @staticmethod - def static_expand( - x: Union[ivy.Array, ivy.NativeArray, ivy.Container], - shape: Union[ivy.Shape, ivy.NativeShape, ivy.Container], - /, - *, - copy: Optional[Union[bool, ivy.Container]] = None, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - - Parameters - ---------- - x - input container. - shape - A 1-D Array indicates the shape you want to expand to, - following the broadcast rule. - copy - boolean indicating whether to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - device - key_chains - The keychains to apply or not apply the method to. Default is ``None``. - to_apply - If True, the method will be applied to key_chains, otherwise key_chains - will be skipped. Default is ``True``. - prune_unapplied - Whether to prune key_chains for which the function was not applied. - Default is ``False``. - map_sequences - Whether to also map method to sequences (lists, tuples). - Default is ``False``. - out - optional output array, for writing the result to. It must have a shape - that the inputs broadcast to. - - Returns - ------- - ret - An output Container with the results. - """ - return ContainerBase.cont_multi_map_in_function( - "expand", - x, - shape, - copy=copy, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - out=out, - ) - - def expand( - self: Union[ivy.Array, ivy.NativeArray, ivy.Container], - shape: Union[ivy.Shape, ivy.NativeShape, ivy.Container], - /, - *, - copy: Optional[Union[bool, ivy.Container]] = None, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - - Parameters - ---------- - self - input container. - shape - A 1-D Array indicates the shape you want to expand to, - following the broadcast rule. - copy - boolean indicating whether to copy the input array. - If True, the function must always copy. - If False, the function must never copy and must - raise a ValueError in case a copy would be necessary. - If None, the function must reuse existing memory buffer if possible - and copy otherwise. Default: ``None``. - device - out - optional output array, for writing the result to. It must have a shape - that the inputs broadcast to. - - Returns - ------- - ret - An output Container with the results. - - - """ - return self.static_expand(self, shape, copy=copy, out=out) - - @staticmethod - def static_as_strided( - x: Union[ivy.Array, ivy.NativeArray, ivy.Container], - shape: Union[ivy.Shape, ivy.NativeShape, Sequence[int], ivy.Container], - strides: Union[Sequence[int], ivy.Container], - /, - *, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - ) -> ivy.Container: - """ - ivy.Container instance method variant of ivy.as_strided. This method simply - wraps the function, and so the docstring for ivy.as_strided also applies to this - method with minimal changes. - - Parameters - ---------- - x - Input container. - shape - The shape of the new arrays. - strides - The strides of the new arrays (specified in bytes). - key_chains - The keychains to apply or not apply the method to. Default is ``None``. - to_apply - If True, the method will be applied to key_chains, otherwise key_chains - will be skipped. Default is ``True``. - prune_unapplied - Whether to prune key_chains for which the function was not applied. - Default is ``False``. - map_sequences - Whether to also map method to sequences (lists, tuples). - Default is ``False``. - - Returns - ------- - ret - Output container. - """ - return ContainerBase.cont_multi_map_in_function( - "as_strided", - x, - shape, - strides, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - ) - - def as_strided( - self: Union[ivy.Array, ivy.NativeArray, ivy.Container], - shape: Union[ivy.Shape, ivy.NativeShape, Sequence[int], ivy.Container], - strides: Union[Sequence[int], ivy.Container], - /, - ) -> ivy.Container: - """ - ivy.Container instance method variant of ivy.as_strided. This method simply - wraps the function, and so the docstring for ivy.as_strided also applies to this - method with minimal changes. - - Parameters - ---------- - self - Input container. - shape - The shape of the new arrays. - strides - The strides of the new arrays (specified in bytes). - - Returns - ------- - ret - Output container. - """ - return self.static_as_strided(self, shape, strides) - - @staticmethod - def static_concat_from_sequence( - input_sequence: Union[ - Tuple[Union[ivy.Array, ivy.NativeArray, ivy.Container]], - List[Union[ivy.Array, ivy.NativeArray, ivy.Container]], - ], - /, - *, - new_axis: Union[int, ivy.Container] = 0, - axis: Union[int, ivy.Container] = 0, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container static method variant of ivy.concat_from_sequence. This method - simply wraps the function, and so the docstring for ivy.concat_from_sequence - also applies to this method with minimal changes. - - Parameters - ---------- - input_sequence - Container with leaves to join. Each array leave must have the same shape. - new_axis - Insert and concatenate on a new axis or not, - default 0 means do not insert new axis. - new_axis = 0: concatenate - new_axis = 1: stack - axis - axis along which the array leaves will be concatenated. More details - can be found in the docstring for ivy.concat_from_sequence. - - key_chains - The key-chains to apply or not apply the method to. Default is ``None``. - to_apply - If True, the method will be applied to key_chains, otherwise key_chains - will be skipped. Default is ``True``. - prune_unapplied - Whether to prune key_chains for which the function was not applied. - Default is ``False``. - map_sequences - Whether to also map method to sequences (lists, tuples). - Default is ``False``. - out - optional output array, for writing the result to. It must have a shape - that the inputs broadcast to. - - Returns - ------- - ret - an output container with the results. - - Examples - -------- - >>> x = ivy.Container(a=ivy.array([[0, 1], [2,3]]), b=ivy.array([[4, 5]])) - >>> z = ivy.Container.static_concat_from_sequence(x,new_axis = 1, axis = 1) - >>> print(z) - { - a: ivy.array([[0, 2], - [1, 3]]), - b: ivy.array([[4], - [5]]) - } - - >>> x = ivy.Container(a=ivy.array([[0, 1], [2,3]]), b=ivy.array([[4, 5]])) - >>> y = ivy.Container(a=ivy.array([[3, 2], [1,0]]), b=ivy.array([[1, 0]])) - >>> z = ivy.Container.static_concat_from_sequence([x,y]) - >>> print(z) - { - a: ivy.array([[0, 1], - [2, 3], - [3, 2], - [1, 0]]), - b: ivy.array([[4, 5], - [1, 0]]) - } - - >>> x = ivy.Container(a=ivy.array([[0, 1], [2,3]]), b=ivy.array([[4, 5]])) - >>> y = ivy.Container(a=ivy.array([[3, 2], [1,0]]), b=ivy.array([[1, 0]])) - >>> z = ivy.Container.static_concat_from_sequence([x,y],new_axis=1, axis=1) - >>> print(z) - { - a: ivy.array([[[0, 1], - [3, 2]], - [[2, 3], - [1, 0]]]), - b: ivy.array([[[4, 5], - [1, 0]]]) - } - """ - return ContainerBase.cont_multi_map_in_function( - "concat_from_sequence", - input_sequence, - new_axis=new_axis, - axis=axis, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - out=out, - ) - - def concat_from_sequence( - self: ivy.Container, - /, - input_sequence: Union[ - Tuple[Union[ivy.Array, ivy.NativeArray, ivy.Container]], - List[Union[ivy.Array, ivy.NativeArray, ivy.Container]], - ], - *, - new_axis: Union[int, ivy.Container] = 0, - axis: Union[int, ivy.Container] = 0, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - out: Optional[ivy.Container] = None, - ) -> ivy.Container: - """ - ivy.Container instance method variant of ivy.stack. This method simply wraps the - function, and so the docstring for ivy.stack also applies to this method with - minimal changes. - - Parameters - ---------- - self - Container with leaves to join with leaves of other arrays/containers. - Each array leave must have the same shape. - input_sequence - Container with other leaves to join. - Each array leave must have the same shape. - new_axis - Insert and concatenate on a new axis or not, - default 0 means do not insert new axis. - new_axis = 0: concatenate - new_axis = 1: stack - axis - axis along which the array leaves will be concatenated. More details can - be found in the docstring for ivy.stack. - key_chains - The key-chains to apply or not apply the method to. Default is ``None``. - to_apply - If True, the method will be applied to key_chains, otherwise key_chains - will be skipped. Default is ``True``. - prune_unapplied - Whether to prune key_chains for which the function was not applied. - Default is ``False``. - map_sequences - Whether to also map method to sequences (lists, tuples). - Default is ``False``. - out - optional output array, for writing the result to. It must have a shape - that the inputs broadcast to. - - Returns - ------- - ret - an output container with the results. - - Examples - -------- - >>> x = ivy.Container(a=ivy.array([[0, 1], [2,3]]), b=ivy.array([[4, 5]])) - >>> y = ivy.Container(a=ivy.array([[3, 2], [1,0]]), b=ivy.array([[1, 0]])) - >>> z = ivy.Container.static_concat_from_sequence([x,y],axis=1) - >>> print(z) - { - a: ivy.array([[[0, 1], - [3, 2]], - [[2, 3], - [1, 0]]]), - b: ivy.array([[[4, 5], - [1, 0]]]) - } - """ - new_input_sequence = ( - input_sequence.cont_copy() - if ivy.is_ivy_container(input_sequence) - else input_sequence.copy() - ) - new_input_sequence.insert(0, self.cont_copy()) - return self.concat_from_sequence( - new_input_sequence, - new_axis=new_axis, - axis=axis, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - out=out, - ) - - def associative_scan( - self: Union[ivy.Array, ivy.NativeArray, ivy.Container], - fn: Union[Callable, ivy.Container], - /, - *, - reverse: Union[bool, ivy.Container] = False, - axis: Union[int, ivy.Container] = 0, - ) -> ivy.Container: - """ - ivy.Container instance method variant of ivy.associative_scan. This method - simply wraps the function, and so the docstring for ivy.associative_scan also - applies to this method with minimal changes. - - Parameters - ---------- - self - The Container to scan over. - fn - The associative function to apply. - reverse - Whether to scan in reverse with respect to the given axis. - axis - The axis to scan over. - - Returns - ------- - ret - The result of the scan. - """ - return ivy.associative_scan(self, fn, reverse=reverse, axis=axis) - - @staticmethod - def _static_unique_consecutive( - x: Union[ivy.Array, ivy.NativeArray, ivy.Container], - /, - *, - axis: Optional[Union[int, ivy.Container]] = None, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - ) -> ivy.Container: - """ - ivy.Container static method variant of ivy.unique_consecutive. - - This method simply wraps the function, and so the docstring for - ivy.unique_consecutive also applies to this method with minimal - changes. - """ - return ContainerBase.cont_multi_map_in_function( - "unique_consecutive", - x, - axis=axis, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - ) - - def unique_consecutive( - self: ivy.Container, - /, - *, - axis: Optional[Union[int, ivy.Container]] = None, - key_chains: Optional[Union[List[str], Dict[str, str], ivy.Container]] = None, - to_apply: Union[bool, ivy.Container] = True, - prune_unapplied: Union[bool, ivy.Container] = False, - map_sequences: Union[bool, ivy.Container] = False, - ) -> ivy.Container: - """ - ivy.Container instance method variant of ivy.unique_consecutive. - - This method simply wraps the function, and so the docstring for - ivy.unique_consecutive also applies to this method with minimal - changes. - """ - return self._static_unique_consecutive( - self, - axis=axis, - key_chains=key_chains, - to_apply=to_apply, - prune_unapplied=prune_unapplied, - map_sequences=map_sequences, - ) - - @staticmethod - def _static_fill_diagonal( - a: Union[ivy.Array, ivy.NativeArray, ivy.Container], - v: Union[ivy.Array, ivy.NativeArray, ivy.Container], - /, - *, - wrap: Union[bool, ivy.Container] = False, - ) -> ivy.Container: - """ - ivy.Container static method variant of ivy.fill_diagonal. - - This method simply wraps the function, and so the docstring for - ivy.fill_diagonal also applies to this method with minimal - changes. - """ - return ContainerBase.cont_multi_map_in_function( - "fill_diagonal", - a, - v, - wrap=wrap, - ) - - def fill_diagonal( - self: ivy.Container, - v: Union[int, float, ivy.Container], - /, - *, - wrap: Union[bool, ivy.Container] = False, - ) -> ivy.Container: - """ - ivy.Container instance method variant of ivy.fill_diagonal. - - This method simply wraps the function, and so the docstring for - ivy.fill_diagonal also applies to this method with minimal - changes. - """ - return self._static_fill_diagonal( - self, - v, - wrap=wrap, - ) - - -@staticmethod -def tensor_ops(tensor, operation_type, *args): - if operation_type == "index": - if len(args) != 1: - raise ValueError - index = args[0] - return tensor[index] - elif operation_type == "slice": - if len(args) != 2: - raise ValueError - start, end = args - return tensor[start:end] - elif operation_type == "join": - if len(args) != 1: - raise ValueError - other_tensor = args[0] - return ivy.cat((tensor, other_tensor), dim=0) - elif operation_type == "mutate": - if len(args) != 1: - raise ValueError - factor = args[0] - tensor.mul_(factor) - return tensor - else: - raise ValueError From 9ed0ec845684b676cbe1c4ce5bdcd47c17f3b637 Mon Sep 17 00:00:00 2001 From: Sanjay <121057369+Sanjay8602@users.noreply.github.com> Date: Wed, 18 Oct 2023 01:13:46 +0530 Subject: [PATCH 29/37] Update test_norm.py --- .../test_paddle/test_nn/test_functional/test_norm.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py index e858999c7bcba..4d3970c8cd81d 100644 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py +++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py @@ -7,12 +7,9 @@ from ivy_tests.test_ivy.helpers.testing_helpers import handle_frontend_test from ivy_tests.test_ivy.test_functional.test_nn.test_norms import ( - _generate_data_layer_norm, + _generate_data_layer_norm, _generate_data_batch_norm ) -from ivy_tests.test_ivy.test_functional.test_nn.test_norms import ( - _generate_data_batch_norm, -) # layer_norm From 8f0df7f9f196b0a51c04697af532a6abc865136f Mon Sep 17 00:00:00 2001 From: ivy-branch Date: Tue, 17 Oct 2023 19:44:42 +0000 Subject: [PATCH 30/37] =?UTF-8?q?=F0=9F=A4=96=20Lint=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test_paddle/test_nn/test_functional/test_norm.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py index 4d3970c8cd81d..0669a922d26f4 100644 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py +++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py @@ -7,11 +7,11 @@ from ivy_tests.test_ivy.helpers.testing_helpers import handle_frontend_test from ivy_tests.test_ivy.test_functional.test_nn.test_norms import ( - _generate_data_layer_norm, _generate_data_batch_norm + _generate_data_layer_norm, + _generate_data_batch_norm, ) - # layer_norm @handle_frontend_test( fn_tree="paddle.nn.functional.layer_norm", From e7547fe712b4d5b11a088ed8c712378fc087d32d Mon Sep 17 00:00:00 2001 From: Sanjay <121057369+Sanjay8602@users.noreply.github.com> Date: Wed, 18 Oct 2023 12:19:49 +0530 Subject: [PATCH 31/37] Update test_norm.py --- .../test_paddle/test_nn/test_functional/test_norm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py index 0669a922d26f4..f52eb8b2bff3f 100644 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py +++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py @@ -26,7 +26,7 @@ def test_paddle_layer_norm( normalized_shape, eps, test_flags, - frontend: Literal["paddle"], + frontend, on_device, fn_tree, ): From 27c57f5e91c86548fed402ee936266bfa2c45fdd Mon Sep 17 00:00:00 2001 From: ivy-branch Date: Wed, 18 Oct 2023 06:50:51 +0000 Subject: [PATCH 32/37] =?UTF-8?q?=F0=9F=A4=96=20Lint=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test_paddle/test_nn/test_functional/test_norm.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py index f52eb8b2bff3f..20230bd3681e6 100644 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py +++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py @@ -1,5 +1,4 @@ # global -from typing import Literal from hypothesis import strategies as st # local From 90ce9174cfc1ed49c10b81d5df3b76a55e0a51ed Mon Sep 17 00:00:00 2001 From: Sanjay Suthar Date: Thu, 18 Jan 2024 07:07:31 +0530 Subject: [PATCH 33/37] bilinear function created --- .../frontends/paddle/nn/functional/common.py | 7 ++++ .../test_nn/test_functional/test_common.py | 34 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/ivy/functional/frontends/paddle/nn/functional/common.py b/ivy/functional/frontends/paddle/nn/functional/common.py index 9685a1ff3759f..12a1d625a0d2e 100644 --- a/ivy/functional/frontends/paddle/nn/functional/common.py +++ b/ivy/functional/frontends/paddle/nn/functional/common.py @@ -92,3 +92,10 @@ def zeropad2d(x, padding, data_format="NCHW", name=None): def linear(x, weight, bias=None, name=None): weight = ivy.swapaxes(weight, -1, -2) return ivy.linear(x, weight, bias=bias) + +@to_ivy_arrays_and_back +@with_supported_dtypes({"2.5.0 and below": ("float32", "float64")}, "paddle") +def bilinear(x1, x2, weight, bias=None, name=None): + x2_transposed = ivy.swapaxes(x2, -1, -2) + result = ivy.linear(ivy.multiply(x1, x2_transposed), weight, bias=bias) + return result diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_common.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_common.py index 7fafe0308c6ae..85986d6d32a62 100644 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_common.py +++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_common.py @@ -3,6 +3,7 @@ # local import ivy +from ivy.functional.frontends.paddle.nn.functional.common import bilinear import ivy_tests.test_ivy.helpers as helpers from ivy_tests.test_ivy.helpers import handle_frontend_test from ivy_tests.test_ivy.test_frontends.test_torch.test_nn.test_functional.test_linear_functions import ( # noqa: E501 @@ -229,3 +230,36 @@ def test_linear( weight=weight, bias=bias, ) + + +@handle_frontend_test( + fn_tree="paddle.nn.functional.common.bilinear", + dtype_x1_x2_weight_bias=bilinear( + dtypes=helpers.get_dtypes("valid", full=False), + ), +) +def test_bilinear( + *, + dtype_x1_x2_weight_bias, + on_device, + fn_tree, + backend_fw, + frontend, + test_flags, +): + dtype, x1, x2, weight, bias = dtype_x1_x2_weight_bias + x2_transposed = ivy.swapaxes(x2, -1, -2) + result = ivy.linear(ivy.multiply(x1, x2_transposed), weight, bias=bias) + helpers.test_frontend_function( + input_dtypes=dtype, + frontend=frontend, + backend_to_test=backend_fw, + test_flags=test_flags, + fn_tree=fn_tree, + on_device=on_device, + x1=x1, + x2=x2, + weight=weight, + bias=bias, + result=result, + ) From 3f7619e221908f8e3e4e937591a1c19f66b145c5 Mon Sep 17 00:00:00 2001 From: ivy-branch Date: Thu, 18 Jan 2024 01:52:36 +0000 Subject: [PATCH 34/37] =?UTF-8?q?=F0=9F=A4=96=20Lint=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ivy/functional/frontends/paddle/nn/functional/common.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ivy/functional/frontends/paddle/nn/functional/common.py b/ivy/functional/frontends/paddle/nn/functional/common.py index 12a1d625a0d2e..20e3fbe1490a4 100644 --- a/ivy/functional/frontends/paddle/nn/functional/common.py +++ b/ivy/functional/frontends/paddle/nn/functional/common.py @@ -93,6 +93,7 @@ def linear(x, weight, bias=None, name=None): weight = ivy.swapaxes(weight, -1, -2) return ivy.linear(x, weight, bias=bias) + @to_ivy_arrays_and_back @with_supported_dtypes({"2.5.0 and below": ("float32", "float64")}, "paddle") def bilinear(x1, x2, weight, bias=None, name=None): From 6c4b1fd6aa00255d529d87f4e83aea4892d93910 Mon Sep 17 00:00:00 2001 From: Sanjay <121057369+Sanjay8602@users.noreply.github.com> Date: Thu, 18 Jan 2024 22:29:54 +0530 Subject: [PATCH 35/37] Delete ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py --- .../test_nn/test_functional/test_norm.py | 68 ------------------- 1 file changed, 68 deletions(-) delete mode 100644 ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py deleted file mode 100644 index 20230bd3681e6..0000000000000 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_norm.py +++ /dev/null @@ -1,68 +0,0 @@ -# global -from hypothesis import strategies as st - -# local -import ivy_tests.test_ivy.helpers as helpers -from ivy_tests.test_ivy.helpers.testing_helpers import handle_frontend_test - -from ivy_tests.test_ivy.test_functional.test_nn.test_norms import ( - _generate_data_layer_norm, - _generate_data_batch_norm, -) - - -# layer_norm -@handle_frontend_test( - fn_tree="paddle.nn.functional.layer_norm", - values_tuple=_generate_data_layer_norm( - available_dtypes=helpers.get_dtypes("float"), - ), - eps=st.floats(min_value=0.01, max_value=0.1), -) -def test_paddle_layer_norm( - *, - values_tuple, - normalized_shape, - eps, - test_flags, - frontend, - on_device, - fn_tree, -): - (dtype, x, normalized_shape, scale, offset) = values_tuple - helpers.test_frontend_function( - input_dtypes=dtype, - frontend=frontend, - test_flags=test_flags, - on_device=on_device, - fn_tree=fn_tree, - x=x[0], - normalized_shape=normalized_shape, - weight=scale[0], - bias=offset[0], - epsilon=eps, - ) - - -@handle_frontend_test( - fn_tree="paddle.nn.functional.batch_norm", - values_tuple=_generate_data_batch_norm( - available_dtypes=helpers.get_dtypes("float"), - ), - eps=st.floats(min_value=0.01, max_value=0.1), -) -def test_batch_norm(*, values_tuple, test_flags, backend_fw, fn_name, on_device): - (dtype, x, gamma, beta, moving_mean, moving_var, epsilon) = values_tuple - helpers.test_function( - input_dtypes=dtype, - test_flags=test_flags, - backend_to_test=backend_fw, - fn_name=fn_name, - on_device=on_device, - x=x, - gamma=gamma, - beta=beta, - moving_mean=moving_mean, - moving_var=moving_var, - epsilon=epsilon, - ) From e71f415c9cc905fe83e9ee101c433883d0dfa8cf Mon Sep 17 00:00:00 2001 From: Sanjay <121057369+Sanjay8602@users.noreply.github.com> Date: Thu, 18 Jan 2024 22:30:24 +0530 Subject: [PATCH 36/37] Delete ivy/functional/frontends/paddle/nn/functional/norm.py --- .../frontends/paddle/nn/functional/norm.py | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 ivy/functional/frontends/paddle/nn/functional/norm.py diff --git a/ivy/functional/frontends/paddle/nn/functional/norm.py b/ivy/functional/frontends/paddle/nn/functional/norm.py deleted file mode 100644 index 8853f204df49d..0000000000000 --- a/ivy/functional/frontends/paddle/nn/functional/norm.py +++ /dev/null @@ -1,16 +0,0 @@ -# local -import ivy -from ivy.func_wrapper import with_supported_dtypes -from ivy.functional.frontends.paddle.func_wrapper import to_ivy_arrays_and_back - - -@to_ivy_arrays_and_back -@with_supported_dtypes({"2.5.0 and below": ("float32", "float64")}, "paddle") -def layer_norm(x, normalized_shape, weight=None, bias=None, epsilon=1e-05, name=None): - return ivy.layer_norm(x, normalized_shape, weight, bias, epsilon) - - -@to_ivy_arrays_and_back -@with_supported_dtypes({"2.5.0 and below": ("float32", "float64")}, "paddle") -def batch_norm(x, gamma, beta, moving_mean, moving_var, epsilon=1e-5): - return ivy.batch_norm(x, gamma, beta, moving_mean, moving_var, epsilon) From 51ffbb15cbf76c3f516063844af199b82ccfb1e4 Mon Sep 17 00:00:00 2001 From: Sanjay <121057369+Sanjay8602@users.noreply.github.com> Date: Thu, 18 Jan 2024 22:30:49 +0530 Subject: [PATCH 37/37] Delete ivy_tests/test_ivy/test_functional/test_nn/test_norms.py --- .../test_functional/test_nn/test_norms.py | 178 ------------------ 1 file changed, 178 deletions(-) delete mode 100644 ivy_tests/test_ivy/test_functional/test_nn/test_norms.py diff --git a/ivy_tests/test_ivy/test_functional/test_nn/test_norms.py b/ivy_tests/test_ivy/test_functional/test_nn/test_norms.py deleted file mode 100644 index 8c13ecc6dcf71..0000000000000 --- a/ivy_tests/test_ivy/test_functional/test_nn/test_norms.py +++ /dev/null @@ -1,178 +0,0 @@ -"""Collection of tests for unified neural network layers.""" - -# global -from hypothesis import strategies as st - -# local -import ivy_tests.test_ivy.helpers as helpers -from ivy_tests.test_ivy.helpers import handle_test - - -@st.composite -def _generate_data_layer_norm( - draw, - *, - available_dtypes, - large_abs_safety_factor=20, - small_abs_safety_factor=20, - safety_factor_scale="log", - min_num_dims=1, - max_num_dims=5, - valid_axis=True, - allow_neg_axes=False, - max_axes_size=1, - force_int_axis=True, - ret_shape=True, - abs_smallest_val=0.1, - allow_inf=False, - allow_nan=False, - exclude_min=False, - exclude_max=False, - min_value=-1e20, - max_value=1e20, - shared_dtype=False, -): - results = draw( - helpers.dtype_values_axis( - available_dtypes=available_dtypes, - min_value=min_value, - max_value=max_value, - large_abs_safety_factor=large_abs_safety_factor, - small_abs_safety_factor=small_abs_safety_factor, - safety_factor_scale=safety_factor_scale, - abs_smallest_val=abs_smallest_val, - min_num_dims=min_num_dims, - max_num_dims=max_num_dims, - valid_axis=valid_axis, - allow_neg_axes=allow_neg_axes, - max_axes_size=max_axes_size, - force_int_axis=force_int_axis, - ret_shape=ret_shape, - ) - ) - - dtype, values, axis, shape = results - - weight_shape = shape[axis:] - bias_shape = shape[axis:] - normalized_idxs = list(range(axis, len(shape))) - - arg_dict = { - "available_dtypes": dtype, - "abs_smallest_val": abs_smallest_val, - "min_value": min_value, - "max_value": max_value, - "large_abs_safety_factor": large_abs_safety_factor, - "small_abs_safety_factor": small_abs_safety_factor, - "safety_factor_scale": safety_factor_scale, - "allow_inf": allow_inf, - "allow_nan": allow_nan, - "exclude_min": exclude_min, - "exclude_max": exclude_max, - "min_num_dims": min_num_dims, - "max_num_dims": max_num_dims, - "shared_dtype": shared_dtype, - "ret_shape": False, - } - - results_weight = draw(helpers.dtype_and_values(shape=weight_shape, **arg_dict)) - results_bias = draw(helpers.dtype_and_values(shape=bias_shape, **arg_dict)) - - _, weight_values = results_weight - _, bias_values = results_bias - - return dtype, values, normalized_idxs, weight_values, bias_values - - -@handle_test( - fn_tree="functional.ivy.layer_norm", - values_tuple=_generate_data_layer_norm( - available_dtypes=helpers.get_dtypes("float"), - ), - new_std=st.floats(min_value=0.01, max_value=0.1), - eps=st.floats(min_value=0.01, max_value=0.1), -) -def test_layer_norm( - *, values_tuple, new_std, eps, test_flags, backend_fw, fn_name, on_device -): - dtype, x, normalized_idxs, scale, offset = values_tuple - helpers.test_function( - input_dtypes=dtype, - test_flags=test_flags, - backend_to_test=backend_fw, - fn_name=fn_name, - on_device=on_device, - rtol_=0.5, - atol_=0.5, - xs_grad_idxs=[[0, 0]], - x=x[0], - normalized_idxs=normalized_idxs, - eps=eps, - scale=scale[0], - offset=offset[0], - new_std=new_std, - ) - - -# created function for batch normalization -def _generate_data_batch_norm( - draw, - *, - available_dtypes, - min_value, - max_value, - min_gamma=0.1, - max_gamma=2.0, - min_beta=-1.0, - max_beta=1.0, - min_moving_mean=0.0, - max_moving_mean=1.0, - min_moving_var=0.1, - max_moving_var=1.0, - epsilon=1e-5, - min_num_dims=1, - max_num_dims=5, - ret_shape=True, -): - results = draw( - helpers.dtype_values( - available_dtypes=available_dtypes, - min_value=min_value, - max_value=max_value, - min_num_dims=min_num_dims, - max_num_dims=max_num_dims, - ret_shape=ret_shape, - ) - ) - - dtype, x, shape = results - - gamma = draw(st.floats(min_value=min_gamma, max_value=max_gamma)) - beta = draw(st.floats(min_value=min_beta, max_value=max_beta)) - moving_mean = draw(st.floats(min_value=min_moving_mean, max_value=max_moving_mean)) - moving_var = draw(st.floats(min_value=min_moving_var, max_value=max_moving_var)) - - return dtype, x, gamma, beta, moving_mean, moving_var, epsilon - - -@handle_test( - fn_tree="functional.batch_norm", - values_tuple=_generate_data_batch_norm( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_batch_norm(*, values_tuple, test_flags, backend_fw, fn_name, on_device): - (dtype, x, gamma, beta, moving_mean, moving_var, epsilon) = values_tuple - helpers.test_function( - input_dtypes=dtype, - test_flags=test_flags, - backend_to_test=backend_fw, - fn_name=fn_name, - on_device=on_device, - x=x, - gamma=gamma, - beta=beta, - moving_mean=moving_mean, - moving_var=moving_var, - epsilon=epsilon, - )