|
| 1 | +/* Copyright (c) 2016 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/lod_reset_op.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | +namespace operators { |
| 19 | + |
| 20 | +class LoDResetOp : public framework::OperatorWithKernel { |
| 21 | + public: |
| 22 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 23 | + |
| 24 | + void InferShape(framework::InferShapeContext *ctx) const override { |
| 25 | + // input check |
| 26 | + PADDLE_ENFORCE(ctx->HasInput("X"), |
| 27 | + "Input(X) of LoDResetOp should not be null."); |
| 28 | + PADDLE_ENFORCE(ctx->HasOutput("Out"), |
| 29 | + "Output(Out) of LoDResetOp should not be null."); |
| 30 | + // If target LoD is not set form Input(), then it must be set from Attr(). |
| 31 | + if (!ctx->HasInput("TargetLoD")) { |
| 32 | + auto level0 = ctx->Attrs().Get<std::vector<int>>("target_lod"); |
| 33 | + PADDLE_ENFORCE(level0.size() > 1, |
| 34 | + "Target LoD is not found, should be set to be a valid one " |
| 35 | + "through Input() or Attr()."); |
| 36 | + } |
| 37 | + ctx->SetOutputDim("Out", ctx->GetInputDim("X")); |
| 38 | + } |
| 39 | + |
| 40 | + protected: |
| 41 | + framework::OpKernelType GetKernelType( |
| 42 | + const framework::ExecutionContext &ctx) const override { |
| 43 | + return framework::OpKernelType( |
| 44 | + framework::ToDataType(ctx.Input<framework::LoDTensor>("X")->type()), |
| 45 | + ctx.device_context()); |
| 46 | + } |
| 47 | +}; |
| 48 | + |
| 49 | +class LoDResetOpMaker : public framework::OpProtoAndCheckerMaker { |
| 50 | + public: |
| 51 | + LoDResetOpMaker(framework::OpProto *proto, |
| 52 | + framework::OpAttrChecker *op_checker) |
| 53 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 54 | + AddInput("X", "(LoDTensor) The input tensor of lod_reset operator."); |
| 55 | + AddInput("TargetLoD", |
| 56 | + "(Tensor, optional) The target level 0 LoD from Input().") |
| 57 | + .AsDispensable(); |
| 58 | + AddOutput("Out", "(LoDTensor) The output tensor of lod_reset operator."); |
| 59 | + AddAttr<std::vector<int>>("target_lod", |
| 60 | + "The target level 0 LoD from Attr().") |
| 61 | + .SetDefault(std::vector<int>{}); |
| 62 | + AddComment(R"DOC(LoDReset operator |
| 63 | +
|
| 64 | +Reset LoD of Input(X) into a new one specified by Input(TargetLoD) or |
| 65 | +Attr(target_lod), or set LoD for Input(X) if it doesn't have one. |
| 66 | +Currently the lod_reset operator only supports the reset of level 0 LoD. |
| 67 | +At least one of Input(TargetLoD) and Attr(target_lod) must be set, |
| 68 | +and if both of them are set, Input(TargetLoD) will be chosen as the |
| 69 | +target LoD. |
| 70 | +
|
| 71 | +An example: |
| 72 | +Given a float LoDTensor X with shape (6, 1), its transpose form represents |
| 73 | +
|
| 74 | + [1.0, 2.0, 3.0, 4.0, 5.0, 6.0], |
| 75 | +
|
| 76 | +with LoD = [[0, 2, 5, 6]] and the three (transposed) sequences look like |
| 77 | +
|
| 78 | + [1.0, 2.0], [3.0, 4.0, 5.0], [6.0]. |
| 79 | +
|
| 80 | +If target LoD = [0, 4, 6], the lod_reset operator will reset the LoD and |
| 81 | +the sequences that the LoDTensor Output(Out) contains becomes: |
| 82 | +
|
| 83 | + [1.0, 2.0, 3.0, 4.0], [5.0, 6.0]. |
| 84 | +
|
| 85 | +)DOC"); |
| 86 | + } |
| 87 | +}; |
| 88 | + |
| 89 | +class LoDResetGradOp : public framework::OperatorWithKernel { |
| 90 | + public: |
| 91 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 92 | + |
| 93 | + void InferShape(framework::InferShapeContext *ctx) const override { |
| 94 | + PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) shouldn't be null."); |
| 95 | + PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")), |
| 96 | + "Input(Out@GRAD) shouldn't be null."); |
| 97 | + ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X")); |
| 98 | + } |
| 99 | + |
| 100 | + protected: |
| 101 | + framework::OpKernelType GetKernelType( |
| 102 | + const framework::ExecutionContext &ctx) const override { |
| 103 | + return framework::OpKernelType( |
| 104 | + framework::ToDataType(ctx.Input<framework::LoDTensor>("X")->type()), |
| 105 | + ctx.device_context()); |
| 106 | + } |
| 107 | +}; |
| 108 | + |
| 109 | +} // namespace operators |
| 110 | +} // namespace paddle |
| 111 | + |
| 112 | +namespace ops = paddle::operators; |
| 113 | +REGISTER_OP(lod_reset, ops::LoDResetOp, ops::LoDResetOpMaker, lod_reset_grad, |
| 114 | + ops::LoDResetGradOp); |
| 115 | +REGISTER_OP_CPU_KERNEL(lod_reset, |
| 116 | + ops::LoDResetKernel<paddle::platform::CPUPlace, float>, |
| 117 | + ops::LoDResetKernel<paddle::platform::CPUPlace, double>); |
| 118 | +REGISTER_OP_CPU_KERNEL( |
| 119 | + lod_reset_grad, ops::LoDResetGradKernel<paddle::platform::CPUPlace, float>, |
| 120 | + ops::LoDResetGradKernel<paddle::platform::CPUPlace, double>); |
0 commit comments