1#!/bin/bash
2# Top-level driver for slicing a given model.
3# Usage: slicing.sh <number of operations> <model>
4# The sliced model would be <model>_sliced_<number of operations>.mod.py
5#
6# Note this tool has the following side effects:
7# * updates ../generated/all_generated_tests.cpp by running
8#   ./generate_test.sh
9# * runs adb sync for producing reference outputs
10# * runs adb remount so reference outputs could be saved by the test harness
11
12if [[ $# -ne 2 || "$1" -eq "-h" || !(-e $2) || $1 -lt 1 ]]; then
13  echo "Usage: $0 <number of operations> <model>"
14  echo "The sliced model would be <model>_sliced_<number of operations>.mod.py"
15  echo "<number of operations> has to be >= 1"
16  echo
17  echo "Note this tool has the following side effects:"
18  echo "* runs adb remount and adb push/pull to /data/local/tmp"
19  echo "* alters ../generated/all_generated_tests.cpp"
20  echo
21  exit 1
22fi
23
24set -eu
25
26NO_OF_OPERATIONS=$1
27INPUT=$2
28SLICE=$ANDROID_BUILD_TOP/frameworks/ml/nn/tools/test_generator/slicing.py
29DIR_AND_NAME=${INPUT/.mod.py/}
30BASENAME=$(basename $DIR_AND_NAME)
31MODEL_ONLY=${DIR_AND_NAME}_sliced.model_only.py
32INPUT_ONLY=${DIR_AND_NAME}_sliced.input_only.py
33REFERENCE=${DIR_AND_NAME}_sliced.ref.py
34FINAL=${DIR_AND_NAME}_sliced_$1.mod.py
35SAVED_OUTPUT_FILE=/data/local/tmp/current_nnapi_example.example.py
36
37set +eu
38source $ANDROID_BUILD_TOP/build/envsetup.sh > /dev/null
39lunch $TARGET_PRODUCT
40set -eu
41source $ANDROID_BUILD_TOP/frameworks/ml/nn/runtime/test/specs/generate_test.sh
42
43$SLICE $INPUT -n $NO_OF_OPERATIONS -m $MODEL_ONLY -e $INPUT_ONLY
44
45# create a temporary spec from the model and the input-only example
46FORCE = "-f"
47echo "collecting_data = True" > ${DIR_AND_NAME}_tmp.mod.py
48cat $MODEL_ONLY $INPUT_ONLY >> ${DIR_AND_NAME}_tmp.mod.py
49generate_wrapper "log" $SAVED_OUTPUT_FILE ${DIR_AND_NAME}_tmp.mod.py
50
51# execute the sliced testcase and collect reference outputs
52TMP_EXEC=$(adb shell mktemp --tmpdir /data/local/tmp)
53HOST_EXEC_DIR=$ANDROID_PRODUCT_OUT/data/nativetest64/NeuralNetworksTest_static/
54if [[ ! -d $HOST_EXEC_DIR ]]; then
55  HOST_EXEC_DIR=$ANDROID_PRODUCT_OUT/data/nativetest/NeuralNetworksTest_static/
56fi
57[[ -d $HOST_EXEC_DIR ]] || (echo "cannot find $HOST_EXEC_DIR"; exit 1)
58
59set +u
60adb remount
61mm -j40
62adb push ${HOST_EXEC_DIR}/NeuralNetworksTest_static $TMP_EXEC
63adb shell $TMP_EXEC --gtest_filter="*.${BASENAME}_tmp"
64adb pull $SAVED_OUTPUT_FILE $REFERENCE
65set -u
66GENERATED=$ANDROID_BUILD_TOP/frameworks/ml/nn/runtime/test/generated/
67
68# remove temporary spec and corresponding generated files
69rm ${DIR_AND_NAME}_tmp.mod.py
70rm ${GENERATED}/models/${BASENAME}_tmp.model.cpp
71rm ${GENERATED}/examples/${BASENAME}_tmp.example.cpp
72
73if [ $? -ne 0 ]; then
74  echo Error: Failed building intermediate model for $2
75  exit $?
76fi
77
78echo "collecting_data = False" > ${FINAL}
79cat $MODEL_ONLY $INPUT_ONLY $REFERENCE |sed s/Ignored// \
80  >> ${FINAL}
81echo "Example((input0, output0))" >> ${FINAL}
82rm -f $MODEL_ONLY $INPUT_ONLY $REFERENCE
83adb shell rm $TMP_EXEC
84adb shell rm -f $SAVED_OUTPUT_FILE
85# Regnerate the tests
86#./generate_test.sh
87echo
88echo Sliced model is at $FINAL
89