[dredd-rule-lib] Introducing dredd-rule-lib (#8833)
author윤현식/On-Device Lab(SR)/Principal Engineer/삼성전자 <hyunsik.yoon@samsung.com>
Fri, 8 Nov 2019 04:12:31 +0000 (13:12 +0900)
committer박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Fri, 8 Nov 2019 04:12:31 +0000 (13:12 +0900)
* [dredd-rule-lib] Introducing dredd-rule-lib

This introduces dredd-rule-lib, which defines rule-lib.sh used by tf2tflite/circle-dredd-pb/pbtxt-test.

Signed-off-by: Hyun Sik Yoon <hyunsik.yoon@samsung.com>
* renaming target

compiler/dredd-rule-lib/CMakeLists.txt [new file with mode: 0644]
compiler/dredd-rule-lib/README.md [new file with mode: 0644]
compiler/dredd-rule-lib/rule-lib.sh [new file with mode: 0755]

diff --git a/compiler/dredd-rule-lib/CMakeLists.txt b/compiler/dredd-rule-lib/CMakeLists.txt
new file mode 100644 (file)
index 0000000..b39d862
--- /dev/null
@@ -0,0 +1,21 @@
+#
+# copy rule-lib.sh (a library of shell script functions)
+#
+set(SOURCE_RULE_LIB "${CMAKE_CURRENT_SOURCE_DIR}/rule-lib.sh")
+set(TARGET_RULE_LIB "${CMAKE_CURRENT_BINARY_DIR}/rule-lib.sh")
+
+add_custom_command(
+  OUTPUT ${TARGET_RULE_LIB}
+  COMMAND ${CMAKE_COMMAND} -E copy "${SOURCE_RULE_LIB}" "${TARGET_RULE_LIB}"
+  DEPENDS ${SOURCE_RULE_LIB}
+  COMMENT "Generate rule lib"
+)
+
+# Generate dependencies
+add_custom_target(dredd_rule_lib ALL DEPENDS ${TARGET_RULE_LIB})
+
+# How to get the path of rule-lib.sh in other CMakeLists.txt
+#
+# get_target_property(DREDD_RULE_LIB_DIR
+#                     dredd_rule_lib BINARY_DIR)
+# set(RULE_LIB_PATH "${DREDD_RULE_LIB_DIR}/rule-lib.sh")
diff --git a/compiler/dredd-rule-lib/README.md b/compiler/dredd-rule-lib/README.md
new file mode 100644 (file)
index 0000000..6f68e5f
--- /dev/null
@@ -0,0 +1,8 @@
+# dredd-rule-lib
+This defines `rule-lib.sh` used by the following:
+- tf2tflite-dredd-pb-test
+- tf2tflite-dredd-pbtxt-test
+
+TODO support the following:
+- tf2circle-dredd-pb-test
+- tf2circle-dredd-pbtxt-test
diff --git a/compiler/dredd-rule-lib/rule-lib.sh b/compiler/dredd-rule-lib/rule-lib.sh
new file mode 100755 (executable)
index 0000000..967e2fd
--- /dev/null
@@ -0,0 +1,120 @@
+#!/bin/bash
+
+ERROR_FLAG="Error"
+
+#
+# Define rule
+#
+#   - Params: rule name (metric), actual value, condition, expected value
+#     - condition is '=', '!=', '<', '>', '<=', '>='. Refer to "man expr"
+#   - Return
+#     - 0 : success
+#     - 1 : fail (condition check fail)
+#     - 10 : error while getting actual value
+#     - 11 : error while running expected value
+#     - 20 : wrong param count
+#
+
+RULE()
+{
+  if [ "$#" -ne 4 ];then
+    echo "** [Error] count of RULE parameter should be 4"
+    return 20
+  fi
+
+  RULE_NAME=$1
+  ACTUAL=$2
+  COND=$3
+  EXPECTED=$4
+
+  # not to exit when expr result with 0
+  set +e
+
+  # check if ACTUAL or EXPECTED has "ERROR" string
+  expr match "${ACTUAL}" ${ERROR_FLAG}
+  if [ $? = 0 ]; then
+    echo "** Error in ACTUAL"
+    return 10
+  fi
+
+  expr match "${EXPECTED}" ${ERROR_FLAG}
+  if [ $? = 0 ]; then
+    echo "** Error in EXPECTED"
+    return 11
+  fi
+
+  expr ${ACTUAL} ${COND} ${EXPECTED}
+  RESULT=$?
+
+  # roll-back
+  set -e
+
+  # Note: return value of 'expr'
+  # - 0 : result is true
+  # - 1 : result is false
+  # - 2 : error
+
+  if [ ${RESULT} -eq 0 ];then
+    echo -e "\n** [${RULE_NAME}] \t success \t ([actual: ${ACTUAL}] ${COND} [expected: ${EXPECTED}])\n"
+  elif [ ${RESULT} -eq 1 ];then
+    echo -e "\n** [${RULE_NAME}] \t ** fail \t ([actual: ${ACTUAL}] ${COND} [expected: ${EXPECTED}])\n"
+  else
+    echo -e "\t** Error in [expr ${ACTUAL} ${COND} ${EXPECTED}]"
+  fi
+
+  return ${RESULT}
+}
+
+#
+# Define each function to get quality value
+#
+
+# Note: These function is called by a sub-shell.
+# So return value should be passed through "echo return_value"
+# tip: for debugging, surround the code with "set -x" and "set +x"
+
+file_size()
+{
+  if [ ! -f ${TFLITE_PATH} ]; then
+    echo "${ERROR_FLAG} - ${TFLITE_PATH} does not exist"
+  else
+    echo `cat ${TFLITE_PATH} | wc -c`
+  fi
+}
+
+all_op_count()
+{
+  if [ ! -f ${TFLITE_PATH} ]; then
+    echo "${ERROR_FLAG} - ${TFLITE_PATH} does not exist"
+  elif [ ! -f ${TFL_INSPECT_PATH} ]; then
+    echo "${ERROR_FLAG} - ${TFL_INSPECT_PATH} does not exist"
+  else
+    echo `${TFL_INSPECT_PATH} --operators ${TFLITE_PATH} | wc -l`
+  fi
+}
+
+op_count()
+{
+  if [ "$#" -ne 1 ];then
+    echo "${ERROR_FLAG} - Wrong argument number"
+  elif [ ! -f ${TFLITE_PATH} ]; then
+    echo "${ERROR_FLAG} - ${TFLITE_PATH} does not exist"
+  elif [ ! -f ${TFL_INSPECT_PATH} ]; then
+    echo "${ERROR_FLAG} - ${TFL_INSPECT_PATH} does not exist"
+  else
+    echo `${TFL_INSPECT_PATH} --operators ${TFLITE_PATH} | grep -w "$1" | wc -l`
+  fi
+}
+
+conv2d_weight_not_constant()
+{
+  if [ ! -f ${TFLITE_PATH} ]; then
+    echo "${ERROR_FLAG} - ${TFLITE_PATH} does not exist"
+  elif [ ! -f ${TFL_INSPECT_PATH} ]; then
+    echo "${ERROR_FLAG} - ${TFL_INSPECT_PATH} does not exist"
+  else
+    echo `${TFL_INSPECT_PATH} --conv2d_weight ${TFLITE_PATH} | awk -F, '{ if ($2 != "CONST") print $0}' | wc -l`
+  fi
+}
+
+# TODO define more qullity test function