2 * Copyright (c) 2018 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 __TFLITE_IMPORT_H__
18 #define __TFLITE_IMPORT_H__
20 #include <mio/tflite/schema_generated.h>
22 #include <tflchef.pb.h>
30 using TFliteSubGraphs_t = flatbuffers::Vector<flatbuffers::Offset<tflite::SubGraph>>;
31 using TFliteTensors_t = flatbuffers::Vector<flatbuffers::Offset<tflite::Tensor>>;
32 using TFliteBuffers_t = flatbuffers::Vector<flatbuffers::Offset<tflite::Buffer>>;
33 using TFliteOperators_t = flatbuffers::Vector<flatbuffers::Offset<tflite::Operator>>;
35 const char *tensor_type(const tflite::Tensor *tensor);
36 const char *tensor_name(const tflite::Tensor *tensor);
37 bool is_valid(const tflite::OperatorCode *opcode);
38 bool is_custom(const tflite::OperatorCode *opcode);
41 * @brief Loads TF lite file and provides helpers to access attributes
46 TFliteImport(const tflite::Model *model);
48 TFliteImport() = delete;
51 bool select_sub_graph(uint32_t subgraph);
54 const TFliteBuffers_t *buffers() { return _buffers; }
55 const TFliteTensors_t *tensors() { return _tensors; }
56 const TFliteOperators_t *operators() { return _operators; }
57 const std::vector<int32_t> &inputs() const { return _inputs; }
58 const std::vector<int32_t> &outputs() const { return _outputs; }
60 uint32_t num_subgraph() const { return _subgraphs->Length(); }
62 tflite::BuiltinOperator builtin_code(const tflite::Operator *op) const;
63 std::string opcode_name(const tflite::Operator *op) const;
64 size_t buffer_info(const tflite::Tensor *tensor, const uint8_t **buff_data);
67 * @brief This will record the tensor by index, if it needs filler option,
68 * such as kernel, bias.
70 void set_tensor_filler(uint32_t tensor_index) { _tensor_filler[tensor_index] = true; }
73 * @brief This will store int32 filler values such as reshape information for the tensor
75 void set_tensor_filler(uint32_t tensor_index, std::vector<int32_t> &expvalues)
77 _tensor_filler_vint32[tensor_index] = expvalues;
80 void set_tensor_filler(uint32_t tensor_index, std::vector<float> &expvalues)
82 _tensor_filler_vfloat[tensor_index] = expvalues;
86 * @brief This will return true if the tensor by index, needs a filler option.
88 bool get_tensor_filler(uint32_t tensor_index)
90 auto it = _tensor_filler.find(tensor_index);
91 if (it != _tensor_filler.end())
99 * @brief This will return true if the tensor by index, needs a int array filler option.
101 bool get_tensor_filler(uint32_t tensor_index, std::vector<int32_t> &expvalues)
103 auto it = _tensor_filler_vint32.find(tensor_index);
104 if (it != _tensor_filler_vint32.end())
106 expvalues = it->second;
112 bool get_tensor_filler(uint32_t tensor_index, std::vector<float> &expvalues)
114 auto it = _tensor_filler_vfloat.find(tensor_index);
115 if (it != _tensor_filler_vfloat.end())
117 expvalues = it->second;
124 const TFliteSubGraphs_t *_subgraphs{nullptr};
125 const TFliteBuffers_t *_buffers{nullptr};
126 const TFliteTensors_t *_tensors{nullptr};
127 const TFliteOperators_t *_operators{nullptr};
129 std::vector<const tflite::OperatorCode *> _op_codes{};
130 std::vector<int32_t> _inputs{};
131 std::vector<int32_t> _outputs{};
133 std::map<uint32_t, bool> _tensor_filler{};
134 std::map<uint32_t, std::vector<int32_t>> _tensor_filler_vint32{};
135 std::map<uint32_t, std::vector<float>> _tensor_filler_vfloat{};
138 } // namespace tflchef
140 #endif // __TFLITE_IMPORT_H__