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 #include "luci_interpreter/core/reader/CircleMicroReader.h"
18 #include "luci_interpreter/core/reader/CircleMicroReaderHelper.h"
22 namespace luci_interpreter
25 // TODO check can we remove it
26 DataType luci_datatype(const circle::TensorType type)
30 case circle::TensorType_FLOAT32:
31 return DataType::FLOAT32;
32 case circle::TensorType_FLOAT16:
33 return DataType::FLOAT16;
34 case circle::TensorType_INT32:
36 case circle::TensorType_UINT8:
38 case circle::TensorType_INT64:
40 case circle::TensorType_BOOL:
41 return DataType::BOOL;
42 case circle::TensorType_INT16:
44 case circle::TensorType_COMPLEX64:
46 case circle::TensorType_INT8:
52 return DataType::Unknown;
55 FusedActFunc luci_actfunc(const circle::ActivationFunctionType type)
59 case circle::ActivationFunctionType::ActivationFunctionType_NONE:
60 return FusedActFunc::NONE;
61 case circle::ActivationFunctionType::ActivationFunctionType_RELU:
62 return FusedActFunc::RELU;
63 case circle::ActivationFunctionType::ActivationFunctionType_RELU_N1_TO_1:
64 return FusedActFunc::RELU_N1_TO_1;
65 case circle::ActivationFunctionType::ActivationFunctionType_RELU6:
66 return FusedActFunc::RELU6;
67 case circle::ActivationFunctionType::ActivationFunctionType_TANH:
68 return FusedActFunc::TANH;
69 case circle::ActivationFunctionType::ActivationFunctionType_SIGN_BIT:
70 return FusedActFunc::SIGN_BIT;
75 return FusedActFunc::UNDEFINED;
78 Padding luci_padding(const circle::Padding padding)
82 case circle::Padding::Padding_SAME:
84 case circle::Padding::Padding_VALID:
85 return Padding::VALID;
88 return Padding::UNDEFINED;
91 MirrorPadMode luci_mirrorpad_mode(const circle::MirrorPadMode mode)
95 case circle::MirrorPadMode::MirrorPadMode_REFLECT:
96 return MirrorPadMode::REFLECT;
97 case circle::MirrorPadMode::MirrorPadMode_SYMMETRIC:
98 return MirrorPadMode::SYMMETRIC;
101 return MirrorPadMode::UNDEFINED;
104 circle::BuiltinOperator CircleReader::builtin_code(const circle::Operator *op) const
106 assert(op != nullptr);
108 const auto op_codes = opcodes();
109 uint32_t index = op->opcode_index();
110 assert(index < op_codes.size());
111 const auto opcode = op_codes[index];
112 assert(opcode != nullptr);
114 return circle::builtin_code_neutral(opcode);
117 bool CircleReader::parse(const circle::Model *model)
119 assert(model != nullptr);
121 // for direct pointer access
127 bool CircleReader::select_subgraph(uint32_t sgindex)
129 if (num_subgraph() <= sgindex)
135 // for direct pointer access
136 auto subgraphs = _model->subgraphs();
137 assert(subgraphs != nullptr);
139 _current_subgraph = subgraphs->Get(sgindex);
140 assert(_current_subgraph != nullptr);
142 _current_subgraph_index = sgindex;
147 template <typename T>
148 VectorWrapper<T>::VectorWrapper(const flatbuffers::Vector<T> *ptr) : _vector(ptr)
153 template <typename T> uint32_t VectorWrapper<T>::size() const
155 return null() ? 0 : _vector->size();
158 template <typename T> const T *VectorWrapper<T>::data() const
160 return null() ? nullptr : _vector->data();
163 template <typename T> typename VectorWrapper<T>::iterator VectorWrapper<T>::begin() const
165 return null() ? iterator(nullptr, 0) : _vector->begin();
168 template <typename T> typename VectorWrapper<T>::iterator VectorWrapper<T>::end() const
170 return null() ? begin() : _vector->end();
173 template <typename T> typename VectorWrapper<T>::value_type VectorWrapper<T>::at(uint32_t i) const
177 // TODO find better error message
178 assert(false && "Access to prohibited vector element");
181 return _vector->Get(i);
184 template <typename T>
185 typename VectorWrapper<T>::value_type VectorWrapper<T>::operator[](uint32_t i) const
190 template <typename T> bool VectorWrapper<T>::null() const { return _vector == nullptr; }
191 template <typename T> bool VectorWrapper<T>::empty() const { return size() == 0; }
193 #define REGISTER_WRAPPER(T) template class VectorWrapper<T>
194 REGISTER_WRAPPER(flatbuffers::Offset<circle::SubGraph>);
195 REGISTER_WRAPPER(flatbuffers::Offset<circle::Buffer>);
196 REGISTER_WRAPPER(flatbuffers::Offset<circle::Tensor>);
197 REGISTER_WRAPPER(flatbuffers::Offset<circle::Operator>);
198 REGISTER_WRAPPER(flatbuffers::Offset<circle::OperatorCode>);
199 REGISTER_WRAPPER(flatbuffers::Offset<circle::Metadata>);
200 REGISTER_WRAPPER(int32_t);
201 REGISTER_WRAPPER(uint8_t);
202 #undef REGISTER_WRAPPER
204 } // namespace luci_interpreter