|
| 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_unpad_op.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | +namespace operators { |
| 19 | + |
| 20 | +class SequenceUnpadOp : public framework::OperatorWithKernel { |
| 21 | + public: |
| 22 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 23 | + |
| 24 | + protected: |
| 25 | + void InferShape(framework::InferShapeContext* ctx) const override { |
| 26 | + PADDLE_ENFORCE(ctx->HasInput("X"), |
| 27 | + "Input(X) of SequenceUnpadOp should not be null."); |
| 28 | + PADDLE_ENFORCE(ctx->HasInput("Length"), |
| 29 | + "Input(Length) of SequenceUnpadOp should not be null."); |
| 30 | + PADDLE_ENFORCE(ctx->HasOutput("Out"), |
| 31 | + "Output(Out) of SequenceUnpadOp should not be null."); |
| 32 | + |
| 33 | + auto x_dims = ctx->GetInputDim("X"); |
| 34 | + PADDLE_ENFORCE_GE(x_dims.size(), 2, |
| 35 | + "The rank of Input(X) can't be less than 2."); |
| 36 | + |
| 37 | + auto len_dims = ctx->GetInputDim("Length"); |
| 38 | + PADDLE_ENFORCE(len_dims.size() == 2 && len_dims[1] == 1, |
| 39 | + "The shape of Input(Length) should be [batch_size, 1]."); |
| 40 | + PADDLE_ENFORCE( |
| 41 | + len_dims[0] == x_dims[0], |
| 42 | + "Input(X) and Input(Length) should have the same first dimension."); |
| 43 | + |
| 44 | + int64_t out_dim_0 = -1; |
| 45 | + if (ctx->IsRuntime()) { |
| 46 | + out_dim_0 = x_dims[0] * x_dims[1]; |
| 47 | + } |
| 48 | + |
| 49 | + std::vector<int64_t> out_dims_vec{out_dim_0}; |
| 50 | + if (x_dims.size() == 2) { |
| 51 | + out_dims_vec.push_back(1); |
| 52 | + } else { |
| 53 | + for (size_t i = 2; i < x_dims.size(); ++i) { |
| 54 | + out_dims_vec.push_back(x_dims[i]); |
| 55 | + } |
| 56 | + } |
| 57 | + ctx->SetOutputDim("Out", framework::make_ddim(out_dims_vec)); |
| 58 | + } |
| 59 | + |
| 60 | + protected: |
| 61 | + framework::OpKernelType GetExpectedKernelType( |
| 62 | + const framework::ExecutionContext& ctx) const override { |
| 63 | + auto data_type = framework::GetDataTypeOfVar(ctx.InputVar("X")); |
| 64 | + return framework::OpKernelType(data_type, ctx.device_context()); |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | +class SequenceUnpadOpMaker : public framework::OpProtoAndCheckerMaker { |
| 69 | + public: |
| 70 | + void Make() override { |
| 71 | + AddInput("X", |
| 72 | + "(LoDTensor, default LoDTensor<float>) Input tensor which " |
| 73 | + "contains the padded sequences with equal length."); |
| 74 | + AddInput("Length", |
| 75 | + "(LoDTensor) The input tensor which specifies the actual ength of " |
| 76 | + "sequences after unpadding."); |
| 77 | + AddOutput( |
| 78 | + "Out", |
| 79 | + "(LoDTensor) The output tensor which contains unpadded sequences."); |
| 80 | + AddComment(R"DOC( |
| 81 | + Sequence Unpad Operator |
| 82 | +
|
| 83 | + This operator removes the padding data in the input sequences and convert |
| 84 | + them into sequences with actual length as output, identitied by lod |
| 85 | + information. |
| 86 | +
|
| 87 | + Example: |
| 88 | +
|
| 89 | + Given input tensor Input(X): |
| 90 | + X.data = [[ 1.0, 2.0, 3.0, 4.0, 5.0], |
| 91 | + [ 6.0, 7.0, 8.0, 9.0, 10.0], |
| 92 | + [11.0, 12.0, 13.0, 14.0, 15.0]], |
| 93 | +` |
| 94 | + in which there are 3 sequences padded to length 5, and the acutal length |
| 95 | + specified by Input(Length): |
| 96 | +
|
| 97 | + Length.data = [[2], [3], [4]], |
| 98 | +
|
| 99 | + after unpadding, Output(Out) will be: |
| 100 | +
|
| 101 | + Out.data = [[1.0, 2.0, 6.0, 7.0, 8.0, 11.0, 12.0, 13.0, 14.0]] |
| 102 | + Out.lod = [[0, 2, 5, 9]] |
| 103 | +
|
| 104 | + )DOC"); |
| 105 | + } |
| 106 | +}; |
| 107 | + |
| 108 | +class SequenceUnpadGradOp : public framework::OperatorWithKernel { |
| 109 | + public: |
| 110 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 111 | + |
| 112 | + void InferShape(framework::InferShapeContext* ctx) const override { |
| 113 | + PADDLE_ENFORCE(ctx->HasInput("X"), |
| 114 | + "Input(X) of SequenceUnpadGradOp should not be null."); |
| 115 | + PADDLE_ENFORCE( |
| 116 | + ctx->HasInput(framework::GradVarName("Out")), |
| 117 | + "Input(Out@GRAD) of SequenceUnpadGradOp should not be null."); |
| 118 | + |
| 119 | + if (ctx->HasOutput(framework::GradVarName("X"))) { |
| 120 | + ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X")); |
| 121 | + ctx->ShareLoD("X", /*->*/ framework::GradVarName("X")); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + protected: |
| 126 | + framework::OpKernelType GetExpectedKernelType( |
| 127 | + const framework::ExecutionContext& ctx) const override { |
| 128 | + auto data_type = framework::GetDataTypeOfVar(ctx.InputVar("X")); |
| 129 | + return framework::OpKernelType(data_type, ctx.device_context()); |
| 130 | + } |
| 131 | +}; |
| 132 | + |
| 133 | +} // namespace operators |
| 134 | +} // namespace paddle |
| 135 | + |
| 136 | +namespace ops = paddle::operators; |
| 137 | +REGISTER_OPERATOR(sequence_unpad, ops::SequenceUnpadOp, |
| 138 | + ops::SequenceUnpadOpMaker, |
| 139 | + paddle::framework::DefaultGradOpDescMaker<true>); |
| 140 | +REGISTER_OPERATOR(sequence_unpad_grad, ops::SequenceUnpadGradOp); |
| 141 | +REGISTER_OP_CPU_KERNEL( |
| 142 | + sequence_unpad, |
| 143 | + ops::SequenceUnpadOpKernel<paddle::platform::CPUDeviceContext, float>, |
| 144 | + ops::SequenceUnpadOpKernel<paddle::platform::CPUDeviceContext, double>, |
| 145 | + ops::SequenceUnpadOpKernel<paddle::platform::CPUDeviceContext, int>, |
| 146 | + ops::SequenceUnpadOpKernel<paddle::platform::CPUDeviceContext, int64_t>); |
| 147 | +REGISTER_OP_CPU_KERNEL( |
| 148 | + sequence_unpad_grad, |
| 149 | + ops::SequenceUnpadGradOpKernel<paddle::platform::CPUDeviceContext, float>, |
| 150 | + ops::SequenceUnpadGradOpKernel<paddle::platform::CPUDeviceContext, double>, |
| 151 | + ops::SequenceUnpadGradOpKernel<paddle::platform::CPUDeviceContext, int>, |
| 152 | + ops::SequenceUnpadGradOpKernel<paddle::platform::CPUDeviceContext, |
| 153 | + int64_t>); |
0 commit comments