109 lines
3.8 KiB
Python
109 lines
3.8 KiB
Python
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 construct_csim_720_config(
|
|
input_file: str, input_size_file: str,
|
|
command_bin=RESULT_FOLDER + '/compiler/command.bin',
|
|
weight_bin=RESULT_FOLDER + '/compiler/weight.bin',
|
|
setup_bin=RESULT_FOLDER + '/compiler/setup.bin'
|
|
):
|
|
return """
|
|
input_location = 1
|
|
file_command = {cmd_bin}
|
|
file_weight = {w_bin}
|
|
file_input = {ifile}
|
|
file_csv = {isize}
|
|
file_setup = {s_bin}
|
|
file_golden = filenotexist.txt
|
|
run_mode = 0
|
|
multithreads = 0
|
|
dump_sync_cmd_begin = 0 # inclusive
|
|
dump_sync_cmd_end = 0 # exclusive
|
|
dump_cload = 0
|
|
dump_cstore = 0
|
|
dump_conv_cropping = 0
|
|
dump_conv_padding = 0
|
|
dump_conv_mac = 0
|
|
dump_conv_coarse_shift = 0
|
|
dump_conv_spad = 0
|
|
dump_conv_load_psum = 0
|
|
dump_conv_psum = 0
|
|
dump_conv_bias = 0
|
|
dump_conv_fine_shift = 0
|
|
dump_pconv_preproc = 0
|
|
dump_pconv_trim = 0
|
|
dump_pconv_gap = 0
|
|
dump_pconv_relu = 0
|
|
dump_pconv_bias = 0
|
|
dump_pload = 0
|
|
dump_pstore = 0
|
|
dump_pfunc_cropping = 0
|
|
dump_pfunc_padding = 0
|
|
dump_pfunc_pooling = 0
|
|
dump_pfunc_fcon = 0
|
|
dump_pfunc_pat = 0
|
|
dump_pfunc_w2fm = 0
|
|
dump_rdma = 0
|
|
dump_wdma = 0
|
|
dump_getw = 0
|
|
dump_getw_decompress = 0
|
|
dump_getw_decompact = 0
|
|
dump_getw_pdec = 0
|
|
dump_registers = 0
|
|
dump_dram = 0
|
|
dump_nmem = 0
|
|
""".format(ifile=input_file, isize=input_size_file,
|
|
cmd_bin=command_bin, w_bin=weight_bin, s_bin=setup_bin)
|
|
|
|
def run_csim_720(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, output_path,
|
|
input_shape[2], input_shape[3], config.preprocess_config["img_channel"],
|
|
config.preprocess_config["img_preprocess_method"], 'img2bin',
|
|
720, 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 = output_path + '/' + os.path.basename(input_file_name).split('.')[0] + ".bin"
|
|
with open(TMP_FOLDER + '/simulator_input_size.csv', 'wb') as f:
|
|
f.write("{ch}\n{h}\n{w}\n".format(
|
|
ch=input_shape[1],
|
|
h=input_shape[2],
|
|
w=input_shape[3]
|
|
).encode(encoding='UTF-8'))
|
|
config_string=construct_csim_720_config(input_bin_path, TMP_FOLDER + '/simulator_input_size.csv',
|
|
cmd_bin_file, weight_bin_file, setup_bin_file)
|
|
with open(TMP_FOLDER + '/simulator_input.ini', 'wb') as f:
|
|
f.write(config_string.encode(encoding='UTF-8'))
|
|
|
|
|
|
# Run simulator
|
|
logging.info('Running hardware simulator...')
|
|
os.chdir(output_path)
|
|
subprocess.run([LIBS_FOLDER + '/c_sim_720/npu_csim',
|
|
TMP_FOLDER + '/simulator_input.ini'], check=True)
|
|
return output_path
|