From 8b611f04048e0c1b554e7db9599c3789c47bac0a Mon Sep 17 00:00:00 2001 From: cyber-pioneer <116002591+cyber-pioneer@users.noreply.github.com> Date: Wed, 30 Nov 2022 16:34:47 +0800 Subject: [PATCH] remove fluid.layer.gather_tree (#48480) --- python/paddle/fluid/layers/nn.py | 65 ------------------- python/paddle/fluid/layers/rnn.py | 2 +- .../seq2seq_dygraph_model.py | 4 +- .../transformer_dygraph_model.py | 2 +- .../tests/unittests/test_gather_tree_op.py | 10 +-- 5 files changed, 10 insertions(+), 73 deletions(-) diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index dbac69df68..15eada61cf 100644 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -128,7 +128,6 @@ __all__ = [ 'shard_index', 'hard_swish', 'mish', - 'gather_tree', 'uniform_random', 'unbind', ] @@ -7928,70 +7927,6 @@ def mish(x, threshold=20, name=None): return out -def gather_tree(ids, parents): - r""" - To be used after beam search. After beam search, we get selected ids at - each time step and the corresponding parents in the search tree. Both ids - and parents have the layout :attr:`[max_time, batch_size, beam_size]`. Then - :attr:`gather_tree` is used to backtrace from the last time step and - generate the full sequences by collecting selected ids. - - Here is an example: - - .. code-block:: text - - Given: - ids = [[[2 2] - [6 1]] - [[3 9] - [6 1]] - [[0 1] - [9 0]]] - parents = [[[0 0] - [1 1]] - [[1 0] - [1 0]] - [[0 0] - [0 1]]] - - Then: - gather_tree(ids, parents) - = [[[2 2] - [1 6]] - [[3 3] - [6 1]] - [[0 1] - [9 0]]] - - Args: - ids(Tensor): A Tensor with shape :attr:`[length, batch_size, beam_size]` - and data type :attr:`int32` or :attr:`int64`. It contains the selected - ids of all time steps. - parents(Tensor): A Tensor with the same shape and data type as :attr:`ids`, - It contains the parents corresponding to selected ids when searching - among beams. - - Returns: - A Tensor with the same shape and data type as :attr:`ids`. \ - It contains the full sequences. The sequences are collected from \ - :attr:`ids` by backtracing according to :attr:`parents`. - - Examples: - .. code-block:: python - - import paddle - - ids = paddle.to_tensor([[[2, 2], [6, 1]], [[3, 9], [6, 1]], [[0, 1], [9, 0]]]) - - parents = paddle.to_tensor([[[0, 0], [1, 1]], [[1, 0], [1, 0]], [[0, 0], [0, 1]]]) - - final_sequences = paddle.nn.functional.gather_tree(ids, parents) - # [[[2, 2], [1, 6]], [[3, 3], [6, 1]], [[0, 1], [9, 0]]] - - """ - return paddle.nn.functional.gather_tree(ids, parents) - - @deprecated(since="2.0.0", update_to="paddle.uniform") @templatedoc() def uniform_random( diff --git a/python/paddle/fluid/layers/rnn.py b/python/paddle/fluid/layers/rnn.py index 23e61db9f4..1914c38f54 100644 --- a/python/paddle/fluid/layers/rnn.py +++ b/python/paddle/fluid/layers/rnn.py @@ -1427,7 +1427,7 @@ class BeamSearchDecoder(Decoder): `[time_step, batch_size, beam_size]`. `final_states` is the same \ as the input argument `final_states`. """ - predicted_ids = nn.gather_tree( + predicted_ids = paddle.nn.functional.gather_tree( outputs.predicted_ids, outputs.parent_ids ) # TODO: use FinalBeamSearchDecoderOutput as output 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 0b5efa636a..adc1909c64 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 @@ -502,7 +502,9 @@ class BaseModel(fluid.dygraph.Layer): predicted_ids = paddle.stack(predicted_ids) parent_ids = paddle.stack(parent_ids) - predicted_ids = fluid.layers.gather_tree(predicted_ids, parent_ids) + predicted_ids = paddle.nn.functional.gather_tree( + predicted_ids, parent_ids + ) predicted_ids = self._transpose_batch_time(predicted_ids) return predicted_ids 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 ae7da008df..fb00473f3d 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 @@ -884,7 +884,7 @@ class Transformer(Layer): predict_ids = paddle.stack(predict_ids, axis=0) parent_ids = paddle.stack(parent_ids, axis=0) finished_seq = paddle.transpose( - layers.gather_tree(predict_ids, parent_ids), [1, 2, 0] + paddle.nn.functional.gather_tree(predict_ids, parent_ids), [1, 2, 0] ) finished_scores = topk_scores diff --git a/python/paddle/fluid/tests/unittests/test_gather_tree_op.py b/python/paddle/fluid/tests/unittests/test_gather_tree_op.py index 1af5776cd6..bcd319ed2d 100644 --- a/python/paddle/fluid/tests/unittests/test_gather_tree_op.py +++ b/python/paddle/fluid/tests/unittests/test_gather_tree_op.py @@ -67,7 +67,7 @@ class TestGatherTreeOpAPI(unittest.TestCase): dtype='int64', append_batch_size=False, ) - final_sequences = fluid.layers.gather_tree(ids, parents) + final_sequences = paddle.nn.functional.gather_tree(ids, parents) paddle.disable_static() def test_case2(self): @@ -100,14 +100,14 @@ class TestGatherTreeOpError(unittest.TestCase): def test_Variable_ids(): # the input type must be Variable np_ids = np.random.random((5, 2, 2), dtype='int64') - fluid.layers.gather_tree(np_ids, parents) + paddle.nn.functional.gather_tree(np_ids, parents) self.assertRaises(TypeError, test_Variable_ids) def test_Variable_parents(): # the input type must be Variable np_parents = np.random.random((5, 2, 2), dtype='int64') - fluid.layers.gather_tree(ids, np_parents) + paddle.nn.functional.gather_tree(ids, np_parents) self.assertRaises(TypeError, test_Variable_parents) @@ -119,7 +119,7 @@ class TestGatherTreeOpError(unittest.TestCase): dtype='float32', append_batch_size=False, ) - fluid.layers.gather_tree(bad_ids, parents) + paddle.nn.functional.gather_tree(bad_ids, parents) self.assertRaises(TypeError, test_type_ids) @@ -131,7 +131,7 @@ class TestGatherTreeOpError(unittest.TestCase): dtype='float32', append_batch_size=False, ) - fluid.layers.gather_tree(ids, bad_parents) + paddle.nn.functional.gather_tree(ids, bad_parents) self.assertRaises(TypeError, test_type_parents) -- GitLab