lod_reset_op.cc 7.4 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"
S
sneaxiy 已提交
16
#include <memory>
17 18 19 20 21 22 23 24 25 26 27 28 29

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.");
30 31

    if (!ctx->HasInput("Y")) {
32
      auto level0 = ctx->Attrs().Get<std::vector<int>>("target_lod");
33
      PADDLE_ENFORCE_GT(level0.size(), 0,
Y
yangyaming 已提交
34
                        "If Input(Y) not provided, the target lod should be "
35
                        "specified by attribute `target_lod`.");
36
    } else if (ctx->IsRuntime()) {
H
Hongyu Liu 已提交
37
      ctx->ShareLoD("Y", "Out");
P
phlrain 已提交
38 39
    }

40 41 42 43
    ctx->SetOutputDim("Out", ctx->GetInputDim("X"));
  }

 protected:
44
  framework::OpKernelType GetExpectedKernelType(
45
      const framework::ExecutionContext &ctx) const override {
Y
Yu Yang 已提交
46 47
    return framework::OpKernelType(ctx.Input<framework::LoDTensor>("X")->type(),
                                   ctx.device_context());
48 49 50
  }
};

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
class LoDResetOpVarTypeInference : public framework::VarTypeInference {
 public:
  void operator()(framework::InferVarTypeContext *ctx) const override {
    auto x_var_name = ctx->Input("X").front();
    auto out_var_name = ctx->Output("Out").front();
    if (ctx->HasInput("Y")) {
      auto y_var_name = ctx->Input("Y").front();
      auto y_lod_level = std::max(ctx->GetLoDLevel(y_var_name), 1);
      ctx->SetLoDLevel(out_var_name, y_lod_level);
    } else {
      ctx->SetLoDLevel(out_var_name, 1);
    }
    ctx->SetDataType(out_var_name, ctx->GetDataType(x_var_name));
    ctx->SetType(out_var_name, paddle::framework::proto::VarType::LOD_TENSOR);
  }
};

68 69
class LoDResetOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
70
  void Make() override {
71 72 73 74 75
    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",
Y
yangyaming 已提交
76 77 78 79
             "(Tensor, LoDTensor, optional) If provided and Y is LoDTensor, "
             "lod of Input(Y) would be considered as the target lod first, "
             "otherwise data of Input(Y) would be considered as the "
             "target lod.")
80
        .AsDispensable();
81 82 83
    AddOutput("Out",
              "(LoDTensor) Output variable of LoDResetOp which should be a "
              "LoDTensor.");
84 85 86 87 88
    AddAttr<std::vector<int>>("target_lod",
                              "The target level 0 LoD from Attr().")
        .SetDefault(std::vector<int>{});
    AddComment(R"DOC(LoDReset operator

89
Set LoD of `X` to a new one specified by `Y` or attribute `target_lod`. When `Y`
Y
yangyaming 已提交
90 91 92 93 94
provided and `Y` is a LoDTensor, `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.
95

Y
yangyaming 已提交
96
Example 1:
97

Y
yangyaming 已提交
98 99
Given a 1-level LoDTensor input(X):
    X.lod =  [[ 0,     2,                   5      6 ]]
100 101
    X.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
    X.dims = [6, 1]
102

Y
yangyaming 已提交
103
attr(target_lod): [0, 4, 6]
104

Y
yangyaming 已提交
105
then we get a 1-level LoDTensor:
106 107 108
    Out.lod =  [[ 0,                   4,            6 ]]
    Out.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
    Out.dims = [6, 1]
109

Y
yangyaming 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
Example 2:

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]

input(Y) is a Tensor:
    Y.data = [[0, 2, 6]]
    Y.dims = [1, 3]

then we get a 1-level LoDTensor:
    Out.lod =  [[ 0,     2,                          6 ]]
    Out.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
    Out.dims = [6, 1]

Example 3:

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]

input(Y) is a 2-level LoDTensor:
    Y.lod =  [[0, 2, 4], [0, 2, 5, 6]]
    Y.data = [[1.1], [2.1], [3.1], [4.1], [5.1], [6.1]]
    Y.dims = [6, 1]

then we get a 2-level LoDTensor:
    Out.lod =  [[0, 2, 4], [0, 2, 5, 6]]
    Out.data = [[1.0], [2.0], [3.0], [4.0], [5.0], [6.0]]
    Out.dims = [6, 1]

143 144 145 146 147 148 149 150 151
)DOC");
  }
};

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

  void InferShape(framework::InferShapeContext *ctx) const override {
152 153
    PADDLE_ENFORCE(ctx->HasInput("X"),
                   "Input(X) of LoDResetGradOp should not be null.");
154
    PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
155 156 157 158 159 160 161
                   "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);
    }
162 163 164
  }

 protected:
165
  framework::OpKernelType GetExpectedKernelType(
166
      const framework::ExecutionContext &ctx) const override {
S
sneaxiy 已提交
167 168 169
    return framework::OpKernelType(
        ctx.Input<framework::LoDTensor>(framework::GradVarName("Out"))->type(),
        ctx.device_context());
170 171 172
  }
};

S
sneaxiy 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
class LoDResetGradDescMaker : public framework::SingleGradOpDescMaker {
 public:
  using framework::SingleGradOpDescMaker::SingleGradOpDescMaker;

 protected:
  std::unique_ptr<framework::OpDesc> Apply() const override {
    std::unique_ptr<framework::OpDesc> op(new framework::OpDesc());
    op->SetType("lod_reset_grad");
    op->SetInput(framework::GradVarName("Out"), OutputGrad("Out"));
    op->SetInput("X", Input("X"));
    op->SetOutput(framework::GradVarName("X"), InputGrad("X"));
    op->SetAttrMap(Attrs());
    return op;
  }
};

DECLARE_NO_NEED_BUFFER_VARS_INFERENCE(LoDResetGradNoNeedBufferVarInference,
                                      "X");

192 193 194 195
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
Y
Yang Yang 已提交
196
REGISTER_OPERATOR(lod_reset, ops::LoDResetOp, ops::LoDResetOpMaker,
197
                  ops::LoDResetGradDescMaker, ops::LoDResetOpVarTypeInference);
S
sneaxiy 已提交
198 199
REGISTER_OPERATOR(lod_reset_grad, ops::LoDResetGradOp,
                  ops::LoDResetGradNoNeedBufferVarInference);
200

201 202 203 204 205
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>);
206 207
REGISTER_OP_CPU_KERNEL(
    lod_reset_grad, ops::LoDResetGradKernel<paddle::platform::CPUPlace, float>,
208 209 210
    ops::LoDResetGradKernel<paddle::platform::CPUPlace, double>,
    ops::LoDResetGradKernel<paddle::platform::CPUPlace, int>,
    ops::LoDResetGradKernel<paddle::platform::CPUPlace, int64_t>);