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 uint8 support for sign kernel -part #59514

Merged
merged 3 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions paddle/phi/kernels/cpu/sign_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ PD_REGISTER_KERNEL(sign,
CPU,
ALL_LAYOUT,
phi::SignKernel,
uint8_t,
int8_t,
int16_t,
int32_t,
Expand Down
1 change: 1 addition & 0 deletions paddle/phi/kernels/funcs/eigen/sign.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct EigenSign<Eigen::DefaultDevice, T> {
}
};

template struct EigenSign<Eigen::DefaultDevice, uint8_t>;
template struct EigenSign<Eigen::DefaultDevice, int8_t>;
template struct EigenSign<Eigen::DefaultDevice, int16_t>;
template struct EigenSign<Eigen::DefaultDevice, int32_t>;
Expand Down
1 change: 1 addition & 0 deletions paddle/phi/kernels/funcs/eigen/sign.cu
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct EigenSign<Eigen::GpuDevice, T> {
}
};

template struct EigenSign<Eigen::GpuDevice, uint8_t>;
template struct EigenSign<Eigen::GpuDevice, int8_t>;
template struct EigenSign<Eigen::GpuDevice, int16_t>;
template struct EigenSign<Eigen::GpuDevice, int32_t>;
Expand Down
1 change: 1 addition & 0 deletions paddle/phi/kernels/gpu/sign_kernel.cu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ PD_REGISTER_KERNEL(sign,
GPU,
ALL_LAYOUT,
phi::SignKernel,
uint8_t,
int8_t,
int16_t,
int32_t,
Expand Down
4 changes: 2 additions & 2 deletions python/paddle/tensor/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -4596,7 +4596,7 @@ def sign(x, name=None):
Returns sign of every element in `x`: 1 for positive, -1 for negative and 0 for zero.

Args:
x (Tensor): The input tensor. The data type can be int8, int16, int32, int64, float16, float32 or float64.
x (Tensor): The input tensor. The data type can be uint8, int8, int16, int32, int64, float16, float32 or float64.
name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

Returns:
Expand All @@ -4620,14 +4620,14 @@ def sign(x, name=None):
x,
'x',
[
'uint8',
'int8',
'int16',
'int32',
'int64',
'float16',
'float32',
'float64',
'uint16',
],
'sign',
)
Expand Down
38 changes: 27 additions & 11 deletions test/legacy_test/test_sign_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,48 +93,64 @@ def test_dygraph(self):
self.assertEqual((np_z == z_expected).all(), True)

def test_static(self):
np_input2 = np.random.uniform(-10, 10, (12, 10)).astype("int16")
np_input3 = np.random.uniform(-10, 10, (12, 10)).astype("int32")
np_input4 = np.random.uniform(-10, 10, (12, 10)).astype("int64")
np_input1 = np.random.uniform(-10, 10, (12, 10)).astype("int8")
np_input2 = np.random.uniform(-10, 10, (12, 10)).astype("uint8")
np_input3 = np.random.uniform(-10, 10, (12, 10)).astype("int16")
np_input4 = np.random.uniform(-10, 10, (12, 10)).astype("int32")
np_input5 = np.random.uniform(-10, 10, (12, 10)).astype("int64")
np_out1 = np.sign(np_input1)
np_out2 = np.sign(np_input2)
np_out3 = np.sign(np_input3)
np_out4 = np.sign(np_input4)
np_out5 = np.sign(np_input5)

def run(place):
with program_guard(Program(), Program()):
# The input type of sign_op must be Variable or numpy.ndarray.
input1 = 12
self.assertRaises(TypeError, paddle.tensor.math.sign, input1)
# The result of sign_op must correct.
input1 = paddle.static.data(
name='input1', shape=[12, 10], dtype="int8"
)
input2 = paddle.static.data(
name='input2', shape=[12, 10], dtype="int16"
name='input2', shape=[12, 10], dtype="uint8"
)
input3 = paddle.static.data(
name='input3', shape=[12, 10], dtype="int32"
name='input3', shape=[12, 10], dtype="int16"
)
input4 = paddle.static.data(
name='input4', shape=[12, 10], dtype="int64"
name='input4', shape=[12, 10], dtype="int32"
)
input5 = paddle.static.data(
name='input5', shape=[12, 10], dtype="int64"
)
out1 = paddle.sign(input1)
out2 = paddle.sign(input2)
out3 = paddle.sign(input3)
out4 = paddle.sign(input4)
out5 = paddle.sign(input5)
exe = paddle.static.Executor(place)
res2, res3, res4 = exe.run(
res1, res2, res3, res4, res5 = exe.run(
paddle.static.default_main_program(),
feed={
"input1": np_input1,
"input2": np_input2,
"input3": np_input3,
"input4": np_input4,
"input5": np_input5,
},
fetch_list=[out2, out3, out4],
fetch_list=[out1, out2, out3, out4, out5],
)
self.assertEqual((res1 == np_out1).all(), True)
self.assertEqual((res2 == np_out2).all(), True)
self.assertEqual((res3 == np_out3).all(), True)
self.assertEqual((res4 == np_out4).all(), True)
input5 = paddle.static.data(
name='input5', shape=[-1, 4], dtype="float16"
self.assertEqual((res5 == np_out5).all(), True)
input6 = paddle.static.data(
name='input6', shape=[-1, 4], dtype="float16"
)
paddle.sign(input5)
paddle.sign(input6)

for place in self.place:
run(place)
Expand Down