ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Reader.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 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 "mio_circle/Reader.h"
18#include "mio_circle/Helper.h"
19
20#include <iostream>
21#include <sstream>
22#include <string>
23
24namespace mio
25{
26namespace circle
27{
28
29Reader::Reader(const ::circle::Model *model)
30{
31 if (model == nullptr)
32 {
33 throw std::runtime_error("Invalid model");
34 }
35
36 _version = model->version();
37 _subgraphs = model->subgraphs();
38 _buffers = model->buffers();
39 _metadata = model->metadata();
40 _signature_defs = model->signature_defs();
41
42 auto opcodes = model->operator_codes();
43 for (const ::circle::OperatorCode *opcode : *opcodes)
44 {
45 _op_codes.push_back(opcode);
46 }
47}
48
49Reader::Reader(const ::circle::Model *model, const std::vector<char> *rawdata)
50{
51 if (model == nullptr)
52 {
53 throw std::runtime_error("Invalid model");
54 }
55
56 _rawdata = rawdata;
57
58 _version = model->version();
59 _subgraphs = model->subgraphs();
60 _buffers = model->buffers();
61 _metadata = model->metadata();
62 _signature_defs = model->signature_defs();
63
64 auto opcodes = model->operator_codes();
65 for (const ::circle::OperatorCode *opcode : *opcodes)
66 {
67 _op_codes.push_back(opcode);
68 }
69}
70
71size_t Reader::buffer_info(uint32_t buf_idx, const uint8_t **buff_data)
72{
73 if (buff_data != nullptr)
74 {
75 *buff_data = nullptr;
76 }
77
78 if (buf_idx == 0)
79 return 0;
80
81 if (auto *buffer = (*_buffers)[buf_idx])
82 {
83 assert(buffer->offset() == 0);
84
85 if (auto *array = buffer->data())
86 {
87 if (size_t size = array->size())
88 {
89 if (buff_data != nullptr)
90 {
91 *buff_data = reinterpret_cast<const uint8_t *>(array->data());
92 }
93 return size;
94 }
95 }
96 }
97
98 return 0;
99}
100
101size_t Reader::buffer_info(uint32_t buf_idx, const uint8_t **buff_data, bool &ext_offset)
102{
103 ext_offset = false;
104
105 if (buff_data != nullptr)
106 {
107 *buff_data = nullptr;
108 }
109
110 if (buf_idx == 0)
111 return 0;
112
113 if (auto *buffer = (*_buffers)[buf_idx])
114 {
115 auto buffer_offset = buffer->offset();
116 if (buffer_offset > 1)
117 {
118 assert(_rawdata); // make debug break for invalid case
119 if (_rawdata == nullptr)
120 return 0;
121
122 ext_offset = true;
123 if (buff_data != nullptr)
124 {
125 *buff_data = reinterpret_cast<const uint8_t *>(&_rawdata->at(buffer_offset));
126 }
127 return buffer->size();
128 }
129 else if (auto *array = buffer->data())
130 {
131 if (size_t size = array->size())
132 {
133 if (buff_data != nullptr)
134 {
135 *buff_data = reinterpret_cast<const uint8_t *>(array->data());
136 }
137 return size;
138 }
139 }
140 else
141 {
142 if (buffer->offset() == 1 && buffer->size() == 1)
143 {
144 std::cerr << "Buffer " << buf_idx << " has invalid offset/size." << std::endl;
145 }
146 }
147 }
148
149 return 0;
150}
151
152::circle::BuiltinOperator Reader::builtin_code(const ::circle::Operator *op) const
153{
154 uint32_t index = op->opcode_index();
155 assert(index < _op_codes.size());
156 const ::circle::OperatorCode *opcode = _op_codes.at(index);
157
159}
160
161std::string Reader::opcode_name(const ::circle::Operator *op) const
162{
163 uint32_t index = op->opcode_index();
164 assert(index < _op_codes.size());
165 const ::circle::OperatorCode *opcode = _op_codes.at(index);
166
167 if (!mio::circle::is_valid(opcode))
168 {
169 std::ostringstream oss;
170 oss << "(invalid: " << index << ")";
171 return oss.str();
172 }
173
174 return mio::circle::opcode_name(opcode);
175}
176
177std::vector<int32_t> Reader::outputs(const ::circle::Operator *op) const
178{
179 return as_index_vector(op->outputs());
180}
181
182std::string Reader::tensor_name(const ::circle::Tensor *tensor) const
183{
184 return mio::circle::tensor_name(tensor);
185}
186
187std::string Reader::tensor_dtype(const ::circle::Tensor *tensor) const
188{
189 return mio::circle::tensor_type(tensor);
190}
191
192bool Reader::select_subgraph(uint32_t sgindex)
193{
194 _subgraph_index = sgindex;
195 _tensors = nullptr;
196 _operators = nullptr;
197
198 _inputs.clear();
199 _outputs.clear();
200
201 if (_subgraphs->size() <= sgindex)
202 {
203 assert(false);
204 return false;
205 }
206
207 const ::circle::SubGraph *subgraph = (*_subgraphs)[sgindex];
208
209 auto name = subgraph->name();
210 _subgraph_name = name ? name->c_str() : "(noname)";
211
212 _tensors = subgraph->tensors();
213 _operators = subgraph->operators();
214
215 _inputs = as_index_vector(subgraph->inputs());
216 _outputs = as_index_vector(subgraph->outputs());
217
218 return true;
219}
220
221} // namespace circle
222} // namespace mio
uoffset_t size() const
const std::vector< const ::circle::OperatorCode * > & opcodes()
Definition Reader.h:56
std::string opcode_name(const ::circle::Operator *op) const
Definition Reader.cpp:85
const std::vector< int32_t > & outputs() const
Definition Reader.h:61
std::string tensor_name(const ::circle::Tensor *tensor) const
Definition Reader.cpp:106
size_t buffer_info(uint32_t buf_idx, const uint8_t **buff_data)
Definition Reader.cpp:48
std::string tensor_dtype(const ::circle::Tensor *tensor) const
Definition Reader.cpp:111
::circle::BuiltinOperator builtin_code(const ::circle::Operator *op) const
Definition Reader.cpp:76
bool select_subgraph(uint32_t subgraph)
Definition Reader.cpp:116
const char * tensor_name(const ::circle::Tensor *tensor)
Definition Helper.cpp:69
std::string opcode_name(const ::circle::OperatorCode *opcode)
Definition Helper.cpp:38
std::vector< T > as_index_vector(const flatbuffers::Vector< T > *flat_array)
Definition Helper.h:36
const char * tensor_type(const ::circle::Tensor *tensor)
Definition Helper.cpp:64
bool is_valid(const ::circle::OperatorCode *opcode)
Definition Helper.cpp:26
::circle::BuiltinOperator builtin_code_neutral(const ::circle::OperatorCode *opcode)
Definition Helper.cpp:33
Definition Helper.h:23
int32_t size[5]
Definition Slice.cpp:35