Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / compiler / luci / export / src / SerializedData.h
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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 __SERIALIZED_DATA_H__
18 #define __SERIALIZED_DATA_H__
19
20 #include <mio/circle/schema_generated.h>
21
22 #include <luci/IR/CircleNodes.h>
23
24 #include <vector>
25
26 #include <unordered_map>
27 #include <map>
28
29 namespace luci
30 {
31
32 struct OpCode
33 {
34   circle::BuiltinOperator opcode;
35   std::string custom_code{""};
36   int32_t version = 1;
37
38   bool operator==(const OpCode &rhs) const
39   {
40     if (opcode == circle::BuiltinOperator_CUSTOM)
41     {
42       return custom_code == rhs.custom_code;
43     }
44     else
45     {
46       return opcode == rhs.opcode;
47     }
48   }
49 };
50
51 } // namespace luci
52
53 namespace std
54 {
55
56 template <> struct hash<luci::OpCode>
57 {
58   size_t operator()(const luci::OpCode &x) const { return hash<int>()(x.opcode); }
59 };
60
61 } // namespace std
62
63 namespace luci
64 {
65
66 /**
67  * @brief Record the information of T/F Lite SubGraph and its mapping to loco
68  */
69 struct SubGraphContext
70 {
71   /// @brief SubGraph name
72   std::string _name;
73   /// @brief SubGraph input tensor id
74   std::vector<int32_t> _inputs;
75   /// @brief SubGraph output tensor id
76   std::vector<int32_t> _outputs;
77   /// @brief DataFormat for SubGraph
78   circle::DataFormat _data_format{circle::DataFormat::DataFormat_CHANNELS_LAST};
79 };
80
81 // Prerequisites for circle::Model object creation
82 struct SerializedModelData final
83 {
84   SerializedModelData() = default;
85   SerializedModelData(const SerializedModelData &) = delete;
86
87   std::unordered_map<OpCode, uint32_t> _operator_codes;
88   std::vector<flatbuffers::Offset<circle::Buffer>> _buffers;
89
90   // This is used for removing buffers with same values
91   std::map<luci::CircleConst *, uint32_t> _cached_buffer_id;
92
93   /**
94    * @brief if opcode is not registered in table of opcodes add it
95    * @param builtin_code
96    * @return idx of opcode in table of opcodes (see schema)
97    */
98   uint32_t registerBuiltinOpcode(circle::BuiltinOperator builtin_code, const int32_t op_version);
99   uint32_t registerCustomOpcode(const std::string &custom_op);
100 };
101
102 // Prerequisites for circle::Model object creation
103 struct SerializedGraphData final : public SubGraphContext
104 {
105   SerializedGraphData() = default;
106   SerializedGraphData(const SerializedModelData &) = delete;
107
108   std::vector<flatbuffers::Offset<circle::Operator>> _operators;
109   std::vector<flatbuffers::Offset<circle::Tensor>> _tensors;
110 };
111
112 } // namespace luci
113
114 #endif // __SERIALIZED_DATA_H__