From 813c09d77eed5ddfd77ffe7c228da05d070eed5a Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Tue, 26 Dec 2023 01:17:46 +0800 Subject: [PATCH 01/27] test --- paddle/phi/api/yaml/ops.yaml | 40 ++ paddle/phi/kernels/bitwise_kernel.h | 25 ++ paddle/phi/kernels/cpu/bitwise_kernel.cc | 66 ++++ paddle/phi/kernels/funcs/bitwise_functors.h | 158 ++++++++ python/paddle/__init__.py | 4 + python/paddle/tensor/__init__.py | 9 + python/paddle/tensor/math.py | 163 ++++++++ test/legacy_test/test_bitwise_shift_op.py | 414 ++++++++++++++++++++ test/legacy_test/test_inplace.py | 101 +++++ 9 files changed, 980 insertions(+) create mode 100644 test/legacy_test/test_bitwise_shift_op.py diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index c15fb2fdb1998..faf163117f5cd 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -332,6 +332,46 @@ kernel : func : binomial +- op : bitwise_left_shift_arithmetic + args : (Tensor x, Tensor y) + output : Tensor(out) + infer_meta : + func : ElementwiseInferMeta + kernel : + func : bitwise_left_shift_arithmetic + backend : x + inplace: (x -> out) + +- op : bitwise_left_shift_logic + args : (Tensor x, Tensor y) + output : Tensor(out) + infer_meta : + func : ElementwiseInferMeta + kernel : + func : bitwise_left_shift_logic + backend : x + inplace: (x -> out) + + - op : bitwise_right_shift_arithmetic + args : (Tensor x, Tensor y) + output : Tensor(out) + infer_meta : + func : ElementwiseInferMeta + kernel : + func : bitwise_right_shift_arithmetic + backend : x + inplace: (x -> out) + +- op : bitwise_right_shift_logic + args : (Tensor x, Tensor y) + output : Tensor(out) + infer_meta : + func : ElementwiseInferMeta + kernel : + func : bitwise_right_shift_logic + backend : x + inplace: (x -> out) + - op : bitwise_and args : (Tensor x, Tensor y) output : Tensor(out) diff --git a/paddle/phi/kernels/bitwise_kernel.h b/paddle/phi/kernels/bitwise_kernel.h index 17307004f360e..d0b26da015a22 100644 --- a/paddle/phi/kernels/bitwise_kernel.h +++ b/paddle/phi/kernels/bitwise_kernel.h @@ -41,4 +41,29 @@ void BitwiseNotKernel(const Context& dev_ctx, const DenseTensor& x, DenseTensor* out); +template +void BitwiseLeftShiftArithmeticKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + DenseTensor* out); + +template +void BitwiseLeftShiftLogicKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + DenseTensor* out); + +template +void BitwiseRightShiftArithmeticKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + DenseTensor* out); + +template +void BitwiseRightShiftLogicKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + DenseTensor* out); + + } // namespace phi diff --git a/paddle/phi/kernels/cpu/bitwise_kernel.cc b/paddle/phi/kernels/cpu/bitwise_kernel.cc index a6297efd9cd3e..5ba49fde84ac6 100644 --- a/paddle/phi/kernels/cpu/bitwise_kernel.cc +++ b/paddle/phi/kernels/cpu/bitwise_kernel.cc @@ -40,6 +40,31 @@ DEFINE_BITWISE_KERNEL(Or) DEFINE_BITWISE_KERNEL(Xor) #undef DEFINE_BITWISE_KERNEL +#define DEFINE_BITWISE_KERNEL_WITH_INVERSE(op_type) \ + template \ + void Bitwise##op_type##Kernel(const Context& dev_ctx, \ + const DenseTensor& x, \ + const DenseTensor& y, \ + DenseTensor* out) { \ + funcs::Bitwise##op_type##Functor func; \ + funcs::InverseBitwise##op_type##Functor inv_func; \ + auto x_dims = x.dims(); \ + auto y_dims = y.dims(); \ + if (x_dims.size() >= y_dims.size()) { \ + funcs::ElementwiseCompute, T>( \ + dev_ctx, x, y, func, out); \ + } else { \ + funcs::ElementwiseCompute, \ + T>(dev_ctx, x, y, inv_func, out); \ + } \ + } + +DEFINE_BITWISE_KERNEL_WITH_INVERSE(LeftShiftArithmetic) +DEFINE_BITWISE_KERNEL_WITH_INVERSE(LeftShiftLogic) +DEFINE_BITWISE_KERNEL_WITH_INVERSE(RightShiftArithmetic) +DEFINE_BITWISE_KERNEL_WITH_INVERSE(RightShiftLogic) +#undef DEFINE_BITWISE_KERNEL_WITH_INVERSE + template void BitwiseNotKernel(const Context& dev_ctx, const DenseTensor& x, @@ -97,3 +122,44 @@ PD_REGISTER_KERNEL(bitwise_not, int16_t, int, int64_t) {} + + +PD_REGISTER_KERNEL(bitwise_left_shift_arithmetic, + CPU, + ALL_LAYOUT, + phi::BitwiseLeftShiftArithmeticKernel, + uint8_t, + int8_t, + int16_t, + int, + int64_t) {} + +PD_REGISTER_KERNEL(bitwise_left_shift_logic, + CPU, + ALL_LAYOUT, + phi::BitwiseLeftShiftLogicKernel, + uint8_t, + int8_t, + int16_t, + int, + int64_t) {} + +PD_REGISTER_KERNEL(bitwise_right_shift_arithmetic, + CPU, + ALL_LAYOUT, + phi::BitwiseRightShiftArithmeticKernel, + uint8_t, + int8_t, + int16_t, + int, + int64_t) {} + +PD_REGISTER_KERNEL(bitwise_right_shift_logic, + CPU, + ALL_LAYOUT, + phi::BitwiseRightShiftLogicKernel, + uint8_t, + int8_t, + int16_t, + int, + int64_t) {} diff --git a/paddle/phi/kernels/funcs/bitwise_functors.h b/paddle/phi/kernels/funcs/bitwise_functors.h index db1fc59f534bc..912a0a7be1d98 100644 --- a/paddle/phi/kernels/funcs/bitwise_functors.h +++ b/paddle/phi/kernels/funcs/bitwise_functors.h @@ -47,5 +47,163 @@ struct BitwiseNotFunctor { HOSTDEVICE bool operator()(const bool a) const { return !a; } }; +template +struct BitwiseLeftShiftArithmeticFunctor { + HOSTDEVICE T operator()(const T a, const T b) const { + if (b >= static_cast(sizeof(T) * 8)) return static_cast(0); + if (b < static_cast(0)) return static_cast(0); + return a << b; + } +}; + +template +struct InverseBitwiseLeftShiftArithmeticFunctor { + inline HOSTDEVICE T operator()(const T a, const T b) const { + if (a >= static_cast(sizeof(T) * 8)) return static_cast(0); + if (a < static_cast(0)) return static_cast(0); + return b << a; + } +}; + +template +struct BitwiseLeftShiftLogicFunctor { + HOSTDEVICE T operator()(const T a, const T b) const { + if (b < static_cast(0) || b >= static_cast(sizeof(T) * 8)) + return static_cast(0); + return a << b; + } +}; + +template +struct InverseBitwiseLeftShiftLogicFunctor { + inline HOSTDEVICE T operator()(const T a, const T b) const { + if (a < static_cast(0) || a >= static_cast(sizeof(T) * 8)) + return static_cast(0); + return b << a; + } +}; + +template +struct BitwiseRightShiftArithmeticFunctor { + HOSTDEVICE T operator()(const T a, const T b) const { + if (b < static_cast(0) || b >= static_cast(sizeof(T) * 8)) + return static_cast(-(a >> (sizeof(T) * 8 - 1) & 1)); + return a >> b; + } +}; + +template +struct InverseBitwiseRightShiftArithmeticFunctor { + inline HOSTDEVICE T operator()(const T a, const T b) const { + if (a < static_cast(0) || a >= static_cast(sizeof(T) * 8)) + return static_cast(-(b >> (sizeof(T) * 8 - 1) & 1)); + return b >> a; + } +}; + +template <> +struct BitwiseRightShiftArithmeticFunctor { + HOSTDEVICE uint8_t operator()(const uint8_t a, const uint8_t b) const { + if (b >= static_cast(sizeof(uint8_t) * 8)) + return static_cast(0); + return a >> b; + } +}; + +template <> +struct InverseBitwiseRightShiftArithmeticFunctor { + inline HOSTDEVICE uint8_t operator()(const uint8_t a, const uint8_t b) const { + if (a >= static_cast(sizeof(uint8_t) * 8)) + return static_cast(0); + return b >> a; + } +}; + +template +struct BitwiseRightShiftLogicFunctor { + HOSTDEVICE T operator()(const T a, const T b) const { + if (b >= static_cast(sizeof(T) * 8)) return static_cast(0); + return a >> b; + } +}; + +template +struct InverseBitwiseRightShiftLogicFunctor { + inline HOSTDEVICE T operator()(const T a, const T b) const { + if (a >= static_cast(sizeof(T) * 8)) return static_cast(0); + return b >> a; + } +}; + +template +HOSTDEVICE T logic_shift_func(const T a, const T b) { + if (b < static_cast(0) || b >= static_cast(sizeof(T) * 8)) + return static_cast(0); + T t = static_cast(sizeof(T) * 8 - 1); + T mask = (((a >> t) << t) >> b) << 1; + return (a >> b) ^ mask; +} + +// signed int8 +template <> +struct BitwiseRightShiftLogicFunctor { + HOSTDEVICE int8_t operator()(const int8_t a, const int8_t b) const { + return logic_shift_func(a, b); + } +}; + +template <> +struct InverseBitwiseRightShiftLogicFunctor { + inline HOSTDEVICE int8_t operator()(const int8_t a, const int8_t b) const { + return logic_shift_func(b, a); + } +}; + +// signed int16 +template <> +struct BitwiseRightShiftLogicFunctor { + HOSTDEVICE int16_t operator()(const int16_t a, const int16_t b) const { + return logic_shift_func(a, b); + } +}; + +template <> +struct InverseBitwiseRightShiftLogicFunctor { + inline HOSTDEVICE int16_t operator()(const int16_t a, const int16_t b) const { + return logic_shift_func(b, a); + } +}; + +// signed int32 +template <> +struct BitwiseRightShiftLogicFunctor { + HOSTDEVICE int operator()(const int a, const int b) const { + return logic_shift_func(a, b); + } +}; + +template <> +struct InverseBitwiseRightShiftLogicFunctor { + inline HOSTDEVICE int operator()(const int a, const int b) const { + return logic_shift_func(b, a); + } +}; + +// signed int64 +template <> +struct BitwiseRightShiftLogicFunctor { + HOSTDEVICE int64_t operator()(const int64_t a, const int64_t b) const { + return logic_shift_func(a, b); + } +}; + +template <> +struct InverseBitwiseRightShiftLogicFunctor { + inline HOSTDEVICE int64_t operator()(const int64_t a, const int64_t b) const { + return logic_shift_func(b, a); + } +}; + + } // namespace funcs } // namespace phi diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index ef4c7c96c4c38..2f222c2c906e7 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -936,6 +936,10 @@ 'i1e', 'polygamma', 'polygamma_', + 'bitwise_left_shift', + 'bitwise_left_shift_', + 'bitwise_right_shift', + 'bitwise_right_shift_', 'masked_fill', 'masked_fill_', 'masked_scatter', diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index 9172d8c7fbf1d..e4111207c7405 100644 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -234,6 +234,10 @@ atan_, atanh, atanh_, + bitwise_left_shift, + bitwise_left_shift_, + bitwise_right_shift, + bitwise_right_shift_, broadcast_shape, ceil, ceil_, @@ -775,6 +779,11 @@ 'asinh_', 'diag', 'normal_', + 'normal_', + 'bitwise_left_shift', + 'bitwise_left_shift_', + 'bitwise_right_shift', + 'bitwise_right_shift_', 'index_fill', 'index_fill_', 'atleast_1d', diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 49fd425726cb5..8ed362f04b5f0 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -7052,6 +7052,169 @@ def ldexp_(x, y, name=None): two = paddle.to_tensor(2, dtype=out_dtype) return paddle.multiply_(x, paddle.pow(two, y)) +def _bitwise_op(op_name, x, y, out=None, name=None): + check_variable_and_dtype( + x, + "x", + ["uint8", "int8", "int16", "int32", "int64"], + op_name, + ) + if y is not None: + check_variable_and_dtype( + y, + "y", + ["uint8", "int8", "int16", "int32", "int64"], + op_name, + ) + + helper = LayerHelper(op_name, **locals()) + assert x.dtype == y.dtype + + out = helper.create_variable_for_type_inference(dtype=x.dtype) + + helper.append_op( + type=op_name, inputs={"x": x, "y": y}, outputs={"out": out} + ) + + return out + + +def bitwise_left_shift(x, y, is_arithmetic=True, out=None, name=None): + r""" + Apply ``bitwise_left_shift`` on Tensor ``X`` and ``Y`` . + .. math:: + Out = X \ll Y + .. note:: + ``paddle.bitwise_left_shift`` supports broadcasting. If you want know more about broadcasting, please refer to please refer to `Introduction to Tensor`_ . + .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor. + Args: + x (Tensor): Input Tensor of ``bitwise_left_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. + y (Tensor): Input Tensor of ``bitwise_left_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. + name (str, optional): The default value is None. Normally there is no need for + user to set this property. For more information, please refer to :ref:`api_guide_Name`. + Returns: + Tensor: Result of ``bitwise_left_shift`` . It is a N-D Tensor with the same data type of input Tensor. + Examples: + .. code-block:: python + >>> import paddle + >>> x= paddle.to_tensor([[1,2,4,8],[16,17,32,65]]) + >>> y = paddle.to_tensor([[1,2,3,4,], [2,3,2,1]]) + >>> paddle.bitwise_left_shift(x, y) + Tensor(shape=[2, 4], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [[2 , 8 , 32 , 128], + [64 , 136, 128, 130]]) + """ + if in_dynamic_mode() and out is None: + if is_arithmetic: + return _C_ops.bitwise_left_shift_arithmetic(x, y) + else: + return _C_ops.bitwise_left_shift_logic(x, y) + if is_arithmetic: + return _bitwise_op( + op_name="bitwise_left_shift_arithmetic", + x=x, + y=y, + name=name, + out=out, + ) + else: + return _bitwise_op( + op_name="bitwise_left_shift_logic", + x=x, + y=y, + name=name, + out=out, + ) + + +@inplace_apis_in_dygraph_only +def bitwise_left_shift_(x, y, is_arithmetic=True, out=None, name=None): + r""" + Inplace version of ``bitwise_left_shift`` API, the output Tensor will be inplaced with input ``x``. + Please refer to :ref:`api_paddle_bitwise_left_shift`. + """ + out_shape = broadcast_shape(x.shape, y.shape) + if out_shape != x.shape: + raise ValueError( + "The shape of broadcast output {} is different from that of inplace tensor {} in the Inplace operation.".format( + out_shape, x.shape + ) + ) + if in_dynamic_or_pir_mode(): + if is_arithmetic: + return _C_ops.bitwise_left_shift_arithmetic_(x, y) + else: + return _C_ops.bitwise_left_shift_logic_(x, y) + + +def bitwise_right_shift(x, y, is_arithmetic=True, out=None, name=None): + r""" + Apply ``bitwise_right_shift`` on Tensor ``X`` and ``Y`` . + .. math:: + Out = X \gg Y + .. note:: + ``paddle.bitwise_right_shift`` supports broadcasting. If you want know more about broadcasting, please refer to please refer to `Introduction to Tensor`_ . + .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor. + Args: + x (Tensor): Input Tensor of ``bitwise_right_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. + y (Tensor): Input Tensor of ``bitwise_right_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. + name (str, optional): The default value is None. Normally there is no need for + user to set this property. For more information, please refer to :ref:`api_guide_Name`. + Returns: + Tensor: Result of ``bitwise_right_shift`` . It is a N-D Tensor with the same data type of input Tensor. + Examples: + .. code-block:: python + >>> import paddle + >>> x= paddle.to_tensor([[10,20,40,80],[16,17,32,65]]) + >>> y = paddle.to_tensor([[1,2,3,4,], [2,3,2,1]]) + >>> paddle.bitwise_right_shift(x, y) + Tensor(shape=[2, 4], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [[5 , 5 , 5 , 5 ], + [4 , 2 , 8 , 32]]) + """ + if in_dynamic_mode() and out is None: + if is_arithmetic: + return _C_ops.bitwise_right_shift_arithmetic(x, y) + else: + return _C_ops.bitwise_right_shift_logic(x, y) + if is_arithmetic: + return _bitwise_op( + op_name="bitwise_right_shift_arithmetic", + x=x, + y=y, + name=name, + out=out, + ) + else: + return _bitwise_op( + op_name="bitwise_right_shift_logic", + x=x, + y=y, + name=name, + out=out, + ) + + +@inplace_apis_in_dygraph_only +def bitwise_right_shift_(x, y, is_arithmetic=True, out=None, name=None): + r""" + Inplace version of ``bitwise_right_shift`` API, the output Tensor will be inplaced with input ``x``. + Please refer to :ref:`api_paddle_bitwise_left_shift`. + """ + out_shape = broadcast_shape(x.shape, y.shape) + if out_shape != x.shape: + raise ValueError( + "The shape of broadcast output {} is different from that of inplace tensor {} in the Inplace operation.".format( + out_shape, x.shape + ) + ) + + if in_dynamic_or_pir_mode(): + if is_arithmetic: + return _C_ops.bitwise_right_shift_arithmetic_(x, y) + else: + return _C_ops.bitwise_right_shift_logic_(x, y) + def hypot(x, y, name=None): """ diff --git a/test/legacy_test/test_bitwise_shift_op.py b/test/legacy_test/test_bitwise_shift_op.py new file mode 100644 index 0000000000000..9b957891de7b1 --- /dev/null +++ b/test/legacy_test/test_bitwise_shift_op.py @@ -0,0 +1,414 @@ +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest + +import numpy as np + +import paddle + +_SIGNED_TO_UNSIGNED_TABLE = { + "int8": "uint8", + "int16": "uint16", + "int32": "uint32", + "int64": "uint64", +} + +_UNSIGNED_TO_SIGNED_TABLE = { + "uint8": "int8", + "uint16": "int16", + "uint32": "int32", + "uint64": "int64", +} + +_UNSIGNED_LIST = ['uint8', 'uint16', 'uint32', 'uint64'] + + +def ref_left_shift_arithmetic(x, y): + out = np.left_shift(x, y) + return out + + +def ref_left_shift_logical(x, y): + out = np.left_shift(x, y) + return out + + +def ref_right_shift_arithmetic(x, y): + return np.right_shift(x, y) + + +def ref_right_shift_logical(x, y): + if str(x.dtype) in _UNSIGNED_LIST: + return np.right_shift(x, y) + else: + orig_dtype = x.dtype + unsigned_dtype = _SIGNED_TO_UNSIGNED_TABLE[str(orig_dtype)] + x = x.astype(unsigned_dtype) + y = y.astype(unsigned_dtype) + res = np.right_shift(x, y) + return res.astype(orig_dtype) + + +class TestBitwiseLeftShiftAPI(unittest.TestCase): + def setUp(self): + self.init_input() + self.place = ( + paddle.CUDAPlace(0) + if paddle.is_compiled_with_cuda() + else paddle.CPUPlace() + ) + + def init_input(self): + self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') + self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') + + def test_static_api_arithmetic(self): + paddle.enable_static() + with paddle.static.program_guard(paddle.static.Program()): + x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) + y = paddle.static.data('y', self.y.shape, dtype=self.y.dtype) + out = paddle.bitwise_left_shift( + x, + y, + ) + exe = paddle.static.Executor(self.place) + res = exe.run(feed={'x': self.x, 'y': self.y}, fetch_list=[out]) + out_ref = ref_left_shift_arithmetic(self.x, self.y) + np.testing.assert_allclose(out_ref, res[0]) + + def test_dygraph_api_arithmetic(self): + paddle.disable_static() + x = paddle.to_tensor(self.x) + y = paddle.to_tensor(self.y) + out = paddle.bitwise_left_shift( + x, + y, + ) + out_ref = ref_left_shift_arithmetic(self.x, self.y) + np.testing.assert_allclose(out_ref, out.numpy()) + paddle.enable_static() + + def test_static_api_logical(self): + paddle.enable_static() + with paddle.static.program_guard(paddle.static.Program()): + x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) + y = paddle.static.data('y', self.y.shape, dtype=self.y.dtype) + out = paddle.bitwise_left_shift(x, y, False) + exe = paddle.static.Executor(self.place) + res = exe.run(feed={'x': self.x, 'y': self.y}, fetch_list=[out]) + out_ref = ref_left_shift_logical(self.x, self.y) + np.testing.assert_allclose(out_ref, res[0]) + + def test_dygraph_api_logical(self): + paddle.disable_static() + x = paddle.to_tensor(self.x) + y = paddle.to_tensor(self.y) + out = paddle.bitwise_left_shift(x, y, False) + out_ref = ref_left_shift_logical(self.x, self.y) + np.testing.assert_allclose(out_ref, out.numpy()) + paddle.enable_static() + + +class TestBitwiseLeftShiftAPI_UINT8(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') + self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') + + +class TestBitwiseLeftShiftAPI_UINT8_broadcast1(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') + self.y = np.random.randint(0, 256, [3]).astype('uint8') + + +class TestBitwiseLeftShiftAPI_UINT8_broadcast2(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(0, 256, [3]).astype('uint8') + self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') + + +class TestBitwiseLeftShiftAPI_INT8(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') + self.y = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') + + +class TestBitwiseLeftShiftAPI_INT8_broadcast1(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') + self.y = np.random.randint(-(2**7), 2**7, [3]).astype('int8') + + +class TestBitwiseLeftShiftAPI_INT8_broadcast2(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**7), 2**7, [3]).astype('int8') + self.y = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') + + +class TestBitwiseLeftShiftAPI_INT16(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') + self.y = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') + + +class TestBitwiseLeftShiftAPI_INT16_broadcast1(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') + self.y = np.random.randint(-(2**15), 2**15, [3]).astype('int16') + + +class TestBitwiseLeftShiftAPI_INT16_broadcast2(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**15), 2**15, [3]).astype('int16') + self.y = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') + + +class TestBitwiseLeftShiftAPI_INT32(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') + self.y = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') + + +class TestBitwiseLeftShiftAPI_INT32_broadcast1(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') + self.y = np.random.randint(-(2**31), 2**31, [3]).astype('int32') + + +class TestBitwiseLeftShiftAPI_INT32_broadcast2(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**31), 2**31, [3]).astype('int32') + self.y = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') + + +class TestBitwiseLeftShiftAPI_INT64(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) + self.y = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) + + +class TestBitwiseLeftShiftAPI_INT64_broadcast1(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) + self.y = np.random.randint(-(2**63), 2**63, [3], dtype=np.int64) + + +class TestBitwiseLeftShiftAPI_INT64_broadcast2(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**63), 2**63, [3], dtype=np.int64) + self.y = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) + + +class TestBitwiseLeftShiftAPI_special_case1(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.array([0b11111111], dtype='int8') + self.y = np.array([1], dtype='int8') + + +class TestBitwiseLeftShiftAPI_special_case2(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.array([0b11111111], dtype='int8') + self.y = np.array([10], dtype='int8') + + +class TestBitwiseLeftShiftAPI_special_case3(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.array([0b11111111], dtype='uint8') + self.y = np.array([1], dtype='uint8') + + +class TestBitwiseLeftShiftAPI_special_case4(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.array([0b11111111], dtype='uint8') + self.y = np.array([10], dtype='uint8') + + +class TestBitwiseRightShiftAPI(unittest.TestCase): + def setUp(self): + self.init_input() + self.place = ( + paddle.CUDAPlace(0) + if paddle.is_compiled_with_cuda() + else paddle.CPUPlace() + ) + + def init_input(self): + self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') + self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') + + def test_static_api_arithmetic(self): + paddle.enable_static() + with paddle.static.program_guard(paddle.static.Program()): + x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) + y = paddle.static.data('y', self.y.shape, dtype=self.y.dtype) + out = paddle.bitwise_right_shift( + x, + y, + ) + exe = paddle.static.Executor(self.place) + res = exe.run(feed={'x': self.x, 'y': self.y}, fetch_list=[out]) + out_ref = ref_right_shift_arithmetic(self.x, self.y) + np.testing.assert_allclose(out_ref, res[0]) + + def test_dygraph_api_arithmetic(self): + paddle.disable_static() + x = paddle.to_tensor(self.x) + y = paddle.to_tensor(self.y) + out = paddle.bitwise_right_shift( + x, + y, + ) + out_ref = ref_right_shift_arithmetic(self.x, self.y) + np.testing.assert_allclose(out_ref, out.numpy()) + paddle.enable_static() + + def test_static_api_logical(self): + paddle.enable_static() + with paddle.static.program_guard(paddle.static.Program()): + x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) + y = paddle.static.data('y', self.y.shape, dtype=self.y.dtype) + out = paddle.bitwise_right_shift(x, y, False) + exe = paddle.static.Executor(self.place) + res = exe.run(feed={'x': self.x, 'y': self.y}, fetch_list=[out]) + out_ref = ref_right_shift_logical(self.x, self.y) + np.testing.assert_allclose(out_ref, res[0]) + + def test_dygraph_api_logical(self): + paddle.disable_static() + x = paddle.to_tensor(self.x) + y = paddle.to_tensor(self.y) + out = paddle.bitwise_right_shift(x, y, False) + out_ref = ref_right_shift_logical(self.x, self.y) + np.testing.assert_allclose(out_ref, out.numpy()) + paddle.enable_static() + + +class TestBitwiseRightShiftAPI_UINT8(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') + self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') + + +class TestBitwiseRightShiftAPI_UINT8_broadcast1(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') + self.y = np.random.randint(0, 256, [3]).astype('uint8') + + +class TestBitwiseRightShiftAPI_UINT8_broadcast2(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(0, 256, [3]).astype('uint8') + self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') + + +class TestBitwiseRightShiftAPI_INT8(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') + self.y = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') + + +class TestBitwiseRightShiftAPI_INT8_broadcast1(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') + self.y = np.random.randint(-(2**7), 2**7, [3]).astype('int8') + + +class TestBitwiseRightShiftAPI_INT8_broadcast2(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**7), 2**7, [3]).astype('int8') + self.y = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') + + +class TestBitwiseRightShiftAPI_INT16(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') + self.y = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') + + +class TestBitwiseRightShiftAPI_INT16_broadcast1(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') + self.y = np.random.randint(-(2**15), 2**15, [3]).astype('int16') + + +class TestBitwiseRightShiftAPI_INT16_broadcast2(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**15), 2**15, [3]).astype('int16') + self.y = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') + + +class TestBitwiseRightShiftAPI_INT32(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') + self.y = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') + + +class TestBitwiseRightShiftAPI_INT32_broadcast1(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') + self.y = np.random.randint(-(2**31), 2**31, [3]).astype('int32') + + +class TestBitwiseRightShiftAPI_INT32_broadcast2(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**31), 2**31, [3]).astype('int32') + self.y = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') + + +class TestBitwiseRightShiftAPI_INT64(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) + self.y = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) + + +class TestBitwiseRightShiftAPI_INT64_broadcast1(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) + self.y = np.random.randint(-(2**63), 2**63, [3], dtype=np.int64) + + +class TestBitwiseRightShiftAPI_INT64_broadcast2(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**63), 2**63, [3], dtype=np.int64) + self.y = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) + + +class TestBitwiseRightShiftAPI_special_case1(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.array([0b11111111]).astype('int8') + self.y = np.array([1]).astype('int8') + + +class TestBitwiseRightShiftAPI_special_case2(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.array([0b11111111]).astype('int8') + self.y = np.array([10]).astype('int8') + + +class TestBitwiseRightShiftAPI_special_case3(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.array([0b11111111], dtype='uint8') + self.y = np.array([1], dtype='uint8') + + +class TestBitwiseRightShiftAPI_special_case4(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.array([0b11111111], dtype='uint8') + self.y = np.array([10], dtype='uint8') + + +if __name__ == '__main__': + paddle.enable_static() + unittest.main() diff --git a/test/legacy_test/test_inplace.py b/test/legacy_test/test_inplace.py index f06edfd83206c..20e2960c29481 100644 --- a/test/legacy_test/test_inplace.py +++ b/test/legacy_test/test_inplace.py @@ -1633,6 +1633,107 @@ def test_forward_version(self): self.assertEqual(var.inplace_version, 3) +class TestDygraphInplaceBitwiseLeftShift_arithmetic(TestDygraphInplaceLogicAnd): + def init_data(self): + self.input_var_numpy = np.random.randint( + low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" + ) + self.input_var_numpy = paddle.to_tensor(self.input_var_numpy) + self.dtype = "int32" + self.y = np.random.randint( + low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" + ) + self.y = paddle.to_tensor(self.y) + self.is_arithmetic = True + + def inplace_api_processing(self, var): + return paddle.bitwise_left_shift_(var, self.y, self.is_arithmetic) + + def non_inplace_api_processing(self, var): + return paddle.bitwise_left_shift(var, self.y, self.is_arithmetic) + + def test_broadcast_error(self): + broadcast_input = paddle.randn([4, 5]) + with self.assertRaises(ValueError): + self.inplace_api_processing(broadcast_input) + + +class TestDygraphInplaceBitwiseRightShift_arithmetic( + TestDygraphInplaceLogicAnd +): + def init_data(self): + self.input_var_numpy = np.random.randint( + low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" + ) + self.input_var_numpy = paddle.to_tensor(self.input_var_numpy) + self.dtype = "int32" + self.y = np.random.randint( + low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" + ) + self.y = paddle.to_tensor(self.y) + self.is_arithmetic = True + + def inplace_api_processing(self, var): + return paddle.bitwise_right_shift_(var, self.y, self.is_arithmetic) + + def non_inplace_api_processing(self, var): + return paddle.bitwise_right_shift_(var, self.y, self.is_arithmetic) + + def test_broadcast_error(self): + broadcast_input = paddle.randn([4, 5]) + with self.assertRaises(ValueError): + self.inplace_api_processing(broadcast_input) + + +class TestDygraphInplaceBitwiseLeftShift_logic(TestDygraphInplaceLogicAnd): + def init_data(self): + self.input_var_numpy = np.random.randint( + low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" + ) + self.input_var_numpy = paddle.to_tensor(self.input_var_numpy) + self.dtype = "int32" + self.y = np.random.randint( + low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" + ) + self.y = paddle.to_tensor(self.y) + self.is_arithmetic = False + + def inplace_api_processing(self, var): + return paddle.bitwise_left_shift_(var, self.y, self.is_arithmetic) + + def non_inplace_api_processing(self, var): + return paddle.bitwise_left_shift(var, self.y, self.is_arithmetic) + + def test_broadcast_error(self): + broadcast_input = paddle.randn([4, 5]) + with self.assertRaises(ValueError): + self.inplace_api_processing(broadcast_input) + + +class TestDygraphInplaceBitwiseRightShift_logic(TestDygraphInplaceLogicAnd): + def init_data(self): + self.input_var_numpy = np.random.randint( + low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" + ) + self.input_var_numpy = paddle.to_tensor(self.input_var_numpy) + self.dtype = "int32" + self.y = np.random.randint( + low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" + ) + self.y = paddle.to_tensor(self.y) + self.is_arithmetic = False + + def inplace_api_processing(self, var): + return paddle.bitwise_right_shift_(var, self.y, self.is_arithmetic) + + def non_inplace_api_processing(self, var): + return paddle.bitwise_right_shift_(var, self.y, self.is_arithmetic) + + def test_broadcast_error(self): + broadcast_input = paddle.randn([4, 5]) + with self.assertRaises(ValueError): + self.inplace_api_processing(broadcast_input) + class TestDygraphInplaceIndexFill(TestDygraphInplace): def init_data(self): self.input_var_numpy = np.random.random((20, 40)) From 42b39722d477ff746fccdc488e64433a5b6bb536 Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Tue, 26 Dec 2023 01:53:08 +0800 Subject: [PATCH 02/27] fix --- paddle/phi/kernels/kps/bitwise_kernel.cu | 44 ++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/paddle/phi/kernels/kps/bitwise_kernel.cu b/paddle/phi/kernels/kps/bitwise_kernel.cu index fcdc7c95e9151..7dd3225f20476 100644 --- a/paddle/phi/kernels/kps/bitwise_kernel.cu +++ b/paddle/phi/kernels/kps/bitwise_kernel.cu @@ -41,6 +41,10 @@ namespace phi { DEFINE_BITWISE_KERNEL(And) DEFINE_BITWISE_KERNEL(Or) DEFINE_BITWISE_KERNEL(Xor) +DEFINE_BITWISE_KERNEL(LeftShiftArithmetic) +DEFINE_BITWISE_KERNEL(LeftShiftLogic) +DEFINE_BITWISE_KERNEL(RightShiftArithmetic) +DEFINE_BITWISE_KERNEL(RightShiftLogic) #undef DEFINE_BITWISE_KERNEL template @@ -112,4 +116,44 @@ PD_REGISTER_KERNEL(bitwise_not, int, int64_t) {} +PD_REGISTER_KERNEL(bitwise_left_shift_arithmetic, + KPS, + ALL_LAYOUT, + phi::BitwiseLeftShiftArithmeticKernel, + uint8_t, + int8_t, + int16_t, + int, + int64_t) {} + +PD_REGISTER_KERNEL(bitwise_left_shift_logic, + KPS, + ALL_LAYOUT, + phi::BitwiseLeftShiftLogicKernel, + uint8_t, + int8_t, + int16_t, + int, + int64_t) {} + +PD_REGISTER_KERNEL(bitwise_right_shift_arithmetic, + KPS, + ALL_LAYOUT, + phi::BitwiseRightShiftArithmeticKernel, + uint8_t, + int8_t, + int16_t, + int, + int64_t) {} + +PD_REGISTER_KERNEL(bitwise_right_shift_logic, + KPS, + ALL_LAYOUT, + phi::BitwiseRightShiftLogicKernel, + uint8_t, + int8_t, + int16_t, + int, + int64_t) {} + #endif From 198d875525da8142350a8a5b625edf99f86e22f3 Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Tue, 26 Dec 2023 02:01:29 +0800 Subject: [PATCH 03/27] fix --- paddle/phi/api/yaml/ops.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index faf163117f5cd..eb620d192463c 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -352,7 +352,7 @@ backend : x inplace: (x -> out) - - op : bitwise_right_shift_arithmetic +- op : bitwise_right_shift_arithmetic args : (Tensor x, Tensor y) output : Tensor(out) infer_meta : From 6ab394d7620cdaf0428c782697f9791606f42c38 Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Wed, 27 Dec 2023 16:56:43 +0800 Subject: [PATCH 04/27] test --- paddle/phi/kernels/bitwise_kernel.h | 46 +- paddle/phi/kernels/cpu/bitwise_kernel.cc | 126 ++-- paddle/phi/kernels/funcs/bitwise_functors.h | 312 ++++----- paddle/phi/kernels/kps/bitwise_kernel.cu | 86 +-- python/paddle/__init__.py | 4 + python/paddle/tensor/math.py | 270 ++++---- test/legacy_test/test_bitwise_shift_op.py | 692 ++++++++++---------- test/legacy_test/test_inplace.py | 200 +++--- 8 files changed, 870 insertions(+), 866 deletions(-) diff --git a/paddle/phi/kernels/bitwise_kernel.h b/paddle/phi/kernels/bitwise_kernel.h index d0b26da015a22..73082fc6c7176 100644 --- a/paddle/phi/kernels/bitwise_kernel.h +++ b/paddle/phi/kernels/bitwise_kernel.h @@ -41,29 +41,29 @@ void BitwiseNotKernel(const Context& dev_ctx, const DenseTensor& x, DenseTensor* out); -template -void BitwiseLeftShiftArithmeticKernel(const Context& dev_ctx, - const DenseTensor& x, - const DenseTensor& y, - DenseTensor* out); - -template -void BitwiseLeftShiftLogicKernel(const Context& dev_ctx, - const DenseTensor& x, - const DenseTensor& y, - DenseTensor* out); - -template -void BitwiseRightShiftArithmeticKernel(const Context& dev_ctx, - const DenseTensor& x, - const DenseTensor& y, - DenseTensor* out); - -template -void BitwiseRightShiftLogicKernel(const Context& dev_ctx, - const DenseTensor& x, - const DenseTensor& y, - DenseTensor* out); +// template +// void BitwiseLeftShiftArithmeticKernel(const Context& dev_ctx, +// const DenseTensor& x, +// const DenseTensor& y, +// DenseTensor* out); + +// template +// void BitwiseLeftShiftLogicKernel(const Context& dev_ctx, +// const DenseTensor& x, +// const DenseTensor& y, +// DenseTensor* out); + +// template +// void BitwiseRightShiftArithmeticKernel(const Context& dev_ctx, +// const DenseTensor& x, +// const DenseTensor& y, +// DenseTensor* out); + +// template +// void BitwiseRightShiftLogicKernel(const Context& dev_ctx, +// const DenseTensor& x, +// const DenseTensor& y, +// DenseTensor* out); } // namespace phi diff --git a/paddle/phi/kernels/cpu/bitwise_kernel.cc b/paddle/phi/kernels/cpu/bitwise_kernel.cc index 5ba49fde84ac6..8eef29a690d7f 100644 --- a/paddle/phi/kernels/cpu/bitwise_kernel.cc +++ b/paddle/phi/kernels/cpu/bitwise_kernel.cc @@ -40,30 +40,30 @@ DEFINE_BITWISE_KERNEL(Or) DEFINE_BITWISE_KERNEL(Xor) #undef DEFINE_BITWISE_KERNEL -#define DEFINE_BITWISE_KERNEL_WITH_INVERSE(op_type) \ - template \ - void Bitwise##op_type##Kernel(const Context& dev_ctx, \ - const DenseTensor& x, \ - const DenseTensor& y, \ - DenseTensor* out) { \ - funcs::Bitwise##op_type##Functor func; \ - funcs::InverseBitwise##op_type##Functor inv_func; \ - auto x_dims = x.dims(); \ - auto y_dims = y.dims(); \ - if (x_dims.size() >= y_dims.size()) { \ - funcs::ElementwiseCompute, T>( \ - dev_ctx, x, y, func, out); \ - } else { \ - funcs::ElementwiseCompute, \ - T>(dev_ctx, x, y, inv_func, out); \ - } \ - } - -DEFINE_BITWISE_KERNEL_WITH_INVERSE(LeftShiftArithmetic) -DEFINE_BITWISE_KERNEL_WITH_INVERSE(LeftShiftLogic) -DEFINE_BITWISE_KERNEL_WITH_INVERSE(RightShiftArithmetic) -DEFINE_BITWISE_KERNEL_WITH_INVERSE(RightShiftLogic) -#undef DEFINE_BITWISE_KERNEL_WITH_INVERSE +// #define DEFINE_BITWISE_KERNEL_WITH_INVERSE(op_type) \ +// template \ +// void Bitwise##op_type##Kernel(const Context& dev_ctx, \ +// const DenseTensor& x, \ +// const DenseTensor& y, \ +// DenseTensor* out) { \ +// funcs::Bitwise##op_type##Functor func; \ +// funcs::InverseBitwise##op_type##Functor inv_func; \ +// auto x_dims = x.dims(); \ +// auto y_dims = y.dims(); \ +// if (x_dims.size() >= y_dims.size()) { \ +// funcs::ElementwiseCompute, T>( \ +// dev_ctx, x, y, func, out); \ +// } else { \ +// funcs::ElementwiseCompute, \ +// T>(dev_ctx, x, y, inv_func, out); \ +// } \ +// } + +// DEFINE_BITWISE_KERNEL_WITH_INVERSE(LeftShiftArithmetic) +// DEFINE_BITWISE_KERNEL_WITH_INVERSE(LeftShiftLogic) +// DEFINE_BITWISE_KERNEL_WITH_INVERSE(RightShiftArithmetic) +// DEFINE_BITWISE_KERNEL_WITH_INVERSE(RightShiftLogic) +// #undef DEFINE_BITWISE_KERNEL_WITH_INVERSE template void BitwiseNotKernel(const Context& dev_ctx, @@ -124,42 +124,42 @@ PD_REGISTER_KERNEL(bitwise_not, int64_t) {} -PD_REGISTER_KERNEL(bitwise_left_shift_arithmetic, - CPU, - ALL_LAYOUT, - phi::BitwiseLeftShiftArithmeticKernel, - uint8_t, - int8_t, - int16_t, - int, - int64_t) {} - -PD_REGISTER_KERNEL(bitwise_left_shift_logic, - CPU, - ALL_LAYOUT, - phi::BitwiseLeftShiftLogicKernel, - uint8_t, - int8_t, - int16_t, - int, - int64_t) {} - -PD_REGISTER_KERNEL(bitwise_right_shift_arithmetic, - CPU, - ALL_LAYOUT, - phi::BitwiseRightShiftArithmeticKernel, - uint8_t, - int8_t, - int16_t, - int, - int64_t) {} - -PD_REGISTER_KERNEL(bitwise_right_shift_logic, - CPU, - ALL_LAYOUT, - phi::BitwiseRightShiftLogicKernel, - uint8_t, - int8_t, - int16_t, - int, - int64_t) {} +// PD_REGISTER_KERNEL(bitwise_left_shift_arithmetic, +// CPU, +// ALL_LAYOUT, +// phi::BitwiseLeftShiftArithmeticKernel, +// uint8_t, +// int8_t, +// int16_t, +// int, +// int64_t) {} + +// PD_REGISTER_KERNEL(bitwise_left_shift_logic, +// CPU, +// ALL_LAYOUT, +// phi::BitwiseLeftShiftLogicKernel, +// uint8_t, +// int8_t, +// int16_t, +// int, +// int64_t) {} + +// PD_REGISTER_KERNEL(bitwise_right_shift_arithmetic, +// CPU, +// ALL_LAYOUT, +// phi::BitwiseRightShiftArithmeticKernel, +// uint8_t, +// int8_t, +// int16_t, +// int, +// int64_t) {} + +// PD_REGISTER_KERNEL(bitwise_right_shift_logic, +// CPU, +// ALL_LAYOUT, +// phi::BitwiseRightShiftLogicKernel, +// uint8_t, +// int8_t, +// int16_t, +// int, +// int64_t) {} diff --git a/paddle/phi/kernels/funcs/bitwise_functors.h b/paddle/phi/kernels/funcs/bitwise_functors.h index 912a0a7be1d98..c5155dea17e60 100644 --- a/paddle/phi/kernels/funcs/bitwise_functors.h +++ b/paddle/phi/kernels/funcs/bitwise_functors.h @@ -47,162 +47,162 @@ struct BitwiseNotFunctor { HOSTDEVICE bool operator()(const bool a) const { return !a; } }; -template -struct BitwiseLeftShiftArithmeticFunctor { - HOSTDEVICE T operator()(const T a, const T b) const { - if (b >= static_cast(sizeof(T) * 8)) return static_cast(0); - if (b < static_cast(0)) return static_cast(0); - return a << b; - } -}; - -template -struct InverseBitwiseLeftShiftArithmeticFunctor { - inline HOSTDEVICE T operator()(const T a, const T b) const { - if (a >= static_cast(sizeof(T) * 8)) return static_cast(0); - if (a < static_cast(0)) return static_cast(0); - return b << a; - } -}; - -template -struct BitwiseLeftShiftLogicFunctor { - HOSTDEVICE T operator()(const T a, const T b) const { - if (b < static_cast(0) || b >= static_cast(sizeof(T) * 8)) - return static_cast(0); - return a << b; - } -}; - -template -struct InverseBitwiseLeftShiftLogicFunctor { - inline HOSTDEVICE T operator()(const T a, const T b) const { - if (a < static_cast(0) || a >= static_cast(sizeof(T) * 8)) - return static_cast(0); - return b << a; - } -}; - -template -struct BitwiseRightShiftArithmeticFunctor { - HOSTDEVICE T operator()(const T a, const T b) const { - if (b < static_cast(0) || b >= static_cast(sizeof(T) * 8)) - return static_cast(-(a >> (sizeof(T) * 8 - 1) & 1)); - return a >> b; - } -}; - -template -struct InverseBitwiseRightShiftArithmeticFunctor { - inline HOSTDEVICE T operator()(const T a, const T b) const { - if (a < static_cast(0) || a >= static_cast(sizeof(T) * 8)) - return static_cast(-(b >> (sizeof(T) * 8 - 1) & 1)); - return b >> a; - } -}; - -template <> -struct BitwiseRightShiftArithmeticFunctor { - HOSTDEVICE uint8_t operator()(const uint8_t a, const uint8_t b) const { - if (b >= static_cast(sizeof(uint8_t) * 8)) - return static_cast(0); - return a >> b; - } -}; - -template <> -struct InverseBitwiseRightShiftArithmeticFunctor { - inline HOSTDEVICE uint8_t operator()(const uint8_t a, const uint8_t b) const { - if (a >= static_cast(sizeof(uint8_t) * 8)) - return static_cast(0); - return b >> a; - } -}; - -template -struct BitwiseRightShiftLogicFunctor { - HOSTDEVICE T operator()(const T a, const T b) const { - if (b >= static_cast(sizeof(T) * 8)) return static_cast(0); - return a >> b; - } -}; - -template -struct InverseBitwiseRightShiftLogicFunctor { - inline HOSTDEVICE T operator()(const T a, const T b) const { - if (a >= static_cast(sizeof(T) * 8)) return static_cast(0); - return b >> a; - } -}; - -template -HOSTDEVICE T logic_shift_func(const T a, const T b) { - if (b < static_cast(0) || b >= static_cast(sizeof(T) * 8)) - return static_cast(0); - T t = static_cast(sizeof(T) * 8 - 1); - T mask = (((a >> t) << t) >> b) << 1; - return (a >> b) ^ mask; -} - -// signed int8 -template <> -struct BitwiseRightShiftLogicFunctor { - HOSTDEVICE int8_t operator()(const int8_t a, const int8_t b) const { - return logic_shift_func(a, b); - } -}; - -template <> -struct InverseBitwiseRightShiftLogicFunctor { - inline HOSTDEVICE int8_t operator()(const int8_t a, const int8_t b) const { - return logic_shift_func(b, a); - } -}; - -// signed int16 -template <> -struct BitwiseRightShiftLogicFunctor { - HOSTDEVICE int16_t operator()(const int16_t a, const int16_t b) const { - return logic_shift_func(a, b); - } -}; - -template <> -struct InverseBitwiseRightShiftLogicFunctor { - inline HOSTDEVICE int16_t operator()(const int16_t a, const int16_t b) const { - return logic_shift_func(b, a); - } -}; - -// signed int32 -template <> -struct BitwiseRightShiftLogicFunctor { - HOSTDEVICE int operator()(const int a, const int b) const { - return logic_shift_func(a, b); - } -}; - -template <> -struct InverseBitwiseRightShiftLogicFunctor { - inline HOSTDEVICE int operator()(const int a, const int b) const { - return logic_shift_func(b, a); - } -}; - -// signed int64 -template <> -struct BitwiseRightShiftLogicFunctor { - HOSTDEVICE int64_t operator()(const int64_t a, const int64_t b) const { - return logic_shift_func(a, b); - } -}; - -template <> -struct InverseBitwiseRightShiftLogicFunctor { - inline HOSTDEVICE int64_t operator()(const int64_t a, const int64_t b) const { - return logic_shift_func(b, a); - } -}; +// template +// struct BitwiseLeftShiftArithmeticFunctor { +// HOSTDEVICE T operator()(const T a, const T b) const { +// if (b >= static_cast(sizeof(T) * 8)) return static_cast(0); +// if (b < static_cast(0)) return static_cast(0); +// return a << b; +// } +// }; + +// template +// struct InverseBitwiseLeftShiftArithmeticFunctor { +// inline HOSTDEVICE T operator()(const T a, const T b) const { +// if (a >= static_cast(sizeof(T) * 8)) return static_cast(0); +// if (a < static_cast(0)) return static_cast(0); +// return b << a; +// } +// }; + +// template +// struct BitwiseLeftShiftLogicFunctor { +// HOSTDEVICE T operator()(const T a, const T b) const { +// if (b < static_cast(0) || b >= static_cast(sizeof(T) * 8)) +// return static_cast(0); +// return a << b; +// } +// }; + +// template +// struct InverseBitwiseLeftShiftLogicFunctor { +// inline HOSTDEVICE T operator()(const T a, const T b) const { +// if (a < static_cast(0) || a >= static_cast(sizeof(T) * 8)) +// return static_cast(0); +// return b << a; +// } +// }; + +// template +// struct BitwiseRightShiftArithmeticFunctor { +// HOSTDEVICE T operator()(const T a, const T b) const { +// if (b < static_cast(0) || b >= static_cast(sizeof(T) * 8)) +// return static_cast(-(a >> (sizeof(T) * 8 - 1) & 1)); +// return a >> b; +// } +// }; + +// template +// struct InverseBitwiseRightShiftArithmeticFunctor { +// inline HOSTDEVICE T operator()(const T a, const T b) const { +// if (a < static_cast(0) || a >= static_cast(sizeof(T) * 8)) +// return static_cast(-(b >> (sizeof(T) * 8 - 1) & 1)); +// return b >> a; +// } +// }; + +// template <> +// struct BitwiseRightShiftArithmeticFunctor { +// HOSTDEVICE uint8_t operator()(const uint8_t a, const uint8_t b) const { +// if (b >= static_cast(sizeof(uint8_t) * 8)) +// return static_cast(0); +// return a >> b; +// } +// }; + +// template <> +// struct InverseBitwiseRightShiftArithmeticFunctor { +// inline HOSTDEVICE uint8_t operator()(const uint8_t a, const uint8_t b) const { +// if (a >= static_cast(sizeof(uint8_t) * 8)) +// return static_cast(0); +// return b >> a; +// } +// }; + +// template +// struct BitwiseRightShiftLogicFunctor { +// HOSTDEVICE T operator()(const T a, const T b) const { +// if (b >= static_cast(sizeof(T) * 8)) return static_cast(0); +// return a >> b; +// } +// }; + +// template +// struct InverseBitwiseRightShiftLogicFunctor { +// inline HOSTDEVICE T operator()(const T a, const T b) const { +// if (a >= static_cast(sizeof(T) * 8)) return static_cast(0); +// return b >> a; +// } +// }; + +// template +// HOSTDEVICE T logic_shift_func(const T a, const T b) { +// if (b < static_cast(0) || b >= static_cast(sizeof(T) * 8)) +// return static_cast(0); +// T t = static_cast(sizeof(T) * 8 - 1); +// T mask = (((a >> t) << t) >> b) << 1; +// return (a >> b) ^ mask; +// } + +// // signed int8 +// template <> +// struct BitwiseRightShiftLogicFunctor { +// HOSTDEVICE int8_t operator()(const int8_t a, const int8_t b) const { +// return logic_shift_func(a, b); +// } +// }; + +// template <> +// struct InverseBitwiseRightShiftLogicFunctor { +// inline HOSTDEVICE int8_t operator()(const int8_t a, const int8_t b) const { +// return logic_shift_func(b, a); +// } +// }; + +// // signed int16 +// template <> +// struct BitwiseRightShiftLogicFunctor { +// HOSTDEVICE int16_t operator()(const int16_t a, const int16_t b) const { +// return logic_shift_func(a, b); +// } +// }; + +// template <> +// struct InverseBitwiseRightShiftLogicFunctor { +// inline HOSTDEVICE int16_t operator()(const int16_t a, const int16_t b) const { +// return logic_shift_func(b, a); +// } +// }; + +// // signed int32 +// template <> +// struct BitwiseRightShiftLogicFunctor { +// HOSTDEVICE int operator()(const int a, const int b) const { +// return logic_shift_func(a, b); +// } +// }; + +// template <> +// struct InverseBitwiseRightShiftLogicFunctor { +// inline HOSTDEVICE int operator()(const int a, const int b) const { +// return logic_shift_func(b, a); +// } +// }; + +// // signed int64 +// template <> +// struct BitwiseRightShiftLogicFunctor { +// HOSTDEVICE int64_t operator()(const int64_t a, const int64_t b) const { +// return logic_shift_func(a, b); +// } +// }; + +// template <> +// struct InverseBitwiseRightShiftLogicFunctor { +// inline HOSTDEVICE int64_t operator()(const int64_t a, const int64_t b) const { +// return logic_shift_func(b, a); +// } +// }; } // namespace funcs diff --git a/paddle/phi/kernels/kps/bitwise_kernel.cu b/paddle/phi/kernels/kps/bitwise_kernel.cu index 7dd3225f20476..3478f570f3a9b 100644 --- a/paddle/phi/kernels/kps/bitwise_kernel.cu +++ b/paddle/phi/kernels/kps/bitwise_kernel.cu @@ -41,10 +41,10 @@ namespace phi { DEFINE_BITWISE_KERNEL(And) DEFINE_BITWISE_KERNEL(Or) DEFINE_BITWISE_KERNEL(Xor) -DEFINE_BITWISE_KERNEL(LeftShiftArithmetic) -DEFINE_BITWISE_KERNEL(LeftShiftLogic) -DEFINE_BITWISE_KERNEL(RightShiftArithmetic) -DEFINE_BITWISE_KERNEL(RightShiftLogic) +// DEFINE_BITWISE_KERNEL(LeftShiftArithmetic) +// DEFINE_BITWISE_KERNEL(LeftShiftLogic) +// DEFINE_BITWISE_KERNEL(RightShiftArithmetic) +// DEFINE_BITWISE_KERNEL(RightShiftLogic) #undef DEFINE_BITWISE_KERNEL template @@ -116,44 +116,44 @@ PD_REGISTER_KERNEL(bitwise_not, int, int64_t) {} -PD_REGISTER_KERNEL(bitwise_left_shift_arithmetic, - KPS, - ALL_LAYOUT, - phi::BitwiseLeftShiftArithmeticKernel, - uint8_t, - int8_t, - int16_t, - int, - int64_t) {} - -PD_REGISTER_KERNEL(bitwise_left_shift_logic, - KPS, - ALL_LAYOUT, - phi::BitwiseLeftShiftLogicKernel, - uint8_t, - int8_t, - int16_t, - int, - int64_t) {} - -PD_REGISTER_KERNEL(bitwise_right_shift_arithmetic, - KPS, - ALL_LAYOUT, - phi::BitwiseRightShiftArithmeticKernel, - uint8_t, - int8_t, - int16_t, - int, - int64_t) {} - -PD_REGISTER_KERNEL(bitwise_right_shift_logic, - KPS, - ALL_LAYOUT, - phi::BitwiseRightShiftLogicKernel, - uint8_t, - int8_t, - int16_t, - int, - int64_t) {} +// PD_REGISTER_KERNEL(bitwise_left_shift_arithmetic, +// KPS, +// ALL_LAYOUT, +// phi::BitwiseLeftShiftArithmeticKernel, +// uint8_t, +// int8_t, +// int16_t, +// int, +// int64_t) {} + +// PD_REGISTER_KERNEL(bitwise_left_shift_logic, +// KPS, +// ALL_LAYOUT, +// phi::BitwiseLeftShiftLogicKernel, +// uint8_t, +// int8_t, +// int16_t, +// int, +// int64_t) {} + +// PD_REGISTER_KERNEL(bitwise_right_shift_arithmetic, +// KPS, +// ALL_LAYOUT, +// phi::BitwiseRightShiftArithmeticKernel, +// uint8_t, +// int8_t, +// int16_t, +// int, +// int64_t) {} + +// PD_REGISTER_KERNEL(bitwise_right_shift_logic, +// KPS, +// ALL_LAYOUT, +// phi::BitwiseRightShiftLogicKernel, +// uint8_t, +// int8_t, +// int16_t, +// int, +// int64_t) {} #endif diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 2f222c2c906e7..c0f4300f61030 100644 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -357,6 +357,10 @@ atan_, atanh, atanh_, + bitwise_left_shift, + bitwise_left_shift_, + bitwise_right_shift, + bitwise_right_shift_, broadcast_shape, ceil, clip, diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 8ed362f04b5f0..c542f8c0bbd56 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -7079,141 +7079,141 @@ def _bitwise_op(op_name, x, y, out=None, name=None): return out -def bitwise_left_shift(x, y, is_arithmetic=True, out=None, name=None): - r""" - Apply ``bitwise_left_shift`` on Tensor ``X`` and ``Y`` . - .. math:: - Out = X \ll Y - .. note:: - ``paddle.bitwise_left_shift`` supports broadcasting. If you want know more about broadcasting, please refer to please refer to `Introduction to Tensor`_ . - .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor. - Args: - x (Tensor): Input Tensor of ``bitwise_left_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. - y (Tensor): Input Tensor of ``bitwise_left_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. - name (str, optional): The default value is None. Normally there is no need for - user to set this property. For more information, please refer to :ref:`api_guide_Name`. - Returns: - Tensor: Result of ``bitwise_left_shift`` . It is a N-D Tensor with the same data type of input Tensor. - Examples: - .. code-block:: python - >>> import paddle - >>> x= paddle.to_tensor([[1,2,4,8],[16,17,32,65]]) - >>> y = paddle.to_tensor([[1,2,3,4,], [2,3,2,1]]) - >>> paddle.bitwise_left_shift(x, y) - Tensor(shape=[2, 4], dtype=int64, place=Place(gpu:0), stop_gradient=True, - [[2 , 8 , 32 , 128], - [64 , 136, 128, 130]]) - """ - if in_dynamic_mode() and out is None: - if is_arithmetic: - return _C_ops.bitwise_left_shift_arithmetic(x, y) - else: - return _C_ops.bitwise_left_shift_logic(x, y) - if is_arithmetic: - return _bitwise_op( - op_name="bitwise_left_shift_arithmetic", - x=x, - y=y, - name=name, - out=out, - ) - else: - return _bitwise_op( - op_name="bitwise_left_shift_logic", - x=x, - y=y, - name=name, - out=out, - ) - - -@inplace_apis_in_dygraph_only -def bitwise_left_shift_(x, y, is_arithmetic=True, out=None, name=None): - r""" - Inplace version of ``bitwise_left_shift`` API, the output Tensor will be inplaced with input ``x``. - Please refer to :ref:`api_paddle_bitwise_left_shift`. - """ - out_shape = broadcast_shape(x.shape, y.shape) - if out_shape != x.shape: - raise ValueError( - "The shape of broadcast output {} is different from that of inplace tensor {} in the Inplace operation.".format( - out_shape, x.shape - ) - ) - if in_dynamic_or_pir_mode(): - if is_arithmetic: - return _C_ops.bitwise_left_shift_arithmetic_(x, y) - else: - return _C_ops.bitwise_left_shift_logic_(x, y) - - -def bitwise_right_shift(x, y, is_arithmetic=True, out=None, name=None): - r""" - Apply ``bitwise_right_shift`` on Tensor ``X`` and ``Y`` . - .. math:: - Out = X \gg Y - .. note:: - ``paddle.bitwise_right_shift`` supports broadcasting. If you want know more about broadcasting, please refer to please refer to `Introduction to Tensor`_ . - .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor. - Args: - x (Tensor): Input Tensor of ``bitwise_right_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. - y (Tensor): Input Tensor of ``bitwise_right_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. - name (str, optional): The default value is None. Normally there is no need for - user to set this property. For more information, please refer to :ref:`api_guide_Name`. - Returns: - Tensor: Result of ``bitwise_right_shift`` . It is a N-D Tensor with the same data type of input Tensor. - Examples: - .. code-block:: python - >>> import paddle - >>> x= paddle.to_tensor([[10,20,40,80],[16,17,32,65]]) - >>> y = paddle.to_tensor([[1,2,3,4,], [2,3,2,1]]) - >>> paddle.bitwise_right_shift(x, y) - Tensor(shape=[2, 4], dtype=int64, place=Place(gpu:0), stop_gradient=True, - [[5 , 5 , 5 , 5 ], - [4 , 2 , 8 , 32]]) - """ - if in_dynamic_mode() and out is None: - if is_arithmetic: - return _C_ops.bitwise_right_shift_arithmetic(x, y) - else: - return _C_ops.bitwise_right_shift_logic(x, y) - if is_arithmetic: - return _bitwise_op( - op_name="bitwise_right_shift_arithmetic", - x=x, - y=y, - name=name, - out=out, - ) - else: - return _bitwise_op( - op_name="bitwise_right_shift_logic", - x=x, - y=y, - name=name, - out=out, - ) - - -@inplace_apis_in_dygraph_only -def bitwise_right_shift_(x, y, is_arithmetic=True, out=None, name=None): - r""" - Inplace version of ``bitwise_right_shift`` API, the output Tensor will be inplaced with input ``x``. - Please refer to :ref:`api_paddle_bitwise_left_shift`. - """ - out_shape = broadcast_shape(x.shape, y.shape) - if out_shape != x.shape: - raise ValueError( - "The shape of broadcast output {} is different from that of inplace tensor {} in the Inplace operation.".format( - out_shape, x.shape - ) - ) - - if in_dynamic_or_pir_mode(): - if is_arithmetic: - return _C_ops.bitwise_right_shift_arithmetic_(x, y) - else: - return _C_ops.bitwise_right_shift_logic_(x, y) +# def bitwise_left_shift(x, y, is_arithmetic=True, out=None, name=None): +# r""" +# Apply ``bitwise_left_shift`` on Tensor ``X`` and ``Y`` . +# .. math:: +# Out = X \ll Y +# .. note:: +# ``paddle.bitwise_left_shift`` supports broadcasting. If you want know more about broadcasting, please refer to please refer to `Introduction to Tensor`_ . +# .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor. +# Args: +# x (Tensor): Input Tensor of ``bitwise_left_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. +# y (Tensor): Input Tensor of ``bitwise_left_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. +# name (str, optional): The default value is None. Normally there is no need for +# user to set this property. For more information, please refer to :ref:`api_guide_Name`. +# Returns: +# Tensor: Result of ``bitwise_left_shift`` . It is a N-D Tensor with the same data type of input Tensor. +# Examples: +# .. code-block:: python +# >>> import paddle +# >>> x= paddle.to_tensor([[1,2,4,8],[16,17,32,65]]) +# >>> y = paddle.to_tensor([[1,2,3,4,], [2,3,2,1]]) +# >>> paddle.bitwise_left_shift(x, y) +# Tensor(shape=[2, 4], dtype=int64, place=Place(gpu:0), stop_gradient=True, +# [[2 , 8 , 32 , 128], +# [64 , 136, 128, 130]]) +# """ +# if in_dynamic_mode() and out is None: +# if is_arithmetic: +# return _C_ops.bitwise_left_shift_arithmetic(x, y) +# else: +# return _C_ops.bitwise_left_shift_logic(x, y) +# if is_arithmetic: +# return _bitwise_op( +# op_name="bitwise_left_shift_arithmetic", +# x=x, +# y=y, +# name=name, +# out=out, +# ) +# else: +# return _bitwise_op( +# op_name="bitwise_left_shift_logic", +# x=x, +# y=y, +# name=name, +# out=out, +# ) + + +# @inplace_apis_in_dygraph_only +# def bitwise_left_shift_(x, y, is_arithmetic=True, out=None, name=None): +# r""" +# Inplace version of ``bitwise_left_shift`` API, the output Tensor will be inplaced with input ``x``. +# Please refer to :ref:`api_paddle_bitwise_left_shift`. +# """ +# out_shape = broadcast_shape(x.shape, y.shape) +# if out_shape != x.shape: +# raise ValueError( +# "The shape of broadcast output {} is different from that of inplace tensor {} in the Inplace operation.".format( +# out_shape, x.shape +# ) +# ) +# if in_dynamic_or_pir_mode(): +# if is_arithmetic: +# return _C_ops.bitwise_left_shift_arithmetic_(x, y) +# else: +# return _C_ops.bitwise_left_shift_logic_(x, y) + + +# def bitwise_right_shift(x, y, is_arithmetic=True, out=None, name=None): +# r""" +# Apply ``bitwise_right_shift`` on Tensor ``X`` and ``Y`` . +# .. math:: +# Out = X \gg Y +# .. note:: +# ``paddle.bitwise_right_shift`` supports broadcasting. If you want know more about broadcasting, please refer to please refer to `Introduction to Tensor`_ . +# .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor. +# Args: +# x (Tensor): Input Tensor of ``bitwise_right_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. +# y (Tensor): Input Tensor of ``bitwise_right_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. +# name (str, optional): The default value is None. Normally there is no need for +# user to set this property. For more information, please refer to :ref:`api_guide_Name`. +# Returns: +# Tensor: Result of ``bitwise_right_shift`` . It is a N-D Tensor with the same data type of input Tensor. +# Examples: +# .. code-block:: python +# >>> import paddle +# >>> x= paddle.to_tensor([[10,20,40,80],[16,17,32,65]]) +# >>> y = paddle.to_tensor([[1,2,3,4,], [2,3,2,1]]) +# >>> paddle.bitwise_right_shift(x, y) +# Tensor(shape=[2, 4], dtype=int64, place=Place(gpu:0), stop_gradient=True, +# [[5 , 5 , 5 , 5 ], +# [4 , 2 , 8 , 32]]) +# """ +# if in_dynamic_mode() and out is None: +# if is_arithmetic: +# return _C_ops.bitwise_right_shift_arithmetic(x, y) +# else: +# return _C_ops.bitwise_right_shift_logic(x, y) +# if is_arithmetic: +# return _bitwise_op( +# op_name="bitwise_right_shift_arithmetic", +# x=x, +# y=y, +# name=name, +# out=out, +# ) +# else: +# return _bitwise_op( +# op_name="bitwise_right_shift_logic", +# x=x, +# y=y, +# name=name, +# out=out, +# ) + + +# @inplace_apis_in_dygraph_only +# def bitwise_right_shift_(x, y, is_arithmetic=True, out=None, name=None): +# r""" +# Inplace version of ``bitwise_right_shift`` API, the output Tensor will be inplaced with input ``x``. +# Please refer to :ref:`api_paddle_bitwise_left_shift`. +# """ +# out_shape = broadcast_shape(x.shape, y.shape) +# if out_shape != x.shape: +# raise ValueError( +# "The shape of broadcast output {} is different from that of inplace tensor {} in the Inplace operation.".format( +# out_shape, x.shape +# ) +# ) + +# if in_dynamic_or_pir_mode(): +# if is_arithmetic: +# return _C_ops.bitwise_right_shift_arithmetic_(x, y) +# else: +# return _C_ops.bitwise_right_shift_logic_(x, y) def hypot(x, y, name=None): diff --git a/test/legacy_test/test_bitwise_shift_op.py b/test/legacy_test/test_bitwise_shift_op.py index 9b957891de7b1..b2dca38a6763a 100644 --- a/test/legacy_test/test_bitwise_shift_op.py +++ b/test/legacy_test/test_bitwise_shift_op.py @@ -1,414 +1,414 @@ -# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import unittest - -import numpy as np - -import paddle - -_SIGNED_TO_UNSIGNED_TABLE = { - "int8": "uint8", - "int16": "uint16", - "int32": "uint32", - "int64": "uint64", -} - -_UNSIGNED_TO_SIGNED_TABLE = { - "uint8": "int8", - "uint16": "int16", - "uint32": "int32", - "uint64": "int64", -} - -_UNSIGNED_LIST = ['uint8', 'uint16', 'uint32', 'uint64'] - - -def ref_left_shift_arithmetic(x, y): - out = np.left_shift(x, y) - return out - - -def ref_left_shift_logical(x, y): - out = np.left_shift(x, y) - return out - - -def ref_right_shift_arithmetic(x, y): - return np.right_shift(x, y) - - -def ref_right_shift_logical(x, y): - if str(x.dtype) in _UNSIGNED_LIST: - return np.right_shift(x, y) - else: - orig_dtype = x.dtype - unsigned_dtype = _SIGNED_TO_UNSIGNED_TABLE[str(orig_dtype)] - x = x.astype(unsigned_dtype) - y = y.astype(unsigned_dtype) - res = np.right_shift(x, y) - return res.astype(orig_dtype) - - -class TestBitwiseLeftShiftAPI(unittest.TestCase): - def setUp(self): - self.init_input() - self.place = ( - paddle.CUDAPlace(0) - if paddle.is_compiled_with_cuda() - else paddle.CPUPlace() - ) - - def init_input(self): - self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') - self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') - - def test_static_api_arithmetic(self): - paddle.enable_static() - with paddle.static.program_guard(paddle.static.Program()): - x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) - y = paddle.static.data('y', self.y.shape, dtype=self.y.dtype) - out = paddle.bitwise_left_shift( - x, - y, - ) - exe = paddle.static.Executor(self.place) - res = exe.run(feed={'x': self.x, 'y': self.y}, fetch_list=[out]) - out_ref = ref_left_shift_arithmetic(self.x, self.y) - np.testing.assert_allclose(out_ref, res[0]) - - def test_dygraph_api_arithmetic(self): - paddle.disable_static() - x = paddle.to_tensor(self.x) - y = paddle.to_tensor(self.y) - out = paddle.bitwise_left_shift( - x, - y, - ) - out_ref = ref_left_shift_arithmetic(self.x, self.y) - np.testing.assert_allclose(out_ref, out.numpy()) - paddle.enable_static() - - def test_static_api_logical(self): - paddle.enable_static() - with paddle.static.program_guard(paddle.static.Program()): - x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) - y = paddle.static.data('y', self.y.shape, dtype=self.y.dtype) - out = paddle.bitwise_left_shift(x, y, False) - exe = paddle.static.Executor(self.place) - res = exe.run(feed={'x': self.x, 'y': self.y}, fetch_list=[out]) - out_ref = ref_left_shift_logical(self.x, self.y) - np.testing.assert_allclose(out_ref, res[0]) +# # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# # +# # Licensed under the Apache License, Version 2.0 (the "License"); +# # you may not use this file except in compliance with the License. +# # You may obtain a copy of the License at +# # +# # http://www.apache.org/licenses/LICENSE-2.0 +# # +# # Unless required by applicable law or agreed to in writing, software +# # distributed under the License is distributed on an "AS IS" BASIS, +# # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# # See the License for the specific language governing permissions and +# # limitations under the License. + +# import unittest + +# import numpy as np + +# import paddle + +# _SIGNED_TO_UNSIGNED_TABLE = { +# "int8": "uint8", +# "int16": "uint16", +# "int32": "uint32", +# "int64": "uint64", +# } + +# _UNSIGNED_TO_SIGNED_TABLE = { +# "uint8": "int8", +# "uint16": "int16", +# "uint32": "int32", +# "uint64": "int64", +# } + +# _UNSIGNED_LIST = ['uint8', 'uint16', 'uint32', 'uint64'] + + +# def ref_left_shift_arithmetic(x, y): +# out = np.left_shift(x, y) +# return out + + +# def ref_left_shift_logical(x, y): +# out = np.left_shift(x, y) +# return out + + +# def ref_right_shift_arithmetic(x, y): +# return np.right_shift(x, y) + + +# def ref_right_shift_logical(x, y): +# if str(x.dtype) in _UNSIGNED_LIST: +# return np.right_shift(x, y) +# else: +# orig_dtype = x.dtype +# unsigned_dtype = _SIGNED_TO_UNSIGNED_TABLE[str(orig_dtype)] +# x = x.astype(unsigned_dtype) +# y = y.astype(unsigned_dtype) +# res = np.right_shift(x, y) +# return res.astype(orig_dtype) + + +# class TestBitwiseLeftShiftAPI(unittest.TestCase): +# def setUp(self): +# self.init_input() +# self.place = ( +# paddle.CUDAPlace(0) +# if paddle.is_compiled_with_cuda() +# else paddle.CPUPlace() +# ) + +# def init_input(self): +# self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') +# self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') + +# def test_static_api_arithmetic(self): +# paddle.enable_static() +# with paddle.static.program_guard(paddle.static.Program()): +# x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) +# y = paddle.static.data('y', self.y.shape, dtype=self.y.dtype) +# out = paddle.bitwise_left_shift( +# x, +# y, +# ) +# exe = paddle.static.Executor(self.place) +# res = exe.run(feed={'x': self.x, 'y': self.y}, fetch_list=[out]) +# out_ref = ref_left_shift_arithmetic(self.x, self.y) +# np.testing.assert_allclose(out_ref, res[0]) + +# def test_dygraph_api_arithmetic(self): +# paddle.disable_static() +# x = paddle.to_tensor(self.x) +# y = paddle.to_tensor(self.y) +# out = paddle.bitwise_left_shift( +# x, +# y, +# ) +# out_ref = ref_left_shift_arithmetic(self.x, self.y) +# np.testing.assert_allclose(out_ref, out.numpy()) +# paddle.enable_static() + +# def test_static_api_logical(self): +# paddle.enable_static() +# with paddle.static.program_guard(paddle.static.Program()): +# x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) +# y = paddle.static.data('y', self.y.shape, dtype=self.y.dtype) +# out = paddle.bitwise_left_shift(x, y, False) +# exe = paddle.static.Executor(self.place) +# res = exe.run(feed={'x': self.x, 'y': self.y}, fetch_list=[out]) +# out_ref = ref_left_shift_logical(self.x, self.y) +# np.testing.assert_allclose(out_ref, res[0]) - def test_dygraph_api_logical(self): - paddle.disable_static() - x = paddle.to_tensor(self.x) - y = paddle.to_tensor(self.y) - out = paddle.bitwise_left_shift(x, y, False) - out_ref = ref_left_shift_logical(self.x, self.y) - np.testing.assert_allclose(out_ref, out.numpy()) - paddle.enable_static() +# def test_dygraph_api_logical(self): +# paddle.disable_static() +# x = paddle.to_tensor(self.x) +# y = paddle.to_tensor(self.y) +# out = paddle.bitwise_left_shift(x, y, False) +# out_ref = ref_left_shift_logical(self.x, self.y) +# np.testing.assert_allclose(out_ref, out.numpy()) +# paddle.enable_static() -class TestBitwiseLeftShiftAPI_UINT8(TestBitwiseLeftShiftAPI): - def init_input(self): - self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') - self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') +# class TestBitwiseLeftShiftAPI_UINT8(TestBitwiseLeftShiftAPI): +# def init_input(self): +# self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') +# self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') -class TestBitwiseLeftShiftAPI_UINT8_broadcast1(TestBitwiseLeftShiftAPI): - def init_input(self): - self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') - self.y = np.random.randint(0, 256, [3]).astype('uint8') +# class TestBitwiseLeftShiftAPI_UINT8_broadcast1(TestBitwiseLeftShiftAPI): +# def init_input(self): +# self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') +# self.y = np.random.randint(0, 256, [3]).astype('uint8') -class TestBitwiseLeftShiftAPI_UINT8_broadcast2(TestBitwiseLeftShiftAPI): - def init_input(self): - self.x = np.random.randint(0, 256, [3]).astype('uint8') - self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') +# class TestBitwiseLeftShiftAPI_UINT8_broadcast2(TestBitwiseLeftShiftAPI): +# def init_input(self): +# self.x = np.random.randint(0, 256, [3]).astype('uint8') +# self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') -class TestBitwiseLeftShiftAPI_INT8(TestBitwiseLeftShiftAPI): - def init_input(self): - self.x = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') - self.y = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') +# class TestBitwiseLeftShiftAPI_INT8(TestBitwiseLeftShiftAPI): +# def init_input(self): +# self.x = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') +# self.y = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') -class TestBitwiseLeftShiftAPI_INT8_broadcast1(TestBitwiseLeftShiftAPI): - def init_input(self): - self.x = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') - self.y = np.random.randint(-(2**7), 2**7, [3]).astype('int8') - - -class TestBitwiseLeftShiftAPI_INT8_broadcast2(TestBitwiseLeftShiftAPI): - def init_input(self): - self.x = np.random.randint(-(2**7), 2**7, [3]).astype('int8') - self.y = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') - - -class TestBitwiseLeftShiftAPI_INT16(TestBitwiseLeftShiftAPI): - def init_input(self): - self.x = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') - self.y = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') - - -class TestBitwiseLeftShiftAPI_INT16_broadcast1(TestBitwiseLeftShiftAPI): - def init_input(self): - self.x = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') - self.y = np.random.randint(-(2**15), 2**15, [3]).astype('int16') - - -class TestBitwiseLeftShiftAPI_INT16_broadcast2(TestBitwiseLeftShiftAPI): - def init_input(self): - self.x = np.random.randint(-(2**15), 2**15, [3]).astype('int16') - self.y = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') - - -class TestBitwiseLeftShiftAPI_INT32(TestBitwiseLeftShiftAPI): - def init_input(self): - self.x = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') - self.y = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') - - -class TestBitwiseLeftShiftAPI_INT32_broadcast1(TestBitwiseLeftShiftAPI): - def init_input(self): - self.x = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') - self.y = np.random.randint(-(2**31), 2**31, [3]).astype('int32') - - -class TestBitwiseLeftShiftAPI_INT32_broadcast2(TestBitwiseLeftShiftAPI): - def init_input(self): - self.x = np.random.randint(-(2**31), 2**31, [3]).astype('int32') - self.y = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') - - -class TestBitwiseLeftShiftAPI_INT64(TestBitwiseLeftShiftAPI): - def init_input(self): - self.x = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) - self.y = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) - - -class TestBitwiseLeftShiftAPI_INT64_broadcast1(TestBitwiseLeftShiftAPI): - def init_input(self): - self.x = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) - self.y = np.random.randint(-(2**63), 2**63, [3], dtype=np.int64) +# class TestBitwiseLeftShiftAPI_INT8_broadcast1(TestBitwiseLeftShiftAPI): +# def init_input(self): +# self.x = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') +# self.y = np.random.randint(-(2**7), 2**7, [3]).astype('int8') + + +# class TestBitwiseLeftShiftAPI_INT8_broadcast2(TestBitwiseLeftShiftAPI): +# def init_input(self): +# self.x = np.random.randint(-(2**7), 2**7, [3]).astype('int8') +# self.y = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') + + +# class TestBitwiseLeftShiftAPI_INT16(TestBitwiseLeftShiftAPI): +# def init_input(self): +# self.x = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') +# self.y = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') + + +# class TestBitwiseLeftShiftAPI_INT16_broadcast1(TestBitwiseLeftShiftAPI): +# def init_input(self): +# self.x = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') +# self.y = np.random.randint(-(2**15), 2**15, [3]).astype('int16') + + +# class TestBitwiseLeftShiftAPI_INT16_broadcast2(TestBitwiseLeftShiftAPI): +# def init_input(self): +# self.x = np.random.randint(-(2**15), 2**15, [3]).astype('int16') +# self.y = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') + + +# class TestBitwiseLeftShiftAPI_INT32(TestBitwiseLeftShiftAPI): +# def init_input(self): +# self.x = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') +# self.y = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') + + +# class TestBitwiseLeftShiftAPI_INT32_broadcast1(TestBitwiseLeftShiftAPI): +# def init_input(self): +# self.x = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') +# self.y = np.random.randint(-(2**31), 2**31, [3]).astype('int32') + + +# class TestBitwiseLeftShiftAPI_INT32_broadcast2(TestBitwiseLeftShiftAPI): +# def init_input(self): +# self.x = np.random.randint(-(2**31), 2**31, [3]).astype('int32') +# self.y = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') + + +# class TestBitwiseLeftShiftAPI_INT64(TestBitwiseLeftShiftAPI): +# def init_input(self): +# self.x = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) +# self.y = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) + + +# class TestBitwiseLeftShiftAPI_INT64_broadcast1(TestBitwiseLeftShiftAPI): +# def init_input(self): +# self.x = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) +# self.y = np.random.randint(-(2**63), 2**63, [3], dtype=np.int64) -class TestBitwiseLeftShiftAPI_INT64_broadcast2(TestBitwiseLeftShiftAPI): - def init_input(self): - self.x = np.random.randint(-(2**63), 2**63, [3], dtype=np.int64) - self.y = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) +# class TestBitwiseLeftShiftAPI_INT64_broadcast2(TestBitwiseLeftShiftAPI): +# def init_input(self): +# self.x = np.random.randint(-(2**63), 2**63, [3], dtype=np.int64) +# self.y = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) -class TestBitwiseLeftShiftAPI_special_case1(TestBitwiseLeftShiftAPI): - def init_input(self): - self.x = np.array([0b11111111], dtype='int8') - self.y = np.array([1], dtype='int8') +# class TestBitwiseLeftShiftAPI_special_case1(TestBitwiseLeftShiftAPI): +# def init_input(self): +# self.x = np.array([0b11111111], dtype='int8') +# self.y = np.array([1], dtype='int8') -class TestBitwiseLeftShiftAPI_special_case2(TestBitwiseLeftShiftAPI): - def init_input(self): - self.x = np.array([0b11111111], dtype='int8') - self.y = np.array([10], dtype='int8') +# class TestBitwiseLeftShiftAPI_special_case2(TestBitwiseLeftShiftAPI): +# def init_input(self): +# self.x = np.array([0b11111111], dtype='int8') +# self.y = np.array([10], dtype='int8') -class TestBitwiseLeftShiftAPI_special_case3(TestBitwiseLeftShiftAPI): - def init_input(self): - self.x = np.array([0b11111111], dtype='uint8') - self.y = np.array([1], dtype='uint8') +# class TestBitwiseLeftShiftAPI_special_case3(TestBitwiseLeftShiftAPI): +# def init_input(self): +# self.x = np.array([0b11111111], dtype='uint8') +# self.y = np.array([1], dtype='uint8') -class TestBitwiseLeftShiftAPI_special_case4(TestBitwiseLeftShiftAPI): - def init_input(self): - self.x = np.array([0b11111111], dtype='uint8') - self.y = np.array([10], dtype='uint8') +# class TestBitwiseLeftShiftAPI_special_case4(TestBitwiseLeftShiftAPI): +# def init_input(self): +# self.x = np.array([0b11111111], dtype='uint8') +# self.y = np.array([10], dtype='uint8') -class TestBitwiseRightShiftAPI(unittest.TestCase): - def setUp(self): - self.init_input() - self.place = ( - paddle.CUDAPlace(0) - if paddle.is_compiled_with_cuda() - else paddle.CPUPlace() - ) +# class TestBitwiseRightShiftAPI(unittest.TestCase): +# def setUp(self): +# self.init_input() +# self.place = ( +# paddle.CUDAPlace(0) +# if paddle.is_compiled_with_cuda() +# else paddle.CPUPlace() +# ) - def init_input(self): - self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') - self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') +# def init_input(self): +# self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') +# self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') - def test_static_api_arithmetic(self): - paddle.enable_static() - with paddle.static.program_guard(paddle.static.Program()): - x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) - y = paddle.static.data('y', self.y.shape, dtype=self.y.dtype) - out = paddle.bitwise_right_shift( - x, - y, - ) - exe = paddle.static.Executor(self.place) - res = exe.run(feed={'x': self.x, 'y': self.y}, fetch_list=[out]) - out_ref = ref_right_shift_arithmetic(self.x, self.y) - np.testing.assert_allclose(out_ref, res[0]) +# def test_static_api_arithmetic(self): +# paddle.enable_static() +# with paddle.static.program_guard(paddle.static.Program()): +# x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) +# y = paddle.static.data('y', self.y.shape, dtype=self.y.dtype) +# out = paddle.bitwise_right_shift( +# x, +# y, +# ) +# exe = paddle.static.Executor(self.place) +# res = exe.run(feed={'x': self.x, 'y': self.y}, fetch_list=[out]) +# out_ref = ref_right_shift_arithmetic(self.x, self.y) +# np.testing.assert_allclose(out_ref, res[0]) - def test_dygraph_api_arithmetic(self): - paddle.disable_static() - x = paddle.to_tensor(self.x) - y = paddle.to_tensor(self.y) - out = paddle.bitwise_right_shift( - x, - y, - ) - out_ref = ref_right_shift_arithmetic(self.x, self.y) - np.testing.assert_allclose(out_ref, out.numpy()) - paddle.enable_static() +# def test_dygraph_api_arithmetic(self): +# paddle.disable_static() +# x = paddle.to_tensor(self.x) +# y = paddle.to_tensor(self.y) +# out = paddle.bitwise_right_shift( +# x, +# y, +# ) +# out_ref = ref_right_shift_arithmetic(self.x, self.y) +# np.testing.assert_allclose(out_ref, out.numpy()) +# paddle.enable_static() - def test_static_api_logical(self): - paddle.enable_static() - with paddle.static.program_guard(paddle.static.Program()): - x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) - y = paddle.static.data('y', self.y.shape, dtype=self.y.dtype) - out = paddle.bitwise_right_shift(x, y, False) - exe = paddle.static.Executor(self.place) - res = exe.run(feed={'x': self.x, 'y': self.y}, fetch_list=[out]) - out_ref = ref_right_shift_logical(self.x, self.y) - np.testing.assert_allclose(out_ref, res[0]) +# def test_static_api_logical(self): +# paddle.enable_static() +# with paddle.static.program_guard(paddle.static.Program()): +# x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) +# y = paddle.static.data('y', self.y.shape, dtype=self.y.dtype) +# out = paddle.bitwise_right_shift(x, y, False) +# exe = paddle.static.Executor(self.place) +# res = exe.run(feed={'x': self.x, 'y': self.y}, fetch_list=[out]) +# out_ref = ref_right_shift_logical(self.x, self.y) +# np.testing.assert_allclose(out_ref, res[0]) - def test_dygraph_api_logical(self): - paddle.disable_static() - x = paddle.to_tensor(self.x) - y = paddle.to_tensor(self.y) - out = paddle.bitwise_right_shift(x, y, False) - out_ref = ref_right_shift_logical(self.x, self.y) - np.testing.assert_allclose(out_ref, out.numpy()) - paddle.enable_static() +# def test_dygraph_api_logical(self): +# paddle.disable_static() +# x = paddle.to_tensor(self.x) +# y = paddle.to_tensor(self.y) +# out = paddle.bitwise_right_shift(x, y, False) +# out_ref = ref_right_shift_logical(self.x, self.y) +# np.testing.assert_allclose(out_ref, out.numpy()) +# paddle.enable_static() -class TestBitwiseRightShiftAPI_UINT8(TestBitwiseRightShiftAPI): - def init_input(self): - self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') - self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') +# class TestBitwiseRightShiftAPI_UINT8(TestBitwiseRightShiftAPI): +# def init_input(self): +# self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') +# self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') -class TestBitwiseRightShiftAPI_UINT8_broadcast1(TestBitwiseRightShiftAPI): - def init_input(self): - self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') - self.y = np.random.randint(0, 256, [3]).astype('uint8') +# class TestBitwiseRightShiftAPI_UINT8_broadcast1(TestBitwiseRightShiftAPI): +# def init_input(self): +# self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') +# self.y = np.random.randint(0, 256, [3]).astype('uint8') -class TestBitwiseRightShiftAPI_UINT8_broadcast2(TestBitwiseRightShiftAPI): - def init_input(self): - self.x = np.random.randint(0, 256, [3]).astype('uint8') - self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') +# class TestBitwiseRightShiftAPI_UINT8_broadcast2(TestBitwiseRightShiftAPI): +# def init_input(self): +# self.x = np.random.randint(0, 256, [3]).astype('uint8') +# self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') -class TestBitwiseRightShiftAPI_INT8(TestBitwiseRightShiftAPI): - def init_input(self): - self.x = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') - self.y = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') +# class TestBitwiseRightShiftAPI_INT8(TestBitwiseRightShiftAPI): +# def init_input(self): +# self.x = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') +# self.y = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') -class TestBitwiseRightShiftAPI_INT8_broadcast1(TestBitwiseRightShiftAPI): - def init_input(self): - self.x = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') - self.y = np.random.randint(-(2**7), 2**7, [3]).astype('int8') +# class TestBitwiseRightShiftAPI_INT8_broadcast1(TestBitwiseRightShiftAPI): +# def init_input(self): +# self.x = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') +# self.y = np.random.randint(-(2**7), 2**7, [3]).astype('int8') -class TestBitwiseRightShiftAPI_INT8_broadcast2(TestBitwiseRightShiftAPI): - def init_input(self): - self.x = np.random.randint(-(2**7), 2**7, [3]).astype('int8') - self.y = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') +# class TestBitwiseRightShiftAPI_INT8_broadcast2(TestBitwiseRightShiftAPI): +# def init_input(self): +# self.x = np.random.randint(-(2**7), 2**7, [3]).astype('int8') +# self.y = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') -class TestBitwiseRightShiftAPI_INT16(TestBitwiseRightShiftAPI): - def init_input(self): - self.x = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') - self.y = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') +# class TestBitwiseRightShiftAPI_INT16(TestBitwiseRightShiftAPI): +# def init_input(self): +# self.x = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') +# self.y = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') -class TestBitwiseRightShiftAPI_INT16_broadcast1(TestBitwiseRightShiftAPI): - def init_input(self): - self.x = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') - self.y = np.random.randint(-(2**15), 2**15, [3]).astype('int16') +# class TestBitwiseRightShiftAPI_INT16_broadcast1(TestBitwiseRightShiftAPI): +# def init_input(self): +# self.x = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') +# self.y = np.random.randint(-(2**15), 2**15, [3]).astype('int16') -class TestBitwiseRightShiftAPI_INT16_broadcast2(TestBitwiseRightShiftAPI): - def init_input(self): - self.x = np.random.randint(-(2**15), 2**15, [3]).astype('int16') - self.y = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') +# class TestBitwiseRightShiftAPI_INT16_broadcast2(TestBitwiseRightShiftAPI): +# def init_input(self): +# self.x = np.random.randint(-(2**15), 2**15, [3]).astype('int16') +# self.y = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') -class TestBitwiseRightShiftAPI_INT32(TestBitwiseRightShiftAPI): - def init_input(self): - self.x = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') - self.y = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') +# class TestBitwiseRightShiftAPI_INT32(TestBitwiseRightShiftAPI): +# def init_input(self): +# self.x = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') +# self.y = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') -class TestBitwiseRightShiftAPI_INT32_broadcast1(TestBitwiseRightShiftAPI): - def init_input(self): - self.x = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') - self.y = np.random.randint(-(2**31), 2**31, [3]).astype('int32') +# class TestBitwiseRightShiftAPI_INT32_broadcast1(TestBitwiseRightShiftAPI): +# def init_input(self): +# self.x = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') +# self.y = np.random.randint(-(2**31), 2**31, [3]).astype('int32') -class TestBitwiseRightShiftAPI_INT32_broadcast2(TestBitwiseRightShiftAPI): - def init_input(self): - self.x = np.random.randint(-(2**31), 2**31, [3]).astype('int32') - self.y = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') +# class TestBitwiseRightShiftAPI_INT32_broadcast2(TestBitwiseRightShiftAPI): +# def init_input(self): +# self.x = np.random.randint(-(2**31), 2**31, [3]).astype('int32') +# self.y = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') -class TestBitwiseRightShiftAPI_INT64(TestBitwiseRightShiftAPI): - def init_input(self): - self.x = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) - self.y = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) +# class TestBitwiseRightShiftAPI_INT64(TestBitwiseRightShiftAPI): +# def init_input(self): +# self.x = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) +# self.y = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) -class TestBitwiseRightShiftAPI_INT64_broadcast1(TestBitwiseRightShiftAPI): - def init_input(self): - self.x = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) - self.y = np.random.randint(-(2**63), 2**63, [3], dtype=np.int64) +# class TestBitwiseRightShiftAPI_INT64_broadcast1(TestBitwiseRightShiftAPI): +# def init_input(self): +# self.x = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) +# self.y = np.random.randint(-(2**63), 2**63, [3], dtype=np.int64) -class TestBitwiseRightShiftAPI_INT64_broadcast2(TestBitwiseRightShiftAPI): - def init_input(self): - self.x = np.random.randint(-(2**63), 2**63, [3], dtype=np.int64) - self.y = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) +# class TestBitwiseRightShiftAPI_INT64_broadcast2(TestBitwiseRightShiftAPI): +# def init_input(self): +# self.x = np.random.randint(-(2**63), 2**63, [3], dtype=np.int64) +# self.y = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) -class TestBitwiseRightShiftAPI_special_case1(TestBitwiseRightShiftAPI): - def init_input(self): - self.x = np.array([0b11111111]).astype('int8') - self.y = np.array([1]).astype('int8') +# class TestBitwiseRightShiftAPI_special_case1(TestBitwiseRightShiftAPI): +# def init_input(self): +# self.x = np.array([0b11111111]).astype('int8') +# self.y = np.array([1]).astype('int8') -class TestBitwiseRightShiftAPI_special_case2(TestBitwiseRightShiftAPI): - def init_input(self): - self.x = np.array([0b11111111]).astype('int8') - self.y = np.array([10]).astype('int8') +# class TestBitwiseRightShiftAPI_special_case2(TestBitwiseRightShiftAPI): +# def init_input(self): +# self.x = np.array([0b11111111]).astype('int8') +# self.y = np.array([10]).astype('int8') -class TestBitwiseRightShiftAPI_special_case3(TestBitwiseRightShiftAPI): - def init_input(self): - self.x = np.array([0b11111111], dtype='uint8') - self.y = np.array([1], dtype='uint8') +# class TestBitwiseRightShiftAPI_special_case3(TestBitwiseRightShiftAPI): +# def init_input(self): +# self.x = np.array([0b11111111], dtype='uint8') +# self.y = np.array([1], dtype='uint8') -class TestBitwiseRightShiftAPI_special_case4(TestBitwiseRightShiftAPI): - def init_input(self): - self.x = np.array([0b11111111], dtype='uint8') - self.y = np.array([10], dtype='uint8') +# class TestBitwiseRightShiftAPI_special_case4(TestBitwiseRightShiftAPI): +# def init_input(self): +# self.x = np.array([0b11111111], dtype='uint8') +# self.y = np.array([10], dtype='uint8') -if __name__ == '__main__': - paddle.enable_static() - unittest.main() +# if __name__ == '__main__': +# paddle.enable_static() +# unittest.main() diff --git a/test/legacy_test/test_inplace.py b/test/legacy_test/test_inplace.py index 20e2960c29481..565729e8ac47e 100644 --- a/test/legacy_test/test_inplace.py +++ b/test/legacy_test/test_inplace.py @@ -1633,106 +1633,106 @@ def test_forward_version(self): self.assertEqual(var.inplace_version, 3) -class TestDygraphInplaceBitwiseLeftShift_arithmetic(TestDygraphInplaceLogicAnd): - def init_data(self): - self.input_var_numpy = np.random.randint( - low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" - ) - self.input_var_numpy = paddle.to_tensor(self.input_var_numpy) - self.dtype = "int32" - self.y = np.random.randint( - low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" - ) - self.y = paddle.to_tensor(self.y) - self.is_arithmetic = True - - def inplace_api_processing(self, var): - return paddle.bitwise_left_shift_(var, self.y, self.is_arithmetic) - - def non_inplace_api_processing(self, var): - return paddle.bitwise_left_shift(var, self.y, self.is_arithmetic) - - def test_broadcast_error(self): - broadcast_input = paddle.randn([4, 5]) - with self.assertRaises(ValueError): - self.inplace_api_processing(broadcast_input) - - -class TestDygraphInplaceBitwiseRightShift_arithmetic( - TestDygraphInplaceLogicAnd -): - def init_data(self): - self.input_var_numpy = np.random.randint( - low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" - ) - self.input_var_numpy = paddle.to_tensor(self.input_var_numpy) - self.dtype = "int32" - self.y = np.random.randint( - low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" - ) - self.y = paddle.to_tensor(self.y) - self.is_arithmetic = True - - def inplace_api_processing(self, var): - return paddle.bitwise_right_shift_(var, self.y, self.is_arithmetic) - - def non_inplace_api_processing(self, var): - return paddle.bitwise_right_shift_(var, self.y, self.is_arithmetic) - - def test_broadcast_error(self): - broadcast_input = paddle.randn([4, 5]) - with self.assertRaises(ValueError): - self.inplace_api_processing(broadcast_input) - - -class TestDygraphInplaceBitwiseLeftShift_logic(TestDygraphInplaceLogicAnd): - def init_data(self): - self.input_var_numpy = np.random.randint( - low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" - ) - self.input_var_numpy = paddle.to_tensor(self.input_var_numpy) - self.dtype = "int32" - self.y = np.random.randint( - low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" - ) - self.y = paddle.to_tensor(self.y) - self.is_arithmetic = False - - def inplace_api_processing(self, var): - return paddle.bitwise_left_shift_(var, self.y, self.is_arithmetic) - - def non_inplace_api_processing(self, var): - return paddle.bitwise_left_shift(var, self.y, self.is_arithmetic) - - def test_broadcast_error(self): - broadcast_input = paddle.randn([4, 5]) - with self.assertRaises(ValueError): - self.inplace_api_processing(broadcast_input) - - -class TestDygraphInplaceBitwiseRightShift_logic(TestDygraphInplaceLogicAnd): - def init_data(self): - self.input_var_numpy = np.random.randint( - low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" - ) - self.input_var_numpy = paddle.to_tensor(self.input_var_numpy) - self.dtype = "int32" - self.y = np.random.randint( - low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" - ) - self.y = paddle.to_tensor(self.y) - self.is_arithmetic = False - - def inplace_api_processing(self, var): - return paddle.bitwise_right_shift_(var, self.y, self.is_arithmetic) - - def non_inplace_api_processing(self, var): - return paddle.bitwise_right_shift_(var, self.y, self.is_arithmetic) - - def test_broadcast_error(self): - broadcast_input = paddle.randn([4, 5]) - with self.assertRaises(ValueError): - self.inplace_api_processing(broadcast_input) +# class TestDygraphInplaceBitwiseLeftShift_arithmetic(TestDygraphInplaceLogicAnd): +# def init_data(self): +# self.input_var_numpy = np.random.randint( +# low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" +# ) +# self.input_var_numpy = paddle.to_tensor(self.input_var_numpy) +# self.dtype = "int32" +# self.y = np.random.randint( +# low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" +# ) +# self.y = paddle.to_tensor(self.y) +# self.is_arithmetic = True + +# def inplace_api_processing(self, var): +# return paddle.bitwise_left_shift_(var, self.y, self.is_arithmetic) + +# def non_inplace_api_processing(self, var): +# return paddle.bitwise_left_shift(var, self.y, self.is_arithmetic) + +# def test_broadcast_error(self): +# broadcast_input = paddle.randn([4, 5]) +# with self.assertRaises(ValueError): +# self.inplace_api_processing(broadcast_input) + + +# class TestDygraphInplaceBitwiseRightShift_arithmetic( +# TestDygraphInplaceLogicAnd +# ): +# def init_data(self): +# self.input_var_numpy = np.random.randint( +# low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" +# ) +# self.input_var_numpy = paddle.to_tensor(self.input_var_numpy) +# self.dtype = "int32" +# self.y = np.random.randint( +# low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" +# ) +# self.y = paddle.to_tensor(self.y) +# self.is_arithmetic = True + +# def inplace_api_processing(self, var): +# return paddle.bitwise_right_shift_(var, self.y, self.is_arithmetic) + +# def non_inplace_api_processing(self, var): +# return paddle.bitwise_right_shift_(var, self.y, self.is_arithmetic) + +# def test_broadcast_error(self): +# broadcast_input = paddle.randn([4, 5]) +# with self.assertRaises(ValueError): +# self.inplace_api_processing(broadcast_input) + + +# class TestDygraphInplaceBitwiseLeftShift_logic(TestDygraphInplaceLogicAnd): +# def init_data(self): +# self.input_var_numpy = np.random.randint( +# low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" +# ) +# self.input_var_numpy = paddle.to_tensor(self.input_var_numpy) +# self.dtype = "int32" +# self.y = np.random.randint( +# low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" +# ) +# self.y = paddle.to_tensor(self.y) +# self.is_arithmetic = False + +# def inplace_api_processing(self, var): +# return paddle.bitwise_left_shift_(var, self.y, self.is_arithmetic) + +# def non_inplace_api_processing(self, var): +# return paddle.bitwise_left_shift(var, self.y, self.is_arithmetic) + +# def test_broadcast_error(self): +# broadcast_input = paddle.randn([4, 5]) +# with self.assertRaises(ValueError): +# self.inplace_api_processing(broadcast_input) + + +# class TestDygraphInplaceBitwiseRightShift_logic(TestDygraphInplaceLogicAnd): +# def init_data(self): +# self.input_var_numpy = np.random.randint( +# low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" +# ) +# self.input_var_numpy = paddle.to_tensor(self.input_var_numpy) +# self.dtype = "int32" +# self.y = np.random.randint( +# low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" +# ) +# self.y = paddle.to_tensor(self.y) +# self.is_arithmetic = False + +# def inplace_api_processing(self, var): +# return paddle.bitwise_right_shift_(var, self.y, self.is_arithmetic) + +# def non_inplace_api_processing(self, var): +# return paddle.bitwise_right_shift_(var, self.y, self.is_arithmetic) + +# def test_broadcast_error(self): +# broadcast_input = paddle.randn([4, 5]) +# with self.assertRaises(ValueError): +# self.inplace_api_processing(broadcast_input) class TestDygraphInplaceIndexFill(TestDygraphInplace): def init_data(self): From 78d8fec98aff8e75d4031f0a68eed7d0b86699fb Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Wed, 27 Dec 2023 16:58:26 +0800 Subject: [PATCH 05/27] update --- paddle/phi/kernels/bitwise_kernel.h | 46 +- paddle/phi/kernels/cpu/bitwise_kernel.cc | 126 ++-- paddle/phi/kernels/funcs/bitwise_functors.h | 312 ++++----- paddle/phi/kernels/kps/bitwise_kernel.cu | 86 +-- python/paddle/tensor/math.py | 270 ++++---- test/legacy_test/test_bitwise_shift_op.py | 692 ++++++++++---------- test/legacy_test/test_inplace.py | 200 +++--- 7 files changed, 866 insertions(+), 866 deletions(-) diff --git a/paddle/phi/kernels/bitwise_kernel.h b/paddle/phi/kernels/bitwise_kernel.h index 73082fc6c7176..d0b26da015a22 100644 --- a/paddle/phi/kernels/bitwise_kernel.h +++ b/paddle/phi/kernels/bitwise_kernel.h @@ -41,29 +41,29 @@ void BitwiseNotKernel(const Context& dev_ctx, const DenseTensor& x, DenseTensor* out); -// template -// void BitwiseLeftShiftArithmeticKernel(const Context& dev_ctx, -// const DenseTensor& x, -// const DenseTensor& y, -// DenseTensor* out); - -// template -// void BitwiseLeftShiftLogicKernel(const Context& dev_ctx, -// const DenseTensor& x, -// const DenseTensor& y, -// DenseTensor* out); - -// template -// void BitwiseRightShiftArithmeticKernel(const Context& dev_ctx, -// const DenseTensor& x, -// const DenseTensor& y, -// DenseTensor* out); - -// template -// void BitwiseRightShiftLogicKernel(const Context& dev_ctx, -// const DenseTensor& x, -// const DenseTensor& y, -// DenseTensor* out); +template +void BitwiseLeftShiftArithmeticKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + DenseTensor* out); + +template +void BitwiseLeftShiftLogicKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + DenseTensor* out); + +template +void BitwiseRightShiftArithmeticKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + DenseTensor* out); + +template +void BitwiseRightShiftLogicKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + DenseTensor* out); } // namespace phi diff --git a/paddle/phi/kernels/cpu/bitwise_kernel.cc b/paddle/phi/kernels/cpu/bitwise_kernel.cc index 8eef29a690d7f..5ba49fde84ac6 100644 --- a/paddle/phi/kernels/cpu/bitwise_kernel.cc +++ b/paddle/phi/kernels/cpu/bitwise_kernel.cc @@ -40,30 +40,30 @@ DEFINE_BITWISE_KERNEL(Or) DEFINE_BITWISE_KERNEL(Xor) #undef DEFINE_BITWISE_KERNEL -// #define DEFINE_BITWISE_KERNEL_WITH_INVERSE(op_type) \ -// template \ -// void Bitwise##op_type##Kernel(const Context& dev_ctx, \ -// const DenseTensor& x, \ -// const DenseTensor& y, \ -// DenseTensor* out) { \ -// funcs::Bitwise##op_type##Functor func; \ -// funcs::InverseBitwise##op_type##Functor inv_func; \ -// auto x_dims = x.dims(); \ -// auto y_dims = y.dims(); \ -// if (x_dims.size() >= y_dims.size()) { \ -// funcs::ElementwiseCompute, T>( \ -// dev_ctx, x, y, func, out); \ -// } else { \ -// funcs::ElementwiseCompute, \ -// T>(dev_ctx, x, y, inv_func, out); \ -// } \ -// } - -// DEFINE_BITWISE_KERNEL_WITH_INVERSE(LeftShiftArithmetic) -// DEFINE_BITWISE_KERNEL_WITH_INVERSE(LeftShiftLogic) -// DEFINE_BITWISE_KERNEL_WITH_INVERSE(RightShiftArithmetic) -// DEFINE_BITWISE_KERNEL_WITH_INVERSE(RightShiftLogic) -// #undef DEFINE_BITWISE_KERNEL_WITH_INVERSE +#define DEFINE_BITWISE_KERNEL_WITH_INVERSE(op_type) \ + template \ + void Bitwise##op_type##Kernel(const Context& dev_ctx, \ + const DenseTensor& x, \ + const DenseTensor& y, \ + DenseTensor* out) { \ + funcs::Bitwise##op_type##Functor func; \ + funcs::InverseBitwise##op_type##Functor inv_func; \ + auto x_dims = x.dims(); \ + auto y_dims = y.dims(); \ + if (x_dims.size() >= y_dims.size()) { \ + funcs::ElementwiseCompute, T>( \ + dev_ctx, x, y, func, out); \ + } else { \ + funcs::ElementwiseCompute, \ + T>(dev_ctx, x, y, inv_func, out); \ + } \ + } + +DEFINE_BITWISE_KERNEL_WITH_INVERSE(LeftShiftArithmetic) +DEFINE_BITWISE_KERNEL_WITH_INVERSE(LeftShiftLogic) +DEFINE_BITWISE_KERNEL_WITH_INVERSE(RightShiftArithmetic) +DEFINE_BITWISE_KERNEL_WITH_INVERSE(RightShiftLogic) +#undef DEFINE_BITWISE_KERNEL_WITH_INVERSE template void BitwiseNotKernel(const Context& dev_ctx, @@ -124,42 +124,42 @@ PD_REGISTER_KERNEL(bitwise_not, int64_t) {} -// PD_REGISTER_KERNEL(bitwise_left_shift_arithmetic, -// CPU, -// ALL_LAYOUT, -// phi::BitwiseLeftShiftArithmeticKernel, -// uint8_t, -// int8_t, -// int16_t, -// int, -// int64_t) {} - -// PD_REGISTER_KERNEL(bitwise_left_shift_logic, -// CPU, -// ALL_LAYOUT, -// phi::BitwiseLeftShiftLogicKernel, -// uint8_t, -// int8_t, -// int16_t, -// int, -// int64_t) {} - -// PD_REGISTER_KERNEL(bitwise_right_shift_arithmetic, -// CPU, -// ALL_LAYOUT, -// phi::BitwiseRightShiftArithmeticKernel, -// uint8_t, -// int8_t, -// int16_t, -// int, -// int64_t) {} - -// PD_REGISTER_KERNEL(bitwise_right_shift_logic, -// CPU, -// ALL_LAYOUT, -// phi::BitwiseRightShiftLogicKernel, -// uint8_t, -// int8_t, -// int16_t, -// int, -// int64_t) {} +PD_REGISTER_KERNEL(bitwise_left_shift_arithmetic, + CPU, + ALL_LAYOUT, + phi::BitwiseLeftShiftArithmeticKernel, + uint8_t, + int8_t, + int16_t, + int, + int64_t) {} + +PD_REGISTER_KERNEL(bitwise_left_shift_logic, + CPU, + ALL_LAYOUT, + phi::BitwiseLeftShiftLogicKernel, + uint8_t, + int8_t, + int16_t, + int, + int64_t) {} + +PD_REGISTER_KERNEL(bitwise_right_shift_arithmetic, + CPU, + ALL_LAYOUT, + phi::BitwiseRightShiftArithmeticKernel, + uint8_t, + int8_t, + int16_t, + int, + int64_t) {} + +PD_REGISTER_KERNEL(bitwise_right_shift_logic, + CPU, + ALL_LAYOUT, + phi::BitwiseRightShiftLogicKernel, + uint8_t, + int8_t, + int16_t, + int, + int64_t) {} diff --git a/paddle/phi/kernels/funcs/bitwise_functors.h b/paddle/phi/kernels/funcs/bitwise_functors.h index c5155dea17e60..912a0a7be1d98 100644 --- a/paddle/phi/kernels/funcs/bitwise_functors.h +++ b/paddle/phi/kernels/funcs/bitwise_functors.h @@ -47,162 +47,162 @@ struct BitwiseNotFunctor { HOSTDEVICE bool operator()(const bool a) const { return !a; } }; -// template -// struct BitwiseLeftShiftArithmeticFunctor { -// HOSTDEVICE T operator()(const T a, const T b) const { -// if (b >= static_cast(sizeof(T) * 8)) return static_cast(0); -// if (b < static_cast(0)) return static_cast(0); -// return a << b; -// } -// }; - -// template -// struct InverseBitwiseLeftShiftArithmeticFunctor { -// inline HOSTDEVICE T operator()(const T a, const T b) const { -// if (a >= static_cast(sizeof(T) * 8)) return static_cast(0); -// if (a < static_cast(0)) return static_cast(0); -// return b << a; -// } -// }; - -// template -// struct BitwiseLeftShiftLogicFunctor { -// HOSTDEVICE T operator()(const T a, const T b) const { -// if (b < static_cast(0) || b >= static_cast(sizeof(T) * 8)) -// return static_cast(0); -// return a << b; -// } -// }; - -// template -// struct InverseBitwiseLeftShiftLogicFunctor { -// inline HOSTDEVICE T operator()(const T a, const T b) const { -// if (a < static_cast(0) || a >= static_cast(sizeof(T) * 8)) -// return static_cast(0); -// return b << a; -// } -// }; - -// template -// struct BitwiseRightShiftArithmeticFunctor { -// HOSTDEVICE T operator()(const T a, const T b) const { -// if (b < static_cast(0) || b >= static_cast(sizeof(T) * 8)) -// return static_cast(-(a >> (sizeof(T) * 8 - 1) & 1)); -// return a >> b; -// } -// }; - -// template -// struct InverseBitwiseRightShiftArithmeticFunctor { -// inline HOSTDEVICE T operator()(const T a, const T b) const { -// if (a < static_cast(0) || a >= static_cast(sizeof(T) * 8)) -// return static_cast(-(b >> (sizeof(T) * 8 - 1) & 1)); -// return b >> a; -// } -// }; - -// template <> -// struct BitwiseRightShiftArithmeticFunctor { -// HOSTDEVICE uint8_t operator()(const uint8_t a, const uint8_t b) const { -// if (b >= static_cast(sizeof(uint8_t) * 8)) -// return static_cast(0); -// return a >> b; -// } -// }; - -// template <> -// struct InverseBitwiseRightShiftArithmeticFunctor { -// inline HOSTDEVICE uint8_t operator()(const uint8_t a, const uint8_t b) const { -// if (a >= static_cast(sizeof(uint8_t) * 8)) -// return static_cast(0); -// return b >> a; -// } -// }; - -// template -// struct BitwiseRightShiftLogicFunctor { -// HOSTDEVICE T operator()(const T a, const T b) const { -// if (b >= static_cast(sizeof(T) * 8)) return static_cast(0); -// return a >> b; -// } -// }; - -// template -// struct InverseBitwiseRightShiftLogicFunctor { -// inline HOSTDEVICE T operator()(const T a, const T b) const { -// if (a >= static_cast(sizeof(T) * 8)) return static_cast(0); -// return b >> a; -// } -// }; - -// template -// HOSTDEVICE T logic_shift_func(const T a, const T b) { -// if (b < static_cast(0) || b >= static_cast(sizeof(T) * 8)) -// return static_cast(0); -// T t = static_cast(sizeof(T) * 8 - 1); -// T mask = (((a >> t) << t) >> b) << 1; -// return (a >> b) ^ mask; -// } - -// // signed int8 -// template <> -// struct BitwiseRightShiftLogicFunctor { -// HOSTDEVICE int8_t operator()(const int8_t a, const int8_t b) const { -// return logic_shift_func(a, b); -// } -// }; - -// template <> -// struct InverseBitwiseRightShiftLogicFunctor { -// inline HOSTDEVICE int8_t operator()(const int8_t a, const int8_t b) const { -// return logic_shift_func(b, a); -// } -// }; - -// // signed int16 -// template <> -// struct BitwiseRightShiftLogicFunctor { -// HOSTDEVICE int16_t operator()(const int16_t a, const int16_t b) const { -// return logic_shift_func(a, b); -// } -// }; - -// template <> -// struct InverseBitwiseRightShiftLogicFunctor { -// inline HOSTDEVICE int16_t operator()(const int16_t a, const int16_t b) const { -// return logic_shift_func(b, a); -// } -// }; - -// // signed int32 -// template <> -// struct BitwiseRightShiftLogicFunctor { -// HOSTDEVICE int operator()(const int a, const int b) const { -// return logic_shift_func(a, b); -// } -// }; - -// template <> -// struct InverseBitwiseRightShiftLogicFunctor { -// inline HOSTDEVICE int operator()(const int a, const int b) const { -// return logic_shift_func(b, a); -// } -// }; - -// // signed int64 -// template <> -// struct BitwiseRightShiftLogicFunctor { -// HOSTDEVICE int64_t operator()(const int64_t a, const int64_t b) const { -// return logic_shift_func(a, b); -// } -// }; - -// template <> -// struct InverseBitwiseRightShiftLogicFunctor { -// inline HOSTDEVICE int64_t operator()(const int64_t a, const int64_t b) const { -// return logic_shift_func(b, a); -// } -// }; +template +struct BitwiseLeftShiftArithmeticFunctor { + HOSTDEVICE T operator()(const T a, const T b) const { + if (b >= static_cast(sizeof(T) * 8)) return static_cast(0); + if (b < static_cast(0)) return static_cast(0); + return a << b; + } +}; + +template +struct InverseBitwiseLeftShiftArithmeticFunctor { + inline HOSTDEVICE T operator()(const T a, const T b) const { + if (a >= static_cast(sizeof(T) * 8)) return static_cast(0); + if (a < static_cast(0)) return static_cast(0); + return b << a; + } +}; + +template +struct BitwiseLeftShiftLogicFunctor { + HOSTDEVICE T operator()(const T a, const T b) const { + if (b < static_cast(0) || b >= static_cast(sizeof(T) * 8)) + return static_cast(0); + return a << b; + } +}; + +template +struct InverseBitwiseLeftShiftLogicFunctor { + inline HOSTDEVICE T operator()(const T a, const T b) const { + if (a < static_cast(0) || a >= static_cast(sizeof(T) * 8)) + return static_cast(0); + return b << a; + } +}; + +template +struct BitwiseRightShiftArithmeticFunctor { + HOSTDEVICE T operator()(const T a, const T b) const { + if (b < static_cast(0) || b >= static_cast(sizeof(T) * 8)) + return static_cast(-(a >> (sizeof(T) * 8 - 1) & 1)); + return a >> b; + } +}; + +template +struct InverseBitwiseRightShiftArithmeticFunctor { + inline HOSTDEVICE T operator()(const T a, const T b) const { + if (a < static_cast(0) || a >= static_cast(sizeof(T) * 8)) + return static_cast(-(b >> (sizeof(T) * 8 - 1) & 1)); + return b >> a; + } +}; + +template <> +struct BitwiseRightShiftArithmeticFunctor { + HOSTDEVICE uint8_t operator()(const uint8_t a, const uint8_t b) const { + if (b >= static_cast(sizeof(uint8_t) * 8)) + return static_cast(0); + return a >> b; + } +}; + +template <> +struct InverseBitwiseRightShiftArithmeticFunctor { + inline HOSTDEVICE uint8_t operator()(const uint8_t a, const uint8_t b) const { + if (a >= static_cast(sizeof(uint8_t) * 8)) + return static_cast(0); + return b >> a; + } +}; + +template +struct BitwiseRightShiftLogicFunctor { + HOSTDEVICE T operator()(const T a, const T b) const { + if (b >= static_cast(sizeof(T) * 8)) return static_cast(0); + return a >> b; + } +}; + +template +struct InverseBitwiseRightShiftLogicFunctor { + inline HOSTDEVICE T operator()(const T a, const T b) const { + if (a >= static_cast(sizeof(T) * 8)) return static_cast(0); + return b >> a; + } +}; + +template +HOSTDEVICE T logic_shift_func(const T a, const T b) { + if (b < static_cast(0) || b >= static_cast(sizeof(T) * 8)) + return static_cast(0); + T t = static_cast(sizeof(T) * 8 - 1); + T mask = (((a >> t) << t) >> b) << 1; + return (a >> b) ^ mask; +} + +// signed int8 +template <> +struct BitwiseRightShiftLogicFunctor { + HOSTDEVICE int8_t operator()(const int8_t a, const int8_t b) const { + return logic_shift_func(a, b); + } +}; + +template <> +struct InverseBitwiseRightShiftLogicFunctor { + inline HOSTDEVICE int8_t operator()(const int8_t a, const int8_t b) const { + return logic_shift_func(b, a); + } +}; + +// signed int16 +template <> +struct BitwiseRightShiftLogicFunctor { + HOSTDEVICE int16_t operator()(const int16_t a, const int16_t b) const { + return logic_shift_func(a, b); + } +}; + +template <> +struct InverseBitwiseRightShiftLogicFunctor { + inline HOSTDEVICE int16_t operator()(const int16_t a, const int16_t b) const { + return logic_shift_func(b, a); + } +}; + +// signed int32 +template <> +struct BitwiseRightShiftLogicFunctor { + HOSTDEVICE int operator()(const int a, const int b) const { + return logic_shift_func(a, b); + } +}; + +template <> +struct InverseBitwiseRightShiftLogicFunctor { + inline HOSTDEVICE int operator()(const int a, const int b) const { + return logic_shift_func(b, a); + } +}; + +// signed int64 +template <> +struct BitwiseRightShiftLogicFunctor { + HOSTDEVICE int64_t operator()(const int64_t a, const int64_t b) const { + return logic_shift_func(a, b); + } +}; + +template <> +struct InverseBitwiseRightShiftLogicFunctor { + inline HOSTDEVICE int64_t operator()(const int64_t a, const int64_t b) const { + return logic_shift_func(b, a); + } +}; } // namespace funcs diff --git a/paddle/phi/kernels/kps/bitwise_kernel.cu b/paddle/phi/kernels/kps/bitwise_kernel.cu index 3478f570f3a9b..7dd3225f20476 100644 --- a/paddle/phi/kernels/kps/bitwise_kernel.cu +++ b/paddle/phi/kernels/kps/bitwise_kernel.cu @@ -41,10 +41,10 @@ namespace phi { DEFINE_BITWISE_KERNEL(And) DEFINE_BITWISE_KERNEL(Or) DEFINE_BITWISE_KERNEL(Xor) -// DEFINE_BITWISE_KERNEL(LeftShiftArithmetic) -// DEFINE_BITWISE_KERNEL(LeftShiftLogic) -// DEFINE_BITWISE_KERNEL(RightShiftArithmetic) -// DEFINE_BITWISE_KERNEL(RightShiftLogic) +DEFINE_BITWISE_KERNEL(LeftShiftArithmetic) +DEFINE_BITWISE_KERNEL(LeftShiftLogic) +DEFINE_BITWISE_KERNEL(RightShiftArithmetic) +DEFINE_BITWISE_KERNEL(RightShiftLogic) #undef DEFINE_BITWISE_KERNEL template @@ -116,44 +116,44 @@ PD_REGISTER_KERNEL(bitwise_not, int, int64_t) {} -// PD_REGISTER_KERNEL(bitwise_left_shift_arithmetic, -// KPS, -// ALL_LAYOUT, -// phi::BitwiseLeftShiftArithmeticKernel, -// uint8_t, -// int8_t, -// int16_t, -// int, -// int64_t) {} - -// PD_REGISTER_KERNEL(bitwise_left_shift_logic, -// KPS, -// ALL_LAYOUT, -// phi::BitwiseLeftShiftLogicKernel, -// uint8_t, -// int8_t, -// int16_t, -// int, -// int64_t) {} - -// PD_REGISTER_KERNEL(bitwise_right_shift_arithmetic, -// KPS, -// ALL_LAYOUT, -// phi::BitwiseRightShiftArithmeticKernel, -// uint8_t, -// int8_t, -// int16_t, -// int, -// int64_t) {} - -// PD_REGISTER_KERNEL(bitwise_right_shift_logic, -// KPS, -// ALL_LAYOUT, -// phi::BitwiseRightShiftLogicKernel, -// uint8_t, -// int8_t, -// int16_t, -// int, -// int64_t) {} +PD_REGISTER_KERNEL(bitwise_left_shift_arithmetic, + KPS, + ALL_LAYOUT, + phi::BitwiseLeftShiftArithmeticKernel, + uint8_t, + int8_t, + int16_t, + int, + int64_t) {} + +PD_REGISTER_KERNEL(bitwise_left_shift_logic, + KPS, + ALL_LAYOUT, + phi::BitwiseLeftShiftLogicKernel, + uint8_t, + int8_t, + int16_t, + int, + int64_t) {} + +PD_REGISTER_KERNEL(bitwise_right_shift_arithmetic, + KPS, + ALL_LAYOUT, + phi::BitwiseRightShiftArithmeticKernel, + uint8_t, + int8_t, + int16_t, + int, + int64_t) {} + +PD_REGISTER_KERNEL(bitwise_right_shift_logic, + KPS, + ALL_LAYOUT, + phi::BitwiseRightShiftLogicKernel, + uint8_t, + int8_t, + int16_t, + int, + int64_t) {} #endif diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index c542f8c0bbd56..8ed362f04b5f0 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -7079,141 +7079,141 @@ def _bitwise_op(op_name, x, y, out=None, name=None): return out -# def bitwise_left_shift(x, y, is_arithmetic=True, out=None, name=None): -# r""" -# Apply ``bitwise_left_shift`` on Tensor ``X`` and ``Y`` . -# .. math:: -# Out = X \ll Y -# .. note:: -# ``paddle.bitwise_left_shift`` supports broadcasting. If you want know more about broadcasting, please refer to please refer to `Introduction to Tensor`_ . -# .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor. -# Args: -# x (Tensor): Input Tensor of ``bitwise_left_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. -# y (Tensor): Input Tensor of ``bitwise_left_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. -# name (str, optional): The default value is None. Normally there is no need for -# user to set this property. For more information, please refer to :ref:`api_guide_Name`. -# Returns: -# Tensor: Result of ``bitwise_left_shift`` . It is a N-D Tensor with the same data type of input Tensor. -# Examples: -# .. code-block:: python -# >>> import paddle -# >>> x= paddle.to_tensor([[1,2,4,8],[16,17,32,65]]) -# >>> y = paddle.to_tensor([[1,2,3,4,], [2,3,2,1]]) -# >>> paddle.bitwise_left_shift(x, y) -# Tensor(shape=[2, 4], dtype=int64, place=Place(gpu:0), stop_gradient=True, -# [[2 , 8 , 32 , 128], -# [64 , 136, 128, 130]]) -# """ -# if in_dynamic_mode() and out is None: -# if is_arithmetic: -# return _C_ops.bitwise_left_shift_arithmetic(x, y) -# else: -# return _C_ops.bitwise_left_shift_logic(x, y) -# if is_arithmetic: -# return _bitwise_op( -# op_name="bitwise_left_shift_arithmetic", -# x=x, -# y=y, -# name=name, -# out=out, -# ) -# else: -# return _bitwise_op( -# op_name="bitwise_left_shift_logic", -# x=x, -# y=y, -# name=name, -# out=out, -# ) - - -# @inplace_apis_in_dygraph_only -# def bitwise_left_shift_(x, y, is_arithmetic=True, out=None, name=None): -# r""" -# Inplace version of ``bitwise_left_shift`` API, the output Tensor will be inplaced with input ``x``. -# Please refer to :ref:`api_paddle_bitwise_left_shift`. -# """ -# out_shape = broadcast_shape(x.shape, y.shape) -# if out_shape != x.shape: -# raise ValueError( -# "The shape of broadcast output {} is different from that of inplace tensor {} in the Inplace operation.".format( -# out_shape, x.shape -# ) -# ) -# if in_dynamic_or_pir_mode(): -# if is_arithmetic: -# return _C_ops.bitwise_left_shift_arithmetic_(x, y) -# else: -# return _C_ops.bitwise_left_shift_logic_(x, y) - - -# def bitwise_right_shift(x, y, is_arithmetic=True, out=None, name=None): -# r""" -# Apply ``bitwise_right_shift`` on Tensor ``X`` and ``Y`` . -# .. math:: -# Out = X \gg Y -# .. note:: -# ``paddle.bitwise_right_shift`` supports broadcasting. If you want know more about broadcasting, please refer to please refer to `Introduction to Tensor`_ . -# .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor. -# Args: -# x (Tensor): Input Tensor of ``bitwise_right_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. -# y (Tensor): Input Tensor of ``bitwise_right_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. -# name (str, optional): The default value is None. Normally there is no need for -# user to set this property. For more information, please refer to :ref:`api_guide_Name`. -# Returns: -# Tensor: Result of ``bitwise_right_shift`` . It is a N-D Tensor with the same data type of input Tensor. -# Examples: -# .. code-block:: python -# >>> import paddle -# >>> x= paddle.to_tensor([[10,20,40,80],[16,17,32,65]]) -# >>> y = paddle.to_tensor([[1,2,3,4,], [2,3,2,1]]) -# >>> paddle.bitwise_right_shift(x, y) -# Tensor(shape=[2, 4], dtype=int64, place=Place(gpu:0), stop_gradient=True, -# [[5 , 5 , 5 , 5 ], -# [4 , 2 , 8 , 32]]) -# """ -# if in_dynamic_mode() and out is None: -# if is_arithmetic: -# return _C_ops.bitwise_right_shift_arithmetic(x, y) -# else: -# return _C_ops.bitwise_right_shift_logic(x, y) -# if is_arithmetic: -# return _bitwise_op( -# op_name="bitwise_right_shift_arithmetic", -# x=x, -# y=y, -# name=name, -# out=out, -# ) -# else: -# return _bitwise_op( -# op_name="bitwise_right_shift_logic", -# x=x, -# y=y, -# name=name, -# out=out, -# ) - - -# @inplace_apis_in_dygraph_only -# def bitwise_right_shift_(x, y, is_arithmetic=True, out=None, name=None): -# r""" -# Inplace version of ``bitwise_right_shift`` API, the output Tensor will be inplaced with input ``x``. -# Please refer to :ref:`api_paddle_bitwise_left_shift`. -# """ -# out_shape = broadcast_shape(x.shape, y.shape) -# if out_shape != x.shape: -# raise ValueError( -# "The shape of broadcast output {} is different from that of inplace tensor {} in the Inplace operation.".format( -# out_shape, x.shape -# ) -# ) - -# if in_dynamic_or_pir_mode(): -# if is_arithmetic: -# return _C_ops.bitwise_right_shift_arithmetic_(x, y) -# else: -# return _C_ops.bitwise_right_shift_logic_(x, y) +def bitwise_left_shift(x, y, is_arithmetic=True, out=None, name=None): + r""" + Apply ``bitwise_left_shift`` on Tensor ``X`` and ``Y`` . + .. math:: + Out = X \ll Y + .. note:: + ``paddle.bitwise_left_shift`` supports broadcasting. If you want know more about broadcasting, please refer to please refer to `Introduction to Tensor`_ . + .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor. + Args: + x (Tensor): Input Tensor of ``bitwise_left_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. + y (Tensor): Input Tensor of ``bitwise_left_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. + name (str, optional): The default value is None. Normally there is no need for + user to set this property. For more information, please refer to :ref:`api_guide_Name`. + Returns: + Tensor: Result of ``bitwise_left_shift`` . It is a N-D Tensor with the same data type of input Tensor. + Examples: + .. code-block:: python + >>> import paddle + >>> x= paddle.to_tensor([[1,2,4,8],[16,17,32,65]]) + >>> y = paddle.to_tensor([[1,2,3,4,], [2,3,2,1]]) + >>> paddle.bitwise_left_shift(x, y) + Tensor(shape=[2, 4], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [[2 , 8 , 32 , 128], + [64 , 136, 128, 130]]) + """ + if in_dynamic_mode() and out is None: + if is_arithmetic: + return _C_ops.bitwise_left_shift_arithmetic(x, y) + else: + return _C_ops.bitwise_left_shift_logic(x, y) + if is_arithmetic: + return _bitwise_op( + op_name="bitwise_left_shift_arithmetic", + x=x, + y=y, + name=name, + out=out, + ) + else: + return _bitwise_op( + op_name="bitwise_left_shift_logic", + x=x, + y=y, + name=name, + out=out, + ) + + +@inplace_apis_in_dygraph_only +def bitwise_left_shift_(x, y, is_arithmetic=True, out=None, name=None): + r""" + Inplace version of ``bitwise_left_shift`` API, the output Tensor will be inplaced with input ``x``. + Please refer to :ref:`api_paddle_bitwise_left_shift`. + """ + out_shape = broadcast_shape(x.shape, y.shape) + if out_shape != x.shape: + raise ValueError( + "The shape of broadcast output {} is different from that of inplace tensor {} in the Inplace operation.".format( + out_shape, x.shape + ) + ) + if in_dynamic_or_pir_mode(): + if is_arithmetic: + return _C_ops.bitwise_left_shift_arithmetic_(x, y) + else: + return _C_ops.bitwise_left_shift_logic_(x, y) + + +def bitwise_right_shift(x, y, is_arithmetic=True, out=None, name=None): + r""" + Apply ``bitwise_right_shift`` on Tensor ``X`` and ``Y`` . + .. math:: + Out = X \gg Y + .. note:: + ``paddle.bitwise_right_shift`` supports broadcasting. If you want know more about broadcasting, please refer to please refer to `Introduction to Tensor`_ . + .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor. + Args: + x (Tensor): Input Tensor of ``bitwise_right_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. + y (Tensor): Input Tensor of ``bitwise_right_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. + name (str, optional): The default value is None. Normally there is no need for + user to set this property. For more information, please refer to :ref:`api_guide_Name`. + Returns: + Tensor: Result of ``bitwise_right_shift`` . It is a N-D Tensor with the same data type of input Tensor. + Examples: + .. code-block:: python + >>> import paddle + >>> x= paddle.to_tensor([[10,20,40,80],[16,17,32,65]]) + >>> y = paddle.to_tensor([[1,2,3,4,], [2,3,2,1]]) + >>> paddle.bitwise_right_shift(x, y) + Tensor(shape=[2, 4], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [[5 , 5 , 5 , 5 ], + [4 , 2 , 8 , 32]]) + """ + if in_dynamic_mode() and out is None: + if is_arithmetic: + return _C_ops.bitwise_right_shift_arithmetic(x, y) + else: + return _C_ops.bitwise_right_shift_logic(x, y) + if is_arithmetic: + return _bitwise_op( + op_name="bitwise_right_shift_arithmetic", + x=x, + y=y, + name=name, + out=out, + ) + else: + return _bitwise_op( + op_name="bitwise_right_shift_logic", + x=x, + y=y, + name=name, + out=out, + ) + + +@inplace_apis_in_dygraph_only +def bitwise_right_shift_(x, y, is_arithmetic=True, out=None, name=None): + r""" + Inplace version of ``bitwise_right_shift`` API, the output Tensor will be inplaced with input ``x``. + Please refer to :ref:`api_paddle_bitwise_left_shift`. + """ + out_shape = broadcast_shape(x.shape, y.shape) + if out_shape != x.shape: + raise ValueError( + "The shape of broadcast output {} is different from that of inplace tensor {} in the Inplace operation.".format( + out_shape, x.shape + ) + ) + + if in_dynamic_or_pir_mode(): + if is_arithmetic: + return _C_ops.bitwise_right_shift_arithmetic_(x, y) + else: + return _C_ops.bitwise_right_shift_logic_(x, y) def hypot(x, y, name=None): diff --git a/test/legacy_test/test_bitwise_shift_op.py b/test/legacy_test/test_bitwise_shift_op.py index b2dca38a6763a..9b957891de7b1 100644 --- a/test/legacy_test/test_bitwise_shift_op.py +++ b/test/legacy_test/test_bitwise_shift_op.py @@ -1,414 +1,414 @@ -# # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. -# # -# # Licensed under the Apache License, Version 2.0 (the "License"); -# # you may not use this file except in compliance with the License. -# # You may obtain a copy of the License at -# # -# # http://www.apache.org/licenses/LICENSE-2.0 -# # -# # Unless required by applicable law or agreed to in writing, software -# # distributed under the License is distributed on an "AS IS" BASIS, -# # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# # See the License for the specific language governing permissions and -# # limitations under the License. - -# import unittest - -# import numpy as np - -# import paddle - -# _SIGNED_TO_UNSIGNED_TABLE = { -# "int8": "uint8", -# "int16": "uint16", -# "int32": "uint32", -# "int64": "uint64", -# } - -# _UNSIGNED_TO_SIGNED_TABLE = { -# "uint8": "int8", -# "uint16": "int16", -# "uint32": "int32", -# "uint64": "int64", -# } - -# _UNSIGNED_LIST = ['uint8', 'uint16', 'uint32', 'uint64'] - - -# def ref_left_shift_arithmetic(x, y): -# out = np.left_shift(x, y) -# return out - - -# def ref_left_shift_logical(x, y): -# out = np.left_shift(x, y) -# return out - - -# def ref_right_shift_arithmetic(x, y): -# return np.right_shift(x, y) - - -# def ref_right_shift_logical(x, y): -# if str(x.dtype) in _UNSIGNED_LIST: -# return np.right_shift(x, y) -# else: -# orig_dtype = x.dtype -# unsigned_dtype = _SIGNED_TO_UNSIGNED_TABLE[str(orig_dtype)] -# x = x.astype(unsigned_dtype) -# y = y.astype(unsigned_dtype) -# res = np.right_shift(x, y) -# return res.astype(orig_dtype) - - -# class TestBitwiseLeftShiftAPI(unittest.TestCase): -# def setUp(self): -# self.init_input() -# self.place = ( -# paddle.CUDAPlace(0) -# if paddle.is_compiled_with_cuda() -# else paddle.CPUPlace() -# ) - -# def init_input(self): -# self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') -# self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') - -# def test_static_api_arithmetic(self): -# paddle.enable_static() -# with paddle.static.program_guard(paddle.static.Program()): -# x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) -# y = paddle.static.data('y', self.y.shape, dtype=self.y.dtype) -# out = paddle.bitwise_left_shift( -# x, -# y, -# ) -# exe = paddle.static.Executor(self.place) -# res = exe.run(feed={'x': self.x, 'y': self.y}, fetch_list=[out]) -# out_ref = ref_left_shift_arithmetic(self.x, self.y) -# np.testing.assert_allclose(out_ref, res[0]) - -# def test_dygraph_api_arithmetic(self): -# paddle.disable_static() -# x = paddle.to_tensor(self.x) -# y = paddle.to_tensor(self.y) -# out = paddle.bitwise_left_shift( -# x, -# y, -# ) -# out_ref = ref_left_shift_arithmetic(self.x, self.y) -# np.testing.assert_allclose(out_ref, out.numpy()) -# paddle.enable_static() - -# def test_static_api_logical(self): -# paddle.enable_static() -# with paddle.static.program_guard(paddle.static.Program()): -# x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) -# y = paddle.static.data('y', self.y.shape, dtype=self.y.dtype) -# out = paddle.bitwise_left_shift(x, y, False) -# exe = paddle.static.Executor(self.place) -# res = exe.run(feed={'x': self.x, 'y': self.y}, fetch_list=[out]) -# out_ref = ref_left_shift_logical(self.x, self.y) -# np.testing.assert_allclose(out_ref, res[0]) +# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest + +import numpy as np + +import paddle + +_SIGNED_TO_UNSIGNED_TABLE = { + "int8": "uint8", + "int16": "uint16", + "int32": "uint32", + "int64": "uint64", +} + +_UNSIGNED_TO_SIGNED_TABLE = { + "uint8": "int8", + "uint16": "int16", + "uint32": "int32", + "uint64": "int64", +} + +_UNSIGNED_LIST = ['uint8', 'uint16', 'uint32', 'uint64'] + + +def ref_left_shift_arithmetic(x, y): + out = np.left_shift(x, y) + return out + + +def ref_left_shift_logical(x, y): + out = np.left_shift(x, y) + return out + + +def ref_right_shift_arithmetic(x, y): + return np.right_shift(x, y) + + +def ref_right_shift_logical(x, y): + if str(x.dtype) in _UNSIGNED_LIST: + return np.right_shift(x, y) + else: + orig_dtype = x.dtype + unsigned_dtype = _SIGNED_TO_UNSIGNED_TABLE[str(orig_dtype)] + x = x.astype(unsigned_dtype) + y = y.astype(unsigned_dtype) + res = np.right_shift(x, y) + return res.astype(orig_dtype) + + +class TestBitwiseLeftShiftAPI(unittest.TestCase): + def setUp(self): + self.init_input() + self.place = ( + paddle.CUDAPlace(0) + if paddle.is_compiled_with_cuda() + else paddle.CPUPlace() + ) + + def init_input(self): + self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') + self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') + + def test_static_api_arithmetic(self): + paddle.enable_static() + with paddle.static.program_guard(paddle.static.Program()): + x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) + y = paddle.static.data('y', self.y.shape, dtype=self.y.dtype) + out = paddle.bitwise_left_shift( + x, + y, + ) + exe = paddle.static.Executor(self.place) + res = exe.run(feed={'x': self.x, 'y': self.y}, fetch_list=[out]) + out_ref = ref_left_shift_arithmetic(self.x, self.y) + np.testing.assert_allclose(out_ref, res[0]) + + def test_dygraph_api_arithmetic(self): + paddle.disable_static() + x = paddle.to_tensor(self.x) + y = paddle.to_tensor(self.y) + out = paddle.bitwise_left_shift( + x, + y, + ) + out_ref = ref_left_shift_arithmetic(self.x, self.y) + np.testing.assert_allclose(out_ref, out.numpy()) + paddle.enable_static() + + def test_static_api_logical(self): + paddle.enable_static() + with paddle.static.program_guard(paddle.static.Program()): + x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) + y = paddle.static.data('y', self.y.shape, dtype=self.y.dtype) + out = paddle.bitwise_left_shift(x, y, False) + exe = paddle.static.Executor(self.place) + res = exe.run(feed={'x': self.x, 'y': self.y}, fetch_list=[out]) + out_ref = ref_left_shift_logical(self.x, self.y) + np.testing.assert_allclose(out_ref, res[0]) -# def test_dygraph_api_logical(self): -# paddle.disable_static() -# x = paddle.to_tensor(self.x) -# y = paddle.to_tensor(self.y) -# out = paddle.bitwise_left_shift(x, y, False) -# out_ref = ref_left_shift_logical(self.x, self.y) -# np.testing.assert_allclose(out_ref, out.numpy()) -# paddle.enable_static() + def test_dygraph_api_logical(self): + paddle.disable_static() + x = paddle.to_tensor(self.x) + y = paddle.to_tensor(self.y) + out = paddle.bitwise_left_shift(x, y, False) + out_ref = ref_left_shift_logical(self.x, self.y) + np.testing.assert_allclose(out_ref, out.numpy()) + paddle.enable_static() -# class TestBitwiseLeftShiftAPI_UINT8(TestBitwiseLeftShiftAPI): -# def init_input(self): -# self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') -# self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') +class TestBitwiseLeftShiftAPI_UINT8(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') + self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') -# class TestBitwiseLeftShiftAPI_UINT8_broadcast1(TestBitwiseLeftShiftAPI): -# def init_input(self): -# self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') -# self.y = np.random.randint(0, 256, [3]).astype('uint8') +class TestBitwiseLeftShiftAPI_UINT8_broadcast1(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') + self.y = np.random.randint(0, 256, [3]).astype('uint8') -# class TestBitwiseLeftShiftAPI_UINT8_broadcast2(TestBitwiseLeftShiftAPI): -# def init_input(self): -# self.x = np.random.randint(0, 256, [3]).astype('uint8') -# self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') +class TestBitwiseLeftShiftAPI_UINT8_broadcast2(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(0, 256, [3]).astype('uint8') + self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') -# class TestBitwiseLeftShiftAPI_INT8(TestBitwiseLeftShiftAPI): -# def init_input(self): -# self.x = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') -# self.y = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') +class TestBitwiseLeftShiftAPI_INT8(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') + self.y = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') -# class TestBitwiseLeftShiftAPI_INT8_broadcast1(TestBitwiseLeftShiftAPI): -# def init_input(self): -# self.x = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') -# self.y = np.random.randint(-(2**7), 2**7, [3]).astype('int8') - - -# class TestBitwiseLeftShiftAPI_INT8_broadcast2(TestBitwiseLeftShiftAPI): -# def init_input(self): -# self.x = np.random.randint(-(2**7), 2**7, [3]).astype('int8') -# self.y = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') - - -# class TestBitwiseLeftShiftAPI_INT16(TestBitwiseLeftShiftAPI): -# def init_input(self): -# self.x = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') -# self.y = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') - - -# class TestBitwiseLeftShiftAPI_INT16_broadcast1(TestBitwiseLeftShiftAPI): -# def init_input(self): -# self.x = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') -# self.y = np.random.randint(-(2**15), 2**15, [3]).astype('int16') - - -# class TestBitwiseLeftShiftAPI_INT16_broadcast2(TestBitwiseLeftShiftAPI): -# def init_input(self): -# self.x = np.random.randint(-(2**15), 2**15, [3]).astype('int16') -# self.y = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') - - -# class TestBitwiseLeftShiftAPI_INT32(TestBitwiseLeftShiftAPI): -# def init_input(self): -# self.x = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') -# self.y = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') - - -# class TestBitwiseLeftShiftAPI_INT32_broadcast1(TestBitwiseLeftShiftAPI): -# def init_input(self): -# self.x = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') -# self.y = np.random.randint(-(2**31), 2**31, [3]).astype('int32') - - -# class TestBitwiseLeftShiftAPI_INT32_broadcast2(TestBitwiseLeftShiftAPI): -# def init_input(self): -# self.x = np.random.randint(-(2**31), 2**31, [3]).astype('int32') -# self.y = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') - - -# class TestBitwiseLeftShiftAPI_INT64(TestBitwiseLeftShiftAPI): -# def init_input(self): -# self.x = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) -# self.y = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) - - -# class TestBitwiseLeftShiftAPI_INT64_broadcast1(TestBitwiseLeftShiftAPI): -# def init_input(self): -# self.x = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) -# self.y = np.random.randint(-(2**63), 2**63, [3], dtype=np.int64) +class TestBitwiseLeftShiftAPI_INT8_broadcast1(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') + self.y = np.random.randint(-(2**7), 2**7, [3]).astype('int8') + + +class TestBitwiseLeftShiftAPI_INT8_broadcast2(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**7), 2**7, [3]).astype('int8') + self.y = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') + + +class TestBitwiseLeftShiftAPI_INT16(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') + self.y = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') + + +class TestBitwiseLeftShiftAPI_INT16_broadcast1(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') + self.y = np.random.randint(-(2**15), 2**15, [3]).astype('int16') + + +class TestBitwiseLeftShiftAPI_INT16_broadcast2(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**15), 2**15, [3]).astype('int16') + self.y = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') + + +class TestBitwiseLeftShiftAPI_INT32(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') + self.y = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') + + +class TestBitwiseLeftShiftAPI_INT32_broadcast1(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') + self.y = np.random.randint(-(2**31), 2**31, [3]).astype('int32') + + +class TestBitwiseLeftShiftAPI_INT32_broadcast2(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**31), 2**31, [3]).astype('int32') + self.y = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') + + +class TestBitwiseLeftShiftAPI_INT64(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) + self.y = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) + + +class TestBitwiseLeftShiftAPI_INT64_broadcast1(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) + self.y = np.random.randint(-(2**63), 2**63, [3], dtype=np.int64) -# class TestBitwiseLeftShiftAPI_INT64_broadcast2(TestBitwiseLeftShiftAPI): -# def init_input(self): -# self.x = np.random.randint(-(2**63), 2**63, [3], dtype=np.int64) -# self.y = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) +class TestBitwiseLeftShiftAPI_INT64_broadcast2(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**63), 2**63, [3], dtype=np.int64) + self.y = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) -# class TestBitwiseLeftShiftAPI_special_case1(TestBitwiseLeftShiftAPI): -# def init_input(self): -# self.x = np.array([0b11111111], dtype='int8') -# self.y = np.array([1], dtype='int8') +class TestBitwiseLeftShiftAPI_special_case1(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.array([0b11111111], dtype='int8') + self.y = np.array([1], dtype='int8') -# class TestBitwiseLeftShiftAPI_special_case2(TestBitwiseLeftShiftAPI): -# def init_input(self): -# self.x = np.array([0b11111111], dtype='int8') -# self.y = np.array([10], dtype='int8') +class TestBitwiseLeftShiftAPI_special_case2(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.array([0b11111111], dtype='int8') + self.y = np.array([10], dtype='int8') -# class TestBitwiseLeftShiftAPI_special_case3(TestBitwiseLeftShiftAPI): -# def init_input(self): -# self.x = np.array([0b11111111], dtype='uint8') -# self.y = np.array([1], dtype='uint8') +class TestBitwiseLeftShiftAPI_special_case3(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.array([0b11111111], dtype='uint8') + self.y = np.array([1], dtype='uint8') -# class TestBitwiseLeftShiftAPI_special_case4(TestBitwiseLeftShiftAPI): -# def init_input(self): -# self.x = np.array([0b11111111], dtype='uint8') -# self.y = np.array([10], dtype='uint8') +class TestBitwiseLeftShiftAPI_special_case4(TestBitwiseLeftShiftAPI): + def init_input(self): + self.x = np.array([0b11111111], dtype='uint8') + self.y = np.array([10], dtype='uint8') -# class TestBitwiseRightShiftAPI(unittest.TestCase): -# def setUp(self): -# self.init_input() -# self.place = ( -# paddle.CUDAPlace(0) -# if paddle.is_compiled_with_cuda() -# else paddle.CPUPlace() -# ) +class TestBitwiseRightShiftAPI(unittest.TestCase): + def setUp(self): + self.init_input() + self.place = ( + paddle.CUDAPlace(0) + if paddle.is_compiled_with_cuda() + else paddle.CPUPlace() + ) -# def init_input(self): -# self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') -# self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') + def init_input(self): + self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') + self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') -# def test_static_api_arithmetic(self): -# paddle.enable_static() -# with paddle.static.program_guard(paddle.static.Program()): -# x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) -# y = paddle.static.data('y', self.y.shape, dtype=self.y.dtype) -# out = paddle.bitwise_right_shift( -# x, -# y, -# ) -# exe = paddle.static.Executor(self.place) -# res = exe.run(feed={'x': self.x, 'y': self.y}, fetch_list=[out]) -# out_ref = ref_right_shift_arithmetic(self.x, self.y) -# np.testing.assert_allclose(out_ref, res[0]) + def test_static_api_arithmetic(self): + paddle.enable_static() + with paddle.static.program_guard(paddle.static.Program()): + x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) + y = paddle.static.data('y', self.y.shape, dtype=self.y.dtype) + out = paddle.bitwise_right_shift( + x, + y, + ) + exe = paddle.static.Executor(self.place) + res = exe.run(feed={'x': self.x, 'y': self.y}, fetch_list=[out]) + out_ref = ref_right_shift_arithmetic(self.x, self.y) + np.testing.assert_allclose(out_ref, res[0]) -# def test_dygraph_api_arithmetic(self): -# paddle.disable_static() -# x = paddle.to_tensor(self.x) -# y = paddle.to_tensor(self.y) -# out = paddle.bitwise_right_shift( -# x, -# y, -# ) -# out_ref = ref_right_shift_arithmetic(self.x, self.y) -# np.testing.assert_allclose(out_ref, out.numpy()) -# paddle.enable_static() + def test_dygraph_api_arithmetic(self): + paddle.disable_static() + x = paddle.to_tensor(self.x) + y = paddle.to_tensor(self.y) + out = paddle.bitwise_right_shift( + x, + y, + ) + out_ref = ref_right_shift_arithmetic(self.x, self.y) + np.testing.assert_allclose(out_ref, out.numpy()) + paddle.enable_static() -# def test_static_api_logical(self): -# paddle.enable_static() -# with paddle.static.program_guard(paddle.static.Program()): -# x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) -# y = paddle.static.data('y', self.y.shape, dtype=self.y.dtype) -# out = paddle.bitwise_right_shift(x, y, False) -# exe = paddle.static.Executor(self.place) -# res = exe.run(feed={'x': self.x, 'y': self.y}, fetch_list=[out]) -# out_ref = ref_right_shift_logical(self.x, self.y) -# np.testing.assert_allclose(out_ref, res[0]) + def test_static_api_logical(self): + paddle.enable_static() + with paddle.static.program_guard(paddle.static.Program()): + x = paddle.static.data('x', self.x.shape, dtype=self.x.dtype) + y = paddle.static.data('y', self.y.shape, dtype=self.y.dtype) + out = paddle.bitwise_right_shift(x, y, False) + exe = paddle.static.Executor(self.place) + res = exe.run(feed={'x': self.x, 'y': self.y}, fetch_list=[out]) + out_ref = ref_right_shift_logical(self.x, self.y) + np.testing.assert_allclose(out_ref, res[0]) -# def test_dygraph_api_logical(self): -# paddle.disable_static() -# x = paddle.to_tensor(self.x) -# y = paddle.to_tensor(self.y) -# out = paddle.bitwise_right_shift(x, y, False) -# out_ref = ref_right_shift_logical(self.x, self.y) -# np.testing.assert_allclose(out_ref, out.numpy()) -# paddle.enable_static() + def test_dygraph_api_logical(self): + paddle.disable_static() + x = paddle.to_tensor(self.x) + y = paddle.to_tensor(self.y) + out = paddle.bitwise_right_shift(x, y, False) + out_ref = ref_right_shift_logical(self.x, self.y) + np.testing.assert_allclose(out_ref, out.numpy()) + paddle.enable_static() -# class TestBitwiseRightShiftAPI_UINT8(TestBitwiseRightShiftAPI): -# def init_input(self): -# self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') -# self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') +class TestBitwiseRightShiftAPI_UINT8(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') + self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') -# class TestBitwiseRightShiftAPI_UINT8_broadcast1(TestBitwiseRightShiftAPI): -# def init_input(self): -# self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') -# self.y = np.random.randint(0, 256, [3]).astype('uint8') +class TestBitwiseRightShiftAPI_UINT8_broadcast1(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') + self.y = np.random.randint(0, 256, [3]).astype('uint8') -# class TestBitwiseRightShiftAPI_UINT8_broadcast2(TestBitwiseRightShiftAPI): -# def init_input(self): -# self.x = np.random.randint(0, 256, [3]).astype('uint8') -# self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') +class TestBitwiseRightShiftAPI_UINT8_broadcast2(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(0, 256, [3]).astype('uint8') + self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') -# class TestBitwiseRightShiftAPI_INT8(TestBitwiseRightShiftAPI): -# def init_input(self): -# self.x = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') -# self.y = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') +class TestBitwiseRightShiftAPI_INT8(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') + self.y = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') -# class TestBitwiseRightShiftAPI_INT8_broadcast1(TestBitwiseRightShiftAPI): -# def init_input(self): -# self.x = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') -# self.y = np.random.randint(-(2**7), 2**7, [3]).astype('int8') +class TestBitwiseRightShiftAPI_INT8_broadcast1(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') + self.y = np.random.randint(-(2**7), 2**7, [3]).astype('int8') -# class TestBitwiseRightShiftAPI_INT8_broadcast2(TestBitwiseRightShiftAPI): -# def init_input(self): -# self.x = np.random.randint(-(2**7), 2**7, [3]).astype('int8') -# self.y = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') +class TestBitwiseRightShiftAPI_INT8_broadcast2(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**7), 2**7, [3]).astype('int8') + self.y = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') -# class TestBitwiseRightShiftAPI_INT16(TestBitwiseRightShiftAPI): -# def init_input(self): -# self.x = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') -# self.y = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') +class TestBitwiseRightShiftAPI_INT16(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') + self.y = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') -# class TestBitwiseRightShiftAPI_INT16_broadcast1(TestBitwiseRightShiftAPI): -# def init_input(self): -# self.x = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') -# self.y = np.random.randint(-(2**15), 2**15, [3]).astype('int16') +class TestBitwiseRightShiftAPI_INT16_broadcast1(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') + self.y = np.random.randint(-(2**15), 2**15, [3]).astype('int16') -# class TestBitwiseRightShiftAPI_INT16_broadcast2(TestBitwiseRightShiftAPI): -# def init_input(self): -# self.x = np.random.randint(-(2**15), 2**15, [3]).astype('int16') -# self.y = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') +class TestBitwiseRightShiftAPI_INT16_broadcast2(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**15), 2**15, [3]).astype('int16') + self.y = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') -# class TestBitwiseRightShiftAPI_INT32(TestBitwiseRightShiftAPI): -# def init_input(self): -# self.x = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') -# self.y = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') +class TestBitwiseRightShiftAPI_INT32(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') + self.y = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') -# class TestBitwiseRightShiftAPI_INT32_broadcast1(TestBitwiseRightShiftAPI): -# def init_input(self): -# self.x = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') -# self.y = np.random.randint(-(2**31), 2**31, [3]).astype('int32') +class TestBitwiseRightShiftAPI_INT32_broadcast1(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') + self.y = np.random.randint(-(2**31), 2**31, [3]).astype('int32') -# class TestBitwiseRightShiftAPI_INT32_broadcast2(TestBitwiseRightShiftAPI): -# def init_input(self): -# self.x = np.random.randint(-(2**31), 2**31, [3]).astype('int32') -# self.y = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') +class TestBitwiseRightShiftAPI_INT32_broadcast2(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**31), 2**31, [3]).astype('int32') + self.y = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') -# class TestBitwiseRightShiftAPI_INT64(TestBitwiseRightShiftAPI): -# def init_input(self): -# self.x = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) -# self.y = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) +class TestBitwiseRightShiftAPI_INT64(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) + self.y = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) -# class TestBitwiseRightShiftAPI_INT64_broadcast1(TestBitwiseRightShiftAPI): -# def init_input(self): -# self.x = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) -# self.y = np.random.randint(-(2**63), 2**63, [3], dtype=np.int64) +class TestBitwiseRightShiftAPI_INT64_broadcast1(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) + self.y = np.random.randint(-(2**63), 2**63, [3], dtype=np.int64) -# class TestBitwiseRightShiftAPI_INT64_broadcast2(TestBitwiseRightShiftAPI): -# def init_input(self): -# self.x = np.random.randint(-(2**63), 2**63, [3], dtype=np.int64) -# self.y = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) +class TestBitwiseRightShiftAPI_INT64_broadcast2(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.random.randint(-(2**63), 2**63, [3], dtype=np.int64) + self.y = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) -# class TestBitwiseRightShiftAPI_special_case1(TestBitwiseRightShiftAPI): -# def init_input(self): -# self.x = np.array([0b11111111]).astype('int8') -# self.y = np.array([1]).astype('int8') +class TestBitwiseRightShiftAPI_special_case1(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.array([0b11111111]).astype('int8') + self.y = np.array([1]).astype('int8') -# class TestBitwiseRightShiftAPI_special_case2(TestBitwiseRightShiftAPI): -# def init_input(self): -# self.x = np.array([0b11111111]).astype('int8') -# self.y = np.array([10]).astype('int8') +class TestBitwiseRightShiftAPI_special_case2(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.array([0b11111111]).astype('int8') + self.y = np.array([10]).astype('int8') -# class TestBitwiseRightShiftAPI_special_case3(TestBitwiseRightShiftAPI): -# def init_input(self): -# self.x = np.array([0b11111111], dtype='uint8') -# self.y = np.array([1], dtype='uint8') +class TestBitwiseRightShiftAPI_special_case3(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.array([0b11111111], dtype='uint8') + self.y = np.array([1], dtype='uint8') -# class TestBitwiseRightShiftAPI_special_case4(TestBitwiseRightShiftAPI): -# def init_input(self): -# self.x = np.array([0b11111111], dtype='uint8') -# self.y = np.array([10], dtype='uint8') +class TestBitwiseRightShiftAPI_special_case4(TestBitwiseRightShiftAPI): + def init_input(self): + self.x = np.array([0b11111111], dtype='uint8') + self.y = np.array([10], dtype='uint8') -# if __name__ == '__main__': -# paddle.enable_static() -# unittest.main() +if __name__ == '__main__': + paddle.enable_static() + unittest.main() diff --git a/test/legacy_test/test_inplace.py b/test/legacy_test/test_inplace.py index 565729e8ac47e..20e2960c29481 100644 --- a/test/legacy_test/test_inplace.py +++ b/test/legacy_test/test_inplace.py @@ -1633,106 +1633,106 @@ def test_forward_version(self): self.assertEqual(var.inplace_version, 3) -# class TestDygraphInplaceBitwiseLeftShift_arithmetic(TestDygraphInplaceLogicAnd): -# def init_data(self): -# self.input_var_numpy = np.random.randint( -# low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" -# ) -# self.input_var_numpy = paddle.to_tensor(self.input_var_numpy) -# self.dtype = "int32" -# self.y = np.random.randint( -# low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" -# ) -# self.y = paddle.to_tensor(self.y) -# self.is_arithmetic = True - -# def inplace_api_processing(self, var): -# return paddle.bitwise_left_shift_(var, self.y, self.is_arithmetic) - -# def non_inplace_api_processing(self, var): -# return paddle.bitwise_left_shift(var, self.y, self.is_arithmetic) - -# def test_broadcast_error(self): -# broadcast_input = paddle.randn([4, 5]) -# with self.assertRaises(ValueError): -# self.inplace_api_processing(broadcast_input) - - -# class TestDygraphInplaceBitwiseRightShift_arithmetic( -# TestDygraphInplaceLogicAnd -# ): -# def init_data(self): -# self.input_var_numpy = np.random.randint( -# low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" -# ) -# self.input_var_numpy = paddle.to_tensor(self.input_var_numpy) -# self.dtype = "int32" -# self.y = np.random.randint( -# low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" -# ) -# self.y = paddle.to_tensor(self.y) -# self.is_arithmetic = True - -# def inplace_api_processing(self, var): -# return paddle.bitwise_right_shift_(var, self.y, self.is_arithmetic) - -# def non_inplace_api_processing(self, var): -# return paddle.bitwise_right_shift_(var, self.y, self.is_arithmetic) - -# def test_broadcast_error(self): -# broadcast_input = paddle.randn([4, 5]) -# with self.assertRaises(ValueError): -# self.inplace_api_processing(broadcast_input) - - -# class TestDygraphInplaceBitwiseLeftShift_logic(TestDygraphInplaceLogicAnd): -# def init_data(self): -# self.input_var_numpy = np.random.randint( -# low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" -# ) -# self.input_var_numpy = paddle.to_tensor(self.input_var_numpy) -# self.dtype = "int32" -# self.y = np.random.randint( -# low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" -# ) -# self.y = paddle.to_tensor(self.y) -# self.is_arithmetic = False - -# def inplace_api_processing(self, var): -# return paddle.bitwise_left_shift_(var, self.y, self.is_arithmetic) - -# def non_inplace_api_processing(self, var): -# return paddle.bitwise_left_shift(var, self.y, self.is_arithmetic) - -# def test_broadcast_error(self): -# broadcast_input = paddle.randn([4, 5]) -# with self.assertRaises(ValueError): -# self.inplace_api_processing(broadcast_input) - - -# class TestDygraphInplaceBitwiseRightShift_logic(TestDygraphInplaceLogicAnd): -# def init_data(self): -# self.input_var_numpy = np.random.randint( -# low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" -# ) -# self.input_var_numpy = paddle.to_tensor(self.input_var_numpy) -# self.dtype = "int32" -# self.y = np.random.randint( -# low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" -# ) -# self.y = paddle.to_tensor(self.y) -# self.is_arithmetic = False - -# def inplace_api_processing(self, var): -# return paddle.bitwise_right_shift_(var, self.y, self.is_arithmetic) - -# def non_inplace_api_processing(self, var): -# return paddle.bitwise_right_shift_(var, self.y, self.is_arithmetic) - -# def test_broadcast_error(self): -# broadcast_input = paddle.randn([4, 5]) -# with self.assertRaises(ValueError): -# self.inplace_api_processing(broadcast_input) +class TestDygraphInplaceBitwiseLeftShift_arithmetic(TestDygraphInplaceLogicAnd): + def init_data(self): + self.input_var_numpy = np.random.randint( + low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" + ) + self.input_var_numpy = paddle.to_tensor(self.input_var_numpy) + self.dtype = "int32" + self.y = np.random.randint( + low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" + ) + self.y = paddle.to_tensor(self.y) + self.is_arithmetic = True + + def inplace_api_processing(self, var): + return paddle.bitwise_left_shift_(var, self.y, self.is_arithmetic) + + def non_inplace_api_processing(self, var): + return paddle.bitwise_left_shift(var, self.y, self.is_arithmetic) + + def test_broadcast_error(self): + broadcast_input = paddle.randn([4, 5]) + with self.assertRaises(ValueError): + self.inplace_api_processing(broadcast_input) + + +class TestDygraphInplaceBitwiseRightShift_arithmetic( + TestDygraphInplaceLogicAnd +): + def init_data(self): + self.input_var_numpy = np.random.randint( + low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" + ) + self.input_var_numpy = paddle.to_tensor(self.input_var_numpy) + self.dtype = "int32" + self.y = np.random.randint( + low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" + ) + self.y = paddle.to_tensor(self.y) + self.is_arithmetic = True + + def inplace_api_processing(self, var): + return paddle.bitwise_right_shift_(var, self.y, self.is_arithmetic) + + def non_inplace_api_processing(self, var): + return paddle.bitwise_right_shift_(var, self.y, self.is_arithmetic) + + def test_broadcast_error(self): + broadcast_input = paddle.randn([4, 5]) + with self.assertRaises(ValueError): + self.inplace_api_processing(broadcast_input) + + +class TestDygraphInplaceBitwiseLeftShift_logic(TestDygraphInplaceLogicAnd): + def init_data(self): + self.input_var_numpy = np.random.randint( + low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" + ) + self.input_var_numpy = paddle.to_tensor(self.input_var_numpy) + self.dtype = "int32" + self.y = np.random.randint( + low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" + ) + self.y = paddle.to_tensor(self.y) + self.is_arithmetic = False + + def inplace_api_processing(self, var): + return paddle.bitwise_left_shift_(var, self.y, self.is_arithmetic) + + def non_inplace_api_processing(self, var): + return paddle.bitwise_left_shift(var, self.y, self.is_arithmetic) + + def test_broadcast_error(self): + broadcast_input = paddle.randn([4, 5]) + with self.assertRaises(ValueError): + self.inplace_api_processing(broadcast_input) + + +class TestDygraphInplaceBitwiseRightShift_logic(TestDygraphInplaceLogicAnd): + def init_data(self): + self.input_var_numpy = np.random.randint( + low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" + ) + self.input_var_numpy = paddle.to_tensor(self.input_var_numpy) + self.dtype = "int32" + self.y = np.random.randint( + low=-(2**31), high=2**31, size=[3, 4, 5], dtype="int32" + ) + self.y = paddle.to_tensor(self.y) + self.is_arithmetic = False + + def inplace_api_processing(self, var): + return paddle.bitwise_right_shift_(var, self.y, self.is_arithmetic) + + def non_inplace_api_processing(self, var): + return paddle.bitwise_right_shift_(var, self.y, self.is_arithmetic) + + def test_broadcast_error(self): + broadcast_input = paddle.randn([4, 5]) + with self.assertRaises(ValueError): + self.inplace_api_processing(broadcast_input) class TestDygraphInplaceIndexFill(TestDygraphInplace): def init_data(self): From 994d832916846d7e9fe79365a56d2a8c2f9cdc26 Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Wed, 27 Dec 2023 17:30:33 +0800 Subject: [PATCH 06/27] update --- paddle/phi/api/yaml/ops.yaml | 40 ++++++++++++++++---------------- python/paddle/tensor/math.py | 1 + test/legacy_test/test_inplace.py | 1 + 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index 56f75a87a10bf..b8aeac47226cc 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -332,75 +332,75 @@ kernel : func : binomial -- op : bitwise_left_shift_arithmetic +- op : bitwise_and args : (Tensor x, Tensor y) output : Tensor(out) infer_meta : func : ElementwiseInferMeta + spmd_rule : ElementwiseBinaryInferSpmd kernel : - func : bitwise_left_shift_arithmetic + func : bitwise_and backend : x inplace: (x -> out) -- op : bitwise_left_shift_logic +- op : bitwise_left_shift_arithmetic args : (Tensor x, Tensor y) output : Tensor(out) infer_meta : func : ElementwiseInferMeta kernel : - func : bitwise_left_shift_logic + func : bitwise_left_shift_arithmetic backend : x inplace: (x -> out) -- op : bitwise_right_shift_arithmetic +- op : bitwise_left_shift_logic args : (Tensor x, Tensor y) output : Tensor(out) infer_meta : func : ElementwiseInferMeta kernel : - func : bitwise_right_shift_arithmetic + func : bitwise_left_shift_logic backend : x inplace: (x -> out) -- op : bitwise_right_shift_logic - args : (Tensor x, Tensor y) +- op : bitwise_not + args : (Tensor x) output : Tensor(out) infer_meta : - func : ElementwiseInferMeta + func : UnchangedInferMeta + spmd_rule : ElementwiseUnaryInferSpmd kernel : - func : bitwise_right_shift_logic + func : bitwise_not backend : x inplace: (x -> out) -- op : bitwise_and +- op : bitwise_or args : (Tensor x, Tensor y) output : Tensor(out) infer_meta : func : ElementwiseInferMeta - spmd_rule : ElementwiseBinaryInferSpmd kernel : - func : bitwise_and + func : bitwise_or backend : x inplace: (x -> out) -- op : bitwise_not - args : (Tensor x) +- op : bitwise_right_shift_arithmetic + args : (Tensor x, Tensor y) output : Tensor(out) infer_meta : - func : UnchangedInferMeta - spmd_rule : ElementwiseUnaryInferSpmd + func : ElementwiseInferMeta kernel : - func : bitwise_not + func : bitwise_right_shift_arithmetic backend : x inplace: (x -> out) -- op : bitwise_or +- op : bitwise_right_shift_logic args : (Tensor x, Tensor y) output : Tensor(out) infer_meta : func : ElementwiseInferMeta kernel : - func : bitwise_or + func : bitwise_right_shift_logic backend : x inplace: (x -> out) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 8ed362f04b5f0..b90cbfbf6044e 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -7052,6 +7052,7 @@ def ldexp_(x, y, name=None): two = paddle.to_tensor(2, dtype=out_dtype) return paddle.multiply_(x, paddle.pow(two, y)) + def _bitwise_op(op_name, x, y, out=None, name=None): check_variable_and_dtype( x, diff --git a/test/legacy_test/test_inplace.py b/test/legacy_test/test_inplace.py index 4ad676ae0b09f..ac99cecd4a3a6 100644 --- a/test/legacy_test/test_inplace.py +++ b/test/legacy_test/test_inplace.py @@ -1734,6 +1734,7 @@ def test_broadcast_error(self): with self.assertRaises(ValueError): self.inplace_api_processing(broadcast_input) + class TestDygraphInplaceIndexFill(TestDygraphInplace): def init_data(self): self.input_var_numpy = np.random.random((20, 40)) From 75878914b21e762176a5dcfc1149d487cc6e3d9b Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Wed, 27 Dec 2023 17:51:12 +0800 Subject: [PATCH 07/27] update --- paddle/phi/kernels/bitwise_kernel.h | 1 - paddle/phi/kernels/cpu/bitwise_kernel.cc | 1 - paddle/phi/kernels/funcs/bitwise_functors.h | 1 - 3 files changed, 3 deletions(-) diff --git a/paddle/phi/kernels/bitwise_kernel.h b/paddle/phi/kernels/bitwise_kernel.h index d0b26da015a22..4122960928eba 100644 --- a/paddle/phi/kernels/bitwise_kernel.h +++ b/paddle/phi/kernels/bitwise_kernel.h @@ -65,5 +65,4 @@ void BitwiseRightShiftLogicKernel(const Context& dev_ctx, const DenseTensor& y, DenseTensor* out); - } // namespace phi diff --git a/paddle/phi/kernels/cpu/bitwise_kernel.cc b/paddle/phi/kernels/cpu/bitwise_kernel.cc index 5ba49fde84ac6..8b19a8fdd863d 100644 --- a/paddle/phi/kernels/cpu/bitwise_kernel.cc +++ b/paddle/phi/kernels/cpu/bitwise_kernel.cc @@ -123,7 +123,6 @@ PD_REGISTER_KERNEL(bitwise_not, int, int64_t) {} - PD_REGISTER_KERNEL(bitwise_left_shift_arithmetic, CPU, ALL_LAYOUT, diff --git a/paddle/phi/kernels/funcs/bitwise_functors.h b/paddle/phi/kernels/funcs/bitwise_functors.h index 912a0a7be1d98..7bfd085f9bf7e 100644 --- a/paddle/phi/kernels/funcs/bitwise_functors.h +++ b/paddle/phi/kernels/funcs/bitwise_functors.h @@ -204,6 +204,5 @@ struct InverseBitwiseRightShiftLogicFunctor { } }; - } // namespace funcs } // namespace phi From aac04c8c1851fbfd28974e34ac63293161ab3dba Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Fri, 29 Dec 2023 00:40:25 +0800 Subject: [PATCH 08/27] test split op --- .../fluid/pir/dialect/op_generator/op_gen.py | 21 +++++++++++++++---- .../pir/dialect/operator/ir/op_dialect.cc | 7 ++++++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/paddle/fluid/pir/dialect/op_generator/op_gen.py b/paddle/fluid/pir/dialect/op_generator/op_gen.py index 7dd754e868f86..f7eafd42e4c19 100644 --- a/paddle/fluid/pir/dialect/op_generator/op_gen.py +++ b/paddle/fluid/pir/dialect/op_generator/op_gen.py @@ -168,9 +168,12 @@ class {TEST_API} {op_name} : public pir::Op<{op_name}{interfaces}{traits}> {{ {define_type_id} """ -CC_OP_INFO_FILE_TEMPLATE = """#ifdef GET_OP_LIST -#undef GET_OP_LIST -{op_declare} +CC_OP_INFO_FILE_TEMPLATE = """#ifdef GET_OP_LIST1 +#undef GET_OP_LIST1 +{op_declare_first_part} +#elif defined(GET_OP_LIST2) +#undef GET_OP_LIST2 +{op_declare_second_part} #else // This file is generated by "paddle/fluid/pir/dialect/op_generator/op_gen.py" #include "{h_file}" @@ -1867,8 +1870,18 @@ def OpGenerator( else: op_to_multi_kernels_map_str = "" + n = len(op_list_strs) // 2 + first_part_op_info = op_list_strs[:n] + second_part_op_info = op_list_strs[n:] + + # op_info_str = CC_OP_INFO_FILE_TEMPLATE.format( + # op_declare=",".join(op_list_strs).replace("\n", ""), + # op_to_multi_kernels_map=op_to_multi_kernels_map_str, + # h_file=op_def_h_file[:-4], + # ) op_info_str = CC_OP_INFO_FILE_TEMPLATE.format( - op_declare=",".join(op_list_strs).replace("\n", ""), + op_declare_first_part=",".join(first_part_op_info).replace("\n", ""), + op_declare_second_part=",".join(second_part_op_info).replace("\n", ""), op_to_multi_kernels_map=op_to_multi_kernels_map_str, h_file=op_def_h_file[:-4], ) diff --git a/paddle/fluid/pir/dialect/operator/ir/op_dialect.cc b/paddle/fluid/pir/dialect/operator/ir/op_dialect.cc index 8cd6375dbe7b6..5b05146940fd3 100644 --- a/paddle/fluid/pir/dialect/operator/ir/op_dialect.cc +++ b/paddle/fluid/pir/dialect/operator/ir/op_dialect.cc @@ -55,7 +55,12 @@ void OperatorDialect::initialize() { // NOTE(Ruting)GET_MANUAL_OP_LIST is define in manual_op.h" // use RegisterOps when list has more than two ops. RegisterOps< -#define GET_OP_LIST +#define GET_OP_LIST1 +#include "paddle/fluid/pir/dialect/operator/ir/pd_op_info.cc" // NOLINT + >(); + + RegisterOps< +#define GET_OP_LIST2 #include "paddle/fluid/pir/dialect/operator/ir/pd_op_info.cc" // NOLINT >(); From 99aef1395c147a8210ebc69c9007f8db65a42140 Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Fri, 29 Dec 2023 00:41:43 +0800 Subject: [PATCH 09/27] codestyle --- paddle/fluid/pir/dialect/op_generator/op_gen.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle/fluid/pir/dialect/op_generator/op_gen.py b/paddle/fluid/pir/dialect/op_generator/op_gen.py index f7eafd42e4c19..6903b55ea4f33 100644 --- a/paddle/fluid/pir/dialect/op_generator/op_gen.py +++ b/paddle/fluid/pir/dialect/op_generator/op_gen.py @@ -1873,7 +1873,7 @@ def OpGenerator( n = len(op_list_strs) // 2 first_part_op_info = op_list_strs[:n] second_part_op_info = op_list_strs[n:] - + # op_info_str = CC_OP_INFO_FILE_TEMPLATE.format( # op_declare=",".join(op_list_strs).replace("\n", ""), # op_to_multi_kernels_map=op_to_multi_kernels_map_str, From af86d962b7585a7ecec04d0167af9fbae9a2a599 Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Fri, 29 Dec 2023 15:29:31 +0800 Subject: [PATCH 10/27] add register --- paddle/fluid/pir/dialect/operator/ir/op_onednn_dialect.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/pir/dialect/operator/ir/op_onednn_dialect.cc b/paddle/fluid/pir/dialect/operator/ir/op_onednn_dialect.cc index 0d65389cc4922..33ba7fd263429 100644 --- a/paddle/fluid/pir/dialect/operator/ir/op_onednn_dialect.cc +++ b/paddle/fluid/pir/dialect/operator/ir/op_onednn_dialect.cc @@ -45,7 +45,11 @@ void OneDNNOperatorDialect::initialize() { // NOTE(Ruting)GET_MANUAL_OP_LIST is define in manual_op.h" // use RegisterOps when list has more than two ops. RegisterOps< -#define GET_OP_LIST +#define GET_OP_LIST1 +#include "paddle/fluid/pir/dialect/operator/ir/pd_onednn_op_info.cc" // NOLINT + >(); + RegisterOps< +#define GET_OP_LIST2 #include "paddle/fluid/pir/dialect/operator/ir/pd_onednn_op_info.cc" // NOLINT >(); } From db05b52adaeb10d015c0f32b7ca37849b248a7a8 Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Sat, 30 Dec 2023 01:45:58 +0800 Subject: [PATCH 11/27] split on win --- .../hlir/dialect/operator/ir/op_dialect.cc | 11 ++++ .../fluid/pir/dialect/op_generator/op_gen.py | 51 +++++++++++++------ .../pir/dialect/operator/ir/op_dialect.cc | 8 ++- .../dialect/operator/ir/op_onednn_dialect.cc | 7 +++ 4 files changed, 61 insertions(+), 16 deletions(-) diff --git a/paddle/cinn/hlir/dialect/operator/ir/op_dialect.cc b/paddle/cinn/hlir/dialect/operator/ir/op_dialect.cc index d7e55095fcc2e..9dd56503e402b 100644 --- a/paddle/cinn/hlir/dialect/operator/ir/op_dialect.cc +++ b/paddle/cinn/hlir/dialect/operator/ir/op_dialect.cc @@ -33,10 +33,21 @@ void OperatorDialect::initialize() { // NOTE(chenxi67): GET_OP_LIST is defined in cinn_op.h which is // generated by op_gen.py, see details in // paddle/cinn/hlir/dialect/CMakeLists.txt. +#ifdef WIN32 + RegisterOps< +#define GET_OP_LIST1 +#include "paddle/cinn/hlir/dialect/operator/ir/cinn_op_info.cc" // NOLINT + >(); + RegisterOps< +#define GET_OP_LIST2 +#include "paddle/cinn/hlir/dialect/operator/ir/cinn_op_info.cc" // NOLINT + >(); +#else RegisterOps< #define GET_OP_LIST #include "paddle/cinn/hlir/dialect/operator/ir/cinn_op_info.cc" // NOLINT >(); +#endif RegisterOp(); RegisterOp(); RegisterOp(); diff --git a/paddle/fluid/pir/dialect/op_generator/op_gen.py b/paddle/fluid/pir/dialect/op_generator/op_gen.py index b53a8d6350e33..7468957eb0ea6 100644 --- a/paddle/fluid/pir/dialect/op_generator/op_gen.py +++ b/paddle/fluid/pir/dialect/op_generator/op_gen.py @@ -168,12 +168,20 @@ class {TEST_API} {op_name} : public pir::Op<{op_name}{interfaces}{traits}> {{ {define_type_id} """ -CC_OP_INFO_FILE_TEMPLATE = """#ifdef GET_OP_LIST1 +CC_OP_INFO_FILE_TEMPLATE_PART1 = """#ifdef GET_OP_LIST +#undef GET_OP_LIST +{op_declare} +""" + +CC_OP_INFO_FILE_TEMPLATE_WIN_PART1 = """#ifdef GET_OP_LIST1 #undef GET_OP_LIST1 {op_declare_first_part} #elif defined(GET_OP_LIST2) #undef GET_OP_LIST2 {op_declare_second_part} +""" + +CC_OP_INFO_FILE_TEMPLATE_PART2 = """ #else // This file is generated by "paddle/fluid/pir/dialect/op_generator/op_gen.py" #include "{h_file}" @@ -1997,20 +2005,33 @@ def OpGenerator( op_to_multi_kernels_map_str = "" if op_info_file is not None: - n = len(op_list_strs) // 2 - first_part_op_info = op_list_strs[:n] - second_part_op_info = op_list_strs[n:] - - op_info_str = CC_OP_INFO_FILE_TEMPLATE.format( - op_declare_first_part=",".join(first_part_op_info).replace( - "\n", "" - ), - op_declare_second_part=",".join(second_part_op_info).replace( - "\n", "" - ), - op_to_multi_kernels_map=op_to_multi_kernels_map_str, - h_file=op_def_h_file[:-4], - ) + if sys.platform == "win32": + n = len(op_list_strs) // 2 + first_part_op_info = op_list_strs[:n] + second_part_op_info = op_list_strs[n:] + CC_OP_INFO_FILE_TEMPLATE = ( + CC_OP_INFO_FILE_TEMPLATE_WIN_PART1 + + CC_OP_INFO_FILE_TEMPLATE_PART2 + ) + op_info_str = CC_OP_INFO_FILE_TEMPLATE.format( + op_declare_first_part=",".join(first_part_op_info).replace( + "\n", "" + ), + op_declare_second_part=",".join(second_part_op_info).replace( + "\n", "" + ), + op_to_multi_kernels_map=op_to_multi_kernels_map_str, + h_file=op_def_h_file[:-4], + ) + else: + CC_OP_INFO_FILE_TEMPLATE = ( + CC_OP_INFO_FILE_TEMPLATE_PART1 + CC_OP_INFO_FILE_TEMPLATE_PART2 + ) + op_info_str = CC_OP_INFO_FILE_TEMPLATE.format( + op_declare=",".join(op_list_strs).replace("\n", ""), + op_to_multi_kernels_map=op_to_multi_kernels_map_str, + h_file=op_def_h_file[:-4], + ) with open(op_info_file, 'w') as f: f.write(op_info_str) diff --git a/paddle/fluid/pir/dialect/operator/ir/op_dialect.cc b/paddle/fluid/pir/dialect/operator/ir/op_dialect.cc index 3e9a333fea45b..2280d620db23f 100644 --- a/paddle/fluid/pir/dialect/operator/ir/op_dialect.cc +++ b/paddle/fluid/pir/dialect/operator/ir/op_dialect.cc @@ -84,6 +84,7 @@ void OperatorDialect::initialize() { // paddle/fluid/pir/dialect/CMakeLists.txt. // NOTE(Ruting)GET_MANUAL_OP_LIST is define in manual_op.h" // use RegisterOps when list has more than two ops. +#ifdef WIN32 RegisterOps< #define GET_OP_LIST1 #include "paddle/fluid/pir/dialect/operator/ir/pd_op_info.cc" // NOLINT @@ -93,7 +94,12 @@ void OperatorDialect::initialize() { #define GET_OP_LIST2 #include "paddle/fluid/pir/dialect/operator/ir/pd_op_info.cc" // NOLINT >(); - +#else + RegisterOps< +#define GET_OP_LIST +#include "paddle/fluid/pir/dialect/operator/ir/pd_op_info.cc" // NOLINT + >(); +#endif RegisterOps< #define GET_OP_LIST #include "paddle/fluid/pir/dialect/operator/ir/control_flow_op.cc" // NOLINT diff --git a/paddle/fluid/pir/dialect/operator/ir/op_onednn_dialect.cc b/paddle/fluid/pir/dialect/operator/ir/op_onednn_dialect.cc index 33ba7fd263429..1d2b5ea6be844 100644 --- a/paddle/fluid/pir/dialect/operator/ir/op_onednn_dialect.cc +++ b/paddle/fluid/pir/dialect/operator/ir/op_onednn_dialect.cc @@ -44,6 +44,7 @@ void OneDNNOperatorDialect::initialize() { // paddle/fluid/pir/dialect/CMakeLists.txt. // NOTE(Ruting)GET_MANUAL_OP_LIST is define in manual_op.h" // use RegisterOps when list has more than two ops. +#ifdef WIN32 RegisterOps< #define GET_OP_LIST1 #include "paddle/fluid/pir/dialect/operator/ir/pd_onednn_op_info.cc" // NOLINT @@ -52,6 +53,12 @@ void OneDNNOperatorDialect::initialize() { #define GET_OP_LIST2 #include "paddle/fluid/pir/dialect/operator/ir/pd_onednn_op_info.cc" // NOLINT >(); +#else + RegisterOps< +#define GET_OP_LIST +#include "paddle/fluid/pir/dialect/operator/ir/pd_onednn_op_info.cc" // NOLINT + >(); +#endif } void OneDNNOperatorDialect::PrintType(pir::Type type, std::ostream &os) const { From 126e5a59b3e2739315e485aab8756544750a32a1 Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Sat, 30 Dec 2023 14:29:51 +0800 Subject: [PATCH 12/27] test bigger shape --- test/legacy_test/test_bitwise_shift_op.py | 176 ++++++++++++++-------- 1 file changed, 112 insertions(+), 64 deletions(-) diff --git a/test/legacy_test/test_bitwise_shift_op.py b/test/legacy_test/test_bitwise_shift_op.py index 9b957891de7b1..14ddbeb63ea1b 100644 --- a/test/legacy_test/test_bitwise_shift_op.py +++ b/test/legacy_test/test_bitwise_shift_op.py @@ -71,8 +71,8 @@ def setUp(self): ) def init_input(self): - self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') - self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') + self.x = np.random.randint(0, 256, [200, 300]).astype('uint8') + self.y = np.random.randint(0, 256, [200, 300]).astype('uint8') def test_static_api_arithmetic(self): paddle.enable_static() @@ -123,92 +123,116 @@ def test_dygraph_api_logical(self): class TestBitwiseLeftShiftAPI_UINT8(TestBitwiseLeftShiftAPI): def init_input(self): - self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') - self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') + self.x = np.random.randint(0, 256, [200, 300]).astype('uint8') + self.y = np.random.randint(0, 256, [200, 300]).astype('uint8') class TestBitwiseLeftShiftAPI_UINT8_broadcast1(TestBitwiseLeftShiftAPI): def init_input(self): - self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') - self.y = np.random.randint(0, 256, [3]).astype('uint8') + self.x = np.random.randint(0, 256, [200, 300]).astype('uint8') + self.y = np.random.randint(0, 256, [300]).astype('uint8') class TestBitwiseLeftShiftAPI_UINT8_broadcast2(TestBitwiseLeftShiftAPI): def init_input(self): - self.x = np.random.randint(0, 256, [3]).astype('uint8') - self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') + self.x = np.random.randint(0, 256, [300]).astype('uint8') + self.y = np.random.randint(0, 256, [200, 300]).astype('uint8') class TestBitwiseLeftShiftAPI_INT8(TestBitwiseLeftShiftAPI): def init_input(self): - self.x = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') - self.y = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') + self.x = np.random.randint(-(2**7), 2**7, [200, 300]).astype('int8') + self.y = np.random.randint(-(2**7), 2**7, [200, 300]).astype('int8') class TestBitwiseLeftShiftAPI_INT8_broadcast1(TestBitwiseLeftShiftAPI): def init_input(self): - self.x = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') - self.y = np.random.randint(-(2**7), 2**7, [3]).astype('int8') + self.x = np.random.randint(-(2**7), 2**7, [200, 300]).astype('int8') + self.y = np.random.randint(-(2**7), 2**7, [300]).astype('int8') class TestBitwiseLeftShiftAPI_INT8_broadcast2(TestBitwiseLeftShiftAPI): def init_input(self): - self.x = np.random.randint(-(2**7), 2**7, [3]).astype('int8') - self.y = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') + self.x = np.random.randint(-(2**7), 2**7, [300]).astype('int8') + self.y = np.random.randint(-(2**7), 2**7, [200, 300]).astype('int8') class TestBitwiseLeftShiftAPI_INT16(TestBitwiseLeftShiftAPI): def init_input(self): - self.x = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') - self.y = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') + self.x = np.random.randint(-(2**15), 2**15, [200, 300]).astype( + 'int16' + ) + self.y = np.random.randint(-(2**15), 2**15, [200, 300]).astype( + 'int16' + ) class TestBitwiseLeftShiftAPI_INT16_broadcast1(TestBitwiseLeftShiftAPI): def init_input(self): - self.x = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') - self.y = np.random.randint(-(2**15), 2**15, [3]).astype('int16') + self.x = np.random.randint(-(2**15), 2**15, [200, 300]).astype( + 'int16' + ) + self.y = np.random.randint(-(2**15), 2**15, [300]).astype('int16') class TestBitwiseLeftShiftAPI_INT16_broadcast2(TestBitwiseLeftShiftAPI): def init_input(self): - self.x = np.random.randint(-(2**15), 2**15, [3]).astype('int16') - self.y = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') + self.x = np.random.randint(-(2**15), 2**15, [300]).astype('int16') + self.y = np.random.randint(-(2**15), 2**15, [200, 300]).astype( + 'int16' + ) class TestBitwiseLeftShiftAPI_INT32(TestBitwiseLeftShiftAPI): def init_input(self): - self.x = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') - self.y = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') + self.x = np.random.randint(-(2**31), 2**31, [200, 300]).astype( + 'int32' + ) + self.y = np.random.randint(-(2**31), 2**31, [200, 300]).astype( + 'int32' + ) class TestBitwiseLeftShiftAPI_INT32_broadcast1(TestBitwiseLeftShiftAPI): def init_input(self): - self.x = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') - self.y = np.random.randint(-(2**31), 2**31, [3]).astype('int32') + self.x = np.random.randint(-(2**31), 2**31, [200, 300]).astype( + 'int32' + ) + self.y = np.random.randint(-(2**31), 2**31, [300]).astype('int32') class TestBitwiseLeftShiftAPI_INT32_broadcast2(TestBitwiseLeftShiftAPI): def init_input(self): - self.x = np.random.randint(-(2**31), 2**31, [3]).astype('int32') - self.y = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') + self.x = np.random.randint(-(2**31), 2**31, [300]).astype('int32') + self.y = np.random.randint(-(2**31), 2**31, [200, 300]).astype( + 'int32' + ) class TestBitwiseLeftShiftAPI_INT64(TestBitwiseLeftShiftAPI): def init_input(self): - self.x = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) - self.y = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) + self.x = np.random.randint( + -(2**63), 2**63, [200, 300], dtype=np.int64 + ) + self.y = np.random.randint( + -(2**63), 2**63, [200, 300], dtype=np.int64 + ) class TestBitwiseLeftShiftAPI_INT64_broadcast1(TestBitwiseLeftShiftAPI): def init_input(self): - self.x = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) - self.y = np.random.randint(-(2**63), 2**63, [3], dtype=np.int64) + self.x = np.random.randint( + -(2**63), 2**63, [200, 300], dtype=np.int64 + ) + self.y = np.random.randint(-(2**63), 2**63, [300], dtype=np.int64) class TestBitwiseLeftShiftAPI_INT64_broadcast2(TestBitwiseLeftShiftAPI): def init_input(self): - self.x = np.random.randint(-(2**63), 2**63, [3], dtype=np.int64) - self.y = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) + self.x = np.random.randint(-(2**63), 2**63, [300], dtype=np.int64) + self.y = np.random.randint( + -(2**63), 2**63, [200, 300], dtype=np.int64 + ) class TestBitwiseLeftShiftAPI_special_case1(TestBitwiseLeftShiftAPI): @@ -245,8 +269,8 @@ def setUp(self): ) def init_input(self): - self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') - self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') + self.x = np.random.randint(0, 256, [200, 300]).astype('uint8') + self.y = np.random.randint(0, 256, [200, 300]).astype('uint8') def test_static_api_arithmetic(self): paddle.enable_static() @@ -297,92 +321,116 @@ def test_dygraph_api_logical(self): class TestBitwiseRightShiftAPI_UINT8(TestBitwiseRightShiftAPI): def init_input(self): - self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') - self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') + self.x = np.random.randint(0, 256, [200, 300]).astype('uint8') + self.y = np.random.randint(0, 256, [200, 300]).astype('uint8') class TestBitwiseRightShiftAPI_UINT8_broadcast1(TestBitwiseRightShiftAPI): def init_input(self): - self.x = np.random.randint(0, 256, [2, 3]).astype('uint8') - self.y = np.random.randint(0, 256, [3]).astype('uint8') + self.x = np.random.randint(0, 256, [200, 300]).astype('uint8') + self.y = np.random.randint(0, 256, [300]).astype('uint8') class TestBitwiseRightShiftAPI_UINT8_broadcast2(TestBitwiseRightShiftAPI): def init_input(self): - self.x = np.random.randint(0, 256, [3]).astype('uint8') - self.y = np.random.randint(0, 256, [2, 3]).astype('uint8') + self.x = np.random.randint(0, 256, [300]).astype('uint8') + self.y = np.random.randint(0, 256, [200, 300]).astype('uint8') class TestBitwiseRightShiftAPI_INT8(TestBitwiseRightShiftAPI): def init_input(self): - self.x = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') - self.y = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') + self.x = np.random.randint(-(2**7), 2**7, [200, 300]).astype('int8') + self.y = np.random.randint(-(2**7), 2**7, [200, 300]).astype('int8') class TestBitwiseRightShiftAPI_INT8_broadcast1(TestBitwiseRightShiftAPI): def init_input(self): - self.x = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') - self.y = np.random.randint(-(2**7), 2**7, [3]).astype('int8') + self.x = np.random.randint(-(2**7), 2**7, [200, 300]).astype('int8') + self.y = np.random.randint(-(2**7), 2**7, [300]).astype('int8') class TestBitwiseRightShiftAPI_INT8_broadcast2(TestBitwiseRightShiftAPI): def init_input(self): - self.x = np.random.randint(-(2**7), 2**7, [3]).astype('int8') - self.y = np.random.randint(-(2**7), 2**7, [2, 3]).astype('int8') + self.x = np.random.randint(-(2**7), 2**7, [300]).astype('int8') + self.y = np.random.randint(-(2**7), 2**7, [200, 300]).astype('int8') class TestBitwiseRightShiftAPI_INT16(TestBitwiseRightShiftAPI): def init_input(self): - self.x = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') - self.y = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') + self.x = np.random.randint(-(2**15), 2**15, [200, 300]).astype( + 'int16' + ) + self.y = np.random.randint(-(2**15), 2**15, [200, 300]).astype( + 'int16' + ) class TestBitwiseRightShiftAPI_INT16_broadcast1(TestBitwiseRightShiftAPI): def init_input(self): - self.x = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') - self.y = np.random.randint(-(2**15), 2**15, [3]).astype('int16') + self.x = np.random.randint(-(2**15), 2**15, [200, 300]).astype( + 'int16' + ) + self.y = np.random.randint(-(2**15), 2**15, [300]).astype('int16') class TestBitwiseRightShiftAPI_INT16_broadcast2(TestBitwiseRightShiftAPI): def init_input(self): - self.x = np.random.randint(-(2**15), 2**15, [3]).astype('int16') - self.y = np.random.randint(-(2**15), 2**15, [2, 3]).astype('int16') + self.x = np.random.randint(-(2**15), 2**15, [300]).astype('int16') + self.y = np.random.randint(-(2**15), 2**15, [200, 300]).astype( + 'int16' + ) class TestBitwiseRightShiftAPI_INT32(TestBitwiseRightShiftAPI): def init_input(self): - self.x = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') - self.y = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') + self.x = np.random.randint(-(2**31), 2**31, [200, 300]).astype( + 'int32' + ) + self.y = np.random.randint(-(2**31), 2**31, [200, 300]).astype( + 'int32' + ) class TestBitwiseRightShiftAPI_INT32_broadcast1(TestBitwiseRightShiftAPI): def init_input(self): - self.x = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') - self.y = np.random.randint(-(2**31), 2**31, [3]).astype('int32') + self.x = np.random.randint(-(2**31), 2**31, [200, 300]).astype( + 'int32' + ) + self.y = np.random.randint(-(2**31), 2**31, [300]).astype('int32') class TestBitwiseRightShiftAPI_INT32_broadcast2(TestBitwiseRightShiftAPI): def init_input(self): - self.x = np.random.randint(-(2**31), 2**31, [3]).astype('int32') - self.y = np.random.randint(-(2**31), 2**31, [2, 3]).astype('int32') + self.x = np.random.randint(-(2**31), 2**31, [300]).astype('int32') + self.y = np.random.randint(-(2**31), 2**31, [200, 300]).astype( + 'int32' + ) class TestBitwiseRightShiftAPI_INT64(TestBitwiseRightShiftAPI): def init_input(self): - self.x = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) - self.y = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) + self.x = np.random.randint( + -(2**63), 2**63, [200, 300], dtype=np.int64 + ) + self.y = np.random.randint( + -(2**63), 2**63, [200, 300], dtype=np.int64 + ) class TestBitwiseRightShiftAPI_INT64_broadcast1(TestBitwiseRightShiftAPI): def init_input(self): - self.x = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) - self.y = np.random.randint(-(2**63), 2**63, [3], dtype=np.int64) + self.x = np.random.randint( + -(2**63), 2**63, [200, 300], dtype=np.int64 + ) + self.y = np.random.randint(-(2**63), 2**63, [300], dtype=np.int64) class TestBitwiseRightShiftAPI_INT64_broadcast2(TestBitwiseRightShiftAPI): def init_input(self): - self.x = np.random.randint(-(2**63), 2**63, [3], dtype=np.int64) - self.y = np.random.randint(-(2**63), 2**63, [2, 3], dtype=np.int64) + self.x = np.random.randint(-(2**63), 2**63, [300], dtype=np.int64) + self.y = np.random.randint( + -(2**63), 2**63, [200, 300], dtype=np.int64 + ) class TestBitwiseRightShiftAPI_special_case1(TestBitwiseRightShiftAPI): From f5f51b8ab8fdbcb56bfd4df650fb57c4d9d54e49 Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Tue, 2 Jan 2024 15:07:41 +0800 Subject: [PATCH 13/27] add note --- paddle/cinn/hlir/dialect/operator/ir/op_dialect.cc | 4 ++++ paddle/fluid/pir/dialect/op_generator/op_gen.py | 5 ++++- paddle/fluid/pir/dialect/operator/ir/op_dialect.cc | 4 ++++ paddle/fluid/pir/dialect/operator/ir/op_onednn_dialect.cc | 4 ++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/paddle/cinn/hlir/dialect/operator/ir/op_dialect.cc b/paddle/cinn/hlir/dialect/operator/ir/op_dialect.cc index 9dd56503e402b..d74ac18e454e9 100644 --- a/paddle/cinn/hlir/dialect/operator/ir/op_dialect.cc +++ b/paddle/cinn/hlir/dialect/operator/ir/op_dialect.cc @@ -33,6 +33,10 @@ void OperatorDialect::initialize() { // NOTE(chenxi67): GET_OP_LIST is defined in cinn_op.h which is // generated by op_gen.py, see details in // paddle/cinn/hlir/dialect/CMakeLists.txt. + + // NOTE(cocoshe): GET_OP_LIST is too long when compiling on MSVC, which + // causes "fatal error C1202: recursive type or function dependency context + // too complex" error, so here split into two GET_OP_LIST on WIN32. #ifdef WIN32 RegisterOps< #define GET_OP_LIST1 diff --git a/paddle/fluid/pir/dialect/op_generator/op_gen.py b/paddle/fluid/pir/dialect/op_generator/op_gen.py index 7468957eb0ea6..93f4fb23a3eac 100644 --- a/paddle/fluid/pir/dialect/op_generator/op_gen.py +++ b/paddle/fluid/pir/dialect/op_generator/op_gen.py @@ -167,7 +167,10 @@ class {TEST_API} {op_name} : public pir::Op<{op_name}{interfaces}{traits}> {{ {define_type_id} """ - +# NOTE(cocoshe): Use CC_OP_INFO_FILE_TEMPLATE_WIN_PART1 to generate two GET_OP_LIST to avoid +# "fatal error C1202: recursive type or function dependency context too complex" error +# when compiling on MSVC because the GET_OP_LIST is too long. +# And use CC_OP_INFO_FILE_TEMPLATE_PART1 to generate just one GET_OP_LIST for other compiler. CC_OP_INFO_FILE_TEMPLATE_PART1 = """#ifdef GET_OP_LIST #undef GET_OP_LIST {op_declare} diff --git a/paddle/fluid/pir/dialect/operator/ir/op_dialect.cc b/paddle/fluid/pir/dialect/operator/ir/op_dialect.cc index 2280d620db23f..508bb6954497e 100644 --- a/paddle/fluid/pir/dialect/operator/ir/op_dialect.cc +++ b/paddle/fluid/pir/dialect/operator/ir/op_dialect.cc @@ -84,6 +84,10 @@ void OperatorDialect::initialize() { // paddle/fluid/pir/dialect/CMakeLists.txt. // NOTE(Ruting)GET_MANUAL_OP_LIST is define in manual_op.h" // use RegisterOps when list has more than two ops. + + // NOTE(cocoshe): GET_OP_LIST is too long when compiling on MSVC, which + // causes "fatal error C1202: recursive type or function dependency context + // too complex" error, so here split into two GET_OP_LIST on WIN32. #ifdef WIN32 RegisterOps< #define GET_OP_LIST1 diff --git a/paddle/fluid/pir/dialect/operator/ir/op_onednn_dialect.cc b/paddle/fluid/pir/dialect/operator/ir/op_onednn_dialect.cc index 1d2b5ea6be844..ca7493305271e 100644 --- a/paddle/fluid/pir/dialect/operator/ir/op_onednn_dialect.cc +++ b/paddle/fluid/pir/dialect/operator/ir/op_onednn_dialect.cc @@ -44,6 +44,10 @@ void OneDNNOperatorDialect::initialize() { // paddle/fluid/pir/dialect/CMakeLists.txt. // NOTE(Ruting)GET_MANUAL_OP_LIST is define in manual_op.h" // use RegisterOps when list has more than two ops. + + // NOTE(cocoshe): GET_OP_LIST is too long when compiling on MSVC, which + // causes "fatal error C1202: recursive type or function dependency context + // too complex" error, so here split into two GET_OP_LIST on WIN32. #ifdef WIN32 RegisterOps< #define GET_OP_LIST1 From d28481dd5ef35e2c240577191eae8b090c6e3dae Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Tue, 2 Jan 2024 16:02:32 +0800 Subject: [PATCH 14/27] fix --- paddle/phi/kernels/funcs/bitwise_functors.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/paddle/phi/kernels/funcs/bitwise_functors.h b/paddle/phi/kernels/funcs/bitwise_functors.h index 7bfd085f9bf7e..82b0a2f158c54 100644 --- a/paddle/phi/kernels/funcs/bitwise_functors.h +++ b/paddle/phi/kernels/funcs/bitwise_functors.h @@ -122,7 +122,8 @@ struct InverseBitwiseRightShiftArithmeticFunctor { template struct BitwiseRightShiftLogicFunctor { HOSTDEVICE T operator()(const T a, const T b) const { - if (b >= static_cast(sizeof(T) * 8)) return static_cast(0); + if (b >= static_cast(sizeof(T) * 8) || b < static_cast(0)) + return static_cast(0); return a >> b; } }; @@ -130,7 +131,8 @@ struct BitwiseRightShiftLogicFunctor { template struct InverseBitwiseRightShiftLogicFunctor { inline HOSTDEVICE T operator()(const T a, const T b) const { - if (a >= static_cast(sizeof(T) * 8)) return static_cast(0); + if (a >= static_cast(sizeof(T) * 8), a < static_cast(0)) + return static_cast(0); return b >> a; } }; From 899d1ff616b9c61354be84d54d4a0c87a139a1e4 Mon Sep 17 00:00:00 2001 From: coco <69197635+cocoshe@users.noreply.github.com> Date: Tue, 2 Jan 2024 18:34:09 +0800 Subject: [PATCH 15/27] Update bitwise_functors.h --- paddle/phi/kernels/funcs/bitwise_functors.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle/phi/kernels/funcs/bitwise_functors.h b/paddle/phi/kernels/funcs/bitwise_functors.h index 82b0a2f158c54..35811f1fca172 100644 --- a/paddle/phi/kernels/funcs/bitwise_functors.h +++ b/paddle/phi/kernels/funcs/bitwise_functors.h @@ -131,7 +131,7 @@ struct BitwiseRightShiftLogicFunctor { template struct InverseBitwiseRightShiftLogicFunctor { inline HOSTDEVICE T operator()(const T a, const T b) const { - if (a >= static_cast(sizeof(T) * 8), a < static_cast(0)) + if (a >= static_cast(sizeof(T) * 8) || a < static_cast(0)) return static_cast(0); return b >> a; } From 04c80b2684cf78dfca327c500e975526c36e03f1 Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Wed, 3 Jan 2024 14:06:50 +0800 Subject: [PATCH 16/27] fix --- .../pir/dialect/operator/ir/op_dialect.cc | 65 +++++-------------- 1 file changed, 16 insertions(+), 49 deletions(-) diff --git a/paddle/fluid/pir/dialect/operator/ir/op_dialect.cc b/paddle/fluid/pir/dialect/operator/ir/op_dialect.cc index 88d5103b2cf27..80ba4b50796fd 100644 --- a/paddle/fluid/pir/dialect/operator/ir/op_dialect.cc +++ b/paddle/fluid/pir/dialect/operator/ir/op_dialect.cc @@ -80,54 +80,6 @@ OperatorDialect::OperatorDialect(pir::IrContext* ctx) CombineOpInferSymbolicShapeInterfaceModel>())); } -void OperatorDialect::initialize() { - RegisterTypes(); - - RegisterAttributes(); - - // NOTE(zhangbo9674): GET_OP_LIST is defined in pd_op.h which is - // generated by op_gen.py, see details in - // paddle/fluid/pir/dialect/CMakeLists.txt. - // NOTE(Ruting)GET_MANUAL_OP_LIST is define in manual_op.h" - // use RegisterOps when list has more than two ops. - - // NOTE(cocoshe): GET_OP_LIST is too long when compiling on MSVC, which - // causes "fatal error C1202: recursive type or function dependency context - // too complex" error, so here split into two GET_OP_LIST on WIN32. -#ifdef WIN32 - RegisterOps< -#define GET_OP_LIST1 -#include "paddle/fluid/pir/dialect/operator/ir/pd_op_info.cc" // NOLINT - >(); - - RegisterOps< -#define GET_OP_LIST2 -#include "paddle/fluid/pir/dialect/operator/ir/pd_op_info.cc" // NOLINT - >(); -#else - RegisterOps< -#define GET_OP_LIST -#include "paddle/fluid/pir/dialect/operator/ir/pd_op_info.cc" // NOLINT - >(); -#endif - RegisterOps< -#define GET_OP_LIST -#include "paddle/fluid/pir/dialect/operator/ir/control_flow_op.cc" // NOLINT - >(); - - RegisterOps< -#define GET_OP_LIST -#include "paddle/fluid/pir/dialect/operator/ir/manual_op.cc" // NOLINT - >(); - - RegisterInterfaces(); -} - void PrintTypeImpl(pir::Type type, std::ostream& os) { os << type.dialect().name(); os << '.'; @@ -204,11 +156,26 @@ void OperatorDialect::initialize() { // paddle/fluid/pir/dialect/CMakeLists.txt. // NOTE(Ruting)GET_MANUAL_OP_LIST is define in manual_op.h" // use RegisterOps when list has more than two ops. + + // NOTE(cocoshe): GET_OP_LIST is too long when compiling on MSVC, which + // causes "fatal error C1202: recursive type or function dependency context + // too complex" error, so here split into two GET_OP_LIST on WIN32. +#ifdef WIN32 RegisterOps< -#define GET_OP_LIST +#define GET_OP_LIST1 #include "paddle/fluid/pir/dialect/operator/ir/pd_op_info.cc" // NOLINT >(); + RegisterOps< +#define GET_OP_LIST2 +#include "paddle/fluid/pir/dialect/operator/ir/pd_op_info.cc" // NOLINT + >(); +#else + RegisterOps< +#define GET_OP_LIST +#include "paddle/fluid/pir/dialect/operator/ir/pd_op_info.cc" // NOLINT + >(); +#endif RegisterOps< #define GET_OP_LIST #include "paddle/fluid/pir/dialect/operator/ir/control_flow_op.cc" // NOLINT From 60f0e9253248a17dd1441e1ce29f3bb61ebfbd5f Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Fri, 5 Jan 2024 18:53:00 +0800 Subject: [PATCH 17/27] fix doc --- python/paddle/tensor/math.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 5c40204ae006c..8ff4f5f677cd0 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -7131,20 +7131,29 @@ def _bitwise_op(op_name, x, y, out=None, name=None): def bitwise_left_shift(x, y, is_arithmetic=True, out=None, name=None): r""" Apply ``bitwise_left_shift`` on Tensor ``X`` and ``Y`` . + .. math:: + Out = X \ll Y + .. note:: + ``paddle.bitwise_left_shift`` supports broadcasting. If you want know more about broadcasting, please refer to please refer to `Introduction to Tensor`_ . + .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor. + Args: x (Tensor): Input Tensor of ``bitwise_left_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. y (Tensor): Input Tensor of ``bitwise_left_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. name (str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`. + Returns: Tensor: Result of ``bitwise_left_shift`` . It is a N-D Tensor with the same data type of input Tensor. + Examples: .. code-block:: python + >>> import paddle >>> x= paddle.to_tensor([[1,2,4,8],[16,17,32,65]]) >>> y = paddle.to_tensor([[1,2,3,4,], [2,3,2,1]]) @@ -7199,20 +7208,29 @@ def bitwise_left_shift_(x, y, is_arithmetic=True, out=None, name=None): def bitwise_right_shift(x, y, is_arithmetic=True, out=None, name=None): r""" Apply ``bitwise_right_shift`` on Tensor ``X`` and ``Y`` . + .. math:: + Out = X \gg Y + .. note:: + ``paddle.bitwise_right_shift`` supports broadcasting. If you want know more about broadcasting, please refer to please refer to `Introduction to Tensor`_ . + .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor. + Args: x (Tensor): Input Tensor of ``bitwise_right_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. y (Tensor): Input Tensor of ``bitwise_right_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. name (str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`. + Returns: Tensor: Result of ``bitwise_right_shift`` . It is a N-D Tensor with the same data type of input Tensor. + Examples: .. code-block:: python + >>> import paddle >>> x= paddle.to_tensor([[10,20,40,80],[16,17,32,65]]) >>> y = paddle.to_tensor([[1,2,3,4,], [2,3,2,1]]) From 3b1e7e40bbd62962ce16055068bf775a0578e5cf Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Fri, 5 Jan 2024 21:39:55 +0800 Subject: [PATCH 18/27] fix doc --- python/paddle/tensor/math.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 8ff4f5f677cd0..af1fb15c9f3a8 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -7145,6 +7145,8 @@ def bitwise_left_shift(x, y, is_arithmetic=True, out=None, name=None): Args: x (Tensor): Input Tensor of ``bitwise_left_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. y (Tensor): Input Tensor of ``bitwise_left_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. + is_arithmetic (bool, optional): A boolean indicating whether to choose arithmetic shift, if False, means logic shift. Default True. + out (Tensor, optional): Result of ``bitwise_left_shift`` . It is a N-D Tensor with the same data type of input Tensor. Default: None. name (str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`. @@ -7155,7 +7157,7 @@ def bitwise_left_shift(x, y, is_arithmetic=True, out=None, name=None): .. code-block:: python >>> import paddle - >>> x= paddle.to_tensor([[1,2,4,8],[16,17,32,65]]) + >>> x = paddle.to_tensor([[1,2,4,8],[16,17,32,65]]) >>> y = paddle.to_tensor([[1,2,3,4,], [2,3,2,1]]) >>> paddle.bitwise_left_shift(x, y) Tensor(shape=[2, 4], dtype=int64, place=Place(gpu:0), stop_gradient=True, @@ -7222,6 +7224,8 @@ def bitwise_right_shift(x, y, is_arithmetic=True, out=None, name=None): Args: x (Tensor): Input Tensor of ``bitwise_right_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. y (Tensor): Input Tensor of ``bitwise_right_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. + is_arithmetic (bool, optional): A boolean indicating whether to choose arithmetic shift, if False, means logic shift. Default True. + out (Tensor, optional): Result of ``bitwise_right_shift`` . It is a N-D Tensor with the same data type of input Tensor. Default: None. name (str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`. @@ -7232,7 +7236,7 @@ def bitwise_right_shift(x, y, is_arithmetic=True, out=None, name=None): .. code-block:: python >>> import paddle - >>> x= paddle.to_tensor([[10,20,40,80],[16,17,32,65]]) + >>> x = paddle.to_tensor([[10,20,40,80],[16,17,32,65]]) >>> y = paddle.to_tensor([[1,2,3,4,], [2,3,2,1]]) >>> paddle.bitwise_right_shift(x, y) Tensor(shape=[2, 4], dtype=int64, place=Place(gpu:0), stop_gradient=True, From d21eea04635e45928be27b5f16c4432457d92b25 Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Mon, 8 Jan 2024 18:06:04 +0800 Subject: [PATCH 19/27] refactor --- paddle/phi/api/yaml/ops.yaml | 36 +++-------- paddle/phi/infermeta/binary.cc | 7 +++ paddle/phi/infermeta/binary.h | 5 ++ paddle/phi/kernels/bitwise_kernel.h | 34 ++++------ paddle/phi/kernels/cpu/bitwise_kernel.cc | 62 +++++++++--------- paddle/phi/kernels/kps/bitwise_kernel.cu | 55 ++++++++-------- python/paddle/tensor/math.py | 80 +++++++++--------------- 7 files changed, 115 insertions(+), 164 deletions(-) diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index ae7dc41c49d45..ac3ec47f8297e 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -343,23 +343,13 @@ backend : x inplace: (x -> out) -- op : bitwise_left_shift_arithmetic - args : (Tensor x, Tensor y) - output : Tensor(out) - infer_meta : - func : ElementwiseInferMeta - kernel : - func : bitwise_left_shift_arithmetic - backend : x - inplace: (x -> out) - -- op : bitwise_left_shift_logic - args : (Tensor x, Tensor y) +- op : bitwise_left_shift + args : (Tensor x, Tensor y, bool is_arithmetic = true) output : Tensor(out) infer_meta : - func : ElementwiseInferMeta + func : BitwiseShiftInferMeta kernel : - func : bitwise_left_shift_logic + func : bitwise_left_shift backend : x inplace: (x -> out) @@ -384,23 +374,13 @@ backend : x inplace: (x -> out) -- op : bitwise_right_shift_arithmetic - args : (Tensor x, Tensor y) - output : Tensor(out) - infer_meta : - func : ElementwiseInferMeta - kernel : - func : bitwise_right_shift_arithmetic - backend : x - inplace: (x -> out) - -- op : bitwise_right_shift_logic - args : (Tensor x, Tensor y) +- op : bitwise_right_shift + args : (Tensor x, Tensor y, bool is_arithmetic = true) output : Tensor(out) infer_meta : - func : ElementwiseInferMeta + func : BitwiseShiftInferMeta kernel : - func : bitwise_right_shift_logic + func : bitwise_right_shift backend : x inplace: (x -> out) diff --git a/paddle/phi/infermeta/binary.cc b/paddle/phi/infermeta/binary.cc index b771fba031317..49aa257286754 100644 --- a/paddle/phi/infermeta/binary.cc +++ b/paddle/phi/infermeta/binary.cc @@ -1269,6 +1269,13 @@ void ElementwiseInferMeta(const MetaTensor& x, return ElementwiseRawInferMeta(x, y, -1, out); } +void BitwiseShiftInferMeta(const MetaTensor& x, + const MetaTensor& y, + bool is_arithmetic, + MetaTensor* out) { + return ElementwiseRawInferMeta(x, y, -1, out); +} + void ElementwiseRawInferMeta(const MetaTensor& x, const MetaTensor& y, int axis, diff --git a/paddle/phi/infermeta/binary.h b/paddle/phi/infermeta/binary.h index 82f5fc64d57a5..ba93038e3702d 100644 --- a/paddle/phi/infermeta/binary.h +++ b/paddle/phi/infermeta/binary.h @@ -231,6 +231,11 @@ void ElementwiseRawInferMeta(const MetaTensor& x_meta, MetaTensor* out, MetaConfig config = MetaConfig()); +void BitwiseShiftInferMeta(const MetaTensor& x, + const MetaTensor& y, + bool is_arithmetic, + MetaTensor* out); + void EmbeddingInferMeta(const MetaTensor& x, const MetaTensor& weight, int64_t padding_idx, diff --git a/paddle/phi/kernels/bitwise_kernel.h b/paddle/phi/kernels/bitwise_kernel.h index 4122960928eba..92fc41531da97 100644 --- a/paddle/phi/kernels/bitwise_kernel.h +++ b/paddle/phi/kernels/bitwise_kernel.h @@ -42,27 +42,17 @@ void BitwiseNotKernel(const Context& dev_ctx, DenseTensor* out); template -void BitwiseLeftShiftArithmeticKernel(const Context& dev_ctx, - const DenseTensor& x, - const DenseTensor& y, - DenseTensor* out); - -template -void BitwiseLeftShiftLogicKernel(const Context& dev_ctx, - const DenseTensor& x, - const DenseTensor& y, - DenseTensor* out); - -template -void BitwiseRightShiftArithmeticKernel(const Context& dev_ctx, - const DenseTensor& x, - const DenseTensor& y, - DenseTensor* out); - -template -void BitwiseRightShiftLogicKernel(const Context& dev_ctx, - const DenseTensor& x, - const DenseTensor& y, - DenseTensor* out); +void BitwiseLeftShiftKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + bool is_arithmetic, + DenseTensor* out); + +template +void BitwiseRightShiftKernel(const Context& dev_ctx, + const DenseTensor& x, + const DenseTensor& y, + bool is_arithmetic, + DenseTensor* out); } // namespace phi diff --git a/paddle/phi/kernels/cpu/bitwise_kernel.cc b/paddle/phi/kernels/cpu/bitwise_kernel.cc index 8b19a8fdd863d..4de0d575e96e4 100644 --- a/paddle/phi/kernels/cpu/bitwise_kernel.cc +++ b/paddle/phi/kernels/cpu/bitwise_kernel.cc @@ -45,24 +45,38 @@ DEFINE_BITWISE_KERNEL(Xor) void Bitwise##op_type##Kernel(const Context& dev_ctx, \ const DenseTensor& x, \ const DenseTensor& y, \ + bool is_arithmetic, \ DenseTensor* out) { \ - funcs::Bitwise##op_type##Functor func; \ - funcs::InverseBitwise##op_type##Functor inv_func; \ auto x_dims = x.dims(); \ auto y_dims = y.dims(); \ if (x_dims.size() >= y_dims.size()) { \ - funcs::ElementwiseCompute, T>( \ - dev_ctx, x, y, func, out); \ + if (is_arithmetic) { \ + funcs::Bitwise##op_type##ArithmeticFunctor func; \ + funcs::ElementwiseCompute< \ + funcs::Bitwise##op_type##ArithmeticFunctor, \ + T>(dev_ctx, x, y, func, out); \ + } else { \ + funcs::Bitwise##op_type##LogicFunctor func; \ + funcs::ElementwiseCompute, \ + T>(dev_ctx, x, y, func, out); \ + } \ } else { \ - funcs::ElementwiseCompute, \ - T>(dev_ctx, x, y, inv_func, out); \ + if (is_arithmetic) { \ + funcs::InverseBitwise##op_type##ArithmeticFunctor inv_func; \ + funcs::ElementwiseCompute< \ + funcs::InverseBitwise##op_type##ArithmeticFunctor, \ + T>(dev_ctx, x, y, inv_func, out); \ + } else { \ + funcs::InverseBitwise##op_type##LogicFunctor inv_func; \ + funcs::ElementwiseCompute< \ + funcs::InverseBitwise##op_type##LogicFunctor, \ + T>(dev_ctx, x, y, inv_func, out); \ + } \ } \ } -DEFINE_BITWISE_KERNEL_WITH_INVERSE(LeftShiftArithmetic) -DEFINE_BITWISE_KERNEL_WITH_INVERSE(LeftShiftLogic) -DEFINE_BITWISE_KERNEL_WITH_INVERSE(RightShiftArithmetic) -DEFINE_BITWISE_KERNEL_WITH_INVERSE(RightShiftLogic) +DEFINE_BITWISE_KERNEL_WITH_INVERSE(LeftShift) +DEFINE_BITWISE_KERNEL_WITH_INVERSE(RightShift) #undef DEFINE_BITWISE_KERNEL_WITH_INVERSE template @@ -123,40 +137,20 @@ PD_REGISTER_KERNEL(bitwise_not, int, int64_t) {} -PD_REGISTER_KERNEL(bitwise_left_shift_arithmetic, +PD_REGISTER_KERNEL(bitwise_left_shift, CPU, ALL_LAYOUT, - phi::BitwiseLeftShiftArithmeticKernel, + phi::BitwiseLeftShiftKernel, uint8_t, int8_t, int16_t, int, int64_t) {} -PD_REGISTER_KERNEL(bitwise_left_shift_logic, +PD_REGISTER_KERNEL(bitwise_right_shift, CPU, ALL_LAYOUT, - phi::BitwiseLeftShiftLogicKernel, - uint8_t, - int8_t, - int16_t, - int, - int64_t) {} - -PD_REGISTER_KERNEL(bitwise_right_shift_arithmetic, - CPU, - ALL_LAYOUT, - phi::BitwiseRightShiftArithmeticKernel, - uint8_t, - int8_t, - int16_t, - int, - int64_t) {} - -PD_REGISTER_KERNEL(bitwise_right_shift_logic, - CPU, - ALL_LAYOUT, - phi::BitwiseRightShiftLogicKernel, + phi::BitwiseRightShiftKernel, uint8_t, int8_t, int16_t, diff --git a/paddle/phi/kernels/kps/bitwise_kernel.cu b/paddle/phi/kernels/kps/bitwise_kernel.cu index 7dd3225f20476..c4fa2b49f7330 100644 --- a/paddle/phi/kernels/kps/bitwise_kernel.cu +++ b/paddle/phi/kernels/kps/bitwise_kernel.cu @@ -41,12 +41,31 @@ namespace phi { DEFINE_BITWISE_KERNEL(And) DEFINE_BITWISE_KERNEL(Or) DEFINE_BITWISE_KERNEL(Xor) -DEFINE_BITWISE_KERNEL(LeftShiftArithmetic) -DEFINE_BITWISE_KERNEL(LeftShiftLogic) -DEFINE_BITWISE_KERNEL(RightShiftArithmetic) -DEFINE_BITWISE_KERNEL(RightShiftLogic) #undef DEFINE_BITWISE_KERNEL +#define DEFINE_BITWISE_KERNEL_WITH_BOOL(op_type) \ + template \ + void Bitwise##op_type##Kernel(const Context& dev_ctx, \ + const DenseTensor& x, \ + const DenseTensor& y, \ + bool is_arithmetic, \ + DenseTensor* out) { \ + dev_ctx.template Alloc(out); \ + std::vector ins = {&x, &y}; \ + std::vector outs = {out}; \ + if (is_arithmetic) { \ + funcs::Bitwise##op_type##ArithmeticFunctor func; \ + funcs::BroadcastKernel(dev_ctx, ins, &outs, func); \ + } else { \ + funcs::Bitwise##op_type##LogicFunctor func; \ + funcs::BroadcastKernel(dev_ctx, ins, &outs, func); \ + } \ + } + +DEFINE_BITWISE_KERNEL_WITH_BOOL(LeftShift) +DEFINE_BITWISE_KERNEL_WITH_BOOL(RightShift) +#undef DEFINE_BITWISE_KERNEL_WITH_BOOL + template void BitwiseNotKernel(const Context& dev_ctx, const DenseTensor& x, @@ -116,40 +135,20 @@ PD_REGISTER_KERNEL(bitwise_not, int, int64_t) {} -PD_REGISTER_KERNEL(bitwise_left_shift_arithmetic, - KPS, - ALL_LAYOUT, - phi::BitwiseLeftShiftArithmeticKernel, - uint8_t, - int8_t, - int16_t, - int, - int64_t) {} - -PD_REGISTER_KERNEL(bitwise_left_shift_logic, - KPS, - ALL_LAYOUT, - phi::BitwiseLeftShiftLogicKernel, - uint8_t, - int8_t, - int16_t, - int, - int64_t) {} - -PD_REGISTER_KERNEL(bitwise_right_shift_arithmetic, +PD_REGISTER_KERNEL(bitwise_left_shift, KPS, ALL_LAYOUT, - phi::BitwiseRightShiftArithmeticKernel, + phi::BitwiseLeftShiftKernel, uint8_t, int8_t, int16_t, int, int64_t) {} -PD_REGISTER_KERNEL(bitwise_right_shift_logic, +PD_REGISTER_KERNEL(bitwise_right_shift, KPS, ALL_LAYOUT, - phi::BitwiseRightShiftLogicKernel, + phi::BitwiseRightShiftKernel, uint8_t, int8_t, int16_t, diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index af1fb15c9f3a8..08f98d6f355f4 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -7101,7 +7101,7 @@ def ldexp_(x, y, name=None): return paddle.multiply_(x, paddle.pow(two, y)) -def _bitwise_op(op_name, x, y, out=None, name=None): +def _bitwise_op(op_name, x, y, is_arithmetic, out=None, name=None): check_variable_and_dtype( x, "x", @@ -7122,7 +7122,10 @@ def _bitwise_op(op_name, x, y, out=None, name=None): out = helper.create_variable_for_type_inference(dtype=x.dtype) helper.append_op( - type=op_name, inputs={"x": x, "y": y}, outputs={"out": out} + type=op_name, + inputs={"x": x, "y": y}, + outputs={"out": out}, + attrs={'is_arithmetic': is_arithmetic}, ) return out @@ -7159,32 +7162,21 @@ def bitwise_left_shift(x, y, is_arithmetic=True, out=None, name=None): >>> import paddle >>> x = paddle.to_tensor([[1,2,4,8],[16,17,32,65]]) >>> y = paddle.to_tensor([[1,2,3,4,], [2,3,2,1]]) - >>> paddle.bitwise_left_shift(x, y) + >>> paddle.bitwise_left_shift(x, y, is_arithmetic=True) Tensor(shape=[2, 4], dtype=int64, place=Place(gpu:0), stop_gradient=True, [[2 , 8 , 32 , 128], [64 , 136, 128, 130]]) """ if in_dynamic_mode() and out is None: - if is_arithmetic: - return _C_ops.bitwise_left_shift_arithmetic(x, y) - else: - return _C_ops.bitwise_left_shift_logic(x, y) - if is_arithmetic: - return _bitwise_op( - op_name="bitwise_left_shift_arithmetic", - x=x, - y=y, - name=name, - out=out, - ) - else: - return _bitwise_op( - op_name="bitwise_left_shift_logic", - x=x, - y=y, - name=name, - out=out, - ) + return _C_ops.bitwise_left_shift(x, y, is_arithmetic) + return _bitwise_op( + op_name="bitwise_left_shift", + x=x, + y=y, + is_arithmetic=is_arithmetic, + name=name, + out=out, + ) @inplace_apis_in_dygraph_only @@ -7201,10 +7193,7 @@ def bitwise_left_shift_(x, y, is_arithmetic=True, out=None, name=None): ) ) if in_dynamic_or_pir_mode(): - if is_arithmetic: - return _C_ops.bitwise_left_shift_arithmetic_(x, y) - else: - return _C_ops.bitwise_left_shift_logic_(x, y) + return _C_ops.bitwise_left_shift_(x, y, is_arithmetic) def bitwise_right_shift(x, y, is_arithmetic=True, out=None, name=None): @@ -7238,32 +7227,22 @@ def bitwise_right_shift(x, y, is_arithmetic=True, out=None, name=None): >>> import paddle >>> x = paddle.to_tensor([[10,20,40,80],[16,17,32,65]]) >>> y = paddle.to_tensor([[1,2,3,4,], [2,3,2,1]]) - >>> paddle.bitwise_right_shift(x, y) + >>> paddle.bitwise_right_shift(x, y, is_arithmetic=True) Tensor(shape=[2, 4], dtype=int64, place=Place(gpu:0), stop_gradient=True, [[5 , 5 , 5 , 5 ], [4 , 2 , 8 , 32]]) """ if in_dynamic_mode() and out is None: - if is_arithmetic: - return _C_ops.bitwise_right_shift_arithmetic(x, y) - else: - return _C_ops.bitwise_right_shift_logic(x, y) - if is_arithmetic: - return _bitwise_op( - op_name="bitwise_right_shift_arithmetic", - x=x, - y=y, - name=name, - out=out, - ) - else: - return _bitwise_op( - op_name="bitwise_right_shift_logic", - x=x, - y=y, - name=name, - out=out, - ) + return _C_ops.bitwise_right_shift(x, y, is_arithmetic) + + return _bitwise_op( + op_name="bitwise_right_shift", + x=x, + y=y, + is_arithmetic=is_arithmetic, + name=name, + out=out, + ) @inplace_apis_in_dygraph_only @@ -7281,10 +7260,7 @@ def bitwise_right_shift_(x, y, is_arithmetic=True, out=None, name=None): ) if in_dynamic_or_pir_mode(): - if is_arithmetic: - return _C_ops.bitwise_right_shift_arithmetic_(x, y) - else: - return _C_ops.bitwise_right_shift_logic_(x, y) + return _C_ops.bitwise_right_shift_(x, y, is_arithmetic) def hypot(x, y, name=None): From 28e0e6a5669b7d39bbece8ba74fb8f5b71423021 Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Mon, 8 Jan 2024 21:06:24 +0800 Subject: [PATCH 20/27] enhence doc --- python/paddle/tensor/math.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 08f98d6f355f4..59e7f069df998 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -7159,6 +7159,8 @@ def bitwise_left_shift(x, y, is_arithmetic=True, out=None, name=None): Examples: .. code-block:: python + :name: example1 + >>> import paddle >>> x = paddle.to_tensor([[1,2,4,8],[16,17,32,65]]) >>> y = paddle.to_tensor([[1,2,3,4,], [2,3,2,1]]) @@ -7166,6 +7168,18 @@ def bitwise_left_shift(x, y, is_arithmetic=True, out=None, name=None): Tensor(shape=[2, 4], dtype=int64, place=Place(gpu:0), stop_gradient=True, [[2 , 8 , 32 , 128], [64 , 136, 128, 130]]) + + .. code-block:: python + + :name: example2 + + >>> import paddle + >>> x = paddle.to_tensor([[1,2,4,8],[16,17,32,65]]) + >>> y = paddle.to_tensor([[1,2,3,4,], [2,3,2,1]]) + >>> paddle.bitwise_left_shift(x, y, is_arithmetic=False) + Tensor(shape=[2, 4], dtype=int64, place=Place(gpu:0), stop_gradient=True, + [[2 , 8 , 32 , 128], + [64 , 136, 128, 130]]) """ if in_dynamic_mode() and out is None: return _C_ops.bitwise_left_shift(x, y, is_arithmetic) @@ -7224,6 +7238,8 @@ def bitwise_right_shift(x, y, is_arithmetic=True, out=None, name=None): Examples: .. code-block:: python + :name: example1 + >>> import paddle >>> x = paddle.to_tensor([[10,20,40,80],[16,17,32,65]]) >>> y = paddle.to_tensor([[1,2,3,4,], [2,3,2,1]]) @@ -7231,6 +7247,18 @@ def bitwise_right_shift(x, y, is_arithmetic=True, out=None, name=None): Tensor(shape=[2, 4], dtype=int64, place=Place(gpu:0), stop_gradient=True, [[5 , 5 , 5 , 5 ], [4 , 2 , 8 , 32]]) + + .. code-block:: python + + :name: example2 + + >>> import paddle + >>> x = paddle.to_tensor([[-10,-20,-40,-80],[-16,-17,-32,-65]], dtype=paddle.int8) + >>> y = paddle.to_tensor([[1,2,3,4,], [2,3,2,1]], dtype=paddle.int8) + >>> paddle.bitwise_right_shift(x, y, is_arithmetic=False) # logic shift + Tensor(shape=[2, 4], dtype=int8, place=Place(gpu:0), stop_gradient=True, + [[123, 59 , 27 , 11 ], + [60 , 29 , 56 , 95 ]]) """ if in_dynamic_mode() and out is None: return _C_ops.bitwise_right_shift(x, y, is_arithmetic) From 62142438c7a52631fd39070ba22c179c71de93ee Mon Sep 17 00:00:00 2001 From: coco <1228759711@qq.com> Date: Tue, 9 Jan 2024 07:56:36 +0800 Subject: [PATCH 21/27] fix doc --- python/paddle/tensor/math.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 59e7f069df998..7f749d0254144 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -7158,7 +7158,6 @@ def bitwise_left_shift(x, y, is_arithmetic=True, out=None, name=None): Examples: .. code-block:: python - :name: example1 >>> import paddle @@ -7170,7 +7169,6 @@ def bitwise_left_shift(x, y, is_arithmetic=True, out=None, name=None): [64 , 136, 128, 130]]) .. code-block:: python - :name: example2 >>> import paddle From 9d50a523894643bc889a7752719f2a105ed4c9bf Mon Sep 17 00:00:00 2001 From: coco <69197635+cocoshe@users.noreply.github.com> Date: Tue, 9 Jan 2024 19:24:42 +0800 Subject: [PATCH 22/27] Update python/paddle/tensor/math.py Co-authored-by: zachary sun <70642955+sunzhongkai588@users.noreply.github.com> --- python/paddle/tensor/math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 7f749d0254144..b49e5282eab09 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -7158,7 +7158,7 @@ def bitwise_left_shift(x, y, is_arithmetic=True, out=None, name=None): Examples: .. code-block:: python - :name: example1 + :name: bitwise_left_shift_example1 >>> import paddle >>> x = paddle.to_tensor([[1,2,4,8],[16,17,32,65]]) From c74106d44f0519d41f4b30de1c1c7b990f66aa53 Mon Sep 17 00:00:00 2001 From: coco <69197635+cocoshe@users.noreply.github.com> Date: Tue, 9 Jan 2024 19:24:57 +0800 Subject: [PATCH 23/27] Update python/paddle/tensor/math.py Co-authored-by: zachary sun <70642955+sunzhongkai588@users.noreply.github.com> --- python/paddle/tensor/math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index b49e5282eab09..c6fe2b3f2aaf0 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -7169,7 +7169,7 @@ def bitwise_left_shift(x, y, is_arithmetic=True, out=None, name=None): [64 , 136, 128, 130]]) .. code-block:: python - :name: example2 + :name: bitwise_left_shift_example2 >>> import paddle >>> x = paddle.to_tensor([[1,2,4,8],[16,17,32,65]]) From fb5ad3c9c27c32840b56b084bca21c606e17897b Mon Sep 17 00:00:00 2001 From: coco <69197635+cocoshe@users.noreply.github.com> Date: Tue, 9 Jan 2024 19:25:08 +0800 Subject: [PATCH 24/27] Update python/paddle/tensor/math.py Co-authored-by: zachary sun <70642955+sunzhongkai588@users.noreply.github.com> --- python/paddle/tensor/math.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index c6fe2b3f2aaf0..27cbea31ee634 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -7235,8 +7235,7 @@ def bitwise_right_shift(x, y, is_arithmetic=True, out=None, name=None): Examples: .. code-block:: python - - :name: example1 + :name: bitwise_right_shift_example1 >>> import paddle >>> x = paddle.to_tensor([[10,20,40,80],[16,17,32,65]]) From b4e8eddc4798c5e1e0131f1092ea61fe024967d3 Mon Sep 17 00:00:00 2001 From: coco <69197635+cocoshe@users.noreply.github.com> Date: Tue, 9 Jan 2024 19:25:19 +0800 Subject: [PATCH 25/27] Update python/paddle/tensor/math.py Co-authored-by: zachary sun <70642955+sunzhongkai588@users.noreply.github.com> --- python/paddle/tensor/math.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 27cbea31ee634..d7e1e5800b4ea 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -7246,8 +7246,7 @@ def bitwise_right_shift(x, y, is_arithmetic=True, out=None, name=None): [4 , 2 , 8 , 32]]) .. code-block:: python - - :name: example2 + :name: bitwise_right_shift_example2 >>> import paddle >>> x = paddle.to_tensor([[-10,-20,-40,-80],[-16,-17,-32,-65]], dtype=paddle.int8) From 0ecabc86d811e5c67cdd1270c5c6fd1a24df5d6e Mon Sep 17 00:00:00 2001 From: coco <69197635+cocoshe@users.noreply.github.com> Date: Tue, 9 Jan 2024 19:25:34 +0800 Subject: [PATCH 26/27] Update python/paddle/tensor/math.py Co-authored-by: zachary sun <70642955+sunzhongkai588@users.noreply.github.com> --- python/paddle/tensor/math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index d7e1e5800b4ea..6d5dc1a761c04 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -7143,7 +7143,7 @@ def bitwise_left_shift(x, y, is_arithmetic=True, out=None, name=None): ``paddle.bitwise_left_shift`` supports broadcasting. If you want know more about broadcasting, please refer to please refer to `Introduction to Tensor`_ . - .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor. + .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor Args: x (Tensor): Input Tensor of ``bitwise_left_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64. From 809fb46919bcea9d369d8a76dd256772dfd494f8 Mon Sep 17 00:00:00 2001 From: coco <69197635+cocoshe@users.noreply.github.com> Date: Tue, 9 Jan 2024 19:25:49 +0800 Subject: [PATCH 27/27] Update python/paddle/tensor/math.py Co-authored-by: zachary sun <70642955+sunzhongkai588@users.noreply.github.com> --- python/paddle/tensor/math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 6d5dc1a761c04..94d685f299edf 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -7220,7 +7220,7 @@ def bitwise_right_shift(x, y, is_arithmetic=True, out=None, name=None): ``paddle.bitwise_right_shift`` supports broadcasting. If you want know more about broadcasting, please refer to please refer to `Introduction to Tensor`_ . - .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor. + .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor Args: x (Tensor): Input Tensor of ``bitwise_right_shift`` . It is a N-D Tensor of uint8, int8, int16, int32, int64.