/* 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. */ #pragma once #include #include #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 class OperatorRegistrarRecursive; template struct OperatorRegistrar : public Registrar { explicit OperatorRegistrar(const std::string& op_type) { if (OpInfoMap::Instance()->Has(op_type)) { 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 info; OperatorRegistrarRecursive(op_type, &info); OpInfoMap::Instance()->Insert(op_type, info); } }; template struct OpInfoFiller { void operator()(const std::string& op_type, OpInfo* info) const { info->creator_ = [](const std::string& type, const VariableNameMap& inputs, const VariableNameMap& outputs, const AttributeMap& attrs, std::shared_ptr scope) { return new T(type, inputs, outputs, attrs, scope); }; } }; template class OperatorRegistrarRecursive { public: using T = typename std::tuple_element>::type; OperatorRegistrarRecursive(const std::string& op_type, OpInfo* info) { OpInfoFiller fill; fill(op_type, info); constexpr auto size = sizeof...(ARGS); OperatorRegistrarRecursive reg( op_type, info); (void)(reg); } }; template class OperatorRegistrarRecursive { public: OperatorRegistrarRecursive(const std::string& op_type, OpInfo* info) {} }; template class OpRegistry { public: static std::shared_ptr> CreateOp( const std::string& type, const VariableNameMap& inputs, const VariableNameMap& outputs, const AttributeMap attrs, std::shared_ptr scope) { auto& info = OpInfoMap::Instance()->Get(type); auto op = info.Creator()(type, inputs, outputs, attrs, scope); return std::shared_ptr>(op); } }; #define REGISTER_OPERATOR(op_type, op_class) \ template \ class _OpClass_##op_type##_ : public op_class { \ public: \ DEFINE_OP_CONSTRUCTOR(_OpClass_##op_type##_, op_class); \ }; \ static paddle_mobile::framework::OperatorRegistrar< \ paddle_mobile::CPU, _OpClass_##op_type##_> \ __op_registrar_##op_type##__(#op_type); \ int TouchOpRegistrar_##op_type() { \ __op_registrar_##op_type##__.Touch(); \ return 0; \ } #define USE_OP(op_type) \ extern int TouchOpRegistrar_##op_type(); \ static int use_op_itself_##op_type##_ __attribute__((unused)) = \ TouchOpRegistrar_##op_type() } // namespace framework } // namespace paddle_mobile