1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include "tensorflow/lite/schema/builtin_ops_header/generator.h"
16 #include "tensorflow/lite/schema/schema_generated.h"
17
18 namespace tflite {
19 namespace builtin_ops_header {
20
21 namespace {
22 const char* kFileHeader =
23 R"(/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
24
25 Licensed under the Apache License, Version 2.0 (the "License");
26 you may not use this file except in compliance with the License.
27 You may obtain a copy of the License at
28
29 http://www.apache.org/licenses/LICENSE-2.0
30
31 Unless required by applicable law or agreed to in writing, software
32 distributed under the License is distributed on an "AS IS" BASIS,
33 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 See the License for the specific language governing permissions and
35 limitations under the License.
36 ==============================================================================*/
37
38 #ifndef TENSORFLOW_LITE_BUILTIN_OPS_H_
39 #define TENSORFLOW_LITE_BUILTIN_OPS_H_
40
41 // DO NOT EDIT MANUALLY: This file is automatically generated by
42 // `schema/builtin_ops_header/generator.cc`.
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif // __cplusplus
47
48 // The enum for builtin operators.
49 // Note: CUSTOM, DELEGATE, and PLACEHOLDER_FOR_GREATER_OP_CODES are 3 special
50 // ops which are not real built-in ops.
51 typedef enum {
52 )";
53
54 const char* kFileFooter =
55 R"(} TfLiteBuiltinOperator;
56
57 #ifdef __cplusplus
58 } // extern "C"
59 #endif // __cplusplus
60 #endif // TENSORFLOW_LITE_BUILTIN_OPS_H_
61 )";
62 } // anonymous namespace
63
IsValidInputEnumName(const std::string & name)64 bool IsValidInputEnumName(const std::string& name) {
65 const char* begin = name.c_str();
66 const char* ch = begin;
67 while (*ch != '\0') {
68 // If it's not the first character, expect an underscore.
69 if (ch != begin) {
70 if (*ch != '_') {
71 return false;
72 }
73 ++ch;
74 }
75
76 // Expecting a word with upper case letters or digits, like "CONV",
77 // "CONV2D", "2D"...etc.
78 bool empty = true;
79 while (isupper(*ch) || isdigit(*ch)) {
80 // It's not empty if at least one character is consumed.
81 empty = false;
82 ++ch;
83 }
84 if (empty) {
85 return false;
86 }
87 }
88 return true;
89 }
90
ConstantizeVariableName(const std::string & name)91 std::string ConstantizeVariableName(const std::string& name) {
92 std::string result = "kTfLiteBuiltin";
93 bool uppercase = true;
94 for (char input_char : name) {
95 if (input_char == '_') {
96 uppercase = true;
97 } else if (uppercase) {
98 result += toupper(input_char);
99 uppercase = false;
100 } else {
101 result += tolower(input_char);
102 }
103 }
104
105 return result;
106 }
107
GenerateHeader(std::ostream & os)108 bool GenerateHeader(std::ostream& os) {
109 auto enum_names = tflite::EnumNamesBuiltinOperator();
110
111 // Check if all the input enum names are valid.
112 for (auto enum_value : EnumValuesBuiltinOperator()) {
113 auto enum_name = enum_names[enum_value];
114 if (!IsValidInputEnumName(enum_name)) {
115 std::cerr << "Invalid input enum name: " << enum_name << std::endl;
116 return false;
117 }
118 }
119
120 os << kFileHeader;
121 for (auto enum_value : EnumValuesBuiltinOperator()) {
122 auto enum_name = enum_names[enum_value];
123 os << " ";
124 os << ConstantizeVariableName(enum_name);
125 os << " = ";
126 os << enum_value;
127 os << ",\n";
128 }
129 os << kFileFooter;
130 return true;
131 }
132
133 } // namespace builtin_ops_header
134 } // namespace tflite
135