• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
46echo "collecting_data = True" > ${DIR_AND_NAME}_tmp.mod.py
47cat $MODEL_ONLY $INPUT_ONLY >> ${DIR_AND_NAME}_tmp.mod.py
48generate_wrapper "log" $SAVED_OUTPUT_FILE ${DIR_AND_NAME}_tmp.mod.py
49
50# execute the sliced testcase and collect reference outputs
51TMP_EXEC=$(adb shell mktemp --tmpdir /data/local/tmp)
52HOST_EXEC_DIR=$ANDROID_PRODUCT_OUT/data/nativetest64/NeuralNetworksTest_static/
53if [[ ! -d $HOST_EXEC_DIR ]]; then
54  HOST_EXEC_DIR=$ANDROID_PRODUCT_OUT/data/nativetest/NeuralNetworksTest_static/
55fi
56[[ -d $HOST_EXEC_DIR ]] || (echo "cannot find $HOST_EXEC_DIR"; exit 1)
57
58set +u
59adb remount
60mm -j40
61adb push ${HOST_EXEC_DIR}/NeuralNetworksTest_static $TMP_EXEC
62adb shell $TMP_EXEC --gtest_filter="*.${BASENAME}_tmp"
63adb pull $SAVED_OUTPUT_FILE $REFERENCE
64set -u
65GENERATED=$ANDROID_BUILD_TOP/frameworks/ml/nn/runtime/test/generated/
66
67# remove temporary spec and corresponding generated files
68rm ${DIR_AND_NAME}_tmp.mod.py
69rm ${GENERATED}/models/${BASENAME}_tmp.model.cpp
70rm ${GENERATED}/examples/${BASENAME}_tmp.example.cpp
71
72if [ $? -ne 0 ]; then
73  echo Error: Failed building intermediate model for $2
74  exit $?
75fi
76
77echo "collecting_data = False" > ${FINAL}
78cat $MODEL_ONLY $INPUT_ONLY $REFERENCE |sed s/Ignored// \
79  >> ${FINAL}
80echo "Example((input0, output0))" >> ${FINAL}
81rm -f $MODEL_ONLY $INPUT_ONLY $REFERENCE
82adb shell rm $TMP_EXEC
83adb shell rm -f $SAVED_OUTPUT_FILE
84# Regnerate the tests
85#./generate_test.sh
86echo
87echo Sliced model is at $FINAL
88