grad_op_builder.cc 4.2 KB
Newer Older
F
fengjiayi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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,
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. */

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

namespace paddle {
namespace framework {

21
OperatorBase* GradOpBuilder::Build() {
22
  BuildOpInOutArgList();
F
fengjiayi 已提交
23
  std::string grad_op_type = OpRegistry::grad_ops().at(op_->type_);
24 25
  OperatorBase* grad_op = OpRegistry::op_creators().at(grad_op_type)();
  grad_op->type_ = grad_op_type;
26 27 28 29
  CompleteGradOp(grad_op);
  return grad_op;
}

30
OpInOutArg* GradOpBuilder::BuildArg(const VarProto& var,
31
                                    const VarIndexMap& var_map,
F
fengjiayi 已提交
32 33
                                    const std::vector<int>& format,
                                    InOutType type) {
34 35 36 37 38 39 40
  int idx = var_map.at(var.name());
  int begin_idx = format.empty() ? idx : format.at(idx);
  int end_idx = format.empty() ? idx + 1 : format.at(idx + 1);
  return new OpInOutArg(var.name(), type, !var.ignore_gradient(), begin_idx,
                        end_idx);
}

41
void GradOpBuilder::BuildOpInOutArgList() {
F
fengjiayi 已提交
42 43
  const OpProto& op_proto = OpRegistry::protos().at(op_->type_);
  const auto& var_map = *(OpRegistry::VarIndexMaps().at(op_->type_));
F
fengjiayi 已提交
44
  const std::vector<int>& in_format =
45
      op_->attrs_.count("input_format")
F
fengjiayi 已提交
46
          ? op_->GetAttr<std::vector<int>>("input_format")
47
          : std::vector<int>();
F
fengjiayi 已提交
48
  const std::vector<int>& out_format =
49
      op_->attrs_.count("output_format")
F
fengjiayi 已提交
50
          ? op_->GetAttr<std::vector<int>>("output_format")
51 52 53 54 55 56 57 58 59 60 61
          : std::vector<int>();
  for (const auto& var : op_proto.inputs()) {
    arg_list_.emplace_back(
        std::shared_ptr<OpInOutArg>(BuildArg(var, var_map, in_format, IN)));
  }
  for (const auto& var : op_proto.outputs()) {
    arg_list_.emplace_back(
        std::shared_ptr<OpInOutArg>(BuildArg(var, var_map, out_format, OUT)));
  }
}

62
void GradOpBuilder::AddArgIntoGradOp(const OpInOutArg* arg,
F
fengjiayi 已提交
63 64 65
                                     std::vector<std::string>& in_out,
                                     std::vector<int>& format,
                                     VarIndexMap* varmap, int& idx,
F
fengjiayi 已提交
66
                                     bool is_grad) const {
67 68 69 70
  std::string var_name = arg->proto_name_;
  if (is_grad) {
    var_name += OperatorBase::GRAD_VAR_SUFFIX();
  }
F
fengjiayi 已提交
71
  (*varmap)[var_name] = idx++;
72
  size_t pre_sz = in_out.size();
F
fengjiayi 已提交
73 74
  auto base_it =
      arg->type_ == IN ? op_->inputs_.begin() : op_->outputs_.begin();
75 76 77 78 79 80 81 82 83 84
  std::copy(base_it + arg->begin_idx_, base_it + arg->end_idx_,
            std::back_inserter(in_out));
  if (is_grad) {
    for (size_t i = pre_sz; i < in_out.size(); ++i) {
      in_out[i] += OperatorBase::GRAD_VAR_SUFFIX();
    }
  }
  format.push_back(in_out.size());
}

85
void GradOpBuilder::CompleteGradOp(OperatorBase* grad_op) const {
86 87 88 89 90 91
  grad_op->attrs_ = op_->attrs_;
  grad_op->attrs_.erase("input_format");
  grad_op->attrs_.erase("output_format");
  VarIndexMap* grad_varmap = new VarIndexMap();
  int in_idx = 0;
  int out_idx = 0;
F
fengjiayi 已提交
92 93
  std::vector<int> in_format({0});
  std::vector<int> out_format({0});
94 95 96
  for (const auto& arg : arg_list_) {
    // op_'s inputs_ and outputs_
    if (arg->needed_in_grad_) {
F
fengjiayi 已提交
97 98
      AddArgIntoGradOp(arg.get(), grad_op->inputs_, in_format, grad_varmap,
                       in_idx, false);
99 100 101
    }
    if (arg->type_ == IN) {
      // gradients of op_'s inputs_
F
fengjiayi 已提交
102 103
      AddArgIntoGradOp(arg.get(), grad_op->outputs_, out_format, grad_varmap,
                       out_idx, true);
104 105
    } else {
      // gradients of op_'s outputs_
F
fengjiayi 已提交
106 107
      AddArgIntoGradOp(arg.get(), grad_op->inputs_, in_format, grad_varmap,
                       in_idx, true);
108 109 110 111
    }
  }
  grad_op->attrs_["input_format"] = in_format;
  grad_op->attrs_["output_format"] = out_format;
Y
Typo  
Yu Yang 已提交
112
  grad_op->in_out_idxs_.reset(grad_varmap);
113 114 115
}

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