1 /** 2 * Copyright 2020 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINDSPORE_LITE_SRC_KERNEL_REGISTRY_H_ 18 #define MINDSPORE_LITE_SRC_KERNEL_REGISTRY_H_ 19 20 #include <string> 21 #include <map> 22 #include <unordered_map> 23 #include <vector> 24 #include <set> 25 #include "src/lite_kernel.h" 26 #include "schema/model_generated.h" 27 28 using mindspore::kernel::kKernelArch_MAX; 29 using mindspore::kernel::kKernelArch_MIN; 30 using mindspore::schema::PrimitiveType_MAX; 31 using mindspore::schema::PrimitiveType_MIN; 32 33 namespace mindspore::lite { 34 class KernelRegistry { 35 public: 36 KernelRegistry() = default; 37 virtual ~KernelRegistry(); 38 39 static KernelRegistry *GetInstance(); 40 virtual kernel::KernelCreator GetCreator(const kernel::KernelKey &desc); 41 int GetCreatorFuncIndex(kernel::KernelKey desc); 42 void RegKernel(kernel::KernelKey desc, kernel::KernelCreator creator); 43 void RegKernel(kernel::KERNEL_ARCH arch, TypeId data_type, int type, kernel::KernelCreator creator); 44 bool SupportKernel(const kernel::KernelKey &key); 45 int GetKernel(const std::vector<Tensor *> &in_tensors, const std::vector<Tensor *> &out_tensors, 46 const InnerContext *ctx, const mindspore::Context *ms_ctx, const kernel::KernelKey &key, 47 OpParameter *op_parameter, kernel::LiteKernel **kernel, const void *primitive = nullptr); 48 49 protected: 50 #ifndef CUSTOM_KERNEL_REGISTRY_CLIP 51 int GetCustomKernel(const std::vector<Tensor *> &in_tensors, const std::vector<Tensor *> &out_tensors, 52 const mindspore::Context *ctx, const kernel::KernelKey &key, kernel::LiteKernel **kernel, 53 const void *primitive = nullptr); 54 #endif 55 static const int device_type_length_{kKernelArch_MAX - kKernelArch_MIN + 1}; 56 static const int data_type_length_{kNumberTypeEnd - kNumberTypeBegin + 1}; 57 static const int op_type_length_{PrimitiveType_MAX - PrimitiveType_MIN + 1}; 58 static const int array_size_{device_type_length_ * data_type_length_ * op_type_length_}; 59 kernel::KernelCreator *creator_arrays_ = nullptr; 60 61 private: 62 void CreatorArraysInit(); 63 64 private: 65 std::mutex lock_; 66 }; 67 68 class KernelRegistrar { 69 public: KernelRegistrar(const kernel::KernelKey & desc,kernel::KernelCreator creator)70 KernelRegistrar(const kernel::KernelKey &desc, kernel::KernelCreator creator) { 71 KernelRegistry::GetInstance()->RegKernel(desc, creator); 72 } 73 ~KernelRegistrar() = default; 74 KernelRegistrar(const kernel::KERNEL_ARCH arch,const TypeId data_type,const int op_type,kernel::KernelCreator creator)75 KernelRegistrar(const kernel::KERNEL_ARCH arch, const TypeId data_type, const int op_type, 76 kernel::KernelCreator creator) { 77 KernelRegistry::GetInstance()->RegKernel(arch, data_type, op_type, creator); 78 } 79 }; 80 81 #define REG_KERNEL(arch, data_type, op_type, kernelCreater) \ 82 static KernelRegistrar g_##arch##data_type##op_type##kernelReg(arch, data_type, op_type, kernelCreater); 83 } // namespace mindspore::lite 84 85 #endif // MINDSPORE_LITE_SRC_KERNEL_REGISTRY_H_ 86