diff --git a/python/paddle/fluid/contrib/slim/tests/imperative_test_utils.py b/python/paddle/fluid/contrib/slim/tests/imperative_test_utils.py index 1a5f52b0406d8a2392463165c880be652666296c..c0637bc7de480e60bc30b40dfaa2cde4f67a5876 100644 --- a/python/paddle/fluid/contrib/slim/tests/imperative_test_utils.py +++ b/python/paddle/fluid/contrib/slim/tests/imperative_test_utils.py @@ -153,7 +153,7 @@ class ImperativeLenet(fluid.dygraph.Layer): x = self.quant_stub(inputs) x = self.features(x) - x = fluid.layers.flatten(x, 1) + x = paddle.flatten(x, 1, -1) x = self.add(x, paddle.to_tensor(0.0)) # For CI x = self.fc(x) return x @@ -238,7 +238,7 @@ class ImperativeLenetWithSkipQuant(fluid.dygraph.Layer): x = self.relu6_0(x) x = self.pool2d_1(x) - x = fluid.layers.flatten(x, 1) + x = paddle.flatten(x, 1, -1) x = self.linear_0(x) x = self.leaky_relu_0(x) diff --git a/python/paddle/fluid/contrib/slim/tests/test_imperative_out_scale.py b/python/paddle/fluid/contrib/slim/tests/test_imperative_out_scale.py index 02b19947ec9f60a5b207a93246b9c7a014032e9f..e978b636452078915b30a74b1a3383929d94b1ee 100644 --- a/python/paddle/fluid/contrib/slim/tests/test_imperative_out_scale.py +++ b/python/paddle/fluid/contrib/slim/tests/test_imperative_out_scale.py @@ -121,7 +121,7 @@ class ImperativeLenet(fluid.dygraph.Layer): def forward(self, inputs): x = self.features(inputs) - x = fluid.layers.flatten(x, 1) + x = paddle.flatten(x, 1, -1) x = self.fc(x) return x diff --git a/python/paddle/fluid/contrib/slim/tests/test_imperative_qat_lsq.py b/python/paddle/fluid/contrib/slim/tests/test_imperative_qat_lsq.py index fbf1f4e7f5180fc5c271d376d58ab80f176ef0a2..1b54a5b55b1083c2fcb8d11260ad2b0ec01fe313 100644 --- a/python/paddle/fluid/contrib/slim/tests/test_imperative_qat_lsq.py +++ b/python/paddle/fluid/contrib/slim/tests/test_imperative_qat_lsq.py @@ -117,7 +117,7 @@ class ImperativeLenet(fluid.dygraph.Layer): def forward(self, inputs): x = self.features(inputs) - x = fluid.layers.flatten(x, 1) + x = paddle.flatten(x, 1, -1) x = self.fc(x) return x diff --git a/python/paddle/fluid/layers/detection.py b/python/paddle/fluid/layers/detection.py index 1519b04243e6ba0c848b221bfae06bd235cb0bc6..543f63b639ea7ca2fb611967cd4c2361a5e1fb9b 100644 --- a/python/paddle/fluid/layers/detection.py +++ b/python/paddle/fluid/layers/detection.py @@ -1739,7 +1739,9 @@ def ssd_loss( conf_shape = nn.shape(confidence) def __reshape_to_2d(var): - return nn.flatten(x=var, axis=2) + out = paddle.flatten(var, 2, -1) + out = paddle.flatten(out, 0, 1) + return out # 1. Find matched bounding box by prior box. # 1.1 Compute IOU similarity between ground-truth boxes and prior boxes. @@ -2335,8 +2337,15 @@ def multi_box_head( """ def _reshape_with_axis_(input, axis=1): - out = nn.flatten(x=input, axis=axis) - return out + # Note : axis!=0 in current references to this func + # if axis == 0: + # x = paddle.flatten(input, 0, -1) + # x = paddle.unsqueeze(x, 0) + # return x + # else: + x = paddle.flatten(input, axis, -1) + x = paddle.flatten(x, 0, axis - 1) + return x def _is_list_or_tuple_(data): return isinstance(data, list) or isinstance(data, tuple) @@ -2445,7 +2454,7 @@ def multi_box_head( ) mbox_loc = paddle.transpose(mbox_loc, perm=[0, 2, 3, 1]) - mbox_loc_flatten = nn.flatten(mbox_loc, axis=1) + mbox_loc_flatten = paddle.flatten(mbox_loc, 1, -1) mbox_locs.append(mbox_loc_flatten) # get conf @@ -2457,8 +2466,9 @@ def multi_box_head( padding=pad, stride=stride, ) + conf_loc = paddle.transpose(conf_loc, perm=[0, 2, 3, 1]) - conf_loc_flatten = nn.flatten(conf_loc, axis=1) + conf_loc_flatten = paddle.flatten(conf_loc, 1, -1) mbox_confs.append(conf_loc_flatten) if len(box_results) == 1: diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 821a974dd7863b697114fb5a9eee8c34b1d1286c..3e40a80216ac67e73cf6a29d82a388b2b5c013d4 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -109,7 +109,6 @@ __all__ = [ 'log', 'crop_tensor', 'prelu', - 'flatten', 'unique', 'unique_with_counts', 'elementwise_add', @@ -6842,98 +6841,6 @@ def prelu(x, mode, param_attr=None, data_format="NCHW", name=None): return out -def flatten(x, axis=1, name=None): - r""" - **Flatten op** - - Flatten the input tensor into a 2D matrix. - - For Example: - - .. code-block:: text - - Case 1: - - Given - X.shape = (3, 100, 100, 4) - - and - axis = 2 - - We get: - Out.shape = (3 * 100, 4 * 100) - - Case 2: - - Given - X.shape = (3, 100, 100, 4) - - and - axis = 0 - - We get: - Out.shape = (1, 3 * 100 * 100 * 4) - - Args: - x (Variable): A tensor of rank >= axis. A tensor with type float32, - float64, int8, int32, int64, uint8. - axis (int): Indicate up to which input dimensions (exclusive) should - be flattened to the outer dimension of the output. - The value for axis must be in the range [0, R], where R - is the rank of the input tensor. Default: 1. - name(str, Optional): For details, please refer to :ref:`api_guide_Name`. - Generally, no setting is required. Default: None. - - Returns: - Variable: A 2D tensor with the contents of the input tensor, with input \ - dimensions up to axis flattened to the outer dimension of \ - the output and remaining input dimensions flattened into the \ - inner dimension of the output. A Tensor with type same as input x. - - Raises: - ValueError: If x is not a variable. - ValueError: If axis is not in range [0, rank(x)]. - - Examples: - - .. code-block:: python - - import paddle - import paddle.fluid as fluid - paddle.enable_static() - x = fluid.data(name="x", shape=[4, 4, 3], dtype="float32") - # x shape is [4, 4, 3] - out = fluid.layers.flatten(x=x, axis=2) - # out shape is [16, 3] - """ - check_variable_and_dtype( - x, - 'x', - ['float32', 'float64', 'int8', 'int32', 'int64', 'uint8'], - 'flatten', - ) - if _non_static_mode(): - return _legacy_C_ops.flatten2(x, 'axis', axis)[0] - - helper = LayerHelper('flatten', **locals()) - - if not (isinstance(x, Variable)): - raise ValueError("The input x should be a Variable") - - if not (isinstance(axis, int)) or axis > len(x.shape) or axis < 0: - raise ValueError("The axis should be a int, and in range [0, rank(x)]") - - out = helper.create_variable_for_type_inference(x.dtype) - x_shape = helper.create_variable_for_type_inference(x.dtype) - helper.append_op( - type='flatten2', - inputs={"X": x}, - outputs={'Out': out, 'XShape': x_shape}, - attrs={"axis": axis}, - ) - return out - - from paddle.fluid.framework import convert_np_dtype_to_dtype_ diff --git a/python/paddle/fluid/tests/unittests/ipu/test_dy2static_fp16_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_dy2static_fp16_ipu.py index d5049979c0f1040459f4efcbf85d33b4909d4ee6..8a13e5abb552379e418ed5cf46b3fba93eed53e9 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_dy2static_fp16_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_dy2static_fp16_ipu.py @@ -31,7 +31,7 @@ class SimpleLayer(paddle.nn.Layer): def forward(self, x, target=None): x = self.conv(x) - x = paddle.fluid.layers.flatten(x, axis=1) + x = paddle.flatten(x, 1, -1) if target is not None: x = paddle.fluid.layers.softmax(x) loss = paddle.fluid.layers.cross_entropy(x, target) diff --git a/python/paddle/fluid/tests/unittests/ipu/test_dy2static_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_dy2static_ipu.py index 4e16b0efdf822ea8e3e20245f39bcba985055d6e..fd1c762f2095caa9316de4b8d6999d9cf8023602 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_dy2static_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_dy2static_ipu.py @@ -46,7 +46,7 @@ class SimpleLayer(paddle.nn.Layer): @to_static() def forward(self, x, target=None): x = self.conv(x) - x = paddle.fluid.layers.flatten(x, axis=1) + x = paddle.flatten(x, 1, -1) if target is not None: if self.use_softmax: x = paddle.fluid.layers.softmax(x) diff --git a/python/paddle/fluid/tests/unittests/ipu/test_flatten_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_flatten_op_ipu.py index 58108b262e0ffc2e81cb7797830c7f1ca19d4454..fe89e81854a1bae385c5ff0f0c2632885788b8fa 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_flatten_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_flatten_op_ipu.py @@ -47,7 +47,12 @@ class TestBase(IPUOpTest): x = paddle.static.data( name=self.feed_list[0], shape=self.feed_shape[0], dtype='float32' ) - out = paddle.fluid.layers.flatten(x=x, **self.attrs) + if self.attrs['axis'] == 0: + x = paddle.flatten(x, 0, -1) + out = paddle.unsqueeze(x, 0) + else: + x = paddle.flatten(x, self.attrs['axis'], -1) + out = paddle.flatten(x, 0, self.attrs['axis'] - 1) self.fetch_list = [out.name] def run_model(self, exec_mode): diff --git a/python/paddle/fluid/tests/unittests/ipu/test_modelruntime_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_modelruntime_ipu.py index 383b2632aa0c5aa3af382cd2ecabf1f5eddbb7ee..9fda7f780e86d4bad2505025b03f82e42009edff 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_modelruntime_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_modelruntime_ipu.py @@ -30,7 +30,7 @@ class SimpleLayer(paddle.nn.Layer): def forward(self, x, target=None): x = self.conv(x) - x = paddle.fluid.layers.flatten(x, axis=1) + x = paddle.flatten(x, 1, -1) if target is not None: x = paddle.fluid.layers.softmax(x) loss = paddle.fluid.layers.cross_entropy(x, target) diff --git a/python/paddle/fluid/tests/unittests/ipu/test_print_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_print_op_ipu.py index 6a0d384fa9c71e44a810efa36fe7a088e9d90c96..ccf0a38bbf88c74636a10f932ca1f5c09b69731d 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_print_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_print_op_ipu.py @@ -117,7 +117,7 @@ class SimpleLayer(paddle.nn.Layer): def forward(self, x, target=None): x = self.conv(x) print(x) - x = paddle.fluid.layers.flatten(x, axis=1) + x = paddle.flatten(x, 1, -1) if target is not None: x = paddle.fluid.layers.softmax(x) loss = paddle.fluid.layers.cross_entropy(x, target) diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_flatten_op.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_flatten_op.py index 8beb2000c688f2258e96d5f02ba55bf951300e1b..4ed648ed9c8062384cdbf11ad90142d12b752cbb 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_flatten_op.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_flatten_op.py @@ -17,6 +17,7 @@ import unittest import numpy as np from inference_pass_test import InferencePassTest +import paddle import paddle.fluid as fluid import paddle.fluid.core as core from paddle.fluid.core import AnalysisConfig, PassVersionChecker @@ -40,7 +41,7 @@ class TRTFlattenTest(InferencePassTest): self.fetch_list = [out] def append_flatten(self, data): - return fluid.layers.flatten(data, axis=1) + return paddle.flatten(data, 1, -1) def test_check_output(self): if core.is_compiled_with_cuda(): @@ -75,7 +76,7 @@ class TRTFlattenDynamicTest(InferencePassTest): self.fetch_list = [out] def append_flatten(self, data): - return fluid.layers.flatten(data, axis=1) + return paddle.flatten(data, 1, -1) def test_check_output(self): if core.is_compiled_with_cuda(): diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_transpose_flatten_concat_fuse_pass.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_transpose_flatten_concat_fuse_pass.py index 8e0fa3a0eba2d7da4b4d81f177795630d065d8cd..192274ef34106e4958db0f1e61619df473e7df6e 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_transpose_flatten_concat_fuse_pass.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_transpose_flatten_concat_fuse_pass.py @@ -32,10 +32,12 @@ class TransposeFlattenConcatFusePassTRTTest(InferencePassTest): data2 = fluid.data( name="data2", shape=[8, 32, 128], dtype="float32" ) + trans1 = paddle.transpose(data1, perm=[0, 2, 1]) trans2 = paddle.transpose(data2, perm=[0, 2, 1]) - flatt1 = fluid.layers.flatten(trans1) - flatt2 = fluid.layers.flatten(trans2) + flatt1 = paddle.flatten(trans1, 1, -1) + flatt2 = paddle.flatten(trans2, 1, -1) + concat_out = fluid.layers.concat([flatt1, flatt2], axis=1) # There is no parameters for above structure. # Hence, append a batch_norm to avoid failure caused by load_combined. diff --git a/python/paddle/fluid/tests/unittests/mlu/test_flatten2_op_mlu.py b/python/paddle/fluid/tests/unittests/mlu/test_flatten2_op_mlu.py index ace458ff42c0576d73ec02b588665a4864df44b9..c6ae69b2d57e0c031fad82e9316d99b3d019f023 100644 --- a/python/paddle/fluid/tests/unittests/mlu/test_flatten2_op_mlu.py +++ b/python/paddle/fluid/tests/unittests/mlu/test_flatten2_op_mlu.py @@ -75,41 +75,5 @@ class TestFlattenOpSixDims(TestFlattenOp): self.new_shape = (36, 16) -class TestStaticFlattenInferShapePythonAPI(unittest.TestCase): - def execute_api(self, x, axis=1): - return fluid.layers.flatten(x, axis=axis) - - def test_static_api(self): - paddle.enable_static() - main_prog = paddle.static.Program() - with paddle.static.program_guard(main_prog, paddle.static.Program()): - x = paddle.static.data( - name="x", shape=[-1, 3, -1, -1], dtype='float32' - ) - out = self.execute_api(x, axis=2) - self.assertTrue((-1, -1) == out.shape) - - -class TestFlatten2OpError(unittest.TestCase): - def test_errors(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): - input_data = np.random.random((3, 2, 4, 5)).astype("float64") - - def test_Variable(): - # the input type must be Variable - fluid.layers.flatten(input_data, axis=1) - - self.assertRaises(TypeError, test_Variable) - - def test_type(): - # dtype must be float32, float64, int8, int32, int64, uint8. - x2 = fluid.layers.data( - name='x2', shape=[3, 2, 4, 5], dtype='float16' - ) - fluid.layers.flatten(x2, axis=1) - - self.assertRaises(TypeError, test_type) - - if __name__ == "__main__": unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_flatten2_op.py b/python/paddle/fluid/tests/unittests/test_flatten2_op.py index fe5aad118af46e5792a110d7ce5c4d7b94f10d84..d67b873084d5a02b8237dc22a641639a3dd7776d 100644 --- a/python/paddle/fluid/tests/unittests/test_flatten2_op.py +++ b/python/paddle/fluid/tests/unittests/test_flatten2_op.py @@ -14,8 +14,6 @@ import unittest import numpy as np -import paddle.fluid as fluid -import paddle from op_test import OpTest @@ -68,41 +66,5 @@ class TestFlattenOpSixDims(TestFlattenOp): self.new_shape = (36, 16) -class TestStaticFlattenInferShapePythonAPI(unittest.TestCase): - def execute_api(self, x, axis=1): - return fluid.layers.flatten(x, axis=axis) - - def test_static_api(self): - paddle.enable_static() - main_prog = paddle.static.Program() - with paddle.static.program_guard(main_prog, paddle.static.Program()): - x = paddle.static.data( - name="x", shape=[-1, 3, -1, -1], dtype='float32' - ) - out = self.execute_api(x, axis=2) - self.assertTrue((-1, -1) == out.shape) - - -class TestFlatten2OpError(unittest.TestCase): - def test_errors(self): - with fluid.program_guard(fluid.Program(), fluid.Program()): - input_data = np.random.random((3, 2, 4, 5)).astype("float64") - - def test_Variable(): - # the input type must be Variable - fluid.layers.flatten(input_data, axis=1) - - self.assertRaises(TypeError, test_Variable) - - def test_type(): - # dtype must be float32, float64, int8, int32, int64, uint8. - x2 = fluid.layers.data( - name='x2', shape=[3, 2, 4, 5], dtype='float16' - ) - fluid.layers.flatten(x2, axis=1) - - self.assertRaises(TypeError, test_type) - - if __name__ == "__main__": unittest.main() diff --git a/python/paddle/fluid/tests/unittests/test_imperative_layer_apply.py b/python/paddle/fluid/tests/unittests/test_imperative_layer_apply.py index 88b91e76b112a0ac7049693b8c8abbb3554ce2b0..156cac0b3a3243d42f13aab2e6fbaf34fbe38004 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_layer_apply.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_layer_apply.py @@ -47,7 +47,7 @@ class LeNetDygraph(fluid.dygraph.Layer): x = self.features(inputs) if self.num_classes > 0: - x = fluid.layers.flatten(x, 1) + x = paddle.flatten(x, 1, -1) x = self.fc(x) return x diff --git a/python/paddle/fluid/tests/unittests/test_layers.py b/python/paddle/fluid/tests/unittests/test_layers.py index 22b4e22061b9be8133be498c0f23ced930e1411c..aff461f8786c9634b2cddd5a064a5a53f5d3bd24 100644 --- a/python/paddle/fluid/tests/unittests/test_layers.py +++ b/python/paddle/fluid/tests/unittests/test_layers.py @@ -4129,7 +4129,7 @@ class TestBook(LayerTest): shape=[4, 4, 3], dtype="float32", ) - out = layers.flatten(x, axis=1, name="flatten") + out = paddle.flatten(x, 1, -1, name="flatten") return out def test_linspace(self): diff --git a/python/paddle/tests/test_model.py b/python/paddle/tests/test_model.py index 91ffb3377d1082dfc8dd80f15f50beb3246ca596..58c88e56482fa3188b56f70c97b757913cc8b72b 100644 --- a/python/paddle/tests/test_model.py +++ b/python/paddle/tests/test_model.py @@ -61,7 +61,7 @@ class LeNetDygraph(paddle.nn.Layer): x = self.features(inputs) if self.num_classes > 0: - x = fluid.layers.flatten(x, 1) + x = paddle.flatten(x, 1, -1) x = self.fc(x) return x