Imported Upstream version 1.4.0
[platform/core/ml/nnfw.git] / tools / nnpackage_tool / nnpkg_test / nnpkg_test.sh
1 #!/bin/bash
2
3 set -u
4
5 command_exists() {
6         command -v "$@" > /dev/null 2>&1
7 }
8
9 progname=$(basename "${BASH_SOURCE[0]}")
10 indir="."
11 outdir="."
12 nnpkg_run=${nnpkg_run:-"Product/out/bin/nnpackage_run"}
13 difftool=${difftool:-"h5diff"}
14 delete_dumped_on_failure=0
15
16 usage() {
17   echo "Usage: $progname [options] nnpackage_test"
18   echo "Run an nnpackage testcase"
19   echo ""
20   echo "Returns"
21   echo "     0       success"
22   echo "  non-zero   failure"
23   echo ""
24   echo "Options:"
25   echo "    -h   show this help"
26   echo "    -i   set input directory (default=$indir)"
27   echo "    -o   set output directory (default=$outdir)"
28   echo "    -d   delete dumped file on failure."
29   echo "         (dumped file are always deleted on success) (default=$delete_dumped_on_failure)"
30   echo ""
31   echo "Environment variables:"
32   echo "   nnpackage_run    path to nnpackage_run (default=Product/out/bin/nnpackage_run)"
33   echo "   difftool         path to i5diff or h5diff (default=h5diff)"
34   echo ""
35   echo "Examples:"
36   echo "    $progname Add_000                => run $indir/Add_000 and check output"
37   echo "    $progname -i nnpkg-tcs Add_000   => run nnpkg-tcs/Add_000 and check output"
38   exit 1
39 }
40
41 if [ $# -eq 0 ]; then
42   echo "For help, type $progname -h"
43   exit 1
44 fi
45
46 while getopts "hdi:o:" OPTION; do
47 case "${OPTION}" in
48     h) usage;;
49     d) delete_dumped_on_failure=1;;
50     i) indir=$OPTARG;;
51     o) outdir=$OPTARG;;
52     ?) exit 1;;
53 esac
54 done
55
56 shift $((OPTIND-1))
57
58 if [ $# -ne 1 ]; then
59   echo "error: wrong argument (no argument or too many arguments)."
60   echo "For help, type $progname -h"
61   exit 1
62 fi
63
64 if [ ! -e Product ]; then
65   echo "error: please make sure to run this script in nnfw home."
66   exit 1
67 fi
68
69 tcname=$(basename "$1")
70 nnpkg="$indir/$tcname"
71
72 # run
73
74 if [ ! -e $nnpkg ]; then
75   echo "error: nnpackage "$nnpkg" does not exist."
76   exit 1
77 fi
78
79 if ! command_exists $nnpkg_run; then
80   echo "error: runner "$nnpkg_run" does not exist."
81   exit 1
82 fi
83
84 dumped="$outdir/$tcname".out.h5
85
86 echo -n "[  Run  ] $nnpkg "
87
88 if $nnpkg_run \
89 --nnpackage "$nnpkg" \
90 --load "$nnpkg/metadata/tc/input.h5" \
91 --dump "$dumped" >& /dev/null > "$dumped.log" 2>&1 ; then
92   echo -e "\tPass"
93   rm "$dumped.log"
94 else
95   echo -e "\tFail"
96   echo ""
97   cat "$dumped.log"
98   echo ""
99   rm "$dumped.log"
100   exit 2
101 fi
102
103 # diff
104
105 if ! command_exists $difftool; then
106   echo "error: difftool "$difftool" does not exist."
107   exit 1
108 fi
109
110 expected="$nnpkg/metadata/tc/expected.h5"
111
112 echo -n "[Compare] $nnpkg "
113
114 if $difftool -d 0.001 -v "$dumped" "$expected" /value >& "$dumped.log"; then
115   echo -e "\tPass"
116   rm "$dumped" "$dumped.log"
117 else
118   echo -e "\tFail"
119   [ $delete_dumped_on_failure ] && rm "$dumped"
120   cat "$dumped.log"
121   rm "$dumped.log"
122   exit 3
123 fi