op_info.h 2.4 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
#include "common/type_define.h"
Z
zhaojiaying01 已提交
20
#include "framework/framework.pb.h"
朔-望's avatar
朔-望 已提交
21 22

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

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

朔-望's avatar
朔-望 已提交
36 37 38
template <typename Dtype>
class OpInfoMap {
 public:
Z
zhaojiaying01 已提交
39 40 41 42
  static OpInfoMap<Dtype> *Instance() {
    static OpInfoMap<Dtype> *s_instance = nullptr;
    if (s_instance == nullptr) {
      s_instance = new OpInfoMap();
朔-望's avatar
朔-望 已提交
43
    }
Z
zhaojiaying01 已提交
44
    return s_instance;
Z
zhaojiaying01 已提交
45
  }
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

  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
朔-望 已提交
72
    }
73
  }
朔-望's avatar
朔-望 已提交
74

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

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

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

87
  //  DISABLE_COPY_AND_ASSIGN(OpInfoMap);
朔-望's avatar
朔-望 已提交
88 89
};

朔-望's avatar
朔-望 已提交
90 91
}  // namespace framework
}  // namespace paddle_mobile