Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean text.py and decode.py for API 2.0 #26853

Merged
merged 29 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d0eae3f
Make dynamic_decode support dygraph and expose to API 2.0
guoshengCS Sep 1, 2020
b55c1c2
Merge branch 'add-dy-decode' of https://github.com/guoshengCS/Paddle …
LiuChiachi Sep 23, 2020
3e185ca
update info about BeamSearchDecoder and dynamic_decode
LiuChiachi Sep 27, 2020
7dec9d6
remove all APIs in paddle.text, expose BeamSearchDecoder and dynamic_…
LiuChiachi Sep 29, 2020
505d0d3
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
LiuChiachi Sep 29, 2020
4c2aea0
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
LiuChiachi Sep 30, 2020
2c2962b
update example code
LiuChiachi Sep 30, 2020
3c67762
delete test_text.py, decode.py, update some doc, fix example code flo…
LiuChiachi Sep 30, 2020
ce0b405
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
LiuChiachi Sep 30, 2020
05602a6
delete decode import from paddle.nn
LiuChiachi Sep 30, 2020
c8f45c1
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
LiuChiachi Sep 30, 2020
7ef37ca
fix unittest bugs
LiuChiachi Sep 30, 2020
9952416
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
LiuChiachi Sep 30, 2020
12ef2ce
use dygraph.Embedding instead of nn.Embedding, add paddle.enbale_stat…
LiuChiachi Oct 9, 2020
402474f
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
LiuChiachi Oct 9, 2020
f0e49b8
solve conflicts of nn/__init__.py
LiuChiachi Oct 10, 2020
289f497
update, correct doc
LiuChiachi Oct 12, 2020
caf5f86
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
LiuChiachi Oct 12, 2020
6d7345e
move dynamic_decode, BeamSearchDecoder API to paddle.nn
LiuChiachi Oct 13, 2020
8f250e1
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
LiuChiachi Oct 13, 2020
18fdade
fix code style
LiuChiachi Oct 13, 2020
342336a
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
LiuChiachi Oct 13, 2020
aeab2f2
Merge branch 'add-dy-decode' of https://github.com/guoshengCS/Paddle …
LiuChiachi Oct 13, 2020
7e5e116
update unittest param, delete import pf text.py
LiuChiachi Oct 13, 2020
9572e2c
set dtype of beamsearchtest float64
LiuChiachi Oct 14, 2020
4cbda17
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
LiuChiachi Oct 14, 2020
185d57e
update example code of BeamSearchDecoder, dynamic_decode
LiuChiachi Oct 14, 2020
10f6bee
solve conflicts
LiuChiachi Oct 14, 2020
b4a6148
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
LiuChiachi Oct 14, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
315 changes: 213 additions & 102 deletions python/paddle/fluid/layers/rnn.py

Large diffs are not rendered by default.

160 changes: 160 additions & 0 deletions python/paddle/fluid/tests/unittests/test_rnn_decode_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,26 @@

from __future__ import print_function

import random
import unittest
import numpy as np

import paddle
import paddle.nn as nn
from paddle import Model, set_device
from paddle.static import InputSpec as Input
from paddle.fluid.dygraph import Layer
from paddle.text import BeamSearchDecoder, dynamic_decode

import paddle.fluid as fluid
import paddle.fluid.layers as layers
import paddle.fluid.core as core

from paddle.fluid.executor import Executor
from paddle.fluid import framework

paddle.enable_static()


class EncoderCell(layers.RNNCell):
def __init__(self, num_layers, hidden_size, dropout_prob=0.):
Expand Down Expand Up @@ -436,6 +446,7 @@ def setUp(self):
self.exe = Executor(place)

def test_mle_train(self):
paddle.enable_static()
self.model_hparams["decoding_strategy"] = "train_greedy"
agent = SeqPGAgent(
model_cls=Seq2SeqModel,
Expand Down Expand Up @@ -468,6 +479,7 @@ def test_mle_train(self):
(iter_idx, reward.mean(), cost))

def test_greedy_train(self):
paddle.enable_static()
self.model_hparams["decoding_strategy"] = "infer_greedy"
agent = SeqPGAgent(
model_cls=Seq2SeqModel,
Expand All @@ -493,6 +505,7 @@ def test_greedy_train(self):
(iter_idx, reward.mean(), cost))

def test_sample_train(self):
paddle.enable_static()
self.model_hparams["decoding_strategy"] = "infer_sample"
agent = SeqPGAgent(
model_cls=Seq2SeqModel,
Expand All @@ -518,6 +531,7 @@ def test_sample_train(self):
(iter_idx, reward.mean(), cost))

def test_beam_search_infer(self):
paddle.enable_static()
self.model_hparams["decoding_strategy"] = "beam_search"
main_program = fluid.Program()
startup_program = fluid.Program()
Expand All @@ -542,5 +556,151 @@ def test_beam_search_infer(self):
fetch_list=[output])[0]


class ModuleApiTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls._np_rand_state = np.random.get_state()
cls._py_rand_state = random.getstate()
cls._random_seed = 123
np.random.seed(cls._random_seed)
random.seed(cls._random_seed)

cls.model_cls = type(cls.__name__ + "Model", (Layer, ), {
"__init__": cls.model_init_wrapper(cls.model_init),
"forward": cls.model_forward
})

@classmethod
def tearDownClass(cls):
np.random.set_state(cls._np_rand_state)
random.setstate(cls._py_rand_state)

@staticmethod
def model_init_wrapper(func):
def __impl__(self, *args, **kwargs):
Layer.__init__(self)
func(self, *args, **kwargs)

return __impl__

@staticmethod
def model_init(model, *args, **kwargs):
raise NotImplementedError(
"model_init acts as `Model.__init__`, thus must implement it")

@staticmethod
def model_forward(model, *args, **kwargs):
return model.module(*args, **kwargs)

def make_inputs(self):
# TODO(guosheng): add default from `self.inputs`
raise NotImplementedError(
"model_inputs makes inputs for model, thus must implement it")

def setUp(self):
"""
For the model which wraps the module to be tested:
Set input data by `self.inputs` list
Set init argument values by `self.attrs` list/dict
Set model parameter values by `self.param_states` dict
Set expected output data by `self.outputs` list
We can create a model instance and run once with these.
"""
self.inputs = []
self.attrs = {}
self.param_states = {}
self.outputs = []

def _calc_output(self, place, mode="test", dygraph=True):
if dygraph:
fluid.enable_dygraph(place)
else:
fluid.disable_dygraph()
gen = paddle.manual_seed(self._random_seed)
gen._is_init_py = False
paddle.framework.random._manual_program_seed(self._random_seed)
scope = fluid.core.Scope()
with fluid.scope_guard(scope):
layer = self.model_cls(**self.attrs) if isinstance(
self.attrs, dict) else self.model_cls(*self.attrs)
model = Model(layer, inputs=self.make_inputs())
model.prepare()
if self.param_states:
model.load(self.param_states, optim_state=None)
return model.test_batch(self.inputs)

def check_output_with_place(self, place, mode="test"):
dygraph_output = self._calc_output(place, mode, dygraph=True)
stgraph_output = self._calc_output(place, mode, dygraph=False)
expect_output = getattr(self, "outputs", None)
for actual_t, expect_t in zip(dygraph_output, stgraph_output):
self.assertTrue(np.allclose(actual_t, expect_t, rtol=1e-5, atol=0))
if expect_output:
for actual_t, expect_t in zip(dygraph_output, expect_output):
self.assertTrue(
np.allclose(
actual_t, expect_t, rtol=1e-5, atol=0))

def check_output(self):
devices = ["CPU", "GPU"] if fluid.is_compiled_with_cuda() else ["CPU"]
for device in devices:
place = set_device(device)
self.check_output_with_place(place)


class TestBeamSearch(ModuleApiTest):
def setUp(self):
shape = (8, 32)
self.inputs = [
np.random.random(shape).astype("float32"),
np.random.random(shape).astype("float32")
]
self.outputs = None
self.attrs = {
"vocab_size": 100,
"embed_dim": 32,
"hidden_size": 32,
}
self.param_states = {}

@staticmethod
def model_init(self,
vocab_size,
embed_dim,
hidden_size,
bos_id=0,
eos_id=1,
beam_size=4,
max_step_num=20):
embedder = paddle.fluid.dygraph.Embedding(size=[vocab_size, embed_dim])
output_layer = nn.Linear(hidden_size, vocab_size)
cell = nn.LSTMCell(embed_dim, hidden_size)
self.max_step_num = max_step_num
self.beam_search_decoder = BeamSearchDecoder(
cell,
start_token=bos_id,
end_token=eos_id,
beam_size=beam_size,
embedding_fn=embedder,
output_fn=output_layer)

@staticmethod
def model_forward(model, init_hidden, init_cell):
return dynamic_decode(
model.beam_search_decoder, [init_hidden, init_cell],
max_step_num=model.max_step_num,
is_test=True)[0]

def make_inputs(self):
inputs = [
Input([None, self.inputs[0].shape[-1]], "float32", "init_hidden"),
Input([None, self.inputs[1].shape[-1]], "float32", "init_cell"),
]
return inputs

def test_check_output(self):
self.check_output()


if __name__ == '__main__':
unittest.main()
5 changes: 0 additions & 5 deletions python/paddle/nn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,9 @@
# from .control_flow import StaticRNN #DEFINE_ALIAS
from .control_flow import while_loop #DEFINE_ALIAS
# from .control_flow import rnn #DEFINE_ALIAS
# from .decode import BeamSearchDecoder #DEFINE_ALIAS
# from .decode import Decoder #DEFINE_ALIAS
from .decode import beam_search #DEFINE_ALIAS
from .decode import beam_search_decode #DEFINE_ALIAS
# from .decode import crf_decoding #DEFINE_ALIAS
# from .decode import ctc_greedy_decoder #DEFINE_ALIAS
# from .decode import dynamic_decode #DEFINE_ALIAS
from .decode import gather_tree #DEFINE_ALIAS
# from .input import Input #DEFINE_ALIAS
from .layer.activation import ELU #DEFINE_ALIAS
from .layer.activation import GELU #DEFINE_ALIAS
Expand Down
30 changes: 0 additions & 30 deletions python/paddle/nn/decode.py

This file was deleted.

1 change: 1 addition & 0 deletions python/paddle/nn/functional/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,4 @@
from .vision import yolov3_loss #DEFINE_ALIAS
from .input import one_hot #DEFINE_ALIAS
from .input import embedding #DEFINE_ALIAS
from ...fluid.layers import gather_tree
Loading