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