op_delegator_registry.h 4.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// Copyright 2020 The MACE 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.

#ifndef MACE_CORE_REGISTRY_OP_DELEGATOR_REGISTRY_H_
#define MACE_CORE_REGISTRY_OP_DELEGATOR_REGISTRY_H_

#include <functional>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>

L
luxuhui 已提交
24
#include "mace/core/bfloat16.h"
L
lichao18 已提交
25
#include "mace/core/fp16.h"
26
#include "mace/core/ops/op_delegator.h"
L
luxuhui 已提交
27
#include "mace/core/types.h"
28 29 30 31 32 33 34
#include "mace/proto/mace.pb.h"
#include "mace/public/mace.h"

namespace mace {
typedef std::function<std::unique_ptr<OpDelegator>(const DelegatorParam &)>
    DelegatorCreator;

L
luxuhui 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
struct DelegatorInfo {
  explicit DelegatorInfo(const char *delegator_name,
                         DataType data_type,
                         DeviceType device,
                         ImplType impl_type,
                         const char *tag);
  explicit DelegatorInfo(const char *delegator_name,
                         DataType data_type,
                         DeviceType device,
                         ImplType impl_type);

  std::string ToString() const;

  bool operator==(const DelegatorInfo &info) const;

  std::string delegator_name;
  DataType data_type;
  DeviceType device;
  ImplType impl_type;
  std::string tag;
};

57 58 59 60 61
class OpDelegatorRegistry {
 public:
  OpDelegatorRegistry() = default;
  ~OpDelegatorRegistry() = default;

L
luxuhui 已提交
62 63
  MaceStatus Register(const DelegatorInfo &key, DelegatorCreator creator);
  DelegatorCreator GetCreator(const DelegatorInfo &key) const;
64 65

 private:
L
luxuhui 已提交
66 67 68 69 70 71
  struct HashName {
    size_t operator()(const DelegatorInfo &delegator_info) const {
      return std::hash<std::string>()(delegator_info.ToString());
    }
  };
  std::unordered_map<DelegatorInfo, DelegatorCreator, HashName> registry_;
72 73 74 75 76 77
};

}  // namespace mace

#ifndef MACE_DELEGATOR_KEY_EX_TMP
#define MACE_DELEGATOR_KEY_EX_TMP(delegator_name, device, DT, impl, tag) \
L
luxuhui 已提交
78
  DelegatorInfo(#delegator_name, DataTypeToEnum<DT>::value, device, impl, #tag)
79 80 81 82 83 84 85
#endif  // MACE_DELEGATOR_KEY_EX_TMP

#ifndef MACE_DELEGATOR_KEY_EX
#define MACE_DELEGATOR_KEY_EX(delegator_name, device, DT, impl, tag) \
  MACE_DELEGATOR_KEY_EX_TMP(delegator_name, device, DT, impl, tag)
#endif  // MACE_DELEGATOR_KEY_EX

L
luxuhui 已提交
86 87 88 89 90
#ifndef MACE_DELEGATOR_KEY
#define MACE_DELEGATOR_KEY(delegator_name, device, DT, impl) \
  DelegatorInfo(#delegator_name, DataTypeToEnum<DT>::value, device, impl)
#endif  // MACE_DELEGATOR_KEY

91 92
#ifndef MACE_REGISTER_DELEGATOR
#define MACE_REGISTER_DELEGATOR(registry, class_name, param_name, key)  \
L
luxuhui 已提交
93
  registry->Register(key, OpDelegator::DefaultCreator<class_name, param_name>)
94 95
#endif  // MACE_REGISTER_DELEGATOR

L
luxuhui 已提交
96 97 98 99 100 101 102 103 104
#ifndef MACE_REGISTER_BF16_DELEGATOR
#ifdef MACE_ENABLE_BFLOAT16
#define MACE_REGISTER_BF16_DELEGATOR(registry, class_name, param_name, key) \
  MACE_REGISTER_DELEGATOR(registry, class_name, param_name, key)
#else
#define MACE_REGISTER_BF16_DELEGATOR(registry, class_name, param_name, key)
#endif  // MACE_ENABLE_BFLOAT16
#endif  // MACE_REGISTER_BF16_DELEGATOR

L
lichao18 已提交
105 106 107 108 109 110 111 112 113
#ifndef MACE_REGISTER_FP16_DELEGATOR
#ifdef MACE_ENABLE_FP16
#define MACE_REGISTER_FP16_DELEGATOR(registry, class_name, param_name, key) \
  MACE_REGISTER_DELEGATOR(registry, class_name, param_name, key)
#else
#define MACE_REGISTER_FP16_DELEGATOR(registry, class_name, param_name, key)
#endif  // MACE_ENABLE_FP16
#endif  // MACE_REGISTER_FP16_DELEGATOR

114 115 116
#ifndef MACE_DEFINE_DELEGATOR_CREATOR
#define MACE_DEFINE_DELEGATOR_CREATOR(class_name)            \
  static std::unique_ptr<class_name> Create(                 \
L
luxuhui 已提交
117
      Workspace *workspace, const DelegatorInfo &key,        \
118 119
      const DelegatorParam &param) {                         \
    DelegatorCreator creator =                               \
L
luxuhui 已提交
120
        workspace->GetDelegatorRegistry()->GetCreator(key);  \
121 122 123 124 125 126 127
    std::unique_ptr<OpDelegator> delegator = creator(param); \
    return  std::unique_ptr<class_name>(                     \
        static_cast<class_name *>(delegator.release()));     \
  }
#endif  // MACE_DEFINE_DELEGATOR_CREATOR

#endif  // MACE_CORE_REGISTRY_OP_DELEGATOR_REGISTRY_H_