From 2e5c67ebb25f0b053cb82189cd0f1d8056b11021 Mon Sep 17 00:00:00 2001 From: megemini Date: Tue, 7 Nov 2023 15:40:25 +0800 Subject: [PATCH 1/3] [Change] reshape more dtype and test --- python/paddle/tensor/manipulation.py | 7 +++- test/legacy_test/test_reshape_op.py | 53 ++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 40856399238ae..51ce3e99f941d 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -3734,7 +3734,7 @@ def reshape(x, shape, name=None): - 3. Given a 3-D tensor x with a shape [2, 4, 6], and the target shape is [-1, 0, 3, 2], the reshape operator will transform x into a 4-D tensor with shape [2, 4, 3, 2] and leaving x's data unchanged. In this case, besides -1, 0 means the actual dimension value is going to be copied from the corresponding dimension of x. Args: - x (Tensor): An N-D Tensor. The data type is ``float32``, ``float64``, ``int32``, ``int64`` or ``bool`` + x (Tensor): An N-D Tensor. The data type is ``float16``, ``float32``, ``float64``, ``int16``, ``int32``, ``int64``, ``uint16``, ``int8``, ``uint8``, ``complex64``, ``complex128``, ``bfloat16`` or ``bool``. shape (list|tuple|Tensor): Define the target shape. At most one dimension of the target shape can be -1. The data type is ``int32`` . If ``shape`` is a list or tuple, each element of it should be integer or Tensor with shape []. If ``shape`` is an Tensor, it should be an 1-D Tensor . @@ -3879,6 +3879,11 @@ def get_attr_shape(list_shape): 'int64', 'bool', 'uint16', + 'int8', + 'uint8', + 'complex64', + 'complex128', + 'bfloat16', ], 'reshape', ) diff --git a/test/legacy_test/test_reshape_op.py b/test/legacy_test/test_reshape_op.py index 69f056741ba2b..9776e479422f7 100755 --- a/test/legacy_test/test_reshape_op.py +++ b/test/legacy_test/test_reshape_op.py @@ -453,9 +453,62 @@ def _test_api(self): np.testing.assert_array_equal(res_3, input.reshape([5, 10])) np.testing.assert_array_equal(res_4, input.reshape(shape)) + def _test_static_dtype(self): + places = [paddle.CPUPlace()] + ( + [paddle.CUDAPlace(0)] if base.core.is_compiled_with_cuda() else [] + ) + + dtypes = [ + 'float16', + 'float32', + 'float64', + 'int16', + 'int32', + 'int64', + 'uint16', + 'int8', + 'uint8', + 'complex64', + 'complex128', + 'bfloat16', + 'bool', + ] + for place in places: + for dtype in dtypes: + # core is not compiled with CUDA and not support the bfloat16 + if ( + dtype == 'bfloat16' + and not base.core.is_compiled_with_cuda() + ): + continue + + dtype_paddle = dtype + # numpy not support bfloat16, use uint16 instead + dtype_numpy = dtype if dtype != 'bfloat16' else 'uint16' + + paddle.enable_static() + input = np.random.random([2, 25]).astype(dtype_numpy) + shape = [2, 5, 5] + main_prog = paddle.static.Program() + with paddle.static.program_guard( + main_prog, paddle.static.Program() + ): + x = self.data(name="x", shape=[2, 25], dtype=dtype_paddle) + out_1 = self.reshape(x, shape) + + exe = paddle.static.Executor(place=place) + res_1 = exe.run( + main_prog, + feed={"x": input}, + fetch_list=[out_1], + )[0] + + np.testing.assert_array_equal(res_1, input.reshape(shape)) + def test_paddle_api(self): self._set_paddle_api() self._test_api() + self._test_static_dtype() def test_imperative(self): self._set_paddle_api() From 1d9d20d8ae979704dc68522c5ea9f26c39b4f29b Mon Sep 17 00:00:00 2001 From: megemini Date: Wed, 8 Nov 2023 14:30:35 +0800 Subject: [PATCH 2/3] [Fix] remove uint16 --- python/paddle/tensor/manipulation.py | 3 +-- test/legacy_test/test_reshape_op.py | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 51ce3e99f941d..5cace4e5c36e7 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -3734,7 +3734,7 @@ def reshape(x, shape, name=None): - 3. Given a 3-D tensor x with a shape [2, 4, 6], and the target shape is [-1, 0, 3, 2], the reshape operator will transform x into a 4-D tensor with shape [2, 4, 3, 2] and leaving x's data unchanged. In this case, besides -1, 0 means the actual dimension value is going to be copied from the corresponding dimension of x. Args: - x (Tensor): An N-D Tensor. The data type is ``float16``, ``float32``, ``float64``, ``int16``, ``int32``, ``int64``, ``uint16``, ``int8``, ``uint8``, ``complex64``, ``complex128``, ``bfloat16`` or ``bool``. + x (Tensor): An N-D Tensor. The data type is ``float16``, ``float32``, ``float64``, ``int16``, ``int32``, ``int64``, ``int8``, ``uint8``, ``complex64``, ``complex128``, ``bfloat16`` or ``bool``. shape (list|tuple|Tensor): Define the target shape. At most one dimension of the target shape can be -1. The data type is ``int32`` . If ``shape`` is a list or tuple, each element of it should be integer or Tensor with shape []. If ``shape`` is an Tensor, it should be an 1-D Tensor . @@ -3878,7 +3878,6 @@ def get_attr_shape(list_shape): 'int32', 'int64', 'bool', - 'uint16', 'int8', 'uint8', 'complex64', diff --git a/test/legacy_test/test_reshape_op.py b/test/legacy_test/test_reshape_op.py index 9776e479422f7..41774827dac48 100755 --- a/test/legacy_test/test_reshape_op.py +++ b/test/legacy_test/test_reshape_op.py @@ -465,7 +465,6 @@ def _test_static_dtype(self): 'int16', 'int32', 'int64', - 'uint16', 'int8', 'uint8', 'complex64', From 96926f80c8efbe40bf75b1ed8de352e975c3e84b Mon Sep 17 00:00:00 2001 From: megemini Date: Wed, 8 Nov 2023 18:33:49 +0800 Subject: [PATCH 3/3] [Change] use uint16 instead of bfloat16 --- python/paddle/tensor/manipulation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/tensor/manipulation.py b/python/paddle/tensor/manipulation.py index 5cace4e5c36e7..6b8fa9ffc7c27 100644 --- a/python/paddle/tensor/manipulation.py +++ b/python/paddle/tensor/manipulation.py @@ -3878,11 +3878,11 @@ def get_attr_shape(list_shape): 'int32', 'int64', 'bool', + 'uint16', 'int8', 'uint8', 'complex64', 'complex128', - 'bfloat16', ], 'reshape', )