Skip to content

Commit 03136f6

Browse files
Merge pull request #4740 from wanghaoshuang/seq_expand_op
Seq expand op
2 parents 8efd087 + 84f471b commit 03136f6

File tree

5 files changed

+344
-5
lines changed

5 files changed

+344
-5
lines changed

paddle/operators/seq_expand_op.cc

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include "paddle/operators/seq_expand_op.h"
16+
17+
namespace paddle {
18+
namespace operators {
19+
20+
using framework::Tensor;
21+
22+
class SeqExpandOp : public framework::OperatorWithKernel {
23+
public:
24+
using framework::OperatorWithKernel::OperatorWithKernel;
25+
26+
protected:
27+
void InferShape(framework::InferShapeContext* ctx) const override {
28+
PADDLE_ENFORCE(ctx->HasInput("X"));
29+
PADDLE_ENFORCE(ctx->HasOutput("Out"));
30+
PADDLE_ENFORCE(ctx->HasInput("Y"));
31+
framework::DDim out_dim;
32+
out_dim = ctx->GetInputDim("Y");
33+
ctx->ShareLoD("Y", "Out");
34+
ctx->SetOutputDim("Out", out_dim);
35+
}
36+
};
37+
38+
class SeqExpandOpMaker : public framework::OpProtoAndCheckerMaker {
39+
public:
40+
SeqExpandOpMaker(framework::OpProto* proto,
41+
framework::OpAttrChecker* op_checker)
42+
: OpProtoAndCheckerMaker(proto, op_checker) {
43+
AddInput("X",
44+
"(Tensor or LoDTensor) The input(X) of this operator can be a "
45+
"LoDTensor or a base Tensor.");
46+
AddInput("Y",
47+
"(LoDTensor)The reference input(Y) of seq_expand op."
48+
"It must be a LoDTensor with k-level(k>0)."
49+
"The input(X) will be expanded according to LOD of input(Y)."
50+
"The element numbers of last level in input(Y) "
51+
"must be equal to dims[0] of input(X).");
52+
AddOutput("Out",
53+
"(LodTensor)The output of seq_expand op."
54+
"The lod of output will be as same as input(Y)'s lod.");
55+
AddComment(R"DOC(
56+
Expand input(X) according to LOD of input(Y).
57+
58+
Case 1:
59+
60+
Given 2-level a LoDTensor input(X)
61+
X.lod = [[0, 2, 3],
62+
[0, 1, 3, 4]]
63+
X.data = [a, b, c, d]
64+
X.dims = [4, 1]
65+
and input(Y)
66+
Y.lod = [[0, 2, 4],
67+
[0, 3, 6, 7, 8]]
68+
with condition len(Y.lod[-1]) -1 == X.dims[0]
69+
then we get 2-level LoDTensor
70+
Out.lod = [[0, 2, 4],
71+
[0, 3, 6, 7, 8]]
72+
Out.data = [a, a, a, b, b, b, c, d]
73+
Out.dims = [8, 1]
74+
75+
Case 2:
76+
77+
Given a 0-level LoDTensor input(X)
78+
X.data = [a, b, c]
79+
X.lod = NULL
80+
X.dims = [3, 1]
81+
and input(Y)
82+
Y.lod = [[0, 2, 3, 6]]
83+
with condition len(Y.lod[-1]) -1 == X.dims[0]
84+
then we get 1-level LoDTensor
85+
Out.lod = [[0, 2, 3, 6]]
86+
Out.data = [a, a, b, c, c, c]
87+
Out.dims = [6, 1]
88+
89+
Case 3:
90+
91+
Given a 0-level LoDTensor input(X)
92+
X.data = [[a, b], [c, d], [e, f]]
93+
X.lod = NULL
94+
X.dims = [3, 2]
95+
and input(Y)
96+
Y.lod = [[0, 2, 3, 6]]
97+
with condition len(Y.lod[-1]) -1 == X.dims[0]
98+
then we get 1-level LoDTensor
99+
Out.lod = [[0, 2, 3, 6]]
100+
Out.data = [[a,b], [a,b] [c,d], [e, f], [e, f], [e, f]]
101+
Out.dims = [6, 2]
102+
103+
Case 4:
104+
105+
Given 2-level a LoDTensor input(X)
106+
X.lod = [[0, 2, 3],
107+
[0, 1, 3, 4]]
108+
X.data = [a, b, c, d]
109+
X.dims = [4, 1]
110+
and input(Y)
111+
Y.lod = [[0, 2, 4],
112+
[0, 3, 6, 6, 8]]
113+
with condition len(Y.lod[-1]) -1 == X.dims[0]
114+
then we get 2-level LoDTensor
115+
Out.lod = [[0, 2, 4],
116+
[0, 3, 6, 6, 8]]
117+
Out.data = [a, a, a, b, b, b, d, d]
118+
Out.dims = [8, 1]
119+
120+
121+
)DOC");
122+
}
123+
};
124+
125+
class SeqExpandOpGrad : public framework::OperatorWithKernel {
126+
public:
127+
using framework::OperatorWithKernel::OperatorWithKernel;
128+
129+
protected:
130+
void InferShape(framework::InferShapeContext* ctx) const override {
131+
PADDLE_ENFORCE(ctx->HasInput("X"));
132+
PADDLE_ENFORCE(ctx->HasInput("Out"));
133+
PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
134+
"The input(Out@GRAD) should not be null");
135+
auto x_dims = ctx->GetInputDim("X");
136+
auto x_grad_name = framework::GradVarName("X");
137+
if (ctx->HasOutput(x_grad_name)) {
138+
ctx->SetOutputDim(x_grad_name, x_dims);
139+
}
140+
}
141+
};
142+
143+
} // namespace operators
144+
} // namespace paddle
145+
146+
namespace ops = paddle::operators;
147+
REGISTER_OP(seq_expand, ops::SeqExpandOp, ops::SeqExpandOpMaker,
148+
seq_expand_grad, ops::SeqExpandOpGrad);
149+
REGISTER_OP_CPU_KERNEL(seq_expand,
150+
ops::SeqExpandKernel<paddle::platform::CPUPlace, float>);
151+
REGISTER_OP_CPU_KERNEL(
152+
seq_expand_grad,
153+
ops::SeqExpandGradKernel<paddle::platform::CPUPlace, float>);

paddle/operators/seq_expand_op.cu

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#define EIGEN_USE_GPU
16+
#include "paddle/operators/seq_expand_op.h"
17+
18+
namespace ops = paddle::operators;
19+
REGISTER_OP_GPU_KERNEL(seq_expand,
20+
ops::SeqExpandKernel<paddle::platform::GPUPlace, float>);
21+
REGISTER_OP_GPU_KERNEL(
22+
seq_expand_grad,
23+
ops::SeqExpandGradKernel<paddle::platform::GPUPlace, float>);

paddle/operators/seq_expand_op.h

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#pragma once
16+
17+
#include "paddle/framework/op_registry.h"
18+
#include "paddle/memory/memcpy.h"
19+
#include "unsupported/Eigen/CXX11/Tensor"
20+
21+
namespace paddle {
22+
namespace operators {
23+
24+
using LoDTensor = framework::LoDTensor;
25+
26+
template <typename Place, typename T>
27+
class SeqExpandKernel : public framework::OpKernel<T> {
28+
public:
29+
void Compute(const framework::ExecutionContext& context) const override {
30+
auto* x = context.Input<LoDTensor>("X");
31+
auto* out = context.Output<LoDTensor>("Out");
32+
const T* x_data = x->data<T>();
33+
auto x_dims = x->dims();
34+
auto* y = context.Input<LoDTensor>("Y");
35+
PADDLE_ENFORCE_EQ(x_dims[0], y->lod().back().size() - 1,
36+
"The size of last lod level in Input(Y)"
37+
"must be equal to dims[0] of Input(X).");
38+
out->set_lod(y->lod());
39+
auto place = context.GetEigenDevice<Place>();
40+
size_t element_len = framework::product(x_dims) / x_dims[0];
41+
T* out_data = out->mutable_data<T>(context.GetPlace());
42+
auto out_starts = out->lod().back();
43+
44+
for (size_t i = 0; i < out_starts.size() - 1; i++) {
45+
int scale = out_starts[i + 1] - out_starts[i];
46+
Eigen::TensorMap<
47+
Eigen::Tensor<const T, 2, Eigen::RowMajor, Eigen::DenseIndex>>
48+
x_t(x_data, 1, element_len);
49+
Eigen::TensorMap<Eigen::Tensor<T, 2, Eigen::RowMajor, Eigen::DenseIndex>>
50+
out_t(out_data, scale, element_len);
51+
Eigen::array<int, 2> cast({scale, 1});
52+
out_t.device(place) = x_t.broadcast(cast);
53+
x_data += element_len;
54+
out_data += element_len * scale;
55+
}
56+
}
57+
};
58+
59+
/*
60+
*Given Grad(Out)
61+
*
62+
* Grad(Out).lod = [[0, 2],
63+
* [0, 3, 6]]
64+
* Grad(Out).data = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
65+
* Then
66+
* Grad(X).data = [(0.1 + 0.2 + 0.3), (0.4 + 0.5 + 0.6)]
67+
* = [0.6, 1.5]
68+
* Grad(X).lod = Input(X).lod
69+
*
70+
* */
71+
template <typename Place, typename T>
72+
class SeqExpandGradKernel : public framework::OpKernel<T> {
73+
public:
74+
void Compute(const framework::ExecutionContext& context) const override {
75+
auto* d_out = context.Input<LoDTensor>(framework::GradVarName("Out"));
76+
auto* x = context.Input<LoDTensor>("X");
77+
auto* out = context.Input<LoDTensor>("Out");
78+
auto* d_x = context.Output<LoDTensor>(framework::GradVarName("X"));
79+
auto out_last_level = out->lod().back();
80+
d_x->set_lod(x->lod());
81+
const T* d_out_data = d_out->data<T>();
82+
T* d_x_data = d_x->mutable_data<T>(context.GetPlace());
83+
size_t element_len = d_out->numel() / d_out->dims()[0];
84+
for (size_t i = 0; i < out_last_level.size() - 1; ++i) {
85+
size_t repeat = out_last_level[i + 1] - out_last_level[i];
86+
Eigen::TensorMap<
87+
Eigen::Tensor<const T, 2, Eigen::RowMajor, Eigen::DenseIndex>>
88+
d_out_t(d_out_data, static_cast<int>(repeat), element_len);
89+
Eigen::TensorMap<Eigen::Tensor<T, 1, Eigen::RowMajor, Eigen::DenseIndex>>
90+
d_x_t(d_x_data, static_cast<int>(element_len));
91+
auto place = context.GetEigenDevice<Place>();
92+
d_x_t.device(place) = d_out_t.sum(Eigen::array<int, 1>({{0}}));
93+
d_out_data += (repeat * element_len);
94+
d_x_data += element_len;
95+
}
96+
}
97+
};
98+
99+
} // namespace operators
100+
} // namespace paddle

paddle/operators/sequence_concat_op.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,20 @@ class SequenceConcatOpMaker : public framework::OpProtoAndCheckerMaker {
6868
"The level should be less than the level number of inputs.")
6969
.SetDefault(0);
7070
AddComment(R"DOC(
71-
The sequence_concat operator concatenates multiple LoDTensors.
72-
It only supports sequence (LoD Tensor with level number is 1)
71+
The sequence_concat operator concatenates multiple LoDTensors.
72+
It only supports sequence (LoD Tensor with level number is 1)
7373
or a nested sequence (LoD tensor with level number is 2) as its input.
7474
- Case1:
7575
If the axis is other than 0(here, axis is 1 and level is 1),
76-
each input should have the same LoD information and the LoD
76+
each input should have the same LoD information and the LoD
7777
information of the output keeps the same as the input.
7878
7979
LoD(x0) = {{0,2,4}, {0,1,2,3,4}}; Dims(x0) = (4,3,4)
8080
LoD(x1) = {{0,2,4}, {0,1,2,3,4}}; Dims(x1) = (4,4,4)
8181
LoD(Out) = {{0,2,4}, {0,1,2,3,4}}; Dims(Out) = (4,7,4)
8282
8383
- Case2:
84-
If the axis is 0(here, leve is 0), the inputs are concatenated along
84+
If the axis is 0(here, leve is 0), the inputs are concatenated along
8585
time steps, the LoD information of the output need to re-compute.
8686
8787
LoD(x0) = {{0,2,4}, {0,1,2,3,4}}; Dims(x0) = (4,3,4)
@@ -94,7 +94,7 @@ class SequenceConcatOpMaker : public framework::OpProtoAndCheckerMaker {
9494
LoD(x0) = {{0,2,4}, {0,1,2,3,4}}; Dims(x0) = (4,3,4)
9595
LoD(x1) = {{0,3,5}, {0,1,3,4,5}}; Dims(x1) = (5,3,4)
9696
LoD(Out) = {{0,5,9}, {0,2,5,7,9}}; Dims(Out) = (9,3,4)
97-
97+
9898
NOTE: The levels of all the inputs should be the same.
9999
)DOC");
100100
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import unittest
2+
import numpy as np
3+
from op_test import OpTest
4+
5+
6+
class TestSeqExpand(OpTest):
7+
def set_data(self):
8+
x_data = np.random.uniform(0.1, 1, [3, 1]).astype('float32')
9+
y_data = np.random.uniform(0.1, 1, [8, 1]).astype('float32')
10+
y_lod = [[0, 1, 4, 8]]
11+
self.inputs = {'X': x_data, 'Y': (y_data, y_lod)}
12+
13+
def compute(self):
14+
x = self.inputs['X']
15+
x_data, x_lod = x if type(x) == tuple else (x, None)
16+
n = 1 + x_data.shape[0] if not x_lod else len(x_lod[0])
17+
y_data, y_lod = self.inputs['Y']
18+
repeats = [((y_lod[-1][i + 1] - y_lod[-1][i]))
19+
for i in range(len(y_lod[-1]) - 1)]
20+
out = x_data.repeat(repeats, axis=0)
21+
self.outputs = {'Out': out}
22+
23+
def setUp(self):
24+
self.op_type = 'seq_expand'
25+
self.set_data()
26+
self.compute()
27+
28+
def test_check_output(self):
29+
self.check_output()
30+
31+
def test_check_grad(self):
32+
self.check_grad(["X"], "Out")
33+
34+
35+
class TestSeqExpandCase1(TestSeqExpand):
36+
def set_data(self):
37+
x_data = np.random.uniform(0.1, 1, [5, 1]).astype('float32')
38+
x_lod = [[0, 2, 5]]
39+
y_data = np.random.uniform(0.1, 1, [13, 1]).astype('float32')
40+
y_lod = [[0, 2, 5], [0, 2, 4, 7, 10, 13]]
41+
self.inputs = {'X': (x_data, x_lod), 'Y': (y_data, y_lod)}
42+
43+
44+
class TestSeqExpandCase2(TestSeqExpand):
45+
def set_data(self):
46+
x_data = np.random.uniform(0.1, 1, [1, 2, 2]).astype('float32')
47+
x_lod = [[0, 1]]
48+
y_data = np.random.uniform(0.1, 1, [2, 2, 2]).astype('float32')
49+
y_lod = [[0, 2]]
50+
self.inputs = {'X': (x_data, x_lod), 'Y': (y_data, y_lod)}
51+
52+
53+
class TestSeqExpandCase3(TestSeqExpand):
54+
def set_data(self):
55+
x_data = np.random.uniform(0.1, 1, [4, 1]).astype('float32')
56+
x_lod = [[0, 1, 2, 3, 4]]
57+
y_data = np.random.uniform(0.1, 1, [6, 1]).astype('float32')
58+
y_lod = [[0, 2, 4, 4, 6]]
59+
self.inputs = {'X': (x_data, x_lod), 'Y': (y_data, y_lod)}
60+
61+
62+
if __name__ == '__main__':
63+
unittest.main()

0 commit comments

Comments
 (0)