Skip to content

Commit fe70c69

Browse files
pkuyymJiayiFeng
authored andcommitted
Add forward and backward.
1 parent cfe611f commit fe70c69

File tree

3 files changed

+251
-0
lines changed

3 files changed

+251
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
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/fluid/operators/sequence_pad_op.h"
16+
17+
namespace paddle {
18+
namespace operators {
19+
20+
class SequencePadOp : public framework::OperatorWithKernel {
21+
public:
22+
using framework::OperatorWithKernel::OperatorWithKernel;
23+
24+
void InferShape(framework::InferShapeContext* ctx) const override {
25+
PADDLE_ENFORCE(ctx->HasInput("X"),
26+
"Input(X) of SequencePadOp should not be null.");
27+
PADDLE_ENFORCE(ctx->HasOutput("Out"),
28+
"Output(Out) of SequencePadOp should not be null.");
29+
30+
auto x_dims = ctx->GetInputDim("X");
31+
32+
PADDLE_ENFORCE_EQ(x_dims.size(), 2,
33+
"Only support 2-D tensor, rank of Input(X) should be 2.");
34+
35+
auto out_dims = x_dims;
36+
37+
if (ctx->IsRuntime()) {
38+
framework::Variable* x_var =
39+
boost::get<framework::Variable*>(ctx->GetInputVarPtrs("X")[0]);
40+
41+
auto& x_lod = x_var->Get<LoDTensor>().lod();
42+
43+
PADDLE_ENFORCE_GE(x_lod.size(), 1,
44+
"Input(X) should be sequences containing lod.");
45+
46+
auto last_level_lod = x_lod[x_lod.size() - 1];
47+
size_t max_len = 0;
48+
49+
for (size_t i = 1; i < last_level_lod.size(); ++i) {
50+
auto seq_len = last_level_lod[i] - last_level_lod[i - 1];
51+
max_len = max_len < seq_len ? seq_len : max_len;
52+
}
53+
54+
out_dims[0] = max_len * (last_level_lod.size() - 1);
55+
} else {
56+
framework::VarDesc* x_desc =
57+
boost::get<framework::VarDesc*>(ctx->GetInputVarPtrs("X")[0]);
58+
PADDLE_ENFORCE_GE(x_desc->GetLoDLevel(), 1,
59+
"Input(X) should be sequences containing lod.");
60+
out_dims[0] = -1;
61+
}
62+
63+
ctx->SetOutputDim("Out", out_dims);
64+
}
65+
66+
protected:
67+
framework::OpKernelType GetExpectedKernelType(
68+
const framework::ExecutionContext& ctx) const override {
69+
return framework::OpKernelType(
70+
framework::ToDataType(ctx.Input<framework::LoDTensor>("X")->type()),
71+
ctx.device_context());
72+
}
73+
};
74+
75+
class SequencePadOpMaker : public framework::OpProtoAndCheckerMaker {
76+
public:
77+
SequencePadOpMaker(OpProto* proto, OpAttrChecker* op_checker)
78+
: OpProtoAndCheckerMaker(proto, op_checker) {
79+
AddInput("X",
80+
"(LoDTensor, default LoDTensor<float>) Input variable which "
81+
"should contain lod information. Length of each sequence would "
82+
"be computed from the most bottom level lod.");
83+
AddOutput("Out",
84+
"(Tensor) Output variable which would be a common tensor "
85+
"without lod. Each sequence would be padded to the maximum "
86+
"length.");
87+
AddAttr<float>("pad_value",
88+
"(float, default 0.0) Value to be padded "
89+
"to the end of each sequence.");
90+
AddComment(R"DOC(
91+
92+
)DOC");
93+
}
94+
};
95+
96+
class SequencePadGradOp : public framework::OperatorWithKernel {
97+
public:
98+
using framework::OperatorWithKernel::OperatorWithKernel;
99+
100+
void InferShape(framework::InferShapeContext* ctx) const override {
101+
PADDLE_ENFORCE(ctx->HasInput("X"),
102+
"Input(X) of SequencePadGradOp should not be null.");
103+
PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
104+
"Input(Out@GRAD) of SequencePadGradOp should not be null.");
105+
106+
if (ctx->HasOutput(framework::GradVarName("X"))) {
107+
ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
108+
ctx->ShareLoD("X", /*->*/ framework::GradVarName("X"));
109+
}
110+
}
111+
};
112+
113+
} // namespace operators
114+
} // namespace paddle
115+
116+
namespace ops = paddle::operators;
117+
REGISTER_OPERATOR(sequence_pad, ops::SequencePadOp, ops::SequencePadOpMaker,
118+
paddle::framework::DefaultGradOpDescMaker<true>);
119+
REGISTER_OPERATOR(sequence_pad_grad, ops::SequencePadGradOp);
120+
REGISTER_OP_CPU_KERNEL(
121+
sequence_pad,
122+
ops::SequencePadOpKernel<paddle::platform::CPUDeviceContext, float>,
123+
ops::SequencePadOpKernel<paddle::platform::CPUDeviceContext, double>,
124+
ops::SequencePadOpKernel<paddle::platform::CPUDeviceContext, int>,
125+
ops::SequencePadOpKernel<paddle::platform::CPUDeviceContext, int64_t>);
126+
REGISTER_OP_CPU_KERNEL(
127+
sequence_pad_grad,
128+
ops::SequencePadGradOpKernel<paddle::platform::CPUDeviceContext, float>,
129+
ops::SequencePadGradOpKernel<paddle::platform::CPUDeviceContext, double>,
130+
ops::SequencePadGradOpKernel<paddle::platform::CPUDeviceContext, int>,
131+
ops::SequencePadGradOpKernel<paddle::platform::CPUDeviceContext, int64_t>);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
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/fluid/operators/sequence_pad_op.h"
16+
17+
namespace ops = paddle::operators;
18+
REGISTER_OP_CUDA_KERNEL(
19+
sequence_pad,
20+
ops::SequencePadOpKernel<paddle::platform::CUDADeviceContext, float>);
21+
REGISTER_OP_CUDA_KERNEL(
22+
sequence_pad_grad,
23+
ops::SequencePadGradOpKernel<paddle::platform::CUDADeviceContext, float>);
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
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+
#include "paddle/fluid/framework/op_registry.h"
17+
#include "paddle/fluid/memory/memcpy.h"
18+
#include "paddle/fluid/operators/math/math_function.h"
19+
20+
namespace paddle {
21+
namespace operators {
22+
23+
using LoDTensor = framework::LoDTensor;
24+
using LoD = framework::LoD;
25+
26+
// @TODO clean code
27+
template <typename DeviceContext, typename T>
28+
class SequencePadOpKernel : public framework::OpKernel<T> {
29+
public:
30+
void Compute(const framework::ExecutionContext& ctx) const override {
31+
auto* x_ptr = ctx.Input<LoDTensor>("X");
32+
auto* out_ptr = ctx.Output<LoDTensor>("Out");
33+
34+
out_ptr->mutable_data<T>(ctx.GetPlace());
35+
36+
T pad_value = static_cast<T>(ctx.Attr<float>("pad_value"));
37+
38+
math::SetConstant<DeviceContext, T> set_func;
39+
set_func(ctx.template device_context<DeviceContext>(), out_ptr, pad_value);
40+
41+
auto& x_lod = x_ptr->lod();
42+
auto& x_last_level_lod = x_lod[x_lod.size() - 1];
43+
auto seq_num = x_last_level_lod.size() - 1;
44+
auto max_len = out_ptr->dims()[0] / seq_num;
45+
46+
PADDLE_ENFORCE_EQ(max_len * seq_num, out_ptr->dims()[0],
47+
"First dimension of `Out` should be equal to "
48+
"maximum length mulplied by sequence number.");
49+
50+
for (size_t i = 1; i < x_last_level_lod.size(); ++i) {
51+
auto x_start = x_last_level_lod[i - 1];
52+
auto x_end = x_last_level_lod[i];
53+
auto out_start = (i - 1) * max_len;
54+
auto out_end = out_start + (x_end - x_start);
55+
auto x_sub_tensor = x_ptr->Slice(x_start, x_end);
56+
auto out_sub_tensor = out_ptr->Slice(out_start, out_end);
57+
framework::TensorCopy(x_sub_tensor, ctx.GetPlace(), &out_sub_tensor);
58+
}
59+
}
60+
};
61+
62+
template <typename DeviceContext, typename T>
63+
class SequencePadGradOpKernel : public framework::OpKernel<T> {
64+
public:
65+
void Compute(const framework::ExecutionContext& ctx) const override {
66+
auto* x_ptr = ctx.Input<LoDTensor>("X");
67+
auto* g_out_ptr = ctx.Input<LoDTensor>(framework::GradVarName("Out"));
68+
auto* g_x_ptr = ctx.Output<LoDTensor>(framework::GradVarName("X"));
69+
70+
math::SetConstant<DeviceContext, T> set_func;
71+
set_func(ctx.template device_context<DeviceContext>(), g_x_ptr,
72+
static_cast<T>(0));
73+
74+
auto& x_lod = x_ptr->lod();
75+
auto& x_last_level_lod = x_lod[x_lod.size() - 1];
76+
auto seq_num = x_last_level_lod.size() - 1;
77+
int64_t max_len = g_out_ptr->dims()[0] / seq_num;
78+
79+
PADDLE_ENFORCE_EQ(max_len * seq_num, g_out_ptr->dims()[0],
80+
"First dimension of `Out` should be equal to "
81+
"maximum length mulplied by sequence number.");
82+
83+
for (size_t i = 1; i < x_last_level_lod.size(); ++i) {
84+
auto x_start = x_last_level_lod[i - 1];
85+
auto x_end = x_last_level_lod[i];
86+
auto out_start = (i - 1) * max_len;
87+
auto out_end = out_start + (x_end - x_start);
88+
89+
auto g_out_sub = g_out_ptr->Slice(out_start, out_end);
90+
auto g_x_sub = g_x_ptr->Slice(x_start, x_end);
91+
framework::TensorCopy(g_x_sub, ctx.GetPlace(), &g_out_sub);
92+
}
93+
}
94+
};
95+
96+
} // namespace operators
97+
} // namespace paddle

0 commit comments

Comments
 (0)