def gen_inout_bin_res(bindir, bmpdt, net, pool_num, downsample, name_list, data_list): file_list = [] alphalist = [] intxt_list = [] outtxt_list = [] from script.run_steps import get_model_name name = get_model_name(net) layer_max, opear_max, block_max = getresnet_max(net, name) def file_exist(name): if os.path.exists(f'{bmpdt}/{name}'): file_list.append(name) for i in range(opear_max): inname = f'conv{i}.input.6.txt' outname = f'quant{i}.output.6.txt' file_exist(inname) file_exist(outname) for layerindex in range(1, layer_max+1): for blockindex in range(int(block_max[layerindex-1].strip())): for opearindex in range(1, opear_max+1): inname = f'layer{layerindex}.{blockindex}.conv{opearindex}.input.6.txt' outname = f'layer{layerindex}.{blockindex}.quant{opearindex}.output.6.txt' file_exist(inname) file_exist(outname) if layerindex and blockindex == 0: inname = f'layer{layerindex}.0.downsample.0.input.6.txt' outname = f'layer{layerindex}.0.quant_shortcut.output.6.txt' file_exist(inname) file_exist(outname) avgout = "quant_avg.output.6.txt" fcin = "fc.input.6.txt" fcout = "quant_fc.output.6.txt" if os.path.exists(f'{bmpdt}/{avgout}'): file_list.append(f'{avgout}') if os.path.exists(f'{bmpdt}/{fcin}'): file_list.append(f'{fcin}') if os.path.exists(f'{bmpdt}/{fcout}'): file_list.append(f'{fcout}') if not os.path.exists(bindir): print(f'run os.mkdir({bindir} error)') return -1 for i in range(len(name_list)): name = name_list[i] if ".alpha" in name: alphalist.append(data_list[i].tolist()) for i in range(len(file_list)): if avgout == file_list[i]: avgin = file_list[i-1] if "quant_avg" in file_list[i]: break if "input" in file_list[i]: intxt_list.append(file_list[i]) if "output" in file_list[i+1]: if "output" in file_list[i+2]: outname = file_list[i+2] else: outname = file_list[i+1] outtxt_list.append(outname) intxt_list.append(fcin) outtxt_list.append(fcout) j = 0 bit = 7 in_q = 5 for i in range(1, layer_cnts+1): if i == 1: C_in, H_in, W_in = get_fstin_CHW(net) elif i in downsample: C_in, H_in, W_in = get_out_CHW(i-2) else: C_in, H_in, W_in = get_out_CHW(i-1) C_out, H_out, W_out = get_out_CHW(i) if i in pool_num: out_q = in_q continue out_q = bit - math.ceil(math.log2(0.5 * alphalist[j])) input_txt = f'{bmpdt}/{intxt_list[j]}' output_txt = f'{bmpdt}/{outtxt_list[j]}' input_bin = f'{bindir}/layer{i}_input_act0_file.bin' output_bin = f'{bindir}/layer{i}_output_file.bin' write_in = threading.Thread(target=write_inout_bin, args=(input_txt, input_bin, H_in, W_in, C_in, in_q)) write_in.start() write_in.join() write_out = threading.Thread(target=write_inout_bin, args=(output_txt, output_bin, H_out, W_out, C_out, out_q)) write_out.start() write_out.join() j += 1 in_q = out_q
def gen_inout_bin_vgg(bindir, bmpdt, net, pool_num, downsample, name_list, data_list): fc_output = [] fc_inputs = [] alphalist = [] conv_inputs = [] conv_output = [] list = os.listdir(bmpdt) for i in range(len(list)): if "input" in list[i]: if "conv" in list[i]: conv_inputs.append(list[i]) elif "classifier" in list[i]: fc_inputs.append(list[i]) elif "output" in list[i]: if "quant." in list[i]: conv_output.append(list[i]) elif "quant_fc" in list[i]: fc_output.append(list[i]) fc_inputs = sorted(fc_inputs) fc_output = sorted(fc_output) conv_inputs.sort(key=lambda x: int(re.match('\D+(\d+)\.conv.input.6.txt', x).group(1))) conv_output.sort(key=lambda x: int(re.match('\D+(\d+)\.quant.output.6.txt', x).group(1))) for i in range(len(name_list)): name = name_list[i] if ".alpha" in name: alphalist.append(data_list[i].tolist()) j = 0 bit = 7 in_q = 5 for i in range(1, layer_cnts - len(fc_inputs) + 1): if i == 1: C_in, H_in, W_in = get_fstin_CHW(net) elif i in downsample: C_in, H_in, W_in = get_out_CHW(i-2) else: C_in, H_in, W_in = get_out_CHW(i-1) C_out, H_out, W_out = get_out_CHW(i) if i in pool_num: out_q = in_q continue out_q = bit - math.ceil(math.log2(0.5 * alphalist[j])) input_txt = f'{bmpdt}/{conv_inputs[j]}' output_txt = f'{bmpdt}/{conv_output[j]}' input_bin = f'{bindir}/layer{i}_input_act0_file.bin' output_bin = f'{bindir}/layer{i}_output_file.bin' write_in = threading.Thread(target=write_inout_bin, args=(input_txt, input_bin, H_in, W_in, C_in, in_q)) write_in.start() write_in.join() write_out = threading.Thread(target=write_inout_bin, args=(output_txt, output_bin, H_out, W_out, C_out, out_q)) write_out.start() write_out.join() j += 1 in_q = out_q if layer_cnts <= 1: # Adapt one layer toolchains return for i in range(len(fc_inputs)): layernum = i + len(conv_inputs) + len(pool_num) + 1 C_in, H_in, W_in = get_out_CHW(layernum-1) C_out, H_out, W_out = get_out_CHW(layernum) out_q = bit - math.ceil(math.log2(0.5 * alphalist[j + len(fc_inputs)])) input_txt = f'{bmpdt}/{fc_inputs[i]}' output_txt = f'{bmpdt}/{fc_output[i]}' input_bin = f'{bindir}/layer{len(conv_inputs) + i + len(pool_num)}_input_act0_file.bin' output_bin = f'{bindir}/layer{len(conv_inputs) + i + len(pool_num)}_output_file.bin' write_in = threading.Thread(target=write_inout_bin, args=(input_txt, input_bin, H_in, W_in, C_in, in_q)) write_in.start() write_in.join() write_out = threading.Thread(target=write_inout_bin, args=(output_txt, output_bin, H_out, W_out, C_out, in_q)) write_out.start() write_out.join() in_q = out_q
def get_nnbaton(num_type, netpth, logpth, actbit, wetbit): flag = 1 nnbaton = [] layermax = 0 dec_index = 0 # res_branch index sizes = get_fstin_CHW(netpth) net = logpth.rsplit('/', 1)[1].split('.')[0] if 'vggnet' not in net and 'resnet' not in net \ and "simulator" not in net: print(f'Muse-v3 toolchains not recognized {net}') exit(0) if 'vggnet' in net or net == 'simulator': flag = 0 elif 'resnet' in net: resnet = get_single_conv(netpth) layermax = find_layer_quant("layer", netpth, net)[0] if net == 'resnet18': flag = 1 resdec = ['a', 'b'] # csv:res2 & csv:res5 = block cnt bradec = ['2a', '2b'] # csv:branch description == layer cnt for rescnt in range(1, int(layermax) + 1): # csv:res2~res5 for j in range(len(resdec)): if resdec[j] == 'a' and rescnt > 1: bradec = ['2a', '2b', '1'] else: bradec = ['2a', '2b'] for k in range(len(bradec)): layer_type = f'res{rescnt + 1}{resdec[j]}_branch{bradec[k]}' resnet.append(layer_type) elif net == 'resnet34': flag = 2 resdec = ['a', 'b', 'c'] # csv:res2 & csv:res5 = block cnt bradec = ['2a', '2b'] # csv:branch description == layer cnt for rescnt in range(1, int(layermax) + 1): # csv:res2~res5 if rescnt == 2: # csv:res3 resdec = ['a', 'b', 'c', 'd'] elif rescnt == 3: # csv:res4 resdec = ['a', 'b', 'c', 'd', 'e', 'f'] elif rescnt == 4: # csv:res5 resdec = ['a', 'b', 'c'] for j in range(len(resdec)): if resdec[j] == 'a' and rescnt > 1: bradec = ['2a', '2b', '1'] else: bradec = ['2a', '2b'] for k in range(len(bradec)): layer_type = f'res{rescnt + 1}{resdec[j]}_branch{bradec[k]}' resnet.append(layer_type) elif net == 'resnet50': flag = 3 resdec = ['a', 'b', 'c'] # csv:res2 & csv:res5 = block cnt bradec = ['1', '2a', '2b', '2c'] # csv:branch description == layer cnt for rescnt in range(1, int(layermax) + 1): # csv:res2~res5 if rescnt == 2: # csv:res3 resdec = ['a', 'b', 'c', 'd'] elif rescnt == 3: # csv:res4 resdec = ['a', 'b', 'c', 'd', 'e', 'f'] elif rescnt == 4: # csv:res5 resdec = ['a', 'b', 'c'] for j in range(len(resdec)): if resdec[j] == 'a' and rescnt == 1: bradec = ['2a', '2b', '2c', '1'] elif resdec[j] == 'a' and rescnt > 1: bradec = ['1', '2a', '2b', '2c'] else: bradec = ['2a', '2b', '2c'] for k in range(len(bradec)): layer_type = f'res{rescnt + 1}{resdec[j]}_branch{bradec[k]}' resnet.append(layer_type) else: print(f'Muse-v3 toolchains unknown model {net}') exit(0) worklad = f'nnbaton/workload/{net}-{sizes[1]}.csv' batonlg = f'../.debug/nnbaton_{net}-{sizes[1]}.log' csvname = f'nnbaton/csv/sort/reuslt_{net}-{sizes[1]}.csv' csvpath = ['sort', 'raw4/output', 'cooked4', 'workload'] data = [ 'cin', 'in_size', 'cout', 'out_size', 'kernel_size', 'stride', 'note' ] run_list = [ f'python3 main.py {net} {sizes[1]} {actbit} {wetbit} >{batonlg} 2>&1', f'python3 view.py >> {batonlg} 2>&1' ] for i in range(len(csvpath)): csvdir = f'nnbaton/csv/{csvpath[i]}' if 'workload' == csvpath[i]: csvdir = f'nnbaton/workload' if os.path.exists(csvdir): shutil.rmtree(csvdir) os.makedirs(csvdir) with open(worklad, 'w', newline='') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=data) writer.writeheader() for i in range(1, len(num_type)): layercnt = int(num_type[i][0]) layerdec = num_type[i][1].split(':')[1] if flag == 0: layerdec = layerdec.split(' ')[0] if "conv" in layerdec: if layercnt == 1: CHW_in = sizes elif "downsample" in layerdec: CHW_in = get_out_CHW(i - 2) else: CHW_in = get_out_CHW(i - 1) CHW_out = get_out_CHW(i) cout = CHW_out[0] stride = num_type[i][4] out_size = num_type[i][3] ker_size = num_type[i][2] cin = align(CHW_in[0], 8) in_size = out_size * stride + ker_size - stride if flag and 'conv' in layerdec: layerdec = resnet[dec_index] dec_index += 1 data = [ cin, in_size, cout, out_size, ker_size, stride, layerdec ] writer = csv.DictWriter(csvfile, fieldnames=data) writer.writeheader() os.chdir("nnbaton") for i in range(len(run_list)): os.system(run_list[i]) os.chdir("..") X1_index = Y1_index = K1_index = 0 X2_index = Y2_index = K2_index = 0 Kc_index = Yc_index = Xc_index = 0 Kp_index = Yp_index = 0 C1_index = C0_index = 0 X0_index = Y0_index = 0 op_index = divindex = 0 with open(csvname, 'r') as my_csv: reader = csv.reader(my_csv) for row in reader: for i in range(len(row)): if row[i] == "note": op_index = i elif row[i] == "X1": X1_index = i elif row[i] == "Y1": Y1_index = i elif row[i] == "K1": K1_index = i elif row[i] == "X2": X2_index = i elif row[i] == "Y2": Y2_index = i elif row[i] == "K2": K2_index = i elif row[i] == "Kp": Kp_index = i elif row[i] == "Yp": Yp_index = i elif row[i] == "Xc": Xc_index = i elif row[i] == "Yc": Yc_index = i elif row[i] == "Kc": Kc_index = i elif row[i] == "C1": C1_index = i elif row[i] == "C0": C0_index = i elif row[i] == "X0": X0_index = i elif row[i] == "Y0": Y0_index = i elif row[i] == "reorder_case": divindex = i break for i in range(1, len(num_type)): layer_type = num_type[i][1].split(':')[1].split(' ', 1)[0] with open(csvname, 'r') as my_csv: reader = csv.reader(my_csv) for row in reader: if row[op_index] == layer_type: nnbaton.append([ int(row[X1_index]), int(row[Y1_index]), int(row[K1_index]), int(row[X2_index]), int(row[Y2_index]), int(row[K2_index]), int(row[Kp_index]), int(row[Yp_index]), int(row[Kc_index]), int(row[Yc_index]), int(row[Xc_index]), int(row[C1_index]), int(row[C0_index]), int(row[X0_index]), int(row[Y0_index]), int(row[divindex]) ]) if flag == 1: for rescnt in range(1, int(layermax) + 1): # csv:res2~res5 for j in range(len(resdec)): if resdec[j] == 'a' and rescnt > 1: bradec = ['2a', '2b', '1'] else: bradec = ['2a', '2b'] for k in range(len(bradec)): layer_type = f'res{rescnt + 1}{resdec[j]}_branch{bradec[k]}' with open(csvname, 'r') as my_csv: reader = csv.reader(my_csv) for row in reader: if row[op_index] == layer_type: nnbaton.append([ int(row[X1_index]), int(row[Y1_index]), int(row[K1_index]), int(row[X2_index]), int(row[Y2_index]), int(row[K2_index]), int(row[Kp_index]), int(row[Yp_index]), int(row[Kc_index]), int(row[Yc_index]), int(row[Xc_index]), int(row[C1_index]), int(row[C0_index]), int(row[X0_index]), int(row[Y0_index]), int(row[divindex]) ]) elif flag == 2: for rescnt in range(1, int(layermax) + 1): # csv:res2~res5 if rescnt == 2: # csv:res3 resdec = ['a', 'b', 'c', 'd'] elif rescnt == 3: # csv:res4 resdec = ['a', 'b', 'c', 'd', 'e', 'f'] elif rescnt == 4: # csv:res5 resdec = ['a', 'b', 'c'] for j in range(len(resdec)): if resdec[j] == 'a' and rescnt > 1: bradec = ['2a', '2b', '1'] else: bradec = ['2a', '2b'] for k in range(len(bradec)): layer_type = f'res{rescnt + 1}{resdec[j]}_branch{bradec[k]}' with open(csvname, 'r') as my_csv: reader = csv.reader(my_csv) for row in reader: if row[op_index] == layer_type: nnbaton.append([ int(row[X1_index]), int(row[Y1_index]), int(row[K1_index]), int(row[X2_index]), int(row[Y2_index]), int(row[K2_index]), int(row[Kp_index]), int(row[Yp_index]), int(row[Kc_index]), int(row[Yc_index]), int(row[Xc_index]), int(row[C1_index]), int(row[C0_index]), int(row[X0_index]), int(row[Y0_index]), int(row[divindex]) ]) elif flag == 3: for rescnt in range(1, int(layermax) + 1): # csv:res2~res5 if rescnt == 2: # csv:res3 resdec = ['a', 'b', 'c', 'd'] elif rescnt == 3: # csv:res4 resdec = ['a', 'b', 'c', 'd', 'e', 'f'] elif rescnt == 4: # csv:res5 resdec = ['a', 'b', 'c'] for j in range(len(resdec)): if resdec[j] == 'a' and rescnt == 1: bradec = ["2a", "2b", "2c", '1'] elif resdec[j] == 'a' and rescnt > 1: bradec = ['1', "2a", "2b", "2c"] else: bradec = ["2a", "2b", "2c"] for k in range(len(bradec)): layer_type = f'res{rescnt + 1}{resdec[j]}_branch{bradec[k]}' with open(csvname, 'r') as my_csv: reader = csv.reader(my_csv) for row in reader: if row[op_index] == layer_type: nnbaton.append([ int(row[X1_index]), int(row[Y1_index]), int(row[K1_index]), int(row[X2_index]), int(row[Y2_index]), int(row[K2_index]), int(row[Kp_index]), int(row[Yp_index]), int(row[Kc_index]), int(row[Yc_index]), int(row[Xc_index]), int(row[C1_index]), int(row[C0_index]), int(row[X0_index]), int(row[Y0_index]), int(row[divindex]) ]) for i in range(1, len(num_type)): layer_type = num_type[i][1].split(':')[1].split(' ', 1)[0] if "conv" not in layer_type: nnbaton.insert(i - 1, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0]) return nnbaton