1 /** 2 * Copyright 2021 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_COMMON_OPS_OPS_UTILS_H_ 18 #define MINDSPORE_LITE_SRC_COMMON_OPS_OPS_UTILS_H_ 19 20 #include <map> 21 #include <string> 22 #include <memory> 23 #include <algorithm> 24 #include "src/common/ops/ops_func_declare.h" 25 #ifdef PRIMITIVE_WRITEABLE 26 #include "src/common/log_adapter.h" 27 28 namespace mindspore { 29 namespace lite { 30 typedef std::unique_ptr<schema::PrimitiveT> (*PrimitiveTCreator)(const PrimitivePtr &primitive); 31 32 class MSOpsRegistry { 33 public: GetInstance()34 static MSOpsRegistry *GetInstance() { 35 static MSOpsRegistry registry; 36 return ®istry; 37 } InsertPrimitiveTMap(const std::string & name,PrimitiveTCreator creator)38 void InsertPrimitiveTMap(const std::string &name, PrimitiveTCreator creator) { 39 std::string lower_name = name; 40 (void)std::transform(name.begin(), name.end(), lower_name.begin(), ::tolower); 41 primitive_creators[lower_name] = creator; 42 } GetPrimitiveCreator(const std::string & name)43 PrimitiveTCreator GetPrimitiveCreator(const std::string &name) { 44 std::string lower_name = name; 45 (void)std::transform(name.begin(), name.end(), lower_name.begin(), ::tolower); 46 (void)lower_name.erase(std::remove(lower_name.begin(), lower_name.end(), '_'), lower_name.end()); 47 if (primitive_creators.find(lower_name) != primitive_creators.end()) { 48 return primitive_creators[lower_name]; 49 } else { 50 MS_LOG(WARNING) << "Unsupported primitive type in Create: " << name; 51 return nullptr; 52 } 53 } 54 55 protected: 56 std::map<std::string, PrimitiveTCreator> primitive_creators; 57 }; 58 59 class RegistryMSOps { 60 public: RegistryMSOps(const std::string & name,PrimitiveTCreator creator)61 RegistryMSOps(const std::string &name, PrimitiveTCreator creator) noexcept { 62 MSOpsRegistry::GetInstance()->InsertPrimitiveTMap(name, creator); 63 } 64 ~RegistryMSOps() = default; 65 }; 66 67 #define REG_MINDSPORE_OPERATOR(OP) \ 68 static RegistryMSOps g_##OP##PrimitiveCreatorRegistry(#OP, PrimitiveCreator<mindspore::ops::OP>); 69 } // namespace lite 70 } // namespace mindspore 71 #endif 72 73 #endif // MINDSPORE_LITE_SRC_COMMON_OPS_OPS_UTILS_H_ 74