2 * Copyright (c) 2023 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 "mio_circle/Reader.h"
18 #include "mio_circle/Helper.h"
28 Reader::Reader(const ::circle::Model *model)
32 throw std::runtime_error("Invalid model");
35 _version = model->version();
36 _subgraphs = model->subgraphs();
37 _buffers = model->buffers();
38 _metadata = model->metadata();
39 _signature_defs = model->signature_defs();
41 auto opcodes = model->operator_codes();
42 for (const ::circle::OperatorCode *opcode : *opcodes)
44 _op_codes.push_back(opcode);
48 size_t Reader::buffer_info(uint32_t buf_idx, const uint8_t **buff_data)
50 if (buff_data != nullptr)
58 if (auto *buffer = (*_buffers)[buf_idx])
60 if (auto *array = buffer->data())
62 if (size_t size = array->size())
64 if (buff_data != nullptr)
66 *buff_data = reinterpret_cast<const uint8_t *>(array->data());
76 ::circle::BuiltinOperator Reader::builtin_code(const ::circle::Operator *op) const
78 uint32_t index = op->opcode_index();
79 assert(index < _op_codes.size());
80 const ::circle::OperatorCode *opcode = _op_codes.at(index);
82 return mio::circle::builtin_code_neutral(opcode);
85 std::string Reader::opcode_name(const ::circle::Operator *op) const
87 uint32_t index = op->opcode_index();
88 assert(index < _op_codes.size());
89 const ::circle::OperatorCode *opcode = _op_codes.at(index);
91 if (!mio::circle::is_valid(opcode))
93 std::ostringstream oss;
94 oss << "(invalid: " << index << ")";
98 return mio::circle::opcode_name(opcode);
101 std::vector<int32_t> Reader::outputs(const ::circle::Operator *op) const
103 return as_index_vector(op->outputs());
106 std::string Reader::tensor_name(const ::circle::Tensor *tensor) const
108 return mio::circle::tensor_name(tensor);
111 std::string Reader::tensor_dtype(const ::circle::Tensor *tensor) const
113 return mio::circle::tensor_type(tensor);
116 bool Reader::select_subgraph(uint32_t sgindex)
118 _subgraph_index = sgindex;
120 _operators = nullptr;
125 if (_subgraphs->Length() <= sgindex)
131 const ::circle::SubGraph *subgraph = (*_subgraphs)[sgindex];
133 auto name = subgraph->name();
134 _subgraph_name = name ? name->c_str() : "(noname)";
136 _tensors = subgraph->tensors();
137 _operators = subgraph->operators();
138 _data_format = subgraph->data_format();
140 _inputs = as_index_vector(subgraph->inputs());
141 _outputs = as_index_vector(subgraph->outputs());
146 } // namespace circle