xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/delegates/flex/BUILD (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1load(
2    "//tensorflow:tensorflow.bzl",
3    "if_mobile",
4    "if_not_mobile",
5    "tf_cc_test",
6    "tf_features_nolayering_check_if_ios",
7    "tf_opts_nortti_if_android",
8    "tf_opts_nortti_if_lite_protos",
9)
10load("//tensorflow/lite:build_def.bzl", "tflite_copts")
11load("//tensorflow/lite:special_rules.bzl", "internal_visibility_allowlist")
12load("//tensorflow/lite/delegates/flex:build_def.bzl", "tflite_flex_cc_library", "tflite_flex_shared_library")
13load("//tensorflow:tensorflow.bzl", "get_compatible_with_cloud")
14
15default_visibility = [
16    "//tensorflow/compiler/mlir/lite:__subpackages__",
17    "//tensorflow/lite/android:__subpackages__",
18    "//tensorflow/lite/toco/tflite:__subpackages__",
19]
20
21#
22# This is a TF Lite delegate that is powered by TensorFlow's Eager.
23#
24package(
25    default_visibility = default_visibility,
26    licenses = ["notice"],
27)
28
29exports_files([
30    "delegate.h",
31    "exported_symbols.lds",
32    "version_script.lds",
33])
34
35cc_library(
36    name = "buffer_map",
37    srcs = ["buffer_map.cc"],
38    hdrs = ["buffer_map.h"],
39    copts = tf_opts_nortti_if_lite_protos(),
40    features = tf_features_nolayering_check_if_ios(),
41    deps = [
42        ":util",
43        ":buffer_map_util",
44        "//tensorflow/lite/c:common",
45        "//tensorflow/lite:string",
46        "//tensorflow/lite/kernels/internal:compatibility",
47    ] + if_mobile([
48        "//tensorflow/core:portable_tensorflow_lib_lite",
49    ]) + if_not_mobile([
50        "//tensorflow/c:c_api_internal",
51        "//tensorflow/core:framework",
52    ]),
53)
54
55cc_library(
56    name = "buffer_map_util",
57    srcs = ["buffer_map_util.cc"],
58    hdrs = ["buffer_map_util.h"],
59    copts = tf_opts_nortti_if_lite_protos(),
60    features = tf_features_nolayering_check_if_ios(),
61    deps = [
62        ":util",
63        "//tensorflow/lite/c:common",
64        "//tensorflow/lite:string_util",
65        "//tensorflow/lite/experimental/resource",
66    ] + if_mobile([
67        "//tensorflow/core:portable_tensorflow_lib_lite",
68    ]) + if_not_mobile([
69        "//tensorflow/c:c_api_internal",
70        "//tensorflow/core:framework",
71        "//tensorflow/core:protos_all_cc",
72        "//tensorflow/core/platform:status",
73    ]),
74)
75
76tf_cc_test(
77    name = "buffer_map_test",
78    size = "small",
79    srcs = ["buffer_map_test.cc"],
80    deps = [
81        ":buffer_map",
82        ":buffer_map_util",
83        "//tensorflow/core:framework",
84        "//tensorflow/lite:framework",
85        "//tensorflow/lite:string_util",
86        "//tensorflow/lite:util",
87        "//tensorflow/lite/c:c_api_types",
88        "//tensorflow/lite/testing:util",
89        "@com_google_googletest//:gtest_main",
90    ],
91)
92
93# Define the standard flex delegate library, that pulls in the standard set
94# of TensorFlow ops and kernels, using tflite_flex_cc_library with no
95# models parameter. Custom flex delegate can be defined with
96# tflite_flex_cc_library if the parameter models is provided. Tensorflow
97# user-provided ops could also be supported by passing to additional_deps.
98# Ex:
99# tflite_flex_cc_library(
100#   name = "sample_delegate",
101#   models = ["model1.tflite", "model2.tflite"],
102#   additional_deps = ["your_custom_ops_lib"],
103# )
104tflite_flex_cc_library(
105    name = "delegate",
106    visibility = ["//visibility:public"],
107)
108
109# Compared to the library above, this one doesn't define a strong symbol for
110# AcquireFlexDelegate(). This is useful if one doesn't want the default flex
111# delegate to be automatically applied when building the interpreter.
112tflite_flex_cc_library(
113    name = "delegate_without_symbol",
114    link_symbol = False,
115    visibility = ["//visibility:public"],
116)
117
118# Shared lib target for convenience, pulls in the standard set of TensorFlow
119# ops and kernels. The output library name is platform dependent:
120#   - Linux/Android: `libtensorflowlite_flex.so`
121#   - Mac: `libtensorflowlite_flex.dylib`
122#   - Windows: `tensorflowlite_flex.dll`
123tflite_flex_shared_library(
124    name = "tensorflowlite_flex",
125)
126
127cc_library(
128    name = "delegate_symbol",
129    srcs = [
130        "delegate_symbol.cc",
131    ],
132    copts = tflite_copts(),
133    visibility = ["//visibility:public"],
134    deps = [
135        ":delegate_only_runtime",
136        "//tensorflow/lite/c:c_api_types",
137    ],
138    alwayslink = 1,
139)
140
141# Delegate implementation that does *not* pull in the standard set of TensorFlow
142# ops and kernels.
143cc_library(
144    name = "delegate_only_runtime",
145    srcs = [
146        "delegate.cc",
147        "kernel.cc",
148        "kernel.h",
149    ],
150    hdrs = [
151        "delegate.h",
152    ],
153    copts = tflite_copts() + tf_opts_nortti_if_android(),
154    features = tf_features_nolayering_check_if_ios(),
155    visibility = ["//visibility:public"],
156    deps = [
157        ":buffer_map",
158        ":delegate_data",
159        ":util",
160        ":tflite_subgraph_execute",
161        "@flatbuffers",
162        "@com_google_absl//absl/strings",
163        "//tensorflow/lite/core/api",
164        "//tensorflow/lite/c:common",
165        "//tensorflow/lite:kernel_api",
166        "//tensorflow/lite:macros",
167        "//tensorflow/lite:minimal_logging",
168        "//tensorflow/lite:string",
169        "//tensorflow/lite:string_util",
170        "//tensorflow/lite:util",
171        "//tensorflow/lite/delegates/utils:simple_delegate",
172        "//tensorflow/lite/kernels:kernel_util",
173        "//tensorflow/core/tfrt/fallback:op_kernel_runner",
174    ] + if_mobile([
175        "//tensorflow/core:portable_tensorflow_lib_lite",
176    ]) + if_not_mobile([
177        "//tensorflow/core/common_runtime/eager:context",
178        "//tensorflow/core:lib",
179        "//tensorflow/core:protos_all_cc",
180        "//tensorflow/core:framework",
181    ]),
182    alwayslink = 1,
183)
184
185tf_cc_test(
186    name = "delegate_test",
187    size = "small",
188    srcs = ["delegate_test.cc"],
189    tags = [
190        "no_gpu",  # GPU + flex is not officially supported.
191    ],
192    deps = [
193        ":delegate",
194        ":test_util",
195        "//tensorflow/lite:shared_library",
196        "//tensorflow/lite/kernels:test_util",
197        "@com_google_googletest//:gtest_main",
198    ],
199)
200
201cc_library(
202    name = "delegate_data",
203    srcs = ["delegate_data.cc"],
204    hdrs = ["delegate_data.h"],
205    copts = tf_opts_nortti_if_android(),
206    features = tf_features_nolayering_check_if_ios(),
207    visibility = ["//visibility:public"],
208    deps = [
209        ":buffer_map",
210        ":subgraph_resource",
211        ":util",
212        "@com_google_absl//absl/memory",
213        "@com_google_absl//absl/strings",
214        "@flatbuffers",
215        "//tensorflow/lite/c:common",
216        "//tensorflow/lite/schema:schema_fbs",
217        "//tensorflow/lite:cc_api",
218        "//tensorflow/lite:util",
219    ] + if_mobile([
220        "//tensorflow/core:portable_tensorflow_lib_lite",
221    ]) + if_not_mobile([
222        "//tensorflow/core/common_runtime/eager:context",
223        "//tensorflow/core/common_runtime/eager:core",
224        "//tensorflow/core:core_cpu",
225        "//tensorflow/core:framework",
226        "//tensorflow/core:lib",
227        "//tensorflow/core:protos_all_cc",
228    ]),
229)
230
231tf_cc_test(
232    name = "delegate_data_test",
233    size = "small",
234    srcs = ["delegate_data_test.cc"],
235    deps = [
236        ":delegate_data",
237        "//tensorflow/core:test",
238        "//tensorflow/core/common_runtime/eager:context",
239        "//tensorflow/core/platform:protobuf",
240        "//tensorflow/core/platform:status",
241        "//tensorflow/lite:framework",
242        "//tensorflow/lite/c:common",
243        "//tensorflow/lite/core/api:error_reporter",
244        "//tensorflow/lite/kernels:subgraph_test_util",
245        "//tensorflow/lite/testing:util",
246        "@com_google_absl//absl/memory",
247        "@com_google_absl//absl/strings",
248        "@com_google_googletest//:gtest_main",
249    ],
250)
251
252cc_library(
253    name = "subgraph_resource",
254    hdrs = ["subgraph_resource.h"],
255    features = tf_features_nolayering_check_if_ios(),
256    deps = [
257        "//tensorflow/lite/c:common",
258        "//tensorflow/lite:cc_api",
259    ] + if_mobile([
260        "//tensorflow/core:portable_tensorflow_lib_lite",
261    ]) + if_not_mobile([
262        "//tensorflow/core:framework",
263        "//tensorflow/core:lib",
264    ]),
265)
266
267tf_cc_test(
268    name = "kernel_test",
269    size = "small",
270    srcs = [
271        "kernel.h",
272        "kernel_test.cc",
273    ],
274    tags = ["no_gpu"],  # GPU + flex is not officially supported.
275    deps = [
276        ":delegate",
277        ":delegate_data",
278        ":test_util",
279        "//tensorflow/core/platform:status",
280        "//tensorflow/core/tfrt/fallback:op_kernel_runner",
281        "//tensorflow/lite/c:common",
282        "//tensorflow/lite/delegates/utils:simple_delegate",
283        "//tensorflow/lite/kernels:kernel_util",
284        "@com_google_googletest//:gtest_main",
285    ],
286)
287
288cc_library(
289    name = "test_util",
290    testonly = True,
291    srcs = ["test_util.cc"],
292    hdrs = ["test_util.h"],
293    visibility = internal_visibility_allowlist(),
294    deps = [
295        "//tensorflow/c:c_api_internal",
296        "//tensorflow/lite:string",
297        "//tensorflow/lite/kernels:test_util",
298        "@com_google_absl//absl/memory",
299        "@flatbuffers",
300    ],
301)
302
303cc_library(
304    name = "util",
305    srcs = ["util.cc"],
306    hdrs = ["util.h"],
307    features = tf_features_nolayering_check_if_ios(),
308    #TODO(b/206038955): Consider restrict the visibility to '//third_party/fcp/client:__subpackages__'.
309    visibility = ["//visibility:public"],
310    deps = [
311        "//tensorflow/lite/c:common",
312        "//tensorflow/lite:kernel_api",
313        "@com_google_absl//absl/strings:str_format",
314        "//tensorflow/lite/kernels/internal:tensor",
315        "//tensorflow/lite:string_util",
316        "//tensorflow/lite:util",
317    ] + if_mobile([
318        "//tensorflow/core:portable_tensorflow_lib_lite",
319    ]) + if_not_mobile([
320        "//tensorflow/c:c_api_internal",
321        "//tensorflow/core:lib",
322        "//tensorflow/core:framework",
323        "//tensorflow/core/protobuf:error_codes_proto_impl_cc",
324    ]),
325)
326
327tf_cc_test(
328    name = "util_test",
329    size = "small",
330    srcs = ["util_test.cc"],
331    deps = [
332        ":util",
333        "//tensorflow/core:framework",
334        "//tensorflow/core/protobuf:error_codes_proto_impl_cc",
335        "//tensorflow/lite:string",
336        "//tensorflow/lite:string_util",
337        "//tensorflow/lite:util",
338        "//tensorflow/lite/c:c_api_types",
339        "//tensorflow/lite/c:common",
340        "//tensorflow/lite/testing:util",
341        "@com_google_googletest//:gtest_main",
342    ],
343)
344
345cc_library(
346    name = "allowlisted_flex_ops_lib",
347    srcs = [
348        "allowlisted_flex_ops.cc",
349    ],
350    hdrs = [
351        "allowlisted_flex_ops.h",
352        "allowlisted_flex_ops_internal.h",
353    ],
354    compatible_with = get_compatible_with_cloud(),
355    features = tf_features_nolayering_check_if_ios(),
356    visibility = internal_visibility_allowlist(),
357    deps = if_mobile([
358        "//tensorflow/core:portable_tensorflow_lib_lite",
359    ]) + if_not_mobile([
360        "//tensorflow/core:framework",
361    ]),
362)
363
364tf_cc_test(
365    name = "allowlisted_flex_ops_test",
366    size = "small",
367    srcs = [
368        "allowlisted_flex_ops_test.cc",
369    ],
370    features = tf_features_nolayering_check_if_ios(),
371    deps = [
372        ":delegate",
373        ":allowlisted_flex_ops_lib",
374        "@com_google_googletest//:gtest_main",
375    ] + if_mobile([
376        "//tensorflow/core:portable_tensorflow_lib_lite",
377    ]) + if_not_mobile([
378        "//tensorflow/core:framework",
379    ]),
380)
381
382# Alias to support selective build of image ops.
383# TODO(b/163285312): Remove after tensorflow/core refactoring completed.
384cc_library(
385    name = "portable_images_lib",
386    visibility = ["//visibility:public"],
387    deps = [
388        "//tensorflow/core:portable_gif_internal",
389        "//tensorflow/core:portable_jpeg_internal",
390        "//tensorflow/core/lib/png:png_io",
391    ],
392)
393
394cc_library(
395    name = "tflite_subgraph_execute",
396    srcs = ["tflite_subgraph_execute.cc"],
397    copts = tf_opts_nortti_if_android(),
398    features = tf_features_nolayering_check_if_ios(),
399    deps = [
400        ":buffer_map_util",
401        ":subgraph_resource",
402        ":util",
403        "@com_google_absl//absl/strings",
404        "//tensorflow/lite/kernels/internal:tensor",
405        "@com_google_absl//absl/strings:str_format",
406        "//tensorflow/lite:cc_api",
407        "//tensorflow/lite:string_util",
408        "//tensorflow/lite/c:c_api_types",
409        "//tensorflow/lite/c:common",
410        "//tensorflow/lite/kernels:builtin_ops",
411        "//tensorflow/lite/kernels:kernel_util",
412    ] + if_mobile([
413        "//tensorflow/core:portable_tensorflow_lib_lite",
414    ]) + if_not_mobile([
415        "//tensorflow/core:framework",
416        "//tensorflow/core:lib",
417    ]),
418    alwayslink = 1,
419)
420