diff --git a/python/paddle/fluid/tests/unittests/test_diff_op.py b/python/paddle/fluid/tests/unittests/test_diff_op.py index b89f7759a73a6164f9dd2f5704d509ee5442cb53..1c1a8639ee2b67483861c98e9c7cc55c392117cc 100644 --- a/python/paddle/fluid/tests/unittests/test_diff_op.py +++ b/python/paddle/fluid/tests/unittests/test_diff_op.py @@ -228,6 +228,36 @@ class TestDiffOpPreAppendAxis(TestDiffOp): self.append = np.array([[2, 3, 4, 7], [1, 3, 5, 6]]).astype('float32') +class TestDiffOpFp16(TestDiffOp): + def test_fp16_with_gpu(self): + paddle.enable_static() + if paddle.fluid.core.is_compiled_with_cuda(): + place = paddle.CUDAPlace(0) + with paddle.static.program_guard( + paddle.static.Program(), paddle.static.Program() + ): + input = np.random.random([4, 4]).astype("float16") + x = paddle.static.data( + name="input", shape=[4, 4], dtype="float16" + ) + exe = paddle.static.Executor(place) + out = paddle.diff( + x, + n=self.n, + axis=self.axis, + prepend=self.prepend, + append=self.append, + ) + fetches = exe.run( + paddle.static.default_main_program(), + feed={ + "input": input, + }, + fetch_list=[out], + ) + paddle.disable_static() + + if __name__ == '__main__': paddle.enable_static() unittest.main() diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 3fbc72c01f6bdfa61ec69a889a07c05a438232f1..d040ba1673ff45e19b34ec7262fd6c7c9ce1da26 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -4611,7 +4611,7 @@ def diff(x, n=1, axis=-1, prepend=None, append=None, name=None): Only n=1 is currently supported. Args: - x (Tensor): The input tensor to compute the forward difference on + x (Tensor): The input tensor to compute the forward difference on, the data type is float16(GPU), float32, float64, bool, int32, int64. n (int, optional): The number of times to recursively compute the difference. Only support n=1. Default:1 axis (int, optional): The axis to compute the difference along. Default:-1 @@ -4706,7 +4706,10 @@ def diff(x, n=1, axis=-1, prepend=None, append=None, name=None): return _C_ops.subtract(input_back, input_front) else: check_variable_and_dtype( - x, 'x', ['float32', 'float64', 'bool', 'int32', 'int64'], 'diff' + x, + 'x', + ['float16', 'float32', 'float64', 'bool', 'int32', 'int64'], + 'diff', ) check_type(axis, 'axis', (int), 'diff') helper = LayerHelper('diff', **locals())