op_info.h 2.3 KB
Newer Older
Z
zhaojiaying01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

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. */
朔-望's avatar
朔-望 已提交
14 15 16

#pragma once

Z
zhaojiaying01 已提交
17 18
#include <string>
#include "common/log.h"
朔-望's avatar
朔-望 已提交
19 20 21
#include "common/type_define.h"

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
22 23
namespace framework {

朔-望's avatar
朔-望 已提交
24 25
template <typename Dtype>
struct OpInfo {
26 27
  OpCreator<Dtype> creator_;
  const OpCreator<Dtype> &Creator() const {
W
wangliu 已提交
28 29
    PADDLE_MOBILE_ENFORCE(creator_ != nullptr,
                          "Operator Creator has not been registered");
30 31
    return creator_;
  }
朔-望's avatar
朔-望 已提交
32 33
};

朔-望's avatar
朔-望 已提交
34 35 36
template <typename Dtype>
class OpInfoMap {
 public:
Z
zhaojiaying01 已提交
37 38 39 40
  static OpInfoMap<Dtype> *Instance() {
    static OpInfoMap<Dtype> *s_instance = nullptr;
    if (s_instance == nullptr) {
      s_instance = new OpInfoMap();
朔-望's avatar
朔-望 已提交
41
    }
Z
zhaojiaying01 已提交
42
    return s_instance;
Z
zhaojiaying01 已提交
43
  }
44 45 46 47 48 49

  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) {
W
wangliu 已提交
50 51
    PADDLE_MOBILE_ENFORCE(!Has(type), "Operator %s has been registered",
                          type.c_str());
52 53 54 55 56
    map_.insert({type, info});
  }

  const OpInfo<Dtype> &Get(const std::string &type) const {
    auto op_info_ptr = GetNullable(type);
W
wangliu 已提交
57 58
    PADDLE_MOBILE_ENFORCE(op_info_ptr != nullptr,
                          "Operator %s has not been registered", type.c_str());
59 60 61 62 63 64 65 66 67
    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
朔-望 已提交
68
    }
69
  }
朔-望's avatar
朔-望 已提交
70

71 72 73
  const std::unordered_map<std::string, OpInfo<Dtype>> &map() const {
    return map_;
  }
朔-望's avatar
朔-望 已提交
74

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

朔-望's avatar
朔-望 已提交
79
 private:
80 81
  OpInfoMap() = default;
  std::unordered_map<std::string, OpInfo<Dtype>> map_;
朔-望's avatar
朔-望 已提交
82

83
  //  DISABLE_COPY_AND_ASSIGN(OpInfoMap);
朔-望's avatar
朔-望 已提交
84 85
};

朔-望's avatar
朔-望 已提交
86 87
}  // namespace framework
}  // namespace paddle_mobile