2 * Copyright (c) 2020 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_IMPORT_H__
18 #define __CIRCLE_IMPORT_H__
20 #include <mio/circle/schema_generated.h>
22 #include <circlechef.pb.h>
30 using CircleSubGraphs_t = flatbuffers::Vector<flatbuffers::Offset<circle::SubGraph>>;
31 using CircleTensors_t = flatbuffers::Vector<flatbuffers::Offset<circle::Tensor>>;
32 using CircleBuffers_t = flatbuffers::Vector<flatbuffers::Offset<circle::Buffer>>;
33 using CircleOperators_t = flatbuffers::Vector<flatbuffers::Offset<circle::Operator>>;
35 const char *tensor_type(const circle::Tensor *tensor);
36 const char *tensor_name(const circle::Tensor *tensor);
37 bool is_valid(const circle::OperatorCode *opcode);
38 bool is_custom(const circle::OperatorCode *opcode);
41 * @brief Loads TF lite file and provides helpers to access attributes
46 CircleImport(const circle::Model *model);
48 CircleImport() = delete;
51 bool select_sub_graph(uint32_t subgraph);
54 const CircleBuffers_t *buffers() { return _buffers; }
55 const CircleTensors_t *tensors() { return _tensors; }
56 const CircleOperators_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 circle::BuiltinOperator builtin_code(const circle::Operator *op) const;
63 std::string opcode_name(const circle::Operator *op) const;
64 size_t buffer_info(const circle::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 CircleSubGraphs_t *_subgraphs{nullptr};
125 const CircleBuffers_t *_buffers{nullptr};
126 const CircleTensors_t *_tensors{nullptr};
127 const CircleOperators_t *_operators{nullptr};
129 std::vector<const circle::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 circlechef
140 #endif // __CIRCLE_IMPORT_H__