|
| 1 | +/* Copyright (c) 2018 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/sequence_reshape_op.h" |
| 16 | +#include "paddle/framework/ddim.h" |
| 17 | + |
| 18 | +namespace paddle { |
| 19 | +namespace operators { |
| 20 | + |
| 21 | +class SequenceReshapeOp : public framework::OperatorWithKernel { |
| 22 | + public: |
| 23 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 24 | + void InferShape(framework::InferShapeContext* ctx) const override { |
| 25 | + PADDLE_ENFORCE(ctx->HasInput("X"), |
| 26 | + "Input(X) of SequenceReshapeOp should not be null."); |
| 27 | + PADDLE_ENFORCE(ctx->HasOutput("Out"), |
| 28 | + "Output(Out) of SequenceReshapeOp should not be null."); |
| 29 | + auto x_dims = ctx->GetInputDim("X"); |
| 30 | + auto x_numel = product(x_dims); |
| 31 | + PADDLE_ENFORCE_EQ(x_dims.size(), 2U, "Rank of Input(X) should be 2."); |
| 32 | + int new_dim = ctx->Attrs().Get<int>("new_dim"); |
| 33 | + ctx->SetOutputDim("Out", |
| 34 | + {x_numel / new_dim, static_cast<int64_t>(new_dim)}); |
| 35 | + } |
| 36 | +}; |
| 37 | + |
| 38 | +class SequenceReshapeOpMaker : public framework::OpProtoAndCheckerMaker { |
| 39 | + public: |
| 40 | + SequenceReshapeOpMaker(OpProto* proto, OpAttrChecker* op_checker) |
| 41 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 42 | + AddInput("X", |
| 43 | + "(LoDTensor, default LoDTensor<float>) A 2-D LoDTensor with shape " |
| 44 | + "being [N, M]."); |
| 45 | + AddOutput("Out", |
| 46 | + "(LoDTensor, default LoDTensor<float>) A 2-D LoDTensor with " |
| 47 | + "shape [T, new_dim] where T is calculated based on X.lod, M and " |
| 48 | + "new_dim."); |
| 49 | + AddAttr<int>("new_dim", "Sequence dimension of the output LoDTensor."); |
| 50 | + AddComment(R"DOC( |
| 51 | +Sequence Reshape Operator. |
| 52 | +
|
| 53 | +This operator will rearrange the input sequences. The new dimension is set by |
| 54 | +attribute and length of each sequence may change longer or shorter which is |
| 55 | +decided by original length, original dimension and new dimension. The following |
| 56 | +example will help to illustrate the function of this operator: |
| 57 | +
|
| 58 | +x is a LoDTensor: |
| 59 | + x.lod = [[0, 2, 6]] |
| 60 | + x.data = [[1, 2], [3, 4], |
| 61 | + [5, 6], [7, 8], [9, 10], [11, 12]] |
| 62 | + x.dims = [6, 2] |
| 63 | +
|
| 64 | +set new_dim = 4 |
| 65 | +
|
| 66 | +then out is a LoDTensor: |
| 67 | + out.lod = [[0, 1, 3]] |
| 68 | + out.data = [[1, 2, 3, 4], |
| 69 | + [5, 6, 7, 8], [9, 10, 11, 12]] |
| 70 | + out.dims = [3, 4] |
| 71 | +
|
| 72 | +Currently, only 1-level LoDTensor is supported and please make sure (original |
| 73 | +length * original dimension) can be divided by new_dim with no remainder for |
| 74 | +each sequence. |
| 75 | +
|
| 76 | +)DOC"); |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +class SequenceReshapeGradOp : public framework::OperatorWithKernel { |
| 81 | + public: |
| 82 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 83 | + |
| 84 | + void InferShape(framework::InferShapeContext* ctx) const override { |
| 85 | + PADDLE_ENFORCE( |
| 86 | + ctx->HasInput(framework::GradVarName("Out")), |
| 87 | + "Input(Out@GRAD) of SequenceReshapeGradOp should not be null."); |
| 88 | + PADDLE_ENFORCE(ctx->HasInput("X"), |
| 89 | + "Input(X) of SequenceReshapeGradOp should not be null."); |
| 90 | + |
| 91 | + ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X")); |
| 92 | + ctx->ShareLoD("X", /*->*/ framework::GradVarName("X")); |
| 93 | + } |
| 94 | +}; |
| 95 | + |
| 96 | +class SequenceReshapeGradOpMaker : public framework::SingleGradOpDescMaker { |
| 97 | + public: |
| 98 | + using framework::SingleGradOpDescMaker::SingleGradOpDescMaker; |
| 99 | + |
| 100 | + protected: |
| 101 | + std::unique_ptr<framework::OpDesc> Apply() const override { |
| 102 | + auto* op_desc_ptr = new framework::OpDesc(); |
| 103 | + op_desc_ptr->SetType("sequence_reshape_grad"); |
| 104 | + op_desc_ptr->SetInput("X", Input("X")); |
| 105 | + op_desc_ptr->SetInput(framework::GradVarName("Out"), OutputGrad("Out")); |
| 106 | + op_desc_ptr->SetOutput(framework::GradVarName("X"), InputGrad("X")); |
| 107 | + op_desc_ptr->SetAttrMap(Attrs()); |
| 108 | + return std::unique_ptr<framework::OpDesc>(op_desc_ptr); |
| 109 | + } |
| 110 | +}; |
| 111 | + |
| 112 | +} // namespace operators |
| 113 | +} // namespace paddle |
| 114 | + |
| 115 | +namespace ops = paddle::operators; |
| 116 | +REGISTER_OPERATOR(sequence_reshape, ops::SequenceReshapeOp, |
| 117 | + ops::SequenceReshapeOpMaker, ops::SequenceReshapeGradOpMaker); |
| 118 | +REGISTER_OPERATOR(sequence_reshape_grad, ops::SequenceReshapeGradOp); |
| 119 | +REGISTER_OP_CPU_KERNEL( |
| 120 | + sequence_reshape, |
| 121 | + ops::SequenceReshapeKernel<paddle::platform::CPUDeviceContext, float>, |
| 122 | + ops::SequenceReshapeKernel<paddle::platform::CPUDeviceContext, double>, |
| 123 | + ops::SequenceReshapeKernel<paddle::platform::CPUDeviceContext, int>, |
| 124 | + ops::SequenceReshapeKernel<paddle::platform::CPUDeviceContext, int64_t>); |
| 125 | +REGISTER_OP_CPU_KERNEL( |
| 126 | + sequence_reshape_grad, |
| 127 | + ops::SequenceReshapeGradKernel<paddle::platform::CPUDeviceContext, float>, |
| 128 | + ops::SequenceReshapeGradKernel<paddle::platform::CPUDeviceContext, double>, |
| 129 | + ops::SequenceReshapeGradKernel<paddle::platform::CPUDeviceContext, int64_t>, |
| 130 | + ops::SequenceReshapeGradKernel<paddle::platform::CPUDeviceContext, int>); |
0 commit comments