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_INFER_PRIMITIVE_TYPE_H_
18 #define MINDSPORE_LITE_SRC_INFER_PRIMITIVE_TYPE_H_
19
20 #include "schema/model_generated.h"
21 #include "src/common/log_adapter.h"
22 #ifdef ENABLE_CLOUD_INFERENCE
23 #include <string>
24 #include <cstring>
25 #include <utility>
26 #include <vector>
27 #include <new>
28 #include <iostream>
29 #include <sstream>
30 #include <unordered_map>
31 #include <map>
32 #endif
33
34 namespace mindspore::kernel {
35 #ifndef ENABLE_CLOUD_INFERENCE
36 using PrimitiveType = mindspore::schema::PrimitiveType;
37 #else
38 class PrimitiveType {
39 public:
40 PrimitiveType() = default;
41 explicit PrimitiveType(std::string primitive_type);
42 explicit PrimitiveType(mindspore::schema::PrimitiveType primitive_type);
43 explicit PrimitiveType(int primitive_type);
44 virtual ~PrimitiveType() = default;
45
46 bool operator==(const std::string &other) const;
47 bool operator!=(const std::string &other) const;
48 bool operator==(mindspore::schema::PrimitiveType other) const;
49 bool operator!=(mindspore::schema::PrimitiveType other) const;
50 bool operator==(int other) const;
51 bool operator!=(int other) const;
52
53 PrimitiveType &operator=(const std::string &other);
54 PrimitiveType &operator=(const mindspore::schema::PrimitiveType &other);
55 PrimitiveType &operator=(int other);
56
57 std::string TypeName() const;
58 schema::PrimitiveType SchemaType() const;
59
60 private:
61 std::string protocolbuffers_type_;
62 int flatbuffers_type_{schema::PrimitiveType_NONE};
63 };
64 #endif
65
TypeName(const PrimitiveType & type)66 inline std::string TypeName(const PrimitiveType &type) {
67 #ifdef ENABLE_CLOUD_INFERENCE
68 return type.TypeName();
69 #else
70 return schema::EnumNamePrimitiveType(type);
71 #endif
72 }
73
SchemaType(const PrimitiveType & type)74 inline schema::PrimitiveType SchemaType(const PrimitiveType &type) {
75 #ifdef ENABLE_CLOUD_INFERENCE
76 return type.SchemaType();
77 #else
78 return type;
79 #endif
80 }
81
82 inline std::ostream &operator<<(std::ostream &os, const PrimitiveType &type) {
83 os << TypeName(type);
84 return os;
85 }
86
87 #ifdef USE_GLOG
88 inline LogStream &operator<<(LogStream &stream, const PrimitiveType &type) {
89 stream << TypeName(type);
90 return stream;
91 }
92 #endif
93 } // namespace mindspore::kernel
94 namespace mindspore::lite {
IsContain(const std::vector<schema::PrimitiveType> & vec,const kernel::PrimitiveType & element)95 inline bool IsContain(const std::vector<schema::PrimitiveType> &vec, const kernel::PrimitiveType &element) {
96 return std::any_of(vec.begin(), vec.end(),
97 [&element](const schema::PrimitiveType &stype) { return element == stype; });
98 }
99 } // namespace mindspore::lite
100 #endif
101