op_info.h 2.7 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

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

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

26 27 28 29 30 31 32 33 34
template <typename Dtype>
class OperatorBase;

template <typename Dtype>
using OpCreator = std::function<framework::OperatorBase<Dtype> *(
    const std::string & /*type*/, const VariableNameMap & /*inputs*/,
    const VariableNameMap & /*outputs*/,
    const framework::AttributeMap & /*attrs*/, framework::Scope * /*scope*/)>;

朔-望's avatar
朔-望 已提交
35 36
template <typename Dtype>
struct OpInfo {
37 38
  OpCreator<Dtype> creator_;
  const OpCreator<Dtype> &Creator() const {
W
wangliu 已提交
39 40
    PADDLE_MOBILE_ENFORCE(creator_ != nullptr,
                          "Operator Creator has not been registered");
41 42
    return creator_;
  }
朔-望's avatar
朔-望 已提交
43 44
};

朔-望's avatar
朔-望 已提交
45 46 47
template <typename Dtype>
class OpInfoMap {
 public:
Z
zhaojiaying01 已提交
48 49 50 51
  static OpInfoMap<Dtype> *Instance() {
    static OpInfoMap<Dtype> *s_instance = nullptr;
    if (s_instance == nullptr) {
      s_instance = new OpInfoMap();
朔-望's avatar
朔-望 已提交
52
    }
Z
zhaojiaying01 已提交
53
    return s_instance;
Z
zhaojiaying01 已提交
54
  }
55 56 57 58 59 60

  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 已提交
61 62
    PADDLE_MOBILE_ENFORCE(!Has(type), "Operator %s has been registered",
                          type.c_str());
63 64 65 66 67
    map_.insert({type, info});
  }

  const OpInfo<Dtype> &Get(const std::string &type) const {
    auto op_info_ptr = GetNullable(type);
W
wangliu 已提交
68 69
    PADDLE_MOBILE_ENFORCE(op_info_ptr != nullptr,
                          "Operator %s has not been registered", type.c_str());
70 71 72 73 74 75 76 77 78
    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
};

朔-望's avatar
朔-望 已提交
95 96
}  // namespace framework
}  // namespace paddle_mobile