from .common import * from .helper import prepare_tmp_folder, prepare_result_folder from .img_preprocess import img2txt_bin from .load_config import ModelConfig from .convert_simulator_int_to_float import convert_sim_int_to_float import os import subprocess import shutil def run_csim_520(input_imgs: Dict, config: ModelConfig, result_folder='c_sim', cmd_bin_file=RESULT_FOLDER + '/compiler/command.bin', weight_bin_file=RESULT_FOLDER + '/compiler/weight.bin', setup_bin_file=RESULT_FOLDER + '/compiler/setup.bin') -> str: output_path = prepare_result_folder(result_folder) prepare_tmp_folder() # Prepare input if len(input_imgs.keys()) > 1: logging.error("Current hardware simulator do not support more than one input.") input_file_name = list(input_imgs.values())[0] input_shape = list(config.input_shapes.values())[0] img2txt_bin(input_file_name, False, RESULT_FOLDER + '/c_sim', input_shape[2], input_shape[3], config.preprocess_config["img_channel"], config.preprocess_config["img_preprocess_method"], 'img2bin', 520, config.preprocess_config["radix"], x_pos=config.preprocess_config["p_crop"]["crop_x"], y_pos=config.preprocess_config["p_crop"]["crop_y"], crop_h=config.preprocess_config["p_crop"]["crop_h"], crop_w=config.preprocess_config["p_crop"]["crop_w"], keep_aspect_ratio=config.preprocess_config["keep_aspect_ratio"], enable_crop=config.enable_crop, bitwidth=8) input_bin_path = RESULT_FOLDER + '/c_sim/' + os.path.basename(input_file_name).split('.')[0] + ".bin" # Run simulator logging.info('Running hardware simulator...') os.chdir(LIBS_FOLDER + '/c_sim_520') subprocess.run(['./npu_sim', '-j', '1', cmd_bin_file, weight_bin_file, input_bin_path, setup_bin_file], check=True) for filename in os.listdir(LIBS_FOLDER + '/c_sim_520'): if filename.startswith('node'): shutil.copy2(LIBS_FOLDER + '/c_sim_520/' + filename, output_path) convert_sim_int_to_float(LIBS_FOLDER + '/c_sim_520/radix_scale_info.txt', output_path) return output_path