lod_reset_op.cc 5.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2

L
Luo Tao 已提交
3 4 5
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
8

L
Luo Tao 已提交
9 10 11 12 13
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
14

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/lod_reset_op.h"
16 17 18 19 20 21 22 23 24 25 26 27 28

namespace paddle {
namespace operators {

class LoDResetOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext *ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("X"),
                   "Input(X) of LoDResetOp should not be null.");
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
                   "Output(Out) of LoDResetOp should not be null.");
29 30

    if (!ctx->HasInput("Y")) {
31
      auto level0 = ctx->Attrs().Get<std::vector<int>>("target_lod");
32 33 34
      PADDLE_ENFORCE_GT(level0.size(), 1,
                        "If Input(Y) is not provided, the target lod should be "
                        "specified by attribute `target_lod`.");
35 36 37 38 39
    }
    ctx->SetOutputDim("Out", ctx->GetInputDim("X"));
  }

 protected:
40
  framework::OpKernelType GetExpectedKernelType(
41 42 43 44 45 46 47 48 49
      const framework::ExecutionContext &ctx) const override {
    return framework::OpKernelType(
        framework::ToDataType(ctx.Input<framework::LoDTensor>("X")->type()),
        ctx.device_context());
  }
};

class LoDResetOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
50
  LoDResetOpMaker(OpProto *proto, OpAttrChecker *op_checker)
51
      : OpProtoAndCheckerMaker(proto, op_checker) {
52 53 54 55 56 57 58 59
    AddInput("X",
             "(Tensor, LoDTensor) Input variable of LoDResetOp which "
             "could be a Tensor or LoDTensor, where the data of output "
             "variable inherits from.");
    AddInput("Y",
             "(Tensor, LoDTensor, optional) If provided, lod of Input(Y) would "
             "be considered as the target lod first, otherwise data of "
             "Input(Y) would be considered as the target lod.")
60
        .AsDispensable();
61 62 63
    AddOutput("Out",
              "(LoDTensor) Output variable of LoDResetOp which should be a "
              "LoDTensor.");
64 65 66 67 68
    AddAttr<std::vector<int>>("target_lod",
                              "The target level 0 LoD from Attr().")
        .SetDefault(std::vector<int>{});
    AddComment(R"DOC(LoDReset operator

69 70 71 72 73
Set LoD of `X` to a new one specified by `Y` or attribute `target_lod`. When `Y`
provided, `Y.lod` would be considered as target LoD first, otherwise `Y.data`
would be considered as target LoD. If `Y` is not provided, target LoD should be
specified by attribute `target_lod`. If target LoD is specified by `Y.data` or
`target_lod`, only one level LoD is supported.
74 75 76

An example:

77 78 79 80
Given a 1-level LoDTensor input(X)
    X.lod =  [[ 0,      2,                   5     6 ]]
    X.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
    X.dims = [6, 1]
81

82
target_lod: [0, 4, 6]
83

84 85 86 87
then we get an 1-level LoDTensor
    Out.lod =  [[ 0,                   4,            6 ]]
    Out.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
    Out.dims = [6, 1]
88 89 90 91 92 93 94 95 96 97

)DOC");
  }
};

class LoDResetGradOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext *ctx) const override {
98 99
    PADDLE_ENFORCE(ctx->HasInput("X"),
                   "Input(X) of LoDResetGradOp should not be null.");
100
    PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
101 102 103 104 105 106 107
                   "Input(Out@Grad) of LoDResetGradOp should not be null.");

    auto x_grad_name = framework::GradVarName("X");
    if (ctx->HasOutput(x_grad_name)) {
      ctx->SetOutputDim(x_grad_name, ctx->GetInputDim("X"));
      ctx->ShareLoD("X", /*->*/ x_grad_name);
    }
108 109 110
  }

 protected:
111
  framework::OpKernelType GetExpectedKernelType(
112 113 114 115 116 117 118 119 120 121 122 123 124
      const framework::ExecutionContext &ctx) const override {
    return framework::OpKernelType(
        framework::ToDataType(ctx.Input<framework::LoDTensor>("X")->type()),
        ctx.device_context());
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP(lod_reset, ops::LoDResetOp, ops::LoDResetOpMaker, lod_reset_grad,
            ops::LoDResetGradOp);
125 126 127 128 129
REGISTER_OP_CPU_KERNEL(
    lod_reset, ops::LoDResetKernel<paddle::platform::CPUPlace, float>,
    ops::LoDResetKernel<paddle::platform::CPUPlace, double>,
    ops::LoDResetKernel<paddle::platform::CPUPlace, int>,
    ops::LoDResetKernel<paddle::platform::CPUPlace, int64_t>);
130 131
REGISTER_OP_CPU_KERNEL(
    lod_reset_grad, ops::LoDResetGradKernel<paddle::platform::CPUPlace, float>,
132 133 134
    ops::LoDResetGradKernel<paddle::platform::CPUPlace, double>,
    ops::LoDResetGradKernel<paddle::platform::CPUPlace, int>,
    ops::LoDResetGradKernel<paddle::platform::CPUPlace, int64_t>);