Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / compiler / mio-circle06 / src / Reader.cpp
1 /*
2  * Copyright (c) 2023 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
23 namespace mio
24 {
25 namespace circle
26 {
27
28 Reader::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
48 size_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
82   return mio::circle::builtin_code_neutral(opcode);
83 }
84
85 std::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
101 std::vector<int32_t> Reader::outputs(const ::circle::Operator *op) const
102 {
103   return as_index_vector(op->outputs());
104 }
105
106 std::string Reader::tensor_name(const ::circle::Tensor *tensor) const
107 {
108   return mio::circle::tensor_name(tensor);
109 }
110
111 std::string Reader::tensor_dtype(const ::circle::Tensor *tensor) const
112 {
113   return mio::circle::tensor_type(tensor);
114 }
115
116 bool 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