op_desc.h 4.8 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
F
fengjiayi 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

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

A
Abhinav Arora 已提交
17
#include <string>
F
fengjiayi 已提交
18
#include <unordered_map>
H
hong 已提交
19
#include <utility>
F
fengjiayi 已提交
20
#include <vector>
W
wanghuancoder 已提交
21

Y
Yi Wang 已提交
22 23 24
#include "paddle/fluid/framework/attribute.h"
#include "paddle/fluid/framework/type_defs.h"
#include "paddle/fluid/framework/var_desc.h"
F
fengjiayi 已提交
25 26 27 28

namespace paddle {
namespace framework {

Y
Yu Yang 已提交
29 30
class BlockDesc;
class ProgramDesc;
W
wanghuancoder 已提交
31

Y
Yu Yang 已提交
32
class OpDesc {
F
fengjiayi 已提交
33
 public:
Y
Yu Yang 已提交
34
  OpDesc() {}
F
fengjiayi 已提交
35

Y
Yu Yang 已提交
36 37
  OpDesc(const std::string &type, const VariableNameMap &inputs,
         const VariableNameMap &outputs, const AttributeMap &attrs);
F
fengjiayi 已提交
38

F
fengjiayi 已提交
39
  OpDesc(const proto::OpDesc &desc, BlockDesc *block);
40 41 42

  explicit OpDesc(BlockDesc *block) : block_(block) {}

X
Xin Pan 已提交
43
  OpDesc(const OpDesc &other, BlockDesc *block);
44

45
  void CopyFrom(const OpDesc &op_desc);
F
fengjiayi 已提交
46

47
  proto::OpDesc *Proto();
F
fengjiayi 已提交
48

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

51
  void SetType(const std::string &type) { desc_.set_type(type); }
F
fengjiayi 已提交
52 53 54

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

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

F
fengjiayi 已提交
57 58 59 60 61
  void SetInput(const std::string &param_name,
                const std::vector<std::string> &args);

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

62 63
  bool HasOutput(const std::string &name) const;

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

F
fengjiayi 已提交
66 67
  void SetOutput(const std::string &param_name,
                 const std::vector<std::string> &args);
68
  void RemoveOutput(const std::string &name);
F
fengjiayi 已提交
69 70 71 72 73

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

74
  bool HasProtoAttr(const std::string &name) const;
F
fengjiayi 已提交
75

76
  proto::AttrType GetAttrType(const std::string &name) const;
F
fengjiayi 已提交
77 78 79 80

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

  void SetAttr(const std::string &name, const Attribute &v);
81
  void RemoveAttr(const std::string &name);
F
fengjiayi 已提交
82

A
Abhinav Arora 已提交
83
  void SetBlockAttr(const std::string &name, BlockDesc *block);
F
fengjiayi 已提交
84

85 86
  void SetBlocksAttr(const std::string &name, std::vector<BlockDesc *> blocks);

F
fengjiayi 已提交
87 88
  Attribute GetAttr(const std::string &name) const;

89 90 91 92
  template <typename T>
  T GetAttrIfExists(const std::string &name) const {
    T result{};
    if (HasAttr(name)) {
93
      result = BOOST_GET_CONST(T, GetAttr(name));
94 95 96 97
    }
    return result;
  }

M
minqiyang 已提交
98
  const proto::OpProto::Attr &GetProtoAttr(const std::string &name) const;
M
minqiyang 已提交
99

Y
yuyang18 已提交
100
  Attribute GetNullableAttr(const std::string &name) const;
Y
Fix bug  
yuyang18 已提交
101

G
gongweibao 已提交
102 103 104
  int GetBlockAttrId(const std::string &name) const;

  std::vector<int> GetBlocksAttrIds(const std::string &name) const;
F
fengjiayi 已提交
105

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

Y
Yang Yang(Tony) 已提交
108 109 110 111
  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 已提交
112
  // Only be used in C++
Y
Yu Yang 已提交
113
  const AttributeMap &GetAttrMap() const;
F
fengjiayi 已提交
114

115 116
  // Only be used in C++
  void SetAttrMap(const AttributeMap &attr_map);
117

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

Y
Yu Yang 已提交
121 122 123 124
  const VariableNameMap &Inputs() const { return inputs_; }

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

125 126 127 128 129
  AttributeMap *MutableAttrMap() {
    this->need_update_ = true;
    return &this->attrs_;
  }

F
fengjiayi 已提交
130 131
  void CheckAttrs();

Y
Yu Yang 已提交
132
  void InferShape(const BlockDesc &block) const;
Y
Yu Yang 已提交
133

Y
Yu Yang 已提交
134
  void InferVarType(BlockDesc *block) const;
Y
Yu Yang 已提交
135

136
  void SetIsTarget(bool is_target) { desc_.set_is_target(is_target); }
137

138 139
  void Flush();

140 141
  BlockDesc *Block() { return this->block_; }

S
sneaxiy 已提交
142 143
  const BlockDesc *Block() const { return this->block_; }

F
fengjiayi 已提交
144
 private:
145 146 147 148 149
  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 已提交
150
        map.begin(), map.end(), std::back_inserter(ret_val),
151 152 153 154
        [](const typename MapType::value_type &pair) { return pair.first; });
    return ret_val;
  }

155
  proto::OpDesc desc_;
156
  BlockDesc *block_;  // not_own
157
  // input arg name => input variable names
Y
Yu Yang 已提交
158
  VariableNameMap inputs_;
159
  // output arg name => output variable names
Y
Yu Yang 已提交
160 161
  VariableNameMap outputs_;
  AttributeMap attrs_;
F
fengjiayi 已提交
162 163 164 165 166

  // 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 已提交
167 168
}  // namespace framework
}  // namespace paddle