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_conversion_utils.h"
16
17#include <algorithm>
18
19#include "tensorflow/lite/kernels/internal/compatibility.h"
20
21namespace tflite {
22
23int8_t ConvertBuiltinCodeToDeprecatedBuiltinCode(
24 const BuiltinOperator builtin_code) {
25 return (builtin_code < BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES)
26 ? static_cast<int8_t>(builtin_code)
27 : static_cast<int8_t>(
28 BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES);
29}
30
31// The following methods are the following `OperatorCode` table object creation
32// methods for backward compatibility. These are manually copied from the
33// flatbuffer generated code from schema v3. They serve as overloads for the
34// v3a's CreateOperatorCode functions in schema_generated.h and enable code that
35// still assumes flatbuffer schema v3 to be unchanged with the inclusion of the
36// schema_utils header.
37// TODO(b/162392898): remove once all callers are updated to use schema v3a
38// functions.
39
40flatbuffers::Offset<OperatorCode> CreateOperatorCode(
41 flatbuffers::FlatBufferBuilder &_fbb, BuiltinOperator builtin_code,
42 flatbuffers::Offset<flatbuffers::String> custom_code, int32_t version) {
43 OperatorCodeBuilder builder_(_fbb);
44 builder_.add_version(version);
45
46 int8_t deprecated_builtin_code =
47 static_cast<int8_t>(BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES);
48 if (builtin_code < BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES) {
49 deprecated_builtin_code = static_cast<int8_t>(builtin_code);
50 }
51 builder_.add_deprecated_builtin_code(deprecated_builtin_code);
52 builder_.add_custom_code(custom_code);
53 builder_.add_builtin_code(builtin_code);
54 return builder_.Finish();
55}
56
57flatbuffers::Offset<OperatorCode> CreateOperatorCodeDirect(
58 flatbuffers::FlatBufferBuilder &_fbb, BuiltinOperator builtin_code,
59 const char *custom_code, int32_t version) {
60 auto custom_code__ = custom_code ? _fbb.CreateString(custom_code) : 0;
61 int8_t deprecated_builtin_code =
62 static_cast<int8_t>(BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES);
63 if (builtin_code < BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES) {
64 deprecated_builtin_code = static_cast<int8_t>(builtin_code);
65 }
66 return CreateOperatorCode(_fbb, deprecated_builtin_code, custom_code__,
67 version, builtin_code);
68}
69
70} // namespace tflite
71