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