From 48c3bae94a8b324525b45f157d638dfd4e8c3be1 Mon Sep 17 00:00:00 2001 From: Nick Kreeger Date: Tue, 27 Oct 2020 14:03:55 -0700 Subject: [PATCH] Split getter and setter functions in schema utility files. This enables TFLite Micro to selectively include these setter functions in unit tests. The APIs used in creating the flatbuffer introduce new and delete symbols which can cause issues for libraries not fully building with --gc-sections in linker flags. PiperOrigin-RevId: 339324965 Change-Id: I720b8dab6d80a94a47b7c8c427067966e2c42943 --- tensorflow/compiler/mlir/lite/BUILD | 2 +- .../compiler/mlir/lite/flatbuffer_export.cc | 2 +- tensorflow/lite/core/api/BUILD | 2 +- tensorflow/lite/core/api/op_resolver_test.cc | 2 +- tensorflow/lite/delegates/BUILD | 2 +- tensorflow/lite/delegates/delegate_test.cc | 2 +- tensorflow/lite/delegates/xnnpack/BUILD | 26 +++---- .../xnnpack/binary_elementwise_tester.cc | 2 +- .../lite/delegates/xnnpack/conv_2d_tester.cc | 2 +- .../xnnpack/depthwise_conv_2d_tester.cc | 2 +- .../xnnpack/fully_connected_tester.cc | 2 +- .../delegates/xnnpack/leaky_relu_tester.cc | 2 +- .../lite/delegates/xnnpack/pad_tester.cc | 2 +- .../lite/delegates/xnnpack/pool_2d_tester.cc | 2 +- .../lite/delegates/xnnpack/prelu_tester.cc | 2 +- .../lite/delegates/xnnpack/reduce_tester.cc | 2 +- .../lite/delegates/xnnpack/reshape_tester.cc | 2 +- .../xnnpack/resize_bilinear_tester.cc | 2 +- .../lite/delegates/xnnpack/softmax_tester.cc | 2 +- .../xnnpack/unary_elementwise_tester.cc | 2 +- tensorflow/lite/kernels/BUILD | 2 +- tensorflow/lite/kernels/test_util.cc | 2 +- tensorflow/lite/micro/BUILD | 1 - tensorflow/lite/micro/test_helpers.cc | 3 +- tensorflow/lite/micro/tools/make/Makefile | 2 + tensorflow/lite/schema/BUILD | 13 ++++ .../lite/schema/schema_conversion_utils.cc | 70 +++++++++++++++++++ .../lite/schema/schema_conversion_utils.h | 41 +++++++++++ tensorflow/lite/schema/schema_utils.cc | 51 +------------- tensorflow/lite/schema/schema_utils.h | 16 ----- tensorflow/lite/toco/tflite/BUILD | 4 +- tensorflow/lite/toco/tflite/export.cc | 2 +- tensorflow/lite/toco/tflite/import_test.cc | 2 +- tensorflow/lite/tools/BUILD | 2 +- tensorflow/lite/tools/optimize/BUILD | 1 + tensorflow/lite/tools/optimize/model_utils.cc | 1 + tensorflow/lite/tools/verifier_test.cc | 2 +- 37 files changed, 172 insertions(+), 107 deletions(-) create mode 100644 tensorflow/lite/schema/schema_conversion_utils.cc create mode 100644 tensorflow/lite/schema/schema_conversion_utils.h diff --git a/tensorflow/compiler/mlir/lite/BUILD b/tensorflow/compiler/mlir/lite/BUILD index cbe15e83762..9667b58613a 100644 --- a/tensorflow/compiler/mlir/lite/BUILD +++ b/tensorflow/compiler/mlir/lite/BUILD @@ -671,8 +671,8 @@ cc_library( "//tensorflow/lite:string_util", "//tensorflow/lite/delegates/flex:allowlisted_flex_ops_lib", "//tensorflow/lite/kernels/internal:kernel_utils", + "//tensorflow/lite/schema:schema_conversion_utils", "//tensorflow/lite/schema:schema_fbs", - "//tensorflow/lite/schema:schema_utils", "//tensorflow/lite/tools/versioning", "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/container:flat_hash_map", diff --git a/tensorflow/compiler/mlir/lite/flatbuffer_export.cc b/tensorflow/compiler/mlir/lite/flatbuffer_export.cc index a98e83b7e1e..e08394b4e18 100644 --- a/tensorflow/compiler/mlir/lite/flatbuffer_export.cc +++ b/tensorflow/compiler/mlir/lite/flatbuffer_export.cc @@ -74,8 +74,8 @@ limitations under the License. #include "tensorflow/core/platform/status.h" #include "tensorflow/lite/delegates/flex/allowlisted_flex_ops.h" #include "tensorflow/lite/kernels/internal/kernel_utils.h" +#include "tensorflow/lite/schema/schema_conversion_utils.h" #include "tensorflow/lite/schema/schema_generated.h" -#include "tensorflow/lite/schema/schema_utils.h" #include "tensorflow/lite/string_util.h" #include "tensorflow/lite/tools/versioning/op_version.h" #include "tensorflow/lite/tools/versioning/runtime_version.h" diff --git a/tensorflow/lite/core/api/BUILD b/tensorflow/lite/core/api/BUILD index 38b2e295da2..55920b5ab55 100644 --- a/tensorflow/lite/core/api/BUILD +++ b/tensorflow/lite/core/api/BUILD @@ -97,7 +97,7 @@ cc_test( srcs = ["op_resolver_test.cc"], deps = [ ":api", - "//tensorflow/lite/schema:schema_utils", + "//tensorflow/lite/schema:schema_conversion_utils", "@com_google_googletest//:gtest", ], ) diff --git a/tensorflow/lite/core/api/op_resolver_test.cc b/tensorflow/lite/core/api/op_resolver_test.cc index 44acc92ba8c..b0c0fda88a0 100644 --- a/tensorflow/lite/core/api/op_resolver_test.cc +++ b/tensorflow/lite/core/api/op_resolver_test.cc @@ -18,7 +18,7 @@ limitations under the License. #include #include -#include "tensorflow/lite/schema/schema_utils.h" +#include "tensorflow/lite/schema/schema_conversion_utils.h" namespace tflite { namespace { diff --git a/tensorflow/lite/delegates/BUILD b/tensorflow/lite/delegates/BUILD index d106ae4a738..240de7fef94 100644 --- a/tensorflow/lite/delegates/BUILD +++ b/tensorflow/lite/delegates/BUILD @@ -85,8 +85,8 @@ cc_test( "//tensorflow/lite/kernels:builtin_ops", "//tensorflow/lite/kernels:kernel_util", "//tensorflow/lite/kernels/internal:compatibility", + "//tensorflow/lite/schema:schema_conversion_utils", "//tensorflow/lite/schema:schema_fbs", - "//tensorflow/lite/schema:schema_utils", "//tensorflow/lite/testing:util", "//third_party/eigen3", "@com_google_googletest//:gtest", diff --git a/tensorflow/lite/delegates/delegate_test.cc b/tensorflow/lite/delegates/delegate_test.cc index b70ebdcc3aa..a51d5bc431a 100644 --- a/tensorflow/lite/delegates/delegate_test.cc +++ b/tensorflow/lite/delegates/delegate_test.cc @@ -30,8 +30,8 @@ limitations under the License. #include "tensorflow/lite/kernels/internal/compatibility.h" #include "tensorflow/lite/kernels/kernel_util.h" #include "tensorflow/lite/kernels/register.h" +#include "tensorflow/lite/schema/schema_conversion_utils.h" #include "tensorflow/lite/schema/schema_generated.h" -#include "tensorflow/lite/schema/schema_utils.h" #include "tensorflow/lite/testing/util.h" #include "tensorflow/lite/util.h" #include "tensorflow/lite/version.h" diff --git a/tensorflow/lite/delegates/xnnpack/BUILD b/tensorflow/lite/delegates/xnnpack/BUILD index e96dfdd187b..f97efec99b5 100644 --- a/tensorflow/lite/delegates/xnnpack/BUILD +++ b/tensorflow/lite/delegates/xnnpack/BUILD @@ -76,8 +76,8 @@ cc_library( "//tensorflow/lite:schema_fbs_version", "//tensorflow/lite/c:common", "//tensorflow/lite/kernels:builtin_ops", + "//tensorflow/lite/schema:schema_conversion_utils", "//tensorflow/lite/schema:schema_fbs", - "//tensorflow/lite/schema:schema_utils", "@FP16", "@com_google_googletest//:gtest", "@flatbuffers", @@ -94,8 +94,8 @@ cc_library( "//tensorflow/lite:schema_fbs_version", "//tensorflow/lite/c:common", "//tensorflow/lite/kernels:builtin_ops", + "//tensorflow/lite/schema:schema_conversion_utils", "//tensorflow/lite/schema:schema_fbs", - "//tensorflow/lite/schema:schema_utils", "@FP16", "@com_google_googletest//:gtest", "@flatbuffers", @@ -112,8 +112,8 @@ cc_library( "//tensorflow/lite:schema_fbs_version", "//tensorflow/lite/c:common", "//tensorflow/lite/kernels:builtin_ops", + "//tensorflow/lite/schema:schema_conversion_utils", "//tensorflow/lite/schema:schema_fbs", - "//tensorflow/lite/schema:schema_utils", "@FP16", "@com_google_googletest//:gtest", "@flatbuffers", @@ -130,8 +130,8 @@ cc_library( "//tensorflow/lite:schema_fbs_version", "//tensorflow/lite/c:common", "//tensorflow/lite/kernels:builtin_ops", + "//tensorflow/lite/schema:schema_conversion_utils", "//tensorflow/lite/schema:schema_fbs", - "//tensorflow/lite/schema:schema_utils", "@FP16", "@com_google_googletest//:gtest", "@flatbuffers", @@ -148,8 +148,8 @@ cc_library( "//tensorflow/lite:schema_fbs_version", "//tensorflow/lite/c:common", "//tensorflow/lite/kernels:builtin_ops", + "//tensorflow/lite/schema:schema_conversion_utils", "//tensorflow/lite/schema:schema_fbs", - "//tensorflow/lite/schema:schema_utils", "@com_google_googletest//:gtest", "@flatbuffers", ], @@ -165,8 +165,8 @@ cc_library( "//tensorflow/lite:schema_fbs_version", "//tensorflow/lite/c:common", "//tensorflow/lite/kernels:builtin_ops", + "//tensorflow/lite/schema:schema_conversion_utils", "//tensorflow/lite/schema:schema_fbs", - "//tensorflow/lite/schema:schema_utils", "@com_google_googletest//:gtest", "@flatbuffers", ], @@ -182,8 +182,8 @@ cc_library( "//tensorflow/lite:schema_fbs_version", "//tensorflow/lite/c:common", "//tensorflow/lite/kernels:builtin_ops", + "//tensorflow/lite/schema:schema_conversion_utils", "//tensorflow/lite/schema:schema_fbs", - "//tensorflow/lite/schema:schema_utils", "@com_google_googletest//:gtest", "@flatbuffers", ], @@ -199,8 +199,8 @@ cc_library( "//tensorflow/lite:schema_fbs_version", "//tensorflow/lite/c:common", "//tensorflow/lite/kernels:builtin_ops", + "//tensorflow/lite/schema:schema_conversion_utils", "//tensorflow/lite/schema:schema_fbs", - "//tensorflow/lite/schema:schema_utils", "@FP16", "@com_google_googletest//:gtest", "@flatbuffers", @@ -217,8 +217,8 @@ cc_library( "//tensorflow/lite:schema_fbs_version", "//tensorflow/lite/c:common", "//tensorflow/lite/kernels:builtin_ops", + "//tensorflow/lite/schema:schema_conversion_utils", "//tensorflow/lite/schema:schema_fbs", - "//tensorflow/lite/schema:schema_utils", "@com_google_googletest//:gtest", "@flatbuffers", ], @@ -234,8 +234,8 @@ cc_library( "//tensorflow/lite:schema_fbs_version", "//tensorflow/lite/c:common", "//tensorflow/lite/kernels:builtin_ops", + "//tensorflow/lite/schema:schema_conversion_utils", "//tensorflow/lite/schema:schema_fbs", - "//tensorflow/lite/schema:schema_utils", "@com_google_googletest//:gtest", "@flatbuffers", ], @@ -251,8 +251,8 @@ cc_library( "//tensorflow/lite:schema_fbs_version", "//tensorflow/lite/c:common", "//tensorflow/lite/kernels:builtin_ops", + "//tensorflow/lite/schema:schema_conversion_utils", "//tensorflow/lite/schema:schema_fbs", - "//tensorflow/lite/schema:schema_utils", "@com_google_googletest//:gtest", "@flatbuffers", ], @@ -268,8 +268,8 @@ cc_library( "//tensorflow/lite:schema_fbs_version", "//tensorflow/lite/c:common", "//tensorflow/lite/kernels:builtin_ops", + "//tensorflow/lite/schema:schema_conversion_utils", "//tensorflow/lite/schema:schema_fbs", - "//tensorflow/lite/schema:schema_utils", "@com_google_googletest//:gtest", "@flatbuffers", ], @@ -285,8 +285,8 @@ cc_library( "//tensorflow/lite:schema_fbs_version", "//tensorflow/lite/c:common", "//tensorflow/lite/kernels:builtin_ops", + "//tensorflow/lite/schema:schema_conversion_utils", "//tensorflow/lite/schema:schema_fbs", - "//tensorflow/lite/schema:schema_utils", "@com_google_googletest//:gtest", "@flatbuffers", ], diff --git a/tensorflow/lite/delegates/xnnpack/binary_elementwise_tester.cc b/tensorflow/lite/delegates/xnnpack/binary_elementwise_tester.cc index bc18c76f7eb..6007ddcec64 100644 --- a/tensorflow/lite/delegates/xnnpack/binary_elementwise_tester.cc +++ b/tensorflow/lite/delegates/xnnpack/binary_elementwise_tester.cc @@ -28,8 +28,8 @@ limitations under the License. #include "tensorflow/lite/interpreter.h" #include "tensorflow/lite/kernels/register.h" #include "tensorflow/lite/model.h" +#include "tensorflow/lite/schema/schema_conversion_utils.h" #include "tensorflow/lite/schema/schema_generated.h" -#include "tensorflow/lite/schema/schema_utils.h" #include "tensorflow/lite/version.h" namespace tflite { diff --git a/tensorflow/lite/delegates/xnnpack/conv_2d_tester.cc b/tensorflow/lite/delegates/xnnpack/conv_2d_tester.cc index b81928717f3..c1d378ba344 100644 --- a/tensorflow/lite/delegates/xnnpack/conv_2d_tester.cc +++ b/tensorflow/lite/delegates/xnnpack/conv_2d_tester.cc @@ -27,8 +27,8 @@ limitations under the License. #include "tensorflow/lite/interpreter.h" #include "tensorflow/lite/kernels/register.h" #include "tensorflow/lite/model.h" +#include "tensorflow/lite/schema/schema_conversion_utils.h" #include "tensorflow/lite/schema/schema_generated.h" -#include "tensorflow/lite/schema/schema_utils.h" #include "tensorflow/lite/version.h" namespace tflite { diff --git a/tensorflow/lite/delegates/xnnpack/depthwise_conv_2d_tester.cc b/tensorflow/lite/delegates/xnnpack/depthwise_conv_2d_tester.cc index ca40e89375a..a14fc15d7b5 100644 --- a/tensorflow/lite/delegates/xnnpack/depthwise_conv_2d_tester.cc +++ b/tensorflow/lite/delegates/xnnpack/depthwise_conv_2d_tester.cc @@ -27,8 +27,8 @@ limitations under the License. #include "tensorflow/lite/interpreter.h" #include "tensorflow/lite/kernels/register.h" #include "tensorflow/lite/model.h" +#include "tensorflow/lite/schema/schema_conversion_utils.h" #include "tensorflow/lite/schema/schema_generated.h" -#include "tensorflow/lite/schema/schema_utils.h" #include "tensorflow/lite/version.h" namespace tflite { diff --git a/tensorflow/lite/delegates/xnnpack/fully_connected_tester.cc b/tensorflow/lite/delegates/xnnpack/fully_connected_tester.cc index 0ea0580bd79..c55555d60ec 100644 --- a/tensorflow/lite/delegates/xnnpack/fully_connected_tester.cc +++ b/tensorflow/lite/delegates/xnnpack/fully_connected_tester.cc @@ -28,8 +28,8 @@ limitations under the License. #include "tensorflow/lite/interpreter.h" #include "tensorflow/lite/kernels/register.h" #include "tensorflow/lite/model.h" +#include "tensorflow/lite/schema/schema_conversion_utils.h" #include "tensorflow/lite/schema/schema_generated.h" -#include "tensorflow/lite/schema/schema_utils.h" #include "tensorflow/lite/version.h" namespace tflite { diff --git a/tensorflow/lite/delegates/xnnpack/leaky_relu_tester.cc b/tensorflow/lite/delegates/xnnpack/leaky_relu_tester.cc index c44143eb18a..cee4c0b55f2 100644 --- a/tensorflow/lite/delegates/xnnpack/leaky_relu_tester.cc +++ b/tensorflow/lite/delegates/xnnpack/leaky_relu_tester.cc @@ -27,8 +27,8 @@ limitations under the License. #include "tensorflow/lite/interpreter.h" #include "tensorflow/lite/kernels/register.h" #include "tensorflow/lite/model.h" +#include "tensorflow/lite/schema/schema_conversion_utils.h" #include "tensorflow/lite/schema/schema_generated.h" -#include "tensorflow/lite/schema/schema_utils.h" #include "tensorflow/lite/version.h" namespace tflite { diff --git a/tensorflow/lite/delegates/xnnpack/pad_tester.cc b/tensorflow/lite/delegates/xnnpack/pad_tester.cc index 0365fd4cbd5..e9790be64d4 100644 --- a/tensorflow/lite/delegates/xnnpack/pad_tester.cc +++ b/tensorflow/lite/delegates/xnnpack/pad_tester.cc @@ -27,8 +27,8 @@ limitations under the License. #include "tensorflow/lite/interpreter.h" #include "tensorflow/lite/kernels/register.h" #include "tensorflow/lite/model.h" +#include "tensorflow/lite/schema/schema_conversion_utils.h" #include "tensorflow/lite/schema/schema_generated.h" -#include "tensorflow/lite/schema/schema_utils.h" #include "tensorflow/lite/version.h" namespace tflite { diff --git a/tensorflow/lite/delegates/xnnpack/pool_2d_tester.cc b/tensorflow/lite/delegates/xnnpack/pool_2d_tester.cc index bb6e8be7b7d..b31c2d2a91c 100644 --- a/tensorflow/lite/delegates/xnnpack/pool_2d_tester.cc +++ b/tensorflow/lite/delegates/xnnpack/pool_2d_tester.cc @@ -26,8 +26,8 @@ limitations under the License. #include "tensorflow/lite/interpreter.h" #include "tensorflow/lite/kernels/register.h" #include "tensorflow/lite/model.h" +#include "tensorflow/lite/schema/schema_conversion_utils.h" #include "tensorflow/lite/schema/schema_generated.h" -#include "tensorflow/lite/schema/schema_utils.h" #include "tensorflow/lite/version.h" namespace tflite { diff --git a/tensorflow/lite/delegates/xnnpack/prelu_tester.cc b/tensorflow/lite/delegates/xnnpack/prelu_tester.cc index cee690e4dbd..496342439bc 100644 --- a/tensorflow/lite/delegates/xnnpack/prelu_tester.cc +++ b/tensorflow/lite/delegates/xnnpack/prelu_tester.cc @@ -28,8 +28,8 @@ limitations under the License. #include "tensorflow/lite/interpreter.h" #include "tensorflow/lite/kernels/register.h" #include "tensorflow/lite/model.h" +#include "tensorflow/lite/schema/schema_conversion_utils.h" #include "tensorflow/lite/schema/schema_generated.h" -#include "tensorflow/lite/schema/schema_utils.h" #include "tensorflow/lite/version.h" namespace tflite { diff --git a/tensorflow/lite/delegates/xnnpack/reduce_tester.cc b/tensorflow/lite/delegates/xnnpack/reduce_tester.cc index 9628dbcc1d4..7715e9cd938 100644 --- a/tensorflow/lite/delegates/xnnpack/reduce_tester.cc +++ b/tensorflow/lite/delegates/xnnpack/reduce_tester.cc @@ -27,8 +27,8 @@ limitations under the License. #include "tensorflow/lite/interpreter.h" #include "tensorflow/lite/kernels/register.h" #include "tensorflow/lite/model.h" +#include "tensorflow/lite/schema/schema_conversion_utils.h" #include "tensorflow/lite/schema/schema_generated.h" -#include "tensorflow/lite/schema/schema_utils.h" #include "tensorflow/lite/version.h" namespace tflite { diff --git a/tensorflow/lite/delegates/xnnpack/reshape_tester.cc b/tensorflow/lite/delegates/xnnpack/reshape_tester.cc index f44e3bd9ea3..8a6d2548b0d 100644 --- a/tensorflow/lite/delegates/xnnpack/reshape_tester.cc +++ b/tensorflow/lite/delegates/xnnpack/reshape_tester.cc @@ -27,8 +27,8 @@ limitations under the License. #include "tensorflow/lite/interpreter.h" #include "tensorflow/lite/kernels/register.h" #include "tensorflow/lite/model.h" +#include "tensorflow/lite/schema/schema_conversion_utils.h" #include "tensorflow/lite/schema/schema_generated.h" -#include "tensorflow/lite/schema/schema_utils.h" #include "tensorflow/lite/version.h" namespace tflite { diff --git a/tensorflow/lite/delegates/xnnpack/resize_bilinear_tester.cc b/tensorflow/lite/delegates/xnnpack/resize_bilinear_tester.cc index df11d740696..7453d92fb6f 100644 --- a/tensorflow/lite/delegates/xnnpack/resize_bilinear_tester.cc +++ b/tensorflow/lite/delegates/xnnpack/resize_bilinear_tester.cc @@ -27,8 +27,8 @@ limitations under the License. #include "tensorflow/lite/interpreter.h" #include "tensorflow/lite/kernels/register.h" #include "tensorflow/lite/model.h" +#include "tensorflow/lite/schema/schema_conversion_utils.h" #include "tensorflow/lite/schema/schema_generated.h" -#include "tensorflow/lite/schema/schema_utils.h" #include "tensorflow/lite/version.h" namespace tflite { diff --git a/tensorflow/lite/delegates/xnnpack/softmax_tester.cc b/tensorflow/lite/delegates/xnnpack/softmax_tester.cc index 4ec916db17f..2ab005c639e 100644 --- a/tensorflow/lite/delegates/xnnpack/softmax_tester.cc +++ b/tensorflow/lite/delegates/xnnpack/softmax_tester.cc @@ -27,8 +27,8 @@ limitations under the License. #include "tensorflow/lite/interpreter.h" #include "tensorflow/lite/kernels/register.h" #include "tensorflow/lite/model.h" +#include "tensorflow/lite/schema/schema_conversion_utils.h" #include "tensorflow/lite/schema/schema_generated.h" -#include "tensorflow/lite/schema/schema_utils.h" #include "tensorflow/lite/version.h" namespace tflite { diff --git a/tensorflow/lite/delegates/xnnpack/unary_elementwise_tester.cc b/tensorflow/lite/delegates/xnnpack/unary_elementwise_tester.cc index df22ae2db7a..60a2599f7a4 100644 --- a/tensorflow/lite/delegates/xnnpack/unary_elementwise_tester.cc +++ b/tensorflow/lite/delegates/xnnpack/unary_elementwise_tester.cc @@ -27,8 +27,8 @@ limitations under the License. #include "tensorflow/lite/interpreter.h" #include "tensorflow/lite/kernels/register.h" #include "tensorflow/lite/model.h" +#include "tensorflow/lite/schema/schema_conversion_utils.h" #include "tensorflow/lite/schema/schema_generated.h" -#include "tensorflow/lite/schema/schema_utils.h" #include "tensorflow/lite/version.h" namespace tflite { diff --git a/tensorflow/lite/kernels/BUILD b/tensorflow/lite/kernels/BUILD index f3a639ad0c6..d27589d06cc 100644 --- a/tensorflow/lite/kernels/BUILD +++ b/tensorflow/lite/kernels/BUILD @@ -190,8 +190,8 @@ cc_library( "//tensorflow/lite/delegates/nnapi:nnapi_delegate", "//tensorflow/lite/kernels/internal:tensor_utils", "//tensorflow/lite/nnapi:nnapi_implementation", + "//tensorflow/lite/schema:schema_conversion_utils", "//tensorflow/lite/schema:schema_fbs", - "//tensorflow/lite/schema:schema_utils", "//tensorflow/lite/testing:util", "//tensorflow/lite/tools:command_line_flags", "//tensorflow/lite/tools:logging", diff --git a/tensorflow/lite/kernels/test_util.cc b/tensorflow/lite/kernels/test_util.cc index 97f8f5c4923..05ce059f0fd 100644 --- a/tensorflow/lite/kernels/test_util.cc +++ b/tensorflow/lite/kernels/test_util.cc @@ -43,8 +43,8 @@ limitations under the License. #include "tensorflow/lite/kernels/test_delegate_providers.h" #include "tensorflow/lite/model.h" #include "tensorflow/lite/nnapi/nnapi_implementation.h" +#include "tensorflow/lite/schema/schema_conversion_utils.h" #include "tensorflow/lite/schema/schema_generated.h" -#include "tensorflow/lite/schema/schema_utils.h" #include "tensorflow/lite/string_type.h" #include "tensorflow/lite/string_util.h" #include "tensorflow/lite/tools/logging.h" diff --git a/tensorflow/lite/micro/BUILD b/tensorflow/lite/micro/BUILD index 73cd4cc3f0c..3348c61844a 100644 --- a/tensorflow/lite/micro/BUILD +++ b/tensorflow/lite/micro/BUILD @@ -88,7 +88,6 @@ cc_library( "//tensorflow/lite/kernels/internal:compatibility", "//tensorflow/lite/kernels/internal:tensor", "//tensorflow/lite/schema:schema_fbs", - "//tensorflow/lite/schema:schema_utils", "@flatbuffers//:runtime_cc", ], ) diff --git a/tensorflow/lite/micro/test_helpers.cc b/tensorflow/lite/micro/test_helpers.cc index e4127f4abdb..008f0a9820f 100644 --- a/tensorflow/lite/micro/test_helpers.cc +++ b/tensorflow/lite/micro/test_helpers.cc @@ -30,7 +30,8 @@ limitations under the License. #include "tensorflow/lite/micro/all_ops_resolver.h" #include "tensorflow/lite/micro/micro_utils.h" #include "tensorflow/lite/schema/schema_generated.h" -#include "tensorflow/lite/schema/schema_utils.h" + +// TODO(b/170464050): Use TFLM test only version of schema_utils. namespace tflite { namespace testing { diff --git a/tensorflow/lite/micro/tools/make/Makefile b/tensorflow/lite/micro/tools/make/Makefile index 49d7b66ce0b..b28fd19d15e 100644 --- a/tensorflow/lite/micro/tools/make/Makefile +++ b/tensorflow/lite/micro/tools/make/Makefile @@ -232,6 +232,7 @@ tensorflow/lite/core/api/op_resolver.cc \ tensorflow/lite/core/api/tensor_utils.cc \ tensorflow/lite/kernels/internal/quantization_util.cc \ tensorflow/lite/kernels/kernel_util.cc \ +tensorflow/lite/schema/schema_conversion_utils.cc \ tensorflow/lite/schema/schema_utils.cc MICROLITE_CC_SRCS := $(filter-out $(MICROLITE_TEST_SRCS), $(MICROLITE_CC_BASE_SRCS)) @@ -308,6 +309,7 @@ tensorflow/lite/kernels/op_macros.h \ tensorflow/lite/kernels/padding.h \ tensorflow/lite/portable_type_to_tflitetype.h \ tensorflow/lite/schema/schema_generated.h \ +tensorflow/lite/schema/schema_conversion_utils.h \ tensorflow/lite/schema/schema_utils.h \ tensorflow/lite/version.h diff --git a/tensorflow/lite/schema/BUILD b/tensorflow/lite/schema/BUILD index a3e0952d627..14f299a7d79 100644 --- a/tensorflow/lite/schema/BUILD +++ b/tensorflow/lite/schema/BUILD @@ -143,4 +143,17 @@ cc_library( ], ) +cc_library( + name = "schema_conversion_utils", + srcs = ["schema_conversion_utils.cc"], + hdrs = ["schema_conversion_utils.h"], + compatible_with = get_compatible_with_portable(), + visibility = [":utils_friends"], + deps = [ + ":schema_fbs", + "//tensorflow/lite/kernels/internal:compatibility", + "@flatbuffers", + ], +) + tflite_portable_test_suite() diff --git a/tensorflow/lite/schema/schema_conversion_utils.cc b/tensorflow/lite/schema/schema_conversion_utils.cc new file mode 100644 index 00000000000..640965c68f7 --- /dev/null +++ b/tensorflow/lite/schema/schema_conversion_utils.cc @@ -0,0 +1,70 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ +#include "tensorflow/lite/schema/schema_conversion_utils.h" + +#include + +#include "tensorflow/lite/kernels/internal/compatibility.h" + +namespace tflite { + +int8_t ConvertBuiltinCodeToDeprecatedBuiltinCode( + const BuiltinOperator builtin_code) { + return (builtin_code < BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES) + ? static_cast(builtin_code) + : static_cast( + BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES); +} + +// The following methods are the following `OperatorCode` table object creation +// methods for backward compatibility. These are manually copied from the +// flatbuffer generated code from schema v3. They serve as overloads for the +// v3a's CreateOperatorCode functions in schema_generated.h and enable code that +// still assumes flatbuffer schema v3 to be unchanged with the inclusion of the +// schema_utils header. +// TODO(b/162392898): remove once all callers are updated to use schema v3a +// functions. + +flatbuffers::Offset CreateOperatorCode( + flatbuffers::FlatBufferBuilder &_fbb, BuiltinOperator builtin_code, + flatbuffers::Offset custom_code, int32_t version) { + OperatorCodeBuilder builder_(_fbb); + builder_.add_version(version); + + int8_t deprecated_builtin_code = + static_cast(BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES); + if (builtin_code < BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES) { + deprecated_builtin_code = static_cast(builtin_code); + } + builder_.add_deprecated_builtin_code(deprecated_builtin_code); + builder_.add_custom_code(custom_code); + builder_.add_builtin_code(builtin_code); + return builder_.Finish(); +} + +flatbuffers::Offset CreateOperatorCodeDirect( + flatbuffers::FlatBufferBuilder &_fbb, BuiltinOperator builtin_code, + const char *custom_code, int32_t version) { + auto custom_code__ = custom_code ? _fbb.CreateString(custom_code) : 0; + int8_t deprecated_builtin_code = + static_cast(BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES); + if (builtin_code < BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES) { + deprecated_builtin_code = static_cast(builtin_code); + } + return CreateOperatorCode(_fbb, deprecated_builtin_code, custom_code__, + version, builtin_code); +} + +} // namespace tflite diff --git a/tensorflow/lite/schema/schema_conversion_utils.h b/tensorflow/lite/schema/schema_conversion_utils.h new file mode 100644 index 00000000000..8a0b11c433b --- /dev/null +++ b/tensorflow/lite/schema/schema_conversion_utils.h @@ -0,0 +1,41 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ +#ifndef TENSORFLOW_LITE_SCHEMA_SCHEMA_CONVERSION_UTILS_H_ +#define TENSORFLOW_LITE_SCHEMA_SCHEMA_CONVERSION_UTILS_H_ + +#include "flatbuffers/flatbuffers.h" +#include "tensorflow/lite/schema/schema_generated.h" + +namespace tflite { + +int8_t ConvertBuiltinCodeToDeprecatedBuiltinCode( + const BuiltinOperator builtin_code); + +// The following methods are for backward compatibility for the early version +// three, which does not have an extended builtin code. +flatbuffers::Offset CreateOperatorCode( + flatbuffers::FlatBufferBuilder &_fbb, + BuiltinOperator builtin_code = BuiltinOperator_ADD, + flatbuffers::Offset custom_code = 0, + int32_t version = 1); + +flatbuffers::Offset CreateOperatorCodeDirect( + flatbuffers::FlatBufferBuilder &_fbb, + BuiltinOperator builtin_code = BuiltinOperator_ADD, + const char *custom_code = nullptr, int32_t version = 1); + +} // namespace tflite + +#endif // TENSORFLOW_LITE_SCHEMA_SCHEMA_CONVERSION_UTILS_H_ diff --git a/tensorflow/lite/schema/schema_utils.cc b/tensorflow/lite/schema/schema_utils.cc index ea930fc9587..fc19290b862 100644 --- a/tensorflow/lite/schema/schema_utils.cc +++ b/tensorflow/lite/schema/schema_utils.cc @@ -42,7 +42,7 @@ namespace tflite { // code. In the case, the maximum value of the two fields will be the value of // the `builtin_code` as the right value. -BuiltinOperator GetBuiltinCode(const OperatorCode *op_code) { +BuiltinOperator GetBuiltinCode(const OperatorCode* op_code) { // Caller should guarantee that the given argument value is not a nullptr. TFLITE_DCHECK(op_code != nullptr); @@ -51,7 +51,7 @@ BuiltinOperator GetBuiltinCode(const OperatorCode *op_code) { static_cast(op_code->deprecated_builtin_code())); } -BuiltinOperator GetBuiltinCode(const OperatorCodeT *op_code) { +BuiltinOperator GetBuiltinCode(const OperatorCodeT* op_code) { // Caller should guarantee that the given argument value is not a nullptr. TFLITE_DCHECK(op_code != nullptr); @@ -59,51 +59,4 @@ BuiltinOperator GetBuiltinCode(const OperatorCodeT *op_code) { op_code->deprecated_builtin_code)); } -int8_t ConvertBuiltinCodeToDeprecatedBuiltinCode( - const BuiltinOperator builtin_code) { - return (builtin_code < BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES) - ? static_cast(builtin_code) - : static_cast( - BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES); -} - -// The following methods are the following `OperatorCode` table object creation -// methods for backward compatibility. These are manually copied from the -// flatbuffer generated code from schema v3. They serve as overloads for the -// v3a's CreateOperatorCode functions in schema_generated.h and enable code that -// still assumes flatbuffer schema v3 to be unchanged with the inclusion of the -// schema_utils header. -// TODO(b/162392898): remove once all callers are updated to use schema v3a -// functions. - -flatbuffers::Offset CreateOperatorCode( - flatbuffers::FlatBufferBuilder &_fbb, BuiltinOperator builtin_code, - flatbuffers::Offset custom_code, int32_t version) { - OperatorCodeBuilder builder_(_fbb); - builder_.add_version(version); - - int8_t deprecated_builtin_code = - static_cast(BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES); - if (builtin_code < BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES) { - deprecated_builtin_code = static_cast(builtin_code); - } - builder_.add_deprecated_builtin_code(deprecated_builtin_code); - builder_.add_custom_code(custom_code); - builder_.add_builtin_code(builtin_code); - return builder_.Finish(); -} - -flatbuffers::Offset CreateOperatorCodeDirect( - flatbuffers::FlatBufferBuilder &_fbb, BuiltinOperator builtin_code, - const char *custom_code, int32_t version) { - auto custom_code__ = custom_code ? _fbb.CreateString(custom_code) : 0; - int8_t deprecated_builtin_code = - static_cast(BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES); - if (builtin_code < BuiltinOperator_PLACEHOLDER_FOR_GREATER_OP_CODES) { - deprecated_builtin_code = static_cast(builtin_code); - } - return CreateOperatorCode(_fbb, deprecated_builtin_code, custom_code__, - version, builtin_code); -} - } // namespace tflite diff --git a/tensorflow/lite/schema/schema_utils.h b/tensorflow/lite/schema/schema_utils.h index 315a8d0daf4..453276b97f0 100644 --- a/tensorflow/lite/schema/schema_utils.h +++ b/tensorflow/lite/schema/schema_utils.h @@ -28,22 +28,6 @@ BuiltinOperator GetBuiltinCode(const OperatorCode *op_code); BuiltinOperator GetBuiltinCode(const OperatorCodeT *op_code); -int8_t ConvertBuiltinCodeToDeprecatedBuiltinCode( - const BuiltinOperator builtin_code); - -// The following methods are for backward compatibility for the early version -// three, which does not have an extended builtin code. -flatbuffers::Offset CreateOperatorCode( - flatbuffers::FlatBufferBuilder &_fbb, - BuiltinOperator builtin_code = BuiltinOperator_ADD, - flatbuffers::Offset custom_code = 0, - int32_t version = 1); - -flatbuffers::Offset CreateOperatorCodeDirect( - flatbuffers::FlatBufferBuilder &_fbb, - BuiltinOperator builtin_code = BuiltinOperator_ADD, - const char *custom_code = nullptr, int32_t version = 1); - } // namespace tflite #endif // TENSORFLOW_LITE_SCHEMA_SCHEMA_UTILS_H_ diff --git a/tensorflow/lite/toco/tflite/BUILD b/tensorflow/lite/toco/tflite/BUILD index 91bb77ff391..fe2a1fd35a6 100644 --- a/tensorflow/lite/toco/tflite/BUILD +++ b/tensorflow/lite/toco/tflite/BUILD @@ -94,8 +94,8 @@ cc_library( ":operator", ":types", "//tensorflow/lite:schema_fbs_version", + "//tensorflow/lite/schema:schema_conversion_utils", "//tensorflow/lite/schema:schema_fbs", - "//tensorflow/lite/schema:schema_utils", "//tensorflow/lite/toco:model", "//tensorflow/lite/toco:tooling_util", "//tensorflow/lite/tools/optimize:quantize_weights", @@ -174,8 +174,8 @@ tf_cc_test( ":import", "//tensorflow/core:ops", "//tensorflow/lite:schema_fbs_version", + "//tensorflow/lite/schema:schema_conversion_utils", "//tensorflow/lite/schema:schema_fbs", - "//tensorflow/lite/schema:schema_utils", "@com_google_googletest//:gtest_main", "@flatbuffers", ], diff --git a/tensorflow/lite/toco/tflite/export.cc b/tensorflow/lite/toco/tflite/export.cc index 3ef1c67c721..0f5e7986ce6 100644 --- a/tensorflow/lite/toco/tflite/export.cc +++ b/tensorflow/lite/toco/tflite/export.cc @@ -18,8 +18,8 @@ limitations under the License. #include "absl/strings/str_join.h" #include "tensorflow/core/lib/core/errors.h" #include "tensorflow/lite/context.h" +#include "tensorflow/lite/schema/schema_conversion_utils.h" #include "tensorflow/lite/schema/schema_generated.h" -#include "tensorflow/lite/schema/schema_utils.h" #include "tensorflow/lite/toco/tflite/op_version.h" #include "tensorflow/lite/toco/tflite/operator.h" #include "tensorflow/lite/toco/tflite/types.h" diff --git a/tensorflow/lite/toco/tflite/import_test.cc b/tensorflow/lite/toco/tflite/import_test.cc index fe7dd31a40a..fc2362f4068 100644 --- a/tensorflow/lite/toco/tflite/import_test.cc +++ b/tensorflow/lite/toco/tflite/import_test.cc @@ -17,8 +17,8 @@ limitations under the License. #include "flatbuffers/flexbuffers.h" #include #include +#include "tensorflow/lite/schema/schema_conversion_utils.h" #include "tensorflow/lite/schema/schema_generated.h" -#include "tensorflow/lite/schema/schema_utils.h" #include "tensorflow/lite/version.h" namespace toco { diff --git a/tensorflow/lite/tools/BUILD b/tensorflow/lite/tools/BUILD index 078f139f19b..aa852093794 100644 --- a/tensorflow/lite/tools/BUILD +++ b/tensorflow/lite/tools/BUILD @@ -220,8 +220,8 @@ cc_test( "//tensorflow/lite:schema_fbs_version", "//tensorflow/lite:util", "//tensorflow/lite/core/api", + "//tensorflow/lite/schema:schema_conversion_utils", "//tensorflow/lite/schema:schema_fbs", - "//tensorflow/lite/schema:schema_utils", "//tensorflow/lite/testing:util", "@com_google_googletest//:gtest", "@flatbuffers", diff --git a/tensorflow/lite/tools/optimize/BUILD b/tensorflow/lite/tools/optimize/BUILD index 7157a7c1002..185d1d69ce0 100644 --- a/tensorflow/lite/tools/optimize/BUILD +++ b/tensorflow/lite/tools/optimize/BUILD @@ -134,6 +134,7 @@ cc_library( "//tensorflow/lite:framework", "//tensorflow/lite/kernels/internal:tensor_utils", "//tensorflow/lite/kernels/internal:types", + "//tensorflow/lite/schema:schema_conversion_utils", "//tensorflow/lite/schema:schema_fbs", "//tensorflow/lite/schema:schema_utils", "@com_google_absl//absl/memory", diff --git a/tensorflow/lite/tools/optimize/model_utils.cc b/tensorflow/lite/tools/optimize/model_utils.cc index 7224c623c77..62a3f85e586 100644 --- a/tensorflow/lite/tools/optimize/model_utils.cc +++ b/tensorflow/lite/tools/optimize/model_utils.cc @@ -20,6 +20,7 @@ limitations under the License. #include "tensorflow/lite/kernels/internal/tensor_utils.h" #include "tensorflow/lite/kernels/internal/types.h" #include "tensorflow/lite/model.h" +#include "tensorflow/lite/schema/schema_conversion_utils.h" #include "tensorflow/lite/schema/schema_generated.h" #include "tensorflow/lite/schema/schema_utils.h" #include "tensorflow/lite/tools/optimize/operator_property.h" diff --git a/tensorflow/lite/tools/verifier_test.cc b/tensorflow/lite/tools/verifier_test.cc index f37eaaa99be..c4eac26049e 100644 --- a/tensorflow/lite/tools/verifier_test.cc +++ b/tensorflow/lite/tools/verifier_test.cc @@ -29,8 +29,8 @@ limitations under the License. #include "tensorflow/lite/model.h" #include "tensorflow/lite/mutable_op_resolver.h" #include "tensorflow/lite/op_resolver.h" +#include "tensorflow/lite/schema/schema_conversion_utils.h" #include "tensorflow/lite/schema/schema_generated.h" -#include "tensorflow/lite/schema/schema_utils.h" #include "tensorflow/lite/testing/util.h" #include "tensorflow/lite/util.h" #include "tensorflow/lite/version.h"