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 LITE_MINDSPORE_LITE_C_OPS_OP_COMPAT_REGISTER_H_ 18 #define LITE_MINDSPORE_LITE_C_OPS_OP_COMPAT_REGISTER_H_ 19 20 #include <unordered_map> 21 #include <string> 22 #include <vector> 23 #include "include/model.h" 24 #include "schema/model_generated.h" 25 #include "src/common/log_adapter.h" 26 #include "src/common/version_manager.h" 27 28 namespace mindspore { 29 namespace lite { 30 // compatibility, transfer attr to input tensor. 31 typedef int (*TransferAttrFunc)(LiteGraph::Node *node, std::vector<schema::Tensor *> *tensor, 32 std::vector<char *> *const tensor_bufs); 33 class CompatRegistry { 34 public: GetInstance()35 static CompatRegistry *GetInstance() { 36 static CompatRegistry registry; 37 return ®istry; 38 } 39 InsertTransferAttrFuncMap(int schema_version,int primitive_type,TransferAttrFunc transfer_attr_func)40 void InsertTransferAttrFuncMap(int schema_version, int primitive_type, TransferAttrFunc transfer_attr_func) { 41 int key = primitive_type * 10 + schema_version; 42 transfer_attr_funcs_[key] = transfer_attr_func; 43 } 44 GetTransferAttrFunc(int schema_version,int primitive_type)45 TransferAttrFunc GetTransferAttrFunc(int schema_version, int primitive_type) { 46 int key = primitive_type * 10 + schema_version; 47 if (transfer_attr_funcs_.find(key) != transfer_attr_funcs_.end()) { 48 return transfer_attr_funcs_[key]; 49 } else { 50 MS_LOG(DEBUG) << "Unsupported transformer type in Create : " << key; 51 return nullptr; 52 } 53 } 54 55 protected: 56 std::unordered_map<int, TransferAttrFunc> transfer_attr_funcs_; 57 }; 58 59 class Register { 60 public: Register(int schema_version,int primitive_type,TransferAttrFunc transfer_attr_func)61 Register(int schema_version, int primitive_type, TransferAttrFunc transfer_attr_func) { 62 CompatRegistry::GetInstance()->InsertTransferAttrFuncMap(schema_version, primitive_type, transfer_attr_func); 63 } 64 virtual ~Register() = default; 65 }; 66 } // namespace lite 67 } // namespace mindspore 68 #endif // LITE_MINDSPORE_LITE_C_OPS_OP_COMPAT_REGISTER_H_ 69