op_registry.h 6.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

#pragma once
Z
zhaojiaying01 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

#include <string>
#include <tuple>
#include "common/log.h"
#include "common/type_define.h"
#include "framework/op_info.h"
#include "framework/operator.h"

namespace paddle_mobile {
namespace framework {

class Registrar {
 public:
  void Touch() {}
};

template <typename Dtype, size_t I, bool at_end, typename... ARGS>
class OperatorRegistrarRecursive;

template <typename Dtype, typename... ARGS>
struct OperatorRegistrar : public Registrar {
  explicit OperatorRegistrar(const std::string& op_type) {
Z
zhaojiaying01 已提交
38
    if (OpInfoMap<Dtype>::Instance()->Has(op_type)) {
Z
zhaojiaying01 已提交
39 40 41 42 43 44 45 46 47 48 49
      LOG(paddle_mobile::kLOG_DEBUG1)
          << op_type << " is registered more than once.";
      return;
    }
    if (sizeof...(ARGS) == 0) {
      LOG(paddle_mobile::kLOG_DEBUG1)
          << "OperatorRegistrar should be invoked at least by OpClass";
      return;
    }
    OpInfo<Dtype> info;
    OperatorRegistrarRecursive<Dtype, 0, false, ARGS...>(op_type, &info);
Z
zhaojiaying01 已提交
50
    OpInfoMap<Dtype>::Instance()->Insert(op_type, info);
Z
zhaojiaying01 已提交
51 52 53 54 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 82 83 84 85 86 87 88 89 90 91 92
  }
};

template <typename Dtype, typename T>
struct OpInfoFiller {
  void operator()(const std::string& op_type, OpInfo<Dtype>* info) const {
    info->creator_ = [](const std::string& type, const VariableNameMap& inputs,
                        const VariableNameMap& outputs,
                        const AttributeMap& attrs,
                        std::shared_ptr<Scope> scope) {
      return new T(type, inputs, outputs, attrs, scope);
    };
  }
};

template <typename Dtype, size_t I, typename... ARGS>
class OperatorRegistrarRecursive<Dtype, I, false, ARGS...> {
 public:
  using T = typename std::tuple_element<I, std::tuple<ARGS...>>::type;
  OperatorRegistrarRecursive(const std::string& op_type, OpInfo<Dtype>* info) {
    OpInfoFiller<Dtype, T> fill;
    fill(op_type, info);
    constexpr auto size = sizeof...(ARGS);
    OperatorRegistrarRecursive<Dtype, I + 1, I + 1 == size, ARGS...> reg(
        op_type, info);
    (void)(reg);
  }
};

template <typename Dtype, size_t I, typename... ARGS>
class OperatorRegistrarRecursive<Dtype, I, true, ARGS...> {
 public:
  OperatorRegistrarRecursive(const std::string& op_type, OpInfo<Dtype>* info) {}
};

template <typename Dtype>
class OpRegistry {
 public:
  static std::shared_ptr<OperatorBase<Dtype>> CreateOp(
      const std::string& type, const VariableNameMap& inputs,
      const VariableNameMap& outputs, const AttributeMap attrs,
      std::shared_ptr<paddle_mobile::framework::Scope> scope) {
Z
zhaojiaying01 已提交
93
    auto& info = OpInfoMap<Dtype>::Instance()->Get(type);
Z
zhaojiaying01 已提交
94 95 96 97 98
    auto op = info.Creator()(type, inputs, outputs, attrs, scope);
    return std::shared_ptr<OperatorBase<Dtype>>(op);
  }
};

L
for  
liuruilong 已提交
99 100
#ifdef PADDLE_MOBILE_CPU

L
liuruilong 已提交
101 102
#define REGISTER_OPERATOR_CPU(op_type, op_class)                               \
  template <typename Dtype, typename T>                                        \
L
for  
liuruilong 已提交
103
  class _OpClass_##op_type##_cpu : public op_class<Dtype, T> {                 \
L
liuruilong 已提交
104
   public:                                                                     \
L
for  
liuruilong 已提交
105
    DEFINE_OP_CONSTRUCTOR(_OpClass_##op_type##_cpu, op_class);                 \
L
liuruilong 已提交
106 107
  };                                                                           \
  static paddle_mobile::framework::OperatorRegistrar<                          \
L
for  
liuruilong 已提交
108 109
      paddle_mobile::CPU, _OpClass_##op_type##_cpu<paddle_mobile::CPU, float>> \
      __op_registrar_##op_type##__cpu(#op_type);                               \
L
liuruilong 已提交
110
  int TouchOpRegistrar_##op_type##_cpu() {                                     \
L
for  
liuruilong 已提交
111
    __op_registrar_##op_type##__cpu.Touch();                                   \
L
liuruilong 已提交
112
    return 0;                                                                  \
Z
zhaojiaying01 已提交
113 114
  }

L
liuruilong 已提交
115 116
#define USE_OP_CPU(op_type)                                       \
  extern int TouchOpRegistrar_##op_type##_cpu();                  \
Z
zhaojiaying01 已提交
117
  static int use_op_itself_##op_type##_ __attribute__((unused)) = \
L
for  
liuruilong 已提交
118 119 120 121 122
      TouchOpRegistrar_##op_type##_cpu()

#endif

#ifdef PADDLE_MOBILE_MALI_GPU
L
liuruilong 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135
#define REGISTER_OPERATOR_MALI_GPU(op_type, op_class)               \
  template <typename Dtype, typename T>                             \
  class _OpClass_##op_type##_mali_gpu : public op_class<Dtype, T> { \
   public:                                                          \
    DEFINE_OP_CONSTRUCTOR(_OpClass_##op_type##_mali_gpu, op_class); \
  };                                                                \
  static paddle_mobile::framework::OperatorRegistrar<               \
      paddle_mobile::CPU,                                           \
      _OpClass_##op_type##_mali_gpu<paddle_mobile::CPU, float>>     \
      __op_registrar_##op_type##__mali_gpu(#op_type);               \
  int TouchOpRegistrar_##op_type##_mali_gpu() {                     \
    __op_registrar_##op_type##__mali_gpu.Touch();                   \
    return 0;                                                       \
L
for  
liuruilong 已提交
136 137
  }

L
liuruilong 已提交
138 139
#define USE_OP_MALI_GPU(op_type)                                  \
  extern int TouchOpRegistrar_##op_type##_mali_gpu();             \
L
for  
liuruilong 已提交
140 141 142 143 144 145
  static int use_op_itself_##op_type##_ __attribute__((unused)) = \
      TouchOpRegistrar_##op_type##_mali_gpu()

#endif

#ifdef PADDLE_MOBILE_FPGA
L
liuruilong 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158
#define REGISTER_OPERATOR_FPGA(op_type, op_class)               \
  template <typename Dtype, typename T>                         \
  class _OpClass_##op_type##_fpga : public op_class<Dtype, T> { \
   public:                                                      \
    DEFINE_OP_CONSTRUCTOR(_OpClass_##op_type##_fpga, op_class); \
  };                                                            \
  static paddle_mobile::framework::OperatorRegistrar<           \
      paddle_mobile::CPU,                                       \
      _OpClass_##op_type##_fpga<paddle_mobile::CPU, float>>     \
      __op_registrar_##op_type##__fpga(#op_type);               \
  int TouchOpRegistrar_##op_type##_fpga() {                     \
    __op_registrar_##op_type##__fpga.Touch();                   \
    return 0;                                                   \
L
for  
liuruilong 已提交
159 160
  }

L
liuruilong 已提交
161 162
#define USE_OP_FPGA(op_type)                                      \
  extern int TouchOpRegistrar_##op_type##_fpga();                 \
L
for  
liuruilong 已提交
163 164 165 166
  static int use_op_itself_##op_type##_ __attribute__((unused)) = \
      TouchOpRegistrar_##op_type##_fpga()

#endif
Z
zhaojiaying01 已提交
167 168 169

}  // namespace framework
}  // namespace paddle_mobile