未验证 提交 b6b6fca6 编写于 作者: V Vvsmile 提交者: GitHub

[Clean Fluid API]Remove API: gather_nd (#47956)

* Replace paddle.fluid.layers.gather_nd with paddle.gather_nd

* replace gather_nd with paddle.gather_nd

* fix the call of gather_nd

* fix code style of gather_nd
上级 105bb929
......@@ -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.
......
......@@ -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(
......
......@@ -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])
......
......@@ -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)
......
......@@ -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')
......
......@@ -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)
......
......@@ -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):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册