op_desc.h 2.8 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#include "paddle/framework/var_desc.h"

namespace paddle {
namespace framework {

class BlockDescBind;

class OpDescBind {
 public:
  OpDesc *Proto();

  std::string Type() const { return op_desc_.type(); }

  void SetType(const std::string &type) { op_desc_.set_type(type); }

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

  void SetInput(const std::string &param_name,
                const std::vector<std::string> &args);

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

  void SetOutput(const std::string &param_name,
                 const std::vector<std::string> &args);

  std::string DebugString() { return this->Proto()->DebugString(); }

  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 已提交
64
  // Only be used in C++
Y
Yu Yang 已提交
65
  const AttributeMap &GetAttrMap() const;
F
fengjiayi 已提交
66

67 68
  // Only be used in C++
  void SetAttrMap(const AttributeMap &attr_map);
69

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

F
fengjiayi 已提交
73
 private:
74 75 76 77 78 79 80 81 82 83
  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(
        map.begin(), map.end(), ret_val.begin(),
        [](const typename MapType::value_type &pair) { return pair.first; });
    return ret_val;
  }

F
fengjiayi 已提交
84 85 86
  void Sync();

  OpDesc op_desc_;
Y
Yu Yang 已提交
87 88 89
  VariableNameMap inputs_;
  VariableNameMap outputs_;
  AttributeMap attrs_;
F
fengjiayi 已提交
90 91 92 93 94

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