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 12 if [[ $# -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 22 fi 23 24 set -eu 25 26 NO_OF_OPERATIONS=$1 27 INPUT=$2 28 SLICE=$ANDROID_BUILD_TOP/frameworks/ml/nn/tools/test_generator/slicing.py 29 DIR_AND_NAME=${INPUT/.mod.py/} 30 BASENAME=$(basename $DIR_AND_NAME) 31 MODEL_ONLY=${DIR_AND_NAME}_sliced.model_only.py 32 INPUT_ONLY=${DIR_AND_NAME}_sliced.input_only.py 33 REFERENCE=${DIR_AND_NAME}_sliced.ref.py 34 FINAL=${DIR_AND_NAME}_sliced_$1.mod.py 35 SAVED_OUTPUT_FILE=/data/local/tmp/current_nnapi_example.example.py 36 37 set +eu 38 source $ANDROID_BUILD_TOP/build/envsetup.sh > /dev/null 39 lunch $TARGET_PRODUCT 40 set -eu 41 source $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 46 FORCE = "-f" 47 echo "collecting_data = True" > ${DIR_AND_NAME}_tmp.mod.py 48 cat $MODEL_ONLY $INPUT_ONLY >> ${DIR_AND_NAME}_tmp.mod.py 49 generate_wrapper "log" $SAVED_OUTPUT_FILE ${DIR_AND_NAME}_tmp.mod.py 50 51 # execute the sliced testcase and collect reference outputs 52 TMP_EXEC=$(adb shell mktemp --tmpdir /data/local/tmp) 53 HOST_EXEC_DIR=$ANDROID_PRODUCT_OUT/data/nativetest64/NeuralNetworksTest_static/ 54 if [[ ! -d $HOST_EXEC_DIR ]]; then 55 HOST_EXEC_DIR=$ANDROID_PRODUCT_OUT/data/nativetest/NeuralNetworksTest_static/ 56 fi 57 [[ -d $HOST_EXEC_DIR ]] || (echo "cannot find $HOST_EXEC_DIR"; exit 1) 58 59 set +u 60 adb remount 61 mm -j40 62 adb push ${HOST_EXEC_DIR}/NeuralNetworksTest_static $TMP_EXEC 63 adb shell $TMP_EXEC --gtest_filter="*.${BASENAME}_tmp" 64 adb pull $SAVED_OUTPUT_FILE $REFERENCE 65 set -u 66 GENERATED=$ANDROID_BUILD_TOP/frameworks/ml/nn/runtime/test/generated/ 67 68 # remove temporary spec and corresponding generated files 69 rm ${DIR_AND_NAME}_tmp.mod.py 70 rm ${GENERATED}/models/${BASENAME}_tmp.model.cpp 71 rm ${GENERATED}/examples/${BASENAME}_tmp.example.cpp 72 73 if [ $? -ne 0 ]; then 74 echo Error: Failed building intermediate model for $2 75 exit $? 76 fi 77 78 echo "collecting_data = False" > ${FINAL} 79 cat $MODEL_ONLY $INPUT_ONLY $REFERENCE |sed s/Ignored// \ 80 >> ${FINAL} 81 echo "Example((input0, output0))" >> ${FINAL} 82 rm -f $MODEL_ONLY $INPUT_ONLY $REFERENCE 83 adb shell rm $TMP_EXEC 84 adb shell rm -f $SAVED_OUTPUT_FILE 85 # Regnerate the tests 86 #./generate_test.sh 87 echo 88 echo Sliced model is at $FINAL 89