|
| 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_expand_as_op.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | +namespace operators { |
| 19 | + |
| 20 | +using framework::LoDTensor; |
| 21 | + |
| 22 | +class SequenceExpandAsOp : 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 | + "Input(X) of SequenceExpandAsOp should not be null."); |
| 30 | + PADDLE_ENFORCE(ctx->HasInput("Y"), |
| 31 | + "Input(Y) of SequenceExpandAsOp should not be null."); |
| 32 | + PADDLE_ENFORCE(ctx->HasOutput("Out"), |
| 33 | + "Output(Out) of SequenceExpandAsOp should not be null."); |
| 34 | + |
| 35 | + auto x_dims = ctx->GetInputDim("X"); |
| 36 | + auto out_dims = x_dims; |
| 37 | + |
| 38 | + PADDLE_ENFORCE_GE(x_dims.size(), 2, |
| 39 | + "Dimension number of Input(X) should be at least 2."); |
| 40 | + |
| 41 | + if (ctx->IsRuntime()) { |
| 42 | + framework::Variable* x_var = |
| 43 | + boost::get<framework::Variable*>(ctx->GetInputVarPtrs("X")[0]); |
| 44 | + framework::Variable* y_var = |
| 45 | + boost::get<framework::Variable*>(ctx->GetInputVarPtrs("Y")[0]); |
| 46 | + |
| 47 | + auto& x_dim = x_var->Get<LoDTensor>().dims(); |
| 48 | + auto& y_lod = y_var->Get<LoDTensor>().lod(); |
| 49 | + |
| 50 | + PADDLE_ENFORCE_EQ(y_lod.size(), 1, |
| 51 | + "Level number of Input(Y)'s lod should be 1."); |
| 52 | + |
| 53 | + PADDLE_ENFORCE_EQ(static_cast<size_t>(x_dim[0]), y_lod[0].size() - 1, |
| 54 | + "The first dimension of Input(X) should be equal " |
| 55 | + "to the size of Input(Y)'s 0 level lod."); |
| 56 | + |
| 57 | + int64_t out_first_dim = 0; |
| 58 | + if (y_lod[0].size() <= 1) { |
| 59 | + out_first_dim = x_dims[0]; |
| 60 | + } else { |
| 61 | + for (size_t i = 1; i < y_lod[0].size(); ++i) { |
| 62 | + out_first_dim += (y_lod[0][i] - y_lod[0][i - 1]); |
| 63 | + } |
| 64 | + } |
| 65 | + out_dims[0] = out_first_dim; |
| 66 | + } else { |
| 67 | + out_dims[0] = -1; |
| 68 | + } |
| 69 | + |
| 70 | + ctx->SetOutputDim("Out", out_dims); |
| 71 | + ctx->ShareLoD("Y", /*->*/ "Out"); |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +class SequenceExpandAsOpMaker : public framework::OpProtoAndCheckerMaker { |
| 76 | + public: |
| 77 | + void Make() override { |
| 78 | + AddInput("X", |
| 79 | + "(LoDTensor, default LoDTensor<float>) A 2-D LoDTensor whose lod " |
| 80 | + "level is at most 1."); |
| 81 | + AddInput("Y", |
| 82 | + "(LoDTensor, default LoDTensor<float>) Referred LoDTensor whose " |
| 83 | + "lod (specified level) is referred by Input(X)."); |
| 84 | + AddOutput("Out", |
| 85 | + "(LodTensor, default LoDTensor<float>) Output LoDTensor which is " |
| 86 | + "generated from Input(X) by referring lod of Input(Y)."); |
| 87 | + AddComment(R"DOC( |
| 88 | +Sequence Expand As Operator. |
| 89 | +
|
| 90 | +This operator expands `X` according to the zeroth level lod of `Y`. Current |
| 91 | +implementation requires the level number of Input(Y)'s lod should be 1, and |
| 92 | +the first dimension of Input(X) should be equal to the size of Input(Y)'s zeroth |
| 93 | +level lod, and lod of Input(X) is not considered. |
| 94 | +
|
| 95 | +Following are cases to better explain how this works: |
| 96 | +
|
| 97 | +Case 1: |
| 98 | +
|
| 99 | +Given a 1-level LoDTensor input(X) |
| 100 | + X.data = [[a], [b], [c], [d]] |
| 101 | + X.dims = [4, 1] |
| 102 | +and input(Y) |
| 103 | + Y.lod = [[0, 3, 6, 7, 8]] |
| 104 | +ref_level: 0 |
| 105 | +then we get 1-level LoDTensor |
| 106 | + Out.lod = [[0, 3, 6, 7, 8]] |
| 107 | + Out.data = [[a], [a], [a], [b], [b], [b], [c], [d]] |
| 108 | + Out.dims = [8, 1] |
| 109 | +
|
| 110 | +Case 2: |
| 111 | +
|
| 112 | +Given a common Tensor input(X) |
| 113 | + X.data = [[a, b], [c, d], [e, f]] |
| 114 | + X.dims = [3, 2] |
| 115 | +and input(Y) |
| 116 | + Y.lod = [[0, 2, 3, 6]] |
| 117 | +ref_level: 0 |
| 118 | +then we get a common LoDTensor |
| 119 | + Out.lod = [[0, 2, 3, 6]] |
| 120 | + Out.data = [[a, b], [a, b] [c, d], [e, f], [e, f], [e, f]] |
| 121 | + Out.dims = [6, 2] |
| 122 | +
|
| 123 | +)DOC"); |
| 124 | + } |
| 125 | +}; |
| 126 | + |
| 127 | +class SequenceExpandAsOpGrad : public framework::OperatorWithKernel { |
| 128 | + public: |
| 129 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 130 | + |
| 131 | + protected: |
| 132 | + void InferShape(framework::InferShapeContext* ctx) const override { |
| 133 | + PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should not be null."); |
| 134 | + PADDLE_ENFORCE(ctx->HasInput("Out"), "Input(Out) should not be null."); |
| 135 | + PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")), |
| 136 | + "Input(Out@GRAD) should not be null."); |
| 137 | + |
| 138 | + auto x_dims = ctx->GetInputDim("X"); |
| 139 | + auto x_grad_name = framework::GradVarName("X"); |
| 140 | + |
| 141 | + if (ctx->HasOutput(x_grad_name)) { |
| 142 | + ctx->SetOutputDim(x_grad_name, x_dims); |
| 143 | + ctx->ShareLoD("X", x_grad_name); |
| 144 | + } |
| 145 | + } |
| 146 | +}; |
| 147 | + |
| 148 | +} // namespace operators |
| 149 | +} // namespace paddle |
| 150 | + |
| 151 | +namespace ops = paddle::operators; |
| 152 | +REGISTER_OPERATOR(sequence_expand_as, ops::SequenceExpandAsOp, |
| 153 | + ops::SequenceExpandAsOpMaker, |
| 154 | + paddle::framework::DefaultGradOpDescMaker<true>); |
| 155 | +REGISTER_OPERATOR(sequence_expand_as_grad, ops::SequenceExpandAsOpGrad); |
| 156 | +REGISTER_OP_CPU_KERNEL( |
| 157 | + sequence_expand_as, |
| 158 | + ops::SequenceExpandAsKernel<paddle::platform::CPUDeviceContext, float>, |
| 159 | + ops::SequenceExpandAsKernel<paddle::platform::CPUDeviceContext, double>, |
| 160 | + ops::SequenceExpandAsKernel<paddle::platform::CPUDeviceContext, int>, |
| 161 | + ops::SequenceExpandAsKernel<paddle::platform::CPUDeviceContext, int64_t>); |
| 162 | +REGISTER_OP_CPU_KERNEL( |
| 163 | + sequence_expand_as_grad, |
| 164 | + ops::SequenceExpandAsGradKernel<paddle::platform::CPUDeviceContext, float>, |
| 165 | + ops::SequenceExpandAsGradKernel<paddle::platform::CPUDeviceContext, double>, |
| 166 | + ops::SequenceExpandAsGradKernel<paddle::platform::CPUDeviceContext, int>, |
| 167 | + ops::SequenceExpandAsGradKernel<paddle::platform::CPUDeviceContext, |
| 168 | + int64_t>); |
0 commit comments