grad_op_creator.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_creator.h"
F
fengjiayi 已提交
16
#include "paddle/framework/op_registry.h"
17 18 19 20 21 22 23 24 25 26 27 28 29

namespace paddle {
namespace framework {

OperatorBase* GradOpCreator::Create() {
  BuildOpInOutArgList();
  OperatorBase* grad_op = OpRegistry::grad_creators().at(op_->type_)();
  CompleteGradOp(grad_op);
  return grad_op;
}

OpInOutArg* GradOpCreator::BuildArg(const VarProto& var,
                                    const VarIndexMap& var_map,
F
fengjiayi 已提交
30 31
                                    const std::vector<int>& format,
                                    InOutType type) {
32 33 34 35 36 37 38 39
  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);
}

void GradOpCreator::BuildOpInOutArgList() {
F
fengjiayi 已提交
40 41
  const OpProto& op_proto = OpRegistry::protos().at(op_->type_);
  const auto& var_map = *(OpRegistry::VarIndexMaps().at(op_->type_));
F
fengjiayi 已提交
42
  const std::vector<int>& in_format =
43
      op_->attrs_.count("input_format")
F
fengjiayi 已提交
44
          ? op_->GetAttr<std::vector<int>>("input_format")
45
          : std::vector<int>();
F
fengjiayi 已提交
46
  const std::vector<int>& out_format =
47
      op_->attrs_.count("output_format")
F
fengjiayi 已提交
48
          ? op_->GetAttr<std::vector<int>>("output_format")
49 50 51 52 53 54 55 56 57 58 59
          : 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)));
  }
}

F
fengjiayi 已提交
60 61 62 63
void GradOpCreator::AddArgIntoGradOp(const OpInOutArg* arg,
                                     std::vector<std::string>& in_out,
                                     std::vector<int>& format,
                                     VarIndexMap* varmap, int& idx,
F
fengjiayi 已提交
64
                                     bool is_grad) const {
65 66 67 68
  std::string var_name = arg->proto_name_;
  if (is_grad) {
    var_name += OperatorBase::GRAD_VAR_SUFFIX();
  }
F
fengjiayi 已提交
69
  (*varmap)[var_name] = idx++;
70
  size_t pre_sz = in_out.size();
F
fengjiayi 已提交
71 72
  auto base_it =
      arg->type_ == IN ? op_->inputs_.begin() : op_->outputs_.begin();
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  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());
}

void GradOpCreator::CompleteGradOp(OperatorBase* grad_op) const {
  grad_op->type_ = op_->type_ + "@GRAD";  // not necessary
  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 已提交
91 92
  std::vector<int> in_format({0});
  std::vector<int> out_format({0});
93 94 95
  for (const auto& arg : arg_list_) {
    // op_'s inputs_ and outputs_
    if (arg->needed_in_grad_) {
F
fengjiayi 已提交
96 97
      AddArgIntoGradOp(arg.get(), grad_op->inputs_, in_format, grad_varmap,
                       in_idx, false);
98 99 100
    }
    if (arg->type_ == IN) {
      // gradients of op_'s inputs_
F
fengjiayi 已提交
101 102
      AddArgIntoGradOp(arg.get(), grad_op->outputs_, out_format, grad_varmap,
                       out_idx, true);
103 104
    } else {
      // gradients of op_'s outputs_
F
fengjiayi 已提交
105 106
      AddArgIntoGradOp(arg.get(), grad_op->inputs_, in_format, grad_varmap,
                       in_idx, true);
107 108 109 110 111 112 113 114
    }
  }
  grad_op->attrs_["input_format"] = in_format;
  grad_op->attrs_["output_format"] = out_format;
  grad_op->in_out_idxs_.reset(grad_varmap);
}

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