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 and DELEGATE are 2 special ops which are not real built-in ops.
50 typedef enum {
51 )";
52
53 const char* kFileFooter =
54 R"(} TfLiteBuiltinOperator;
55
56 #ifdef __cplusplus
57 } // extern "C"
58 #endif // __cplusplus
59 #endif // TENSORFLOW_LITE_BUILTIN_OPS_H_
60 )";
61 } // anonymous namespace
62
IsValidInputEnumName(const std::string & name)63 bool IsValidInputEnumName(const std::string& name) {
64 const char* begin = name.c_str();
65 const char* ch = begin;
66 while (*ch != '\0') {
67 // If it's not the first character, expect an underscore.
68 if (ch != begin) {
69 if (*ch != '_') {
70 return false;
71 }
72 ++ch;
73 }
74
75 // Expecting a word with upper case letters or digits, like "CONV",
76 // "CONV2D", "2D"...etc.
77 bool empty = true;
78 while (isupper(*ch) || isdigit(*ch)) {
79 // It's not empty if at least one character is consumed.
80 empty = false;
81 ++ch;
82 }
83 if (empty) {
84 return false;
85 }
86 }
87 return true;
88 }
89
ConstantizeVariableName(const std::string & name)90 std::string ConstantizeVariableName(const std::string& name) {
91 std::string result = "kTfLiteBuiltin";
92 bool uppercase = true;
93 for (char input_char : name) {
94 if (input_char == '_') {
95 uppercase = true;
96 } else if (uppercase) {
97 result += toupper(input_char);
98 uppercase = false;
99 } else {
100 result += tolower(input_char);
101 }
102 }
103
104 return result;
105 }
106
GenerateHeader(std::ostream & os)107 bool GenerateHeader(std::ostream& os) {
108 auto enum_names = tflite::EnumNamesBuiltinOperator();
109
110 // Check if all the input enum names are valid.
111 for (auto enum_value : EnumValuesBuiltinOperator()) {
112 auto enum_name = enum_names[enum_value];
113 if (!IsValidInputEnumName(enum_name)) {
114 std::cerr << "Invalid input enum name: " << enum_name << std::endl;
115 return false;
116 }
117 }
118
119 os << kFileHeader;
120 for (auto enum_value : EnumValuesBuiltinOperator()) {
121 auto enum_name = enum_names[enum_value];
122 os << " ";
123 os << ConstantizeVariableName(enum_name);
124 os << " = ";
125 os << enum_value;
126 os << ",\n";
127 }
128 os << kFileFooter;
129 return true;
130 }
131
132 } // namespace builtin_ops_header
133 } // namespace tflite
134