From e14032df16fff75053695412560b8c7717f6f4f6 Mon Sep 17 00:00:00 2001 From: ivy-branch Date: Sat, 9 Dec 2023 16:11:48 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Lint=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../numpy/fft/discrete_fourier_transform.py | 16 +- .../indexing_like_operations.py | 12 +- .../frontends/numpy/ma/MaskedArray.py | 13 +- .../arithmetic_operations.py | 10 +- .../statistics/averages_and_variances.py | 4 +- .../frontends/numpy/ufunc/methods.py | 24 +- .../frontends/paddle/nn/functional/pooling.py | 3 +- .../frontends/paddle/nn/functional/vision.py | 30 +- .../sklearn/model_selection/_split.py | 18 +- .../frontends/sklearn/tree/_criterion.py | 38 +- .../frontends/sklearn/tree/_tree.py | 27 +- .../frontends/tensorflow/keras/metrics.py | 13 +- ivy/functional/frontends/tensorflow/linalg.py | 12 +- ivy/functional/frontends/tensorflow/nn.py | 6 +- .../frontends/tensorflow/raw_ops.py | 8 +- .../frontends/torch/comparison_ops.py | 2 +- .../frontends/torch/miscellaneous_ops.py | 6 +- .../torch/nn/functional/distance_functions.py | 12 +- .../torch/nn/functional/loss_functions.py | 10 +- .../torch/nn/functional/vision_functions.py | 10 +- .../frontends/torch/nn/modules/module.py | 27 +- ivy/functional/frontends/xgboost/core.py | 12 +- ivy/functional/frontends/xgboost/gbm/gbm.py | 10 +- .../test_jax/test__src/test_tree_util.py | 18 +- .../test_jax/test_lax/test_operators.py | 32 +- .../test_jax/test_numpy/test_indexing.py | 6 +- .../test_jax/test_numpy/test_linalg.py | 6 +- .../test_jax/test_numpy/test_manipulations.py | 52 +- .../test_jax/test_numpy/test_statistical.py | 24 +- .../test_function/test_mindspore_nn_func.py | 20 +- .../test_numpy/test_func_wrapper.py | 28 +- .../test_indexing_like_operations.py | 34 +- .../test_matrix_and_vector_products.py | 24 +- ...olving_equations_and_inverting_matrices.py | 6 +- .../test_padding_arrays.py | 26 +- .../test_numpy/test_ndarray/test_ndarray.py | 14 +- .../test_nn/test_functional/test_common.py | 30 +- .../test_paddle/test_tensor/test_tensor.py | 138 +- .../test_tensorflow/test_func_wrapper.py | 32 +- .../test_tensorflow/test_general_functions.py | 52 +- .../test_tensorflow/test_linalg.py | 12 +- .../test_functional/test_vision_functions.py | 14 +- .../test_core/test_creation.py | 14 +- .../test_functional/test_core/test_general.py | 54 +- .../test_functional/test_core/test_meta.py | 382 +++-- .../test_core/test_gradients.py | 24 +- .../test_core/test_manipulation.py | 28 +- .../test_experimental/test_nn/test_layers.py | 30 +- ivy_tests/test_ivy/test_misc/test_array.py | 6 +- .../test_backend_handler.py | 10 +- .../test_ivy/test_misc/test_container.py | 1431 +++++++++-------- .../test_tucker_tensor.py | 6 +- .../test_misc/test_handle_exceptions.py | 20 +- .../test_ivy/test_stateful/test_layers.py | 92 +- .../test_ivy/test_stateful/test_modules.py | 50 +- 55 files changed, 1684 insertions(+), 1354 deletions(-) diff --git a/ivy/functional/frontends/numpy/fft/discrete_fourier_transform.py b/ivy/functional/frontends/numpy/fft/discrete_fourier_transform.py index 796f1521b0aa0..bbf898a2c02d7 100644 --- a/ivy/functional/frontends/numpy/fft/discrete_fourier_transform.py +++ b/ivy/functional/frontends/numpy/fft/discrete_fourier_transform.py @@ -3,6 +3,14 @@ from ivy.func_wrapper import with_unsupported_dtypes +_SWAP_DIRECTION_MAP = { + None: "forward", + "backward": "forward", + "ortho": "ortho", + "forward": "backward", +} + + # --- Helpers --- # # --------------- # @@ -148,11 +156,3 @@ def rfftfreq(n, d=1.0): def rfftn(a, s=None, axes=None, norm=None): a = ivy.asarray(a, dtype=ivy.complex128) return ivy.rfftn(a, s=s, axes=axes, norm=norm) - - -_SWAP_DIRECTION_MAP = { - None: "forward", - "backward": "forward", - "ortho": "ortho", - "forward": "backward", -} diff --git a/ivy/functional/frontends/numpy/indexing_routines/indexing_like_operations.py b/ivy/functional/frontends/numpy/indexing_routines/indexing_like_operations.py index 3cae814a951a3..f5d7414b09c46 100644 --- a/ivy/functional/frontends/numpy/indexing_routines/indexing_like_operations.py +++ b/ivy/functional/frontends/numpy/indexing_routines/indexing_like_operations.py @@ -83,6 +83,12 @@ def put_along_axis(arr, indices, values, axis): ivy.put_along_axis(arr, indices, values, axis) +@to_ivy_arrays_and_back +@handle_numpy_out +def take(a, indices, /, *, axis=None, out=None, mode="raise"): + return ivy.take(a, indices, axis=axis, out=out, mode=mode) + + @to_ivy_arrays_and_back def take_along_axis(arr, indices, axis): return ivy.take_along_axis(arr, indices, axis) @@ -98,9 +104,3 @@ def tril_indices(n, k=0, m=None): def unravel_index(indices, shape, order="C"): ret = [x.astype("int64") for x in ivy.unravel_index(indices, shape)] return tuple(ret) - - -@to_ivy_arrays_and_back -@handle_numpy_out -def take(a, indices, /, *, axis=None, out=None, mode="raise"): - return ivy.take(a, indices, axis=axis, out=out, mode=mode) diff --git a/ivy/functional/frontends/numpy/ma/MaskedArray.py b/ivy/functional/frontends/numpy/ma/MaskedArray.py index 2eaf48cae2916..a97a447608cda 100644 --- a/ivy/functional/frontends/numpy/ma/MaskedArray.py +++ b/ivy/functional/frontends/numpy/ma/MaskedArray.py @@ -2,6 +2,8 @@ import ivy.functional.frontends.numpy as np_frontend import numpy as np + +masked = True masked_print_options = "--" nomask = False @@ -177,10 +179,12 @@ def _array_in_str(self): return masked_print_options return str(self._data.to_list()) if ivy.any(self._mask): - return str([ - masked_print_options if mask else x - for x, mask in zip(self._data.to_list(), self._mask.to_list()) - ]) + return str( + [ + masked_print_options if mask else x + for x, mask in zip(self._data.to_list(), self._mask.to_list()) + ] + ) return str(self._data.to_list()) @@ -192,7 +196,6 @@ def _is_masked_array(x): return isinstance(x, (np.ma.MaskedArray, np_frontend.ma.MaskedArray)) -masked = True # Instance Methods # # ---------------- # diff --git a/ivy/functional/frontends/numpy/mathematical_functions/arithmetic_operations.py b/ivy/functional/frontends/numpy/mathematical_functions/arithmetic_operations.py index 759e76272ca9a..ffe7dde3e21e2 100644 --- a/ivy/functional/frontends/numpy/mathematical_functions/arithmetic_operations.py +++ b/ivy/functional/frontends/numpy/mathematical_functions/arithmetic_operations.py @@ -91,10 +91,12 @@ def _divmod( ret = ivy.where( where, ret, - ([ - ivy.default(out, ivy.zeros_like(ret[0])), - ivy.default(out, ivy.zeros_like(ret[1])), - ]), + ( + [ + ivy.default(out, ivy.zeros_like(ret[0])), + ivy.default(out, ivy.zeros_like(ret[1])), + ] + ), out=out, ) return ret diff --git a/ivy/functional/frontends/numpy/statistics/averages_and_variances.py b/ivy/functional/frontends/numpy/statistics/averages_and_variances.py index f22f4756a95c8..3cfd82ed68ced 100644 --- a/ivy/functional/frontends/numpy/statistics/averages_and_variances.py +++ b/ivy/functional/frontends/numpy/statistics/averages_and_variances.py @@ -200,7 +200,9 @@ def var(x, /, *, axis=None, ddof=0.0, keepdims=False, out=None, dtype=None, wher dtype = ( dtype if dtype is not None - else ivy.float64 if ivy.is_int_dtype(x.dtype) else x.dtype + else ivy.float64 + if ivy.is_int_dtype(x.dtype) + else x.dtype ) ret = ivy.var(x, axis=axis, correction=ddof, keepdims=keepdims, out=out) ret = ( diff --git a/ivy/functional/frontends/numpy/ufunc/methods.py b/ivy/functional/frontends/numpy/ufunc/methods.py index 0a4a9c5bfb5c1..5d35a7a8a2448 100644 --- a/ivy/functional/frontends/numpy/ufunc/methods.py +++ b/ivy/functional/frontends/numpy/ufunc/methods.py @@ -214,20 +214,24 @@ def __init__(self, name) -> None: @property def nargs(self): sig = inspect.signature(self.func) - return len([ - param - for param in sig.parameters.values() - if param.kind in [param.POSITIONAL_ONLY, param.POSITIONAL_OR_KEYWORD] - ]) + return len( + [ + param + for param in sig.parameters.values() + if param.kind in [param.POSITIONAL_ONLY, param.POSITIONAL_OR_KEYWORD] + ] + ) @property def nin(self): sig = inspect.signature(self.func) - return len([ - param - for param in sig.parameters.values() - if param.kind == param.POSITIONAL_ONLY - ]) + return len( + [ + param + for param in sig.parameters.values() + if param.kind == param.POSITIONAL_ONLY + ] + ) @property def nout(self): diff --git a/ivy/functional/frontends/paddle/nn/functional/pooling.py b/ivy/functional/frontends/paddle/nn/functional/pooling.py index 910946dfbaf6e..ddb7dc569d8d6 100644 --- a/ivy/functional/frontends/paddle/nn/functional/pooling.py +++ b/ivy/functional/frontends/paddle/nn/functional/pooling.py @@ -118,8 +118,7 @@ def max_pool2d( if data_format not in ["NCHW", "NHWC"]: raise ValueError( "Attr(data_format) should be 'NCHW' or 'NHWC'. Received " - "Attr(data_format): %s." - % str(data_format) + "Attr(data_format): %s." % str(data_format) ) if data_format == "NHWC" and return_mask: diff --git a/ivy/functional/frontends/paddle/nn/functional/vision.py b/ivy/functional/frontends/paddle/nn/functional/vision.py index 935ce239a7dbb..73fcae0c89e1b 100644 --- a/ivy/functional/frontends/paddle/nn/functional/vision.py +++ b/ivy/functional/frontends/paddle/nn/functional/vision.py @@ -18,9 +18,9 @@ def affine_grid(theta, out_shape, align_corners=True): base_grid[:, :, :, 0] = ivy.linspace(-1, 1, W) base_grid[:, :, :, 1] = ivy.expand_dims(ivy.linspace(-1, 1, H), axis=-1) height_values = ivy.expand_dims(ivy.linspace(-1, 1, H), axis=-1) - base_grid[:, :, :, 1] = ivy.array([ - [[height_values[i]] * W for i in range(H)] - ])[:, :, :, 0] + base_grid[:, :, :, 1] = ivy.array( + [[[height_values[i]] * W for i in range(H)]] + )[:, :, :, 0] base_grid[:, :, :, 2] = ivy.full((H, W), 1) grid = ivy.matmul(base_grid.view((N, H * W, 3)), theta.swapaxes(1, 2)) return grid.view((N, H, W, 2)) @@ -32,9 +32,9 @@ def affine_grid(theta, out_shape, align_corners=True): height_values = ivy.expand_dims( ivy.linspace(-1, 1, H) * (H - 1) / H, axis=-1 ) - base_grid[:, :, :, 1] = ivy.array([ - [[height_values[i]] * W for i in range(H)] - ])[:, :, :, 0] + base_grid[:, :, :, 1] = ivy.array( + [[[height_values[i]] * W for i in range(H)]] + )[:, :, :, 0] base_grid[:, :, :, 2] = ivy.full((H, W), 1) grid = ivy.matmul(base_grid.view((N, H * W, 3)), ivy.swapaxes(theta, 1, 2)) return grid.view((N, H, W, 2)) @@ -45,9 +45,9 @@ def affine_grid(theta, out_shape, align_corners=True): base_grid[:, :, :, :, 0] = ivy.linspace(-1, 1, W) base_grid[:, :, :, :, 1] = ivy.expand_dims(ivy.linspace(-1, 1, H), axis=-1) height_values = ivy.linspace(-1, 1, H) - base_grid[:, :, :, :, 1] = ivy.array([ - [[[height_values[i]] * W for i in range(H)]] * D - ]) + base_grid[:, :, :, :, 1] = ivy.array( + [[[[height_values[i]] * W for i in range(H)]] * D] + ) base_grid[:, :, :, :, 2] = ivy.expand_dims( ivy.expand_dims(ivy.linspace(-1, 1, D), axis=-1), axis=-1 ) @@ -58,17 +58,17 @@ def affine_grid(theta, out_shape, align_corners=True): ivy.linspace(-1, 1, H) * (H - 1) / H, axis=-1 ) height_values = ivy.linspace(-1, 1, H) * (H - 1) / H - base_grid[:, :, :, :, 1] = ivy.array([ - [[[height_values[i]] * W for i in range(H)]] * D - ]) + base_grid[:, :, :, :, 1] = ivy.array( + [[[[height_values[i]] * W for i in range(H)]] * D] + ) base_grid[:, :, :, :, 2] = ivy.expand_dims( ivy.expand_dims(ivy.linspace(-1, 1, D) * (D - 1) / D, axis=-1), axis=-1 ) width_values = ivy.linspace(-1, 1, D) * (D - 1) / D - base_grid[:, :, :, :, 2] = ivy.array([ - [ivy.array([[width_values[i]] * W] * H) for i in range(D)] - ]) + base_grid[:, :, :, :, 2] = ivy.array( + [[ivy.array([[width_values[i]] * W] * H) for i in range(D)]] + ) base_grid[:, :, :, :, 3] = ivy.full((D, H, W), 1) grid = ivy.matmul(base_grid.view((N, D * H * W, 4)), theta.swapaxes(1, 2)) return grid.view((N, D, H, W, 3)) diff --git a/ivy/functional/frontends/sklearn/model_selection/_split.py b/ivy/functional/frontends/sklearn/model_selection/_split.py index 406ae7c7d22a2..ec6dc3d067f1e 100644 --- a/ivy/functional/frontends/sklearn/model_selection/_split.py +++ b/ivy/functional/frontends/sklearn/model_selection/_split.py @@ -83,10 +83,12 @@ def _iter_test_indices(self, X=None, y=None, groups=None): n_classes = len(y_idx) y_order = ivy.sort(y_encoded) - allocation = ivy.asarray([ - ivy.bincount(y_order[i :: self.n_splits], minlength=n_classes) - for i in range(self.n_splits) - ]) + allocation = ivy.asarray( + [ + ivy.bincount(y_order[i :: self.n_splits], minlength=n_classes) + for i in range(self.n_splits) + ] + ) test_folds = ivy.empty(len(y), dtype="int64") for k in range(n_classes): folds_for_class = ivy.arange(self.n_splits).repeat(allocation[:, k]) @@ -121,12 +123,16 @@ def train_test_split( n_train = ( ivy.floor(train_size * n_samples) if isinstance(train_size, float) - else float(train_size) if isinstance(train_size, int) else None + else float(train_size) + if isinstance(train_size, int) + else None ) n_test = ( ivy.ceil(test_size * n_samples) if isinstance(test_size, float) - else float(test_size) if isinstance(test_size, int) else None + else float(test_size) + if isinstance(test_size, int) + else None ) if train_size is None: n_train = n_samples - n_test diff --git a/ivy/functional/frontends/sklearn/tree/_criterion.py b/ivy/functional/frontends/sklearn/tree/_criterion.py index 2213df22946cf..092552ceeac3b 100644 --- a/ivy/functional/frontends/sklearn/tree/_criterion.py +++ b/ivy/functional/frontends/sklearn/tree/_criterion.py @@ -107,27 +107,33 @@ def init( def reset(self): self.pos = self.start - self.weighted_n_left, self.weighted_n_right, self.sum_left, self.sum_right = ( - _move_sums_classification( - self, - self.sum_left, - self.sum_right, - self.weighted_n_left, - self.weighted_n_right, - ) + ( + self.weighted_n_left, + self.weighted_n_right, + self.sum_left, + self.sum_right, + ) = _move_sums_classification( + self, + self.sum_left, + self.sum_right, + self.weighted_n_left, + self.weighted_n_right, ) return 0 def reverse_reset(self): self.pos = self.end - self.weighted_n_right, self.weighted_n_left, self.sum_right, self.sum_left = ( - _move_sums_classification( - self, - self.sum_right, - self.sum_left, - self.weighted_n_right, - self.weighted_n_left, - ) + ( + self.weighted_n_right, + self.weighted_n_left, + self.sum_right, + self.sum_left, + ) = _move_sums_classification( + self, + self.sum_right, + self.sum_left, + self.weighted_n_right, + self.weighted_n_left, ) return 0 diff --git a/ivy/functional/frontends/sklearn/tree/_tree.py b/ivy/functional/frontends/sklearn/tree/_tree.py index 4e8bf6622c75e..16dbf9cc6da74 100644 --- a/ivy/functional/frontends/sklearn/tree/_tree.py +++ b/ivy/functional/frontends/sklearn/tree/_tree.py @@ -4,10 +4,10 @@ EPSILON = ivy.finfo(ivy.double).eps INFINITY = ivy.inf INTPTR_MAX = ivy.iinfo(ivy.int32).max -TREE_UNDEFINED = -2 -_TREE_UNDEFINED = TREE_UNDEFINED TREE_LEAF = -1 +TREE_UNDEFINED = -2 _TREE_LEAF = TREE_LEAF +_TREE_UNDEFINED = TREE_UNDEFINED class Node: @@ -57,17 +57,19 @@ def _resize_c(self, capacity=INTPTR_MAX): dtype=ivy.float32, ) else: - self.value = ivy.concat([ - self.value, - ivy.zeros( - ( - int(capacity - self.capacity), - int(self.n_outputs), - int(self.max_n_classes), + self.value = ivy.concat( + [ + self.value, + ivy.zeros( + ( + int(capacity - self.capacity), + int(self.n_outputs), + int(self.max_n_classes), + ), + dtype=ivy.float32, ), - dtype=ivy.float32, - ), - ]) + ] + ) if capacity < self.node_count: self.node_count = capacity self.capacity = capacity @@ -199,7 +201,6 @@ def __init__( def build( self, tree, X, y, sample_weight=None, missing_values_in_feature_mask=None ): - if tree.max_depth <= 10: init_capacity = int(2 ** (tree.max_depth + 1)) - 1 else: diff --git a/ivy/functional/frontends/tensorflow/keras/metrics.py b/ivy/functional/frontends/tensorflow/keras/metrics.py index 2c8402c8b8067..d32ef36cf1ebc 100644 --- a/ivy/functional/frontends/tensorflow/keras/metrics.py +++ b/ivy/functional/frontends/tensorflow/keras/metrics.py @@ -96,10 +96,15 @@ def _top_k(input, topk): labels = ivy.shape(predictions)[1] # float comparison? - return ivy.array([ - (0 <= res < labels and ivy.min(top_k[ind] - predictions[ind, res]) <= 1e-9) - for ind, res in enumerate(targets) - ]) + return ivy.array( + [ + ( + 0 <= res < labels + and ivy.min(top_k[ind] - predictions[ind, res]) <= 1e-9 + ) + for ind, res in enumerate(targets) + ] + ) reshape = False y_true = ivy.array(y_true) diff --git a/ivy/functional/frontends/tensorflow/linalg.py b/ivy/functional/frontends/tensorflow/linalg.py index 90c896909a925..01a8f293f4497 100644 --- a/ivy/functional/frontends/tensorflow/linalg.py +++ b/ivy/functional/frontends/tensorflow/linalg.py @@ -496,11 +496,13 @@ def tridiagonal_solve( dim = diagonals[0].shape[0] diagonals[[0, -1], [-1, 0]] = 0 dummy_idx = [0, 0] - indices = ivy.array([ - [(i, i + 1) for i in range(dim - 1)] + [dummy_idx], - [(i, i) for i in range(dim)], - [dummy_idx] + [(i + 1, i) for i in range(dim - 1)], - ]) + indices = ivy.array( + [ + [(i, i + 1) for i in range(dim - 1)] + [dummy_idx], + [(i, i) for i in range(dim)], + [dummy_idx] + [(i + 1, i) for i in range(dim - 1)], + ] + ) constructed_matrix = ivy.scatter_nd( indices, diagonals, shape=ivy.array([dim, dim]) ) diff --git a/ivy/functional/frontends/tensorflow/nn.py b/ivy/functional/frontends/tensorflow/nn.py index 398f88faa8c65..7c473ff53e72e 100644 --- a/ivy/functional/frontends/tensorflow/nn.py +++ b/ivy/functional/frontends/tensorflow/nn.py @@ -314,9 +314,9 @@ def depthwise_conv2d( dilations = 1 if dilations is None else dilations strides, dilations = _reduce_strides_dilations(2, strides, dilations) fc = filter.shape[-2] - filter = filter.reshape([ - *filter.shape[0:2], 1, filter.shape[-2] * filter.shape[-1] - ]) + filter = filter.reshape( + [*filter.shape[0:2], 1, filter.shape[-2] * filter.shape[-1]] + ) return ivy.conv_general_dilated( input, filter, diff --git a/ivy/functional/frontends/tensorflow/raw_ops.py b/ivy/functional/frontends/tensorflow/raw_ops.py index 030e2be87b6fc..50ea03ed0cd3d 100644 --- a/ivy/functional/frontends/tensorflow/raw_ops.py +++ b/ivy/functional/frontends/tensorflow/raw_ops.py @@ -14,6 +14,7 @@ Acos = to_ivy_arrays_and_back(map_raw_ops_alias(tf_frontend.math.acos)) Acosh = to_ivy_arrays_and_back(map_raw_ops_alias(tf_frontend.math.acosh)) +Add = to_ivy_arrays_and_back(map_raw_ops_alias(tf_frontend.math.add)) AddN = to_ivy_arrays_and_back(map_raw_ops_alias(tf_frontend.math.add_n)) AddV2 = to_ivy_arrays_and_back(map_raw_ops_alias(tf_frontend.math.add)) ArgMax = to_ivy_arrays_and_back( @@ -248,6 +249,7 @@ ) Sin = to_ivy_arrays_and_back(map_raw_ops_alias(tf_frontend.math.sin)) Size = to_ivy_arrays_and_back(map_raw_ops_alias(tf_frontend.general_functions.size)) +Slice = to_ivy_arrays_and_back(map_raw_ops_alias(tf_frontend.slice)) Softmax = to_ivy_arrays_and_back( with_unsupported_dtypes( { @@ -280,6 +282,7 @@ Squeeze = to_ivy_arrays_and_back( map_raw_ops_alias(tf_frontend.general_functions.squeeze) ) +Sub = to_ivy_arrays_and_back(map_raw_ops_alias(tf_frontend.math.subtract)) Tan = to_ivy_arrays_and_back(map_raw_ops_alias(tf_frontend.math.tan)) Tanh = to_ivy_arrays_and_back(map_raw_ops_alias(tf_frontend.math.tanh)) Tile = to_ivy_arrays_and_back(map_raw_ops_alias(tf_frontend.general_functions.tile)) @@ -868,8 +871,3 @@ def Xlog1py(*, x, y, name="Xlog1py"): @to_ivy_arrays_and_back def ZerosLike(*, x, name="ZerosLike"): return ivy.zeros_like(x) - - -Add = to_ivy_arrays_and_back(map_raw_ops_alias(tf_frontend.math.add)) -Slice = to_ivy_arrays_and_back(map_raw_ops_alias(tf_frontend.slice)) -Sub = to_ivy_arrays_and_back(map_raw_ops_alias(tf_frontend.math.subtract)) diff --git a/ivy/functional/frontends/torch/comparison_ops.py b/ivy/functional/frontends/torch/comparison_ops.py index 80d55d6957f39..76d75d1eff808 100644 --- a/ivy/functional/frontends/torch/comparison_ops.py +++ b/ivy/functional/frontends/torch/comparison_ops.py @@ -295,8 +295,8 @@ def topk(input, k, dim=None, largest=True, sorted=True, *, out=None): return ivy.top_k(input, k, axis=dim, largest=largest, sorted=sorted, out=out) -gt = greater ge = greater_equal +gt = greater le = less_equal lt = less ne = not_equal diff --git a/ivy/functional/frontends/torch/miscellaneous_ops.py b/ivy/functional/frontends/torch/miscellaneous_ops.py index 23eeef185e73e..446227f997913 100644 --- a/ivy/functional/frontends/torch/miscellaneous_ops.py +++ b/ivy/functional/frontends/torch/miscellaneous_ops.py @@ -60,9 +60,9 @@ def block_diag(*tensors): ret_dim_1 = 0 for idx, t_shape in enumerate(shapes_list): dim_0, dim_1 = t_shape - ret[ret_dim_0 : ret_dim_0 + dim_0, ret_dim_1 : ret_dim_1 + dim_1] = ( - ivy.copy_array(tensors_2d[idx]) - ) + ret[ + ret_dim_0 : ret_dim_0 + dim_0, ret_dim_1 : ret_dim_1 + dim_1 + ] = ivy.copy_array(tensors_2d[idx]) ret_dim_0 += dim_0 ret_dim_1 += dim_1 diff --git a/ivy/functional/frontends/torch/nn/functional/distance_functions.py b/ivy/functional/frontends/torch/nn/functional/distance_functions.py index ba8b6f0930cc1..d490f459a18f3 100644 --- a/ivy/functional/frontends/torch/nn/functional/distance_functions.py +++ b/ivy/functional/frontends/torch/nn/functional/distance_functions.py @@ -45,9 +45,11 @@ def pairwise_distance(x1, x2, *, p=2.0, eps=1e-06, keepdim=False): @with_unsupported_dtypes({"2.1.1 and below": ("float16", "bfloat16")}, "torch") @to_ivy_arrays_and_back def pdist(input, p=2): - x = ivy.array([ - abs(input[i] - input[j]) - for i in range(len(input) - 1) - for j in range(i + 1, len(input)) - ]) + x = ivy.array( + [ + abs(input[i] - input[j]) + for i in range(len(input) - 1) + for j in range(i + 1, len(input)) + ] + ) return ivy.vector_norm(x, ord=p, axis=1) diff --git a/ivy/functional/frontends/torch/nn/functional/loss_functions.py b/ivy/functional/frontends/torch/nn/functional/loss_functions.py index 2ba4f45d333fa..d82eb7b599e2c 100644 --- a/ivy/functional/frontends/torch/nn/functional/loss_functions.py +++ b/ivy/functional/frontends/torch/nn/functional/loss_functions.py @@ -166,10 +166,12 @@ def calculate_loss(x1, x2, target): if target.ndim == 0: loss = calculate_loss(input1, input2, target) else: - loss = ivy.array([ - calculate_loss(input1[i], input2[i], target[i]) - for i in range(input1.shape[0]) - ]) + loss = ivy.array( + [ + calculate_loss(input1[i], input2[i], target[i]) + for i in range(input1.shape[0]) + ] + ) reduction = _get_reduction(reduction, size_average, reduce) loss = reduction(loss) diff --git a/ivy/functional/frontends/torch/nn/functional/vision_functions.py b/ivy/functional/frontends/torch/nn/functional/vision_functions.py index 8b661a78b3fbc..26b103906b8c1 100644 --- a/ivy/functional/frontends/torch/nn/functional/vision_functions.py +++ b/ivy/functional/frontends/torch/nn/functional/vision_functions.py @@ -11,10 +11,12 @@ def _handle_padding_shape(padding, n, mode): - padding = tuple([ - (padding[i * 2], padding[i * 2 + 1]) - for i in range(int(len(padding) / 2) - 1, -1, -1) - ]) + padding = tuple( + [ + (padding[i * 2], padding[i * 2 + 1]) + for i in range(int(len(padding) / 2) - 1, -1, -1) + ] + ) if mode == "circular": padding = padding + ((0, 0),) * (n - len(padding)) else: diff --git a/ivy/functional/frontends/torch/nn/modules/module.py b/ivy/functional/frontends/torch/nn/modules/module.py index c59e52b5eb97c..66dc306159614 100644 --- a/ivy/functional/frontends/torch/nn/modules/module.py +++ b/ivy/functional/frontends/torch/nn/modules/module.py @@ -36,11 +36,13 @@ def _create_variables(self, device=None, dtype=None): # Create variables stored in the `__dict__` that were set # using direct `__setattr__` e.g. self.weight = ... v = ivy.Container( - OrderedDict([ - (k.replace(".", "/"), v) - for k, v in self.__dict__.items() - if isinstance(v, Parameter) - ]), + OrderedDict( + [ + (k.replace(".", "/"), v) + for k, v in self.__dict__.items() + if isinstance(v, Parameter) + ] + ), dynamic_backend=self._dynamic_backend, ) # Created variables that were added using `register_paramter`, @@ -48,12 +50,14 @@ def _create_variables(self, device=None, dtype=None): v = ( ivy.Container( OrderedDict( - ({ - _k.replace(".", "/"): _v - for (_k, _v) in self._v.items() - if _k.replace(".", "/") not in v - and not isinstance(_v, ivy.Container) - }), + ( + { + _k.replace(".", "/"): _v + for (_k, _v) in self._v.items() + if _k.replace(".", "/") not in v + and not isinstance(_v, ivy.Container) + } + ), **v, ) ) @@ -132,7 +136,6 @@ def get_submodule(self, target: str) -> "Module": mod: Module = self for item in atoms: - if not hasattr(mod, item): raise AttributeError( mod._get_name() + " has no attribute `" + item + "`" diff --git a/ivy/functional/frontends/xgboost/core.py b/ivy/functional/frontends/xgboost/core.py index 0ec6ac791099e..c5404abf5d839 100644 --- a/ivy/functional/frontends/xgboost/core.py +++ b/ivy/functional/frontends/xgboost/core.py @@ -103,11 +103,13 @@ def __init__(self, params=None, cache=None, model_file=None, compile=False): ) # add num_feature, num_target and num_instances to params - params.update({ - "num_feature": n_feat, - "num_output_group": n_output_group - 1, - "num_instances": n_inst, - }) + params.update( + { + "num_feature": n_feat, + "num_output_group": n_output_group - 1, + "num_instances": n_inst, + } + ) # create gbm(as for now only gblinear booster is available) self.gbm = GBLinear(params, compile=compile, cache=cache) diff --git a/ivy/functional/frontends/xgboost/gbm/gbm.py b/ivy/functional/frontends/xgboost/gbm/gbm.py index 8241acf4d29e1..41271ea82cc56 100644 --- a/ivy/functional/frontends/xgboost/gbm/gbm.py +++ b/ivy/functional/frontends/xgboost/gbm/gbm.py @@ -163,10 +163,12 @@ def _get_gradient(obj, pred, label, scale_pos_weight): # group weights for positive class are scaled w_scaled = ivy.where(label == 1.0, w * scale_pos_weight, w) - return ivy.hstack([ - obj.first_order_gradient(p, label) * w_scaled, - obj.second_order_gradient(p, label) * w_scaled, - ]) + return ivy.hstack( + [ + obj.first_order_gradient(p, label) * w_scaled, + obj.second_order_gradient(p, label) * w_scaled, + ] + ) def _pred(dt, w, base): diff --git a/ivy_tests/test_ivy/test_frontends/test_jax/test__src/test_tree_util.py b/ivy_tests/test_ivy/test_frontends/test_jax/test__src/test_tree_util.py index 5a53c51bd5196..171cf37f2d825 100644 --- a/ivy_tests/test_ivy/test_frontends/test_jax/test__src/test_tree_util.py +++ b/ivy_tests/test_ivy/test_frontends/test_jax/test__src/test_tree_util.py @@ -87,14 +87,16 @@ def tree_strategy(max_depth=2): return leaf_strategy() else: return st.dictionaries( - keys=st.one_of(*[ - st.text( - alphabet=st.characters(min_codepoint=97, max_codepoint=122), - min_size=1, - max_size=1, - ).filter(lambda x: x not in used_keys) - for used_keys in [set()] - ]), + keys=st.one_of( + *[ + st.text( + alphabet=st.characters(min_codepoint=97, max_codepoint=122), + min_size=1, + max_size=1, + ).filter(lambda x: x not in used_keys) + for used_keys in [set()] + ] + ), values=st.one_of(leaf_strategy(), tree_strategy(max_depth - 1)), min_size=1, max_size=10, diff --git a/ivy_tests/test_ivy/test_frontends/test_jax/test_lax/test_operators.py b/ivy_tests/test_ivy/test_frontends/test_jax/test_lax/test_operators.py index f355a902382ea..649b2cfaba96f 100644 --- a/ivy_tests/test_ivy/test_frontends/test_jax/test_lax/test_operators.py +++ b/ivy_tests/test_ivy/test_frontends/test_jax/test_lax/test_operators.py @@ -460,24 +460,30 @@ def _x_and_filters(draw, dim=2, transpose=False, general=False): dim_num_st2 = st.sampled_from(["OIDHW", "DHWIO"]) dim_seq = [*range(0, dim + 2)] dimension_numbers = draw( - st.sampled_from([ - None, - (draw(dim_num_st1), draw(dim_num_st2), draw(dim_num_st1)), - ConvDimensionNumbers( - *map( - tuple, - draw( - st.lists(st.permutations(dim_seq), min_size=3, max_size=3) - ), - ) - ), - ]) + st.sampled_from( + [ + None, + (draw(dim_num_st1), draw(dim_num_st2), draw(dim_num_st1)), + ConvDimensionNumbers( + *map( + tuple, + draw( + st.lists( + st.permutations(dim_seq), min_size=3, max_size=3 + ) + ), + ) + ), + ] + ) ) else: dimension_numbers = ( ("NCH", "OIH", "NCH") if dim == 1 - else ("NCHW", "OIHW", "NCHW") if dim == 2 else ("NCDHW", "OIDHW", "NCDHW") + else ("NCHW", "OIHW", "NCHW") + if dim == 2 + else ("NCDHW", "OIDHW", "NCDHW") ) dim_nums = _dimension_numbers(dimension_numbers, dim + 2, transp=transpose) if not transpose: diff --git a/ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_indexing.py b/ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_indexing.py index 5ebef754a9aab..4d76bfcc9a139 100644 --- a/ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_indexing.py +++ b/ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_indexing.py @@ -113,9 +113,9 @@ def test_jax_choose( on_device, ): dtypes, x, indices, axis, _ = dtype_x_indices_axis - choices = ivy.array([ - np.random.randint(0, 10, size=x.shape) for _ in range(len(dtypes)) - ]) + choices = ivy.array( + [np.random.randint(0, 10, size=x.shape) for _ in range(len(dtypes))] + ) helpers.test_frontend_function( input_dtypes=dtypes, backend_to_test=backend_fw, diff --git a/ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_linalg.py b/ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_linalg.py index c027097165f2e..fb081e1cd406a 100644 --- a/ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_linalg.py +++ b/ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_linalg.py @@ -39,9 +39,9 @@ def _get_inv_square_matrices(draw): ) shape, ind = draw( - st.sampled_from([ - (generated_shape, generated_ind), (handpicked_shape, handpicked_ind) - ]) + st.sampled_from( + [(generated_shape, generated_ind), (handpicked_shape, handpicked_ind)] + ) ) input_dtype = draw( diff --git a/ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_manipulations.py b/ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_manipulations.py index 92b3a644c42bb..2d9d150748115 100644 --- a/ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_manipulations.py +++ b/ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_manipulations.py @@ -111,19 +111,21 @@ def _get_input_and_block(draw): max_size=10, ) ) - x_dtypes, xs = zip(*[ - draw( - helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), - min_num_dims=1, - max_num_dims=5, - min_dim_size=2, - max_dim_size=10, - shape=shape, + x_dtypes, xs = zip( + *[ + draw( + helpers.dtype_and_values( + available_dtypes=helpers.get_dtypes("valid"), + min_num_dims=1, + max_num_dims=5, + min_dim_size=2, + max_dim_size=10, + shape=shape, + ) ) - ) - for shape in shapes - ]) + for shape in shapes + ] + ) return x_dtypes, xs @@ -228,18 +230,20 @@ def _get_input_and_two_swapabble_axes(draw): @st.composite def _pad_helper(draw): mode = draw( - st.sampled_from([ - "constant", - "edge", - "linear_ramp", - "maximum", - "mean", - "median", - "minimum", - "reflect", - "symmetric", - "wrap", - ]) + st.sampled_from( + [ + "constant", + "edge", + "linear_ramp", + "maximum", + "mean", + "median", + "minimum", + "reflect", + "symmetric", + "wrap", + ] + ) ) if mode == "median": dtypes = "float" diff --git a/ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_statistical.py b/ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_statistical.py index c972407723b23..28d6388d2af2c 100644 --- a/ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_statistical.py +++ b/ivy_tests/test_ivy/test_frontends/test_jax/test_numpy/test_statistical.py @@ -546,17 +546,19 @@ def test_jax_cumsum( # einsum @handle_frontend_test( fn_tree="jax.numpy.einsum", - eq_n_op=st.sampled_from([ - ( - "ii", - np.arange(25).reshape(5, 5), - ), - ( - "ii->i", - np.arange(25).reshape(5, 5), - ), - ("ij,j", np.arange(25).reshape(5, 5), np.arange(5)), - ]), + eq_n_op=st.sampled_from( + [ + ( + "ii", + np.arange(25).reshape(5, 5), + ), + ( + "ii->i", + np.arange(25).reshape(5, 5), + ), + ("ij,j", np.arange(25).reshape(5, 5), np.arange(5)), + ] + ), dtype=helpers.get_dtypes("float", full=False), ) def test_jax_einsum( diff --git a/ivy_tests/test_ivy/test_frontends/test_mindspore/test_ops/test_function/test_mindspore_nn_func.py b/ivy_tests/test_ivy/test_frontends/test_mindspore/test_ops/test_function/test_mindspore_nn_func.py index 8b07920b02287..46a0d22c152cd 100644 --- a/ivy_tests/test_ivy/test_frontends/test_mindspore/test_ops/test_function/test_mindspore_nn_func.py +++ b/ivy_tests/test_ivy/test_frontends/test_mindspore/test_ops/test_function/test_mindspore_nn_func.py @@ -529,15 +529,17 @@ def test_mindspore_flatten( num_arrays=1, shared_dtype=True, ), - mode=st.sampled_from([ - "nearest", - "linear", - "bilinear", - "bicubic", - "trilinear", - "area", - "nearest-exact", - ]), + mode=st.sampled_from( + [ + "nearest", + "linear", + "bilinear", + "bicubic", + "trilinear", + "area", + "nearest-exact", + ] + ), align_corners=st.booleans(), recompute_scale_factor=st.booleans(), size_and_scale_factor=_size_and_scale_factor_strategy(), diff --git a/ivy_tests/test_ivy/test_frontends/test_numpy/test_func_wrapper.py b/ivy_tests/test_ivy/test_frontends/test_numpy/test_func_wrapper.py index fc918a1d85da6..d93895355b185 100644 --- a/ivy_tests/test_ivy/test_frontends/test_numpy/test_func_wrapper.py +++ b/ivy_tests/test_ivy/test_frontends/test_numpy/test_func_wrapper.py @@ -23,17 +23,23 @@ @st.composite def _dtype_helper(draw): return draw( - st.sampled_from([ - draw(st.sampled_from([int, float, bool])), - ivy.as_native_dtype( - draw(helpers.get_dtypes("valid", full=False, prune_function=False))[0] - ), - np_frontend.dtype( - draw(helpers.get_dtypes("valid", full=False, prune_function=False))[0] - ), - draw(st.sampled_from(list(np_frontend.numpy_scalar_to_dtype.keys()))), - draw(st.sampled_from(list(np_frontend.numpy_str_to_type_table.keys()))), - ]) + st.sampled_from( + [ + draw(st.sampled_from([int, float, bool])), + ivy.as_native_dtype( + draw(helpers.get_dtypes("valid", full=False, prune_function=False))[ + 0 + ] + ), + np_frontend.dtype( + draw(helpers.get_dtypes("valid", full=False, prune_function=False))[ + 0 + ] + ), + draw(st.sampled_from(list(np_frontend.numpy_scalar_to_dtype.keys()))), + draw(st.sampled_from(list(np_frontend.numpy_str_to_type_table.keys()))), + ] + ) ) diff --git a/ivy_tests/test_ivy/test_frontends/test_numpy/test_indexing_routines/test_indexing_like_operations.py b/ivy_tests/test_ivy/test_frontends/test_numpy/test_indexing_routines/test_indexing_like_operations.py index 342b705f03a0f..463bf32a3a750 100644 --- a/ivy_tests/test_ivy/test_frontends/test_numpy/test_indexing_routines/test_indexing_like_operations.py +++ b/ivy_tests/test_ivy/test_frontends/test_numpy/test_indexing_routines/test_indexing_like_operations.py @@ -151,21 +151,23 @@ def test_numpy_put_along_axis( @handle_frontend_test( - fn_tree="numpy.take_along_axis", + fn_tree="numpy.take", dtype_x_indices_axis=helpers.array_indices_axis( - array_dtypes=helpers.get_dtypes("numeric"), + array_dtypes=helpers.get_dtypes("valid"), indices_dtypes=["int32", "int64"], min_num_dims=1, - max_num_dims=5, + max_num_dims=3, min_dim_size=1, - max_dim_size=10, + max_dim_size=5, indices_same_dims=True, + valid_bounds=False, ), - test_with_out=st.just(False), + mode=st.sampled_from(["clip", "wrap"]), ) -def test_numpy_take_along_axis( +def test_numpy_take( *, dtype_x_indices_axis, + mode, test_flags, frontend, backend_fw, @@ -180,30 +182,29 @@ def test_numpy_take_along_axis( frontend=frontend, fn_tree=fn_tree, on_device=on_device, - arr=x, + a=x, indices=indices, axis=axis, + mode=mode, ) @handle_frontend_test( - fn_tree="numpy.take", + fn_tree="numpy.take_along_axis", dtype_x_indices_axis=helpers.array_indices_axis( - array_dtypes=helpers.get_dtypes("valid"), + array_dtypes=helpers.get_dtypes("numeric"), indices_dtypes=["int32", "int64"], min_num_dims=1, - max_num_dims=3, + max_num_dims=5, min_dim_size=1, - max_dim_size=5, + max_dim_size=10, indices_same_dims=True, - valid_bounds=False, ), - mode=st.sampled_from(["clip", "wrap"]), + test_with_out=st.just(False), ) -def test_numpy_take( +def test_numpy_take_along_axis( *, dtype_x_indices_axis, - mode, test_flags, frontend, backend_fw, @@ -218,8 +219,7 @@ def test_numpy_take( frontend=frontend, fn_tree=fn_tree, on_device=on_device, - a=x, + arr=x, indices=indices, axis=axis, - mode=mode, ) diff --git a/ivy_tests/test_ivy/test_frontends/test_numpy/test_linalg/test_matrix_and_vector_products.py b/ivy_tests/test_ivy/test_frontends/test_numpy/test_linalg/test_matrix_and_vector_products.py index db40d797d4039..e72396bb1f976 100644 --- a/ivy_tests/test_ivy/test_frontends/test_numpy/test_linalg/test_matrix_and_vector_products.py +++ b/ivy_tests/test_ivy/test_frontends/test_numpy/test_linalg/test_matrix_and_vector_products.py @@ -161,17 +161,19 @@ def test_numpy_dot( @handle_frontend_test( fn_tree="numpy.linalg.matrix_and_vector_products.einsum", gt_fn_tree="numpy.einsum", - args=st.sampled_from([ - ( - "ii", - np.arange(25).reshape(5, 5), - ), - ( - "ii->i", - np.arange(25).reshape(5, 5), - ), - ("ij,j", np.arange(25).reshape(5, 5), np.arange(5)), - ]), + args=st.sampled_from( + [ + ( + "ii", + np.arange(25).reshape(5, 5), + ), + ( + "ii->i", + np.arange(25).reshape(5, 5), + ), + ("ij,j", np.arange(25).reshape(5, 5), np.arange(5)), + ] + ), dtype=helpers.get_dtypes("float", full=False), ) def test_numpy_einsum( diff --git a/ivy_tests/test_ivy/test_frontends/test_numpy/test_linalg/test_solving_equations_and_inverting_matrices.py b/ivy_tests/test_ivy/test_frontends/test_numpy/test_linalg/test_solving_equations_and_inverting_matrices.py index 1af6216ee5d01..3a12867c31bef 100644 --- a/ivy_tests/test_ivy/test_frontends/test_numpy/test_linalg/test_solving_equations_and_inverting_matrices.py +++ b/ivy_tests/test_ivy/test_frontends/test_numpy/test_linalg/test_solving_equations_and_inverting_matrices.py @@ -27,9 +27,9 @@ def _get_inv_square_matrices(draw): ) shape, ind = draw( - st.sampled_from([ - (generated_shape, generated_ind), (handpicked_shape, handpicked_ind) - ]) + st.sampled_from( + [(generated_shape, generated_ind), (handpicked_shape, handpicked_ind)] + ) ) input_dtype = draw( diff --git a/ivy_tests/test_ivy/test_frontends/test_numpy/test_manipulation_routines/test_padding_arrays.py b/ivy_tests/test_ivy/test_frontends/test_numpy/test_manipulation_routines/test_padding_arrays.py index 6156d1fd3b612..8fe30ac7ca58f 100644 --- a/ivy_tests/test_ivy/test_frontends/test_numpy/test_manipulation_routines/test_padding_arrays.py +++ b/ivy_tests/test_ivy/test_frontends/test_numpy/test_manipulation_routines/test_padding_arrays.py @@ -13,18 +13,20 @@ @st.composite def _pad_helper(draw): mode = draw( - st.sampled_from([ - "constant", - "edge", - "linear_ramp", - "maximum", - "mean", - "median", - "minimum", - "reflect", - "symmetric", - "wrap", - ]) + st.sampled_from( + [ + "constant", + "edge", + "linear_ramp", + "maximum", + "mean", + "median", + "minimum", + "reflect", + "symmetric", + "wrap", + ] + ) ) if mode in ["median", "mean"]: dtypes = "float" diff --git a/ivy_tests/test_ivy/test_frontends/test_numpy/test_ndarray/test_ndarray.py b/ivy_tests/test_ivy/test_frontends/test_numpy/test_ndarray/test_ndarray.py index 833da964cdfdd..6404b98dade7f 100644 --- a/ivy_tests/test_ivy/test_frontends/test_numpy/test_ndarray/test_ndarray.py +++ b/ivy_tests/test_ivy/test_frontends/test_numpy/test_ndarray/test_ndarray.py @@ -3545,12 +3545,14 @@ def test_numpy_sum( if ivy.current_backend_str() == "torch": assume(not method_flags.as_variable[0]) - where, input_dtypes, method_flags = ( - np_frontend_helpers.handle_where_and_array_bools( - where=where, - input_dtype=input_dtypes, - test_flags=method_flags, - ) + ( + where, + input_dtypes, + method_flags, + ) = np_frontend_helpers.handle_where_and_array_bools( + where=where, + input_dtype=input_dtypes, + test_flags=method_flags, ) where = ivy.array(where, dtype="bool") helpers.test_frontend_method( 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 c70b27ae2e3c2..f020caefc0d57 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 @@ -60,20 +60,22 @@ def _interp_args(draw, mode=None, mode_list=None): mode = draw(st.sampled_from(jax_modes)) else: mode = draw( - st.sampled_from([ - "linear", - "bilinear", - "trilinear", - "nearest", - "nearest-exact", - "area", - "tf_area", - "tf_bicubic", - "lanczos3", - "lanczos5", - "mitchellcubic", - "gaussian", - ]) + st.sampled_from( + [ + "linear", + "bilinear", + "trilinear", + "nearest", + "nearest-exact", + "area", + "tf_area", + "tf_bicubic", + "lanczos3", + "lanczos5", + "mitchellcubic", + "gaussian", + ] + ) ) elif mode_list: mode = draw(st.sampled_from(mode_list)) diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_tensor.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_tensor.py index 0921b094e50bb..5a911e9595be9 100644 --- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_tensor.py +++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_tensor/test_tensor.py @@ -1144,40 +1144,6 @@ def test_paddle_abs( ) -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="paddle.to_tensor", - method_name="acos", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("float"), - ), -) -def test_paddle_tensor_acos( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - # acosh @handle_frontend_method( class_tree=CLASS_TREE, @@ -1213,41 +1179,6 @@ def test_paddle_acosh( ) -# add -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="paddle.to_tensor", - method_name="add", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("valid"), num_arrays=2, shared_dtype=True - ), -) -def test_paddle_tensor_add( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, - backend_fw, -): - input_dtype, x = dtype_and_x - helpers.test_frontend_method( - init_input_dtypes=input_dtype, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "data": x[0], - }, - method_input_dtypes=input_dtype, - method_all_as_kwargs_np={"y": x[1]}, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - frontend=frontend, - on_device=on_device, - ) - - # add_ @handle_frontend_method( class_tree=CLASS_TREE, @@ -5374,6 +5305,75 @@ def test_paddle_tanh_( ) +@handle_frontend_method( + class_tree=CLASS_TREE, + init_tree="paddle.to_tensor", + method_name="acos", + dtype_and_x=helpers.dtype_and_values( + available_dtypes=helpers.get_dtypes("float"), + ), +) +def test_paddle_tensor_acos( + dtype_and_x, + frontend_method_data, + init_flags, + method_flags, + frontend, + on_device, + backend_fw, +): + input_dtype, x = dtype_and_x + helpers.test_frontend_method( + init_input_dtypes=input_dtype, + backend_to_test=backend_fw, + init_all_as_kwargs_np={ + "data": x[0], + }, + method_input_dtypes=input_dtype, + method_all_as_kwargs_np={}, + frontend_method_data=frontend_method_data, + init_flags=init_flags, + method_flags=method_flags, + frontend=frontend, + on_device=on_device, + ) + + +# add +@handle_frontend_method( + class_tree=CLASS_TREE, + init_tree="paddle.to_tensor", + method_name="add", + dtype_and_x=helpers.dtype_and_values( + available_dtypes=helpers.get_dtypes("valid"), num_arrays=2, shared_dtype=True + ), +) +def test_paddle_tensor_add( + dtype_and_x, + frontend_method_data, + init_flags, + method_flags, + frontend, + on_device, + backend_fw, +): + input_dtype, x = dtype_and_x + helpers.test_frontend_method( + init_input_dtypes=input_dtype, + backend_to_test=backend_fw, + init_all_as_kwargs_np={ + "data": x[0], + }, + method_input_dtypes=input_dtype, + method_all_as_kwargs_np={"y": x[1]}, + frontend_method_data=frontend_method_data, + init_flags=init_flags, + method_flags=method_flags, + frontend=frontend, + on_device=on_device, + ) + + # chunk @handle_frontend_method( class_tree=CLASS_TREE, diff --git a/ivy_tests/test_ivy/test_frontends/test_tensorflow/test_func_wrapper.py b/ivy_tests/test_ivy/test_frontends/test_tensorflow/test_func_wrapper.py index 5d0290a5ea5cb..036a6fa1e8c54 100644 --- a/ivy_tests/test_ivy/test_frontends/test_tensorflow/test_func_wrapper.py +++ b/ivy_tests/test_ivy/test_frontends/test_tensorflow/test_func_wrapper.py @@ -22,18 +22,26 @@ @st.composite def _dtype_helper(draw): return draw( - st.sampled_from([ - draw(helpers.get_dtypes("valid", prune_function=False, full=False))[0], - ivy.as_native_dtype( - draw(helpers.get_dtypes("valid", prune_function=False, full=False))[0] - ), - draw(st.sampled_from(list(tf_frontend.tensorflow_enum_to_type.values()))), - draw(st.sampled_from(list(tf_frontend.tensorflow_enum_to_type.keys()))), - np_frontend.dtype( - draw(helpers.get_dtypes("valid", prune_function=False, full=False))[0] - ), - draw(st.sampled_from(list(np_frontend.numpy_scalar_to_dtype.keys()))), - ]) + st.sampled_from( + [ + draw(helpers.get_dtypes("valid", prune_function=False, full=False))[0], + ivy.as_native_dtype( + draw(helpers.get_dtypes("valid", prune_function=False, full=False))[ + 0 + ] + ), + draw( + st.sampled_from(list(tf_frontend.tensorflow_enum_to_type.values())) + ), + draw(st.sampled_from(list(tf_frontend.tensorflow_enum_to_type.keys()))), + np_frontend.dtype( + draw(helpers.get_dtypes("valid", prune_function=False, full=False))[ + 0 + ] + ), + draw(st.sampled_from(list(np_frontend.numpy_scalar_to_dtype.keys()))), + ] + ) ) diff --git a/ivy_tests/test_ivy/test_frontends/test_tensorflow/test_general_functions.py b/ivy_tests/test_ivy/test_frontends/test_tensorflow/test_general_functions.py index 436047049f5e4..bec86d46301a2 100644 --- a/ivy_tests/test_ivy/test_frontends/test_tensorflow/test_general_functions.py +++ b/ivy_tests/test_ivy/test_frontends/test_tensorflow/test_general_functions.py @@ -228,11 +228,13 @@ def _multiple_shape_helper(draw): @st.composite def _pad_helper(draw): mode = draw( - st.sampled_from([ - "CONSTANT", - "REFLECT", - "SYMMETRIC", - ]) + st.sampled_from( + [ + "CONSTANT", + "REFLECT", + "SYMMETRIC", + ] + ) ) dtype, input, shape = draw( helpers.dtype_and_values( @@ -283,19 +285,21 @@ def _sequence_mask_helper(draw): max_len = draw(st.integers(min_value=max_val, max_value=max_val)) dtype = draw( - st.sampled_from([ - "float16", - "uint8", - "complex128", - "bool", - "float64", - "int8", - "int16", - "complex64", - "float32", - "int32", - "int64", - ]) + st.sampled_from( + [ + "float16", + "uint8", + "complex128", + "bool", + "float64", + "int8", + "int16", + "complex64", + "float32", + "int32", + "int64", + ] + ) ) return in_dtype, lens, max_len, dtype @@ -725,11 +729,13 @@ def test_tensorflow_convert_to_tensor( # einsum @handle_frontend_test( fn_tree="tensorflow.einsum", - eq_n_op_n_shp=st.sampled_from([ - ("ii", (np.arange(25).reshape(5, 5),), ()), - ("ii->i", (np.arange(25).reshape(5, 5),), (5,)), - ("ij,j", (np.arange(25).reshape(5, 5), np.arange(5)), (5,)), - ]), + eq_n_op_n_shp=st.sampled_from( + [ + ("ii", (np.arange(25).reshape(5, 5),), ()), + ("ii->i", (np.arange(25).reshape(5, 5),), (5,)), + ("ij,j", (np.arange(25).reshape(5, 5), np.arange(5)), (5,)), + ] + ), dtype=helpers.get_dtypes("float", full=False), ) def test_tensorflow_einsum( diff --git a/ivy_tests/test_ivy/test_frontends/test_tensorflow/test_linalg.py b/ivy_tests/test_ivy/test_frontends/test_tensorflow/test_linalg.py index 1a021be263a75..936a069aa950e 100644 --- a/ivy_tests/test_ivy/test_frontends/test_tensorflow/test_linalg.py +++ b/ivy_tests/test_ivy/test_frontends/test_tensorflow/test_linalg.py @@ -1288,11 +1288,13 @@ def tridiagonal_compact_filter(x): dim = diagonals[0].shape[0] diagonals[[0, -1], [-1, 0]] = 0 dummy_idx = [0, 0] - indices = ivy.array([ - [(i, i + 1) for i in range(dim - 1)] + [dummy_idx], - [(i, i) for i in range(dim)], - [dummy_idx] + [(i + 1, i) for i in range(dim - 1)], - ]) + indices = ivy.array( + [ + [(i, i + 1) for i in range(dim - 1)] + [dummy_idx], + [(i, i) for i in range(dim)], + [dummy_idx] + [(i + 1, i) for i in range(dim - 1)], + ] + ) matrix = ivy.scatter_nd( indices, diagonals, ivy.array([dim, dim]), reduction="replace" ) diff --git a/ivy_tests/test_ivy/test_frontends/test_torch/test_nn/test_functional/test_vision_functions.py b/ivy_tests/test_ivy/test_frontends/test_torch/test_nn/test_functional/test_vision_functions.py index 5d9de4b6aff10..912c3e82599be 100644 --- a/ivy_tests/test_ivy/test_frontends/test_torch/test_nn/test_functional/test_vision_functions.py +++ b/ivy_tests/test_ivy/test_frontends/test_torch/test_nn/test_functional/test_vision_functions.py @@ -78,12 +78,14 @@ def _pad_generator(draw, shape, mode): @st.composite def _pad_helper(draw): mode = draw( - st.sampled_from([ - "constant", - "reflect", - "replicate", - "circular", - ]) + st.sampled_from( + [ + "constant", + "reflect", + "replicate", + "circular", + ] + ) ) min_v = 1 max_v = 5 diff --git a/ivy_tests/test_ivy/test_functional/test_core/test_creation.py b/ivy_tests/test_ivy/test_functional/test_core/test_creation.py index c2686e8d00b35..99a105e454651 100644 --- a/ivy_tests/test_ivy/test_functional/test_core/test_creation.py +++ b/ivy_tests/test_ivy/test_functional/test_core/test_creation.py @@ -46,12 +46,14 @@ def _asarray_helper(draw): )[-1] dtype = draw(st.sampled_from([dtype, None])) x = draw( - st.sampled_from([ - x, - x_list, - sh, - # nested_values, - ]) + st.sampled_from( + [ + x, + x_list, + sh, + # nested_values, + ] + ) ) return x_dtype, x, dtype diff --git a/ivy_tests/test_ivy/test_functional/test_core/test_general.py b/ivy_tests/test_ivy/test_functional/test_core/test_general.py index 0ce3c2eb561c7..ab35dc81db1a5 100644 --- a/ivy_tests/test_ivy/test_functional/test_core/test_general.py +++ b/ivy_tests/test_ivy/test_functional/test_core/test_general.py @@ -408,10 +408,12 @@ def test_arg_info(): @given( - x_n_value=st.sampled_from([ - [ivy.value_is_nan, ["x", "include_infs"]], - [ivy.clip_matrix_norm, ["x", "max_norm", "p", "out"]], - ]) + x_n_value=st.sampled_from( + [ + [ivy.value_is_nan, ["x", "include_infs"]], + [ivy.clip_matrix_norm, ["x", "max_norm", "p", "out"]], + ] + ) ) def test_arg_names(x_n_value): x, value = x_n_value @@ -710,15 +712,17 @@ def test_default(x, default_val, test_flags, backend_fw): and (ivy.array([x[1][0]], dtype="float32").shape[3] % 2 == 0) and (x[0][0] not in ["float16", "bfloat16"]) ), - pattern_and_axes_lengths=st.sampled_from([ - ("b h w c -> b h w c", {}), - ("b h w c -> (b h) w c", {}), - ("b h w c -> b c h w", {}), - ("b h w c -> h (b w) c", {}), - ("b h w c -> b (c h w)", {}), - ("b (h1 h) (w1 w) c -> (b h1 w1) h w c", {"h1": 2, "w1": 2}), - ("b (h h1) (w w1) c -> b h w (c h1 w1)", {"h1": 2, "w1": 2}), - ]), + pattern_and_axes_lengths=st.sampled_from( + [ + ("b h w c -> b h w c", {}), + ("b h w c -> (b h) w c", {}), + ("b h w c -> b c h w", {}), + ("b h w c -> h (b w) c", {}), + ("b h w c -> b (c h w)", {}), + ("b (h1 h) (w1 w) c -> (b h1 w1) h w c", {"h1": 2, "w1": 2}), + ("b (h h1) (w w1) c -> b h w (c h1 w1)", {"h1": 2, "w1": 2}), + ] + ), ) def test_einops_rearrange( dtype_x, pattern_and_axes_lengths, test_flags, backend_fw, fn_name, on_device @@ -754,9 +758,11 @@ def test_einops_rearrange( and (ivy.array([x[1][0]], dtype="float32").shape[3] % 2 == 0) and (x[0][0] not in ["float16", "bfloat16"]) ), - pattern_and_axes_lengths=st.sampled_from([ - ("b c (h1 h2) (w1 w2) -> b c h1 w1", {"h2": 2, "w2": 2}), - ]), + pattern_and_axes_lengths=st.sampled_from( + [ + ("b c (h1 h2) (w1 w2) -> b c h1 w1", {"h2": 2, "w2": 2}), + ] + ), floattypes=helpers.get_dtypes("float"), reduction=st.sampled_from(["min", "max", "sum", "mean", "prod"]), ) @@ -803,13 +809,15 @@ def test_einops_reduce( max_num_dims=2, min_dim_size=2, ), - pattern_and_axes_lengths=st.sampled_from([ - ("h w -> h w repeat", {"repeat": 2}), - ("h w -> (repeat h) w", {"repeat": 2}), - ("h w -> h (repeat w)", {"repeat": 2}), - ("h w -> (h h2) (w w2)", {"h2": 2, "w2": 2}), - ("h w -> w h", {}), - ]), + pattern_and_axes_lengths=st.sampled_from( + [ + ("h w -> h w repeat", {"repeat": 2}), + ("h w -> (repeat h) w", {"repeat": 2}), + ("h w -> h (repeat w)", {"repeat": 2}), + ("h w -> (h h2) (w w2)", {"h2": 2, "w2": 2}), + ("h w -> w h", {}), + ] + ), ) def test_einops_repeat( *, dtype_x, pattern_and_axes_lengths, test_flags, backend_fw, fn_name, on_device diff --git a/ivy_tests/test_ivy/test_functional/test_core/test_meta.py b/ivy_tests/test_ivy/test_functional/test_core/test_meta.py index 54fceaf059e28..a0b3cc5723e1c 100644 --- a/ivy_tests/test_ivy/test_functional/test_core/test_meta.py +++ b/ivy_tests/test_ivy/test_functional/test_core/test_meta.py @@ -45,32 +45,36 @@ def test_fomaml_step_overlapping_vars( # create variables if batched: - variables = ivy_backend.Container({ - "latent": variable_fn( - ivy_backend.repeat( - ivy_backend.array([[0.0]], device=on_device), - num_tasks, - axis=0, - ) - ), - "weight": variable_fn( - ivy_backend.repeat( - ivy_backend.array([[1.0]], device=on_device), - num_tasks, - axis=0, - ) - ), - }) + variables = ivy_backend.Container( + { + "latent": variable_fn( + ivy_backend.repeat( + ivy_backend.array([[0.0]], device=on_device), + num_tasks, + axis=0, + ) + ), + "weight": variable_fn( + ivy_backend.repeat( + ivy_backend.array([[1.0]], device=on_device), + num_tasks, + axis=0, + ) + ), + } + ) else: - variables = ivy_backend.Container({ - "latent": variable_fn(ivy_backend.array([0.0], device=on_device)), - "weight": variable_fn(ivy_backend.array([1.0], device=on_device)), - }) + variables = ivy_backend.Container( + { + "latent": variable_fn(ivy_backend.array([0.0], device=on_device)), + "weight": variable_fn(ivy_backend.array([1.0], device=on_device)), + } + ) # batch - batch = ivy_backend.Container({ - "x": ivy_backend.arange(1, num_tasks + 1, dtype="float32") - }) + batch = ivy_backend.Container( + {"x": ivy_backend.arange(1, num_tasks + 1, dtype="float32")} + ) # inner cost function def inner_cost_fn(batch_in, v): @@ -102,14 +106,16 @@ def outer_cost_fn(batch_in, v): # true gradient all_outer_grads = [] for sub_batch in batch_np.cont_unstack_conts(0, True, num_tasks): - all_outer_grads.append([ - ( - -i * inner_learning_rate * weight_np * sub_batch["x"][0] ** 2 - - sub_batch["x"][0] * latent_np - ) - * (-1 if with_outer_cost_fn else 1) - for i in range(inner_grad_steps + 1) - ]) + all_outer_grads.append( + [ + ( + -i * inner_learning_rate * weight_np * sub_batch["x"][0] ** 2 + - sub_batch["x"][0] * latent_np + ) + * (-1 if with_outer_cost_fn else 1) + for i in range(inner_grad_steps + 1) + ] + ) if average_across_steps: true_weight_grad = ( sum(sum(og) / len(og) for og in all_outer_grads) / num_tasks @@ -118,9 +124,9 @@ def outer_cost_fn(batch_in, v): true_weight_grad = sum(og[-1] for og in all_outer_grads) / num_tasks # true latent gradient - true_latent_grad = np.array([ - (-1 - (num_tasks - 1) / 2) * (-1 if with_outer_cost_fn else 1) - ]) + true_latent_grad = np.array( + [(-1 - (num_tasks - 1) / 2) * (-1 if with_outer_cost_fn else 1)] + ) # true cost true_cost_dict = { @@ -212,24 +218,26 @@ def test_fomaml_step_shared_vars( # create variable if batched: - variables = ivy_backend.Container({ - "latent": variable_fn( - ivy_backend.repeat( - ivy_backend.array([[1.0]], device=on_device), - num_tasks, - axis=0, + variables = ivy_backend.Container( + { + "latent": variable_fn( + ivy_backend.repeat( + ivy_backend.array([[1.0]], device=on_device), + num_tasks, + axis=0, + ) ) - ) - }) + } + ) else: - variables = ivy_backend.Container({ - "latent": variable_fn(ivy_backend.array([1.0], device=on_device)) - }) + variables = ivy_backend.Container( + {"latent": variable_fn(ivy_backend.array([1.0], device=on_device))} + ) # batch - batch = ivy_backend.Container({ - "x": ivy_backend.arange(1, num_tasks + 1, dtype="float32") - }) + batch = ivy_backend.Container( + {"x": ivy_backend.arange(1, num_tasks + 1, dtype="float32")} + ) # inner cost function def inner_cost_fn(batch_in, v): @@ -401,32 +409,36 @@ def test_fomaml_step_unique_vars( # create variables if batched: - variables = ivy_backend.Container({ - "latent": variable_fn( - ivy_backend.repeat( - ivy_backend.array([[0.0]], device=on_device), - num_tasks, - axis=0, - ) - ), - "weight": variable_fn( - ivy_backend.repeat( - ivy_backend.array([[1.0]], device=on_device), - num_tasks, - axis=0, - ) - ), - }) + variables = ivy_backend.Container( + { + "latent": variable_fn( + ivy_backend.repeat( + ivy_backend.array([[0.0]], device=on_device), + num_tasks, + axis=0, + ) + ), + "weight": variable_fn( + ivy_backend.repeat( + ivy_backend.array([[1.0]], device=on_device), + num_tasks, + axis=0, + ) + ), + } + ) else: - variables = ivy_backend.Container({ - "latent": variable_fn(ivy_backend.array([0.0], device=on_device)), - "weight": variable_fn(ivy_backend.array([1.0], device=on_device)), - }) + variables = ivy_backend.Container( + { + "latent": variable_fn(ivy_backend.array([0.0], device=on_device)), + "weight": variable_fn(ivy_backend.array([1.0], device=on_device)), + } + ) # batch - batch = ivy_backend.Container({ - "x": ivy_backend.arange(1, num_tasks + 1, dtype="float32") - }) + batch = ivy_backend.Container( + {"x": ivy_backend.arange(1, num_tasks + 1, dtype="float32")} + ) # inner cost function def inner_cost_fn(batch_in, v): @@ -458,14 +470,16 @@ def outer_cost_fn(batch_in, v): # true gradient all_outer_grads = [] for sub_batch in batch_np.cont_unstack_conts(0, True, num_tasks): - all_outer_grads.append([ - ( - -i * inner_learning_rate * weight_np * sub_batch["x"][0] ** 2 - - sub_batch["x"][0] * latent_np - ) - * (-1 if with_outer_cost_fn else 1) - for i in range(inner_grad_steps + 1) - ]) + all_outer_grads.append( + [ + ( + -i * inner_learning_rate * weight_np * sub_batch["x"][0] ** 2 + - sub_batch["x"][0] * latent_np + ) + * (-1 if with_outer_cost_fn else 1) + for i in range(inner_grad_steps + 1) + ] + ) if average_across_steps: true_weight_grad = ( sum(sum(og) / len(og) for og in all_outer_grads) / num_tasks @@ -559,32 +573,36 @@ def test_maml_step_overlapping_vars( # create variables if batched: - variables = ivy_backend.Container({ - "latent": variable_fn( - ivy_backend.repeat( - ivy_backend.array([[0.0]], device=on_device), - num_tasks, - axis=0, - ) - ), - "weight": variable_fn( - ivy_backend.repeat( - ivy_backend.array([[1.0]], device=on_device), - num_tasks, - axis=0, - ) - ), - }) + variables = ivy_backend.Container( + { + "latent": variable_fn( + ivy_backend.repeat( + ivy_backend.array([[0.0]], device=on_device), + num_tasks, + axis=0, + ) + ), + "weight": variable_fn( + ivy_backend.repeat( + ivy_backend.array([[1.0]], device=on_device), + num_tasks, + axis=0, + ) + ), + } + ) else: - variables = ivy_backend.Container({ - "latent": variable_fn(ivy_backend.array([0.0], device=on_device)), - "weight": variable_fn(ivy_backend.array([1.0], device=on_device)), - }) + variables = ivy_backend.Container( + { + "latent": variable_fn(ivy_backend.array([0.0], device=on_device)), + "weight": variable_fn(ivy_backend.array([1.0], device=on_device)), + } + ) # batch - batch = ivy_backend.Container({ - "x": ivy_backend.arange(1, num_tasks + 1, dtype="float32") - }) + batch = ivy_backend.Container( + {"x": ivy_backend.arange(1, num_tasks + 1, dtype="float32")} + ) # inner cost function def inner_cost_fn(batch_in, v): @@ -616,14 +634,20 @@ def outer_cost_fn(batch_in, v): # true weight gradient all_outer_grads = [] for sub_batch in batch_np.cont_unstack_conts(0, True, num_tasks): - all_outer_grads.append([ - ( - -2 * i * inner_learning_rate * weight_np * sub_batch["x"][0] ** 2 - - sub_batch["x"][0] * latent_np - ) - * (-1 if with_outer_cost_fn else 1) - for i in range(inner_grad_steps + 1) - ]) + all_outer_grads.append( + [ + ( + -2 + * i + * inner_learning_rate + * weight_np + * sub_batch["x"][0] ** 2 + - sub_batch["x"][0] * latent_np + ) + * (-1 if with_outer_cost_fn else 1) + for i in range(inner_grad_steps + 1) + ] + ) if average_across_steps: true_weight_grad = ( sum(sum(og) / len(og) for og in all_outer_grads) / num_tasks @@ -632,9 +656,9 @@ def outer_cost_fn(batch_in, v): true_weight_grad = sum(og[-1] for og in all_outer_grads) / num_tasks # true latent gradient - true_latent_grad = np.array([ - (-1 - (num_tasks - 1) / 2) * (-1 if with_outer_cost_fn else 1) - ]) + true_latent_grad = np.array( + [(-1 - (num_tasks - 1) / 2) * (-1 if with_outer_cost_fn else 1)] + ) # true cost true_cost_dict = { @@ -724,24 +748,26 @@ def test_maml_step_shared_vars( # create variable if batched: - variables = ivy_backend.Container({ - "latent": variable_fn( - ivy_backend.repeat( - ivy_backend.array([[1.0]], device=on_device), - num_tasks, - axis=0, + variables = ivy_backend.Container( + { + "latent": variable_fn( + ivy_backend.repeat( + ivy_backend.array([[1.0]], device=on_device), + num_tasks, + axis=0, + ) ) - ) - }) + } + ) else: - variables = ivy_backend.Container({ - "latent": variable_fn(ivy_backend.array([1.0], device=on_device)) - }) + variables = ivy_backend.Container( + {"latent": variable_fn(ivy_backend.array([1.0], device=on_device))} + ) # batch - batch = ivy_backend.Container({ - "x": ivy_backend.arange(1, num_tasks + 1, dtype="float32") - }) + batch = ivy_backend.Container( + {"x": ivy_backend.arange(1, num_tasks + 1, dtype="float32")} + ) # inner cost function def inner_cost_fn(batch_in, v): @@ -954,32 +980,36 @@ def test_maml_step_unique_vars( # create variables if batched: - variables = ivy_backend.Container({ - "latent": variable_fn( - ivy_backend.repeat( - ivy_backend.array([[0.0]], device=on_device), - num_tasks, - axis=0, - ) - ), - "weight": variable_fn( - ivy_backend.repeat( - ivy_backend.array([[1.0]], device=on_device), - num_tasks, - axis=0, - ) - ), - }) + variables = ivy_backend.Container( + { + "latent": variable_fn( + ivy_backend.repeat( + ivy_backend.array([[0.0]], device=on_device), + num_tasks, + axis=0, + ) + ), + "weight": variable_fn( + ivy_backend.repeat( + ivy_backend.array([[1.0]], device=on_device), + num_tasks, + axis=0, + ) + ), + } + ) else: - variables = ivy_backend.Container({ - "latent": variable_fn(ivy_backend.array([0.0], device=on_device)), - "weight": variable_fn(ivy_backend.array([1.0], device=on_device)), - }) + variables = ivy_backend.Container( + { + "latent": variable_fn(ivy_backend.array([0.0], device=on_device)), + "weight": variable_fn(ivy_backend.array([1.0], device=on_device)), + } + ) # batch - batch = ivy_backend.Container({ - "x": ivy_backend.arange(1, num_tasks + 1, dtype="float32") - }) + batch = ivy_backend.Container( + {"x": ivy_backend.arange(1, num_tasks + 1, dtype="float32")} + ) # inner cost function def inner_cost_fn(batch_in, v): @@ -1011,14 +1041,20 @@ def outer_cost_fn(batch_in, v): # true gradient all_outer_grads = [] for sub_batch in batch_np.cont_unstack_conts(0, True, num_tasks): - all_outer_grads.append([ - ( - -2 * i * inner_learning_rate * weight_np * sub_batch["x"][0] ** 2 - - sub_batch["x"][0] * latent_np - ) - * (-1 if with_outer_cost_fn else 1) - for i in range(inner_grad_steps + 1) - ]) + all_outer_grads.append( + [ + ( + -2 + * i + * inner_learning_rate + * weight_np + * sub_batch["x"][0] ** 2 + - sub_batch["x"][0] * latent_np + ) + * (-1 if with_outer_cost_fn else 1) + for i in range(inner_grad_steps + 1) + ] + ) if average_across_steps: true_outer_grad = ( sum(sum(og) / len(og) for og in all_outer_grads) / num_tasks @@ -1107,24 +1143,26 @@ def test_reptile_step( # create variable if batched: - variables = ivy_backend.Container({ - "latent": variable_fn( - ivy_backend.repeat( - ivy_backend.array([[1.0]], device=on_device), - num_tasks, - axis=0, + variables = ivy_backend.Container( + { + "latent": variable_fn( + ivy_backend.repeat( + ivy_backend.array([[1.0]], device=on_device), + num_tasks, + axis=0, + ) ) - ) - }) + } + ) else: - variables = ivy_backend.Container({ - "latent": variable_fn(ivy_backend.array([1.0], device=on_device)) - }) + variables = ivy_backend.Container( + {"latent": variable_fn(ivy_backend.array([1.0], device=on_device))} + ) # batch - batch = ivy_backend.Container({ - "x": ivy_backend.arange(1, num_tasks + 1, dtype="float32") - }) + batch = ivy_backend.Container( + {"x": ivy_backend.arange(1, num_tasks + 1, dtype="float32")} + ) # inner cost function def inner_cost_fn(batch_in, v): diff --git a/ivy_tests/test_ivy/test_functional/test_experimental/test_core/test_gradients.py b/ivy_tests/test_ivy/test_functional/test_experimental/test_core/test_gradients.py index b5226c371a127..f34b3513a8290 100644 --- a/ivy_tests/test_ivy/test_functional/test_experimental/test_core/test_gradients.py +++ b/ivy_tests/test_ivy/test_functional/test_experimental/test_core/test_gradients.py @@ -13,22 +13,26 @@ def _get_primals_and_tangents(x_, dtype, ivy_backend, primals_cont, tangents_cont): if primals_cont: - primals = ivy_backend.Container({ - "l": { - "a": ivy_backend.array(x_[0][0], dtype=dtype), - "b": ivy_backend.array(x_[0][1], dtype=dtype), + primals = ivy_backend.Container( + { + "l": { + "a": ivy_backend.array(x_[0][0], dtype=dtype), + "b": ivy_backend.array(x_[0][1], dtype=dtype), + } } - }) + ) else: primals = ivy_backend.array(x_[0], dtype=dtype) if tangents_cont: - tangents = ivy_backend.Container({ - "l": { - "a": ivy_backend.array([t[0] for t in x_[1]], dtype=dtype), - "b": ivy_backend.array([t[0] for t in x_[1]], dtype=dtype), + tangents = ivy_backend.Container( + { + "l": { + "a": ivy_backend.array([t[0] for t in x_[1]], dtype=dtype), + "b": ivy_backend.array([t[0] for t in x_[1]], dtype=dtype), + } } - }) + ) else: if primals_cont: tangents = ivy_backend.array([t[0] for t in x_[1]], dtype=dtype) diff --git a/ivy_tests/test_ivy/test_functional/test_experimental/test_core/test_manipulation.py b/ivy_tests/test_ivy/test_functional/test_experimental/test_core/test_manipulation.py index 0c0bb83992e84..87b09dccecfd6 100644 --- a/ivy_tests/test_ivy/test_functional/test_experimental/test_core/test_manipulation.py +++ b/ivy_tests/test_ivy/test_functional/test_experimental/test_core/test_manipulation.py @@ -205,19 +205,21 @@ def _matricize_data(draw): @st.composite def _pad_helper(draw): mode = draw( - st.sampled_from([ - "constant", - "dilated", - "edge", - "linear_ramp", - "maximum", - "mean", - "median", - "minimum", - "reflect", - "symmetric", - "wrap", - ]) + st.sampled_from( + [ + "constant", + "dilated", + "edge", + "linear_ramp", + "maximum", + "mean", + "median", + "minimum", + "reflect", + "symmetric", + "wrap", + ] + ) ) if mode in ["median", "minimum", "maximum", "linear_ramp"]: dtypes = "float" diff --git a/ivy_tests/test_ivy/test_functional/test_experimental/test_nn/test_layers.py b/ivy_tests/test_ivy/test_functional/test_experimental/test_nn/test_layers.py index ba074d564ba52..e02a2e1b49101 100644 --- a/ivy_tests/test_ivy/test_functional/test_experimental/test_nn/test_layers.py +++ b/ivy_tests/test_ivy/test_functional/test_experimental/test_nn/test_layers.py @@ -65,20 +65,22 @@ def _interp_args(draw, mode=None, mode_list=None): mode = draw(st.sampled_from(jax_modes)) else: mode = draw( - st.sampled_from([ - "linear", - "bilinear", - "trilinear", - "nearest", - "nearest-exact", - "area", - "tf_area", - "tf_bicubic", - "lanczos3", - "lanczos5", - "mitchellcubic", - "gaussian", - ]) + st.sampled_from( + [ + "linear", + "bilinear", + "trilinear", + "nearest", + "nearest-exact", + "area", + "tf_area", + "tf_bicubic", + "lanczos3", + "lanczos5", + "mitchellcubic", + "gaussian", + ] + ) ) elif mode_list: mode = draw(st.sampled_from(mode_list)) diff --git a/ivy_tests/test_ivy/test_misc/test_array.py b/ivy_tests/test_ivy/test_misc/test_array.py index c961c59d59f0b..3098bceadf03c 100644 --- a/ivy_tests/test_ivy/test_misc/test_array.py +++ b/ivy_tests/test_ivy/test_misc/test_array.py @@ -2553,9 +2553,9 @@ def test_array_property_strides(dtype_x, backend_fw): min_value=3.0, max_value=10.0, ), - op=st.sampled_from([ - "!=", ">", "<", ">=", "<=", "*", "/", "%", "==", "&", "@", "**", "/" - ]), + op=st.sampled_from( + ["!=", ">", "<", ">=", "<=", "*", "/", "%", "==", "&", "@", "**", "/"] + ), ) def test_dunder_wrapping( dtype_x, diff --git a/ivy_tests/test_ivy/test_misc/test_backend_utils/test_backend_handler.py b/ivy_tests/test_ivy/test_misc/test_backend_utils/test_backend_handler.py index db75505b51629..16cee4a3dd4ef 100644 --- a/ivy_tests/test_ivy/test_misc/test_backend_utils/test_backend_handler.py +++ b/ivy_tests/test_ivy/test_misc/test_backend_utils/test_backend_handler.py @@ -107,10 +107,12 @@ def test_dynamic_backend_all_combos(middle_backend, end_backend): # set the middle backend ivy.set_backend(middle_backend, dynamic=True) - var_cont = ivy.Container({ - "w": ivy.gradients._variable(ivy.array([10, 20, 30])), - "b": ivy.gradients._variable(ivy.array([40, 50, 60])), - }) + var_cont = ivy.Container( + { + "w": ivy.gradients._variable(ivy.array([10, 20, 30])), + "b": ivy.gradients._variable(ivy.array([40, 50, 60])), + } + ) # set dynamic_backend to true for all objects ivy_cont.dynamic_backend = True a.dynamic_backend = True diff --git a/ivy_tests/test_ivy/test_misc/test_container.py b/ivy_tests/test_ivy/test_misc/test_container.py index 4e0621b0ef31f..f9641e5e98cbe 100644 --- a/ivy_tests/test_ivy/test_misc/test_container.py +++ b/ivy_tests/test_ivy/test_misc/test_container.py @@ -19,9 +19,9 @@ def test_container_all_false(on_device): assert not Container({"a": False, "b": {"c": [1], "d": 0}}).cont_all_false() # noinspection PyBroadException try: - assert Container({ - "a": ivy.array([1], device=on_device), "b": {"c": [1], "d": True} - }).cont_all_false(assert_is_bool=True) + assert Container( + {"a": ivy.array([1], device=on_device), "b": {"c": [1], "d": True}} + ).cont_all_false(assert_is_bool=True) error_raised = False except IvyException: error_raised = True @@ -42,17 +42,17 @@ def test_container_all_key_chains(include_empty, on_device): def test_container_all_true(on_device): - assert not Container({ - "a": ivy.array([1], device=on_device), "b": {"c": [], "d": True} - }).cont_all_true() - assert Container({ - "a": ivy.array([1], device=on_device), "b": {"c": [1], "d": True} - }).cont_all_true() + assert not Container( + {"a": ivy.array([1], device=on_device), "b": {"c": [], "d": True}} + ).cont_all_true() + assert Container( + {"a": ivy.array([1], device=on_device), "b": {"c": [1], "d": True}} + ).cont_all_true() # noinspection PyBroadException try: - assert Container({ - "a": ivy.array([1], device=on_device), "b": {"c": [1], "d": True} - }).cont_all_true(assert_is_bool=True) + assert Container( + {"a": ivy.array([1], device=on_device), "b": {"c": [1], "d": True}} + ).cont_all_true(assert_is_bool=True) error_raised = False except IvyException: error_raised = True @@ -103,10 +103,12 @@ def test_container_assert_contains(on_device): error_caught = True assert error_caught # sub-structure - sub_struc = Container({ - "c": ivy.array([3.0], device=on_device), - "d": ivy.array([4.0], device=on_device), - }) + sub_struc = Container( + { + "c": ivy.array([3.0], device=on_device), + "d": ivy.array([4.0], device=on_device), + } + ) try: not container.cont_assert_contains_sub_container(sub_struc) error_caught = False @@ -136,13 +138,15 @@ def test_container_assert_identical(on_device): arr3 = ivy.array([3], device=on_device) container0 = Container({"a": arr1, "b": {"c": arr2, "d": arr3}}) container1 = Container({"a": arr1, "b": {"c": arr2, "d": arr3}}) - container2 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) + container2 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) container3 = Container({"b": {"d": arr3}}) container4 = Container({"d": arr3}) @@ -177,36 +181,44 @@ def test_container_assert_identical(on_device): def test_container_assert_identical_structure(on_device): # without key_chains specification - container0 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) - container1 = Container({ - "a": ivy.array([3], device=on_device), - "b": { - "c": ivy.array([4], device=on_device), - "d": ivy.array([5], device=on_device), - }, - }) - container2 = Container({ - "a": ivy.array([3], device=on_device), - "b": { - "c": ivy.array([4], device=on_device), - "d": ivy.array([5], device=on_device), + container0 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) + container1 = Container( + { + "a": ivy.array([3], device=on_device), + "b": { + "c": ivy.array([4], device=on_device), + "d": ivy.array([5], device=on_device), + }, + } + ) + container2 = Container( + { + "a": ivy.array([3], device=on_device), + "b": { + "c": ivy.array([4], device=on_device), + "d": ivy.array([5], device=on_device), + "e": ivy.array([6], device=on_device), + }, + } + ) + container3 = Container( + { + "a": ivy.array([3], device=on_device), + "b": { + "c": ivy.array([4], device=on_device), + "d": ivy.array([5], device=on_device), + }, "e": ivy.array([6], device=on_device), - }, - }) - container3 = Container({ - "a": ivy.array([3], device=on_device), - "b": { - "c": ivy.array([4], device=on_device), - "d": ivy.array([5], device=on_device), - }, - "e": ivy.array([6], device=on_device), - }) + } + ) container4 = Container({"b": {"d": ivy.array([4], device=on_device)}}) container5 = Container({"d": ivy.array([4], device=on_device)}) @@ -217,9 +229,9 @@ def test_container_assert_identical_structure(on_device): # without identical try: - ivy.Container.cont_assert_identical_structure([ - container0, container1, container2, container3 - ]) + ivy.Container.cont_assert_identical_structure( + [container0, container1, container2, container3] + ) error_caught = False except IvyException: error_caught = True @@ -314,20 +326,24 @@ def test_container_at_keys(on_device): def test_container_combine(on_device): - container_0 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) - container_1 = Container({ - "a": ivy.array([4], device=on_device), - "b": { - "c": ivy.array([5], device=on_device), - "e": ivy.array([6], device=on_device), - }, - }) + container_0 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) + container_1 = Container( + { + "a": ivy.array([4], device=on_device), + "b": { + "c": ivy.array([5], device=on_device), + "e": ivy.array([6], device=on_device), + }, + } + ) container_comb = ivy.Container.cont_combine(container_0, container_1) assert np.equal(ivy.to_numpy(container_comb.a), np.array([4])) assert np.equal(ivy.to_numpy(container_comb.b.c), np.array([5])) @@ -375,21 +391,25 @@ def test_container_common_key_chains(on_device): def test_container_cont_inplace_update(on_device): - container0 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([1], device=on_device), - "d": ivy.array([2], device=on_device), - }, - }) + container0 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([1], device=on_device), + "d": ivy.array([2], device=on_device), + }, + } + ) id0 = id(container0) - container1 = Container({ - "a": ivy.array([0], device=on_device), - "b": { - "c": ivy.array([0], device=on_device), - "d": ivy.array([0], device=on_device), - }, - }) + container1 = Container( + { + "a": ivy.array([0], device=on_device), + "b": { + "c": ivy.array([0], device=on_device), + "d": ivy.array([0], device=on_device), + }, + } + ) id1 = id(container1) assert ivy.Container.cont_all_false(container0.all_equal(container1)) container0.inplace_update(container1) @@ -425,10 +445,12 @@ def test_container_contains(on_device): assert not partial_sub_cont.cont_contains_sub_container(container, partial=True) # sub-structure - sub_struc = Container({ - "c": ivy.array([3.0], device=on_device), - "d": ivy.array([4.0], device=on_device), - }) + sub_struc = Container( + { + "c": ivy.array([3.0], device=on_device), + "d": ivy.array([4.0], device=on_device), + } + ) assert not container.cont_contains_sub_container(sub_struc) assert sub_struc not in container assert container.cont_contains_sub_structure(sub_struc) @@ -585,30 +607,36 @@ def test_container_deep_copy(on_device): def test_container_depth(on_device): - cont_depth1 = Container({ - "a": ivy.array([1], device=on_device), "b": ivy.array([2], device=on_device) - }) + cont_depth1 = Container( + {"a": ivy.array([1], device=on_device), "b": ivy.array([2], device=on_device)} + ) assert cont_depth1.cont_max_depth == 1 - cont_depth2 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) + cont_depth2 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) assert cont_depth2.cont_max_depth == 2 - cont_depth3 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": {"d": ivy.array([2], device=on_device)}, - "e": ivy.array([3], device=on_device), - }, - }) + cont_depth3 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": {"d": ivy.array([2], device=on_device)}, + "e": ivy.array([3], device=on_device), + }, + } + ) assert cont_depth3.cont_max_depth == 3 - cont_depth4 = Container({ - "a": ivy.array([1], device=on_device), - "b": {"c": {"d": {"e": ivy.array([2], device=on_device)}}}, - }) + cont_depth4 = Container( + { + "a": ivy.array([1], device=on_device), + "b": {"c": {"d": {"e": ivy.array([2], device=on_device)}}}, + } + ) assert cont_depth4.cont_max_depth == 4 @@ -626,20 +654,24 @@ def test_container_dev_str(on_device): def test_container_diff(on_device): # all different arrays - container_0 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) - container_1 = Container({ - "a": ivy.array([4], device=on_device), - "b": { - "c": ivy.array([5], device=on_device), - "d": ivy.array([6], device=on_device), - }, - }) + container_0 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) + container_1 = Container( + { + "a": ivy.array([4], device=on_device), + "b": { + "c": ivy.array([5], device=on_device), + "d": ivy.array([6], device=on_device), + }, + } + ) container_diff = ivy.Container.cont_diff(container_0, container_1) assert np.equal(ivy.to_numpy(container_diff.a.diff_0), np.array([1])) assert np.equal(ivy.to_numpy(container_diff.a.diff_1), np.array([4])) @@ -657,20 +689,24 @@ def test_container_diff(on_device): assert container_diff_same_only.cont_to_dict() == {} # some different arrays - container_0 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) - container_1 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([5], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) + container_0 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) + container_1 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([5], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) container_diff = ivy.Container.cont_diff(container_0, container_1) assert np.equal(ivy.to_numpy(container_diff.a), np.array([1])) assert np.equal(ivy.to_numpy(container_diff.b.c.diff_0), np.array([2])) @@ -692,20 +728,24 @@ def test_container_diff(on_device): assert "d" in container_diff_same_only["b"] # all different keys - container_0 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) - container_1 = Container({ - "e": ivy.array([1], device=on_device), - "f": { - "g": ivy.array([2], device=on_device), - "h": ivy.array([3], device=on_device), - }, - }) + container_0 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) + container_1 = Container( + { + "e": ivy.array([1], device=on_device), + "f": { + "g": ivy.array([2], device=on_device), + "h": ivy.array([3], device=on_device), + }, + } + ) container_diff = ivy.Container.cont_diff(container_0, container_1) assert np.equal(ivy.to_numpy(container_diff.a.diff_0), np.array([1])) assert np.equal(ivy.to_numpy(container_diff.b.diff_0.c), np.array([2])) @@ -723,20 +763,24 @@ def test_container_diff(on_device): assert container_diff_same_only.cont_to_dict() == {} # some different keys - container_0 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) - container_1 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "e": ivy.array([3], device=on_device), - }, - }) + container_0 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) + container_1 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "e": ivy.array([3], device=on_device), + }, + } + ) container_diff = ivy.Container.cont_diff(container_0, container_1) assert np.equal(ivy.to_numpy(container_diff.a), np.array([1])) assert np.equal(ivy.to_numpy(container_diff.b.c), np.array([2])) @@ -760,20 +804,24 @@ def test_container_diff(on_device): assert "e" not in container_diff_same_only["b"] # same containers - container_0 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) - container_1 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) + container_0 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) + container_1 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) container_diff = ivy.Container.cont_diff(container_0, container_1) assert np.equal(ivy.to_numpy(container_diff.a), np.array([1])) assert np.equal(ivy.to_numpy(container_diff.b.c), np.array([2])) @@ -811,13 +859,15 @@ def test_container_duplicate_array_keychains(on_device): arr1 = ivy.array([1], device=on_device) arr2 = ivy.array([2], device=on_device) container0 = Container({"a": arr1, "b": {"c": arr1, "d": arr2}}) - container1 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([1], device=on_device), - "d": ivy.array([2], device=on_device), - }, - }) + container1 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([1], device=on_device), + "d": ivy.array([2], device=on_device), + }, + } + ) res = ivy.Container.cont_duplicate_array_keychains(container0) assert res == (("a", "b/c"),) res = ivy.Container.cont_duplicate_array_keychains(container1) @@ -861,9 +911,9 @@ def test_container_find_sub_structure(on_device): top_cont = Container(dict_in) # full - sub_cont = Container({ - "c": ivy.array([4], device=on_device), "d": ivy.array([5], device=on_device) - }) + sub_cont = Container( + {"c": ivy.array([4], device=on_device), "d": ivy.array([5], device=on_device)} + ) assert not top_cont.cont_find_sub_container(sub_cont) found_kc = top_cont.cont_find_sub_structure(sub_cont) assert found_kc == "b" @@ -880,13 +930,15 @@ def test_container_find_sub_structure(on_device): def test_container_flatten_key_chains(on_device): - container = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": {"d": ivy.array([2], device=on_device)}, - "e": {"f": {"g": ivy.array([3], device=on_device)}}, - }, - }) + container = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": {"d": ivy.array([2], device=on_device)}, + "e": {"f": {"g": ivy.array([3], device=on_device)}}, + }, + } + ) # full container_flat = container.cont_flatten_key_chains() @@ -970,10 +1022,12 @@ def test_container_from_dict_w_cont_types(on_device): dict_in = { "a": ivy.array([1], device=on_device), - "b": FlatMapping({ - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }), + "b": FlatMapping( + { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + } + ), } container = Container(dict_in) assert np.allclose(ivy.to_numpy(container["a"]), np.array([1])) @@ -1053,12 +1107,15 @@ def worker_fn(in_queue, out_queue, load_size, worker_id): keep_going = in_queue.get(timeout=0.1) except queue.Empty: continue - out_queue.put({ - "a": [ - ivy.to_native(ivy.array([1.0, 2.0, 3.0], device=on_device)) - * worker_id - ] * load_size - }) + out_queue.put( + { + "a": [ + ivy.to_native(ivy.array([1.0, 2.0, 3.0], device=on_device)) + * worker_id + ] + * load_size + } + ) workers = [] in_queues = [] @@ -1190,13 +1247,15 @@ def test_container_identical(on_device): arr3 = ivy.array([3], device=on_device) container0 = Container({"a": arr1, "b": {"c": arr2, "d": arr3}}) container1 = Container({"a": arr1, "b": {"c": arr2, "d": arr3}}) - container2 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) + container2 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) container3 = Container({"b": {"d": arr3}}) container4 = Container({"d": arr3}) @@ -1219,34 +1278,40 @@ def test_container_identical(on_device): def test_container_identical_array_shapes(on_device): # without key_chains specification - container0 = Container({ - "a": ivy.array([1, 2], device=on_device), - "b": { - "c": ivy.array([2, 3, 4], device=on_device), - "d": ivy.array([3, 4, 5, 6], device=on_device), - }, - }) - container1 = Container({ - "a": ivy.array([1, 2, 3, 4], device=on_device), - "b": { - "c": ivy.array([3, 4], device=on_device), - "d": ivy.array([3, 4, 5], device=on_device), - }, - }) - container2 = Container({ - "a": ivy.array([1, 2, 3, 4], device=on_device), - "b": { - "c": ivy.array([3, 4], device=on_device), - "d": ivy.array([3, 4, 5, 6], device=on_device), - }, - }) + container0 = Container( + { + "a": ivy.array([1, 2], device=on_device), + "b": { + "c": ivy.array([2, 3, 4], device=on_device), + "d": ivy.array([3, 4, 5, 6], device=on_device), + }, + } + ) + container1 = Container( + { + "a": ivy.array([1, 2, 3, 4], device=on_device), + "b": { + "c": ivy.array([3, 4], device=on_device), + "d": ivy.array([3, 4, 5], device=on_device), + }, + } + ) + container2 = Container( + { + "a": ivy.array([1, 2, 3, 4], device=on_device), + "b": { + "c": ivy.array([3, 4], device=on_device), + "d": ivy.array([3, 4, 5, 6], device=on_device), + }, + } + ) # with identical assert ivy.Container.cont_identical_array_shapes([container0, container1]) assert ivy.Container.cont_identical_array_shapes([container1, container0]) - assert ivy.Container.cont_identical_array_shapes([ - container1, container0, container1 - ]) + assert ivy.Container.cont_identical_array_shapes( + [container1, container0, container1] + ) assert not ivy.Container.cont_identical([container0, container2]) assert not ivy.Container.cont_identical([container1, container2]) assert not ivy.Container.cont_identical([container0, container1, container2]) @@ -1264,43 +1329,51 @@ def test_container_identical_configs(on_device): # without identical assert not ivy.Container.cont_identical_configs([container1, container2]) - assert not ivy.Container.cont_identical_configs([ - container1, container0, container2 - ]) + assert not ivy.Container.cont_identical_configs( + [container1, container0, container2] + ) def test_container_identical_structure(on_device): # without key_chains specification - container0 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) - container1 = Container({ - "a": ivy.array([3], device=on_device), - "b": { - "c": ivy.array([4], device=on_device), - "d": ivy.array([5], device=on_device), - }, - }) - container2 = Container({ - "a": ivy.array([3], device=on_device), - "b": { - "c": ivy.array([4], device=on_device), - "d": ivy.array([5], device=on_device), + container0 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) + container1 = Container( + { + "a": ivy.array([3], device=on_device), + "b": { + "c": ivy.array([4], device=on_device), + "d": ivy.array([5], device=on_device), + }, + } + ) + container2 = Container( + { + "a": ivy.array([3], device=on_device), + "b": { + "c": ivy.array([4], device=on_device), + "d": ivy.array([5], device=on_device), + "e": ivy.array([6], device=on_device), + }, + } + ) + container3 = Container( + { + "a": ivy.array([3], device=on_device), + "b": { + "c": ivy.array([4], device=on_device), + "d": ivy.array([5], device=on_device), + }, "e": ivy.array([6], device=on_device), - }, - }) - container3 = Container({ - "a": ivy.array([3], device=on_device), - "b": { - "c": ivy.array([4], device=on_device), - "d": ivy.array([5], device=on_device), - }, - "e": ivy.array([6], device=on_device), - }) + } + ) container4 = Container({"b": {"d": ivy.array([4], device=on_device)}}) container5 = Container({"d": ivy.array([4], device=on_device)}) @@ -1313,9 +1386,9 @@ def test_container_identical_structure(on_device): assert not ivy.Container.cont_identical_structure([container2, container3]) assert not ivy.Container.cont_identical_structure([container0, container3]) assert not ivy.Container.cont_identical_structure([container1, container2]) - assert not ivy.Container.cont_identical_structure([ - container1, container0, container2 - ]) + assert not ivy.Container.cont_identical_structure( + [container1, container0, container2] + ) # partial assert ivy.Container.cont_identical_structure( @@ -1376,22 +1449,26 @@ def test_container_if_exists(on_device): def test_container_inplace(on_device): - container0 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([1], device=on_device), - "d": ivy.array([2], device=on_device), - }, - }) + container0 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([1], device=on_device), + "d": ivy.array([2], device=on_device), + }, + } + ) const = 3 arr = ivy.array([1], device=on_device) - container1 = Container({ - "a": ivy.array([3], device=on_device), - "b": { - "c": ivy.array([4], device=on_device), - "d": ivy.array([5], device=on_device), - }, - }) + container1 = Container( + { + "a": ivy.array([3], device=on_device), + "b": { + "c": ivy.array([4], device=on_device), + "d": ivy.array([5], device=on_device), + }, + } + ) special_funcs = [ "__add__", @@ -1443,20 +1520,24 @@ def test_container_key_chains_containing(include_empty, on_device): def test_container_list_join(on_device): - container_0 = Container({ - "a": [ivy.array([1], device=on_device)], - "b": { - "c": [ivy.array([2], device=on_device)], - "d": [ivy.array([3], device=on_device)], - }, - }) - container_1 = Container({ - "a": [ivy.array([4], device=on_device)], - "b": { - "c": [ivy.array([5], device=on_device)], - "d": [ivy.array([6], device=on_device)], - }, - }) + container_0 = Container( + { + "a": [ivy.array([1], device=on_device)], + "b": { + "c": [ivy.array([2], device=on_device)], + "d": [ivy.array([3], device=on_device)], + }, + } + ) + container_1 = Container( + { + "a": [ivy.array([4], device=on_device)], + "b": { + "c": [ivy.array([5], device=on_device)], + "d": [ivy.array([6], device=on_device)], + }, + } + ) container_list_joined = ivy.Container.cont_list_join([container_0, container_1]) assert np.allclose(ivy.to_numpy(container_list_joined["a"][0]), np.array([1])) assert np.allclose(ivy.to_numpy(container_list_joined.a[0]), np.array([1])) @@ -1473,20 +1554,24 @@ def test_container_list_join(on_device): def test_container_list_stack(on_device): - container_0 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) - container_1 = Container({ - "a": ivy.array([4], device=on_device), - "b": { - "c": ivy.array([5], device=on_device), - "d": ivy.array([6], device=on_device), - }, - }) + container_0 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) + container_1 = Container( + { + "a": ivy.array([4], device=on_device), + "b": { + "c": ivy.array([5], device=on_device), + "d": ivy.array([6], device=on_device), + }, + } + ) container_list_stacked = ivy.Container.cont_list_stack( [container_0, container_1], 0 ) @@ -1595,10 +1680,12 @@ def test_container_map(inplace, on_device): assert "b/d" not in container_mapped # with sequences - container_orig = Container({ - "a": ivy.array([1], device=on_device), - "b": [ivy.array([2], device=on_device), ivy.array([3], device=on_device)], - }) + container_orig = Container( + { + "a": ivy.array([1], device=on_device), + "b": [ivy.array([2], device=on_device), ivy.array([3], device=on_device)], + } + ) container = container_orig.cont_deep_copy() container_mapped = container.cont_map( lambda x, _: x + 1, inplace=inplace, map_sequences=True @@ -1613,13 +1700,15 @@ def test_container_map(inplace, on_device): @pytest.mark.parametrize("inplace", [True, False]) def test_container_map_sub_conts(inplace, on_device): # without key_chains specification - container_orig = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) + container_orig = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) def _add_e_attr(cont_in): cont_in.e = ivy.array([4], device=on_device) @@ -1651,20 +1740,24 @@ def _add_e_attr(cont_in): def test_container_multi_map(on_device): # without key_chains specification - container0 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) - container1 = Container({ - "a": ivy.array([3], device=on_device), - "b": { - "c": ivy.array([4], device=on_device), - "d": ivy.array([5], device=on_device), - }, - }) + container0 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) + container1 = Container( + { + "a": ivy.array([3], device=on_device), + "b": { + "c": ivy.array([4], device=on_device), + "d": ivy.array([5], device=on_device), + }, + } + ) # with key_chains to apply container_mapped = ivy.Container.cont_multi_map( @@ -1678,20 +1771,24 @@ def test_container_multi_map(on_device): assert np.allclose(ivy.to_numpy(container_mapped.b.d), np.array([[8]])) # with sequences - container0 = Container({ - "a": ivy.array([1], device=on_device), - "b": [ - ivy.array([2], device=on_device), - ivy.array([3], device=on_device), - ], - }) - container1 = Container({ - "a": ivy.array([3], device=on_device), - "b": [ - ivy.array([4], device=on_device), - ivy.array([5], device=on_device), - ], - }) + container0 = Container( + { + "a": ivy.array([1], device=on_device), + "b": [ + ivy.array([2], device=on_device), + ivy.array([3], device=on_device), + ], + } + ) + container1 = Container( + { + "a": ivy.array([3], device=on_device), + "b": [ + ivy.array([4], device=on_device), + ivy.array([5], device=on_device), + ], + } + ) container_mapped = ivy.Container.cont_multi_map( lambda x, _: x[0] + x[1], @@ -1769,17 +1866,21 @@ def test_container_overwrite_at_key_chain(on_device): def test_container_overwrite_at_key_chains(on_device): - container = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) - target_container = Container({ - "a": ivy.array([4], device=on_device), - "b": {"d": ivy.array([5], device=on_device)}, - }) + container = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) + target_container = Container( + { + "a": ivy.array([4], device=on_device), + "b": {"d": ivy.array([5], device=on_device)}, + } + ) new_container = container.cont_overwrite_at_key_chains( target_container, inplace=False ) @@ -1949,17 +2050,19 @@ def _test_bc_exception(container_in): def test_container_prune_key_from_key_chains(on_device): - container = Container({ - "Ayy": ivy.array([1], device=on_device), - "Bee": { - "Cee": ivy.array([2], device=on_device), - "Dee": ivy.array([3], device=on_device), - }, - "Beh": { - "Ceh": ivy.array([4], device=on_device), - "Deh": ivy.array([5], device=on_device), - }, - }) + container = Container( + { + "Ayy": ivy.array([1], device=on_device), + "Bee": { + "Cee": ivy.array([2], device=on_device), + "Dee": ivy.array([3], device=on_device), + }, + "Beh": { + "Ceh": ivy.array([4], device=on_device), + "Deh": ivy.array([5], device=on_device), + }, + } + ) # absolute container_pruned = container.cont_prune_key_from_key_chains("Bee") @@ -2036,14 +2139,16 @@ def _test_bd_exception(container_in): def test_container_prune_keys_from_key_chains(on_device): - container = Container({ - "Ayy": ivy.array([1], device=on_device), - "Bee": { - "Cee": ivy.array([2], device=on_device), - "Dee": ivy.array([3], device=on_device), - }, - "Eee": {"Fff": ivy.array([4], device=on_device)}, - }) + container = Container( + { + "Ayy": ivy.array([1], device=on_device), + "Bee": { + "Cee": ivy.array([2], device=on_device), + "Dee": ivy.array([3], device=on_device), + }, + "Eee": {"Fff": ivy.array([4], device=on_device)}, + } + ) # absolute container_pruned = container.cont_prune_keys_from_key_chains(["Bee", "Eee"]) @@ -2073,20 +2178,24 @@ def test_container_prune_keys_from_key_chains(on_device): def test_container_reduce(on_device): - container_a = ivy.Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) - container_b = ivy.Container({ - "a": ivy.array([2], device=on_device), - "b": { - "c": ivy.array([4], device=on_device), - "d": ivy.array([6], device=on_device), - }, - }) + container_a = ivy.Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) + container_b = ivy.Container( + { + "a": ivy.array([2], device=on_device), + "b": { + "c": ivy.array([4], device=on_device), + "d": ivy.array([6], device=on_device), + }, + } + ) res = ivy.Container.cont_reduce([container_a, container_b], lambda x: x[0] + x[1]) assert np.allclose(ivy.to_numpy(res.a), np.array([3.0])) assert np.allclose(ivy.to_numpy(res.b.c), np.array([6])) @@ -2094,13 +2203,15 @@ def test_container_reduce(on_device): def test_container_remove_key_length_limit(on_device): - cont = Container({ - "a": ivy.array([0.0], device=on_device), - "b": { - "c": ivy.array([1.0], device=on_device), - "d": ivy.array([2.0], device=on_device), - }, - }) + cont = Container( + { + "a": ivy.array([0.0], device=on_device), + "b": { + "c": ivy.array([1.0], device=on_device), + "d": ivy.array([2.0], device=on_device), + }, + } + ) cont.cont_with_key_length_limit(5, inplace=True) default_key_length_limit = cont._key_length_limit id_cont = id(cont) @@ -2117,13 +2228,15 @@ def test_container_remove_key_length_limit(on_device): def test_container_remove_print_limit(on_device): - cont = Container({ - "a": ivy.array([0.0], device=on_device), - "b": { - "c": ivy.array([1.0], device=on_device), - "d": ivy.array([2.0], device=on_device), - }, - }) + cont = Container( + { + "a": ivy.array([0.0], device=on_device), + "b": { + "c": ivy.array([1.0], device=on_device), + "d": ivy.array([2.0], device=on_device), + }, + } + ) default_print_limit = cont._print_limit id_cont = id(cont) cont1 = cont.cont_remove_print_limit() @@ -2139,13 +2252,15 @@ def test_container_remove_print_limit(on_device): def test_container_reshape_like(on_device): - container = Container({ - "a": ivy.array([[1.0]], device=on_device), - "b": { - "c": ivy.array([[3.0], [4.0]], device=on_device), - "d": ivy.array([[5.0], [6.0], [7.0]], device=on_device), - }, - }) + container = Container( + { + "a": ivy.array([[1.0]], device=on_device), + "b": { + "c": ivy.array([[3.0], [4.0]], device=on_device), + "d": ivy.array([[5.0], [6.0], [7.0]], device=on_device), + }, + } + ) new_shapes = Container({"a": (1,), "b": {"c": (1, 2, 1), "d": (3, 1, 1)}}) # without leading shape @@ -2158,22 +2273,24 @@ def test_container_reshape_like(on_device): assert list(container_reshaped.b.d.shape) == [3, 1, 1] # with leading shape - container = Container({ - "a": ivy.array([[[1.0]], [[1.0]], [[1.0]]], device=on_device), - "b": { - "c": ivy.array( - [[[3.0], [4.0]], [[3.0], [4.0]], [[3.0], [4.0]]], device=on_device - ), - "d": ivy.array( - [ - [[5.0], [6.0], [7.0]], - [[5.0], [6.0], [7.0]], - [[5.0], [6.0], [7.0]], - ], - device=on_device, - ), - }, - }) + container = Container( + { + "a": ivy.array([[[1.0]], [[1.0]], [[1.0]]], device=on_device), + "b": { + "c": ivy.array( + [[[3.0], [4.0]], [[3.0], [4.0]], [[3.0], [4.0]]], device=on_device + ), + "d": ivy.array( + [ + [[5.0], [6.0], [7.0]], + [[5.0], [6.0], [7.0]], + [[5.0], [6.0], [7.0]], + ], + device=on_device, + ), + }, + } + ) container_reshaped = container.cont_reshape_like(new_shapes, leading_shape=[3]) assert list(container_reshaped["a"].shape) == [3, 1] assert list(container_reshaped.a.shape) == [3, 1] @@ -2184,13 +2301,15 @@ def test_container_reshape_like(on_device): def test_container_restructure(on_device): - container = Container({ - "a": ivy.array([[1, 2], [3, 4]], device=on_device), - "b": { - "c": ivy.array([[2, 4], [6, 8]], device=on_device), - "d": ivy.array([3, 6, 9, 12], device=on_device), - }, - }) + container = Container( + { + "a": ivy.array([[1, 2], [3, 4]], device=on_device), + "b": { + "c": ivy.array([[2, 4], [6, 8]], device=on_device), + "d": ivy.array([3, 6, 9, 12], device=on_device), + }, + } + ) container_restructured = container.cont_restructure( { "a": {"key_chain": "A", "pattern": "a b -> b a"}, @@ -2223,13 +2342,15 @@ def test_container_restructure(on_device): def test_container_restructure_key_chains(on_device): # single - container = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) + container = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) container_restructured = container.cont_restructure_key_chains({"a": "A"}) assert np.allclose(ivy.to_numpy(container_restructured["A"]), np.array([[1]])) assert np.allclose(ivy.to_numpy(container_restructured.A), np.array([[1]])) @@ -2239,16 +2360,18 @@ def test_container_restructure_key_chains(on_device): assert np.allclose(ivy.to_numpy(container_restructured.b.d), np.array([[3]])) # full - container = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) - container_restructured = container.cont_restructure_key_chains({ - "a": "A", "b/c": "B/C", "b/d": "B/D" - }) + container = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) + container_restructured = container.cont_restructure_key_chains( + {"a": "A", "b/c": "B/C", "b/d": "B/D"} + ) assert np.allclose(ivy.to_numpy(container_restructured["A"]), np.array([[1]])) assert np.allclose(ivy.to_numpy(container_restructured.A), np.array([[1]])) assert np.allclose(ivy.to_numpy(container_restructured["B/C"]), np.array([[2]])) @@ -2300,17 +2423,21 @@ def test_container_set_at_key_chain(on_device): def test_container_set_at_key_chains(on_device): - container = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) - target_container = Container({ - "a": ivy.array([4], device=on_device), - "b": {"d": ivy.array([5], device=on_device)}, - }) + container = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) + target_container = Container( + { + "a": ivy.array([4], device=on_device), + "b": {"d": ivy.array([5], device=on_device)}, + } + ) new_container = container.cont_set_at_key_chains(target_container, inplace=False) assert np.allclose(ivy.to_numpy(new_container["a"]), np.array([4])) assert np.allclose(ivy.to_numpy(new_container["b"]["c"]), np.array([2])) @@ -2340,9 +2467,9 @@ def test_container_set_at_keys(on_device): assert np.allclose(ivy.to_numpy(container["b"]), np.array([4])) assert not container.cont_has_key("c") # noqa assert not container.cont_has_key("d") # noqa - container = orig_container.cont_set_at_keys({ - "a": ivy.array([5], device=on_device), "c": ivy.array([6], device=on_device) - }) + container = orig_container.cont_set_at_keys( + {"a": ivy.array([5], device=on_device), "c": ivy.array([6], device=on_device)} + ) assert np.allclose(ivy.to_numpy(container["a"]), np.array([5])) assert np.allclose(ivy.to_numpy(container["b"]["c"]), np.array([6])) assert np.allclose(ivy.to_numpy(container["b"]["d"]), np.array([3])) @@ -2467,9 +2594,9 @@ def test_container_slice_keys(str_slice, on_device): # with dict, depth 0 sub_cont = Container({"a": a_val, "b": b_val, "c": c_val, "d": d_val, "e": e_val}) - cont = Container({ - "a": sub_cont, "b": sub_cont, "c": sub_cont, "d": sub_cont, "e": sub_cont - }) + cont = Container( + {"a": sub_cont, "b": sub_cont, "c": sub_cont, "d": sub_cont, "e": sub_cont} + ) cont_sliced = cont.cont_slice_keys({0: slc}) assert "a" not in cont_sliced assert Container.cont_identical([cont_sliced.b, sub_cont]) @@ -2480,9 +2607,9 @@ def test_container_slice_keys(str_slice, on_device): # with dict, depth 1 sub_cont = Container({"a": a_val, "b": b_val, "c": c_val, "d": d_val, "e": e_val}) sub_sub_cont = Container({"b": b_val, "c": c_val, "d": d_val}) - cont = Container({ - "a": sub_cont, "b": sub_cont, "c": sub_cont, "d": sub_cont, "e": sub_cont - }) + cont = Container( + {"a": sub_cont, "b": sub_cont, "c": sub_cont, "d": sub_cont, "e": sub_cont} + ) cont_sliced = cont.cont_slice_keys({1: slc}) assert Container.cont_identical([cont_sliced.a, sub_sub_cont]) assert Container.cont_identical([cont_sliced.b, sub_sub_cont]) @@ -2493,9 +2620,9 @@ def test_container_slice_keys(str_slice, on_device): # with dict, depth 0, 1 sub_cont = Container({"a": a_val, "b": b_val, "c": c_val, "d": d_val, "e": e_val}) sub_sub_cont = Container({"b": b_val, "c": c_val, "d": d_val}) - cont = Container({ - "a": sub_cont, "b": sub_cont, "c": sub_cont, "d": sub_cont, "e": sub_cont - }) + cont = Container( + {"a": sub_cont, "b": sub_cont, "c": sub_cont, "d": sub_cont, "e": sub_cont} + ) cont_sliced = cont.cont_slice_keys({0: slc, 1: slc}) assert "a" not in cont_sliced assert Container.cont_identical([cont_sliced.b, sub_sub_cont]) @@ -2506,9 +2633,9 @@ def test_container_slice_keys(str_slice, on_device): # all depths sub_cont = Container({"a": a_val, "b": b_val, "c": c_val, "d": d_val, "e": e_val}) sub_sub_cont = Container({"b": b_val, "c": c_val, "d": d_val}) - cont = Container({ - "a": sub_cont, "b": sub_cont, "c": sub_cont, "d": sub_cont, "e": sub_cont - }) + cont = Container( + {"a": sub_cont, "b": sub_cont, "c": sub_cont, "d": sub_cont, "e": sub_cont} + ) cont_sliced = cont.cont_slice_keys(slc, all_depths=True) assert "a" not in cont_sliced assert Container.cont_identical([cont_sliced.b, sub_sub_cont]) @@ -2590,20 +2717,24 @@ def test_container_split_conts(on_device): def test_container_structural_diff(on_device): # all different keys or shapes - container_0 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) - container_1 = Container({ - "a": ivy.array([[4]], device=on_device), - "b": { - "c": ivy.array([[[5]]], device=on_device), - "e": ivy.array([3], device=on_device), - }, - }) + container_0 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) + container_1 = Container( + { + "a": ivy.array([[4]], device=on_device), + "b": { + "c": ivy.array([[[5]]], device=on_device), + "e": ivy.array([3], device=on_device), + }, + } + ) container_diff = ivy.Container.cont_structural_diff(container_0, container_1) assert np.equal(ivy.to_numpy(container_diff.a.diff_0), np.array([1])) assert np.equal(ivy.to_numpy(container_diff.a.diff_1), np.array([[4]])) @@ -2621,20 +2752,24 @@ def test_container_structural_diff(on_device): assert container_diff_same_only.cont_to_dict() == {} # some different shapes - container_0 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) - container_1 = Container({ - "a": ivy.array([4], device=on_device), - "b": { - "c": ivy.array([[5]], device=on_device), - "d": ivy.array([6], device=on_device), - }, - }) + container_0 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) + container_1 = Container( + { + "a": ivy.array([4], device=on_device), + "b": { + "c": ivy.array([[5]], device=on_device), + "d": ivy.array([6], device=on_device), + }, + } + ) container_diff = ivy.Container.cont_structural_diff(container_0, container_1) assert np.equal(ivy.to_numpy(container_diff.a), np.array([1])) assert np.equal(ivy.to_numpy(container_diff.b.c.diff_0), np.array([2])) @@ -2656,20 +2791,24 @@ def test_container_structural_diff(on_device): assert "d" in container_diff_same_only["b"] # all different keys - container_0 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) - container_1 = Container({ - "e": ivy.array([4], device=on_device), - "f": { - "g": ivy.array([5], device=on_device), - "h": ivy.array([6], device=on_device), - }, - }) + container_0 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) + container_1 = Container( + { + "e": ivy.array([4], device=on_device), + "f": { + "g": ivy.array([5], device=on_device), + "h": ivy.array([6], device=on_device), + }, + } + ) container_diff = ivy.Container.cont_structural_diff(container_0, container_1) assert np.equal(ivy.to_numpy(container_diff.a.diff_0), np.array([1])) assert np.equal(ivy.to_numpy(container_diff.b.diff_0.c), np.array([2])) @@ -2687,20 +2826,24 @@ def test_container_structural_diff(on_device): assert container_diff_same_only.cont_to_dict() == {} # some different keys - container_0 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) - container_1 = Container({ - "a": ivy.array([4], device=on_device), - "b": { - "c": ivy.array([5], device=on_device), - "e": ivy.array([6], device=on_device), - }, - }) + container_0 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) + container_1 = Container( + { + "a": ivy.array([4], device=on_device), + "b": { + "c": ivy.array([5], device=on_device), + "e": ivy.array([6], device=on_device), + }, + } + ) container_diff = ivy.Container.cont_structural_diff(container_0, container_1) assert np.equal(ivy.to_numpy(container_diff.a), np.array([1])) assert np.equal(ivy.to_numpy(container_diff.b.c), np.array([2])) @@ -2724,20 +2867,24 @@ def test_container_structural_diff(on_device): assert "e" not in container_diff_same_only["b"] # all same - container_0 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([2], device=on_device), - "d": ivy.array([3], device=on_device), - }, - }) - container_1 = Container({ - "a": ivy.array([4], device=on_device), - "b": { - "c": ivy.array([5], device=on_device), - "d": ivy.array([6], device=on_device), - }, - }) + container_0 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([2], device=on_device), + "d": ivy.array([3], device=on_device), + }, + } + ) + container_1 = Container( + { + "a": ivy.array([4], device=on_device), + "b": { + "c": ivy.array([5], device=on_device), + "d": ivy.array([6], device=on_device), + }, + } + ) container_diff = ivy.Container.cont_structural_diff(container_0, container_1) assert np.equal(ivy.to_numpy(container_diff.a), np.array([1])) assert np.equal(ivy.to_numpy(container_diff.b.c), np.array([2])) @@ -2879,16 +3026,18 @@ def test_container_to_and_from_disk_as_pickled(on_device): def test_container_to_dict(on_device): - container0 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([True], device=on_device), - "d": { - "g": ivy.array([2.0], device=on_device), - "h": ivy.array([3], device=on_device), + container0 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([True], device=on_device), + "d": { + "g": ivy.array([2.0], device=on_device), + "h": ivy.array([3], device=on_device), + }, }, - }, - }) + } + ) res = ivy.Container.cont_to_dict(container0) assert res == {"a": 1, "b": {"c": True, "d": {"g": 2.0, "h": 3}}} @@ -3020,16 +3169,18 @@ def test_container_to_iterator_values(include_empty, on_device): def test_container_to_nested_list(on_device): - container0 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([True], device=on_device), - "d": { - "g": ivy.array([2.0], device=on_device), - "h": ivy.array([3], device=on_device), + container0 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([True], device=on_device), + "d": { + "g": ivy.array([2.0], device=on_device), + "h": ivy.array([3], device=on_device), + }, }, - }, - }) + } + ) res = ivy.Container.cont_to_nested_list(container0) assert res == [1, [True, [2.0, 3]]] @@ -3054,13 +3205,15 @@ def test_container_trim_key(on_device): def test_container_try_kc(on_device): - cont = Container({ - "a": ivy.array([0.0], device=on_device), - "b": { - "c": ivy.array([1.0], device=on_device), - "d": ivy.array([2.0], device=on_device), - }, - }) + cont = Container( + { + "a": ivy.array([0.0], device=on_device), + "b": { + "c": ivy.array([1.0], device=on_device), + "d": ivy.array([2.0], device=on_device), + }, + } + ) assert cont.cont_try_kc("a") == cont.a assert cont.cont_try_kc("b/c") == cont.b.c assert cont.cont_try_kc("b/d") == cont.b.d @@ -3073,21 +3226,25 @@ def test_container_unify(on_device): dev0 = on_device on_devices.append(dev0) conts = {} - conts[dev0] = Container({ - "a": ivy.array([1], device=dev0), - "b": {"c": ivy.array([2], device=dev0), "d": ivy.array([3], device=dev0)}, - }) + conts[dev0] = Container( + { + "a": ivy.array([1], device=dev0), + "b": {"c": ivy.array([2], device=dev0), "d": ivy.array([3], device=dev0)}, + } + ) if "gpu" in on_device and ivy.num_gpus() > 1: idx = ivy.num_gpus() - 1 dev1 = on_device[:-1] + str(idx) on_devices.append(dev1) - conts[dev1] = Container({ - "a": ivy.array([4], device=dev1), - "b": { - "c": ivy.array([5], device=dev1), - "d": ivy.array([6], device=dev1), - }, - }) + conts[dev1] = Container( + { + "a": ivy.array([4], device=dev1), + "b": { + "c": ivy.array([5], device=dev1), + "d": ivy.array([6], device=dev1), + }, + } + ) # test container_unified = ivy.Container.cont_unify(conts, dev0, "concat", 0) @@ -3122,13 +3279,15 @@ def test_container_unstack_conts(on_device): def test_container_with_default_key_color(on_device): - cont = Container({ - "a": ivy.array([0.0], device=on_device), - "b": { - "c": ivy.array([1.0], device=on_device), - "d": ivy.array([2.0], device=on_device), - }, - }) + cont = Container( + { + "a": ivy.array([0.0], device=on_device), + "b": { + "c": ivy.array([1.0], device=on_device), + "d": ivy.array([2.0], device=on_device), + }, + } + ) default_default_key_color = cont._default_key_color id_cont = id(cont) cont1 = cont.cont_with_default_key_color("red") @@ -3160,13 +3319,15 @@ def test_container_with_entries_as_lists(on_device): def test_container_with_ivy_backend(on_device): - container0 = Container({ - "a": ivy.array([1], device=on_device), - "b": { - "c": ivy.array([1], device=on_device), - "d": ivy.array([2], device=on_device), - }, - }) + container0 = Container( + { + "a": ivy.array([1], device=on_device), + "b": { + "c": ivy.array([1], device=on_device), + "d": ivy.array([2], device=on_device), + }, + } + ) id_container0 = id(container0) container0 = ivy.Container.cont_with_ivy_backend(container0, "numpy") assert container0.cont_config["ivyh"] == "numpy" @@ -3178,13 +3339,15 @@ def test_container_with_ivy_backend(on_device): def test_container_with_key_length_limit(on_device): - cont = Container({ - "a": ivy.array([0.0], device=on_device), - "b": { - "c": ivy.array([1.0], device=on_device), - "d": ivy.array([2.0], device=on_device), - }, - }) + cont = Container( + { + "a": ivy.array([0.0], device=on_device), + "b": { + "c": ivy.array([1.0], device=on_device), + "d": ivy.array([2.0], device=on_device), + }, + } + ) default_key_length_limit = cont._key_length_limit id_cont = id(cont) cont1 = cont.cont_with_key_length_limit(5) @@ -3200,13 +3363,15 @@ def test_container_with_key_length_limit(on_device): def test_container_with_print_indent(on_device): - cont = Container({ - "a": ivy.array([0.0], device=on_device), - "b": { - "c": ivy.array([1.0], device=on_device), - "d": ivy.array([2.0], device=on_device), - }, - }) + cont = Container( + { + "a": ivy.array([0.0], device=on_device), + "b": { + "c": ivy.array([1.0], device=on_device), + "d": ivy.array([2.0], device=on_device), + }, + } + ) default_print_indent = cont._print_indent id_cont = id(cont) cont1 = cont.cont_with_print_indent(default_print_indent + 5) @@ -3222,13 +3387,15 @@ def test_container_with_print_indent(on_device): def test_container_with_print_limit(on_device): - cont = Container({ - "a": ivy.array([0.0], device=on_device), - "b": { - "c": ivy.array([1.0], device=on_device), - "d": ivy.array([2.0], device=on_device), - }, - }) + cont = Container( + { + "a": ivy.array([0.0], device=on_device), + "b": { + "c": ivy.array([1.0], device=on_device), + "d": ivy.array([2.0], device=on_device), + }, + } + ) default_print_limit = cont._print_limit id_cont = id(cont) cont1 = cont.cont_with_print_limit(default_print_limit + 5) @@ -3243,13 +3410,15 @@ def test_container_with_print_limit(on_device): def test_container_with_print_line_spacing(on_device): - cont = Container({ - "a": ivy.array([0.0], device=on_device), - "b": { - "c": ivy.array([1.0], device=on_device), - "d": ivy.array([2.0], device=on_device), - }, - }) + cont = Container( + { + "a": ivy.array([0.0], device=on_device), + "b": { + "c": ivy.array([1.0], device=on_device), + "d": ivy.array([2.0], device=on_device), + }, + } + ) default_print_line_spacing = cont._print_line_spacing id_cont = id(cont) cont1 = cont.cont_with_print_line_spacing(default_print_line_spacing + 5) diff --git a/ivy_tests/test_ivy/test_misc/test_factorized_tensor/test_tucker_tensor.py b/ivy_tests/test_ivy/test_misc/test_factorized_tensor/test_tucker_tensor.py index 1e2efc8fdb98c..bb03df0116781 100644 --- a/ivy_tests/test_ivy/test_misc/test_factorized_tensor/test_tucker_tensor.py +++ b/ivy_tests/test_ivy/test_misc/test_factorized_tensor/test_tucker_tensor.py @@ -7,9 +7,9 @@ @pytest.mark.parametrize(("shape", "rank"), [((5, 4, 6), (3, 2, 3))]) def test_n_param_tucker(shape, rank): tucker_tensor = ivy.random_tucker(shape, rank) - true_n_param = ivy.prod(ivy.shape(tucker_tensor[0])) + ivy.sum([ - ivy.prod(ivy.shape(f)) for f in tucker_tensor[1] - ]) + true_n_param = ivy.prod(ivy.shape(tucker_tensor[0])) + ivy.sum( + [ivy.prod(ivy.shape(f)) for f in tucker_tensor[1]] + ) n_param = tucker_tensor.n_param assert np.allclose(n_param, true_n_param) diff --git a/ivy_tests/test_ivy/test_misc/test_handle_exceptions.py b/ivy_tests/test_ivy/test_misc/test_handle_exceptions.py index b13bf4d6915b1..2c379e20e8b51 100644 --- a/ivy_tests/test_ivy/test_misc/test_handle_exceptions.py +++ b/ivy_tests/test_ivy/test_misc/test_handle_exceptions.py @@ -67,15 +67,17 @@ def test_non_ivy_errors_mapping(e, to_be_raised): @given( - e=st.sampled_from([ - Exception, - ZeroDivisionError, - BufferError, - AssertionError, - ImportError, - KeyError, - LookupError, - ]) + e=st.sampled_from( + [ + Exception, + ZeroDivisionError, + BufferError, + AssertionError, + ImportError, + KeyError, + LookupError, + ] + ) ) def test_non_ivy_errors_raising(e): with pytest.raises(IvyBackendException): diff --git a/ivy_tests/test_ivy/test_stateful/test_layers.py b/ivy_tests/test_ivy/test_stateful/test_layers.py index b421fbf483eab..40d1425775ed7 100644 --- a/ivy_tests/test_ivy/test_stateful/test_layers.py +++ b/ivy_tests/test_ivy/test_stateful/test_layers.py @@ -23,6 +23,9 @@ all_constant_initializers = (ivy.Zeros, ivy.Ones) all_gaussian_initializers = (ivy.KaimingNormal, ivy.Siren) all_uniform_initializers = (ivy.GlorotUniform, ivy.FirstLayerSiren, ivy.Siren) +all_initializers = ( + all_constant_initializers + all_uniform_initializers + all_gaussian_initializers +) # --- Helpers --- # @@ -1555,18 +1558,20 @@ def test_multi_head_attention_layer( # # Sequential # @handle_method( method_tree="Sequential.__call__", - bs_c_target=st.sampled_from([ - ( - [1, 2], - 5, - [ + bs_c_target=st.sampled_from( + [ + ( + [1, 2], + 5, [ - [-0.34784955, 0.47909835, 0.7241975, -0.82175905, -0.43836743], - [-0.34784955, 0.47909835, 0.7241975, -0.82175905, -0.43836743], - ] - ], - ) - ]), + [ + [-0.34784955, 0.47909835, 0.7241975, -0.82175905, -0.43836743], + [-0.34784955, 0.47909835, 0.7241975, -0.82175905, -0.43836743], + ] + ], + ) + ] + ), with_v=st.booleans(), seq_v=st.booleans(), dtype=helpers.get_dtypes("float", full=False), @@ -1605,34 +1610,40 @@ def test_sequential_layer( if with_v: np.random.seed(0) wlim = (6 / (channels + channels)) ** 0.5 - v = Container({ - "submodules": { - "v0": { - "w": _variable( - ivy.array( - np.random.uniform(-wlim, wlim, (channels, channels)), - dtype=dtype, - device=on_device, - ) - ), - "b": _variable( - ivy.zeros([channels], device=on_device, dtype=dtype) - ), - }, - "v2": { - "w": _variable( - ivy.array( - np.random.uniform(-wlim, wlim, (channels, channels)), - dtype=dtype, - device=on_device, - ) - ), - "b": _variable( - ivy.zeros([channels], device=on_device, dtype=dtype) - ), - }, + v = Container( + { + "submodules": { + "v0": { + "w": _variable( + ivy.array( + np.random.uniform( + -wlim, wlim, (channels, channels) + ), + dtype=dtype, + device=on_device, + ) + ), + "b": _variable( + ivy.zeros([channels], device=on_device, dtype=dtype) + ), + }, + "v2": { + "w": _variable( + ivy.array( + np.random.uniform( + -wlim, wlim, (channels, channels) + ), + dtype=dtype, + device=on_device, + ) + ), + "b": _variable( + ivy.zeros([channels], device=on_device, dtype=dtype) + ), + }, + } } - }) + ) else: v = None if seq_v: @@ -1675,8 +1686,3 @@ def test_sequential_layer( assert np.allclose( ivy.to_numpy(seq(x)), np.array(target), rtol=tolerance_dict[dtype] ) - - -all_initializers = ( - all_constant_initializers + all_uniform_initializers + all_gaussian_initializers -) diff --git a/ivy_tests/test_ivy/test_stateful/test_modules.py b/ivy_tests/test_ivy/test_stateful/test_modules.py index 3a0c97444e38d..63bd2c069ccd0 100644 --- a/ivy_tests/test_ivy/test_stateful/test_modules.py +++ b/ivy_tests/test_ivy/test_stateful/test_modules.py @@ -513,21 +513,23 @@ def test_module_w_partial_v( ivy.linspace(ivy.zeros(batch_shape), ivy.ones(batch_shape), input_channels), "float32", ) - v = ivy.Container({ - "linear0": { - "b": _variable(ivy.random_uniform(shape=[64])), - "w": _variable(ivy.random_uniform(shape=[64, 4])), - }, - "linear1": { - "b": _variable(ivy.random_uniform(shape=[64])), - "w": _variable(ivy.random_uniform(shape=[64, 64])), - "extra": _variable(ivy.random_uniform(shape=[64, 64])), - }, - "linear2": { - "b": _variable(ivy.random_uniform(shape=[5])), - "w": _variable(ivy.random_uniform(shape=[5, 64])), - }, - }) + v = ivy.Container( + { + "linear0": { + "b": _variable(ivy.random_uniform(shape=[64])), + "w": _variable(ivy.random_uniform(shape=[64, 4])), + }, + "linear1": { + "b": _variable(ivy.random_uniform(shape=[64])), + "w": _variable(ivy.random_uniform(shape=[64, 64])), + "extra": _variable(ivy.random_uniform(shape=[64, 64])), + }, + "linear2": { + "b": _variable(ivy.random_uniform(shape=[5])), + "w": _variable(ivy.random_uniform(shape=[5, 64])), + }, + } + ) try: TrainableModule( input_channels, @@ -542,13 +544,17 @@ def test_module_w_partial_v( ) except ivy.utils.exceptions.IvyException: pass - v = ivy.Container({ - "linear0": { - "b": _variable(ivy.random_uniform(shape=[64])), - }, - "linear1": {"w": _variable(ivy.random_uniform(shape=[64, 64]))}, - "linear2": {"b": _variable(ivy.random_uniform(shape=[output_channels]))}, - }) + v = ivy.Container( + { + "linear0": { + "b": _variable(ivy.random_uniform(shape=[64])), + }, + "linear1": {"w": _variable(ivy.random_uniform(shape=[64, 64]))}, + "linear2": { + "b": _variable(ivy.random_uniform(shape=[output_channels])) + }, + } + ) try: TrainableModule(input_channels, output_channels, device=on_device, v=v) raise Exception(