Skip to content

Commit 2315dae

Browse files
committed
update function
1 parent e01955e commit 2315dae

File tree

4 files changed

+20
-36
lines changed

4 files changed

+20
-36
lines changed

paddle/fluid/operators/math/sequence_padding.cc

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,6 @@ class PaddingLoDTensorFunctor<platform::CPUDeviceContext, T> {
9898

9999
CopyValidData<T>(pad_tensor, &seq_tensor, seq_offsets, pad_seq_len,
100100
step_width, norm_by_times, kSeqToPad, layout);
101-
102-
// Set pad_tensor's lod info if possible
103-
if (layout == kBatchLengthWidth) {
104-
framework::LoD pad_lod(seq_lod.begin() + lod_level, seq_lod.end());
105-
for (size_t i = 0; i < pad_lod[0].size(); ++i) {
106-
pad_lod[0][i] = i * pad_seq_len;
107-
}
108-
pad_tensor->set_lod(pad_lod);
109-
}
110101
}
111102
};
112103

paddle/fluid/operators/math/sequence_padding.cu

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,6 @@ class PaddingLoDTensorFunctor<platform::CUDADeviceContext, T> {
106106
pad_data, seq_data, pad_value_data, pad_value.numel() == 1,
107107
seq_offsets.CUDAData(context.GetPlace()), seq_num, pad_seq_len,
108108
step_width, norm_by_times, layout);
109-
110-
if (layout == kBatchLengthWidth) {
111-
framework::LoD pad_lod(seq_lod.begin() + lod_level, seq_lod.end());
112-
for (size_t i = 0; i < pad_lod[0].size(); ++i) {
113-
pad_lod[0][i] = i * pad_seq_len;
114-
}
115-
pad_tensor->set_lod(pad_lod);
116-
}
117109
}
118110
};
119111

paddle/fluid/operators/sequence_pad_op.cc

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class SequencePadOp : public framework::OperatorWithKernel {
4040
"The Input(PadValue) must be a scalar or a tensor whose "
4141
"shape equals to time steps in sequences");
4242

43-
int batch_dim_size = -1;
43+
int out_dim_0 = -1;
44+
int out_dim_1 = -1;
4445

4546
if (ctx->IsRuntime()) {
4647
// run time
@@ -64,17 +65,20 @@ class SequencePadOp : public framework::OperatorWithKernel {
6465
PADDLE_ENFORCE_GE(padded_length, max_seq_len,
6566
"The Attr(padded_length) must be -1 or an int greater "
6667
"than the length of the longest original sequence.");
67-
batch_dim_size = padded_length * seq_num;
68+
out_dim_0 = seq_num;
69+
out_dim_1 = padded_length;
6870
} else {
6971
// compile time
7072
framework::VarDesc* x_desc =
7173
boost::get<framework::VarDesc*>(ctx->GetInputVarPtrs("X")[0]);
7274
PADDLE_ENFORCE_GE(x_desc->GetLoDLevel(), 1);
7375
}
7476

75-
auto out_dims = x_dims;
76-
out_dims[0] = batch_dim_size;
77-
ctx->SetOutputDim("Out", out_dims);
77+
std::vector<int> out_dims_vec{out_dim_0, out_dim_1};
78+
auto time_step_dims_vec = framework::vectorize2int(time_step_dims);
79+
out_dims_vec.insert(out_dims_vec.end(), time_step_dims_vec.begin(),
80+
time_step_dims_vec.end());
81+
ctx->SetOutputDim("Out", framework::make_ddim(out_dims_vec));
7882
}
7983
};
8084

@@ -118,9 +122,9 @@ class SequencePadOpMaker : public framework::OpProtoAndCheckerMaker {
118122
and Input(PadValue):
119123
PadValue.data = [0]
120124
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]
125+
then we get LoDTensor:
126+
Out.data = [[a, b, 0, 0],
127+
[c, d, e, 0]]
124128
125129
Case 2:
126130
@@ -131,9 +135,9 @@ class SequencePadOpMaker : public framework::OpProtoAndCheckerMaker {
131135
PadValue.data = [0]
132136
and attribite 'padded_length' = -1, which mean using the length
133137
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]]
138+
then we get LoDTensor:
139+
Out.data = [[[a1, a2], [b1, b2], [0, 0]],
140+
[[c1, c2], [d1, d2], [e1, e2]]]
137141
138142
Case 3:
139143
@@ -144,9 +148,9 @@ class SequencePadOpMaker : public framework::OpProtoAndCheckerMaker {
144148
PadValue.data = [p1, p2]
145149
and attribite 'padded_length' = -1, which mean using the length
146150
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]]
151+
then we get LoDTensor:
152+
Out.data = [[[a1, a2], [b1, b2], [p1, p2]],
153+
[[c1, c2], [d1, d2], [e1, e2]]]
150154
151155
)DOC");
152156
}

python/paddle/fluid/tests/unittests/test_sequence_pad_op.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,8 @@ def compute(self):
6161
padded_sequences.append(seq)
6262
start_idx = end_idx
6363

64-
out_len_lod = self.x_len_lod[:]
65-
out_len_lod_0 = [padded_length] * len(x_len_lod_0)
66-
out_len_lod[0] = out_len_lod_0
67-
out_data = np.concatenate(padded_sequences, axis=0)
68-
self.outputs = {'Out': (out_data, out_len_lod)}
64+
out_data = np.array(padded_sequences)
65+
self.outputs = {'Out': out_data}
6966

7067
def setUp(self):
7168
self.op_type = 'sequence_pad'

0 commit comments

Comments
 (0)