def copy_bin_to_hbm(self, data: bytes) -> ctypes.c_void_p: """ Copy bin data to hbm Parameters ---------- data: bytes binary data Returns ------- hbm buffer pointer """ if not isinstance(data, bytes): raise TypeError( "Copy binary to hbm supports bytes only, reveviced %s" % str(type(data))) try: c_memory_p = self.malloc(int(math.ceil(len(data) / 32) * 32 + 32), "RT_MEMORY_HBM") except BaseException as e: logger.log_err("rtMalloc on HBM failed, HBM memory info: %s" % str(self.get_memory_info_ex("RT_MEMORYINFO_HBM"))) raise self.memcpy(c_memory_p, int(math.ceil(len(data) / 32) * 32 + 32), data, len(data), "RT_MEMCPY_HOST_TO_DEVICE") return c_memory_p
def _run_ut_case_file(run_arg: RunUTCaseFileArgs): logger.log_info("start run: %s" % run_arg.case_file) res = True if run_arg.cov_report: cov_src = get_cov_relate_source(run_arg.op_module_name) ut_cover = coverage.Coverage(source=cov_src, data_file=run_arg.cov_data_path) ut_cover.start() try: if sys.modules.get(run_arg.op_module_name): print("[INFO]reload module for coverage ,moule name:", sys.modules.get(run_arg.op_module_name)) importlib.reload(sys.modules.get(run_arg.op_module_name)) case_dir = os.path.dirname(os.path.realpath(run_arg.case_file)) case_module_name = os.path.basename(os.path.realpath( run_arg.case_file))[:-3] sys.path.insert(0, case_dir) __import__(case_module_name) case_module = sys.modules[case_module_name] ut_case = getattr(case_module, "ut_case", None) case_usage_list = [ CaseUsage.IMPL, CaseUsage.CUSTOM, CaseUsage.CFG_COVERAGE_CHECK, CaseUsage.CHECK_SUPPORT, CaseUsage.SELECT_FORMAT, CaseUsage.PRECISION ] if not run_arg.simulator_mode: case_usage_list.remove(CaseUsage.PRECISION) case_runner = OpUTTestRunner( print_summary=False, simulator_mode=run_arg.simulator_mode, simulator_lib_path=run_arg.simulator_lib_path, simulator_dump_path=run_arg.dump_model_dir, data_dump_dir=run_arg.data_dir) if isinstance(run_arg.case_name, str): case_name_list = run_arg.case_name.split(",") else: case_name_list = run_arg.case_name ut_rpt = case_runner.run(run_arg.soc_version, ut_case, case_name_list, case_usage_list) ut_rpt.save(run_arg.test_report_data_path) del sys.modules[case_module_name] except BaseException as run_err: # 'pylint: disable=broad-except logger.log_err("Test Failed! case_file: %s, error_msg: %s" % (run_arg.case_file, run_err.args[0]), print_trace=True) res = False if run_arg.cov_report: ut_cover.stop() ut_cover.save() logger.log_info("end run: %s" % run_arg.case_file) return res
def combine_report(self, report_paths, strict=False, file_pattern=None): """ combine all report in report paths :param report_paths: report path :param strict: True is not found report will raise runtime exception :param file_pattern: report file pattern :return: None """ def _add_report(rpt: OpUTReport): for case_rpt in rpt.get_case_rpt_list(): self.add_case_report(case_rpt) if not isinstance(report_paths, (tuple, list)): report_paths = (report_paths, ) find_report_list = [] for report_path in report_paths: if not os.path.exists(report_path): logger.log_warn("combine_report report path not exist: %s" % report_path) if os.path.isfile(report_path): ut_report = OpUTReport() ut_report.load(report_path) print("load %s success, case cnt: %d" % (report_path, ut_report.total_cnt)) _add_report(ut_report) continue for path, dir_names, file_names in os.walk(report_path): print(path, dir_names, file_names) for file_name in file_names: if file_pattern and not fnmatch.fnmatch( file_name, file_pattern): continue report_file = os.path.join(path, file_name) find_report_list.append(report_file) report_file = os.path.join(path, file_name) ut_report = OpUTReport() ut_report.load(report_file) print("load %s success, case cnt: %d" % (report_file, ut_report.total_cnt)) _add_report(ut_report) if not find_report_list and strict: logger.log_err( "combine_report not found any report to combine in: [%s]" % ", ".join(report_paths)) raise RuntimeError( "combine_report not found any report to combine in: [%s]" % ", ".join(report_paths))
def get_case_name_from_file(params): """ get case name :param params: param contains case_file_path and soc_version :return: case_file: case_name_list dict """ case_file_path = params.get("case_file_path") soc_version = params.get("soc_version") try: case_dir = os.path.dirname(os.path.realpath(case_file_path)) case_module_name = os.path.basename( os.path.realpath(case_file_path))[:-3] sys.path.insert(0, case_dir) __import__(case_module_name) case_module = sys.modules[case_module_name] ut_case = getattr(case_module, "ut_case", None) case_name_list_inner = ut_case.get_all_test_case_name(soc_version) except BaseException as run_err: # 'pylint: disable=broad-except logger.log_err("Get case name failed, error msg: %s" % run_err.args[0]) return None return {case_file_path: case_name_list_inner}
def _check_args(case_dir, test_report, cov_report): if not case_dir: logger.log_err("Not set case dir") return False if test_report and test_report not in ("json", "console"): logger.log_err("'test_report' only support 'json/console'.") return False if cov_report and cov_report not in ("html", "json", "xml"): logger.log_err("'cov_report' only support 'html/json/xml'.") return False return True
def run_ut( case_dir, soc_version, case_name=None, # 'pylint: disable=too-many-arguments, too-many-locals test_report="json", test_report_path="./report", cov_report=None, cov_report_path="./cov_report", simulator_mode=None, simulator_lib_path=None, simulator_data_path="./model", test_data_path="./data", process_num=0): """ run ut test case :param case_dir: a test case dir or a test case file :param soc_version: like "Ascend910", "Ascend310" :param case_name: run case name, default is None, run all test case :param test_report: support console/json, report format type :param test_report_path: test report save path :param cov_report: support html/json/xml type, if None means not need coverage report :param cov_report_path: coverage report save path :param simulator_mode: simulator_mode can be None/pv/ca/tm/esl :param simulator_lib_path: simulator library path :param simulator_data_path: test data directory, input, output and expect output data :param test_data_path: when run ca or tm mode, dump data save in this dirctory :param process_num: when 0 means use cpu_count, else means process count :return: success or failed """ success = "success" failed = "failed" print("start run ops ut time: %s" % datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")) if not _check_args(case_dir, test_report, cov_report): return failed case_file_info_list, load_has_err = ut_loader.load_ut_cases(case_dir) if not case_file_info_list: logger.log_err("Not found any test cases.") return failed cov_combine_dir = _build_cov_data_path(cov_report_path) rpt_combine_dir = _build_report_data_path(test_report_path) def _build_multiprocess_run_args(): ps_count = 1 if not isinstance(soc_version, (tuple, list)): soc_version_list = str(soc_version).split(",") else: soc_version_list = soc_version total_run_arg_list = {} for one_soc in soc_version_list: total_run_arg_list[one_soc] = [] for case_file_info in case_file_info_list: case_file_tmp = os.path.basename(case_file_info.case_file)[:-3] for one_soc_version in soc_version_list: single_cov_data_path = os.path.join( cov_combine_dir, ".coverage_" + str(ps_count) + "_" + case_file_tmp) single_rpt_data_path = os.path.join( rpt_combine_dir, "rpt_" + str(ps_count) + "_" + case_file_tmp + ".data") run_arg = RunUTCaseFileArgs( case_file=case_file_info.case_file, op_module_name=case_file_info.op_module_name, soc_version=one_soc_version, case_name=case_name, test_report=test_report, test_report_data_path=single_rpt_data_path, cov_report=cov_report, cov_data_path=single_cov_data_path, simulator_mode=simulator_mode, simulator_lib_path=simulator_lib_path, data_dir=test_data_path, dump_model_dir=simulator_data_path) total_run_arg_list[one_soc_version].append(run_arg) ps_count += 1 return total_run_arg_list, ps_count multiprocess_run_args, total_count = _build_multiprocess_run_args() logger.log_info("multiprocess_run_args count: %d" % total_count) if process_num == 1: logger.log_info("process_num is 1, run cases one by one") results = [] for _, soc_args in multiprocess_run_args.items(): for soc_arg in soc_args: res = _run_ut_case_file(soc_arg) results.append(res) run_success = reduce(lambda x, y: x and y, results) else: if process_num == 0: cpu_count = max(multiprocessing.cpu_count() - 1, 1) logger.log_info("multiprocessing cpu count: %d" % cpu_count) else: cpu_count = process_num logger.log_info("process_num is %s" % process_num) if simulator_mode == "esl": cpu_count = 1 if total_count < cpu_count: total_args = [] for _, soc_args in multiprocess_run_args.items(): for soc_arg in soc_args: total_args.append(soc_arg) if len(total_args) == 1: res = _run_ut_case_file(total_args[0]) results = [ res, ] else: with Pool(processes=cpu_count) as pool: results = pool.map(_run_ut_case_file, total_args) run_success = reduce(lambda x, y: x and y, results) else: results = [] for _, soc_args in multiprocess_run_args.items(): with Pool(processes=cpu_count) as pool: one_soc_results = pool.map(_run_ut_case_file, soc_args) for result in one_soc_results: results.append(result) run_success = reduce(lambda x, y: x and y, results) test_report = ut_report.OpUTReport() test_report.combine_report(rpt_combine_dir) report_data_path = os.path.join(test_report_path, ".ut_test_report") test_report.save(report_data_path) if test_report: test_report.console_print() if cov_report and len(multiprocess_run_args) > 0: _combine_coverage(cov_report_path, cov_combine_dir) print("end run ops ut time: %s" % datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")) if load_has_err: logger.log_err( "Has error in case files, you can see error log by key word 'import case file failed'." ) run_result = success if run_success and not load_has_err else failed if test_report.err_cnt > 0 or test_report.failed_cnt > 0: run_result = failed return run_result