|
| 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>); |
0 commit comments