op_desc.h 4.5 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 19
#include <unordered_map>
#include <vector>
Y
Yi Wang 已提交
20 21 22
#include "paddle/fluid/framework/attribute.h"
#include "paddle/fluid/framework/type_defs.h"
#include "paddle/fluid/framework/var_desc.h"
F
fengjiayi 已提交
23 24 25 26

namespace paddle {
namespace framework {

Y
Yu Yang 已提交
27 28 29
class BlockDesc;
class ProgramDesc;
class OpDesc {
F
fengjiayi 已提交
30
 public:
Y
Yu Yang 已提交
31
  OpDesc() {}
F
fengjiayi 已提交
32

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

36 37 38 39 40 41 42 43
  OpDesc(const proto::OpDesc &desc, ProgramDesc *prog, BlockDesc *block);

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

  OpDesc(const OpDesc &other, BlockDesc *block) {
    *this = other;
    block_ = 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;

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

F
fengjiayi 已提交
64 65 66 67 68 69 70
  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();
  }

71
  proto::AttrType GetAttrType(const std::string &name) const;
F
fengjiayi 已提交
72 73 74 75 76

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

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

A
Abhinav Arora 已提交
77
  void SetBlockAttr(const std::string &name, BlockDesc *block);
F
fengjiayi 已提交
78 79 80 81 82

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

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

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

Y
Yang Yang(Tony) 已提交
85 86 87 88
  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 已提交
89
  // Only be used in C++
Y
Yu Yang 已提交
90
  const AttributeMap &GetAttrMap() const;
F
fengjiayi 已提交
91

92 93
  // Only be used in C++
  void SetAttrMap(const AttributeMap &attr_map);
94

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

Y
Yu Yang 已提交
98
  void SetInputMap(const VariableNameMap &input) {
Y
Stash  
Yu Yang 已提交
99 100 101 102
    this->inputs_ = input;
    this->need_update_ = true;
  }

Y
Yu Yang 已提交
103
  void SetOutputMap(const VariableNameMap &output) {
Y
Stash  
Yu Yang 已提交
104 105 106 107
    this->outputs_ = output;
    this->need_update_ = true;
  }

Y
Yu Yang 已提交
108 109 110 111
  const VariableNameMap &Inputs() const { return inputs_; }

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

112 113 114 115 116
  AttributeMap *MutableAttrMap() {
    this->need_update_ = true;
    return &this->attrs_;
  }

F
fengjiayi 已提交
117 118
  void CheckAttrs();

Y
Yu Yang 已提交
119
  void InferShape(const BlockDesc &block) const;
Y
Yu Yang 已提交
120

Y
Yu Yang 已提交
121
  void InferVarType(BlockDesc *block) const;
Y
Yu Yang 已提交
122

123
  void SetIsTarget(bool is_target) { desc_.set_is_target(is_target); }
124

125 126
  void Flush();

127 128
  BlockDesc *Block() { return this->block_; }

129 130
  const BlockDesc &BlockRef() const { return *this->block_; }

131 132
  void SetBlock(BlockDesc *block) { this->block_ = block; }

F
fengjiayi 已提交
133
 private:
134 135 136 137 138
  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 已提交
139
        map.begin(), map.end(), std::back_inserter(ret_val),
140 141 142 143
        [](const typename MapType::value_type &pair) { return pair.first; });
    return ret_val;
  }

144
  proto::OpDesc desc_;
145
  BlockDesc *block_;  // not_own
146
  // input arg name => input variable names
Y
Yu Yang 已提交
147
  VariableNameMap inputs_;
148
  // output arg name => output variable names
Y
Yu Yang 已提交
149 150
  VariableNameMap outputs_;
  AttributeMap attrs_;
F
fengjiayi 已提交
151 152 153 154 155

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