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】为 Paddle 新增 signbit API #57882

Merged
merged 46 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
4c77ca9
refactor: added basic code of signbit
PommesPeter Sep 26, 2023
2b45bf7
:sparkles: Refactor: added signbit API
PommesPeter Oct 6, 2023
84cdb0d
:safety_vest: Fix: updated test case
PommesPeter Oct 6, 2023
d80e679
:pencil2: Fix: remove int8 support
PommesPeter Oct 6, 2023
112ce14
:sparkles: Refactor: added int support to sign and less_than op
PommesPeter Oct 10, 2023
ad5b059
:art: Fix: updated code style
PommesPeter Oct 10, 2023
0ebade0
:sparkles: Refactor: added int8 support for less_than gpu kernel
PommesPeter Oct 10, 2023
7c5a2ec
Merge branch 'develop' into signbit
PommesPeter Oct 19, 2023
5dafc10
Update test_sign_op.py
PommesPeter Oct 19, 2023
5766a13
Update test_sign_op.py
PommesPeter Oct 19, 2023
71ea073
Update sign_kernel.cc
PommesPeter Oct 19, 2023
32a742e
Update compare_kernel.cc
PommesPeter Oct 19, 2023
9585972
Update compare_kernel.cu
PommesPeter Oct 19, 2023
c3ce852
:pencil2: Fix: fixed indent error
PommesPeter Oct 19, 2023
5edf3b9
:pencil2: Fix: fixed type
PommesPeter Oct 20, 2023
642120a
:pencil2: Fix: fixed typo
PommesPeter Oct 20, 2023
892d2d0
Merge branch 'develop' into signbit
PommesPeter Nov 4, 2023
6b09f26
Merge branch 'develop' into signbit
PommesPeter Nov 23, 2023
ea7ea79
Refactor: added uint8 support to sign and updated test case
PommesPeter Nov 29, 2023
e86aeee
:art: Refactor: updated code style
PommesPeter Nov 29, 2023
e11eeca
:truck: Refactor: moved uint8 support for sign to #59514
PommesPeter Nov 29, 2023
76ed6cb
:truck: Refactor: moved uint8 support for sign to #59514
PommesPeter Nov 29, 2023
643e88e
:recycle: Refactor: added uint8 type
PommesPeter Nov 29, 2023
519aeed
:pencil2: Fix: updated test case
PommesPeter Nov 30, 2023
68f1a36
Merge branch 'develop' into signbit
PommesPeter Dec 1, 2023
2bddf39
Fix: fixed intent error
PommesPeter Dec 1, 2023
62a3721
Delete paddle/fluid/ir/dialect/paddle_dialect/ir/generated/pd_ops.par…
PommesPeter Dec 1, 2023
161c035
Delete paddle/fluid/ir/dialect/paddle_dialect/ir/generated/pd_ops_bac…
PommesPeter Dec 1, 2023
be7b028
:recycle: Fixed commit
PommesPeter Dec 1, 2023
0c0f238
Fixed commit
PommesPeter Dec 1, 2023
08b79df
:art: Fixed commit
PommesPeter Dec 1, 2023
2ec4162
:pencil2: Fixed commit
PommesPeter Dec 1, 2023
5ea9b13
:memo: Updated docs
PommesPeter Dec 11, 2023
82ac407
updated docs
PommesPeter Dec 11, 2023
034f2bb
Merge branch 'develop' into signbit
PommesPeter Dec 12, 2023
70d31d3
Update python/paddle/tensor/math.py
PommesPeter Dec 12, 2023
3ca65ce
Update python/paddle/tensor/math.py
PommesPeter Dec 12, 2023
5a02692
:art: Updated code style
PommesPeter Dec 12, 2023
e4d17c3
:pencil2: Fixed doc error
PommesPeter Dec 13, 2023
36d6028
:pencil2: Fixed doc error
PommesPeter Dec 13, 2023
90144c6
:memo: Fixed doc error
PommesPeter Dec 14, 2023
4aeca1e
:memo: Fixed doc
PommesPeter Dec 16, 2023
4837543
Update test_signbit.py
PommesPeter Dec 17, 2023
0e39270
:white_check_mark: Updated test case
PommesPeter Dec 17, 2023
9b3a135
:white_check_mark: Updated test case
PommesPeter Dec 18, 2023
c2d511f
:bug: Fixed logical error
PommesPeter Dec 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions python/paddle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@
scale,
sgn,
sign,
signbit,
sin,
sin_,
sinh,
Expand Down Expand Up @@ -919,4 +920,5 @@
"index_fill_",
'diagonal_scatter',
'combinations',
'signbit',
]
1 change: 1 addition & 0 deletions python/paddle/tensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,7 @@
'atleast_3d',
'diagonal_scatter',
"combinations",
'signbit',
]

# this list used in math_op_patch.py for magic_method bind
Expand Down
57 changes: 57 additions & 0 deletions python/paddle/tensor/math.py
PommesPeter marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -4632,6 +4632,7 @@ def sign(x, name=None):
'int32',
'int64',
'float16',
'bfloat16',
'float32',
'float64',
],
Expand Down Expand Up @@ -7169,3 +7170,59 @@ def combinations(x, r=2, with_replacement=False, name=None):
grids[i] = grids[i].masked_select(mask)

return paddle.stack(grids, 1)


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.
PommesPeter marked this conversation as resolved.
Show resolved Hide resolved
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.

Examples:
.. code-block:: python
:name: signbit-example-1

>>> import paddle
>>> x = paddle.to_tensor([1.1, -2.1, 0., 2.5, -0.0], dtype='float32')
>>> res = paddle.signbit(x, y)
>>> print(res)
Tensor(shape=[4], dtype=bool, place=Place(cpu), stop_gradient=True,
[False , True, False, False, True])
PommesPeter marked this conversation as resolved.
Show resolved Hide resolved

.. code-block:: python
:name: signbit-example-2

>>> 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])
PommesPeter marked this conversation as resolved.
Show resolved Hide resolved
PommesPeter marked this conversation as resolved.
Show resolved Hide resolved
"""
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',
'uint8',
'int8',
'int16',
'int32',
'int64',
],
"signbit",
)

x = paddle.sign(x)
out = paddle.cast(x < 0, dtype='bool')
return out
72 changes: 72 additions & 0 deletions test/legacy_test/test_signbit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# 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',
'uint8',
'int8',
'int16',
'int32',
'int64',
]
if paddle.device.get_device() == 'cpu':
self.support_dtypes = [
'float32',
'float64',
'uint8',
'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)

def test_input_type(self):
with self.assertRaises(TypeError):
x = np.random.randint(-10, 10, size=[12, 20, 2]).astype('float32')
x = paddle.signbit(x)


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