diff --git a/python/paddle/fluid/tests/unittests/test_clip_op.py b/python/paddle/fluid/tests/unittests/test_clip_op.py index 359220a7a601f131f89e68c6da8b424d20070c3d..efa8be16088e89ad5d7883138154aeee7540b249 100644 --- a/python/paddle/fluid/tests/unittests/test_clip_op.py +++ b/python/paddle/fluid/tests/unittests/test_clip_op.py @@ -299,6 +299,33 @@ class TestClipAPI(unittest.TestCase): paddle.disable_static() +class TestClipOpFp16(unittest.TestCase): + def test_fp16(self): + paddle.enable_static() + data_shape = [1, 9, 9, 4] + data = np.random.random(data_shape).astype('float16') + + with paddle.static.program_guard(paddle.static.Program()): + images = paddle.static.data( + name='image1', shape=data_shape, dtype='float16' + ) + min = paddle.static.data(name='min1', shape=[1], dtype='float16') + max = paddle.static.data(name='max1', shape=[1], dtype='float16') + out = paddle.clip(images, min, max) + if fluid.core.is_compiled_with_cuda(): + place = paddle.CUDAPlace(0) + exe = paddle.static.Executor(place) + res1 = exe.run( + feed={ + "image1": data, + "min1": np.array([0.2]).astype('float16'), + "max1": np.array([0.8]).astype('float16'), + }, + fetch_list=[out], + ) + paddle.disable_static() + + class TestInplaceClipAPI(TestClipAPI): def _executed_api(self, x, min=None, max=None): return x.clip_(min, max) diff --git a/python/paddle/tensor/math.py b/python/paddle/tensor/math.py index 6391bb093195742d7930a506c90bf3ee307c3da7..4fcb70798ed38ff3d28fb5e64e527242d809a871 100644 --- a/python/paddle/tensor/math.py +++ b/python/paddle/tensor/math.py @@ -2770,11 +2770,11 @@ def clip(x, min=None, max=None, name=None): Out = MIN(MAX(x, min), max) Args: - x (Tensor): An N-D Tensor with data type float32, float64, int32 or int64. + x (Tensor): An N-D Tensor with data type float16, float32, float64, int32 or int64. min (float|int|Tensor, optional): The lower bound with type ``float`` , ``int`` or a ``Tensor`` - with shape [1] and type ``int32``, ``float32``, ``float64``. + with shape [1] and type ``int32``, ``float16``, ``float32``, ``float64``. max (float|int|Tensor, optional): The upper bound with type ``float``, ``int`` or a ``Tensor`` - with shape [1] and type ``int32``, ``float32``, ``float64``. + with shape [1] and type ``int32``, ``float16``, ``float32``, ``float64``. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. Returns: @@ -2803,6 +2803,9 @@ def clip(x, min=None, max=None, name=None): elif x_dtype == 'paddle.int64': min_ = np.iinfo(np.int64).min max_ = np.iinfo(np.int64).max - 2**39 + elif x_dtype == 'paddle.float16': + min_ = float(np.finfo(np.float16).min) + max_ = float(np.finfo(np.float16).max) else: min_ = float(np.finfo(np.float32).min) max_ = float(np.finfo(np.float32).max) @@ -2822,7 +2825,7 @@ def clip(x, min=None, max=None, name=None): check_dtype( min.dtype, 'min', - ['float32', 'float64', 'int32'], + ['float16', 'float32', 'float64', 'int32'], 'clip', '(When the type of min in clip is Variable.)', ) @@ -2832,13 +2835,13 @@ def clip(x, min=None, max=None, name=None): check_dtype( max.dtype, 'max', - ['float32', 'float64', 'int32'], + ['float16', 'float32', 'float64', 'int32'], 'clip', '(When the type of max in clip is Variable.)', ) check_variable_and_dtype( - x, 'x', ['float32', 'float64', 'int32', 'int64'], 'clip' + x, 'x', ['float16', 'float32', 'float64', 'int32', 'int64'], 'clip' ) inputs = {'X': x}