From 844ab6fe1d5eb5d214cfc090817c43c3aa2eee1e Mon Sep 17 00:00:00 2001 From: Vvsmile <450864116@qq.com> Date: Mon, 21 Nov 2022 12:42:03 +0800 Subject: [PATCH] [Clean Fluid API]Remove API: gather (#47954) * Remove API: gather replace the paddle.fluid.layers.gather with paddle.gather * modify the call of gather from old style to new style --- python/paddle/fluid/layers/detection.py | 8 +- python/paddle/fluid/layers/nn.py | 76 ------------------- .../dygraph_to_static/bert_dygraph_model.py | 2 +- .../unittests/ipu/test_fp16_support_ipu.py | 2 +- .../tests/unittests/ipu/test_gather_op_ipu.py | 2 +- .../ir/inference/test_trt_gather_op.py | 5 +- .../unittests/mlu/test_gather_nd_op_mlu.py | 2 +- .../tests/unittests/mlu/test_gather_op_mlu.py | 6 +- .../unittests/npu/test_gather_nd_op_npu.py | 2 +- .../tests/unittests/npu/test_gather_op_npu.py | 2 +- .../tests/unittests/test_gather_nd_op.py | 2 +- .../fluid/tests/unittests/test_gather_op.py | 8 +- .../tests/unittests/test_imperative_deepcf.py | 4 +- 13 files changed, 23 insertions(+), 98 deletions(-) diff --git a/python/paddle/fluid/layers/detection.py b/python/paddle/fluid/layers/detection.py index 097875a75f2..1f5c0273e59 100644 --- a/python/paddle/fluid/layers/detection.py +++ b/python/paddle/fluid/layers/detection.py @@ -332,8 +332,8 @@ def retinanet_target_assign( cls_logits = nn.reshape(x=cls_logits, shape=(-1, num_classes)) bbox_pred = nn.reshape(x=bbox_pred, shape=(-1, 4)) - predicted_cls_logits = nn.gather(cls_logits, score_index) - predicted_bbox_pred = nn.gather(bbox_pred, loc_index) + predicted_cls_logits = paddle.gather(cls_logits, score_index) + predicted_bbox_pred = paddle.gather(bbox_pred, loc_index) return ( predicted_cls_logits, @@ -514,8 +514,8 @@ def rpn_target_assign( cls_logits = nn.reshape(x=cls_logits, shape=(-1, 1)) bbox_pred = nn.reshape(x=bbox_pred, shape=(-1, 4)) - predicted_cls_logits = nn.gather(cls_logits, score_index) - predicted_bbox_pred = nn.gather(bbox_pred, loc_index) + predicted_cls_logits = paddle.gather(cls_logits, score_index) + predicted_bbox_pred = paddle.gather(bbox_pred, loc_index) return ( predicted_cls_logits, diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 50602825c1d..0ccd6ea0074 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -117,7 +117,6 @@ __all__ = [ 'resize_bilinear', 'resize_trilinear', 'resize_nearest', - 'gather', 'gather_nd', 'scatter', 'scatter_nd_add', @@ -8612,81 +8611,6 @@ def image_resize_short(input, out_short_len, resample='BILINEAR'): return image_resize(input=input, out_shape=out_shape, resample=resample) -@deprecated(since="2.0.0", update_to="paddle.gather") -def gather(input, index, overwrite=True): - """ - - Output is obtained by gathering entries of the outer-most dimension - of X indexed by `index` and concatenate them together. - - .. math:: - - Out = X[Index] - - - .. code-block:: text - - - Given: - - X = [[1, 2], - [3, 4], - [5, 6]] - - Index = [1, 2] - - Then: - - Out = [[3, 4], - [5, 6]] - - Args: - input (Tensor): The source input tensor with rank>=1. Supported data type is - int32, int64, float32, float64 and uint8 (only for CPU), - float16 (only for GPU). - index (Tensor): The index input tensor with rank=1. Data type is int32 or int64. - overwrite (bool, optional): The mode that updating the grad when has same index. - If True, use the overwrite mode to update the grad of the same index, - if False, use the accumulate mode to update the grad of the same index. - Default value is True. - - Returns: - output (Tensor): The output is a tensor with the same rank as input. - - Examples: - - .. code-block:: python - - import paddle - import paddle.fluid as fluid - paddle.enable_static() - - x = fluid.data(name='x', shape=[-1, 5], dtype='float32') - index = fluid.data(name='index', shape=[-1, 1], dtype='int32') - output = fluid.layers.gather(x, index) - """ - if _non_static_mode(): - return _legacy_C_ops.gather(input, index, None, 'overwrite', overwrite) - - check_variable_and_dtype( - input, - 'x', - ['float16', 'float32', 'float64', 'int32', 'int64', 'uint8'], - 'gather', - ) - check_variable_and_dtype(index, 'index', ['int32', 'int64'], 'gather') - helper = LayerHelper('gather', **locals()) - dtype = helper.input_dtype() - out = helper.create_variable_for_type_inference(dtype) - helper.append_op( - type="gather", - inputs={"X": input, "Index": index}, - outputs={"Out": out}, - attrs={'overwrite': overwrite}, - ) - return out - - @deprecated(since="2.0.0", update_to="paddle.gather_nd") def gather_nd(input, index, name=None): """ diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/bert_dygraph_model.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/bert_dygraph_model.py index e5b85be96b8..721bc912216 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/bert_dygraph_model.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/bert_dygraph_model.py @@ -395,7 +395,7 @@ class PretrainModelLayer(Layer): x=enc_output, shape=[-1, self._emb_size] ) - mask_feat = fluid.layers.gather(input=reshaped_emb_out, index=mask_pos) + mask_feat = paddle.gather(reshaped_emb_out, index=mask_pos) mask_trans_feat = self.pooled_fc(mask_feat) mask_trans_feat = self.pre_process_layer(mask_trans_feat) diff --git a/python/paddle/fluid/tests/unittests/ipu/test_fp16_support_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_fp16_support_ipu.py index 8433e45f46c..c0c6cb20735 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_fp16_support_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_fp16_support_ipu.py @@ -97,7 +97,7 @@ class TestIntInput(TestBase): y = paddle.static.data( name=self.feed_list[1], shape=self.feed_shape[1], dtype='int32' ) - out = paddle.fluid.layers.gather(x, index=y) + out = paddle.gather(x, index=y) self.fetch_list = [out.name] diff --git a/python/paddle/fluid/tests/unittests/ipu/test_gather_op_ipu.py b/python/paddle/fluid/tests/unittests/ipu/test_gather_op_ipu.py index 5913db81f31..4f8a62e5c4e 100644 --- a/python/paddle/fluid/tests/unittests/ipu/test_gather_op_ipu.py +++ b/python/paddle/fluid/tests/unittests/ipu/test_gather_op_ipu.py @@ -49,7 +49,7 @@ class TestBase(IPUOpTest): y = paddle.static.data( name=self.feed_list[1], shape=self.feed_shape[1], dtype='int32' ) - out = paddle.fluid.layers.gather(x, index=y, **self.attrs) + out = paddle.gather(x, index=y, **self.attrs) self.fetch_list = [out.name] def run_model(self, exec_mode): diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_gather_op.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_gather_op.py index 72c62ff4127..4930fd12c85 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_gather_op.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_gather_op.py @@ -19,6 +19,7 @@ import paddle.fluid as fluid import paddle.fluid.core as core from paddle.fluid.core import PassVersionChecker from paddle.fluid.core import AnalysisConfig +import paddle class TRTGatherTest1(InferencePassTest): @@ -27,7 +28,7 @@ class TRTGatherTest1(InferencePassTest): with fluid.program_guard(self.main_program, self.startup_program): data = fluid.data(name='data', shape=[-1, 128], dtype='float32') index = fluid.data(name='index', shape=[-1, 1], dtype='int32') - scale_out = fluid.layers.gather(data, index=index) + scale_out = paddle.gather(data, index=index) out = fluid.layers.softmax(input=scale_out) self.feeds = { @@ -66,7 +67,7 @@ class TRTGatherTest2(InferencePassTest): with fluid.program_guard(self.main_program, self.startup_program): data = fluid.data(name='data', shape=[16, 64], dtype='float32') index = fluid.data(name='index', shape=[2], dtype='int32') - scale_out = fluid.layers.gather(data, index=index) + scale_out = paddle.gather(data, index=index) out = fluid.layers.softmax(input=scale_out) self.feeds = { diff --git a/python/paddle/fluid/tests/unittests/mlu/test_gather_nd_op_mlu.py b/python/paddle/fluid/tests/unittests/mlu/test_gather_nd_op_mlu.py index 3fb5634df45..bcec54413a3 100644 --- a/python/paddle/fluid/tests/unittests/mlu/test_gather_nd_op_mlu.py +++ b/python/paddle/fluid/tests/unittests/mlu/test_gather_nd_op_mlu.py @@ -281,7 +281,7 @@ class TestGatherNdAPI2(unittest.TestCase): index_1 = np.array([[1]]).astype("int32") input = fluid.dygraph.to_variable(input_1) index = fluid.dygraph.to_variable(index_1) - output = paddle.fluid.layers.gather(input, index) + output = paddle.gather(input, index) output_np = output.numpy() expected_output = np.array([3, 4]) np.testing.assert_allclose(output_np[0], expected_output, rtol=1e-6) diff --git a/python/paddle/fluid/tests/unittests/mlu/test_gather_op_mlu.py b/python/paddle/fluid/tests/unittests/mlu/test_gather_op_mlu.py index 2f50c10a62f..d4c5e966570 100644 --- a/python/paddle/fluid/tests/unittests/mlu/test_gather_op_mlu.py +++ b/python/paddle/fluid/tests/unittests/mlu/test_gather_op_mlu.py @@ -92,7 +92,7 @@ class API_TestDygraphGather(unittest.TestCase): index_1 = np.array([1, 2]) input = paddle.to_tensor(input_1) index = paddle.to_tensor(index_1) - output = paddle.fluid.layers.gather(input, index) + output = paddle.gather(input, index) output_np = output.numpy() expected_output = np.array([[3, 4], [5, 6]]).astype('int32') np.testing.assert_allclose(output_np, expected_output) @@ -167,12 +167,12 @@ class TestGathertError(unittest.TestCase): ) def test_x_type(): - paddle.fluid.layers.gather(x, index) + paddle.gather(x, index) self.assertRaises(TypeError, test_x_type) def test_index_type(): - paddle.fluid.layers.gather(x, index_float) + paddle.gather(x, index_float) self.assertRaises(TypeError, test_index_type) diff --git a/python/paddle/fluid/tests/unittests/npu/test_gather_nd_op_npu.py b/python/paddle/fluid/tests/unittests/npu/test_gather_nd_op_npu.py index 4d66c28d6fa..cea2e4ff37d 100644 --- a/python/paddle/fluid/tests/unittests/npu/test_gather_nd_op_npu.py +++ b/python/paddle/fluid/tests/unittests/npu/test_gather_nd_op_npu.py @@ -270,7 +270,7 @@ class TestGatherNdAPI(unittest.TestCase): index_1 = np.array([[1]]) input = fluid.dygraph.to_variable(input_1) index = fluid.dygraph.to_variable(index_1) - output = paddle.fluid.layers.gather(input, index) + output = paddle.gather(input, index) output_np = output.numpy() expected_output = np.array([3, 4]) np.testing.assert_allclose(output_np[0], expected_output) diff --git a/python/paddle/fluid/tests/unittests/npu/test_gather_op_npu.py b/python/paddle/fluid/tests/unittests/npu/test_gather_op_npu.py index 5e783b952af..1d27eadbc12 100644 --- a/python/paddle/fluid/tests/unittests/npu/test_gather_op_npu.py +++ b/python/paddle/fluid/tests/unittests/npu/test_gather_op_npu.py @@ -86,7 +86,7 @@ class API_TestGather(unittest.TestCase): with fluid.program_guard(fluid.Program(), fluid.Program()): data1 = fluid.layers.data('data1', shape=[-1, 2], dtype='float32') index = fluid.layers.data('index', shape=[-1, 1], dtype='int32') - out = paddle.fluid.layers.gather(data1, index) + out = paddle.gather(data1, index) place = paddle.NPUPlace(0) exe = fluid.Executor(place) input = np.array([[1, 2], [3, 4], [5, 6]]) diff --git a/python/paddle/fluid/tests/unittests/test_gather_nd_op.py b/python/paddle/fluid/tests/unittests/test_gather_nd_op.py index 176012e96a2..c49db2815ac 100644 --- a/python/paddle/fluid/tests/unittests/test_gather_nd_op.py +++ b/python/paddle/fluid/tests/unittests/test_gather_nd_op.py @@ -248,7 +248,7 @@ class TestGatherNdAPI2(unittest.TestCase): index_1 = np.array([[1]]) input = fluid.dygraph.to_variable(input_1) index = fluid.dygraph.to_variable(index_1) - output = paddle.fluid.layers.gather(input, index) + output = paddle.gather(input, index) output_np = output.numpy() expected_output = np.array([[3, 4]]) np.testing.assert_allclose(output_np, expected_output, rtol=1e-05) diff --git a/python/paddle/fluid/tests/unittests/test_gather_op.py b/python/paddle/fluid/tests/unittests/test_gather_op.py index c6301d3e472..4f722c8bdc2 100644 --- a/python/paddle/fluid/tests/unittests/test_gather_op.py +++ b/python/paddle/fluid/tests/unittests/test_gather_op.py @@ -225,7 +225,7 @@ class API_TestGather(unittest.TestCase): with fluid.program_guard(fluid.Program(), fluid.Program()): data1 = fluid.layers.data('data1', shape=[-1, 2], dtype='float64') index = fluid.layers.data('index', shape=[-1, 1], dtype='int32') - out = paddle.fluid.layers.gather(data1, index) + out = paddle.gather(data1, index) place = fluid.CPUPlace() exe = fluid.Executor(place) input = np.array([[1, 2], [3, 4], [5, 6]]) @@ -264,7 +264,7 @@ class API_TestDygraphGather(unittest.TestCase): index_1 = np.array([1, 2]) input = paddle.to_tensor(input_1) index = paddle.to_tensor(index_1) - output = paddle.fluid.layers.gather(input, index) + output = paddle.gather(input, index) output_np = output.numpy() expected_output = np.array([[3, 4], [5, 6]]) np.testing.assert_allclose(output_np, expected_output, rtol=1e-05) @@ -372,12 +372,12 @@ class TestGathertError(unittest.TestCase): ) def test_x_type(): - paddle.fluid.layers.gather(x, index) + paddle.gather(x, index) self.assertRaises(TypeError, test_x_type) def test_index_type(): - paddle.fluid.layers.gather(x, index_float) + paddle.gather(x, index_float) self.assertRaises(TypeError, test_index_type) diff --git a/python/paddle/fluid/tests/unittests/test_imperative_deepcf.py b/python/paddle/fluid/tests/unittests/test_imperative_deepcf.py index 8c307f57da3..2b0291b601e 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_deepcf.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_deepcf.py @@ -119,8 +119,8 @@ class DeepCF(fluid.Layer): def forward(self, users, items): # users_emb = self._user_emb(users) # items_emb = self._item_emb(items) - users_emb = fluid.layers.gather(self._rating_matrix, users) - items_emb = fluid.layers.gather( + users_emb = paddle.gather(self._rating_matrix, users) + items_emb = paddle.gather( fluid.layers.transpose(self._rating_matrix, [1, 0]), items ) users_emb.stop_gradient = True -- GitLab