generator.cc 3.8 KB

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