2 * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2020 The TensorFlow Authors. All Rights Reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include "mio_tflite2121/Helper.h"
28 * This will provide v3/v3a format neutral BuiltinOperator
30 * This function referenced
31 * https://github.com/tensorflow/tensorflow/blob/7d12007d7800d3714a02e05059f3ea602d1aec78/tensorflow/lite/schema/schema_utils.cc
33 ::tflite::BuiltinOperator builtin_code_neutral(const ::tflite::OperatorCode *opcode)
35 assert(opcode != nullptr);
36 return std::max(opcode->builtin_code(),
37 static_cast<::tflite::BuiltinOperator>(opcode->deprecated_builtin_code()));
40 bool is_valid(const ::tflite::OperatorCode *opcode)
42 // Valid Range : 0 <= deprecated_builtin_code <= 127
43 const int8_t deprecated_builtin_code = opcode->deprecated_builtin_code();
44 if (deprecated_builtin_code < 0)
47 const ::tflite::BuiltinOperator builtin_code = opcode->builtin_code();
48 if (!(::tflite::BuiltinOperator_MIN <= builtin_code &&
49 builtin_code <= ::tflite::BuiltinOperator_MAX))
55 bool is_custom(const ::tflite::OperatorCode *opcode)
57 ::tflite::BuiltinOperator code = builtin_code_neutral(opcode);
58 return (code == ::tflite::BuiltinOperator_CUSTOM);
61 std::string opcode_name(const ::tflite::OperatorCode *opcode)
65 if (!is_valid(opcode))
67 std::ostringstream oss;
72 if (is_custom(opcode))
74 if (!opcode->custom_code())
75 return "(invalid custom)";
77 std::string custom_op = "CUSTOM(";
78 custom_op += opcode->custom_code()->c_str();
83 ::tflite::BuiltinOperator code = builtin_code_neutral(opcode);
84 return ::tflite::EnumNameBuiltinOperator(code);
87 const char *tensor_type(const ::tflite::Tensor *tensor)
89 return ::tflite::EnumNameTensorType(tensor->type());
92 const char *tensor_name(const ::tflite::Tensor *tensor)
94 static const char *kEmptyTensorName = "(noname)";
96 auto name = tensor->name();
100 return kEmptyTensorName;
103 } // namespace tflite