op_info.h 3.1 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
/* 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

Z
zhaojiaying01 已提交
21 22
#include <string>
#include "common/log.h"
朔-望's avatar
朔-望 已提交
23
#include "common/type_define.h"
Z
zhaojiaying01 已提交
24
#include "framework/framework.pb.h"
朔-望's avatar
朔-望 已提交
25 26

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
27 28
namespace framework {

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

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

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

朔-望's avatar
朔-望 已提交
46 47 48
template <typename Dtype>
class OpInfoMap {
 public:
49
  static OpInfoMap &Instance() {
Z
zhaojiaying01 已提交
50
    LOG(paddle_mobile::kLOG_DEBUG1) << " TODO: fix bug";
51 52
    if (g_op_info_map<Dtype> == nullptr) {
      g_op_info_map<Dtype> = new OpInfoMap();
朔-望's avatar
朔-望 已提交
53
    }
54
    return *g_op_info_map<Dtype>;
Z
zhaojiaying01 已提交
55
  }
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

  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
朔-望 已提交
82
    }
83
  }
朔-望's avatar
朔-望 已提交
84

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

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

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

97
  //  DISABLE_COPY_AND_ASSIGN(OpInfoMap);
朔-望's avatar
朔-望 已提交
98 99
};

朔-望's avatar
朔-望 已提交
100 101
}  // namespace framework
}  // namespace paddle_mobile