schema_utils.cc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* Copyright 2020 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/schema_utils.h"
  13. #include <algorithm>
  14. #include "tensorflow/lite/kernels/internal/compatibility.h"
  15. namespace tflite {
  16. // The following GetBuiltinCode methods are the utility methods for reading
  17. // builtin operatore code, ensuring compatibility issues between v3 and v3a
  18. // schema. Always the maximum value of the two fields always will be the correct
  19. // value as follows:
  20. //
  21. // - Supporting schema version v3 models
  22. //
  23. // The `builtin_code` field is not available in the v3 models. Flatbuffer
  24. // library will feed zero value, which is the default value in the v3a schema.
  25. // The actual builtin operatore code value will exist in the
  26. // `deprecated_builtin_code` field. At the same time, it implies that
  27. // `deprecated_builtin_code` >= `builtin_code` and the maximum value of the two
  28. // fields will be same with `deprecated_builtin_code'.
  29. //
  30. // - Supporting builtin operator codes beyonds 127
  31. //
  32. // New builtin operators, whose operator code is larger than 127, can not be
  33. // assigned to the `deprecated_builtin_code` field. In such cases, the
  34. // value of the `builtin_code` field should be used for the builtin operator
  35. // code. In the case, the maximum value of the two fields will be the value of
  36. // the `builtin_code` as the right value.
  37. BuiltinOperator GetBuiltinCode(const OperatorCode* op_code) {
  38. // Caller should guarantee that the given argument value is not a nullptr.
  39. TFLITE_DCHECK(op_code != nullptr);
  40. return std::max(
  41. op_code->builtin_code(),
  42. static_cast<BuiltinOperator>(op_code->deprecated_builtin_code()));
  43. }
  44. BuiltinOperator GetBuiltinCode(const OperatorCodeT* op_code) {
  45. // Caller should guarantee that the given argument value is not a nullptr.
  46. TFLITE_DCHECK(op_code != nullptr);
  47. return std::max(op_code->builtin_code, static_cast<BuiltinOperator>(
  48. op_code->deprecated_builtin_code));
  49. }
  50. } // namespace tflite