From f12c943ab9a38da9bc820d284a8f6fea6d94c781 Mon Sep 17 00:00:00 2001 From: Feiyu Chan Date: Thu, 22 Apr 2021 14:41:17 +0800 Subject: [PATCH] import sequence_* API to new namespace (#32089) * import sequence_* API to new namespace * fix typos, remove alias marking * update sample code * fix sample code * fix docstring for sequence_mask --- python/paddle/fluid/layers/sequence_lod.py | 176 ++++++++++++--------- python/paddle/nn/functional/__init__.py | 1 + python/paddle/nn/functional/extension.py | 3 +- python/paddle/static/nn/__init__.py | 31 ++++ 4 files changed, 136 insertions(+), 75 deletions(-) diff --git a/python/paddle/fluid/layers/sequence_lod.py b/python/paddle/fluid/layers/sequence_lod.py index df1113660f7..a42ec2c92a3 100644 --- a/python/paddle/fluid/layers/sequence_lod.py +++ b/python/paddle/fluid/layers/sequence_lod.py @@ -139,10 +139,11 @@ def sequence_conv(input, .. code-block:: python - import paddle.fluid as fluid + import paddle + paddle.enable_static() - x = fluid.data(name='x', shape=[-1, 10], dtype='float32', lod_level=1) - x_conved = fluid.layers.sequence_conv(input=x, num_filters=2, filter_size=3, padding_start=-1) + x = paddle.static.data(name='x', shape=[-1, 10], dtype='float32', lod_level=1) + x_conved = paddle.static.nn.sequence_conv(input=x, num_filters=2, filter_size=3, padding_start=-1) """ assert not in_dygraph_mode(), ( @@ -233,15 +234,17 @@ def sequence_softmax(input, use_cudnn=False, name=None): Examples: .. code-block:: python - - import paddle.fluid as fluid - x = fluid.data(name='x', shape=[7, 1], + + import paddle + paddle.enable_static() + + x = paddle.static.data(name='x', shape=[7, 1], dtype='float32', lod_level=1) - x_sequence_softmax_1 = fluid.layers.sequence_softmax(input=x) + x_sequence_softmax_1 = paddle.static.nn.sequence_softmax(input=x) - y = fluid.data(name='y', shape=[7], + y = paddle.static.data(name='y', shape=[7], dtype='float32', lod_level=1) - x_sequence_softmax_2 = fluid.layers.sequence_softmax(input=y) + x_sequence_softmax_2 = paddle.static.nn.sequence_softmax(input=y) """ assert not in_dygraph_mode(), ( "sequence layer is not supported in dygraph mode yet.") @@ -334,15 +337,16 @@ def sequence_pool(input, pool_type, is_test=False, pad_value=0.0): .. code-block:: python - import paddle.fluid as fluid + import paddle + paddle.enable_static() - x = fluid.data(name='x', shape=[None, 10], dtype='float32', lod_level=1) - avg_x = fluid.layers.sequence_pool(input=x, pool_type='average') - sum_x = fluid.layers.sequence_pool(input=x, pool_type='sum') - sqrt_x = fluid.layers.sequence_pool(input=x, pool_type='sqrt') - max_x = fluid.layers.sequence_pool(input=x, pool_type='max') - last_x = fluid.layers.sequence_pool(input=x, pool_type='last') - first_x = fluid.layers.sequence_pool(input=x, pool_type='first') + x = paddle.static.data(name='x', shape=[None, 10], dtype='float32', lod_level=1) + avg_x = paddle.static.nn.sequence_pool(input=x, pool_type='average') + sum_x = paddle.static.nn.sequence_pool(input=x, pool_type='sum') + sqrt_x = paddle.static.nn.sequence_pool(input=x, pool_type='sqrt') + max_x = paddle.static.nn.sequence_pool(input=x, pool_type='max') + last_x = paddle.static.nn.sequence_pool(input=x, pool_type='last') + first_x = paddle.static.nn.sequence_pool(input=x, pool_type='first') """ assert not in_dygraph_mode(), ( "sequence layer is not supported in dygraph mode yet.") @@ -413,10 +417,12 @@ def sequence_concat(input, name=None): Examples: .. code-block:: python - import paddle.fluid as fluid - x = fluid.data(name='x', shape=[-1, 10], dtype='float32', lod_level=1) - y = fluid.data(name='y', shape=[-1, 10], dtype='float32', lod_level=1) - out = fluid.layers.sequence_concat(input=[x, y]) + import paddle + paddle.enable_static() + + x = paddle.static.data(name='x', shape=[-1, 10], dtype='float32', lod_level=1) + y = paddle.static.data(name='y', shape=[-1, 10], dtype='float32', lod_level=1) + out = paddle.static.nn.sequence_concat(input=[x, y]) """ assert not in_dygraph_mode(), ( "sequence layer is not supported in dygraph mode yet.") @@ -481,9 +487,11 @@ def sequence_first_step(input): .. code-block:: python - import paddle.fluid as fluid - x = fluid.data(name='x', shape=[None, 10], dtype='float32', lod_level=1) - x_first_step = fluid.layers.sequence_first_step(input=x) + import paddle + paddle.enable_static() + + x = paddle.static.data(name='x', shape=[None, 10], dtype='float32', lod_level=1) + x_first_step = paddle.static.nn.sequence_first_step(input=x) """ check_variable_and_dtype(input, 'input', ['float32', 'float64'], 'sequence_first_step') @@ -538,9 +546,11 @@ def sequence_last_step(input): .. code-block:: python - import paddle.fluid as fluid - x = fluid.data(name='x', shape=[None, 10], dtype='float32', lod_level=1) - x_last_step = fluid.layers.sequence_last_step(input=x) + import paddle + paddle.enable_static() + + x = paddle.static.data(name='x', shape=[None, 10], dtype='float32', lod_level=1) + x_last_step = paddle.static.nn.sequence_last_step(input=x) """ check_variable_and_dtype(input, 'input', ['float32', 'float64'], 'sequence_last_step') @@ -598,13 +608,15 @@ def sequence_slice(input, offset, length, name=None): .. code-block:: python - import paddle.fluid as fluid + import paddle + paddle.enable_static() + import numpy as np - seqs = fluid.data(name='x', shape=[10, 5], + seqs = paddle.static.data(name='x', shape=[10, 5], dtype='float32', lod_level=1) - offset = fluid.layers.assign(input=np.array([[0, 1]]).astype("int32")) - length = fluid.layers.assign(input=np.array([[2, 1]]).astype("int32")) - subseqs = fluid.layers.sequence_slice(input=seqs, offset=offset, + offset = paddle.assign(np.array([[0, 1]]).astype("int32")) + length = paddle.assign(np.array([[2, 1]]).astype("int32")) + subseqs = paddle.static.nn.sequence_slice(input=seqs, offset=offset, length=length) """ assert not in_dygraph_mode(), ( @@ -715,17 +727,18 @@ def sequence_expand(x, y, ref_level=-1, name=None): Examples: .. code-block:: python - import paddle.fluid as fluid - import paddle.fluid.layers as layers + import paddle + from paddle import fluid + paddle.enable_static() import numpy as np - x = fluid.data(name='x', shape=[4, 1], dtype='float32') - y = fluid.data(name='y', shape=[8, 1], + x = paddle.static.data(name='x', shape=[4, 1], dtype='float32') + y = paddle.static.data(name='y', shape=[8, 1], dtype='float32', lod_level=1) - out = layers.sequence_expand(x=x, y=y, ref_level=0) + out = paddle.static.nn.sequence_expand(x=x, y=y, ref_level=0) - exe = fluid.Executor(fluid.CPUPlace()) - place = fluid.CPUPlace() + exe = paddle.static.Executor(fluid.CPUPlace()) + place = paddle.CPUPlace() np_data = np.array([[1], [2], [3], [4]]).astype('float32') x_lod_tensor = fluid.create_lod_tensor(np_data, [[2, 2]], place) @@ -836,13 +849,14 @@ def sequence_expand_as(x, y, name=None): Examples: .. code-block:: python + import paddle import paddle.fluid as fluid - import paddle.fluid.layers as layers + paddle.enable_static() import numpy as np - x = fluid.data(name='x', shape=[4, 1], dtype='float32') - y = fluid.data(name='y', shape=[8, 1], dtype='float32', lod_level=1) - out = layers.sequence_expand_as(x=x, y=y) + x = paddle.static.data(name='x', shape=[4, 1], dtype='float32') + y = paddle.static.data(name='y', shape=[8, 1], dtype='float32', lod_level=1) + out = paddle.static.nn.sequence_expand_as(x=x, y=y) exe = fluid.Executor(fluid.CPUPlace()) place = fluid.CPUPlace() @@ -969,13 +983,15 @@ def sequence_pad(x, pad_value, maxlen=None, name=None): Examples: .. code-block:: python + import paddle + paddle.enable_static() import paddle.fluid as fluid import numpy - x = fluid.data(name='x', shape=[10, 5], dtype='float32', lod_level=1) - pad_value = fluid.layers.assign( - input=numpy.array([0.0], dtype=numpy.float32)) - out = fluid.layers.sequence_pad(x=x, pad_value=pad_value) + x = paddle.static.data(name='x', shape=[10, 5], dtype='float32', lod_level=1) + pad_value = paddle.assign( + numpy.array([0.0], dtype=numpy.float32)) + out = paddle.static.nn.sequence_pad(x=x, pad_value=pad_value) """ assert not in_dygraph_mode(), ( @@ -1048,16 +1064,18 @@ def sequence_unpad(x, length, name=None): Examples: .. code-block:: python + import paddle + paddle.enable_static() import paddle.fluid as fluid import numpy # pad data - x = fluid.data(name='x', shape=[10, 5], dtype='float32', lod_level=1) - pad_value = fluid.layers.assign(input=numpy.array([0.0], dtype=numpy.float32)) - pad_data, len = fluid.layers.sequence_pad(x=x, pad_value=pad_value) + x = paddle.static.data(name='x', shape=[10, 5], dtype='float32', lod_level=1) + pad_value = paddle.assign(numpy.array([0.0], dtype=numpy.float32)) + pad_data, len = paddle.static.nn.sequence_pad(x=x, pad_value=pad_value) # unpad data - unpad_data = fluid.layers.sequence_unpad(x=pad_data, length=len) + unpad_data = paddle.static.nn.sequence_unpad(x=pad_data, length=len) """ assert not in_dygraph_mode(), ( @@ -1123,9 +1141,11 @@ def sequence_reshape(input, new_dim): Examples: .. code-block:: python - import paddle.fluid as fluid - x = fluid.data(name='x', shape=[None, 16], dtype='float32', lod_level=1) - x_reshaped = fluid.layers.sequence_reshape(input=x, new_dim=4) + import paddle + paddle.enable_static() + + x = paddle.static.data(name='x', shape=[None, 16], dtype='float32', lod_level=1) + x_reshaped = paddle.static.nn.sequence_reshape(input=x, new_dim=4) """ assert not in_dygraph_mode(), ( "sequence layer is not supported in dygraph mode yet.") @@ -1200,12 +1220,13 @@ def sequence_scatter(input, index, updates, name=None): .. code-block:: python - import paddle.fluid as fluid + import paddle + paddle.enable_static() - input = fluid.data( name="x", shape=[None, 3, 6], dtype='float32' ) - index = fluid.data( name='index', shape=[12, 1], dtype='int64', lod_level=1) - updates = fluid.data( name='updates', shape=[12, 1], dtype='float32', lod_level=1) - output = fluid.layers.sequence_scatter(input, index, updates) + input = paddle.static.data(name="x", shape=[None, 3, 6], dtype='float32' ) + index = paddle.static.data(name='index', shape=[12, 1], dtype='int64', lod_level=1) + updates = paddle.static.data(name='updates', shape=[12, 1], dtype='float32', lod_level=1) + output = paddle.static.nn.sequence_scatter(input, index, updates) """ assert not in_dygraph_mode(), ( @@ -1279,10 +1300,11 @@ def sequence_enumerate(input, win_size, pad_value=0, name=None): Examples: .. code-block:: python - import paddle.fluid as fluid - - x = fluid.data(name='x', shape=[-1, 1], dtype='int32', lod_level=1) - out = fluid.layers.sequence_enumerate(input=x, win_size=3, pad_value=0) + import paddle + paddle.enable_static() + + x = paddle.static.data(name='x', shape=[-1, 1], dtype='int32', lod_level=1) + out = paddle.static.nn.sequence_enumerate(input=x, win_size=3, pad_value=0) """ assert not in_dygraph_mode(), ( "sequence layer is not supported in dygraph mode yet.") @@ -1333,26 +1355,30 @@ def sequence_mask(x, maxlen=None, dtype='int64', name=None): Tensor or LodTensor with shape [d_1, d_2, ..., d_n]. maxlen (int, optional): Maximum length of the sequence. If :code:`maxlen` \ is None, it would be replace with :math:`max(x)`. - dtype (np.dtype|core.VarDesc.VarType|str, optional): Data type of the output, \ + dtype (np.dtype|paddle.dtype|str, optional): Data type of the output, \ ``int64`` by default. name(str, optional): For detailed information, please refer \ to :ref:`api_guide_Name`. Usually name is no need to set and \ None by default. - Returns: The output sequence mask. Tensor or LodTensor with shape [d_1, d_2, ..., d_n, maxlen] \ - and data type of :code:`dtype`. The data type should be float32, float64, int8, \ + Returns: The output sequence mask. Tensor with shape [d_1, d_2, ..., d_n, maxlen] \ + and data type of :code:`dtype`. The data type should be bool, float32, float64, int8, \ int32 or int64. - Return Type: Variable + Return Type: Tensor Examples: .. code-block:: python - import paddle.fluid as fluid - import paddle.fluid.layers as layers + import paddle + + lengths = paddle.to_tensor([10, 9, 8]) + mask = paddle.nn.functional.sequence_mask(lengths) - x = fluid.data(name='x', shape=[10], dtype='float32', lod_level=1) - mask = layers.sequence_mask(x=x) + print(mask.numpy()) + # [[1 1 1 1 1 1 1 1 1 1] + # [1 1 1 1 1 1 1 1 1 0] + # [1 1 1 1 1 1 1 1 0 0]] """ helper = LayerHelper('sequence_mask', **locals()) @@ -1414,9 +1440,11 @@ def sequence_reverse(x, name=None): Examples: .. code-block:: python - import paddle.fluid as fluid - x = fluid.data(name='x', shape=[None, 10], dtype='float32', lod_level=1) - x_reversed = fluid.layers.sequence_reverse(x) + import paddle + paddle.enable_static() + + x = paddle.static.data(name='x', shape=[None, 10], dtype='float32', lod_level=1) + x_reversed = paddle.static.nn.sequence_reverse(x) """ assert not in_dygraph_mode(), ( "sequence layer is not supported in dygraph mode yet.") diff --git a/python/paddle/nn/functional/__init__.py b/python/paddle/nn/functional/__init__.py index f1f9913e558..268bdffeb36 100644 --- a/python/paddle/nn/functional/__init__.py +++ b/python/paddle/nn/functional/__init__.py @@ -98,6 +98,7 @@ from .conv import conv3d_transpose #DEFINE_ALIAS # from .extension import temporal_shift #DEFINE_ALIAS # from .extension import warpctc #DEFINE_ALIAS from .extension import diag_embed #DEFINE_ALIAS +from .extension import sequence_mask # from .lod import sequence_concat #DEFINE_ALIAS # from .lod import sequence_conv #DEFINE_ALIAS # from .lod import sequence_enumerate #DEFINE_ALIAS diff --git a/python/paddle/nn/functional/extension.py b/python/paddle/nn/functional/extension.py index 3bbdb89f16c..b004d79a877 100644 --- a/python/paddle/nn/functional/extension.py +++ b/python/paddle/nn/functional/extension.py @@ -14,7 +14,7 @@ # TODO: define the extention functions -__all__ = ['diag_embed'] +__all__ = ['diag_embed', 'sequence_mask'] import numpy as np from ...fluid.data_feeder import check_dtype @@ -23,6 +23,7 @@ from ...fluid.framework import Variable, in_dygraph_mode from ...fluid.layers.tensor import assign from ...fluid import core, dygraph_utils from ...fluid.layers.layer_function_generator import templatedoc +from ...fluid.layers.sequence_lod import sequence_mask def diag_embed(input, offset=0, dim1=-2, dim2=-1): diff --git a/python/paddle/static/nn/__init__.py b/python/paddle/static/nn/__init__.py index fd84a0a9284..0e9754d3c1f 100644 --- a/python/paddle/static/nn/__init__.py +++ b/python/paddle/static/nn/__init__.py @@ -39,6 +39,21 @@ __all__ = [ 'switch_case', 'while_loop', 'sparse_embedding', + 'sequence_conv', + 'sequence_softmax', + 'sequence_pool', + 'sequence_concat', + 'sequence_first_step', + 'sequence_last_step', + 'sequence_slice', + 'sequence_expand', + 'sequence_expand_as', + 'sequence_pad', + 'sequence_unpad', + 'sequence_reshape', + 'sequence_scatter', + 'sequence_enumerate', + 'sequence_reverse', ] from .common import fc #DEFINE_ALIAS @@ -69,3 +84,19 @@ from ...fluid.layers import while_loop #DEFINE_ALIAS from ...fluid.input import embedding #DEFINE_ALIAS from ...fluid.contrib.layers import sparse_embedding #DEFINE_ALIAS + +from ...fluid.layers.sequence_lod import sequence_conv +from ...fluid.layers.sequence_lod import sequence_softmax +from ...fluid.layers.sequence_lod import sequence_pool +from ...fluid.layers.sequence_lod import sequence_concat +from ...fluid.layers.sequence_lod import sequence_first_step +from ...fluid.layers.sequence_lod import sequence_last_step +from ...fluid.layers.sequence_lod import sequence_slice +from ...fluid.layers.sequence_lod import sequence_expand +from ...fluid.layers.sequence_lod import sequence_expand_as +from ...fluid.layers.sequence_lod import sequence_pad +from ...fluid.layers.sequence_lod import sequence_unpad +from ...fluid.layers.sequence_lod import sequence_reshape +from ...fluid.layers.sequence_lod import sequence_scatter +from ...fluid.layers.sequence_lod import sequence_enumerate +from ...fluid.layers.sequence_lod import sequence_reverse -- GitLab