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.
19 #include <mio_tflite2121/Helper.h>
28 Reader::Reader(const tflite::Model *model)
30 _subgraphs = model->subgraphs();
31 _buffers = model->buffers();
33 auto opcodes = model->operator_codes();
34 for (const ::tflite::OperatorCode *opcode : *opcodes)
36 _op_codes.push_back(opcode);
40 size_t Reader::buffer_info(uint32_t buf_idx, const uint8_t **buff_data)
42 if (buff_data != nullptr)
50 if (auto *buffer = (*_buffers)[buf_idx])
52 if (auto *array = buffer->data())
54 if (size_t size = array->size())
56 if (buff_data != nullptr)
58 *buff_data = reinterpret_cast<const uint8_t *>(array->data());
68 tflite::BuiltinOperator Reader::builtin_code(const tflite::Operator *op) const
70 uint32_t index = op->opcode_index();
71 assert(index < _op_codes.size());
72 const tflite::OperatorCode *opcode = _op_codes.at(index);
74 return mio::tflite::builtin_code_neutral(opcode);
77 std::string Reader::opcode_name(const tflite::Operator *op) const
79 uint32_t index = op->opcode_index();
80 assert(index < _op_codes.size());
81 const tflite::OperatorCode *opcode = _op_codes.at(index);
83 if (!mio::tflite::is_valid(opcode))
85 std::ostringstream oss;
86 oss << "(invalid: " << index << ")";
90 return mio::tflite::opcode_name(opcode);
93 bool Reader::select_subgraph(uint32_t sgindex)
101 if (_subgraphs->Length() <= sgindex)
107 const tflite::SubGraph *subgraph = (*_subgraphs)[sgindex];
109 _tensors = subgraph->tensors();
110 _operators = subgraph->operators();
112 _inputs = as_index_vector(subgraph->inputs());
113 _outputs = as_index_vector(subgraph->outputs());
118 } // namespace tflinspect