2 * Copyright (c) 2018 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.
25 // This will provide v3/v3a format neutral BuiltinOperator
26 tflite::BuiltinOperator builtin_code_neutral(const tflite::OperatorCode *opcode)
28 assert(opcode != nullptr);
29 int8_t dp_code = opcode->deprecated_builtin_code();
30 if (dp_code < 127 && dp_code >= 0)
31 return tflite::BuiltinOperator(dp_code);
32 return opcode->builtin_code();
35 bool is_valid(const tflite::OperatorCode *opcode)
37 tflite::BuiltinOperator code = builtin_code_neutral(opcode);
38 return (tflite::BuiltinOperator_MIN <= code && code <= tflite::BuiltinOperator_MAX);
41 bool is_custom(const tflite::OperatorCode *opcode)
43 tflite::BuiltinOperator code = builtin_code_neutral(opcode);
44 return (code == tflite::BuiltinOperator_CUSTOM);
47 std::string opcode_name(const tflite::OperatorCode *opcode)
51 if (!is_valid(opcode))
53 std::ostringstream oss;
58 if (is_custom(opcode))
60 if (!opcode->custom_code())
61 return "(invalid custom)";
63 std::string custom_op = "CUSTOM(";
64 custom_op += opcode->custom_code()->c_str();
69 tflite::BuiltinOperator code = builtin_code_neutral(opcode);
70 return tflite::EnumNameBuiltinOperator(code);
73 const char *tensor_type(const tflite::Tensor *tensor)
75 return tflite::EnumNameTensorType(tensor->type());
78 const char *tensor_name(const tflite::Tensor *tensor)
80 static const char *kEmptyTensorName = "(noname)";
82 auto name = tensor->name();
86 return kEmptyTensorName;
89 Reader::Reader(const tflite::Model *model)
91 _version = model->version();
92 _subgraphs = model->subgraphs();
93 _buffers = model->buffers();
94 _metadata = model->metadata();
95 _signaturedefs = model->signature_defs();
97 auto opcodes = model->operator_codes();
98 for (const ::tflite::OperatorCode *opcode : *opcodes)
100 _op_codes.push_back(opcode);
104 size_t Reader::buffer_info(uint32_t buf_idx, const uint8_t **buff_data)
106 *buff_data = nullptr;
111 if (auto *buffer = (*_buffers)[buf_idx])
113 if (auto *array = buffer->data())
115 if (size_t size = array->size())
117 *buff_data = reinterpret_cast<const uint8_t *>(array->data());
126 tflite::BuiltinOperator Reader::builtin_code(const tflite::Operator *op) const
128 uint32_t index = op->opcode_index();
129 assert(index < _op_codes.size());
130 const tflite::OperatorCode *opcode = _op_codes.at(index);
132 return tflread::builtin_code_neutral(opcode);
135 std::string Reader::opcode_name(const tflite::Operator *op) const
137 uint32_t index = op->opcode_index();
138 assert(index < _op_codes.size());
139 const tflite::OperatorCode *opcode = _op_codes.at(index);
141 if (!is_valid(opcode))
143 std::ostringstream oss;
144 oss << "(invalid: " << index << ")";
148 return tflread::opcode_name(opcode);
151 bool Reader::select_subgraph(uint32_t sgindex)
153 _subgraph_index = sgindex;
155 _operators = nullptr;
160 if (_subgraphs->Length() <= sgindex)
166 const tflite::SubGraph *subgraph = (*_subgraphs)[sgindex];
168 auto name = subgraph->name();
169 _subgraph_name = name ? name->c_str() : "(noname)";
171 _tensors = subgraph->tensors();
172 _operators = subgraph->operators();
174 _inputs = as_index_vector(subgraph->inputs());
175 _outputs = as_index_vector(subgraph->outputs());
180 } // namespace tflread