Skip to content

Commit 3da4247

Browse files
seanshparkshs-park
andauthored
[circle-mlir/tools-test] Introduce initial rewrite-test (#14828)
This will introduce initial circle-rewrite-test to check rewriting circle graph works expected. ONE-DCO-1.0-Signed-off-by: SaeHie Park <[email protected]> Co-authored-by: Seungho Henry Park <[email protected]>
1 parent b0d0df9 commit 3da4247

File tree

6 files changed

+178
-0
lines changed

6 files changed

+178
-0
lines changed

circle-mlir/circle-mlir/tools-test/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ add_subdirectory(gen-onnx)
66
add_subdirectory(onnx2circle-models)
77
# value test of source onnx vs circle
88
add_subdirectory(onnx2circle-value-test)
9+
# rewrite test of circle
10+
add_subdirectory(onnx2circle-rewrite-test)
911
# circle import/export test
1012
add_subdirectory(circle-impexp-test)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# TODO run when test is enabled
2+
3+
# python3 venv folder
4+
# NOTE Docker image for CI doesn't use venv
5+
set(VENV_PATH "${CMAKE_SOURCE_DIR}/infra/overlay/venv")
6+
7+
set(OPTION_O2C_SINGLE)
8+
string(TOLOWER "${CMAKE_BUILD_TYPE}" BUILD_TYPE_LOWER)
9+
if(BUILD_TYPE_LOWER STREQUAL "debug")
10+
set(OPTION_O2C_SINGLE "-o2c-single")
11+
endif()
12+
13+
# DEPEND FILES
14+
set(FILE_DEPS_VALCHK )
15+
16+
macro(COPY_SCRIPT FILENAME)
17+
set(SCRIPT_SRC "${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME}")
18+
set(SCRIPT_DST "${CMAKE_CURRENT_BINARY_DIR}/${FILENAME}")
19+
add_custom_command(
20+
OUTPUT ${SCRIPT_DST}
21+
COMMAND ${CMAKE_COMMAND} -E copy "${SCRIPT_SRC}" "${SCRIPT_DST}"
22+
DEPENDS ${SCRIPT_SRC}
23+
COMMENT "tools/onnx2circle: prepare ${FILENAME}"
24+
)
25+
list(APPEND FILE_DEPS_VALCHK "${SCRIPT_DST}")
26+
endmacro(COPY_SCRIPT)
27+
28+
# copy test scripts
29+
COPY_SCRIPT(run_circle_ops_test.sh)
30+
COPY_SCRIPT(check_circle_ops.py)
31+
32+
# AddModel used in test.lst
33+
set(UNIT_TEST_MODELS )
34+
35+
get_target_property(ONNX_ARTIFACTS_BIN_PATH gen_onnx_target BINARY_DIR)
36+
37+
macro(AddModel MODEL_NAME)
38+
# copy ONNX to build folder
39+
set(TEST_ONNX_MODEL_SRC "${ONNX_ARTIFACTS_BIN_PATH}/${MODEL_NAME}.onnx")
40+
set(TEST_ONNX_MODEL_DST "${CMAKE_CURRENT_BINARY_DIR}/${MODEL_NAME}.onnx")
41+
add_custom_command(
42+
OUTPUT ${TEST_ONNX_MODEL_DST}
43+
COMMAND ${CMAKE_COMMAND} -E copy "${TEST_ONNX_MODEL_SRC}" "${TEST_ONNX_MODEL_DST}"
44+
DEPENDS ${TEST_ONNX_MODEL_SRC}
45+
COMMENT "onnx2circle-rewrite-test: prepare ${MODEL_NAME}.onnx"
46+
)
47+
# generate .ops files containing Operators
48+
set(TEST_CIRCLE_MODEL_DST "${CMAKE_CURRENT_BINARY_DIR}/${MODEL_NAME}.circle")
49+
add_custom_command(
50+
OUTPUT ${TEST_CIRCLE_MODEL_DST}.ops
51+
COMMAND "$<TARGET_FILE:onnx2circle>" --save_ops "${OPTION_O2C_SINGLE}"
52+
"${TEST_ONNX_MODEL_DST}" "${TEST_CIRCLE_MODEL_DST}"
53+
DEPENDS ${TEST_ONNX_MODEL_DST} onnx2circle
54+
COMMENT "onnx2circle-rewrite-test: generate ${MODEL_NAME}.circle.ops"
55+
)
56+
57+
list(APPEND UNIT_TEST_MODELS "${MODEL_NAME}")
58+
list(APPEND FILE_DEPS_VALCHK "${TEST_CIRCLE_MODEL_DST}.ops")
59+
endmacro(AddModel)
60+
61+
# Read "test.lst"
62+
include("test.lst")
63+
64+
add_custom_target(
65+
onnx2circle_rewrite_test_target ALL
66+
DEPENDS onnx2circle_models_target ${FILE_DEPS_VALCHK}
67+
)
68+
69+
set(MODELS_ROOT "${CMAKE_SOURCE_DIR}/models/net")
70+
set(RUN_PATH "${CMAKE_CURRENT_BINARY_DIR}")
71+
72+
foreach(MODEL IN ITEMS ${UNIT_TEST_MODELS})
73+
add_test(
74+
NAME onnx2circle_circle_ops_${MODEL}
75+
COMMAND bash run_circle_ops_test.sh ${VENV_PATH} ${RUN_PATH} ${MODELS_ROOT} ${MODEL}
76+
)
77+
endforeach()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## onnx2circle-rewrite-test
2+
3+
This test validates whether the "RewriteCirclePass" works as expected.
4+
5+
The test checks:
6+
- Expected operators exist in the output.
7+
- Non-expected operators do not exist.
8+
9+
Procedure are like follows.
10+
- load models used only from 'models/net' folder
11+
- models are loaded from 'test.lst' file
12+
- onnx2circle is executed with `--save_ops` option
13+
- `.circle.ops` text file should be generated for each model
14+
- Circle-MLIR operator type names are listed in the text file
15+
- for each model `__init__.py` file, `check_circle_operators()` should exist
16+
- if operators expection is OK, `check_circle_operators()` should return 0
17+
- if not, `check_circle_operators()` should return non zero value
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import importlib
2+
import sys
3+
4+
from pathlib import Path
5+
6+
7+
# Check operators with final Circle MLIR
8+
def check_model(models_root, model_name, ops_path):
9+
sys.path.append(models_root)
10+
module = importlib.import_module(model_name)
11+
12+
m_keys = module.__dict__.keys()
13+
if 'check_circle_operators' in m_keys:
14+
try:
15+
opsDict = dict()
16+
with open(ops_path, 'rt') as file:
17+
for line in file:
18+
op = line.strip()
19+
if op in opsDict:
20+
opsDict[op] = opsDict[op] + 1
21+
else:
22+
opsDict[op] = 1
23+
24+
operators = list(opsDict.keys())
25+
print("ops dict:", opsDict)
26+
print("operators:", operators)
27+
return module.check_circle_operators(opsDict, operators)
28+
except:
29+
return 1
30+
return 1
31+
32+
33+
if __name__ == "__main__":
34+
if len(sys.argv) != 4:
35+
thispath = Path(sys.argv[0])
36+
sys.exit("Usage: " + thispath.name + " [models_root] [model_name] [ops_path]")
37+
38+
result = check_model(sys.argv[1], sys.argv[2], sys.argv[3])
39+
sys.exit(result)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
3+
# HOW TO USE
4+
#
5+
# ./run_circle_ops_test.sh <path/to/venv_dir> <run_path> <models_path> <model_name>
6+
# venv_dir : python virtual environment home directory
7+
# run_path : current path where model.ops file exist
8+
# models_path : models source path
9+
# model_name : model name
10+
11+
set -e
12+
13+
TEST_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
14+
15+
VENV_PATH="$1"; shift
16+
RUN_PATH="$1"; shift
17+
MODELS_PATH="$1"; shift
18+
MODEL_NAME="$1"; shift
19+
20+
CHECK_CIRCLE_OPS_SCRIPT=${TEST_PATH}/check_circle_ops.py
21+
CIRCLE_OPS_FILE="${RUN_PATH}/${MODEL_NAME}.circle.ops"
22+
23+
# Execute ONNX, Circle and compare
24+
echo "======================================================================"
25+
if [[ -f ${VENV_PATH}/bin/activate ]]; then
26+
echo "Enter VENV ${VENV_PATH}"
27+
source ${VENV_PATH}/bin/activate
28+
fi
29+
30+
# run check_rewrite script
31+
echo "Run ${CHECK_CIRCLE_OPS_SCRIPT} ${MODELS_PATH} ${MODEL_NAME} ${CIRCLE_OPS_FILE}"
32+
python3 ${CHECK_CIRCLE_OPS_SCRIPT} ${MODELS_PATH} ${MODEL_NAME} ${CIRCLE_OPS_FILE}
33+
COMP_RESULT=$?
34+
35+
if [[ -f ${VENV_PATH}/bin/activate ]]; then
36+
deactivate
37+
fi
38+
39+
exit ${COMP_RESULT}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## EXAMPLE
2+
#
3+
# AddModel(test_model)
4+
#

0 commit comments

Comments
 (0)