op_info.h 3.0 KB
Newer Older
朔-望's avatar
朔-望 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
==============================================================================*/

#pragma once

#include "common/type_define.h"
#include "framework.pb.h"

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
25 26
namespace framework {

朔-望's avatar
朔-望 已提交
27 28
template <typename Dtype>
struct OpInfo {
29 30 31 32 33 34 35
  OpCreator<Dtype> creator_;
  const OpCreator<Dtype> &Creator() const {
    //    PADDLE_ENFORCE_NOT_NULL(creator_,
    //                            "Operator Creator has not been
    //                            registered");
    return creator_;
  }
朔-望's avatar
朔-望 已提交
36 37
};

朔-望's avatar
朔-望 已提交
38 39
template <typename Dtype>
class OpInfoMap;
朔-望's avatar
朔-望 已提交
40

朔-望's avatar
朔-望 已提交
41 42
template <typename Dtype>
static OpInfoMap<Dtype> *g_op_info_map = nullptr;
朔-望's avatar
朔-望 已提交
43

朔-望's avatar
朔-望 已提交
44 45 46
template <typename Dtype>
class OpInfoMap {
 public:
47 48 49
  static OpInfoMap &Instance() {
    if (g_op_info_map<Dtype> == nullptr) {
      g_op_info_map<Dtype> = new OpInfoMap();
朔-望's avatar
朔-望 已提交
50
    }
51 52 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
    return *g_op_info_map<Dtype>;
  };

  bool Has(const std::string &op_type) const {
    return map_.find(op_type) != map_.end();
  }

  void Insert(const std::string &type, const OpInfo<Dtype> &info) {
    //    PADDLE_ENFORCE(!Has(type), "Operator %s has been
    //    registered", type);
    map_.insert({type, info});
  }

  const OpInfo<Dtype> &Get(const std::string &type) const {
    auto op_info_ptr = GetNullable(type);
    //    PADDLE_ENFORCE_NOT_NULL(op_info_ptr, "Operator %s has not
    //    been
    //    registered",
    //                            type);
    return *op_info_ptr;
  }

  const OpInfo<Dtype> *GetNullable(const std::string &type) const {
    auto it = map_.find(type);
    if (it == map_.end()) {
      return nullptr;
    } else {
      return &it->second;
朔-望's avatar
朔-望 已提交
79
    }
80
  }
朔-望's avatar
朔-望 已提交
81

82 83 84
  const std::unordered_map<std::string, OpInfo<Dtype>> &map() const {
    return map_;
  }
朔-望's avatar
朔-望 已提交
85

86 87 88
  std::unordered_map<std::string, OpInfo<Dtype>> *mutable_map() {
    return &map_;
  }
朔-望's avatar
朔-望 已提交
89

朔-望's avatar
朔-望 已提交
90
 private:
91 92
  OpInfoMap() = default;
  std::unordered_map<std::string, OpInfo<Dtype>> map_;
朔-望's avatar
朔-望 已提交
93

94
  //  DISABLE_COPY_AND_ASSIGN(OpInfoMap);
朔-望's avatar
朔-望 已提交
95 96
};

朔-望's avatar
朔-望 已提交
97 98
}  // namespace framework
}  // namespace paddle_mobile