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 *
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 "mio_circle/Helper.h"
18
19#include <algorithm>
20#include <sstream>
21
22namespace mio
23{
24namespace circle
25{
26
33::circle::BuiltinOperator builtin_code_neutral(const ::circle::OperatorCode *opcode)
34{
35 assert(opcode != nullptr);
36 if (opcode->deprecated_builtin_code() == 127)
37 {
38 assert(opcode->builtin_code() >= 127);
39 return opcode->builtin_code();
40 }
41 // There was no 255(-1) value in v0.3
42 assert(opcode->deprecated_builtin_code() != -1);
43 return static_cast<::circle::BuiltinOperator>(opcode->deprecated_builtin_code());
44}
45
46bool is_valid(const ::circle::OperatorCode *opcode)
47{
48 // Valid Range : BuiltinOperator_MIN <= deprecated_builtin_code <= 127
49 const int8_t deprecated_builtin_code = opcode->deprecated_builtin_code();
50 if (deprecated_builtin_code < ::circle::BuiltinOperator_MIN)
51 return false;
52 // There was no 255(-1) value in v0.3
53 if (deprecated_builtin_code == -1)
54 return false;
55
56 const ::circle::BuiltinOperator builtin_code = opcode->builtin_code();
57 if (!(::circle::BuiltinOperator_MIN <= builtin_code &&
58 builtin_code <= ::circle::BuiltinOperator_MAX))
59 return false;
60
61 return true;
62}
63
64bool is_custom(const ::circle::OperatorCode *opcode)
65{
66 ::circle::BuiltinOperator code = builtin_code_neutral(opcode);
67 return (code == ::circle::BuiltinOperator_CUSTOM);
68}
69
70std::string opcode_name(const ::circle::OperatorCode *opcode)
71{
72 assert(opcode);
73
74 if (!is_valid(opcode))
75 {
76 std::ostringstream oss;
77 oss << "(invalid)";
78 return oss.str();
79 }
80
81 if (is_custom(opcode))
82 {
83 if (!opcode->custom_code())
84 return "(invalid custom)";
85
86 std::string custom_op = "CUSTOM(";
87 custom_op += opcode->custom_code()->c_str();
88 custom_op += ")";
89 return custom_op;
90 }
91
92 ::circle::BuiltinOperator code = builtin_code_neutral(opcode);
93 return ::circle::EnumNameBuiltinOperator(code);
94}
95
96const char *tensor_type(const ::circle::Tensor *tensor)
97{
98 return ::circle::EnumNameTensorType(tensor->type());
99}
100
101const char *tensor_name(const ::circle::Tensor *tensor)
102{
103 if (tensor->name() == nullptr || std::string(tensor->name()->c_str()).empty())
104 return "(noname)";
105
106 return tensor->name()->c_str();
107}
108
109} // namespace circle
110} // namespace mio
Code * code(const SessionID &sess)
Definition Session.cpp:54
const char * tensor_name(const ::circle::Tensor *tensor)
Definition Helper.cpp:69
std::string opcode_name(const ::circle::OperatorCode *opcode)
Definition Helper.cpp:38
const char * tensor_type(const ::circle::Tensor *tensor)
Definition Helper.cpp:64
bool is_custom(const ::circle::OperatorCode *opcode)
Definition Helper.cpp:32
bool is_valid(const ::circle::OperatorCode *opcode)
Definition Helper.cpp:26
::circle::BuiltinOperator builtin_code_neutral(const ::circle::OperatorCode *opcode)
Definition Helper.cpp:33
Definition Helper.h:23