1/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15#include "tensorflow/lite/schema/schema_utils.h"
16
17#include <algorithm>
18
19#include "tensorflow/lite/kernels/internal/compatibility.h"
20
21namespace tflite {
22
23// The following GetBuiltinCode methods are the utility methods for reading
24// builtin operatore code, ensuring compatibility issues between v3 and v3a
25// schema. Always the maximum value of the two fields always will be the correct
26// value as follows:
27//
28// - Supporting schema version v3 models
29//
30// The `builtin_code` field is not available in the v3 models. Flatbuffer
31// library will feed zero value, which is the default value in the v3a schema.
32// The actual builtin operatore code value will exist in the
33// `deprecated_builtin_code` field. At the same time, it implies that
34// `deprecated_builtin_code` >= `builtin_code` and the maximum value of the two
35// fields will be same with `deprecated_builtin_code'.
36//
37// - Supporting builtin operator codes beyonds 127
38//
39// New builtin operators, whose operator code is larger than 127, can not be
40// assigned to the `deprecated_builtin_code` field. In such cases, the
41// value of the `builtin_code` field should be used for the builtin operator
42// code. In the case, the maximum value of the two fields will be the value of
43// the `builtin_code` as the right value.
44
45BuiltinOperator GetBuiltinCode(const OperatorCode* op_code) {
46 // Caller should guarantee that the given argument value is not a nullptr.
47 TFLITE_DCHECK(op_code != nullptr);
48
49 return std::max(
50 op_code->builtin_code(),
51 static_cast<BuiltinOperator>(op_code->deprecated_builtin_code()));
52}
53
54BuiltinOperator GetBuiltinCode(const OperatorCodeT* op_code) {
55 // Caller should guarantee that the given argument value is not a nullptr.
56 TFLITE_DCHECK(op_code != nullptr);
57
58 return std::max(op_code->builtin_code, static_cast<BuiltinOperator>(
59 op_code->deprecated_builtin_code));
60}
61
62} // namespace tflite
63