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 #include "coder/opcoders/op_coder_register.h"
17 #include <utility>
18 #include <string>
19 #include "coder/utils/type_cast.h"
20 namespace mindspore::lite::micro {
operator <(const CoderKey rhs) const21 bool CoderKey::operator<(const CoderKey rhs) const {
22 return std::tie(this->target_, this->data_type_, this->op_type_) <
23 std::tie(rhs.target_, rhs.data_type_, rhs.op_type_);
24 }
25
ToString() const26 std::string CoderKey::ToString() const {
27 std::ostringstream code;
28 code << "target: " << EnumNameTarget(target_) << "\t"
29 << "data_type_: " << data_type_ << "\t"
30 << "op_type: " << schema::EnumNamePrimitiveType(static_cast<schema::PrimitiveType>(op_type_));
31 return code.str();
32 }
33
GetInstance()34 OpCoderFactory *OpCoderFactory::GetInstance() {
35 static OpCoderFactory reg;
36 return ®
37 }
38
RegistOpCoder(Target target,TypeId data_type,schema::PrimitiveType operator_type,const CoderCreatorFunc & creator_func)39 int OpCoderFactory::RegistOpCoder(Target target, TypeId data_type, schema::PrimitiveType operator_type,
40 const CoderCreatorFunc &creator_func) {
41 // check key
42 CoderKey key(target, data_type, operator_type);
43 // insert pair to registry
44 if (this->opcoder_sets_.find(key) != this->opcoder_sets_.end()) {
45 MS_LOG(ERROR) << "coder already exist: " << key.ToString();
46 return RET_ERROR;
47 }
48 this->opcoder_sets_.insert(std::pair<CoderKey, CoderCreatorFunc>(key, creator_func));
49 return RET_OK;
50 }
51
FindOpCoder(const CoderKey & key)52 CoderCreatorFunc OpCoderFactory::FindOpCoder(const CoderKey &key) {
53 auto iterator = this->opcoder_sets_.find(key);
54 if (iterator != this->opcoder_sets_.end()) {
55 return iterator->second;
56 }
57 // matching kAllTargets
58 iterator = this->opcoder_sets_.find(key.AllKey());
59 if (iterator != this->opcoder_sets_.end()) {
60 return iterator->second;
61 }
62 return nullptr;
63 }
64
OpCoderRegister(Target target,TypeId data_type,schema::PrimitiveType operator_type,const CoderCreatorFunc & creatorFunc)65 OpCoderRegister::OpCoderRegister(Target target, TypeId data_type, schema::PrimitiveType operator_type,
66 const CoderCreatorFunc &creatorFunc) {
67 OpCoderFactory::GetInstance()->RegistOpCoder(target, data_type, operator_type, creatorFunc);
68 }
69 } // namespace mindspore::lite::micro
70