17#include "mio_circle/Reader.h"
18#include "mio_circle/Helper.h"
33 throw std::runtime_error(
"Invalid model");
36 _version =
model->version();
37 _subgraphs =
model->subgraphs();
38 _buffers =
model->buffers();
39 _metadata =
model->metadata();
40 _signature_defs =
model->signature_defs();
43 for (const ::circle::OperatorCode *opcode : *
opcodes)
45 _op_codes.push_back(opcode);
49Reader::Reader(const ::circle::Model *model,
const std::vector<char> *rawdata)
53 throw std::runtime_error(
"Invalid model");
58 _version = model->version();
59 _subgraphs = model->subgraphs();
60 _buffers = model->buffers();
61 _metadata = model->metadata();
62 _signature_defs = model->signature_defs();
64 auto opcodes = model->operator_codes();
65 for (const ::circle::OperatorCode *opcode : *
opcodes)
67 _op_codes.push_back(opcode);
73 if (buff_data !=
nullptr)
81 if (
auto *buffer = (*_buffers)[buf_idx])
83 assert(buffer->offset() == 0);
85 if (
auto *array = buffer->data())
87 if (
size_t size = array->size())
89 if (buff_data !=
nullptr)
91 *buff_data =
reinterpret_cast<const uint8_t *
>(array->data());
105 if (buff_data !=
nullptr)
107 *buff_data =
nullptr;
113 if (
auto *buffer = (*_buffers)[buf_idx])
115 auto buffer_offset = buffer->offset();
116 if (buffer_offset > 1)
119 if (_rawdata ==
nullptr)
123 if (buff_data !=
nullptr)
125 *buff_data =
reinterpret_cast<const uint8_t *
>(&_rawdata->at(buffer_offset));
127 return buffer->size();
129 else if (
auto *array = buffer->data())
131 if (
size_t size = array->size())
133 if (buff_data !=
nullptr)
135 *buff_data =
reinterpret_cast<const uint8_t *
>(array->data());
142 if (buffer->offset() == 1 && buffer->size() == 1)
144 std::cerr <<
"Buffer " << buf_idx <<
" has invalid offset/size." << std::endl;
154 uint32_t index = op->opcode_index();
155 assert(index < _op_codes.size());
156 const ::circle::OperatorCode *opcode = _op_codes.at(index);
163 uint32_t index = op->opcode_index();
164 assert(index < _op_codes.size());
165 const ::circle::OperatorCode *opcode = _op_codes.at(index);
169 std::ostringstream oss;
170 oss <<
"(invalid: " << index <<
")";
177std::vector<int32_t>
Reader::outputs(const ::circle::Operator *op)
const
194 _subgraph_index = sgindex;
196 _operators =
nullptr;
201 if (_subgraphs->
size() <= sgindex)
207 const ::circle::SubGraph *subgraph = (*_subgraphs)[sgindex];
209 auto name = subgraph->name();
210 _subgraph_name = name ? name->c_str() :
"(noname)";
212 _tensors = subgraph->tensors();
213 _operators = subgraph->operators();
const std::vector< const ::circle::OperatorCode * > & opcodes()
std::string opcode_name(const ::circle::Operator *op) const
const std::vector< int32_t > & outputs() const
std::string tensor_name(const ::circle::Tensor *tensor) const
size_t buffer_info(uint32_t buf_idx, const uint8_t **buff_data)
std::string tensor_dtype(const ::circle::Tensor *tensor) const
::circle::BuiltinOperator builtin_code(const ::circle::Operator *op) const
bool select_subgraph(uint32_t subgraph)
const char * tensor_name(const ::circle::Tensor *tensor)
std::string opcode_name(const ::circle::OperatorCode *opcode)
std::vector< T > as_index_vector(const flatbuffers::Vector< T > *flat_array)
const char * tensor_type(const ::circle::Tensor *tensor)
bool is_valid(const ::circle::OperatorCode *opcode)
::circle::BuiltinOperator builtin_code_neutral(const ::circle::OperatorCode *opcode)