Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / circle-tensordump / src / Reader.cpp
1 /*
2  * Copyright (c) 2020 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 "Reader.h"
18
19 #include <sstream>
20 #include <string>
21
22 namespace circletensordump
23 {
24
25 bool is_valid(const circle::OperatorCode *opcode)
26 {
27   circle::BuiltinOperator code = opcode->builtin_code();
28   return (circle::BuiltinOperator_MIN <= code && code <= circle::BuiltinOperator_MAX);
29 }
30
31 bool is_custom(const circle::OperatorCode *opcode)
32 {
33   circle::BuiltinOperator code = opcode->builtin_code();
34   return (code == circle::BuiltinOperator_CUSTOM);
35 }
36
37 std::string opcode_name(const circle::OperatorCode *opcode)
38 {
39   assert(opcode);
40
41   if (!is_valid(opcode))
42   {
43     std::ostringstream oss;
44     oss << "(invalid)";
45     return oss.str();
46   }
47
48   if (is_custom(opcode))
49   {
50     if (!opcode->custom_code())
51       return "(invalid custom)";
52
53     std::string custom_op = "CUSTOM(";
54     custom_op += opcode->custom_code()->c_str();
55     custom_op += ")";
56     return custom_op;
57   }
58
59   circle::BuiltinOperator code = opcode->builtin_code();
60   return circle::EnumNameBuiltinOperator(code);
61 }
62
63 const char *tensor_type(const circle::Tensor *tensor)
64 {
65   return circle::EnumNameTensorType(tensor->type());
66 }
67
68 const char *tensor_name(const circle::Tensor *tensor)
69 {
70   static const char *kEmptyTensorName = "(noname)";
71
72   auto name = tensor->name();
73   if (name)
74     return name->c_str();
75
76   return kEmptyTensorName;
77 }
78
79 Reader::Reader(const circle::Model *model)
80 {
81   _subgraphs = model->subgraphs();
82   _buffers = model->buffers();
83
84   auto opcodes = model->operator_codes();
85   for (const ::circle::OperatorCode *opcode : *opcodes)
86   {
87     _op_codes.push_back(opcode);
88   }
89 }
90
91 size_t Reader::buffer_info(uint32_t buf_idx, const uint8_t **buff_data)
92 {
93   if (buff_data != nullptr)
94   {
95     *buff_data = nullptr;
96   }
97
98   if (buf_idx == 0)
99     return 0;
100
101   if (auto *buffer = (*_buffers)[buf_idx])
102   {
103     if (auto *array = buffer->data())
104     {
105       if (size_t size = array->size())
106       {
107         if (buff_data != nullptr)
108         {
109           *buff_data = reinterpret_cast<const uint8_t *>(array->data());
110         }
111         return size;
112       }
113     }
114   }
115
116   return 0;
117 }
118
119 circle::BuiltinOperator Reader::builtin_code(const circle::Operator *op) const
120 {
121   uint32_t index = op->opcode_index();
122   assert(index < _op_codes.size());
123   const circle::OperatorCode *opcode = _op_codes.at(index);
124
125   return opcode->builtin_code();
126 }
127
128 std::string Reader::opcode_name(const circle::Operator *op) const
129 {
130   uint32_t index = op->opcode_index();
131   assert(index < _op_codes.size());
132   const circle::OperatorCode *opcode = _op_codes.at(index);
133
134   if (!is_valid(opcode))
135   {
136     std::ostringstream oss;
137     oss << "(invalid: " << index << ")";
138     return oss.str();
139   }
140
141   return circletensordump::opcode_name(opcode);
142 }
143
144 bool Reader::select_subgraph(uint32_t sgindex)
145 {
146   _tensors = nullptr;
147   _operators = nullptr;
148
149   _inputs.clear();
150   _outputs.clear();
151
152   if (_subgraphs->Length() <= sgindex)
153   {
154     assert(false);
155     return false;
156   }
157
158   const circle::SubGraph *subgraph = (*_subgraphs)[sgindex];
159
160   _tensors = subgraph->tensors();
161   _operators = subgraph->operators();
162
163   _inputs = as_index_vector(subgraph->inputs());
164   _outputs = as_index_vector(subgraph->outputs());
165
166   return true;
167 }
168
169 } // namespace circletensordump