Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / tests / scripts / models / run_test.sh
1 #!/usr/bin/env bash
2 #
3 # Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #    http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17
18 MY_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
19 NNFW_HOME="$(dirname $(dirname $(dirname ${MY_PATH})))"
20 CACHE_ROOT_PATH=$MY_PATH/"cache"
21 TEST_ROOT_PATH=$MY_PATH/"config"
22 REPORT_DIR="report"
23
24 RUN_DISABLED="true"
25
26 function command_exists() {
27         command -v "$@" > /dev/null 2>&1
28 }
29
30 function Usage()
31 {
32     echo "Usage: ./$0 --driverbin={such as tflite_run} {tests to test or empty for all of tests}"
33     echo "Usage: ./$0 --driverbin=Product/out/bin/tflite_run --reportdir=report --tapname=verification.tap avgpool1 avgpool2"
34     echo ""
35     echo "--download            - (default=on) Download model files"
36     echo "--run                 - (default=on) Test model files"
37     echo "--driverbin           - (default=../../Product/out/bin/tflite_run) Runner for runnning model tests"
38     echo "--reportdir           - (default=report) Directory to place tap files"
39     echo "--tapname             - (default=framework_test.tap) File name to be written for tap"
40     echo "--md5                 - (default=on) MD5 check when download model files"
41     echo "--configdir           - (default=$TEST_ROOT_PATH) Config directory to download and test model"
42     echo "--cachedir            - (default=$CACHE_ROOT_PATH) Directory to download model"
43     echo ""
44 }
45
46 function need_download()
47 {
48     LOCAL_PATH=$1
49     REMOTE_URL=$2
50     if [ ! -e $LOCAL_PATH ]; then
51         return 0;
52     fi
53     # Ignore checking md5 in cache
54     # TODO Use "--md5" option only and remove IGNORE_MD5 environment variable
55     if [ ! -z $IGNORE_MD5 ] && [ "$IGNORE_MD5" == "1" ]; then
56         return 1
57     fi
58     if [ "$MD5_CHECK" = "off" ]; then
59         return 1
60     fi
61
62     LOCAL_HASH=$(md5sum $LOCAL_PATH | awk '{ print $1 }')
63     REMOTE_HASH=$(curl -ss $REMOTE_URL | md5sum  | awk '{ print $1 }')
64     # TODO Emit an error when Content-MD5 field was not found. (Server configuration issue)
65     if [ "$LOCAL_HASH" != "$REMOTE_HASH" ]; then
66         echo "Downloaded file is outdated or incomplete."
67         return 0
68     fi
69     return 1
70 }
71
72 DRIVER_BIN=""
73 TAP_NAME="framework_test.tap"
74 TEST_LIST=()
75 DOWNLOAD_MODEL="on"
76 RUN_TEST="on"
77 MD5_CHECK="on"
78
79 # Support environment variable setting for mirror server
80 FIXED_MODELFILE_SERVER="${MODELFILE_SERVER:-}"
81
82 for i in "$@"
83 do
84     case $i in
85         -h|--help|help)
86             Usage
87             exit 1
88             ;;
89         --driverbin=*)
90             DRIVER_BIN=${i#*=}
91             ;;
92         --reportdir=*)
93             REPORT_DIR=${i#*=}
94             ;;
95         --tapname=*)
96             TAP_NAME=${i#*=}
97             ;;
98         --download=*)
99             DOWNLOAD_MODE=${i#*=}
100             ;;
101         --md5=*)
102             MD5_CHECK=${i#*=}
103             ;;
104         --run=*)
105             RUN_TEST=${i#*=}
106             ;;
107         --configdir=*)
108             TEST_ROOT_PATH=${i#*=}
109             ;;
110         --cachedir=*)
111             CACHE_ROOT_PATH=${i#*=}
112             ;;
113         *)
114             TEST_LIST+=( $i )
115             ;;
116     esac
117     shift
118 done
119
120 if [[ ${#TEST_LIST[@]} -eq 0 ]]; then
121     RUN_DISABLED="false"
122 fi
123
124 if [ ! -n "$DRIVER_BIN" ]; then
125     DRIVER_BIN="$NNFW_HOME/Product/out/bin/tflite_run"
126 fi
127
128 if [ ! -d "$TEST_ROOT_PATH" ]; then
129     echo "Cannot find config directory for test: please set proper configdir"
130     exit 1
131 fi
132
133 # Check test driver setting
134 if [ ! command_exists $DRIVER_BIN ] && [ "$RUN_TEST" = "on" ]; then
135     echo "Cannot find test driver" $DRIVER_BIN ": please set proper DRIVER_BIN"
136     exit 1
137 fi
138
139 run_tests()
140 {
141     echo "1..$#" > $REPORT_DIR/$TAP_NAME
142     SELECTED_TESTS=$@
143
144     echo ""
145     echo "Running tests:"
146     echo "======================"
147     for TEST_NAME in $SELECTED_TESTS; do
148         echo $TEST_NAME
149     done
150     echo "======================"
151
152     TOTAL_RESULT=0  # 0(normal) or 1(abnormal)
153     i=0
154     for TEST_NAME in $SELECTED_TESTS; do
155         # Test configure initialization
156         ((i++))
157         STATUS="enabled"
158         MODELFILE_SERVER_PATH=""
159         MODELFILE_NAME=""
160         source $TEST_ROOT_PATH/$TEST_NAME/config.sh
161
162         LOWER_STATUS="$(echo $STATUS | awk '{print tolower($0)}')"
163         if [ "$LOWER_STATUS" == "disabled" ] && [ "$RUN_DISABLED" == "false" ]; then
164             echo ""
165             echo "Skip $TEST_NAME"
166             echo "======================"
167             echo "ok $i # skip $TEST_NAME" >> $REPORT_DIR/$TAP_NAME
168             continue
169         fi
170
171         TEST_CACHE_PATH=$CACHE_ROOT_PATH/$TEST_NAME
172         MODELFILE=$TEST_CACHE_PATH/$MODELFILE_NAME
173
174         # Find model file for downloaded by zip
175         if [ "${MODELFILE_NAME##*.}" = "zip" ]; then
176             pushd $TEST_CACHE_PATH
177             MODELFILE=$TEST_CACHE_PATH/$(ls *.tflite)
178             popd
179         fi
180
181         echo ""
182         echo "Run $TEST_NAME"
183         echo "======================"
184
185         # Run driver to test framework
186         $DRIVER_BIN $MODELFILE
187
188         if [[ $? -eq 0 ]]; then
189             echo "ok $i - $TEST_NAME" >> $REPORT_DIR/$TAP_NAME
190         else
191             echo "not ok $i - $TEST_NAME" >> $REPORT_DIR/$TAP_NAME
192             TOTAL_RESULT=1
193         fi
194     done
195     return $TOTAL_RESULT
196 }
197
198 download_tests()
199 {
200     SELECTED_TESTS=$@
201
202     echo ""
203     echo "Downloading tests:"
204     echo "======================"
205     for TEST_NAME in $SELECTED_TESTS; do
206         echo $TEST_NAME
207     done
208     echo "======================"
209
210     i=0
211     for TEST_NAME in $SELECTED_TESTS; do
212         # Test configure initialization
213         ((i++))
214         MODELFILE_SERVER_PATH=""
215         MODELFILE_NAME=""
216         source $TEST_ROOT_PATH/$TEST_NAME/config.sh
217
218         TEST_CACHE_PATH=$CACHE_ROOT_PATH/$TEST_NAME
219         MODELFILE=$TEST_CACHE_PATH/$MODELFILE_NAME
220         MODELFILE_URL="$MODELFILE_SERVER/$MODELFILE_NAME"
221         if [ -n  "$FIXED_MODELFILE_SERVER" ]; then
222             MODELFILE_URL="$FIXED_MODELFILE_SERVER/$MODELFILE_NAME"
223         fi
224
225         # Download model file
226         if [ ! -e $TEST_CACHE_PATH ]; then
227             mkdir -p $TEST_CACHE_PATH
228         fi
229
230         # Download unless we have it in cache (Also check md5sum)
231         if need_download "$MODELFILE" "$MODELFILE_URL"; then
232             echo ""
233             echo "Download test file for $TEST_NAME"
234             echo "======================"
235
236             rm -f $MODELFILE # Remove invalid file if exists
237             pushd $TEST_CACHE_PATH
238             wget -nv $MODELFILE_URL
239             if [ "${MODELFILE_NAME##*.}" == "zip" ]; then
240                 unzip -o $MODELFILE_NAME
241             fi
242             popd
243         fi
244
245     done
246 }
247
248
249 find_tests()
250 {
251     local TEST_DIRS="$@"
252     local TESTS_TO_RUN=""
253
254     if [[ $# -eq 0 ]]; then
255         TEST_DIRS="."
256     fi
257
258     shift $#
259
260     pushd $TEST_ROOT_PATH > /dev/null
261     for DIR in $TEST_DIRS; do
262         if [ -d "$DIR" ]; then
263             TESTS_FOUND=$(find "$DIR" -type f -name 'config.sh' -exec dirname {} \;| sed 's|^./||' | sort)
264             TESTS_TO_RUN="$TESTS_TO_RUN $TESTS_FOUND"
265         else
266             echo "Test $DIR was not found. This test is not added." 1>&2
267         fi
268     done
269     popd > /dev/null
270
271     echo $TESTS_TO_RUN
272 }
273
274 mkdir -p $REPORT_DIR
275 TESTS_TO_RUN=$(find_tests ${TEST_LIST[@]})
276
277 if [ "$DOWNLOAD_MODEL" = "on" ]; then
278     download_tests $TESTS_TO_RUN
279 fi
280
281 if [ "$RUN_TEST" = "on" ]; then
282     run_tests $TESTS_TO_RUN
283 fi
284 exit $?