• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15
16load("@flatbuffers//:build_defs.bzl", "DEFAULT_FLATC_ARGS", "flatbuffer_cc_library", "flatbuffer_java_library", "flatc_path")
17load("//tensorflow:tensorflow.bzl", "get_compatible_with_portable")
18load("//tensorflow/lite:build_def.bzl", "tflite_copts", "tflite_copts_warnings")
19load("//tensorflow/lite:special_rules.bzl", "nnapi_plugin_impl_visibility_allowlist", "tflite_portable_test_suite")
20load("//tensorflow/lite/core/shims:cc_library_with_tflite.bzl", "cc_library_with_tflite")
21
22# copybara:comment_begin(oss-only)
23load("//tensorflow/tsl/platform/default:build_config.bzl", "tf_proto_library_py")
24# copybara:comment_end
25
26package(
27    default_visibility = [
28        "//visibility:public",
29    ],
30    licenses = ["notice"],
31)
32
33genrule(
34    name = "configuration_schema",
35    srcs = ["configuration.proto"],
36    outs = ["configuration.fbs"],
37    # We rename the namespace since otherwise the proto classes and flatbuffer
38    # classes would have the same names.
39    cmd = """
40    $(location {}) --proto -o $(@D) $(location :configuration.proto)
41    perl -p -i -e 's/tflite.proto/tflite/' $(@D)/configuration.fbs
42    """.format(flatc_path),
43    compatible_with = get_compatible_with_portable(),
44    tools = [
45        flatc_path,
46    ],
47)
48
49genrule(
50    name = "configuration_fbs_contents_cc",
51    srcs = ["configuration.fbs"],
52    outs = ["configuration_fbs_contents-inl.h"],
53    cmd = """
54      echo 'constexpr char configuration_fbs_contents[] = R"Delimiter(' > $(@)
55      cat < $(<) >> $(@)
56      echo ')Delimiter";' >> $(@)
57    """,
58)
59
60exports_files(["configuration.proto"])
61
62proto_library(
63    name = "configuration_proto",
64    srcs = [
65        "configuration.proto",
66    ],
67)
68
69cc_proto_library(
70    name = "configuration_cc_proto",
71    deps = [":configuration_proto"],
72)
73
74java_lite_proto_library(
75    name = "configuration_java_proto_lite",
76    deps = [":configuration_proto"],
77)
78
79# copybara:comment_begin(oss-only)
80tf_proto_library_py(
81    name = "configuration_proto_external",
82    srcs = ["configuration.proto"],
83)
84# copybara:comment_end
85
86flatbuffer_cc_library(
87    name = "configuration_fbs",
88    srcs = [":configuration.fbs"],
89    compatible_with = get_compatible_with_portable(),
90    flatc_args = DEFAULT_FLATC_ARGS + ["--gen-compare"],
91)
92
93flatbuffer_java_library(
94    name = "configuration_fbs_java",
95    srcs = [":configuration.fbs"],
96)
97
98cc_library(
99    name = "proto_to_flatbuffer",
100    srcs = [
101        "proto_to_flatbuffer.cc",
102    ],
103    hdrs = ["proto_to_flatbuffer.h"],
104    deps = [
105        ":configuration_cc_proto",
106        ":configuration_fbs",
107        "//tensorflow/lite:minimal_logging",
108        "@flatbuffers",
109    ],
110)
111
112cc_library(
113    name = "flatbuffer_to_proto",
114    srcs = [
115        "flatbuffer_to_proto.cc",
116    ],
117    hdrs = ["flatbuffer_to_proto.h"],
118    deps = [
119        ":configuration_cc_proto",
120        ":configuration_fbs",
121        "//tensorflow/lite:minimal_logging",
122        "@flatbuffers",
123    ],
124)
125
126cc_test(
127    name = "flatbuffer_to_proto_test",
128    srcs = ["flatbuffer_to_proto_test.cc"],
129    deps = [
130        ":configuration_cc_proto",
131        ":configuration_fbs",
132        ":flatbuffer_to_proto",
133        "@com_google_googletest//:gtest_main",
134    ],
135)
136
137cc_library(
138    name = "delegate_registry",
139    srcs = ["delegate_registry.cc"],
140    hdrs = ["delegate_registry.h"],
141    deps = [
142        ":configuration_fbs",
143        "//tensorflow/lite/c:common",
144        "@com_google_absl//absl/synchronization",
145    ],
146)
147
148cc_library_with_tflite(
149    name = "delegate_plugin_converter",
150    srcs = ["delegate_plugin_converter.cc"],
151    hdrs = ["delegate_plugin_converter.h"],
152    tflite_deps = [
153        "//tensorflow/lite/core/shims:delegate_plugin",
154        "//tensorflow/lite/core/shims:delegate_registry",
155        "//tensorflow/lite/core/shims:common",
156    ],
157    deps = ["@com_google_absl//absl/memory"],
158)
159
160cc_library(
161    name = "nnapi_plugin",
162    deps = [
163        ":nnapi_plugin_impl",
164    ],
165)
166
167cc_library(
168    name = "nnapi_plugin_impl",
169    srcs = ["nnapi_plugin.cc"],
170    hdrs = ["nnapi_plugin.h"],
171    visibility = nnapi_plugin_impl_visibility_allowlist() + [
172        "//tensorflow/lite/experimental/acceleration/configuration/c:__pkg__",
173    ],
174    deps = [
175        ":configuration_fbs",
176        ":delegate_registry",
177        "//tensorflow/lite/c:common",
178        "//tensorflow/lite/delegates/nnapi:nnapi_delegate",
179        "//tensorflow/lite/experimental/acceleration/configuration/c:delegate_plugin",
180        "//tensorflow/lite/nnapi:nnapi_implementation_headers",
181        "@com_google_absl//absl/memory",
182    ],
183    alwayslink = 1,  # For registration to always run.
184)
185
186cc_test(
187    name = "nnapi_plugin_test",
188    srcs = ["nnapi_plugin_test.cc"],
189    tags = [
190        "no_mac",
191        "no_windows",
192        "tflite_not_portable_ios",
193    ],
194    deps = [
195        ":configuration_fbs",
196        ":delegate_registry",
197        ":nnapi_plugin",
198        "//tensorflow/lite:framework",
199        "//tensorflow/lite/c:common",
200        "//tensorflow/lite/delegates/nnapi:nnapi_delegate",
201        "//tensorflow/lite/delegates/nnapi:nnapi_delegate_mock_test",
202        "//tensorflow/lite/kernels:test_util",
203        "@com_google_googletest//:gtest_main",
204        "@flatbuffers",
205    ],
206)
207
208cc_library(
209    name = "hexagon_plugin",
210    srcs = ["hexagon_plugin.cc"],
211    deps = [
212        ":configuration_fbs",
213        ":delegate_registry",
214        "@com_google_absl//absl/memory",
215    ] + select({
216        "//tensorflow:android": [
217            "//tensorflow/lite/delegates/hexagon:hexagon_delegate",
218        ],
219        "//conditions:default": [],
220    }),
221    alwayslink = 1,  # For registration to always run.
222)
223
224cc_library(
225    name = "gpu_plugin",
226    deps = [
227        ":gpu_plugin_impl",
228    ],
229)
230
231common_copts = tflite_copts() + tflite_copts_warnings()
232
233cc_library(
234    name = "gpu_plugin_impl",
235    srcs = ["gpu_plugin.cc"],
236    hdrs = ["gpu_plugin.h"],
237    copts = common_copts + select({
238        "//tensorflow:ios": [
239            "-xobjective-c++",
240        ],
241        "//tensorflow:macos_arm64": [
242            "-xobjective-c++",
243        ],
244        "//conditions:default": [],
245    }),
246    visibility = [
247        "//tensorflow/lite/experimental/acceleration/configuration/c:__pkg__",
248    ],
249    deps = [
250        ":configuration_fbs",
251        ":delegate_registry",
252        "@com_google_absl//absl/memory",
253    ] + select({
254        "//tensorflow/lite/delegates/gpu:supports_gpu_delegate": [
255            "//tensorflow/lite/delegates/gpu:delegate",
256        ],
257        "//conditions:default": [],
258    }) + select({
259        "//tensorflow:ios": [
260            "//tensorflow/lite/delegates/gpu:metal_delegate",
261        ],
262        "//tensorflow:macos_arm64": [
263            "//tensorflow/lite/delegates/gpu:metal_delegate",
264        ],
265        "//conditions:default": [],
266    }),
267    alwayslink = 1,  # For registration to always run.
268)
269
270cc_test(
271    name = "gpu_plugin_test",
272    srcs = ["gpu_plugin_test.cc"],
273    tags = [
274        # TODO(b/214492180): Enable the tests for mac/ios too.  This most likely
275        # needs TAP to recognize the xobjective-c++ flag.
276        "no_mac",
277        "tflite_not_portable_ios",
278    ],
279    deps = [
280        ":configuration_cc_proto",
281        ":configuration_fbs",
282        ":delegate_registry",
283        ":gpu_plugin_impl",
284        ":proto_to_flatbuffer",
285        "@com_google_googletest//:gtest_main",
286        "@flatbuffers//:runtime_cc",
287    ],
288)
289
290cc_library(
291    name = "xnnpack_plugin",
292    srcs = ["xnnpack_plugin.cc"],
293    deps = [
294        ":configuration_fbs",
295        ":delegate_registry",
296        "//tensorflow/lite:minimal_logging",
297        "//tensorflow/lite/delegates/xnnpack:xnnpack_delegate",
298        "@com_google_absl//absl/base:log_severity",
299        "@com_google_absl//absl/memory",
300    ],
301    alwayslink = 1,  # For registration to always run.
302)
303
304cc_test(
305    name = "xnnpack_plugin_test",
306    srcs = ["xnnpack_plugin_test.cc"],
307    deps = [
308        ":configuration_fbs",
309        ":delegate_registry",
310        ":xnnpack_plugin",
311        "//tensorflow/lite/delegates/xnnpack:xnnpack_delegate",
312        "@com_google_googletest//:gtest_main",
313        "@flatbuffers//:runtime_cc",
314        "@pthreadpool",
315    ],
316)
317
318cc_library(
319    name = "coreml_plugin",
320    srcs = ["coreml_plugin.cc"],
321    deps = [
322        ":configuration_fbs",
323        ":delegate_registry",
324        "@com_google_absl//absl/memory",
325        "//tensorflow/lite:minimal_logging",
326    ] + select({
327        "//tensorflow:macos": [
328            "//tensorflow/lite/delegates/coreml:coreml_delegate",
329        ],
330        "//tensorflow:ios": [
331            "//tensorflow/lite/delegates/coreml:coreml_delegate",
332        ],
333        "//conditions:default": [],
334    }),
335    alwayslink = 1,  # For registration to always run.
336)
337
338tflite_portable_test_suite()
339