Skip to content

Commit 7cf8aa4

Browse files
Load tflite and tfdf wasm files with http_archive (#7124)
tfjs-tfdf and tfjs-tflite both depend on pre-compiled WASM files. These WASM files are downloaded by scripts (i.e. curl) during the build step. This violates Bazel's hermeticity requirements because build steps should not access remote resources. This PR fixes this by downloading these WASM files using http_archive in the WORKSPACE file. Fixes #7103
1 parent fcc2f92 commit 7cf8aa4

12 files changed

+140
-177
lines changed

WORKSPACE

+8
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,11 @@ pip_install(
238238
python_interpreter_target = interpreter,
239239
requirements = "@//tfjs-converter/python:requirements.txt",
240240
)
241+
242+
load("//tfjs-tflite:tflite_repositories.bzl", "tflite_repositories")
243+
244+
tflite_repositories()
245+
246+
load("//tfjs-tfdf:tfdf_repositories.bzl", "tfdf_repositories")
247+
248+
tfdf_repositories()

tfjs-tfdf/scripts/BUILD.bazel

-21
This file was deleted.

tfjs-tfdf/scripts/download-tfdf-web-api.sh

-43
This file was deleted.

tfjs-tfdf/tfdf_repositories.bzl

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2022 Google LLC.
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+
16+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
17+
18+
def tfdf_repositories(version = "1.1.0"):
19+
versions = {
20+
"1.1.0": "36b6974996d899589ba99ee95fb56699bf34c582f71e2d98475d7db4bce43b5b",
21+
}
22+
23+
if not version in versions:
24+
versions_string = ", ".join(versions.keys())
25+
fail("Unsupported tfdf wasm files version %s. Supported versions are %s." %
26+
(version, versions_string))
27+
28+
http_archive(
29+
name = "tfdf_wasm_files",
30+
sha256 = versions[version],
31+
url = "https://github.com/google/yggdrasil-decision-forests/releases/download/%s/javascript_wasm.zip" % version,
32+
build_file_content = """
33+
filegroup(
34+
name = "wasm_files",
35+
visibility = ["//visibility:public"],
36+
srcs = [
37+
"inference.js",
38+
"inference.wasm",
39+
],
40+
)
41+
""",
42+
)

tfjs-tfdf/wasm/BUILD.bazel

+7-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
# =============================================================================
1515

16+
load("//tools:copy_to_dist.bzl", "copy_to_dist")
1617
load("//tools:defaults.bzl", "esbuild")
1718
load("//tools:get_extension.bzl", "get_extension")
1819

@@ -22,18 +23,14 @@ package(default_visibility = ["//visibility:public"])
2223
# them available for other targets.
2324
#
2425
# The downloaded files will be in `tfjs/dist/bin/tfjs-tfdf/wasm`.
25-
genrule(
26+
27+
copy_to_dist(
2628
name = "wasm_files",
27-
outs = [
28-
"inference.js",
29-
"inference.wasm",
30-
],
31-
# $(@D) stores the output directory.
32-
cmd = "$(location //tfjs-tfdf/scripts:download_tfdf_web_api) \"$(@D)\"",
33-
exec_tools = [
34-
"//tfjs-tfdf/scripts:download_tfdf_web_api",
29+
srcs = [
30+
"@tfdf_wasm_files//:wasm_files",
3531
],
36-
tags = ["no-remote-exec"],
32+
dest_dir = ".",
33+
global_root = "../tfdf_wasm_files", # See tfdf_repositories.bzl
3734
)
3835

3936
get_extension(

tfjs-tflite/BUILD.bazel

+2-11
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ tfjs_bundle(
3838
"//tfjs-tflite/src:tfjs-tflite_lib",
3939
"//tfjs-tflite/src:tfjs-tflite_src_lib",
4040
"//tfjs-tflite/src:tflite_web_api_client_js",
41-
"//tfjs-tflite/wasm:wasm_files",
41+
"//tfjs-tflite/wasm",
4242
],
4343
)
4444

@@ -66,15 +66,6 @@ copy_to_dist(
6666
],
6767
)
6868

69-
# Copy wasm related files to dist/.
70-
copy_to_dist(
71-
name = "copy_wasm_files",
72-
srcs = [
73-
"//tfjs-tflite/wasm:wasm_files",
74-
],
75-
root = "wasm",
76-
)
77-
7869
# Package for npm.
7970
pkg_npm(
8071
name = "tfjs-tflite_pkg",
@@ -87,7 +78,7 @@ pkg_npm(
8778
deps = [
8879
":copy_bundles",
8980
":copy_src_to_dist",
90-
":copy_wasm_files",
81+
"//tfjs-tflite/wasm",
9182
],
9283
)
9384

tfjs-tflite/scripts/BUILD.bazel

-21
This file was deleted.

tfjs-tflite/scripts/download-tflite-web-api.sh

-43
This file was deleted.

tfjs-tflite/src/BUILD.bazel

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ esbuild(
8484
":tflite_web_api_client_js",
8585
"//tfjs-backend-cpu/src:tfjs-backend-cpu_lib",
8686
"//tfjs-core/src:tfjs-core_lib",
87-
"//tfjs-tflite/wasm:wasm_files",
87+
"//tfjs-tflite/wasm",
8888
],
8989
)
9090

@@ -121,7 +121,7 @@ tfjs_web_test(
121121
"//tfjs-backend-cpu:tf-backend-cpu.min.js.map",
122122
"//tfjs-tflite:tf-tflite.min.js",
123123
"//tfjs-tflite:tf-tflite.min.js.map",
124-
"//tfjs-tflite/wasm:wasm_files",
124+
"//tfjs-tflite/wasm",
125125
"//tfjs-tflite/test_files:add4.tflite",
126126
],
127127
deps = [

tfjs-tflite/tflite_repositories.bzl

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright 2022 Google LLC.
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+
16+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
17+
18+
def tflite_repositories(version = "0.0.9"):
19+
versions = {
20+
"0.0.3": "918dc8c5008cc57907315f78572b233abafefaf668c34e20596cedf96decf381",
21+
"0.0.4": "0d6f487cf7a5afd576417381130a0d9234f1d223012d75fe58eafe98177737b2",
22+
"0.0.5": "0a5fbba206016265d9422b0c9d0514591b01306e392c4f9371cf1d4352d1161d",
23+
"0.0.6": "a5f197e8c8c03bbf93659c9f2d48ddb8226a6a67795677dbdb82e2a9fde0b47c",
24+
"0.0.7": "ffedda0e96485adcb0fde272320e2cc121202fdbaa403dc39eaea8c1e6fac84e",
25+
"0.0.8": "cb3c4ee99aacaba325dedca837c5a83828aeeb2adbcae43179a24a3e819cbcc7",
26+
"0.0.9": "25819b6841dc460e43f4e43746eae3b89d6a372615cfff726495d3910fce9178",
27+
}
28+
29+
if not version in versions:
30+
versions_string = ", ".join(versions.keys())
31+
fail("Unsupported tflite web api version %s. Supported versions are %s." %
32+
(version, versions_string))
33+
34+
http_archive(
35+
name = "tflite_wasm_files",
36+
sha256 = versions[version],
37+
url = "https://storage.googleapis.com/tfweb/%s/tflite_web_api.zip" % version,
38+
build_file_content = """
39+
filegroup(
40+
name = "wasm_files",
41+
visibility = ["//visibility:public"],
42+
srcs = [
43+
"tflite_web_api_cc.js",
44+
"tflite_web_api_cc.wasm",
45+
"tflite_web_api_cc_simd.js",
46+
"tflite_web_api_cc_simd_threaded.js",
47+
"tflite_web_api_cc_simd_threaded.wasm",
48+
"tflite_web_api_cc_simd_threaded.worker.js",
49+
"tflite_web_api_cc_simd.wasm",
50+
"tflite_web_api_cc_threaded.js",
51+
"tflite_web_api_cc_threaded.wasm",
52+
"tflite_web_api_cc_threaded.worker.js",
53+
"tflite_web_api_client.js",
54+
],
55+
)
56+
""",
57+
)

tfjs-tflite/wasm/BUILD.bazel

+10-25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2021 Google LLC. All Rights Reserved.
1+
# Copyright 2022 Google LLC.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -13,31 +13,16 @@
1313
# limitations under the License.
1414
# =============================================================================
1515

16+
load("//tools:copy_to_dist.bzl", "copy_to_dist")
17+
1618
package(default_visibility = ["//visibility:public"])
1719

18-
# Download the TFLite web API related WASM files from google storage, and make
19-
# them available for other targets.
20-
#
21-
# The downloaded files will be in `tfjs/dist/bin/tfjs-tflite/wasm`.
22-
genrule(
23-
name = "wasm_files",
24-
outs = [
25-
"tflite_web_api_cc.js",
26-
"tflite_web_api_cc.wasm",
27-
"tflite_web_api_cc_simd.js",
28-
"tflite_web_api_cc_simd.wasm",
29-
"tflite_web_api_cc_simd_threaded.js",
30-
"tflite_web_api_cc_simd_threaded.wasm",
31-
"tflite_web_api_cc_simd_threaded.worker.js",
32-
"tflite_web_api_cc_threaded.js",
33-
"tflite_web_api_cc_threaded.wasm",
34-
"tflite_web_api_cc_threaded.worker.js",
35-
"tflite_web_api_client.js",
36-
],
37-
# $(@D) stores the output directory.
38-
cmd = "$(location //tfjs-tflite/scripts:download_tflite_web_api) \"$(@D)\"",
39-
exec_tools = [
40-
"//tfjs-tflite/scripts:download_tflite_web_api",
20+
# Copy wasm related files to dist/.
21+
copy_to_dist(
22+
name = "wasm",
23+
srcs = [
24+
"@tflite_wasm_files//:wasm_files",
4125
],
42-
tags = ["no-remote-exec"],
26+
dest_dir = ".",
27+
global_root = "../tflite_wasm_files", # See tflite_repositories.bzl
4328
)

0 commit comments

Comments
 (0)