From e3787a1788528c6c64cfd0d5bddb4b1dbcd21365 Mon Sep 17 00:00:00 2001 From: Abdurrahman Rajab Date: Sun, 17 Sep 2023 13:56:03 +0300 Subject: [PATCH 1/5] feat: add ilshift for numpy --- .../frontends/numpy/ndarray/ndarray.py | 4 ++ .../test_numpy/test_ndarray/test_ndarray.py | 53 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/ivy/functional/frontends/numpy/ndarray/ndarray.py b/ivy/functional/frontends/numpy/ndarray/ndarray.py index c9e5c88a22313..35a69cba5a4a4 100644 --- a/ivy/functional/frontends/numpy/ndarray/ndarray.py +++ b/ivy/functional/frontends/numpy/ndarray/ndarray.py @@ -618,6 +618,10 @@ def __rshift__(self, value, /): def __lshift__(self, value, /): return ivy.bitwise_left_shift(self.ivy_array, value) + def __ilshift__(self, value, /): + self.ivy_array = ivy.bitwise_left_shift(self.ivy_array, value) + return self + def round(self, decimals=0, out=None): return np_frontend.round(self, decimals=decimals, out=out) 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 f595f36a290c3..41027ca15ca31 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 @@ -3671,3 +3671,56 @@ def test_numpy_ndarray_view( frontend_method_data=frontend_method_data, on_device=on_device, ) + +@handle_frontend_method( + class_tree=CLASS_TREE, + init_tree="numpy.array", + method_name="__ilshift__", + dtype_and_x=helpers.dtype_and_values( + available_dtypes=helpers.get_dtypes("integer"), + num_arrays=2, + max_dim_size=1, + max_value=2**31 - 1, + ), +) +def test_numpy_instance_ilshift__( + dtype_and_x, + frontend_method_data, + init_flags, + method_flags, + frontend, + on_device, +): + input_dtypes, x = dtype_and_x + max_bits = np.iinfo(input_dtypes[0]).bits + max_shift = max_bits - 1 + + x[1] = np.asarray(np.clip(x[1], 0, max_shift), dtype=input_dtypes[1]) + + max_value_before_shift = 2 ** (max_bits - x[1]) - 1 + overflow_threshold = 2 ** (max_bits - 1) + + x[0] = np.asarray( + np.clip(x[0], None, max_value_before_shift), dtype=input_dtypes[0] + ) + + if np.any(x[0] > overflow_threshold): + x[0] = np.clip(x[0], None, overflow_threshold) + if np.any(x[0] < 0): + x[0] = np.abs(x[0]) + + helpers.test_frontend_method( + init_input_dtypes=input_dtypes, + init_all_as_kwargs_np={ + "object": x[0], + }, + method_input_dtypes=input_dtypes, + method_all_as_kwargs_np={ + "value": x[1], + }, + frontend=frontend, + frontend_method_data=frontend_method_data, + init_flags=init_flags, + method_flags=method_flags, + on_device=on_device, + ) \ No newline at end of file From 43ca177a1b843a568f30eed3e244e8546587c1a2 Mon Sep 17 00:00:00 2001 From: Abdurrahman Rajab Date: Sun, 17 Sep 2023 13:56:50 +0300 Subject: [PATCH 2/5] chore: format --- ivy/functional/backends/paddle/general.py | 2 +- ivy/functional/backends/tensorflow/general.py | 2 +- ivy/functional/backends/torch/general.py | 2 +- ivy/functional/frontends/jax/__init__.py | 2 +- .../test_frontends/test_numpy/test_ndarray/test_ndarray.py | 5 +++-- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/ivy/functional/backends/paddle/general.py b/ivy/functional/backends/paddle/general.py index f4258c4f452b9..2a06dd89110c0 100644 --- a/ivy/functional/backends/paddle/general.py +++ b/ivy/functional/backends/paddle/general.py @@ -1,7 +1,7 @@ """Collection of Paddle general functions, wrapped to fit Ivy syntax and signature.""" # global from numbers import Number -from typing import Optional, Union, Sequence, Callable, List, Tuple +from typing import Optional, Union, Sequence, Callable, List import paddle import numpy as np import multiprocessing as _multiprocessing diff --git a/ivy/functional/backends/tensorflow/general.py b/ivy/functional/backends/tensorflow/general.py index cb24872314909..b7e30808efed4 100644 --- a/ivy/functional/backends/tensorflow/general.py +++ b/ivy/functional/backends/tensorflow/general.py @@ -6,7 +6,7 @@ """ # global -from typing import Optional, Union, Sequence, Callable, Tuple +from typing import Optional, Union, Sequence, Callable import numpy as np import multiprocessing as _multiprocessing from numbers import Number diff --git a/ivy/functional/backends/torch/general.py b/ivy/functional/backends/torch/general.py index f8cd14960cddb..7a330696fd084 100644 --- a/ivy/functional/backends/torch/general.py +++ b/ivy/functional/backends/torch/general.py @@ -3,7 +3,7 @@ from functools import reduce as _reduce from numbers import Number from operator import mul -from typing import Optional, Union, Sequence, Callable, List, Tuple +from typing import Optional, Union, Sequence, Callable, List try: import functorch diff --git a/ivy/functional/frontends/jax/__init__.py b/ivy/functional/frontends/jax/__init__.py index 234e378bbe693..c03cd37d26499 100644 --- a/ivy/functional/frontends/jax/__init__.py +++ b/ivy/functional/frontends/jax/__init__.py @@ -7,7 +7,7 @@ from . import array from .array import * from . import general_functions -from .general_functions import * +from .general_functions import ivy from . import lax from . import nn from . import numpy 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 41027ca15ca31..930fcc4607807 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 @@ -3672,6 +3672,7 @@ def test_numpy_ndarray_view( on_device=on_device, ) + @handle_frontend_method( class_tree=CLASS_TREE, init_tree="numpy.array", @@ -3690,7 +3691,7 @@ def test_numpy_instance_ilshift__( method_flags, frontend, on_device, -): +): input_dtypes, x = dtype_and_x max_bits = np.iinfo(input_dtypes[0]).bits max_shift = max_bits - 1 @@ -3723,4 +3724,4 @@ def test_numpy_instance_ilshift__( init_flags=init_flags, method_flags=method_flags, on_device=on_device, - ) \ No newline at end of file + ) From 96ec0c224f083f77b06cbce3fc403450a08fbcaf Mon Sep 17 00:00:00 2001 From: Abdurrahman Rajab Date: Sun, 17 Sep 2023 13:57:20 +0300 Subject: [PATCH 3/5] Revert: "chore: format" This reverts commit e8cff1de443458bd5ef1b8fb4c7bbe727964f929. --- ivy/functional/backends/mxnet/general.py | 2 +- ivy/functional/backends/paddle/general.py | 2 +- ivy/functional/backends/tensorflow/general.py | 2 +- ivy/functional/backends/torch/general.py | 3 ++- ivy/functional/frontends/jax/__init__.py | 2 +- .../test_frontends/test_numpy/test_ndarray/test_ndarray.py | 5 ++--- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/ivy/functional/backends/mxnet/general.py b/ivy/functional/backends/mxnet/general.py index c469b517c47c1..d72d978d7d5a3 100644 --- a/ivy/functional/backends/mxnet/general.py +++ b/ivy/functional/backends/mxnet/general.py @@ -1,5 +1,5 @@ import mxnet as mx -from typing import Union, Optional +from typing import Union, Optional, Tuple import numpy as np from ivy.utils.exceptions import IvyNotImplementedException diff --git a/ivy/functional/backends/paddle/general.py b/ivy/functional/backends/paddle/general.py index 2a06dd89110c0..f4258c4f452b9 100644 --- a/ivy/functional/backends/paddle/general.py +++ b/ivy/functional/backends/paddle/general.py @@ -1,7 +1,7 @@ """Collection of Paddle general functions, wrapped to fit Ivy syntax and signature.""" # global from numbers import Number -from typing import Optional, Union, Sequence, Callable, List +from typing import Optional, Union, Sequence, Callable, List, Tuple import paddle import numpy as np import multiprocessing as _multiprocessing diff --git a/ivy/functional/backends/tensorflow/general.py b/ivy/functional/backends/tensorflow/general.py index b7e30808efed4..cb24872314909 100644 --- a/ivy/functional/backends/tensorflow/general.py +++ b/ivy/functional/backends/tensorflow/general.py @@ -6,7 +6,7 @@ """ # global -from typing import Optional, Union, Sequence, Callable +from typing import Optional, Union, Sequence, Callable, Tuple import numpy as np import multiprocessing as _multiprocessing from numbers import Number diff --git a/ivy/functional/backends/torch/general.py b/ivy/functional/backends/torch/general.py index 7a330696fd084..06c0d9a84f895 100644 --- a/ivy/functional/backends/torch/general.py +++ b/ivy/functional/backends/torch/general.py @@ -1,9 +1,10 @@ """Collection of PyTorch general functions, wrapped to fit Ivy syntax and signature.""" # global from functools import reduce as _reduce +import math from numbers import Number from operator import mul -from typing import Optional, Union, Sequence, Callable, List +from typing import Optional, Union, Sequence, Callable, List, Tuple try: import functorch diff --git a/ivy/functional/frontends/jax/__init__.py b/ivy/functional/frontends/jax/__init__.py index c03cd37d26499..234e378bbe693 100644 --- a/ivy/functional/frontends/jax/__init__.py +++ b/ivy/functional/frontends/jax/__init__.py @@ -7,7 +7,7 @@ from . import array from .array import * from . import general_functions -from .general_functions import ivy +from .general_functions import * from . import lax from . import nn from . import numpy 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 930fcc4607807..41027ca15ca31 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 @@ -3672,7 +3672,6 @@ def test_numpy_ndarray_view( on_device=on_device, ) - @handle_frontend_method( class_tree=CLASS_TREE, init_tree="numpy.array", @@ -3691,7 +3690,7 @@ def test_numpy_instance_ilshift__( method_flags, frontend, on_device, -): +): input_dtypes, x = dtype_and_x max_bits = np.iinfo(input_dtypes[0]).bits max_shift = max_bits - 1 @@ -3724,4 +3723,4 @@ def test_numpy_instance_ilshift__( init_flags=init_flags, method_flags=method_flags, on_device=on_device, - ) + ) \ No newline at end of file From fe56f45a2cdd4e9b5dd047c4af3124401e35e1ba Mon Sep 17 00:00:00 2001 From: Abdurrahman Rajab Date: Sun, 17 Sep 2023 14:14:12 +0300 Subject: [PATCH 4/5] fix: testing backend --- ivy/functional/backends/mxnet/general.py | 2 +- ivy/functional/backends/torch/general.py | 1 - .../test_frontends/test_numpy/test_ndarray/test_ndarray.py | 1 + 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ivy/functional/backends/mxnet/general.py b/ivy/functional/backends/mxnet/general.py index d72d978d7d5a3..c469b517c47c1 100644 --- a/ivy/functional/backends/mxnet/general.py +++ b/ivy/functional/backends/mxnet/general.py @@ -1,5 +1,5 @@ import mxnet as mx -from typing import Union, Optional, Tuple +from typing import Union, Optional import numpy as np from ivy.utils.exceptions import IvyNotImplementedException diff --git a/ivy/functional/backends/torch/general.py b/ivy/functional/backends/torch/general.py index 06c0d9a84f895..f8cd14960cddb 100644 --- a/ivy/functional/backends/torch/general.py +++ b/ivy/functional/backends/torch/general.py @@ -1,7 +1,6 @@ """Collection of PyTorch general functions, wrapped to fit Ivy syntax and signature.""" # global from functools import reduce as _reduce -import math from numbers import Number from operator import mul from typing import Optional, Union, Sequence, Callable, List, Tuple 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 41027ca15ca31..f0f2bc7792b31 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 @@ -3711,6 +3711,7 @@ def test_numpy_instance_ilshift__( helpers.test_frontend_method( init_input_dtypes=input_dtypes, + backend_to_test=backend_fw, init_all_as_kwargs_np={ "object": x[0], }, From 490b43c948dff736ca153853f54d251713707de1 Mon Sep 17 00:00:00 2001 From: ivy-branch Date: Tue, 26 Sep 2023 21:33:16 +0000 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=A4=96=20Lint=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test_numpy/test_ndarray/test_ndarray.py | 109 +++++++++--------- 1 file changed, 55 insertions(+), 54 deletions(-) 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 f0f2bc7792b31..758654a432b45 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 @@ -1921,6 +1921,61 @@ def test_numpy_getitem( ) +@handle_frontend_method( + class_tree=CLASS_TREE, + init_tree="numpy.array", + method_name="__ilshift__", + dtype_and_x=helpers.dtype_and_values( + available_dtypes=helpers.get_dtypes("integer"), + num_arrays=2, + max_dim_size=1, + max_value=2**31 - 1, + ), +) +def test_numpy_instance_ilshift__( + dtype_and_x, + frontend_method_data, + init_flags, + method_flags, + frontend, + on_device, +): + input_dtypes, x = dtype_and_x + max_bits = np.iinfo(input_dtypes[0]).bits + max_shift = max_bits - 1 + + x[1] = np.asarray(np.clip(x[1], 0, max_shift), dtype=input_dtypes[1]) + + max_value_before_shift = 2 ** (max_bits - x[1]) - 1 + overflow_threshold = 2 ** (max_bits - 1) + + x[0] = np.asarray( + np.clip(x[0], None, max_value_before_shift), dtype=input_dtypes[0] + ) + + if np.any(x[0] > overflow_threshold): + x[0] = np.clip(x[0], None, overflow_threshold) + if np.any(x[0] < 0): + x[0] = np.abs(x[0]) + + helpers.test_frontend_method( + init_input_dtypes=input_dtypes, + backend_to_test=backend_fw, + init_all_as_kwargs_np={ + "object": x[0], + }, + method_input_dtypes=input_dtypes, + method_all_as_kwargs_np={ + "value": x[1], + }, + frontend=frontend, + frontend_method_data=frontend_method_data, + init_flags=init_flags, + method_flags=method_flags, + on_device=on_device, + ) + + @handle_frontend_method( class_tree=CLASS_TREE, init_tree="numpy.array", @@ -3671,57 +3726,3 @@ def test_numpy_ndarray_view( frontend_method_data=frontend_method_data, on_device=on_device, ) - -@handle_frontend_method( - class_tree=CLASS_TREE, - init_tree="numpy.array", - method_name="__ilshift__", - dtype_and_x=helpers.dtype_and_values( - available_dtypes=helpers.get_dtypes("integer"), - num_arrays=2, - max_dim_size=1, - max_value=2**31 - 1, - ), -) -def test_numpy_instance_ilshift__( - dtype_and_x, - frontend_method_data, - init_flags, - method_flags, - frontend, - on_device, -): - input_dtypes, x = dtype_and_x - max_bits = np.iinfo(input_dtypes[0]).bits - max_shift = max_bits - 1 - - x[1] = np.asarray(np.clip(x[1], 0, max_shift), dtype=input_dtypes[1]) - - max_value_before_shift = 2 ** (max_bits - x[1]) - 1 - overflow_threshold = 2 ** (max_bits - 1) - - x[0] = np.asarray( - np.clip(x[0], None, max_value_before_shift), dtype=input_dtypes[0] - ) - - if np.any(x[0] > overflow_threshold): - x[0] = np.clip(x[0], None, overflow_threshold) - if np.any(x[0] < 0): - x[0] = np.abs(x[0]) - - helpers.test_frontend_method( - init_input_dtypes=input_dtypes, - backend_to_test=backend_fw, - init_all_as_kwargs_np={ - "object": x[0], - }, - method_input_dtypes=input_dtypes, - method_all_as_kwargs_np={ - "value": x[1], - }, - frontend=frontend, - frontend_method_data=frontend_method_data, - init_flags=init_flags, - method_flags=method_flags, - on_device=on_device, - ) \ No newline at end of file