diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index fcafa992beb84736f0cef767afe53325a511c63b..91a5376abb0f3415a38866394d8393a537194757 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -101,7 +101,6 @@ __all__ = [ 'resize_bilinear', 'resize_trilinear', 'resize_nearest', - 'gather_nd', 'relu', 'log', 'prelu', @@ -6017,104 +6016,6 @@ def resize_nearest( ) -@deprecated(since="2.0.0", update_to="paddle.gather_nd") -def gather_nd(input, index, name=None): - """ - **Gather Nd Layer** - - This function is actually a high-dimensional extension of :code:`gather` - and supports for simultaneous indexing by multiple axes. :attr:`index` is a - K-dimensional integer tensor, which is regarded as a (K-1)-dimensional - tensor of :attr:`index` into :attr:`input`, where each element defines - a slice of params: - - .. math:: - - output[(i_0, ..., i_{K-2})] = input[index[(i_0, ..., i_{K-2})]] - - Obviously, :code:`index.shape[-1] <= input.rank` . And, the output tensor has - shape :code:`index.shape[:-1] + input.shape[index.shape[-1]:]` . - - .. code-block:: text - - Given: - input = [[[ 0, 1, 2, 3], - [ 4, 5, 6, 7], - [ 8, 9, 10, 11]], - [[12, 13, 14, 15], - [16, 17, 18, 19], - [20, 21, 22, 23]]] - input.shape = (2, 3, 4) - - * Case 1: - index = [[1]] - - gather_nd(input, index) - = [input[1, :, :]] - = [[12, 13, 14, 15], - [16, 17, 18, 19], - [20, 21, 22, 23]] - - * Case 2: - index = [[0,2]] - - gather_nd(input, index) - = [input[0, 2, :]] - = [8, 9, 10, 11] - - * Case 3: - index = [[1, 2, 3]] - - gather_nd(input, index) - = [input[1, 2, 3]] - = [23] - - Args: - input (Tensor): The input Tensor which it's data type should be bool, float32, float64, int32, int64. - index (Tensor): The index input with rank > 1, index.shape[-1] <= input.rank. - Its dtype should be int32, int64. - name(str, optional): The default value is None. Normally there is no need for user to set this property. - For more information, please refer to :ref:`api_guide_Name` . - - Returns: - output (Tensor): A tensor with the shape index.shape[:-1] + input.shape[index.shape[-1]:] - - Examples: - - .. code-block:: python - - import paddle - import paddle.fluid as fluid - paddle.enable_static() - - x = fluid.data(name='x', shape=[3, 4, 5], dtype='float32') - index = fluid.data(name='index', shape=[2, 2], dtype='int32') - output = fluid.layers.gather_nd(x, index) - - """ - if in_dygraph_mode(): - return _C_ops.gather_nd(input, index) - else: - if _in_legacy_dygraph(): - return _legacy_C_ops.gather_nd(input, index) - check_variable_and_dtype( - input, - 'input', - ['bool', 'float32', 'float64', 'int16', 'int32', 'int64'], - 'gather_np', - ) - check_variable_and_dtype(index, 'index', ['int32', 'int64'], 'gather_np') - helper = LayerHelper('gather_nd', **locals()) - dtype = helper.input_dtype() - output = helper.create_variable_for_type_inference(dtype) - helper.append_op( - type="gather_nd", - inputs={"X": input, "Index": index}, - outputs={"Out": output}, - ) - return output - - def log(x, name=None): r""" Calculates the natural log of the given input tensor, element-wise. diff --git a/python/paddle/fluid/layers/rnn.py b/python/paddle/fluid/layers/rnn.py index 024eb4208ecc24ad77f0ebf941bc634f41fd8d46..b82a965e84fc87240dd502e4dbce7808bec2cb29 100644 --- a/python/paddle/fluid/layers/rnn.py +++ b/python/paddle/fluid/layers/rnn.py @@ -1167,7 +1167,7 @@ class BeamSearchDecoder(Decoder): ) topk_coordinates = paddle.stack([batch_pos, indices], axis=2) topk_coordinates.stop_gradient = True - return nn.gather_nd(x, topk_coordinates) + return paddle.gather_nd(x, topk_coordinates) class OutputWrapper( collections.namedtuple( diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/seq2seq_dygraph_model.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/seq2seq_dygraph_model.py index da9214c466fa36f11521b9245451e22dd7e05ff7..aa0219880a073c6371d937f040033dd520282f56 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/seq2seq_dygraph_model.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/seq2seq_dygraph_model.py @@ -199,7 +199,7 @@ class BaseModel(fluid.dygraph.Layer): def _gather(self, x, indices, batch_pos): topk_coordinates = paddle.stack([batch_pos, indices], axis=2) - return fluid.layers.gather_nd(x, topk_coordinates) + return paddle.gather_nd(x, topk_coordinates) @declarative def forward(self, inputs): @@ -684,7 +684,7 @@ class AttentionModel(fluid.dygraph.Layer): def _gather(self, x, indices, batch_pos): topk_coordinates = paddle.stack([batch_pos, indices], axis=2) - return fluid.layers.gather_nd(x, topk_coordinates) + return paddle.gather_nd(x, topk_coordinates) def attention(self, query, enc_output, mask=None): query = fluid.layers.unsqueeze(query, [1]) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/transformer_dygraph_model.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/transformer_dygraph_model.py index 296d221c415c2d193b956a51b70a769346fe56ac..bcd881c7996024bb82f0f5900fcbe79d499cfb46 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/transformer_dygraph_model.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/transformer_dygraph_model.py @@ -768,7 +768,7 @@ class Transformer(Layer): def gather(input, indices, batch_pos): topk_coordinates = paddle.stack([batch_pos, indices], axis=2) - return layers.gather_nd(input, topk_coordinates) + return paddle.gather_nd(input, topk_coordinates) # run encoder enc_output = self.encoder(src_word, src_pos, src_slf_attn_bias) diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_gather_nd_op.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_gather_nd_op.py index 3a193e496c05a1a5edabf15992db5a23bd31af37..b96eddb87e779f22e14888d07b222d86c64acb55 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_gather_nd_op.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_gather_nd_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 @@ -27,7 +28,7 @@ class TRTGatherNdTest(InferencePassTest): with fluid.program_guard(self.main_program, self.startup_program): data = fluid.data(name="data", shape=[-1, 3, 4], dtype="float32") index = fluid.data(name="index", shape=[-1, 2, 2], dtype="int32") - gather_nd = fluid.layers.gather_nd(data, index) + gather_nd = paddle.gather_nd(data, index) out = fluid.layers.batch_norm(gather_nd, is_test=True) self.feeds = { @@ -64,7 +65,7 @@ class TRTGatherNdFp16Test(InferencePassTest): name="data", shape=[-1, 1280, 192], dtype="float32" ) index = fluid.data(name="index", shape=[-1, 1028, 2], dtype="int32") - gather_nd = fluid.layers.gather_nd(data, index) + gather_nd = paddle.gather_nd(data, index) out = fluid.layers.batch_norm(gather_nd, is_test=True) index_data = np.zeros((1, 1028, 2), dtype='int32') diff --git a/python/paddle/fluid/tests/unittests/test_dynamic_rnn_stop_gradient.py b/python/paddle/fluid/tests/unittests/test_dynamic_rnn_stop_gradient.py index d20ad3d0c0ec673bcfa39e52e791bb4a82fe0cb6..fff63bcb0057eecf5f0d0d2cd427abcb03eb1ab7 100644 --- a/python/paddle/fluid/tests/unittests/test_dynamic_rnn_stop_gradient.py +++ b/python/paddle/fluid/tests/unittests/test_dynamic_rnn_stop_gradient.py @@ -49,7 +49,7 @@ def build_and_run_program(place, batch_size, beam_size, stop_gradient=False): ) topk_coordinates = paddle.stack([batch_pos, indices], axis=2) topk_coordinates.stop_gradient = stop_gradient - score = layers.gather_nd(x, topk_coordinates) + score = paddle.gather_nd(x, topk_coordinates) layers.increment(x=step_idx, value=1.0, in_place=True) layers.array_write(score, i=step_idx, array=scores) length_cond = layers.less_than(x=step_idx, y=max_len) 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 c49db2815ac1e6d5fec8a26f1409cbcc97b55803..20fc57ff023685f34ba095971cdd2740996c9e32 100644 --- a/python/paddle/fluid/tests/unittests/test_gather_nd_op.py +++ b/python/paddle/fluid/tests/unittests/test_gather_nd_op.py @@ -162,17 +162,17 @@ class TestGatherNdOpAPI(unittest.TestCase): name='x1', shape=[30, 40, 50, 60], dtype='float32' ) index1 = fluid.layers.data(name='index1', shape=[2, 4], dtype='int32') - output1 = fluid.layers.gather_nd(x1, index1) + output1 = paddle.gather_nd(x1, index1) def test_case2(self): x2 = fluid.layers.data(name='x2', shape=[30, 40, 50], dtype='float32') index2 = fluid.layers.data(name='index2', shape=[2, 2], dtype='int64') - output2 = fluid.layers.gather_nd(x2, index2) + output2 = paddle.gather_nd(x2, index2) def test_case3(self): x3 = fluid.layers.data(name='x3', shape=[3, 4, 5], dtype='float32') index3 = fluid.layers.data(name='index3', shape=[2, 1], dtype='int32') - output3 = fluid.layers.gather_nd(x3, index3, name="gather_nd_layer") + output3 = paddle.gather_nd(x3, index3, name="gather_nd_layer") # Test Raise Index Error @@ -186,7 +186,7 @@ class TestGatherNdOpRaise(unittest.TestCase): index = fluid.layers.data( name='index', shape=[2, 10], dtype='int32' ) - output = fluid.layers.gather_nd(x, index) + output = paddle.gather_nd(x, index) except Exception as e: t = "Input(Index).shape[-1] should be no greater than Input(X).rank" if t in str(e):