Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / compiler / exo / src / Circle / CircleExporterUtils.h
1 /*
2  * Copyright (c) 2019 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 __CIRCLE_EXPORTER_UTILS_H__
18 #define __CIRCLE_EXPORTER_UTILS_H__
19
20 #include "ExporterUtils.h"
21
22 #include "circle_schema_generated.h"
23
24 #include "Dialect/IR/TFLNodes.h"
25
26 #include <loco.h>
27
28 #include <unordered_map>
29
30 namespace exo
31 {
32 namespace circle_detail
33 {
34
35 struct OpCode
36 {
37   circle::BuiltinOperator opcode;
38
39   bool operator==(const OpCode &rhs) const { return opcode == rhs.opcode; }
40 };
41
42 } // namespace circle_detail
43 } // namespace exo
44
45 namespace exo
46 {
47
48 circle::ActivationFunctionType to_circle_actfunc(locoex::FusedActFunc func);
49
50 } // namespace exo
51
52 namespace std
53 {
54
55 template <> struct hash<exo::circle_detail::OpCode>
56 {
57   size_t operator()(const exo::circle_detail::OpCode &x) const { return hash<int>()(x.opcode); }
58 };
59
60 } // namespace std
61
62 namespace exo
63 {
64 namespace circle_detail
65 {
66
67 /**
68  * @brief Record the information of T/F Lite SubGraph and its mapping to loco
69  */
70 struct SubGraphContext
71 {
72   /// @brief SubGraph input tensor id
73   std::vector<int32_t> _inputs;
74   /// @brief SubGraph output tensor id
75   std::vector<int32_t> _outputs;
76   /// @DataFormat for SubGraph
77   circle::DataFormat _data_format{circle::DataFormat::DataFormat_CHANNELS_LAST};
78 };
79
80 // Prerequisites for circle::Model object creation
81 struct SerializedModelData final : public SubGraphContext
82 {
83   SerializedModelData() = default;
84   SerializedModelData(const SerializedModelData &) = delete;
85
86   std::unordered_map<OpCode, uint32_t> _operator_codes;
87   std::unordered_map<OpCode, std::string> _custom_operator_codes;
88   std::vector<flatbuffers::Offset<circle::Operator>> _operators;
89   std::vector<flatbuffers::Offset<circle::Tensor>> _tensors;
90   std::vector<flatbuffers::Offset<circle::Buffer>> _buffers;
91
92   // Graph input and output names
93   std::unordered_map<loco::Pull *, std::string> _pull_to_name;
94   std::unordered_map<loco::Push *, std::string> _push_to_name;
95
96   /**
97    * @brief if opcode is not registered in table of opcodes add it
98    * @param builtin_code
99    * @return idx of opcode in table of opcodes (see schema)
100    */
101   uint32_t registerBuiltinOpcode(circle::BuiltinOperator builtin_code);
102   uint32_t registerCustomOpcode(const std::string &custom_op);
103 };
104
105 circle::Padding getOpPadding(const loco::Padding2D *pad, const loco::Stride<2> *stride,
106                              const ShapeDescription &ifm, const ShapeDescription &ofm);
107 circle::Padding getOpPadding(const locoex::Padding pad);
108
109 /// @brief Register graph input and output names to SerializedModelData
110 void registerGraphIOName(loco::Graph *graph, SerializedModelData &gd);
111
112 using TFLTensorIndex = int32_t;
113
114 void set_tensor_index(loco::Node *node, const TFLTensorIndex &tensor_id);
115 TFLTensorIndex get_tensor_index(loco::Node *node);
116
117 } // namespace circle_detail
118 } // namespace exo
119
120 #endif // __TFL_EXPORTER_UTILS_H__