op_info.h 2.9 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 25 26
/* 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 {
namespace framework {

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

L
liuruilong 已提交
36
template <typename Dtype> class OpInfoMap;
朔-望's avatar
朔-望 已提交
37

L
liuruilong 已提交
38
template <typename Dtype> static OpInfoMap<Dtype> *g_op_info_map = nullptr;
朔-望's avatar
朔-望 已提交
39

L
liuruilong 已提交
40 41 42
template <typename Dtype> class OpInfoMap {
public:
  static OpInfoMap &Instance() {
朔-望's avatar
朔-望 已提交
43 44 45 46 47 48
    if (g_op_info_map<Dtype> == nullptr) {
      g_op_info_map<Dtype> = new OpInfoMap();
    }
    return *g_op_info_map<Dtype>;
  };

L
liuruilong 已提交
49
  bool Has(const std::string &op_type) const {
朔-望's avatar
朔-望 已提交
50 51 52
    return map_.find(op_type) != map_.end();
  }

L
liuruilong 已提交
53
  void Insert(const std::string &type, const OpInfo<Dtype> &info) {
朔-望's avatar
朔-望 已提交
54 55 56 57
    //    PADDLE_ENFORCE(!Has(type), "Operator %s has been registered", type);
    map_.insert({type, info});
  }

L
liuruilong 已提交
58
  const OpInfo<Dtype> &Get(const std::string &type) const {
朔-望's avatar
朔-望 已提交
59 60 61 62 63 64 65
    auto op_info_ptr = GetNullable(type);
    //    PADDLE_ENFORCE_NOT_NULL(op_info_ptr, "Operator %s has not been
    //    registered",
    //                            type);
    return *op_info_ptr;
  }

L
liuruilong 已提交
66
  const OpInfo<Dtype> *GetNullable(const std::string &type) const {
朔-望's avatar
朔-望 已提交
67 68 69 70 71 72 73 74
    auto it = map_.find(type);
    if (it == map_.end()) {
      return nullptr;
    } else {
      return &it->second;
    }
  }

L
liuruilong 已提交
75
  const std::unordered_map<std::string, OpInfo<Dtype>> &map() const {
朔-望's avatar
朔-望 已提交
76 77 78
    return map_;
  }

L
liuruilong 已提交
79
  std::unordered_map<std::string, OpInfo<Dtype>> *mutable_map() {
朔-望's avatar
朔-望 已提交
80 81 82
    return &map_;
  }

L
liuruilong 已提交
83
private:
朔-望's avatar
朔-望 已提交
84 85 86 87 88 89
  OpInfoMap() = default;
  std::unordered_map<std::string, OpInfo<Dtype>> map_;

  //  DISABLE_COPY_AND_ASSIGN(OpInfoMap);
};

L
liuruilong 已提交
90 91
} // namespace framework
} // namespace paddle_mobile