grad_op_desc_maker.h 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/* 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. */

#pragma once
#include "paddle/framework/op_desc.h"
#include "paddle/framework/operator.h"

namespace paddle {
namespace framework {

class GradOpDescMakerBase {
 public:
  explicit GradOpDescMakerBase(const OpDescBind& fwd_op) : fwd_op_(fwd_op) {}

  virtual ~GradOpDescMakerBase() = default;
  virtual std::vector<OpDescBind> operator()() const = 0;

 protected:
  static std::vector<std::string> ToGradNames(
      const std::vector<std::string>& var_names) {
    std::vector<std::string> ret_val;
    ret_val.reserve(var_names.size());
    std::transform(var_names.begin(), var_names.end(),
                   std::back_inserter(ret_val), GradVarName);
    return ret_val;
  }

  std::vector<std::string> InputGrad(const std::string& name) const {
    return ToGradNames(fwd_op_.Input(name));
  }

  std::vector<std::string> OutputGrad(const std::string& name) const {
    return ToGradNames(fwd_op_.Output(name));
  }

Y
Yu Yang 已提交
47 48
  std::vector<std::string> InputNames() const {
    return this->fwd_op_.InputNames();
49 50
  }

Y
Yu Yang 已提交
51 52
  std::vector<std::string> OutputNames() const {
    return this->fwd_op_.OutputNames();
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  }

  std::vector<std::string> Input(const std::string& name) const {
    return fwd_op_.Input(name);
  }

  std::vector<std::string> Output(const std::string& name) const {
    return fwd_op_.Output(name);
  }

  const std::unordered_map<std::string, Attribute>& Attrs() const {
    return fwd_op_.GetAttrMap();
  }

  const Attribute& GetAttr(const std::string& name) const {
    auto& map = fwd_op_.GetAttrMap();
    auto it = map.find(name);
    PADDLE_ENFORCE(it != map.end(), "Cannot find attribute %s", name);
    return it->second;
  }

  std::string ForwardOpType() const { return this->fwd_op_.Type(); }

 private:
  const OpDescBind& fwd_op_;
};

class SingleGradOpDescMaker : public GradOpDescMakerBase {
 public:
Y
Yu Yang 已提交
82 83
  using GradOpDescMakerBase::GradOpDescMakerBase;

84 85 86 87 88 89 90
  std::vector<OpDescBind> operator()() const { return {this->Apply()}; }

 protected:
  virtual OpDescBind Apply() const = 0;
};

class DefaultGradOpDescMaker : public SingleGradOpDescMaker {
Y
Yu Yang 已提交
91 92 93
 public:
  using SingleGradOpDescMaker::SingleGradOpDescMaker;

94 95 96 97 98
 protected:
  virtual OpDescBind Apply() const {
    OpDescBind grad;
    grad.SetType(this->GradOpType());

Y
Yu Yang 已提交
99
    for (auto& input_param : this->InputNames()) {
100 101 102 103
      grad.SetInput(input_param, this->Input(input_param));
      grad.SetOutput(GradVarName(input_param), this->InputGrad(input_param));
    }

Y
Yu Yang 已提交
104
    for (auto& output_param : this->OutputNames()) {
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
      grad.SetInput(output_param, this->Output(output_param));
      grad.SetInput(GradVarName(output_param), this->OutputGrad(output_param));
    }

    grad.SetAttrMap(this->Attrs());

    return grad;
  }

  virtual std::string GradOpType() const {
    return this->ForwardOpType() + "_grad";
  }
};

}  // namespace framework
}  // namespace paddle