1 /**
2 * Copyright 2023 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_RUNTIME_KERNEL_CPU_NNACL_MANAGER_H_
18 #define MINDSPORE_LITE_SRC_RUNTIME_KERNEL_CPU_NNACL_MANAGER_H_
19
20 #include <map>
21 #include <vector>
22 #include "nnacl/nnacl_kernel.h"
23
24 namespace mindspore::nnacl {
25 struct KeyDesc {
26 int op_;
27 TypeId dt_;
28 bool operator=(const KeyDesc &comp) const { return ((dt_ == comp.dt_) && (op_ == comp.op_)); }
29 bool operator<(const KeyDesc &comp) const { return (op_ != comp.op_) ? (op_ < comp.op_) : (dt_ < comp.dt_); }
30 };
31
32 typedef NNACLKernel *(*NNACLCreator)(OpParameter *parameter, const std::vector<lite::Tensor *> &in,
33 const std::vector<lite::Tensor *> &out, const lite::InnerContext *ctx);
34
35 class KernelRegistry {
36 public:
GetInstance()37 static KernelRegistry *GetInstance() {
38 static KernelRegistry instance;
39 return &instance;
40 }
Register(KeyDesc desc,NNACLCreator creator)41 void Register(KeyDesc desc, NNACLCreator creator) { nnacl_map_[desc] = creator; }
Creator(KeyDesc desc)42 NNACLCreator Creator(KeyDesc desc) {
43 auto iter = nnacl_map_.find(desc);
44 if (iter != nnacl_map_.end()) {
45 return iter->second;
46 }
47 return nullptr;
48 }
49
50 protected:
51 std::map<KeyDesc, NNACLCreator> nnacl_map_;
52 };
53
54 class NNACLKernelRegistrar {
55 public:
NNACLKernelRegistrar(int op_type,TypeId data_type,NNACLCreator creator)56 NNACLKernelRegistrar(int op_type, TypeId data_type, NNACLCreator creator) {
57 KernelRegistry::GetInstance()->Register({op_type, data_type}, creator);
58 }
59 ~NNACLKernelRegistrar() = default;
60 };
61
62 template <class T>
NNACLOpt(OpParameter * parameter,const std::vector<lite::Tensor * > & in,const std::vector<lite::Tensor * > & out,const lite::InnerContext * ctx)63 NNACLKernel *NNACLOpt(OpParameter *parameter, const std::vector<lite::Tensor *> &in,
64 const std::vector<lite::Tensor *> &out, const lite::InnerContext *ctx) {
65 auto *kernel = new (std::nothrow) T(parameter, in, out, ctx);
66 return kernel;
67 }
68
69 #define NNACL_KERNEL(op_type, data_type, creator) \
70 static NNACLKernelRegistrar g_kernel##op_type##data_type##kernelReg(op_type, data_type, creator);
71
72 bool NNACLSupportKernel(int op_type, TypeId data_type);
73 NNACLKernel *NNACLKernelRegistry(OpParameter *parameter, const std::vector<lite::Tensor *> &inputs,
74 const std::vector<lite::Tensor *> &outputs, const lite::InnerContext *ctx,
75 const kernel::KernelKey &key);
76 } // namespace mindspore::nnacl
77 #endif // MINDSPORE_LITE_SRC_RUNTIME_KERNEL_CPU_NNACL_KERNEL_H_
78