ONE - On-device Neural Engine
Loading...
Searching...
No Matches
conftest.py
Go to the documentation of this file.
1import re
2import os
3import shutil
4
5
6def extract_test_args(s):
7 p = re.compile('eval\\((.*)\\)')
8 result = p.search(s)
9 return result.group(1)
10
11
12def pytest_addoption(parser):
13 parser.addoption("--test_list", action="store", help="Path to test list")
14 parser.addoption("--artifacts", action="store", help="Path to test artifacts")
15 parser.addoption("--tflrecipe", action="store", help="Path to tfl recipies")
16 parser.addoption("--circlerecipe", action="store", help="Path to circle recipies")
17 parser.addoption("--binary", action="store", help="Path to test binary")
18 parser.addoption("--luci_eval_driver",
19 action="store",
20 help="Path to luci eval driver")
21
22
23def copy_if_changed(src_filepath, dst_filepath):
24 do_copy = False
25 if (os.path.isfile(dst_filepath)):
26 file_diff = os.stat(src_filepath).st_mtime - os.stat(dst_filepath).st_mtime
27 if file_diff > 1:
28 print("file:" + src_filepath + " changed, update")
29 do_copy = True
30 else:
31 do_copy = True
32
33 if do_copy:
34 print("file:" + src_filepath + " copy to: " + dst_filepath)
35 shutil.copyfile(src_filepath, dst_filepath)
36
37
38# prepare reference input/output files to build folder for luci-eval-driver
39# from ref data in res/TensorFlowLiteRecipes/*/ref.input* and ref.output*
40# as model_name.ref.input* and model_name.ref.output*
41def copy_ref_files(ref_file_src, ref_file_dst):
42 num_data = 0
43 while True:
44 input_file_src = ref_file_src + str(num_data)
45 if (not os.path.isfile(input_file_src)):
46 break
47 input_file_dst = ref_file_dst + str(num_data)
48 copy_if_changed(input_file_src, input_file_dst)
49 # try next file
50 num_data = num_data + 1
51
52
53# copy circle mode from common-artifacts to build binary
54def copy_circle_model(model_src, model_dst):
55 copy_if_changed(model_src, model_dst)
56
57
58def prepare_materials(test_name, tflrecipe_path, circlerecipe_path, binary_path,
59 artifacts_path):
60 # tfl? or circle?
61 recipe_path = tflrecipe_path
62 # check with 'test.recipe' file as 'ref.input?' can be absent for no input model
63 test_recipe = os.path.join(recipe_path, test_name, 'test.recipe')
64 if (not os.path.isfile(test_recipe)):
65 recipe_path = circlerecipe_path
66
67 ref_input_src = os.path.join(recipe_path, test_name, 'ref.input')
68 ref_input_dst = os.path.join(binary_path, test_name + '.ref.input')
69 copy_ref_files(ref_input_src, ref_input_dst)
70
71 ref_input_src = os.path.join(recipe_path, test_name, 'ref.output')
72 ref_input_dst = os.path.join(binary_path, test_name + '.ref.output')
73 copy_ref_files(ref_input_src, ref_input_dst)
74
75 cirle_model_src = os.path.join(artifacts_path, test_name + '.circle')
76 cicle_model_dst = os.path.join(binary_path, test_name + '.circle')
77 copy_circle_model(cirle_model_src, cicle_model_dst)
78
79
80def pytest_generate_tests(metafunc):
81 list_path = metafunc.config.getoption('test_list')
82 artifacts_path = metafunc.config.getoption('artifacts')
83 tflrecipe_path = metafunc.config.getoption('tflrecipe')
84 circlerecipe_path = metafunc.config.getoption('circlerecipe')
85 binary_path = metafunc.config.getoption('binary')
86 eval_driver_path = metafunc.config.getoption('luci_eval_driver')
87 if list_path is None:
88 tests_default_tol = []
89 tests_with_tol = []
90 else:
91 with open(list_path) as f:
92 contents = [line.rstrip() for line in f]
93
94 comment_removed = [line for line in contents if not line.startswith('#')]
95 newline_removed = [line for line in comment_removed if line.startswith('eval(')]
96 test_args = [extract_test_args(line) for line in newline_removed]
97 # eval(TEST_NAME)
98 tests_default_tol = [(arg, binary_path, eval_driver_path) for arg in test_args
99 if len(arg.split()) == 1]
100 # eval(TEST_NAME RTOL ATOL)
101 tests_with_tol = [(arg.split()[0], binary_path, eval_driver_path, arg.split()[1],
102 arg.split()[2]) for arg in test_args if len(arg.split()) == 3]
103
104 # copy circle file to binary
105 for test_item in tests_default_tol:
106 prepare_materials(test_item[0], tflrecipe_path, circlerecipe_path,
107 binary_path, artifacts_path)
108
109 for test_item in tests_with_tol:
110 prepare_materials(test_item[0], tflrecipe_path, circlerecipe_path,
111 binary_path, artifacts_path)
112
113 if 'default_test_name' in metafunc.fixturenames:
114 metafunc.parametrize('default_test_name,binary_path,eval_driver_path',
115 tests_default_tol)
116
117 if 'tol_test_name' in metafunc.fixturenames:
118 metafunc.parametrize('tol_test_name,binary_path,eval_driver_path,rtolf32,atolf32',
119 tests_with_tol)
copy_if_changed(src_filepath, dst_filepath)
Definition conftest.py:23
pytest_generate_tests(metafunc)
Definition conftest.py:18
copy_circle_model(model_src, model_dst)
Definition conftest.py:54
copy_ref_files(ref_file_src, ref_file_dst)
Definition conftest.py:41
prepare_materials(test_name, tflrecipe_path, circlerecipe_path, binary_path, artifacts_path)
Definition conftest.py:59
extract_test_args(s)
Definition conftest.py:4