Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / compiler / mio-tflite2121 / src / Helper.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2020 The TensorFlow Authors. All Rights Reserved.
4  *
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
8  *
9  *    http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include "mio_tflite2121/Helper.h"
19
20 #include <sstream>
21
22 namespace mio
23 {
24 namespace tflite
25 {
26
27 /**
28  * This will provide v3/v3a format neutral BuiltinOperator
29  *
30  * This function referenced
31  * https://github.com/tensorflow/tensorflow/blob/7d12007d7800d3714a02e05059f3ea602d1aec78/tensorflow/lite/schema/schema_utils.cc
32  */
33 ::tflite::BuiltinOperator builtin_code_neutral(const ::tflite::OperatorCode *opcode)
34 {
35   assert(opcode != nullptr);
36   return std::max(opcode->builtin_code(),
37                   static_cast<::tflite::BuiltinOperator>(opcode->deprecated_builtin_code()));
38 }
39
40 bool is_valid(const ::tflite::OperatorCode *opcode)
41 {
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)
45     return false;
46
47   const ::tflite::BuiltinOperator builtin_code = opcode->builtin_code();
48   if (!(::tflite::BuiltinOperator_MIN <= builtin_code &&
49         builtin_code <= ::tflite::BuiltinOperator_MAX))
50     return false;
51
52   return true;
53 }
54
55 bool is_custom(const ::tflite::OperatorCode *opcode)
56 {
57   ::tflite::BuiltinOperator code = builtin_code_neutral(opcode);
58   return (code == ::tflite::BuiltinOperator_CUSTOM);
59 }
60
61 std::string opcode_name(const ::tflite::OperatorCode *opcode)
62 {
63   assert(opcode);
64
65   if (!is_valid(opcode))
66   {
67     std::ostringstream oss;
68     oss << "(invalid)";
69     return oss.str();
70   }
71
72   if (is_custom(opcode))
73   {
74     if (!opcode->custom_code())
75       return "(invalid custom)";
76
77     std::string custom_op = "CUSTOM(";
78     custom_op += opcode->custom_code()->c_str();
79     custom_op += ")";
80     return custom_op;
81   }
82
83   ::tflite::BuiltinOperator code = builtin_code_neutral(opcode);
84   return ::tflite::EnumNameBuiltinOperator(code);
85 }
86
87 const char *tensor_type(const ::tflite::Tensor *tensor)
88 {
89   return ::tflite::EnumNameTensorType(tensor->type());
90 }
91
92 const char *tensor_name(const ::tflite::Tensor *tensor)
93 {
94   static const char *kEmptyTensorName = "(noname)";
95
96   auto name = tensor->name();
97   if (name)
98     return name->c_str();
99
100   return kEmptyTensorName;
101 }
102
103 } // namespace tflite
104 } // namespace mio