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 __TFL_EXPORTER_UTILS_H__
18 #define __TFL_EXPORTER_UTILS_H__
20 #include "ExporterUtils.h"
22 #include "schema_generated.h"
24 #include "Dialect/IR/TFLNodes.h"
28 #include <unordered_map>
32 namespace tflite_detail
37 tflite::BuiltinOperator opcode;
39 bool operator==(const OpCode &rhs) const { return opcode == rhs.opcode; }
42 } // namespace tflite_detail
48 tflite::ActivationFunctionType to_tflite_actfunc(locoex::FusedActFunc func);
55 template <> struct hash<exo::tflite_detail::OpCode>
57 size_t operator()(const exo::tflite_detail::OpCode &x) const { return hash<int>()(x.opcode); }
64 namespace tflite_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;
78 // Prerequisites for tflite::Model object creation
79 struct SerializedModelData final : public SubGraphContext
81 SerializedModelData() = default;
82 SerializedModelData(const SerializedModelData &) = delete;
84 std::unordered_map<OpCode, uint32_t> _operator_codes;
85 std::unordered_map<OpCode, std::string> _custom_operator_codes;
86 std::vector<flatbuffers::Offset<tflite::Operator>> _operators;
87 std::vector<flatbuffers::Offset<tflite::Tensor>> _tensors;
88 std::vector<flatbuffers::Offset<tflite::Buffer>> _buffers;
90 // Graph input and output names
91 std::unordered_map<loco::Pull *, std::string> _pull_to_name;
92 std::unordered_map<loco::Push *, std::string> _push_to_name;
95 * @brief if opcode is not registered in table of opcodes add it
97 * @return idx of opcode in table of opcodes (see schema)
99 uint32_t registerBuiltinOpcode(tflite::BuiltinOperator builtin_code);
100 uint32_t registerCustomOpcode(const std::string &custom_op);
103 tflite::Padding getOpPadding(const loco::Padding2D *pad, const loco::Stride<2> *stride,
104 const ShapeDescription &ifm, const ShapeDescription &ofm);
105 tflite::Padding getOpPadding(const locoex::Padding pad);
107 /// @brief Register graph input and output names to SerializedModelData
108 void registerGraphIOName(loco::Graph *graph, SerializedModelData &gd);
110 using TFLTensorIndex = int32_t;
112 void set_tensor_index(loco::Node *node, const TFLTensorIndex &tensor_id);
113 TFLTensorIndex get_tensor_index(loco::Node *node);
115 } // namespace tflite_detail
118 #endif // __TFL_EXPORTER_UTILS_H__