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

[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
上级 1ba308f5
...@@ -332,8 +332,8 @@ def retinanet_target_assign( ...@@ -332,8 +332,8 @@ def retinanet_target_assign(
cls_logits = nn.reshape(x=cls_logits, shape=(-1, num_classes)) cls_logits = nn.reshape(x=cls_logits, shape=(-1, num_classes))
bbox_pred = nn.reshape(x=bbox_pred, shape=(-1, 4)) bbox_pred = nn.reshape(x=bbox_pred, shape=(-1, 4))
predicted_cls_logits = nn.gather(cls_logits, score_index) predicted_cls_logits = paddle.gather(cls_logits, score_index)
predicted_bbox_pred = nn.gather(bbox_pred, loc_index) predicted_bbox_pred = paddle.gather(bbox_pred, loc_index)
return ( return (
predicted_cls_logits, predicted_cls_logits,
...@@ -514,8 +514,8 @@ def rpn_target_assign( ...@@ -514,8 +514,8 @@ def rpn_target_assign(
cls_logits = nn.reshape(x=cls_logits, shape=(-1, 1)) cls_logits = nn.reshape(x=cls_logits, shape=(-1, 1))
bbox_pred = nn.reshape(x=bbox_pred, shape=(-1, 4)) bbox_pred = nn.reshape(x=bbox_pred, shape=(-1, 4))
predicted_cls_logits = nn.gather(cls_logits, score_index) predicted_cls_logits = paddle.gather(cls_logits, score_index)
predicted_bbox_pred = nn.gather(bbox_pred, loc_index) predicted_bbox_pred = paddle.gather(bbox_pred, loc_index)
return ( return (
predicted_cls_logits, predicted_cls_logits,
......
...@@ -117,7 +117,6 @@ __all__ = [ ...@@ -117,7 +117,6 @@ __all__ = [
'resize_bilinear', 'resize_bilinear',
'resize_trilinear', 'resize_trilinear',
'resize_nearest', 'resize_nearest',
'gather',
'gather_nd', 'gather_nd',
'scatter', 'scatter',
'scatter_nd_add', 'scatter_nd_add',
...@@ -8612,81 +8611,6 @@ def image_resize_short(input, out_short_len, resample='BILINEAR'): ...@@ -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) 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") @deprecated(since="2.0.0", update_to="paddle.gather_nd")
def gather_nd(input, index, name=None): def gather_nd(input, index, name=None):
""" """
......
...@@ -395,7 +395,7 @@ class PretrainModelLayer(Layer): ...@@ -395,7 +395,7 @@ class PretrainModelLayer(Layer):
x=enc_output, shape=[-1, self._emb_size] 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.pooled_fc(mask_feat)
mask_trans_feat = self.pre_process_layer(mask_trans_feat) mask_trans_feat = self.pre_process_layer(mask_trans_feat)
......
...@@ -97,7 +97,7 @@ class TestIntInput(TestBase): ...@@ -97,7 +97,7 @@ class TestIntInput(TestBase):
y = paddle.static.data( y = paddle.static.data(
name=self.feed_list[1], shape=self.feed_shape[1], dtype='int32' 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] self.fetch_list = [out.name]
......
...@@ -49,7 +49,7 @@ class TestBase(IPUOpTest): ...@@ -49,7 +49,7 @@ class TestBase(IPUOpTest):
y = paddle.static.data( y = paddle.static.data(
name=self.feed_list[1], shape=self.feed_shape[1], dtype='int32' 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] self.fetch_list = [out.name]
def run_model(self, exec_mode): def run_model(self, exec_mode):
......
...@@ -19,6 +19,7 @@ import paddle.fluid as fluid ...@@ -19,6 +19,7 @@ import paddle.fluid as fluid
import paddle.fluid.core as core import paddle.fluid.core as core
from paddle.fluid.core import PassVersionChecker from paddle.fluid.core import PassVersionChecker
from paddle.fluid.core import AnalysisConfig from paddle.fluid.core import AnalysisConfig
import paddle
class TRTGatherTest1(InferencePassTest): class TRTGatherTest1(InferencePassTest):
...@@ -27,7 +28,7 @@ class TRTGatherTest1(InferencePassTest): ...@@ -27,7 +28,7 @@ class TRTGatherTest1(InferencePassTest):
with fluid.program_guard(self.main_program, self.startup_program): with fluid.program_guard(self.main_program, self.startup_program):
data = fluid.data(name='data', shape=[-1, 128], dtype='float32') data = fluid.data(name='data', shape=[-1, 128], dtype='float32')
index = fluid.data(name='index', shape=[-1, 1], dtype='int32') 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) out = fluid.layers.softmax(input=scale_out)
self.feeds = { self.feeds = {
...@@ -66,7 +67,7 @@ class TRTGatherTest2(InferencePassTest): ...@@ -66,7 +67,7 @@ class TRTGatherTest2(InferencePassTest):
with fluid.program_guard(self.main_program, self.startup_program): with fluid.program_guard(self.main_program, self.startup_program):
data = fluid.data(name='data', shape=[16, 64], dtype='float32') data = fluid.data(name='data', shape=[16, 64], dtype='float32')
index = fluid.data(name='index', shape=[2], dtype='int32') 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) out = fluid.layers.softmax(input=scale_out)
self.feeds = { self.feeds = {
......
...@@ -281,7 +281,7 @@ class TestGatherNdAPI2(unittest.TestCase): ...@@ -281,7 +281,7 @@ class TestGatherNdAPI2(unittest.TestCase):
index_1 = np.array([[1]]).astype("int32") index_1 = np.array([[1]]).astype("int32")
input = fluid.dygraph.to_variable(input_1) input = fluid.dygraph.to_variable(input_1)
index = fluid.dygraph.to_variable(index_1) index = fluid.dygraph.to_variable(index_1)
output = paddle.fluid.layers.gather(input, index) output = paddle.gather(input, index)
output_np = output.numpy() output_np = output.numpy()
expected_output = np.array([3, 4]) expected_output = np.array([3, 4])
np.testing.assert_allclose(output_np[0], expected_output, rtol=1e-6) np.testing.assert_allclose(output_np[0], expected_output, rtol=1e-6)
......
...@@ -92,7 +92,7 @@ class API_TestDygraphGather(unittest.TestCase): ...@@ -92,7 +92,7 @@ class API_TestDygraphGather(unittest.TestCase):
index_1 = np.array([1, 2]) index_1 = np.array([1, 2])
input = paddle.to_tensor(input_1) input = paddle.to_tensor(input_1)
index = paddle.to_tensor(index_1) index = paddle.to_tensor(index_1)
output = paddle.fluid.layers.gather(input, index) output = paddle.gather(input, index)
output_np = output.numpy() output_np = output.numpy()
expected_output = np.array([[3, 4], [5, 6]]).astype('int32') expected_output = np.array([[3, 4], [5, 6]]).astype('int32')
np.testing.assert_allclose(output_np, expected_output) np.testing.assert_allclose(output_np, expected_output)
...@@ -167,12 +167,12 @@ class TestGathertError(unittest.TestCase): ...@@ -167,12 +167,12 @@ class TestGathertError(unittest.TestCase):
) )
def test_x_type(): def test_x_type():
paddle.fluid.layers.gather(x, index) paddle.gather(x, index)
self.assertRaises(TypeError, test_x_type) self.assertRaises(TypeError, test_x_type)
def test_index_type(): def test_index_type():
paddle.fluid.layers.gather(x, index_float) paddle.gather(x, index_float)
self.assertRaises(TypeError, test_index_type) self.assertRaises(TypeError, test_index_type)
......
...@@ -270,7 +270,7 @@ class TestGatherNdAPI(unittest.TestCase): ...@@ -270,7 +270,7 @@ class TestGatherNdAPI(unittest.TestCase):
index_1 = np.array([[1]]) index_1 = np.array([[1]])
input = fluid.dygraph.to_variable(input_1) input = fluid.dygraph.to_variable(input_1)
index = fluid.dygraph.to_variable(index_1) index = fluid.dygraph.to_variable(index_1)
output = paddle.fluid.layers.gather(input, index) output = paddle.gather(input, index)
output_np = output.numpy() output_np = output.numpy()
expected_output = np.array([3, 4]) expected_output = np.array([3, 4])
np.testing.assert_allclose(output_np[0], expected_output) np.testing.assert_allclose(output_np[0], expected_output)
......
...@@ -86,7 +86,7 @@ class API_TestGather(unittest.TestCase): ...@@ -86,7 +86,7 @@ class API_TestGather(unittest.TestCase):
with fluid.program_guard(fluid.Program(), fluid.Program()): with fluid.program_guard(fluid.Program(), fluid.Program()):
data1 = fluid.layers.data('data1', shape=[-1, 2], dtype='float32') data1 = fluid.layers.data('data1', shape=[-1, 2], dtype='float32')
index = fluid.layers.data('index', shape=[-1, 1], dtype='int32') 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) place = paddle.NPUPlace(0)
exe = fluid.Executor(place) exe = fluid.Executor(place)
input = np.array([[1, 2], [3, 4], [5, 6]]) input = np.array([[1, 2], [3, 4], [5, 6]])
......
...@@ -248,7 +248,7 @@ class TestGatherNdAPI2(unittest.TestCase): ...@@ -248,7 +248,7 @@ class TestGatherNdAPI2(unittest.TestCase):
index_1 = np.array([[1]]) index_1 = np.array([[1]])
input = fluid.dygraph.to_variable(input_1) input = fluid.dygraph.to_variable(input_1)
index = fluid.dygraph.to_variable(index_1) index = fluid.dygraph.to_variable(index_1)
output = paddle.fluid.layers.gather(input, index) output = paddle.gather(input, index)
output_np = output.numpy() output_np = output.numpy()
expected_output = np.array([[3, 4]]) expected_output = np.array([[3, 4]])
np.testing.assert_allclose(output_np, expected_output, rtol=1e-05) np.testing.assert_allclose(output_np, expected_output, rtol=1e-05)
......
...@@ -225,7 +225,7 @@ class API_TestGather(unittest.TestCase): ...@@ -225,7 +225,7 @@ class API_TestGather(unittest.TestCase):
with fluid.program_guard(fluid.Program(), fluid.Program()): with fluid.program_guard(fluid.Program(), fluid.Program()):
data1 = fluid.layers.data('data1', shape=[-1, 2], dtype='float64') data1 = fluid.layers.data('data1', shape=[-1, 2], dtype='float64')
index = fluid.layers.data('index', shape=[-1, 1], dtype='int32') 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() place = fluid.CPUPlace()
exe = fluid.Executor(place) exe = fluid.Executor(place)
input = np.array([[1, 2], [3, 4], [5, 6]]) input = np.array([[1, 2], [3, 4], [5, 6]])
...@@ -264,7 +264,7 @@ class API_TestDygraphGather(unittest.TestCase): ...@@ -264,7 +264,7 @@ class API_TestDygraphGather(unittest.TestCase):
index_1 = np.array([1, 2]) index_1 = np.array([1, 2])
input = paddle.to_tensor(input_1) input = paddle.to_tensor(input_1)
index = paddle.to_tensor(index_1) index = paddle.to_tensor(index_1)
output = paddle.fluid.layers.gather(input, index) output = paddle.gather(input, index)
output_np = output.numpy() output_np = output.numpy()
expected_output = np.array([[3, 4], [5, 6]]) expected_output = np.array([[3, 4], [5, 6]])
np.testing.assert_allclose(output_np, expected_output, rtol=1e-05) np.testing.assert_allclose(output_np, expected_output, rtol=1e-05)
...@@ -372,12 +372,12 @@ class TestGathertError(unittest.TestCase): ...@@ -372,12 +372,12 @@ class TestGathertError(unittest.TestCase):
) )
def test_x_type(): def test_x_type():
paddle.fluid.layers.gather(x, index) paddle.gather(x, index)
self.assertRaises(TypeError, test_x_type) self.assertRaises(TypeError, test_x_type)
def test_index_type(): def test_index_type():
paddle.fluid.layers.gather(x, index_float) paddle.gather(x, index_float)
self.assertRaises(TypeError, test_index_type) self.assertRaises(TypeError, test_index_type)
......
...@@ -119,8 +119,8 @@ class DeepCF(fluid.Layer): ...@@ -119,8 +119,8 @@ class DeepCF(fluid.Layer):
def forward(self, users, items): def forward(self, users, items):
# users_emb = self._user_emb(users) # users_emb = self._user_emb(users)
# items_emb = self._item_emb(items) # items_emb = self._item_emb(items)
users_emb = fluid.layers.gather(self._rating_matrix, users) users_emb = paddle.gather(self._rating_matrix, users)
items_emb = fluid.layers.gather( items_emb = paddle.gather(
fluid.layers.transpose(self._rating_matrix, [1, 0]), items fluid.layers.transpose(self._rating_matrix, [1, 0]), items
) )
users_emb.stop_gradient = True users_emb.stop_gradient = True
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册