diff --git a/paddle/phi/kernels/cpu/sign_kernel.cc b/paddle/phi/kernels/cpu/sign_kernel.cc index f03f39f80dcbe..0db47fc2f5266 100644 --- a/paddle/phi/kernels/cpu/sign_kernel.cc +++ b/paddle/phi/kernels/cpu/sign_kernel.cc @@ -25,6 +25,7 @@ PD_REGISTER_KERNEL(sign, CPU, ALL_LAYOUT, phi::SignKernel, + uint8_t, int8_t, int16_t, int32_t, diff --git a/paddle/phi/kernels/funcs/eigen/sign.cc b/paddle/phi/kernels/funcs/eigen/sign.cc index e71257f3f74aa..fe5868e5ef441 100644 --- a/paddle/phi/kernels/funcs/eigen/sign.cc +++ b/paddle/phi/kernels/funcs/eigen/sign.cc @@ -29,6 +29,7 @@ struct EigenSign { } }; +template struct EigenSign; template struct EigenSign; template struct EigenSign; template struct EigenSign; diff --git a/paddle/phi/kernels/funcs/eigen/sign.cu b/paddle/phi/kernels/funcs/eigen/sign.cu index 58a4fe36232b6..88922bd86f2e2 100644 --- a/paddle/phi/kernels/funcs/eigen/sign.cu +++ b/paddle/phi/kernels/funcs/eigen/sign.cu @@ -29,6 +29,7 @@ struct EigenSign { } }; +template struct EigenSign; template struct EigenSign; template struct EigenSign; template struct EigenSign; diff --git a/paddle/phi/kernels/gpu/sign_kernel.cu.cc b/paddle/phi/kernels/gpu/sign_kernel.cu.cc index bbccc906a06e3..c160410ea6054 100644 --- a/paddle/phi/kernels/gpu/sign_kernel.cu.cc +++ b/paddle/phi/kernels/gpu/sign_kernel.cu.cc @@ -25,6 +25,7 @@ PD_REGISTER_KERNEL(sign, GPU, ALL_LAYOUT, phi::SignKernel, + uint8_t, int8_t, int16_t, int32_t, diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 85d1e1f8541c7..763d061872929 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -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: @@ -4620,6 +4620,7 @@ def sign(x, name=None): x, 'x', [ + 'uint8', 'int8', 'int16', 'int32', @@ -4627,7 +4628,6 @@ def sign(x, name=None): 'float16', 'float32', 'float64', - 'uint16', ], 'sign', ) diff --git a/test/legacy_test/test_sign_op.py b/test/legacy_test/test_sign_op.py index 92422a6fb9520..24a5983c279db 100644 --- a/test/legacy_test/test_sign_op.py +++ b/test/legacy_test/test_sign_op.py @@ -93,12 +93,16 @@ 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()): @@ -106,35 +110,47 @@ def run(place): 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)