[nnpkg_tool] Introduce nnpkg_test.sh (#7020)
author이상규/On-Device Lab(SR)/Principal Engineer/삼성전자 <sg5.lee@samsung.com>
Thu, 29 Aug 2019 05:39:06 +0000 (14:39 +0900)
committer오형석/On-Device Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Thu, 29 Aug 2019 05:39:06 +0000 (14:39 +0900)
Introduce a tool for running an nnpackage testcase.
It will run nnpackage with nnpackage_run, and compare the result.
For usage, find README.md.

Signed-off-by: Sanggyu Lee <sg5.lee@samsung.com>
tools/nnpackage_tool/nnpkg_test/README.md [new file with mode: 0644]
tools/nnpackage_tool/nnpkg_test/nnpkg_test.sh [new file with mode: 0755]

diff --git a/tools/nnpackage_tool/nnpkg_test/README.md b/tools/nnpackage_tool/nnpkg_test/README.md
new file mode 100644 (file)
index 0000000..916877a
--- /dev/null
@@ -0,0 +1,42 @@
+# nnpkg_test
+
+`nnpkg_test` is a tool to run an nnpackage testcase.
+
+`nnpackage testcase` is an nnpackage with additional data:
+
+- input.h5 (input data)
+- expected.h5 (expected outpute data)
+
+`nnpkg_test` uses `nnpackage_run` internally to run `nnpackage`.
+
+Then, it compares through `difftool` (either `i5diff` or `h5diff`).
+
+`nnpkg_test` returns `0` on success, `non-zero` otherwise.
+
+## Usage
+
+```
+$ tools/nnpackage_tool/nnpkg_test/nnpkg_test.sh -h
+Usage: nnpkg_test.sh [options] nnpackage_test
+Run an nnpackage testcase
+
+Returns
+     0       success
+  non-zero   failure
+
+Options:
+    -h   show this help
+    -i   set input directory (default=.)
+    -o   set output directory (default=.)
+    -d   delete dumped file on failure.
+         (dumped file are always deleted on success) (default=0)
+
+Environment variables:
+   nnpackage_run    path to nnpackage_run (default=Product/out/bin/nnpackage_run)
+   difftool         path to i5diff or h5diff (default=h5diff)
+
+Examples:
+    nnpkg_test.sh Add_000                => run ./Add_000 and check output
+    nnpkg_test.sh -i nnpkg-tcs Add_000   => run nnpkg-tcs/Add_000 and check output
+
+```
diff --git a/tools/nnpackage_tool/nnpkg_test/nnpkg_test.sh b/tools/nnpackage_tool/nnpkg_test/nnpkg_test.sh
new file mode 100755 (executable)
index 0000000..caa5aa4
--- /dev/null
@@ -0,0 +1,108 @@
+#!/bin/bash
+
+set -eu
+
+command_exists() {
+       command -v "$@" > /dev/null 2>&1
+}
+
+progname=$(basename "${BASH_SOURCE[0]}")
+indir="."
+outdir="."
+nnpkg_run=${nnpkg_run:-"Product/out/bin/nnpackage_run"}
+difftool=${difftool:-"h5diff"}
+delete_dumped_on_failure=0
+
+usage() {
+  echo "Usage: $progname [options] nnpackage_test"
+  echo "Run an nnpackage testcase"
+  echo ""
+  echo "Returns"
+  echo "     0       success"
+  echo "  non-zero   failure"
+  echo ""
+  echo "Options:"
+  echo "    -h   show this help"
+  echo "    -i   set input directory (default=$indir)"
+  echo "    -o   set output directory (default=$outdir)"
+  echo "    -d   delete dumped file on failure."
+  echo "         (dumped file are always deleted on success) (default=$delete_dumped_on_failure)"
+  echo ""
+  echo "Environment variables:"
+  echo "   nnpackage_run    path to nnpackage_run (default=Product/out/bin/nnpackage_run)"
+  echo "   difftool         path to i5diff or h5diff (default=h5diff)"
+  echo ""
+  echo "Examples:"
+  echo "    $progname Add_000                => run $indir/Add_000 and check output"
+  echo "    $progname -i nnpkg-tcs Add_000   => run nnpkg-tcs/Add_000 and check output"
+  exit 1
+}
+
+if [ $# -eq 0 ]; then
+  echo "For help, type $progname -h"
+  exit 1
+fi
+
+while getopts "hdi:" OPTION; do
+case "${OPTION}" in
+    h) usage;;
+    d) delete_dumped_on_failure=1;;
+    i) indir=$OPTARG;;
+    ?) exit 1;;
+esac
+done
+
+shift $((OPTIND-1))
+
+if [ $# -ne 1 ]; then
+  echo "error: wrong argument (no argument or too many arguments)."
+  echo "For help, type $progname -h"
+  exit 1
+fi
+
+if [ ! -e Product ]; then
+  echo "error: please make sure to run this script in nnfw home."
+  exit 1
+fi
+
+tcname=$(basename "$1")
+nnpkg="$indir/$tcname"
+
+# run
+
+if [ ! -e $nnpkg ]; then
+  echo "error: nnpackage "$nnpkg" does not exist."
+  exit 1
+fi
+
+if ! command_exists $nnpkg_run; then
+  echo "error: runner "$nnpkg_run" does not exist."
+  exit 1
+fi
+
+dumped="$outdir/$tcname".out.h5
+
+LD_LIBRARY_PATH=Product/out/lib $nnpkg_run \
+--nnpackage "$nnpkg" \
+--load "$nnpkg/metadata/tc/input.h5" \
+--dump "$dumped" >& /dev/null
+
+# diff
+
+if ! command_exists $difftool; then
+  echo "error: difftool "$difftool" does not exist."
+  exit 1
+fi
+
+expected="$nnpkg/metadata/tc/expected.h5"
+
+if $difftool -d 0.001 -v "$dumped" "$expected" /value >& $dumped.log; then
+  echo "Pass: $tcname"
+  rm "$dumped" "$dumped.log"
+else
+  echo "Fail: $tcname"
+  [ $delete_dumped_on_failure ] && rm "$dumped"
+  cat "$dumped.log"
+  rm "$dumped.log"
+  exit 2
+fi