summaryrefslogtreecommitdiff
path: root/tests/nnapi/specs/slicing.sh
blob: 564a7d2639d77c11e5a8d377f2677fe28ac26209 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash
# Top-level driver for slicing a given model.
# Usage: slicing.sh <number of operations> <model>
# The sliced model would be <model>_sliced_<number of operations>.mod.py
#
# Note this tool has the following side effects:
# * updates ../generated/all_generated_tests.cpp by running
#   ./generate_test.sh
# * runs adb sync for producing reference outputs
# * runs adb remount so reference outputs could be saved by the test harness

if [[ $# -ne 2 || "$1" -eq "-h" || !(-e $2) || $1 -lt 1 ]]; then
  echo "Usage: $0 <number of operations> <model>"
  echo "The sliced model would be <model>_sliced_<number of operations>.mod.py"
  echo "<number of operations> has to be >= 1"
  echo
  echo "Note this tool has the following side effects:"
  echo "* runs adb remount and adb push/pull to /data/local/tmp"
  echo "* alters ../generated/all_generated_tests.cpp"
  echo
  exit 1
fi

source $ANDROID_BUILD_TOP/build/envsetup.sh > /dev/null
source $ANDROID_BUILD_TOP/frameworks/ml/nn/runtime/test/specs/generate_test.sh

SLICE=$ANDROID_BUILD_TOP/frameworks/ml/nn/tools/test_generator/slicing.py
BASENAME=`basename -s .mod.py $2`
MODEL_ONLY=${BASENAME}_sliced.model_only.py
INPUT_ONLY=${BASENAME}_sliced.input_only.py
REFERENCE=${BASENAME}_sliced.ref.py
FINAL=${BASENAME}_sliced_$1.mod.py
SAVED_OUTPUT_FILE=/data/local/tmp/current_nnapi_example.example.py
$SLICE $2 -n $1 -m $MODEL_ONLY -e $INPUT_ONLY
if [ $? -ne 0 ]; then
  echo Error: Failed slicing $2
  exit $?
fi

# create a temporary spec from the model and the input-only example
echo "collecting_data = True" > ${BASENAME}_tmp.mod.py
cat $MODEL_ONLY $INPUT_ONLY >> ${BASENAME}_tmp.mod.py
generate_wrapper "log" $SAVED_OUTPUT_FILE ${BASENAME}_tmp.mod.py

# execute the sliced testcase and collect reference outputs
TMP_EXEC=$(adb shell mktemp --tmpdir /data/local/tmp)
HOST_EXEC_DIR=$ANDROID_PRODUCT_OUT/data/nativetest64/NeuralNetworksTest/
adb remount && mm -j40 > /dev/null && \
  adb push ${HOST_EXEC_DIR}/NeuralNetworksTest $TMP_EXEC && \
  adb shell $TMP_EXEC --gtest_filter="*.${BASENAME}_tmp" \
  &&  adb pull $SAVED_OUTPUT_FILE $REFERENCE
GENERATED=$ANDROID_BUILD_TOP/frameworks/ml/nn/runtime/test/generated/

# remove temporary spec and corresponding generated files
rm -f ${BASENAME}_tmp.mod.py
rm -f ${GENERATED}/models/${BASENAME}_tmp.model.cpp
rm -f ${GENERATED}/examples/${BASENAME}_tmp.example.cpp

if [ $? -ne 0 ]; then
  echo Error: Failed building intermediate model for $2
  exit $?
fi

echo "collecting_data = False" > ${FINAL}
cat $MODEL_ONLY $INPUT_ONLY $REFERENCE |sed s/Ignored// \
  >> ${FINAL}
echo "Example((input0, output0))" >> ${FINAL}
rm -f $MODEL_ONLY $INPUT_ONLY $REFERENCE
adb shell rm $TMP_EXEC
adb shell rm -f $SAVED_OUTPUT_FILE
# Regnerate the tests
#./generate_test.sh
echo
echo Sliced model is at $FINAL