Skip to content

Commit 1da7158

Browse files
committed
add op comment and python layer
1 parent d50f518 commit 1da7158

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

paddle/fluid/operators/math/sequence_padding.cu

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ class PaddingLoDTensorFunctor<platform::CUDADeviceContext, T> {
6666
if (pad_seq_len == -1) {
6767
pad_seq_len = max_seq_len;
6868
}
69+
PADDLE_ENFORCE_GE(pad_seq_len, max_seq_len,
70+
"The pad_seq_len must be equal to or greater than the "
71+
"original max sequence length.");
6972
int step_width = seq_tensor.numel() / seq_tensor_dims[0];
7073
int seq_num = seq_offsets.size() - 1;
7174

paddle/fluid/operators/sequence_pad_op.cc

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,52 @@ class SequencePadOpMaker : public framework::OpProtoAndCheckerMaker {
101101
"sequence.")
102102
.SetDefault(-1);
103103
AddComment(R"DOC(
104+
Sequence Pad Operator
105+
106+
This operator pads sequences in a same batch to a consistent length.
107+
The length is specified by attribute 'padded_length'. New elements,
108+
whose values are specified by input 'PadValue', will be appended to
109+
the end of each sequence, to make their final lengths consistent.
110+
111+
Following are cases to better explain how this works:
112+
113+
Case 1:
114+
115+
Given a 1-level LoDTensor input(X):
116+
X.lod = [[0, 2, 5]]
117+
X.data = [a, b, c, d, e]
118+
and Input(PadValue):
119+
PadValue.data = [0]
120+
and attribite 'padded_length' = 4,
121+
then we get 1-level LoDTensor:
122+
Out.lod = [[0, 4, 8]]
123+
Out.data = [a, b, 0, 0, c, d, e, 0]
124+
125+
Case 2:
126+
127+
Given a 1-level LoDTensor input(X):
128+
X.lod = [[0, 2, 5]]
129+
X.data = [[a1, a2], [b1, b2], [c1, c2], [d1, d2], [e1, e2]]
130+
and Input(PadValue):
131+
PadValue.data = [0]
132+
and attribite 'padded_length' = -1, which mean using the length
133+
of longest input sequence(3 in this case),
134+
then we get 1-level LoDTensor:
135+
Out.lod = [[0, 3, 6]]
136+
Out.data = [[a1, a2], [b1, b2], [0, 0], [c1, c2], [d1, d2], [e1, e2]]
137+
138+
Case 3:
139+
140+
Given a 1-level LoDTensor input(X):
141+
X.lod = [[0, 2, 5]]
142+
X.data = [[a1, a2], [b1, b2], [c1, c2], [d1, d2], [e1, e2]]
143+
and Input(PadValue):
144+
PadValue.data = [p1, p2]
145+
and attribite 'padded_length' = -1, which mean using the length
146+
of longest input sequence(3 in this case),
147+
then we get 1-level LoDTensor:
148+
Out.lod = [[0, 3, 6]]
149+
Out.data = [[a1, a2], [b1, b2], [p1, p2], [c1, c2], [d1, d2], [e1, e2]]
104150
105151
)DOC");
106152
}

python/paddle/fluid/layers/nn.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2657,6 +2657,51 @@ def sequence_expand(x, y, ref_level=-1, name=None):
26572657
return tmp
26582658

26592659

2660+
@templatedoc()
2661+
def sequence_pad(x, pad_value, maxlen=None):
2662+
"""
2663+
${comment}
2664+
2665+
Args:
2666+
x(Variable): Input variable which should contain lod information.
2667+
pad_value(Variable): The Variable that holds values that will be fill
2668+
into padded steps. It can be a scalar or a tensor whose shape
2669+
equals to time steps in sequences. If it's a scalar, it will be
2670+
automatically broadcasted to the shape of time step.
2671+
maxlen(int, default None): The length of padded sequences. It can be
2672+
None or any positive int. When it is None, all sequences will be
2673+
padded up to the length of the longest one among them; when it a
2674+
certain positive value, it must be greater than the length of the
2675+
longest original sequence."
2676+
2677+
Returns:
2678+
Variable: The padded sequence batch. All sequences has the same length.
2679+
2680+
Examples:
2681+
.. code-block:: python
2682+
2683+
import numpy
2684+
2685+
x = fluid.layers.data(name='y', shape=[10, 5],
2686+
dtype='float32', lod_level=1)
2687+
pad_value = fluid.layers.assign(input=numpy.array([0]))
2688+
out = fluid.layers.sequence_pad(x=x, pad_value=pad_value)
2689+
"""
2690+
2691+
helper = LayerHelper('sequence_pad', input=x, **locals())
2692+
dtype = helper.input_dtype()
2693+
out = helper.create_tmp_variable(dtype)
2694+
if maxlen is None:
2695+
maxlen = -1
2696+
helper.append_op(
2697+
type='sequence_pad',
2698+
inputs={'X': x,
2699+
'PadValue': pad_value},
2700+
outputs={'Out': out},
2701+
attrs={'padded_length': maxlen})
2702+
return out
2703+
2704+
26602705
def beam_search(pre_ids,
26612706
pre_scores,
26622707
ids,

0 commit comments

Comments
 (0)