Skip to content

Commit bdd501b

Browse files
ecalubaquibcopybara-github
authored andcommitted
part#1 Use pywrap rules for LiteRT repo to fix duplicate registration errors
PiperOrigin-RevId: 767324105
1 parent acd57a7 commit bdd501b

File tree

5 files changed

+63
-9
lines changed

5 files changed

+63
-9
lines changed

tflite/python/BUILD

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
load("@flatbuffers//:build_defs.bzl", "flatbuffer_py_library")
22
load("@org_tensorflow//tensorflow:pytype.default.bzl", "pytype_strict_contrib_test", "pytype_strict_library")
33
load("@org_tensorflow//tensorflow:strict.default.bzl", "py_strict_binary", "py_strict_library", "py_strict_test")
4-
load("@org_tensorflow//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable")
4+
load("@org_tensorflow//tensorflow:tensorflow.default.bzl", "get_compatible_with_portable", "pywrap_library")
5+
load("@org_tensorflow//tensorflow/core/platform:build_config_root.bzl", "if_pywrap")
56
load("@rules_shell//shell:sh_test.bzl", "sh_test")
67
load("//tflite:special_rules.bzl", "internal_visibility_allowlist")
78

@@ -474,7 +475,9 @@ pytype_strict_library(
474475
"@org_tensorflow//tensorflow/python/framework:tensor_shape",
475476
"@org_tensorflow//tensorflow/python/util:deprecation",
476477
"@org_tensorflow//tensorflow/python/util:tf_export",
477-
],
478+
] + if_pywrap(
479+
if_true = [":pywrap_tflite"],
480+
),
478481
)
479482

480483
py_strict_library(
@@ -515,7 +518,9 @@ pytype_strict_contrib_test(
515518
"@org_tensorflow//tensorflow/python/ops:math_ops",
516519
"@org_tensorflow//tensorflow/python/platform:client_testlib",
517520
"@org_tensorflow//third_party/py/numpy",
518-
],
521+
] + if_pywrap(
522+
if_true = [":pywrap_tflite"],
523+
),
519524
)
520525

521526
py_strict_library(
@@ -608,10 +613,16 @@ py_strict_library(
608613
],
609614
visibility = ["//visibility:public"],
610615
deps = [
611-
"//tflite/python/analyzer_wrapper:_pywrap_analyzer_wrapper",
612616
"@org_tensorflow//tensorflow/compiler/mlir/lite/python:wrap_converter",
613617
"@org_tensorflow//tensorflow/python/util:tf_export",
614-
],
618+
] + if_pywrap(
619+
if_false = [
620+
"//tflite/python/analyzer_wrapper:_pywrap_analyzer_wrapper",
621+
],
622+
if_true = [
623+
":pywrap_tflite",
624+
],
625+
),
615626
)
616627

617628
py_strict_test(
@@ -633,3 +644,27 @@ py_strict_test(
633644
"@org_tensorflow//tensorflow/python/trackable:autotrackable",
634645
],
635646
)
647+
648+
# Use pywrap_library to avoid duplicate registration of pybind11 modules.
649+
# A great example on how to use pywrap_library is
650+
# https://github.com/vam-google/symbol-locations/blob/main/pybind/BUILD
651+
# The following pywrap_library is used by LiteRT repo to avoid shared links provided
652+
# by Tensorflow under tensorflow/python:_pywrap_tensorflow
653+
# This isolate LiteRT's pybind11 dependencies. To use, add pybind deps under pywrap_tflite
654+
# and refer pywrap_tflite to any target that needsd to selected shared objects.
655+
pywrap_library(
656+
name = "pywrap_tflite",
657+
common_lib_versions = {
658+
"framework": "2",
659+
},
660+
visibility = ["//visibility:public"],
661+
deps = [
662+
"//tflite/python/analyzer_wrapper:_pywrap_analyzer_wrapper",
663+
"//tflite/python/interpreter_wrapper:_pywrap_tensorflow_interpreter_wrapper",
664+
"//tflite/python/metrics:_pywrap_tensorflow_lite_metrics_wrapper",
665+
"//tflite/python/optimize:_pywrap_tensorflow_lite_calibration_wrapper",
666+
"//tflite/testing:_pywrap_string_util",
667+
"//tflite/tools/optimize/python:_pywrap_modify_model_interface",
668+
"//tflite/tools/optimize/sparsity:format_converter_wrapper_pybind11",
669+
],
670+
)

tflite/python/metrics/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pybind_extension(
3636
pytype_srcs = [
3737
"_pywrap_tensorflow_lite_metrics_wrapper.pyi",
3838
],
39-
visibility = ["__subpackages__"],
39+
visibility = ["//visibility:public"],
4040
deps = [
4141
":metrics_wrapper_lib",
4242
"@com_google_protobuf//:protobuf",

tflite/testing/BUILD

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ load(
66
"tf_features_nolayering_check_if_ios",
77
)
88
load("@org_tensorflow//tensorflow:tensorflow.default.bzl", "pybind_extension")
9+
load("@org_tensorflow//tensorflow/core/platform:build_config_root.bzl", "if_pywrap")
910
load(
1011
"//tflite:build_def.bzl",
1112
"tflite_custom_android_library",
@@ -170,7 +171,6 @@ py_strict_library(
170171
name = "zip_test_utils",
171172
srcs = ["zip_test_utils.py"],
172173
deps = [
173-
":_pywrap_string_util",
174174
":generate_examples_report",
175175
"//tflite/python:lite",
176176
"//tflite/tools:flatbuffer_utils",
@@ -179,7 +179,14 @@ py_strict_library(
179179
"@org_tensorflow//tensorflow/python/framework:convert_to_constants",
180180
"@org_tensorflow//tensorflow/python/saved_model:signature_constants",
181181
"@org_tensorflow//third_party/py/numpy",
182-
],
182+
] + if_pywrap(
183+
if_false = [
184+
":_pywrap_string_util",
185+
],
186+
if_true = [
187+
"//tflite/python:pywrap_tflite",
188+
],
189+
),
183190
)
184191

185192
py_strict_binary(
@@ -698,6 +705,9 @@ pybind_extension(
698705
additional_stubgen_deps = [
699706
"@org_tensorflow//third_party/py/numpy:numpy",
700707
],
708+
common_lib_packages = [
709+
"tflite/python",
710+
],
701711
enable_stub_generation = True,
702712
features = ["-use_header_modules"],
703713
pytype_srcs = [

tflite/tools/optimize/python/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ py_strict_library(
5454
pybind_extension(
5555
name = "_pywrap_modify_model_interface",
5656
srcs = ["modify_model_interface.cc"],
57+
common_lib_packages = [
58+
"tflite/python",
59+
],
5760
enable_stub_generation = True,
5861
pytype_srcs = [
5962
"_pywrap_modify_model_interface.pyi",

tflite/tools/optimize/sparsity/BUILD

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
load("@org_tensorflow//tensorflow:strict.default.bzl", "py_strict_test")
22
load("@org_tensorflow//tensorflow:tensorflow.default.bzl", "pybind_extension")
3+
load("@org_tensorflow//tensorflow/core/platform:build_config_root.bzl", "if_pywrap")
34

45
package(
56
# copybara:uncomment default_applicable_licenses = ["@org_tensorflow//tensorflow:license"],
@@ -12,6 +13,9 @@ package(
1213
pybind_extension(
1314
name = "format_converter_wrapper_pybind11",
1415
srcs = ["format_converter_wrapper_pybind11.cc"],
16+
common_lib_packages = [
17+
"tflite/python",
18+
],
1519
copts = [
1620
"-fexceptions",
1721
"-fno-strict-aliasing",
@@ -37,5 +41,7 @@ py_strict_test(
3741
"@absl_py//absl/testing:absltest",
3842
#internal proto upb dep
3943
"@org_tensorflow//third_party/py/numpy",
40-
],
44+
] + if_pywrap(
45+
if_true = ["//tflite/python:pywrap_tflite"],
46+
),
4147
)

0 commit comments

Comments
 (0)