blob: e3926eab69dcf0615aee661b1e1c1607067abaa3 [file] [log] [blame]
Eric Kunzee5e26762020-10-13 16:11:07 -07001#!/usr/bin/env python3
2
3# Copyright (c) 2020, ARM Limited.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17
18import argparse
19import sys
20import re
21import os
22import subprocess
23import shlex
24import json
25import glob
26import math
27import queue
28import threading
29import traceback
30import importlib
31
32
33from enum import IntEnum, Enum, unique
34from datetime import datetime
35
Kevin Cheng550ccc52021-03-03 11:21:43 -080036# Include the ../scripts and ../scripts/xunit directory in PYTHONPATH
Eric Kunzee5e26762020-10-13 16:11:07 -070037parent_dir = os.path.dirname(os.path.realpath(__file__))
Kevin Cheng550ccc52021-03-03 11:21:43 -080038sys.path.append(os.path.join(parent_dir, "..", "scripts"))
39sys.path.append(os.path.join(parent_dir, "..", "scripts", "xunit"))
40
Eric Kunzee5e26762020-10-13 16:11:07 -070041import xunit
Kevin Cheng550ccc52021-03-03 11:21:43 -080042
43# Include the ../thirdparty/serialization_lib/python directory in PYTHONPATH
44parent_dir = os.path.dirname(os.path.realpath(__file__))
45sys.path.append(
46 os.path.join(parent_dir, "..", "thirdparty", "serialization_lib", "python")
47)
Eric Kunzee5e26762020-10-13 16:11:07 -070048import tosa
49from tosa_test_gen import TosaTestGen
50from tosa_test_runner import TosaTestRunner
51
52no_color_printing = False
Kevin Cheng550ccc52021-03-03 11:21:43 -080053# from run_tf_unit_test import LogColors, print_color, run_sh_command
54
Eric Kunzee5e26762020-10-13 16:11:07 -070055
56def parseArgs():
57
58 parser = argparse.ArgumentParser()
Kevin Cheng550ccc52021-03-03 11:21:43 -080059 parser.add_argument(
60 "-t", "--test", dest="test", type=str, nargs="+", help="Test(s) to run"
61 )
62 parser.add_argument(
63 "--seed",
64 dest="random_seed",
65 default=42,
66 type=int,
67 help="Random seed for test generation",
68 )
69 parser.add_argument(
70 "--ref-model-path",
71 dest="ref_model_path",
72 default="build/reference_model/tosa_reference_model",
73 type=str,
74 help="Path to reference model executable",
75 )
76 parser.add_argument(
77 "--ref-debug",
78 dest="ref_debug",
79 default="",
80 type=str,
81 help="Reference debug flag (low, med, high)",
82 )
83 parser.add_argument(
84 "--ref-intermediates",
85 dest="ref_intermediates",
86 default=0,
87 type=int,
88 help="Reference model dumps intermediate tensors",
89 )
90 parser.add_argument(
91 "-v", "--verbose", dest="verbose", action="count", help="Verbose operation"
92 )
93 parser.add_argument(
94 "-j", "--jobs", dest="jobs", type=int, default=1, help="Number of parallel jobs"
95 )
96 parser.add_argument(
97 "--sut-module",
98 "-s",
99 dest="sut_module",
100 type=str,
101 nargs="+",
102 default=["tosa_ref_run"],
103 help="System under test module to load (derives from TosaTestRunner). May be repeated",
104 )
105 parser.add_argument(
106 "--sut-module-args",
107 dest="sut_module_args",
108 type=str,
109 nargs="+",
110 default=[],
111 help="System under test module arguments. Use sutmodulename:argvalue to pass an argument. May be repeated.",
112 )
113 parser.add_argument(
114 "--xunit-file",
115 dest="xunit_file",
116 type=str,
117 default="result.xml",
118 help="XUnit output file",
119 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700120
121 args = parser.parse_args()
122
123 # Autodetect CPU count
124 if args.jobs <= 0:
125 args.jobs = os.cpu_count()
126
127 return args
128
Kevin Cheng550ccc52021-03-03 11:21:43 -0800129
Eric Kunzee5e26762020-10-13 16:11:07 -0700130def workerThread(task_queue, runnerList, args, result_queue):
131 while True:
132 try:
133 test = task_queue.get(block=False)
134 except queue.Empty:
135 break
136
137 if test is None:
138 break
139
Kevin Cheng550ccc52021-03-03 11:21:43 -0800140 msg = ""
Eric Kunzee5e26762020-10-13 16:11:07 -0700141 start_time = datetime.now()
142 try:
143
144 for runnerModule, runnerArgs in runnerList:
145 if args.verbose:
Kevin Cheng550ccc52021-03-03 11:21:43 -0800146 print(
147 "Running runner {} with test {}".format(
148 runnerModule.__name__, test
149 )
150 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700151 runner = runnerModule.TosaRefRunner(args, runnerArgs, test)
152 try:
153 rc = runner.runModel()
154 except Exception as e:
155 rc = TosaTestRunner.Result.INTERNAL_ERROR
156 except Exception as e:
Kevin Cheng550ccc52021-03-03 11:21:43 -0800157 print("Internal regression error: {}".format(e))
158 print(
159 "".join(
160 traceback.format_exception(
161 etype=type(e), value=e, tb=e.__traceback__
162 )
163 )
164 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700165 rc = TosaTestRunner.Result.INTERNAL_ERROR
166
167 end_time = datetime.now()
168
169 result_queue.put((test, rc, msg, end_time - start_time))
170 task_queue.task_done()
171
172 return True
173
Kevin Cheng550ccc52021-03-03 11:21:43 -0800174
Eric Kunzee5e26762020-10-13 16:11:07 -0700175def loadRefModules(args):
176 # Returns a tuple of (runner_module, [argument list])
177 runnerList = []
178 for r in args.sut_module:
179 if args.verbose:
Kevin Cheng550ccc52021-03-03 11:21:43 -0800180 print("Loading module {}".format(r))
Eric Kunzee5e26762020-10-13 16:11:07 -0700181
182 runner = importlib.import_module(r)
183
184 # Look for arguments associated with this runner
Kevin Cheng550ccc52021-03-03 11:21:43 -0800185 runnerArgPrefix = "{}:".format(r)
Eric Kunzee5e26762020-10-13 16:11:07 -0700186 runnerArgList = []
187 for a in args.sut_module_args:
188 if a.startswith(runnerArgPrefix):
Kevin Cheng550ccc52021-03-03 11:21:43 -0800189 runnerArgList.append(a[len(runnerArgPrefix) :])
Eric Kunzee5e26762020-10-13 16:11:07 -0700190 runnerList.append((runner, runnerArgList))
191
192 return runnerList
193
Kevin Cheng550ccc52021-03-03 11:21:43 -0800194
Eric Kunzee5e26762020-10-13 16:11:07 -0700195def main():
196 args = parseArgs()
197
198 runnerList = loadRefModules(args)
199
200 threads = []
201 taskQueue = queue.Queue()
202 resultQueue = queue.Queue()
203
204 for t in args.test:
205 taskQueue.put((t))
206
Kevin Cheng550ccc52021-03-03 11:21:43 -0800207 print("Running {} tests ".format(taskQueue.qsize()))
Eric Kunzee5e26762020-10-13 16:11:07 -0700208
209 for i in range(args.jobs):
Kevin Cheng550ccc52021-03-03 11:21:43 -0800210 t = threading.Thread(
211 target=workerThread, args=(taskQueue, runnerList, args, resultQueue)
212 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700213 t.setDaemon(True)
214 t.start()
215 threads.append(t)
216
217 taskQueue.join()
218
219 resultList = []
220 results = [0] * len(TosaTestRunner.Result)
221
222 while True:
223 try:
224 test, rc, msg, time_delta = resultQueue.get(block=False)
225 except queue.Empty:
226 break
227
228 resultList.append((test, rc, msg, time_delta))
229 results[rc] = results[rc] + 1
230
Kevin Cheng550ccc52021-03-03 11:21:43 -0800231 xunit_result = xunit.xunit_results("Regressions")
232 xunit_suite = xunit_result.create_suite("Unit tests")
Eric Kunzee5e26762020-10-13 16:11:07 -0700233
234 # Sort by test name
235 for test, rc, msg, time_delta in sorted(resultList, key=lambda tup: tup[0]):
236 test_name = test
Kevin Cheng550ccc52021-03-03 11:21:43 -0800237 xt = xunit.xunit_test(test_name, "reference")
Eric Kunzee5e26762020-10-13 16:11:07 -0700238
Kevin Cheng550ccc52021-03-03 11:21:43 -0800239 xt.time = str(
240 float(time_delta.seconds) + (float(time_delta.microseconds) * 1e-6)
241 )
Eric Kunzee5e26762020-10-13 16:11:07 -0700242
Kevin Cheng550ccc52021-03-03 11:21:43 -0800243 if (
244 rc == TosaTestRunner.Result.EXPECTED_PASS
245 or rc == TosaTestRunner.Result.EXPECTED_FAILURE
246 ):
Eric Kunzee5e26762020-10-13 16:11:07 -0700247 if args.verbose:
Kevin Cheng550ccc52021-03-03 11:21:43 -0800248 print("{} {}".format(rc.name, test_name))
Eric Kunzee5e26762020-10-13 16:11:07 -0700249 else:
250 xt.failed(msg)
Kevin Cheng550ccc52021-03-03 11:21:43 -0800251 print("{} {}".format(rc.name, test_name))
Eric Kunzee5e26762020-10-13 16:11:07 -0700252
253 xunit_suite.tests.append(xt)
254 resultQueue.task_done()
255
256 xunit_result.write_results(args.xunit_file)
257
Kevin Cheng550ccc52021-03-03 11:21:43 -0800258 print("Totals: ", end="")
Eric Kunzee5e26762020-10-13 16:11:07 -0700259 for result in TosaTestRunner.Result:
Kevin Cheng550ccc52021-03-03 11:21:43 -0800260 print("{} {}, ".format(results[result], result.name.lower()), end="")
Eric Kunzee5e26762020-10-13 16:11:07 -0700261 print()
262
263 return 0
264
Kevin Cheng550ccc52021-03-03 11:21:43 -0800265
266if __name__ == "__main__":
Eric Kunzee5e26762020-10-13 16:11:07 -0700267 exit(main())