ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Read.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include "Read.h"
18
20
21#include <iostream>
22#include <sstream>
23#include <string>
24
25namespace tflread
26{
27
28Reader::Reader(const tflite::Model *model)
29{
30 _version = model->version();
31 _subgraphs = model->subgraphs();
32 _buffers = model->buffers();
33 _metadata = model->metadata();
34 _signaturedefs = model->signature_defs();
35
36 auto opcodes = model->operator_codes();
37 for (const ::tflite::OperatorCode *opcode : *opcodes)
38 {
39 _op_codes.push_back(opcode);
40 }
41}
42
43Reader::Reader(const tflite::Model *model, const std::vector<char> *rawdata)
44{
45 _rawdata = rawdata;
46
47 _version = model->version();
48 _subgraphs = model->subgraphs();
49 _buffers = model->buffers();
50 _metadata = model->metadata();
51 _signaturedefs = model->signature_defs();
52
53 auto opcodes = model->operator_codes();
54 for (const ::tflite::OperatorCode *opcode : *opcodes)
55 {
56 _op_codes.push_back(opcode);
57 }
58}
59
60size_t Reader::buffer_info(uint32_t buf_idx, const uint8_t **buff_data)
61{
62 *buff_data = nullptr;
63
64 if (buf_idx == 0)
65 return 0;
66
67 if (auto *buffer = (*_buffers)[buf_idx])
68 {
69 if (auto *array = buffer->data())
70 {
71 if (size_t size = array->size())
72 {
73 *buff_data = reinterpret_cast<const uint8_t *>(array->data());
74 return size;
75 }
76 }
77 }
78
79 return 0;
80}
81
82size_t Reader::buffer_info(uint32_t buf_idx, const uint8_t **buff_data, bool &ext_offset)
83{
84 *buff_data = nullptr;
85 ext_offset = false;
86
87 if (buf_idx == 0)
88 return 0;
89
90 if (auto *buffer = (*_buffers)[buf_idx])
91 {
92 auto buffer_offset = buffer->offset();
93 if (buffer->offset() > 1)
94 {
95 assert(_rawdata); // make debug break for invalid case
96 if (_rawdata == nullptr)
97 return 0;
98
99 ext_offset = true;
100 *buff_data = reinterpret_cast<const uint8_t *>(&_rawdata->at(buffer_offset));
101 return buffer->size();
102 }
103 else if (auto *array = buffer->data())
104 {
105 if (size_t size = array->size())
106 {
107 *buff_data = reinterpret_cast<const uint8_t *>(array->data());
108 return size;
109 }
110 }
111 else
112 {
113 if (buffer->offset() == 1 && buffer->size() == 1)
114 {
115 std::cerr << "Buffer " << buf_idx << " has invalid offset/size." << std::endl;
116 }
117 }
118 }
119
120 return 0;
121}
122
123tflite::BuiltinOperator Reader::builtin_code(const tflite::Operator *op) const
124{
125 uint32_t index = op->opcode_index();
126 assert(index < _op_codes.size());
127 const tflite::OperatorCode *opcode = _op_codes.at(index);
128
130}
131
132std::string Reader::opcode_name(const tflite::Operator *op) const
133{
134 uint32_t index = op->opcode_index();
135 assert(index < _op_codes.size());
136 const tflite::OperatorCode *opcode = _op_codes.at(index);
137
138 if (!mio::tflite::is_valid(opcode))
139 {
140 std::ostringstream oss;
141 oss << "(invalid: " << index << ")";
142 return oss.str();
143 }
144
145 return mio::tflite::opcode_name(opcode);
146}
147
148bool Reader::select_subgraph(uint32_t sgindex)
149{
150 _subgraph_index = sgindex;
151 _tensors = nullptr;
152 _operators = nullptr;
153
154 _inputs.clear();
155 _outputs.clear();
156
157 if (_subgraphs->size() <= sgindex)
158 {
159 assert(false);
160 return false;
161 }
162
163 const tflite::SubGraph *subgraph = (*_subgraphs)[sgindex];
164
165 auto name = subgraph->name();
166 _subgraph_name = name ? name->c_str() : "(noname)";
167
168 _tensors = subgraph->tensors();
169 _operators = subgraph->operators();
170
171 _inputs = as_index_vector(subgraph->inputs());
172 _outputs = as_index_vector(subgraph->outputs());
173
174 return true;
175}
176
177} // namespace tflread
uoffset_t size() const
const std::vector< const tflite::OperatorCode * > & opcodes()
Definition Read.h:61
tflite::BuiltinOperator builtin_code(const tflite::Operator *op) const
Definition Read.cpp:123
size_t buffer_info(uint32_t buf_idx, const uint8_t **buff_data)
Definition Read.cpp:60
bool select_subgraph(uint32_t subgraph)
Definition Read.cpp:148
std::string opcode_name(const tflite::Operator *op) const
Definition Read.cpp:132
std::string opcode_name(const ::tflite::OperatorCode *opcode)
Definition Helper.cpp:61
bool is_valid(const ::tflite::OperatorCode *opcode)
Definition Helper.cpp:40
::tflite::BuiltinOperator builtin_code_neutral(const ::tflite::OperatorCode *opcode)
Definition Helper.cpp:33
std::vector< T > as_index_vector(const flatbuffers::Vector< T > *flat_array)
Definition Read.h:29
int32_t size[5]
Definition Slice.cpp:35