From 08e11411a8c4a6c021e7b0bbb4f0fe0dbb5eb4ee Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=9C=A4=ED=98=84=EC=8B=9D/On-Device=20Lab=28SR=29/Princip?= =?utf8?q?al=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Mon, 11 Nov 2019 16:15:18 +0900 Subject: [PATCH] [tf2circle-dredd-pb-test] Introducing tf2circle-dredd-pb-test (#8883) * [tf2circle-dredd-pb-test] Introducing tf2circle-dredd-pb-test tf2circle-dredd-pb-test is introduced, which is a test for circle files generated from pb files in ./contrib dir. Note that files in the PR is copied files from tf2tflite-dredd-pb-test, after replacing string "tflite" and "tfl" to "circle". Signed-off-by: Hyun Sik Yoon * remove nncc_find_resource(TensorFlowTests) --- compiler/tf2circle-dredd-pb-test/.gitignore | 1 + compiler/tf2circle-dredd-pb-test/CMakeLists.txt | 141 +++++++++++++++++++++ compiler/tf2circle-dredd-pb-test/README.md | 3 + .../tf2circle-dredd-pb-test/contrib/.gitignore | 3 + compiler/tf2circle-dredd-pb-test/requires.cmake | 4 + compiler/tf2circle-dredd-pb-test/runner.sh | 110 ++++++++++++++++ 6 files changed, 262 insertions(+) create mode 100644 compiler/tf2circle-dredd-pb-test/.gitignore create mode 100644 compiler/tf2circle-dredd-pb-test/CMakeLists.txt create mode 100644 compiler/tf2circle-dredd-pb-test/README.md create mode 100644 compiler/tf2circle-dredd-pb-test/contrib/.gitignore create mode 100644 compiler/tf2circle-dredd-pb-test/requires.cmake create mode 100755 compiler/tf2circle-dredd-pb-test/runner.sh diff --git a/compiler/tf2circle-dredd-pb-test/.gitignore b/compiler/tf2circle-dredd-pb-test/.gitignore new file mode 100644 index 0000000..23c7c1b --- /dev/null +++ b/compiler/tf2circle-dredd-pb-test/.gitignore @@ -0,0 +1 @@ +/contrib.lst diff --git a/compiler/tf2circle-dredd-pb-test/CMakeLists.txt b/compiler/tf2circle-dredd-pb-test/CMakeLists.txt new file mode 100644 index 0000000..cdd653c --- /dev/null +++ b/compiler/tf2circle-dredd-pb-test/CMakeLists.txt @@ -0,0 +1,141 @@ +nnas_include(TargetRequire) + +unset(REQUIRED_TARGETS) +list(APPEND REQUIRED_TARGETS circle-inspect) +list(APPEND REQUIRED_TARGETS circle-verify) +list(APPEND REQUIRED_TARGETS tf2circle) +list(APPEND REQUIRED_TARGETS dredd_rule_lib) +TargetRequire_Return(${REQUIRED_TARGETS}) + +set(PB_MODEL_REPO "${CMAKE_CURRENT_SOURCE_DIR}/contrib") # Where to find models to test + +unset(KEYS) +unset(DEPS) + +function(check_file_exist) + + foreach(FILE_PATH IN LISTS ARGV) + if(NOT EXISTS "${FILE_PATH}") + message(FATAL_ERROR "${FILE_PATH} does not exist." ) + endif() + endforeach() + +endfunction() + +# +# processing models in contrib.lst +# +# Example) +# +# Add(Inception_v3 RULE test.rule) +# -> Read compiler/tf2circle-dredd-pb-test/contrib/Inception_v3/test.pb and generate +# "Inception_v3.circle". Then rule file is tested for the generated circle file. +# +macro(Add MODEL_DIR) + + set(ARG_OPTION) + set(ARG_ONE_VALUE RULE) # rule file name + set(ARG_MULTI_VALUE) + cmake_parse_arguments(ARG "${ARG_OPTION}" "${ARG_ONE_VALUE}" "${ARG_MULTI_VALUE}" ${ARGN}) + + if(NOT ARG_RULE ) + message( FATAL_ERROR "RULE is mandadatory arg" ) + endif() + + set(RULE_FILENAME ${ARG_RULE}) + + set(MODEL_SOURCE_DIR "${PB_MODEL_REPO}/${MODEL_DIR}") + + set(PB_PATH "${MODEL_SOURCE_DIR}/test.pb") + set(INFO_PATH "${MODEL_SOURCE_DIR}/test.info") + set(RULE_PATH "${MODEL_SOURCE_DIR}/${RULE_FILENAME}") + + check_file_exist(${PB_PATH} ${INFO_PATH} ${RULE_PATH}) + + # Generate .test file which declares path of target pb, info, rule files + set(TARGET_TESTNAME "${MODEL_DIR}") + set(TEST_CONFIG_FILE "${CMAKE_CURRENT_BINARY_DIR}/${TARGET_TESTNAME}.test") + + add_custom_command( + OUTPUT ${TEST_CONFIG_FILE} + COMMAND ${CMAKE_COMMAND} -E remove -f ${TEST_CONFIG_FILE} + COMMAND ${CMAKE_COMMAND} -E echo 'MODEL_PB_PATH="${PB_PATH}"' >> ${TEST_CONFIG_FILE} + COMMAND ${CMAKE_COMMAND} -E echo 'MODEL_INFO_PATH="${INFO_PATH}"' >> ${TEST_CONFIG_FILE} + COMMAND ${CMAKE_COMMAND} -E echo 'MODEL_RULE_PATH="${RULE_PATH}"' >> ${TEST_CONFIG_FILE} + DEPENDS + ${PB_PATH} + COMMENT "Generate ${TARGET_TESTNAME} configuration for BIN" + ) + + list(APPEND KEYS "${TARGET_TESTNAME}") + list(APPEND DEPS "${TEST_CONFIG_FILE}") + +endmacro(Add) + +include(contrib.lst OPTIONAL) + +# +# Generate toolchain.config +# +set(TOOLCHAIN_CONFIG "${CMAKE_CURRENT_BINARY_DIR}/toolchain.config") + +add_custom_command( + OUTPUT ${TOOLCHAIN_CONFIG} + COMMAND ${CMAKE_COMMAND} -E remove -f ${TOOLCHAIN_CONFIG} + COMMAND ${CMAKE_COMMAND} -E echo 'CIRCLE_INSPECT_PATH=\"$\"' >> ${TOOLCHAIN_CONFIG} + COMMAND ${CMAKE_COMMAND} -E echo 'CIRCLE_VERIFY_PATH=\"$\"' >> ${TOOLCHAIN_CONFIG} + COMMAND ${CMAKE_COMMAND} -E echo 'TF2CIRCLE_PATH=\"$\"' >> ${TOOLCHAIN_CONFIG} + # add more if new excutable file is needed in runner.sh and rule-lib.sh + DEPENDS + circle-inspect + circle-verify + tf2circle + COMMENT "Generate toolchin configuration" +) + +list(APPEND DEPS "${TOOLCHAIN_CONFIG}") + +# +# Generate quality test runner +# +set(SOURCE_RUNNER "${CMAKE_CURRENT_SOURCE_DIR}/runner.sh") +set(TARGET_RUNNER "${CMAKE_CURRENT_BINARY_DIR}/runner.sh") + +add_custom_command( + OUTPUT ${TARGET_RUNNER} + COMMAND ${CMAKE_COMMAND} -E copy "${SOURCE_RUNNER}" "${TARGET_RUNNER}" + DEPENDS ${SOURCE_RUNNER} + COMMENT "Generate test runner" +) + +list(APPEND DEPS "${TARGET_RUNNER}") + +# +# copy rule-lib.sh (a library of shell script functions) +# + +# getting path for rule-lib.sh in dredd-rule-lib +get_target_property(DREDD_RULE_LIB_DIR dredd_rule_lib BINARY_DIR) + +set(SOURCE_RULE_LIB "${DREDD_RULE_LIB_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" +) + +list(APPEND DEPS "${TARGET_RULE_LIB}") + +# Generate dependencies +add_custom_target(tf2circle_dredd_pb_deps ALL DEPENDS ${DEPS}) + +add_test( + NAME tf2circle_dredd_pb_test + COMMAND + "${TARGET_RUNNER}" + "${TOOLCHAIN_CONFIG}" + ${KEYS} +) diff --git a/compiler/tf2circle-dredd-pb-test/README.md b/compiler/tf2circle-dredd-pb-test/README.md new file mode 100644 index 0000000..65b4cd1 --- /dev/null +++ b/compiler/tf2circle-dredd-pb-test/README.md @@ -0,0 +1,3 @@ +# tf2circle-dredd-pb-test + +TODO write content diff --git a/compiler/tf2circle-dredd-pb-test/contrib/.gitignore b/compiler/tf2circle-dredd-pb-test/contrib/.gitignore new file mode 100644 index 0000000..968c345 --- /dev/null +++ b/compiler/tf2circle-dredd-pb-test/contrib/.gitignore @@ -0,0 +1,3 @@ +/* +# Exclude all except below +!.gitignore diff --git a/compiler/tf2circle-dredd-pb-test/requires.cmake b/compiler/tf2circle-dredd-pb-test/requires.cmake new file mode 100644 index 0000000..0fb6cde --- /dev/null +++ b/compiler/tf2circle-dredd-pb-test/requires.cmake @@ -0,0 +1,4 @@ +require("tf2circle") +require("circle-inspect") +require("circle-verify") +require("dredd-rule-lib") diff --git a/compiler/tf2circle-dredd-pb-test/runner.sh b/compiler/tf2circle-dredd-pb-test/runner.sh new file mode 100755 index 0000000..5745db1 --- /dev/null +++ b/compiler/tf2circle-dredd-pb-test/runner.sh @@ -0,0 +1,110 @@ +#!/bin/bash + +# This script checks circle file generated by tf2circle + +WORKDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) + +# Need at least toolchain.config +if [[ $# -lt 1 ]]; then + echo "USAGE: $0 ..." + echo + echo "ARGUMENTS:" + echo " [toolchain.config path]" + echo " [Prefix1]" + echo " [Prefix2]" + echo " ..." + exit 255 +fi + +CONFIG_PATH="$1"; shift + +source "${CONFIG_PATH}" + +echo "-- Found circle-inspect: ${CIRCLE_INSPECT_PATH}" +echo "-- Found circle-verify: ${CIRCLE_VERIFY_PATH}" +echo "-- Found tf2circle: ${TF2CIRCLE_PATH}" +echo "-- Found workdir: ${WORKDIR}" + +TESTED=() +PASSED=() +FAILED=() + +pushd "${WORKDIR}" + +# running each rule file + +while [[ $# -ne 0 ]]; do + PREFIX="$1"; shift + + echo "[ RUN ] ${PREFIX}" + + TESTED+=("${PREFIX}") + + PASSED_TAG="${PREFIX}.passed" + + rm -f "${PASSED_TAG}" + + cat > "${PREFIX}.log" <( + exec 2>&1 + + source "${PREFIX}.test" + + echo "-- Use '${MODEL_PB_PATH}', '${MODEL_INFO_PATH}', and '${MODEL_RULE_PATH}'" + + # Exit immediately if any command fails + set -e + # Show commands + set -x + + # Generate circle + "${TF2CIRCLE_PATH}" \ + "${MODEL_INFO_PATH}" \ + "${MODEL_PB_PATH}" \ + "${WORKDIR}/${PREFIX}.circle" + + # + # Run rule prepared to check circle file + # + + # set vars needed by rule file + CIRCLE_PATH="${WORKDIR}/${PREFIX}.circle" + + # Note: turn off 'command printing'. Otherwise printing will be so messy + set +x + + # set vars required by rule-lib.sh and rule file + COMPILED_FILE=${CIRCLE_PATH} + INSPECT_PROG_PATH=${CIRCLE_INSPECT_PATH} + VERIFY_PROG_PATH=${CIRCLE_VERIFY_PATH} + + source rule-lib.sh + source "${MODEL_RULE_PATH}" + + set -x + + if [[ $? -eq 0 ]]; then + touch "${PASSED_TAG}" + fi + ) + + if [[ -f "${PASSED_TAG}" ]]; then + echo "[ OK ] ${PREFIX}" + PASSED+=("$PREFIX") + else + echo "[ FAIL] ${PREFIX}" + FAILED+=("$PREFIX") + fi +done +popd + +if [[ ${#TESTED[@]} -ne ${#PASSED[@]} ]]; then + echo "FAILED" + for TEST in "${FAILED[@]}" + do + echo "- ${TEST}" + done + exit 255 +fi + +echo "PASSED" +exit 0 -- 2.7.4