Added script for running delegate testing on Linux/Android.
Fixes to delegate testing.
PiperOrigin-RevId: 323390232
Change-Id: Iafc6e97c399933a0e45a83f3276ac2a44f82611e
diff --git a/tensorflow/lite/delegates/gpu/cl/testing/delegate_testing.cc b/tensorflow/lite/delegates/gpu/cl/testing/delegate_testing.cc
index 4e92f89..10b7ac3 100644
--- a/tensorflow/lite/delegates/gpu/cl/testing/delegate_testing.cc
+++ b/tensorflow/lite/delegates/gpu/cl/testing/delegate_testing.cc
@@ -32,11 +32,19 @@
void FillInputTensor(tflite::Interpreter* interpreter) {
for (int k = 0; k < interpreter->inputs().size(); ++k) {
- float* p = interpreter->typed_input_tensor<float>(k);
- const auto n =
- tflite::NumElements(interpreter->tensor(interpreter->inputs()[k]));
- for (int i = 0; i < n; ++i) {
- p[i] = std::sin(i);
+ TfLiteTensor* tensor_ptr = interpreter->tensor(interpreter->inputs()[k]);
+ const auto tensor_elements_count = tflite::NumElements(tensor_ptr);
+ if (tensor_ptr->type == kTfLiteFloat32) {
+ float* p = interpreter->typed_input_tensor<float>(k);
+ for (int i = 0; i < tensor_elements_count; ++i) {
+ p[i] = std::sin(i);
+ }
+ }
+ if (tensor_ptr->type == kTfLiteInt32) {
+ int* p = interpreter->typed_input_tensor<int>(k);
+ for (int i = 0; i < tensor_elements_count; ++i) {
+ p[i] = i % 2;
+ }
}
}
}
@@ -124,6 +132,7 @@
options.inference_priority1 = TFLITE_GPU_INFERENCE_PRIORITY_MIN_LATENCY;
options.inference_priority2 = TFLITE_GPU_INFERENCE_PRIORITY_MIN_MEMORY_USAGE;
options.inference_priority3 = TFLITE_GPU_INFERENCE_PRIORITY_MAX_PRECISION;
+ options.max_delegated_partitions = 1;
auto* gpu_delegate = TfLiteGpuDelegateV2Create(&options);
status = gpu_inference->ModifyGraphWithDelegate(gpu_delegate);
if (status != kTfLiteOk) {
diff --git a/tensorflow/lite/delegates/gpu/cl/testing/run_delegate_testing.sh b/tensorflow/lite/delegates/gpu/cl/testing/run_delegate_testing.sh
new file mode 100755
index 0000000..7b86407
--- /dev/null
+++ b/tensorflow/lite/delegates/gpu/cl/testing/run_delegate_testing.sh
@@ -0,0 +1,96 @@
+#!/bin/bash
+# Copyright 2020 The TensorFlow Authors. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ==============================================================================
+
+shopt -s expand_aliases # to work with commands aliases in .sh
+
+description="Delegate testing sample:
+Compares GPU backend vs TFLite CPU(speed/correctness).
+How to use:
+[-h or --help, print instructions]
+[-m or --model_path, path to the model in .tflite format]
+[-d or --device, select device](optional, if you have few connected devices)"
+
+model_path=""
+alias ADB='adb'
+host=""
+
+while [[ "$1" != "" ]]; do
+ case $1 in
+ -m | --model_path)
+ shift
+ model_path=$1
+ ;;
+ -d | --device)
+ shift
+ if [[ "$1" == "HOST" ]]
+ then
+ host="HOST"
+ fi
+ alias ADB='adb -s '$1''
+ ;;
+ -h | --help)
+ echo "$description"
+ exit
+ ;;
+ esac
+ shift
+done
+
+if [ "$model_path" = "" ]
+then
+echo "No model provided."
+echo "$description"
+exit
+fi
+
+SHELL_DIR=$(dirname "$0")
+BINARY_NAME=delegate_testing
+
+if [[ "$host" == "HOST" ]]
+then
+bazel build -c opt --copt -DCL_DELEGATE_NO_GL //"$SHELL_DIR":"$BINARY_NAME"
+chmod +x bazel-bin/"$SHELL_DIR"/"$BINARY_NAME"
+./bazel-bin/"$SHELL_DIR"/"$BINARY_NAME" "$model_path"
+exit
+fi
+
+model_name=${model_path##*/} # finds last token after '/'
+
+OPENCL_DIR=/data/local/tmp/delegate_testing/
+
+ADB shell mkdir -p $OPENCL_DIR
+
+ADB push "$model_path" "$OPENCL_DIR"
+
+declare -a BUILD_CONFIG
+abi_version=$(ADB shell getprop ro.product.cpu.abi | tr -d '\r')
+if [[ "$abi_version" == "armeabi-v7a" ]]; then
+#"32 bit"
+BUILD_CONFIG=( --config=android_arm -c opt --copt=-fPIE --linkopt=-pie )
+else
+#"64 bit"
+BUILD_CONFIG=( --config=android_arm64 -c opt )
+fi
+
+bazel build "${BUILD_CONFIG[@]}" //$SHELL_DIR:$BINARY_NAME
+
+ADB push bazel-bin/$SHELL_DIR/$BINARY_NAME $OPENCL_DIR
+
+ADB shell chmod +x $OPENCL_DIR/$BINARY_NAME
+ADB shell "cd $OPENCL_DIR && ./$BINARY_NAME $model_name"
+
+# clean up files from device
+ADB shell rm -rf $OPENCL_DIR