op_desc.h 4.0 KB
Newer Older
F
fengjiayi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* 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 <unordered_map>
#include <vector>
#include "paddle/framework/attribute.h"
20
#include "paddle/framework/type_defs.h"
F
fengjiayi 已提交
21 22 23 24 25 26
#include "paddle/framework/var_desc.h"

namespace paddle {
namespace framework {

class BlockDescBind;
27
class ProgramDescBind;
F
fengjiayi 已提交
28 29 30

class OpDescBind {
 public:
F
fengjiayi 已提交
31 32 33 34 35
  OpDescBind() {}

  OpDescBind(const std::string &type, const VariableNameMap &inputs,
             const VariableNameMap &outputs, const AttributeMap &attrs);

36 37
  OpDescBind(const OpDesc &desc, ProgramDescBind *prog);

F
fengjiayi 已提交
38 39
  void CopyFrom(const OpDescBind &op_desc);

F
fengjiayi 已提交
40 41
  OpDesc *Proto();

42
  std::string Type() const { return desc_.type(); }
F
fengjiayi 已提交
43

44
  void SetType(const std::string &type) { desc_.set_type(type); }
F
fengjiayi 已提交
45 46 47

  const std::vector<std::string> &Input(const std::string &name) const;

F
Update  
fengjiayi 已提交
48 49
  std::vector<std::string> InputArgumentNames() const;

F
fengjiayi 已提交
50 51 52 53 54
  void SetInput(const std::string &param_name,
                const std::vector<std::string> &args);

  const std::vector<std::string> &Output(const std::string &name) const;

F
Update  
fengjiayi 已提交
55 56
  std::vector<std::string> OutputArgumentNames() const;

F
fengjiayi 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  void SetOutput(const std::string &param_name,
                 const std::vector<std::string> &args);

  bool HasAttr(const std::string &name) const {
    return attrs_.find(name) != attrs_.end();
  }

  AttrType GetAttrType(const std::string &name) const;

  std::vector<std::string> AttrNames() const;

  void SetAttr(const std::string &name, const Attribute &v);

  void SetBlockAttr(const std::string &name, BlockDescBind &block);

  Attribute GetAttr(const std::string &name) const;

  int GetBlockAttr(const std::string &name) const;

F
fengjiayi 已提交
76 77
  void Rename(const std::string &old_name, const std::string &new_name);

Y
Yang Yang(Tony) 已提交
78 79 80 81
  void RenameOutput(const std::string &old_name, const std::string &new_name);

  void RenameInput(const std::string &old_name, const std::string &new_name);

F
fengjiayi 已提交
82
  // Only be used in C++
Y
Yu Yang 已提交
83
  const AttributeMap &GetAttrMap() const;
F
fengjiayi 已提交
84

85 86
  // Only be used in C++
  void SetAttrMap(const AttributeMap &attr_map);
87

Y
Yu Yang 已提交
88 89
  std::vector<std::string> InputNames() const { return MapKeys(inputs_); }
  std::vector<std::string> OutputNames() const { return MapKeys(outputs_); }
90

Y
Yu Yang 已提交
91
  void SetInputMap(const VariableNameMap &input) {
Y
Stash  
Yu Yang 已提交
92 93 94 95
    this->inputs_ = input;
    this->need_update_ = true;
  }

Y
Yu Yang 已提交
96
  void SetOutputMap(const VariableNameMap &output) {
Y
Stash  
Yu Yang 已提交
97 98 99 100
    this->outputs_ = output;
    this->need_update_ = true;
  }

Y
Yu Yang 已提交
101 102 103 104
  const VariableNameMap &Inputs() const { return inputs_; }

  const VariableNameMap &Outputs() const { return outputs_; }

105 106 107 108 109
  AttributeMap *MutableAttrMap() {
    this->need_update_ = true;
    return &this->attrs_;
  }

F
fengjiayi 已提交
110 111
  void CheckAttrs();

Y
Yu Yang 已提交
112 113
  void InferShape(const BlockDescBind &block) const;

Y
Yu Yang 已提交
114 115
  void InferVarType(BlockDescBind *block) const;

116 117
  void MarkAsTarget() { desc_.set_is_target(true); }

118 119
  void Flush();

F
fengjiayi 已提交
120
 private:
121 122 123 124 125
  template <typename MapType>
  static std::vector<typename MapType::key_type> MapKeys(const MapType &map) {
    std::vector<typename MapType::key_type> ret_val;
    ret_val.reserve(map.size());
    std::transform(
Y
Fix CI  
Yu Yang 已提交
126
        map.begin(), map.end(), std::back_inserter(ret_val),
127 128 129 130
        [](const typename MapType::value_type &pair) { return pair.first; });
    return ret_val;
  }

131
  OpDesc desc_;
Y
Yu Yang 已提交
132 133 134
  VariableNameMap inputs_;
  VariableNameMap outputs_;
  AttributeMap attrs_;
F
fengjiayi 已提交
135 136 137 138 139

  // need_update_ indicate there some local changes not be synchronized. If
  // local changes should be synchronized, need_update_ should be set to true.
  bool need_update_{false};
};
F
fengjiayi 已提交
140 141
}  // namespace framework
}  // namespace paddle