ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Helper.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 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
19
20#include <sstream>
21
22namespace mio
23{
24namespace tflite
25{
26
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
40bool 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
55bool is_custom(const ::tflite::OperatorCode *opcode)
56{
57 ::tflite::BuiltinOperator code = builtin_code_neutral(opcode);
58 return (code == ::tflite::BuiltinOperator_CUSTOM);
59}
60
61std::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
87const char *tensor_type(const ::tflite::Tensor *tensor)
88{
89 return ::tflite::EnumNameTensorType(tensor->type());
90}
91
92const 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
Code * code(const SessionID &sess)
Definition Session.cpp:54
std::string opcode_name(const ::tflite::OperatorCode *opcode)
Definition Helper.cpp:61
bool is_valid(const ::tflite::OperatorCode *opcode)
Definition Helper.cpp:40
::tflite::BuiltinOperator builtin_code_neutral(const ::tflite::OperatorCode *opcode)
Definition Helper.cpp:33
bool is_custom(const ::tflite::OperatorCode *opcode)
Definition Helper.cpp:55
const char * tensor_type(const ::tflite::Tensor *tensor)
Definition Helper.cpp:87
const char * tensor_name(const ::tflite::Tensor *tensor)
Definition Helper.cpp:92
Definition Helper.h:23