forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
【Hackathon 5th No.13】为 Paddle 新增 signbit API (PaddlePaddle#57882)
* refactor: added basic code of signbit * ✨ Refactor: added signbit API * 🦺 Fix: updated test case * ✏️ Fix: remove int8 support * ✨ Refactor: added int support to sign and less_than op * 🎨 Fix: updated code style * ✨ Refactor: added int8 support for less_than gpu kernel * Update test_sign_op.py * Update test_sign_op.py * Update sign_kernel.cc * Update compare_kernel.cc * Update compare_kernel.cu * ✏️ Fix: fixed indent error * ✏️ Fix: fixed type * ✏️ Fix: fixed typo * Refactor: added uint8 support to sign and updated test case * 🎨 Refactor: updated code style * 🚚 Refactor: moved uint8 support for sign to PaddlePaddle#59514 * 🚚 Refactor: moved uint8 support for sign to PaddlePaddle#59514 * ♻️ Refactor: added uint8 type * ✏️ Fix: updated test case * Fix: fixed intent error * Delete paddle/fluid/ir/dialect/paddle_dialect/ir/generated/pd_ops.parsed.yaml * Delete paddle/fluid/ir/dialect/paddle_dialect/ir/generated/pd_ops_backward.parsed.yaml * ♻️ Fixed commit * Fixed commit * 🎨 Fixed commit * ✏️ Fixed commit * 📝 Updated docs * updated docs * Update python/paddle/tensor/math.py Co-authored-by: zachary sun <70642955+sunzhongkai588@users.noreply.github.com> * Update python/paddle/tensor/math.py Co-authored-by: zachary sun <70642955+sunzhongkai588@users.noreply.github.com> * 🎨 Updated code style * ✏️ Fixed doc error * ✏️ Fixed doc error * 📝 Fixed doc error * 📝 Fixed doc * Update test_signbit.py * ✅ Updated test case * ✅ Updated test case * 🐛 Fixed logical error --------- Co-authored-by: zachary sun <70642955+sunzhongkai588@users.noreply.github.com>
- Loading branch information
Showing
4 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# 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 | ||
from paddle.base import core | ||
|
||
|
||
def ref_np_signbit(x: np.ndarray): | ||
return np.signbit(x) | ||
|
||
|
||
class TestSignbitAPI(unittest.TestCase): | ||
def setUp(self) -> None: | ||
self.cuda_support_dtypes = [ | ||
'float32', | ||
'float64', | ||
'uint8', | ||
'int8', | ||
'int16', | ||
'int32', | ||
'int64', | ||
] | ||
self.cpu_support_dtypes = [ | ||
'float32', | ||
'float64', | ||
'uint8', | ||
'int8', | ||
'int16', | ||
'int32', | ||
'int64', | ||
] | ||
self.place = [paddle.CPUPlace()] | ||
if core.is_compiled_with_cuda(): | ||
self.place.append(paddle.CUDAPlace(0)) | ||
|
||
def test_dtype(self): | ||
def run(place): | ||
paddle.disable_static(place) | ||
if core.is_compiled_with_cuda(): | ||
support_dtypes = self.cuda_support_dtypes | ||
else: | ||
support_dtypes = self.cpu_support_dtypes | ||
|
||
for dtype in support_dtypes: | ||
x = paddle.to_tensor( | ||
np.random.randint(-10, 10, size=[12, 20, 2]).astype(dtype) | ||
) | ||
paddle.signbit(x) | ||
|
||
for place in self.place: | ||
run(place) | ||
|
||
def test_float(self): | ||
def run(place): | ||
paddle.disable_static(place) | ||
if core.is_compiled_with_cuda(): | ||
support_dtypes = self.cuda_support_dtypes | ||
else: | ||
support_dtypes = self.cpu_support_dtypes | ||
|
||
for dtype in 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 = ref_np_signbit(np_x) | ||
np.testing.assert_allclose(np_out, out_expected, rtol=1e-05) | ||
|
||
for place in self.place: | ||
run(place) | ||
|
||
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() |