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

import sequence_* API to new namespace #32089

Merged
merged 6 commits into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
164 changes: 94 additions & 70 deletions python/paddle/fluid/layers/sequence_lod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(), (
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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(), (
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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(), (
Expand Down Expand Up @@ -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(), (
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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(), (
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -1348,11 +1370,11 @@ def sequence_mask(x, maxlen=None, dtype='int64', name=None):
Examples:
.. code-block:: python

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


x = fluid.data(name='x', shape=[10], dtype='float32', lod_level=1)
mask = layers.sequence_mask(x=x)
lengths = paddle.to_tensor([10, 9, 8])
mask = paddle.nn.functional.sequence_mask(lengths)

"""
helper = LayerHelper('sequence_mask', **locals())
Expand Down Expand Up @@ -1414,9 +1436,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.")
Expand Down
1 change: 1 addition & 0 deletions python/paddle/nn/functional/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
# 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
Expand Down
3 changes: 2 additions & 1 deletion python/paddle/nn/functional/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,6 +23,7 @@
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):
Expand Down
31 changes: 31 additions & 0 deletions python/paddle/static/nn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@
'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
Expand Down Expand Up @@ -69,3 +84,19 @@

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