Allow users to specficy cache dir in the host machine

The container will use this directory to store build cache.

PiperOrigin-RevId: 340838033
Change-Id: Idd691f304fdda80f4944b6a207445038f7be8035
This commit is contained in:
Thai Nguyen 2020-11-05 06:13:23 -08:00 committed by TensorFlower Gardener
parent fca99ecafe
commit 3281b92474
3 changed files with 53 additions and 23 deletions

View File

@ -137,14 +137,20 @@ paths as follows.
sh build_aar_with_docker.sh \
--input_models=/a/b/model_one.tflite,/c/d/model_two.tflite \
--target_archs=x86,x86_64,arm64-v8a,armeabi-v7a \
--checkpoint=master
--checkpoint=master \
[--cache_dir=<path to cache directory>]
```
The `checkpoint` flag is a commit, a branch or a tag of the TensorFlow repo that
you want to checkout before building the libraries. The above command will
generate the AAR file `tensorflow-lite.aar` for TensorFlow Lite built-in and
custom ops and optionally the AAR file `tensorflow-lite-select-tf-ops.aar` for
Select TensorFlow ops in your current directory.
you want to checkout before building the libraries; by default it is the latest
release branch. The above command will generate the AAR file
`tensorflow-lite.aar` for TensorFlow Lite built-in and custom ops and optionally
the AAR file `tensorflow-lite-select-tf-ops.aar` for Select TensorFlow ops in
your current directory.
The --cache_dir specify the cache directory. If not provided, the script will
create a directory named `bazel-build-cache` under current working directory for
caching.
## Add AAR files to project

View File

@ -41,7 +41,7 @@ function generate_list_field {
local list_string="$2"
local list=(${list_string//,/ })
local message+=("$name=[")
local message=("$name=[")
for item in "${list[@]}"
do
message+=("\"$item\",")
@ -51,12 +51,14 @@ function generate_list_field {
}
function print_output {
echo "Output can be found here:"
for i in "$@"
do
# Check if the file exist.
ls -1a ${ROOT_DIR}/$i
done
if [ -z OMIT_PRINTING_OUTPUT_PATHS ]; then
echo "Output can be found here:"
for i in "$@"
do
# Check if the file exist.
ls -1a ${ROOT_DIR}/$i
done
fi
}
function generate_tflite_aar {
@ -85,7 +87,7 @@ function generate_tflite_aar {
# Build the aar package.
popd > /dev/null
bazel build -c opt --cxxopt='--std=c++14' \
bazel ${CACHE_DIR_FLAG} build -c opt --cxxopt='--std=c++14' \
--fat_apk_cpu=${TARGET_ARCHS} \
--host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
//tmp:tensorflow-lite
@ -119,7 +121,7 @@ function generate_flex_aar {
popd
# Build the aar package.
bazel build -c opt --cxxopt='--std=c++14' \
bazel ${CACHE_DIR_FLAG} build -c opt --cxxopt='--std=c++14' \
--fat_apk_cpu=${TARGET_ARCHS} \
--host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
//tmp:tensorflow-lite-select-tf-ops
@ -129,6 +131,11 @@ function generate_flex_aar {
# Check command line flags.
TARGET_ARCHS=x86,x86_64,arm64-v8a,armeabi-v7a
# If the environmant variable BAZEL_CACHE_DIR is set, use it as the user root
# directory of bazel.
if [ ! -z ${BAZEL_CACHE_DIR} ]; then
CACHE_DIR_FLAG="--output_user_root=${BAZEL_CACHE_DIR}/cache"
fi
if [ "$#" -gt 4 ]; then
echo "ERROR: Too many arguments."
@ -170,7 +177,7 @@ fi
# Build the standard aar package of no models provided.
if [ -z ${FLAG_MODELS} ]; then
bazel build -c opt --cxxopt='--std=c++14' \
bazel ${CACHE_DIR_FLAG} build -c opt --cxxopt='--std=c++14' \
--fat_apk_cpu=${TARGET_ARCHS} \
--host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
//tensorflow/lite/java:tensorflow-lite
@ -203,8 +210,10 @@ done
generate_tflite_aar
# Build flex aar if one of the models contain flex ops.
bazel build -c opt --config=monolithic //tensorflow/lite/tools:list_flex_ops_no_kernel_main
bazel-bin/tensorflow/lite/tools/list_flex_ops_no_kernel_main --graphs=${FLAG_MODELS} > ${TMP_DIR}/ops_list.txt
bazel ${CACHE_DIR_FLAG} build -c opt --config=monolithic \
//tensorflow/lite/tools:list_flex_ops_no_kernel_main
bazel-bin/tensorflow/lite/tools/list_flex_ops_no_kernel_main \
--graphs=${FLAG_MODELS} > ${TMP_DIR}/ops_list.txt
if [[ `cat ${TMP_DIR}/ops_list.txt` != "[]" ]]; then
generate_flex_aar
fi

View File

@ -23,12 +23,16 @@ function print_usage {
echo " $(basename ${BASH_SOURCE}) \\"
echo " --input_models=model1.tflite,model2.tflite \\"
echo " --target_archs=x86,x86_64,arm64-v8a,armeabi-v7a \\"
echo " --checkpoint=master"
echo " --checkpoint=master \\"
echo " [--cache_dir=<path to cache directory>]"
echo ""
echo "Where: "
echo " --input_models: Supported TFLite models. "
echo " --target_archs: Supported arches included in the aar file."
echo " --checkpoint: Checkpoint of the github repo, could be a branch, a commit or a tag. Default: master"
echo " --checkpoint: Checkpoint of the github repo, could be a branch, a "
echo " commit or a tag. Default: lastest release branch."
echo " --cache_dir: Path to the directory to store bazel cache. If not "
echo " provided, a directory name bazel-build-cache will be created."
echo ""
exit 1
}
@ -37,9 +41,9 @@ function print_usage {
ARGUMENTS=$@
BUILD_FLAGS=""
TARGET_ARCHS=x86,x86_64,arm64-v8a,armeabi-v7a
FLAG_CHECKPOINT="master"
FLAG_CHECKPOINT="r2.4" # TODO(b/163918542) Set default to lastest release.
if [ "$#" -gt 3 ]; then
if [ "$#" -gt 4 ]; then
echo "ERROR: Too many arguments."
print_usage
fi
@ -58,6 +62,9 @@ case $i in
--checkpoint=*)
FLAG_CHECKPOINT="${i#*=}"
shift;;
--cache_dir=*)
BAZEL_CACHE_DIR="${i#*=}"
shift;;
*)
echo "ERROR: Unrecognized argument: ${i}"
print_usage;;
@ -70,6 +77,14 @@ if [ ! -d /tensorflow_src ]; then
do
FLAG_DIR="${FLAG_DIR} -v ${model}:${model}"
done
if [ -z ${BAZEL_CACHE_DIR} ]; then
mkdir "bazel-build-cache"
BAZEL_CACHE_DIR="$PWD/bazel-build-cache"
ARGUMENTS="${ARGUMENTS} --cache_dir=${BAZEL_CACHE_DIR}"
fi
FLAG_DIR="${FLAG_DIR} -v ${BAZEL_CACHE_DIR}:${BAZEL_CACHE_DIR}"
docker run --rm -it -v $PWD:/host_dir -v ${SCRIPT_DIR}:/script_dir ${FLAG_DIR} \
--entrypoint /script_dir/build_aar_with_docker.sh tflite-builder \
${ARGUMENTS}
@ -100,10 +115,11 @@ else
git checkout ${FLAG_CHECKPOINT}
# Building with bazel.
export BAZEL_CACHE_DIR=${BAZEL_CACHE_DIR}
export OMIT_PRINTING_OUTPUT_PATHS=YES
bash /tensorflow_src/tensorflow/lite/tools/build_aar.sh ${BUILD_FLAGS}
# Copy the output files from docker container.
clear
OUT_FILES="/tensorflow_src/bazel-bin/tmp/tensorflow-lite.aar"
OUT_FILES="${OUT_FILES} /tensorflow_src/bazel-bin/tmp/tensorflow-lite-select-tf-ops.aar"
echo "Output can be found here:"
@ -115,4 +131,3 @@ else
fi
done
fi