“e402c0ec7d813264d76841fc4972ebc631f7696e”上不存在“paddle/fluid/operators/interpolate_v2_op.h”
rnn_memory_helper_op.cc 6.5 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yang Yang(Tony) 已提交
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
Y
Yang Yang(Tony) 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
Y
Yang Yang(Tony) 已提交
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. */
Y
Yang Yang(Tony) 已提交
14

Y
Yi Wang 已提交
15 16
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
Y
Yang Yang(Tony) 已提交
17

W
wanghuancoder 已提交
18 19 20 21 22 23 24 25 26 27 28
namespace paddle {
namespace framework {
class InferShapeContext;
class OpDesc;
class Scope;
}  // namespace framework
namespace imperative {
class OpBase;
}  // namespace imperative
}  // namespace paddle

Y
Yang Yang(Tony) 已提交
29 30 31 32 33 34 35 36 37
namespace paddle {
namespace operators {
class RNNMemoryHelperOp : public framework::OperatorBase {
 public:
  RNNMemoryHelperOp(const std::string &type,
                    const framework::VariableNameMap &inputs,
                    const framework::VariableNameMap &outputs,
                    const framework::AttributeMap &attrs)
      : OperatorBase(type, inputs, outputs, attrs) {}
38 39 40 41

 private:
  void RunImpl(const framework::Scope &scope,
               const platform::Place &dev_place) const override {
Y
Yang Yang(Tony) 已提交
42 43
    auto mem_var_name = Input("X");
    auto *mem_var = scope.FindVar(mem_var_name);
44 45 46
    PADDLE_ENFORCE_NOT_NULL(
        mem_var, platform::errors::NotFound("Cannot find mem_var: %s in scope.",
                                            mem_var_name));
Y
Yang Yang(Tony) 已提交
47 48 49

    auto out_name = this->Output("Out");
    auto *out_var = scope.FindVar(out_name);
50 51 52
    PADDLE_ENFORCE_NOT_NULL(
        out_var, platform::errors::NotFound("Cannot find out_var: %s in scope.",
                                            out_name));
Y
Yang Yang(Tony) 已提交
53

C
chengduo 已提交
54 55 56
    platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance();
    auto &dev_ctx = *pool.Get(dev_place);

Y
Yang Yang(Tony) 已提交
57 58
    auto *out_tensor = out_var->GetMutable<framework::LoDTensor>();
    auto &mem_tensor = mem_var->Get<framework::LoDTensor>();
C
chengduo 已提交
59
    framework::TensorCopy(mem_tensor, dev_place, dev_ctx, out_tensor);
Y
Yang Yang(Tony) 已提交
60 61 62 63 64 65 66
    out_tensor->set_lod(mem_tensor.lod());
  }
};

class RNNMemoryHelperOpShapeInference : public framework::InferShapeBase {
 public:
  void operator()(framework::InferShapeContext *ctx) const override {
67 68 69
    OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "RNNMemoryHelper");
    OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "RNNMemoryHelper");

70
    ctx->ShareDim("X", /*->*/ "Out");
Y
Yang Yang(Tony) 已提交
71 72 73 74 75 76
    ctx->ShareLoD("X", /*->*/ "Out");
  }
};

class RNNMemoryHelperOpInfoMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
77
  void Make() override {
Y
Yang Yang(Tony) 已提交
78 79
    AddInput("X", "");
    AddOutput("Out", "");
F
fengjiayi 已提交
80
    AddAttr<int>("dtype",
Y
Yang Yang(Tony) 已提交
81 82
                 "(int, default 5 (FP32)) "
                 "Output data type")
83
        .SetDefault(framework::proto::VarType::FP32);
Y
Yang Yang(Tony) 已提交
84 85 86 87 88 89 90 91 92 93 94
    AddComment("");
  }
};

class RNNMemoryHelperGradOp : public framework::OperatorBase {
 public:
  RNNMemoryHelperGradOp(const std::string &type,
                        const framework::VariableNameMap &inputs,
                        const framework::VariableNameMap &outputs,
                        const framework::AttributeMap &attrs)
      : OperatorBase(type, inputs, outputs, attrs) {}
95 96 97 98

 private:
  void RunImpl(const framework::Scope &scope,
               const platform::Place &dev_place) const override {
Y
Yang Yang(Tony) 已提交
99 100 101 102 103
    auto out_grad_var_name = Input(framework::GradVarName("Out"));
    auto *out_grad_var = scope.FindVar(out_grad_var_name);

    auto in_grad_var_name = Output(framework::GradVarName("X"));
    auto *in_grad_var = scope.FindVar(in_grad_var_name);
104 105 106 107
    PADDLE_ENFORCE_NOT_NULL(
        in_grad_var,
        platform::errors::NotFound("Cannot find in_grad_var: %s in scope.",
                                   in_grad_var_name));
Y
Yang Yang(Tony) 已提交
108

C
chengduo 已提交
109 110 111
    platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance();
    auto &dev_ctx = *pool.Get(dev_place);

Y
Yang Yang(Tony) 已提交
112
    if (out_grad_var == nullptr) {
M
minqiyang 已提交
113
      VLOG(5) << "Using fill constant 0 as starting gradient";
Y
Yang Yang(Tony) 已提交
114 115 116 117 118
      auto in_var_name = Input("X");
      auto *in_var = scope.FindVar(in_var_name);
      auto &in_var_tensor = in_var->Get<framework::LoDTensor>();

      framework::AttributeMap attrs;
Y
Yu Yang 已提交
119
      attrs["dtype"] = in_var_tensor.type();
120
      attrs["shape"] = framework::vectorize<int>(in_var_tensor.dims());
Y
Yang Yang(Tony) 已提交
121 122 123 124
      attrs["value"] = 0.0f;

      auto zero_op = framework::OpRegistry::CreateOp(
          "fill_constant", {}, {{"Out", {in_grad_var_name}}}, attrs);
D
dzhwinter 已提交
125
      zero_op->Run(scope, dev_place);
Y
Yang Yang(Tony) 已提交
126 127 128
    } else {
      auto &out_grad_tensor = out_grad_var->Get<framework::LoDTensor>();
      auto *in_grad_tensor = in_grad_var->GetMutable<framework::LoDTensor>();
C
chengduo 已提交
129 130
      framework::TensorCopy(out_grad_tensor, dev_place, dev_ctx,
                            in_grad_tensor);
Y
Yang Yang(Tony) 已提交
131 132 133 134 135 136 137 138
      in_grad_tensor->set_lod(out_grad_tensor.lod());
    }
  }
};

class RNNMemoryHelperGradOpInfoMaker
    : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
139
  void Make() override {
Y
Yang Yang(Tony) 已提交
140 141 142 143
    AddInput(framework::GradVarName("Out"), "");
    AddInput("X", "");
    AddInput("Out", "");
    AddOutput(framework::GradVarName("X"), "");
F
fengjiayi 已提交
144
    AddAttr<int>("dtype",
Y
Yang Yang(Tony) 已提交
145 146
                 "(int, default 5 (FP32)) "
                 "Output data type")
147
        .SetDefault(framework::proto::VarType::FP32);
Y
Yang Yang(Tony) 已提交
148 149 150 151 152 153 154 155
    AddComment("");
  }
};

class RNNMemoryHelperGradOpShapeInference : public framework::InferShapeBase {
 public:
  void operator()(framework::InferShapeContext *ctx) const override {
    auto x_grad_name = framework::GradVarName("X");
156 157 158
    OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "RNNMemoryHelperGrad");
    OP_INOUT_CHECK(ctx->HasOutput(x_grad_name), "Output", x_grad_name,
                   "RNNMemoryHelperGrad");
Y
Yu Yang 已提交
159 160
    ctx->SetOutputDim(x_grad_name, ctx->GetInputDim("X"));
    ctx->ShareLoD("X", /*->*/ x_grad_name);
Y
Yang Yang(Tony) 已提交
161 162 163 164 165 166
  }
};

}  // namespace operators
}  // namespace paddle

H
hong 已提交
167 168 169 170 171 172
REGISTER_OPERATOR(
    rnn_memory_helper, paddle::operators::RNNMemoryHelperOp,
    paddle::operators::RNNMemoryHelperOpInfoMaker,
    paddle::operators::RNNMemoryHelperOpShapeInference,
    paddle::framework::DefaultGradOpMaker<paddle::framework::OpDesc, true>,
    paddle::framework::DefaultGradOpMaker<paddle::imperative::OpBase, true>);
Y
Yang Yang(Tony) 已提交
173 174 175 176
REGISTER_OPERATOR(rnn_memory_helper_grad,
                  paddle::operators::RNNMemoryHelperGradOp,
                  paddle::operators::RNNMemoryHelperGradOpInfoMaker,
                  paddle::operators::RNNMemoryHelperGradOpShapeInference);