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);
145 template <typename T>
146 VectorWrapper<T>::VectorWrapper(const flatbuffers::Vector<T> *ptr) : _vector(ptr)
151 template <typename T> uint32_t VectorWrapper<T>::size() const
153 return null() ? 0 : _vector->size();
156 template <typename T> const T *VectorWrapper<T>::data() const
158 return null() ? nullptr : _vector->data();
161 template <typename T> typename VectorWrapper<T>::iterator VectorWrapper<T>::begin() const
163 return null() ? iterator(nullptr, 0) : _vector->begin();
166 template <typename T> typename VectorWrapper<T>::iterator VectorWrapper<T>::end() const
168 return null() ? begin() : _vector->end();
171 template <typename T> typename VectorWrapper<T>::value_type VectorWrapper<T>::at(uint32_t i) const
175 // TODO find better error message
176 assert(false && "Access to prohibited vector element");
179 return _vector->Get(i);
182 template <typename T>
183 typename VectorWrapper<T>::value_type VectorWrapper<T>::operator[](uint32_t i) const
188 template <typename T> bool VectorWrapper<T>::null() const { return _vector == nullptr; }
189 template <typename T> bool VectorWrapper<T>::empty() const { return size() == 0; }
191 #define REGISTER_WRAPPER(T) template class VectorWrapper<T>
192 REGISTER_WRAPPER(flatbuffers::Offset<circle::SubGraph>);
193 REGISTER_WRAPPER(flatbuffers::Offset<circle::Buffer>);
194 REGISTER_WRAPPER(flatbuffers::Offset<circle::Tensor>);
195 REGISTER_WRAPPER(flatbuffers::Offset<circle::Operator>);
196 REGISTER_WRAPPER(flatbuffers::Offset<circle::OperatorCode>);
197 REGISTER_WRAPPER(flatbuffers::Offset<circle::Metadata>);
198 REGISTER_WRAPPER(int32_t);
199 REGISTER_WRAPPER(uint8_t);
200 #undef REGISTER_WRAPPER
202 } // namespace luci_interpreter