ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Reader.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 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 <sstream>
21#include <string>
22
23namespace mio
24{
25namespace circle
26{
27
28Reader::Reader(const ::circle::Model *model)
29{
30 if (model == nullptr)
31 {
32 throw std::runtime_error("Invalid model");
33 }
34
35 _version = model->version();
36 _subgraphs = model->subgraphs();
37 _buffers = model->buffers();
38 _metadata = model->metadata();
39 _signature_defs = model->signature_defs();
40
41 auto opcodes = model->operator_codes();
42 for (const ::circle::OperatorCode *opcode : *opcodes)
43 {
44 _op_codes.push_back(opcode);
45 }
46}
47
48size_t Reader::buffer_info(uint32_t buf_idx, const uint8_t **buff_data)
49{
50 if (buff_data != nullptr)
51 {
52 *buff_data = nullptr;
53 }
54
55 if (buf_idx == 0)
56 return 0;
57
58 if (auto *buffer = (*_buffers)[buf_idx])
59 {
60 if (auto *array = buffer->data())
61 {
62 if (size_t size = array->size())
63 {
64 if (buff_data != nullptr)
65 {
66 *buff_data = reinterpret_cast<const uint8_t *>(array->data());
67 }
68 return size;
69 }
70 }
71 }
72
73 return 0;
74}
75
76::circle::BuiltinOperator Reader::builtin_code(const ::circle::Operator *op) const
77{
78 uint32_t index = op->opcode_index();
79 assert(index < _op_codes.size());
80 const ::circle::OperatorCode *opcode = _op_codes.at(index);
81
83}
84
85std::string Reader::opcode_name(const ::circle::Operator *op) const
86{
87 uint32_t index = op->opcode_index();
88 assert(index < _op_codes.size());
89 const ::circle::OperatorCode *opcode = _op_codes.at(index);
90
91 if (!mio::circle::is_valid(opcode))
92 {
93 std::ostringstream oss;
94 oss << "(invalid: " << index << ")";
95 return oss.str();
96 }
97
98 return mio::circle::opcode_name(opcode);
99}
100
101std::vector<int32_t> Reader::outputs(const ::circle::Operator *op) const
102{
103 return as_index_vector(op->outputs());
104}
105
106std::string Reader::tensor_name(const ::circle::Tensor *tensor) const
107{
108 return mio::circle::tensor_name(tensor);
109}
110
111std::string Reader::tensor_dtype(const ::circle::Tensor *tensor) const
112{
113 return mio::circle::tensor_type(tensor);
114}
115
116bool Reader::select_subgraph(uint32_t sgindex)
117{
118 _subgraph_index = sgindex;
119 _tensors = nullptr;
120 _operators = nullptr;
121
122 _inputs.clear();
123 _outputs.clear();
124
125 if (_subgraphs->Length() <= sgindex)
126 {
127 assert(false);
128 return false;
129 }
130
131 const ::circle::SubGraph *subgraph = (*_subgraphs)[sgindex];
132
133 auto name = subgraph->name();
134 _subgraph_name = name ? name->c_str() : "(noname)";
135
136 _tensors = subgraph->tensors();
137 _operators = subgraph->operators();
138 _data_format = subgraph->data_format();
139
140 _inputs = as_index_vector(subgraph->inputs());
141 _outputs = as_index_vector(subgraph->outputs());
142
143 return true;
144}
145
146} // namespace circle
147} // namespace mio
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