2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef __CIRCLE_EXPORTER_UTILS_H__
18 #define __CIRCLE_EXPORTER_UTILS_H__
20 #include "ExporterUtils.h"
22 #include "circle_schema_generated.h"
24 #include "Dialect/IR/TFLNodes.h"
28 #include <unordered_map>
32 namespace circle_detail
37 circle::BuiltinOperator opcode;
39 bool operator==(const OpCode &rhs) const { return opcode == rhs.opcode; }
42 } // namespace circle_detail
48 circle::ActivationFunctionType to_circle_actfunc(locoex::FusedActFunc func);
55 template <> struct hash<exo::circle_detail::OpCode>
57 size_t operator()(const exo::circle_detail::OpCode &x) const { return hash<int>()(x.opcode); }
64 namespace circle_detail
68 * @breif Record the information of T/F Lite SubGraph and its mapping to loco
70 struct SubGraphContext
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};
80 // Prerequisites for circle::Model object creation
81 struct SerializedModelData final : public SubGraphContext
83 SerializedModelData() = default;
84 SerializedModelData(const SerializedModelData &) = delete;
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;
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;
97 * @brief if opcode is not registered in table of opcodes add it
99 * @return idx of opcode in table of opcodes (see schema)
101 uint32_t registerBuiltinOpcode(circle::BuiltinOperator builtin_code);
102 uint32_t registerCustomOpcode(const std::string &custom_op);
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);
109 /// @brief Register graph input and output names to SerializedModelData
110 void registerGraphIOName(loco::Graph *graph, SerializedModelData &gd);
112 using TFLTensorIndex = int32_t;
114 void set_tensor_index(loco::Node *node, const TFLTensorIndex &tensor_id);
115 TFLTensorIndex get_tensor_index(loco::Node *node);
117 } // namespace circle_detail
120 #endif // __TFL_EXPORTER_UTILS_H__