Skip to content

Commit 83b742e

Browse files
ecalubaquibcopybara-github
authored andcommitted
Make a custom rule for LiteRT WORKSPACE to enable usage of both http_archive or local_repository
LiteRT-PiperOrigin-RevId: 775364074
1 parent 10477ee commit 83b742e

9 files changed

+85
-11
lines changed

.bazelrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,3 +528,12 @@ common --noenable_bzlmod
528528
# TODO: Migrate for https://github.com/bazelbuild/bazel/issues/7260
529529
common --noincompatible_enable_cc_toolchain_resolution
530530
common --noincompatible_enable_android_toolchain_resolution
531+
532+
# Flags for tensorflow source rules to use local_repository rather than http_archive.
533+
# NOTE: due to bazel caching, when switching from http_archive to local_repository,
534+
# do a bazel clean --expunge --async before building or use the
535+
# flag --override_repository=org_tensorflow=$TF_LOCAL_SOURCE_PATH
536+
build:use_local_tf --action_env=USE_LOCAL_TF=true
537+
build:use_local_tf --action_env=TF_LOCAL_SOURCE_PATH
538+
test:use_local_tf --action_env=USE_LOCAL_TF=true
539+
test:use_local_tf --action_env=TF_LOCAL_SOURCE_PATH

WORKSPACE

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,16 @@ http_archive(
2626
url = "https://github.com/bazelbuild/rules_java/releases/download/5.3.5/rules_java-5.3.5.tar.gz",
2727
)
2828

29-
# TensorFlow
30-
# TODO(ecalubaquib): Revert this to http_archive once tflite is fully separated from tensorflow.
31-
local_repository(
29+
# Load the custom repository rule to select either a local TensorFlow source or a remote http_archive.
30+
load("//litert:tensorflow_source_rules.bzl", "tensorflow_source_repo")
31+
32+
tensorflow_source_repo(
3233
name = "org_tensorflow",
33-
path = "third_party/tensorflow",
34+
sha256 = "",
35+
strip_prefix = "",
36+
urls = [
37+
"https://github.com/tensorflow/tensorflow/archive/master.tar.gz",
38+
],
3439
)
3540

3641
# Initialize the TensorFlow repository and all dependencies.

ci/build_android_package.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ BUILD_FLAGS=("-c" "opt" \
177177
"--define=android_dexmerger_tool=d8_dexmerger" \
178178
"--define=android_incremental_dexing_tool=d8_dexbuilder" \
179179
"--repo_env=HERMETIC_PYTHON_VERSION=3.11" \
180-
"--show_timestamps")
180+
"--show_timestamps"
181+
"--config=use_local_tf")
181182

182183
# Merge extra config flags from the environment
183184
BUILD_FLAGS+=(${BAZEL_CONFIG_FLAGS})

ci/build_maven_with_docker.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ else
6565
cp .tf_configure.bazelrc /root_dir
6666

6767
cd /root_dir
68+
export TF_LOCAL_SOURCE_PATH="/root_dir/third_party/tensorflow"
6869
bash /script_dir/build_android_package.sh
6970

7071
# Bundle the Maven package

ci/build_pip_package_with_bazel.sh

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
set -ex
1717

1818
# Run this script under the root directory.
19+
export TF_LOCAL_SOURCE_PATH=${TF_LOCAL_SOURCE_PATH:"$(pwd)/third_party/tensorflow"}
1920

2021
ARCH="$(uname -m)"
2122
TENSORFLOW_TARGET=${TENSORFLOW_TARGET:-$1}
@@ -30,25 +31,28 @@ case "${TENSORFLOW_TARGET}" in
3031
--copt=-march=armv7-a --copt=-mfpu=neon-vfpv4
3132
--copt=-O3 --copt=-fno-tree-pre --copt=-fpermissive
3233
--define tensorflow_mkldnn_contraction_kernel=0
33-
--define=raspberry_pi_with_neon=true"
34+
--define=raspberry_pi_with_neon=true
35+
--config=use_local_tf"
3436
;;
3537
rpi0)
3638
BAZEL_FLAGS="--config=elinux_armhf
3739
--copt=-march=armv6 -mfpu=vfp -mfloat-abi=hard
38-
--copt=-O3 --copt=-fno-tree-pre --copt=-fpermissive
40+
--copt=-O3 --copt=-fno-tree-pre --copt=-fpermissivec
3941
--define tensorflow_mkldnn_contraction_kernel=0
40-
--define=raspberry_pi_with_neon=true"
42+
--define=raspberry_pi_with_neon=true
43+
--config=use_local_tf"
4144
;;
4245
aarch64)
4346
BAZEL_FLAGS="--config=release_arm64_linux
4447
--define tensorflow_mkldnn_contraction_kernel=0
45-
--copt=-O3"
48+
--copt=-O3
49+
--config=use_local_tf"
4650
;;
4751
native)
48-
BAZEL_FLAGS="--copt=-O3 --copt=-march=native"
52+
BAZEL_FLAGS="--copt=-O3 --copt=-march=native --config=use_local_tf"
4953
;;
5054
*)
51-
BAZEL_FLAGS="--copt=-O3"
55+
BAZEL_FLAGS="--copt=-O3 --config=use_local_tf"
5256
;;
5357
esac
5458

ci/build_pip_package_with_docker.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ if [ ! -d /root_dir ]; then
4848
else
4949
export CI_BUILD_PYTHON="python${DOCKER_PYTHON_VERSION}"
5050
export HERMETIC_PYTHON_VERSION="${DOCKER_PYTHON_VERSION}"
51+
export TF_LOCAL_SOURCE_PATH="/root_dir/third_party/tensorflow"
5152

5253
# Running inside docker container
5354
cd /third_party_tensorflow

ci/run_bazel_test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ TEST_LANG_FILTERS="${TEST_LANG_FILTERS:-cc,py}"
2424
BUILD_FLAGS=(
2525
"--config=bulk_test_cpu"
2626
"--config=disable_tf_lite_py"
27+
"--config=use_local_tf"
2728
"--test_lang_filters=${TEST_LANG_FILTERS}"
2829
"--keep_going"
2930
"--repo_env=USE_PYWRAP_RULES=True"

ci/run_bazel_test_with_docker.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ else
6666
cp .tf_configure.bazelrc /root_dir
6767

6868
export HERMETIC_PYTHON_VERSION=${DOCKER_PYTHON_VERSION}
69+
export TF_LOCAL_SOURCE_PATH="/root_dir/third_party/tensorflow"
6970

7071
cd /root_dir
7172
bash /script_dir/run_bazel_test.sh

litert/tensorflow_source_rules.bzl

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# This file is used to define a custom repository rule for TensorFlow submodule used by LiteRT.
2+
#
3+
# The function below allows us to select between a local TensorFlow source from local_repository
4+
# or a remote http_archive based on the 'USE_LOCAL_TF' environment variable.
5+
#
6+
# To use this rule, you must set the 'USE_LOCAL_TF' environment variable to
7+
# 'true' and the 'TF_LOCAL_SOURCE_PATH' environment variable to the absolute path of
8+
# the tensorflow source directory. We need to pass the absolute path because we cannot use
9+
# ctx.path() on a relative path for lower bazel versions.
10+
11+
"""
12+
Implementation function for custom TensorFlow source repository rule.
13+
This rule is used to select between a local TensorFlow source from local_repository
14+
or a remote http_archive based on the 'USE_LOCAL_TF' environment variable.
15+
"""
16+
17+
def _tensorflow_source_repo_impl(ctx):
18+
use_local_tf = ctx.os.environ.get("USE_LOCAL_TF", "false") == "true"
19+
20+
if use_local_tf:
21+
# TF_LOCAL_SOURCE_PATH must be set to the absolute path to the tensorflow source directory.
22+
TF_LOCAL_SOURCE_PATH_ENV = ctx.os.environ.get("TF_LOCAL_SOURCE_PATH", "")
23+
if not TF_LOCAL_SOURCE_PATH_ENV:
24+
fail("""ERROR: USE_LOCAL_TF is true, but TF_LOCAL_SOURCE_PATH environment variable
25+
is not set with the absolute path to TensorFlow source.""")
26+
27+
local_path_str = TF_LOCAL_SOURCE_PATH_ENV # Get the path from the environment variable
28+
resolved_local_path = ctx.path(local_path_str)
29+
30+
for f in resolved_local_path.readdir():
31+
ctx.symlink(f, f.basename)
32+
else:
33+
ctx.download_and_extract(
34+
url = ctx.attr.urls[0],
35+
sha256 = ctx.attr.sha256,
36+
stripPrefix = ctx.attr.strip_prefix,
37+
)
38+
39+
tensorflow_source_repo = repository_rule(
40+
implementation = _tensorflow_source_repo_impl,
41+
local = False,
42+
attrs = {
43+
"sha256": attr.string(mandatory = False),
44+
"strip_prefix": attr.string(mandatory = False),
45+
"urls": attr.string_list(mandatory = True),
46+
},
47+
doc = """
48+
A custom repository rule to select between a local TensorFlow source or a remote http_archive
49+
based on the'USE_LOCAL_TF' environment variable and TF_LOCAL_SOURCE_PATH flag.
50+
""",
51+
)

0 commit comments

Comments
 (0)