Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Hackathon 5th No.13】【关联 PR】Added int support for sign #58191

Closed
wants to merge 14 commits into from
Closed
2 changes: 2 additions & 0 deletions python/paddle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@
polygamma_,
hypot,
hypot_,
signbit,
)

from .tensor.random import (
Expand Down Expand Up @@ -908,4 +909,5 @@
'polygamma_',
'hypot',
'hypot_',
'signbit',
]
1 change: 1 addition & 0 deletions python/paddle/tensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,7 @@
'asinh_',
'diag',
'normal_',
'signbit',
]

# this list used in math_op_patch.py for magic_method bind
Expand Down
48 changes: 48 additions & 0 deletions python/paddle/tensor/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -6985,3 +6985,51 @@ def hypot_(x, y, name=None):

out = x.pow_(2).add_(y.pow(2)).sqrt_()
return out


def signbit(x, name=None):
r"""
Tests if each element of input has its sign bit set or not.

Args:
x (Tensor): The input Tensor. Must be one of the following types: float16, float32, float64, bfloat16, uint8, int8, int16, int32, int64.
name (str, optional): Name for the operation (optional, default is None).For more information, please refer to :ref:`api_guide_Name`.

Returns:
out (Tensor): The output Tensor. The sign bit of the corresponding element of the input tensor, True means negative, False means positive.
>>> # example1
>>> x = paddle.to_tensor([1.1, -2.1, -0., 2.5], dtype='float32')
>>> res = paddle.signbit(x, y)
>>> print(res)
Tensor(shape=[4], dtype=bool, place=Place(cpu), stop_gradient=True,
[False , True, True, False])

>>> # example2
>>> x = paddle.to_tensor([-5, -2, 3], dtype='int32')
>>> res = paddle.signbit(x, y)
>>> print(res)
Tensor(shape=[3], dtype=bool, place=Place(cpu), stop_gradient=True,
[True. , True , False])
"""
if not isinstance(x, (paddle.Tensor, Variable)):
raise TypeError(f"x must be tensor type, but got {type(x)}")

check_variable_and_dtype(
x,
"x",
[
'float16',
'float32',
'float64',
'bfloat16',
'int8',
'int16',
'int32',
'int64',
],
"signbit",
)

out_mask = paddle.cast(x < 0, dtype='bool')
out_mask[x == -0] = True
return out_mask
66 changes: 66 additions & 0 deletions test/legacy_test/test_signbit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# 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


def np_signbit(x: np.ndarray):
return np.signbit(x)


class TestSignbitAPI(unittest.TestCase):
def setUp(self) -> None:
self.support_dtypes = [
'float16',
'float32',
'float64',
'bfloat16',
'int8',
'int16',
'int32',
'int64',
]
if paddle.device.get_device() == 'cpu':
self.support_dtypes = [
'float32',
'float64',
'int8',
'int16',
'int32',
'int64',
]

def test_dtype(self):
for dtype in self.support_dtypes:
x = paddle.to_tensor(
np.random.randint(-10, 10, size=[12, 20, 2]).astype(dtype)
)
paddle.signbit(x)

def test_float(self):
for dtype in self.support_dtypes:
np_x = np.random.randint(-10, 10, size=[12, 20, 2]).astype(dtype)
x = paddle.to_tensor(np_x)
out = paddle.signbit(x)
np_out = out.numpy()
out_expected = np_signbit(np_x)
np.testing.assert_allclose(np_out, out_expected, rtol=1e-05)


if __name__ == "__main__":
unittest.main()