[tf2tflite-dredd-pbtxt-test] introducing rule-lib.sh (#8761)
author윤현식/On-Device Lab(SR)/Principal Engineer/삼성전자 <hyunsik.yoon@samsung.com>
Tue, 5 Nov 2019 08:01:56 +0000 (17:01 +0900)
committer박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Tue, 5 Nov 2019 08:01:56 +0000 (17:01 +0900)
* [tf2tflite-dredd-pbtxt-test] introducing rule-lib.sh

rule-lib.sh is a library of shell script functions that are used by dredd rule.

Signed-off-by: Hyun Sik Yoon <hyunsik.yoon@samsung.com>
* Update compiler/tf2tflite-dredd-pbtxt-test/rule-lib.sh

Co-Authored-By: hyunsik-yoon <hyunsik.yoon@samsung.com>
compiler/tf2tflite-dredd-pbtxt-test/rule-lib.sh

index 9fa4bf9..967e2fd 100755 (executable)
@@ -1 +1,120 @@
-# TODO write this
+#!/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