grad_op_builder.cc 2.7 KB
Newer Older
F
fengjiayi 已提交
1 2 3 4 5 6 7 8 9 10
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
F
fengjiayi 已提交
11 12 13
WITHOpArgType::OUT WARRANTIES OR CONDITIONS OF ANY KOpArgType::IND, either
express or implied. See the License for the specific language governing
permissions and limitations under the License. */
F
fengjiayi 已提交
14

15
#include "paddle/framework/grad_op_builder.h"
Y
Yu Yang 已提交
16
#include "paddle/framework/framework.pb.h"
F
fengjiayi 已提交
17
#include "paddle/framework/op_registry.h"
18 19 20

namespace paddle {
namespace framework {
F
fengjiayi 已提交
21
enum class OpArgType { IN, OUT };
22

23 24
static void TransOpArg(const OperatorBase* src_op, const OpArgType& src_type,
                       bool is_grad, OperatorBase::VarNameMap* vars) {
25
  const auto& src_inout =
F
fengjiayi 已提交
26
      src_type == OpArgType::IN ? src_op->inputs_ : src_op->outputs_;
Y
Yu Yang 已提交
27
  auto& dst_inout = *vars;
F
fengjiayi 已提交
28
  const OpProto* proto = OpRegistry::op_info_map().at(src_op->type_).proto_;
F
fengjiayi 已提交
29
  const auto& src_arg_list =
F
fengjiayi 已提交
30
      src_type == OpArgType::IN ? proto->inputs() : proto->outputs();
31
  for (const auto& arg : src_arg_list) {
Q
qingqing01 已提交
32
    if (arg.no_gradient() && !is_grad) continue;
Q
qingqing01 已提交
33
    const std::string src_name = arg.name();
34
    std::string dst_name = is_grad ? GradVarName(src_name) : src_name;
Q
qingqing01 已提交
35
    dst_inout[dst_name].reserve(src_inout.at(src_name).size());
36
    for (auto& var_name : src_inout.at(src_name)) {
Q
qingqing01 已提交
37
      std::string s = is_grad ? GradVarName(var_name) : var_name;
38
      dst_inout[dst_name].emplace_back(s);
39 40 41 42
    }
  }
}

43
OperatorBase* BuildGradOp(const OperatorBase* op) {
F
fengjiayi 已提交
44
  auto it = OpRegistry::op_info_map().find(op->type_);
F
WIP  
fengjiayi 已提交
45
  PADDLE_ENFORCE(it != OpRegistry::op_info_map().end(),
F
fengjiayi 已提交
46
                 "'%s' has not been registered.", op->type_);
F
fengjiayi 已提交
47 48
  PADDLE_ENFORCE(it->second.proto_ != nullptr, "'%s' has no OpProto.",
                 op->type_);
F
WIP  
fengjiayi 已提交
49 50
  std::string grad_op_type = it->second.grad_op_type_;
  PADDLE_ENFORCE(!grad_op_type.empty(), "'%s' has no gradient operator.",
F
fengjiayi 已提交
51
                 op->type_);
Y
Yi Wang 已提交
52

Y
Yu Yang 已提交
53 54
  OperatorBase::VarNameMap inputs;
  OperatorBase::VarNameMap outputs;
55 56 57 58
  TransOpArg(op, OpArgType::IN, false, &inputs);   // I
  TransOpArg(op, OpArgType::OUT, false, &inputs);  // O
  TransOpArg(op, OpArgType::OUT, true, &inputs);   // OG
  TransOpArg(op, OpArgType::IN, true, &outputs);   // IG
Y
Yi Wang 已提交
59

F
fengjiayi 已提交
60
  it = OpRegistry::op_info_map().find(grad_op_type);
F
WIP  
fengjiayi 已提交
61 62
  PADDLE_ENFORCE(it != OpRegistry::op_info_map().end(),
                 "'%s' has not been registered.", grad_op_type);
63
  return it->second.creator_(grad_op_type, inputs, outputs, op->attrs_);
64 65 66
}

}  // namespace framework
F
fengjiayi 已提交
67
}  // namespace paddle