1#!/bin/bash
2
3# This script build and run DumpIntermediateTensors activity
4# The results will be pulled to /tmp/intermediate by default.
5# Usage
6# ./test/mlts/benchmark/tools/build_and_dump_intermediate.sh -o /tmp -r intermediate_test -p -m fssd_100_8bit_gray_v1,fssd_100_8bit_v1,fssd_25_8bit_gray_v1,fssd_25_8bit_v1
7
8if [[ -z "$ANDROID_BUILD_TOP" ]]; then
9  echo ANDROID_BUILD_TOP not set, bailing out
10  echo you must run lunch before running this script
11  exit 1
12fi
13
14# Default output directory: /tmp/intermediate_currentdate
15INTERMEDIATE_OUTPUT_DIR="/tmp"
16CURRENTDATE=`date +"%m%d%y"`
17RENAME="intermediate_$CURRENTDATE"
18BUILD_MODE=true
19RUN_PYTHON=false
20MODEL_LIST=""
21
22while getopts 'o:r:m:nph' flag; do
23  case "${flag}" in
24    o) INTERMEDIATE_OUTPUT_DIR="${OPTARG}" ;;
25    r) RENAME="${OPTARG}" ;;
26    m) MODEL_LIST="modelName ${OPTARG}" ;;
27    n) BUILD_MODE=false ;;
28    p) RUN_PYTHON=true ;;
29    h)
30      echo "Optional flags:"
31      echo "  -h                  Display this help message."
32      echo "  -o <output_dir>     Set destination directory for the output folder."
33      echo "  -r <output_name>    Name of the output folder."
34      echo "  -m <model_list>     A list of target model names separated by comma(,) e.g. asr_float,tts_float."
35      echo "  -n                  If set, skipping build and installation to save time."
36      echo "  -p                  If set, run Python script to generate visualization html."
37      exit 0
38      ;;
39    *)
40      error "Unexpected option ${flag}, please run with -h to see the options"
41      exit 1
42      ;;
43  esac
44done
45
46cd $ANDROID_BUILD_TOP
47
48if [[ "$BUILD_MODE" == true ]]; then
49  # Build and install benchmark app
50  build/soong/soong_ui.bash --make-mode NeuralNetworksApiBenchmark
51  if ! adb install -r $OUT/testcases/NeuralNetworksApiBenchmark/arm64/NeuralNetworksApiBenchmark.apk; then
52    adb uninstall com.android.nn.benchmark.app
53    adb install -r $OUT/testcases/NeuralNetworksApiBenchmark/arm64/NeuralNetworksApiBenchmark.apk
54  fi
55fi
56
57# Default to run all public models in DumpIntermediateTensors
58adb shell am start -n com.android.nn.benchmark.app/com.android.nn.benchmark.util.DumpIntermediateTensors \
59--es "$MODEL_LIST" inputAssetIndex 0 &&
60# Wait for the files to finish writing.
61# TODO(veralin): find a better way to wait, maybe some sort of callback
62sleep 13 &&
63
64mkdir -p $INTERMEDIATE_OUTPUT_DIR &&
65cd $INTERMEDIATE_OUTPUT_DIR &&
66rm -rf intermediate &&
67adb pull /data/data/com.android.nn.benchmark.app/files/intermediate/ &&
68rsync -a --delete intermediate/ $RENAME/ &&
69echo "Results pulled to $INTERMEDIATE_OUTPUT_DIR/$RENAME"
70
71if [[ "$RUN_PYTHON" == true ]]; then
72  cd $ANDROID_BUILD_TOP &&
73  python test/mlts/benchmark/tools/tensor_utils.py $ANDROID_BUILD_TOP $INTERMEDIATE_OUTPUT_DIR/$RENAME
74fi
75
76exit