Commit 08fb0edf by zhengzifu

Add FSM_tp_k_gp_0_0127 module and related templates; refactor output directory…

Add FSM_tp_k_gp_0_0127 module and related templates; refactor output directory handling in generation scripts
parent 26e7f36d
......@@ -536,10 +536,12 @@ def process_task(i, name, weights_file_name, matrix, H, L, VN, config: CFG):
def run(name: str, config: CFG):
shutil.rmtree(config.output_dir, ignore_errors=True)
os.makedirs(config.output_dir, exist_ok=True)
weights_file = os.path.join(config.weights_dir, config.run_weights)
weights_file_name = os.path.splitext(os.path.basename(weights_file))[0]
file_dir = os.path.join(config.output_dir, name, weights_file_name)
if os.path.exists(file_dir):
shutil.rmtree(file_dir, ignore_errors=True)
os.makedirs(file_dir, exist_ok=True)
print(f"Processing {weights_file}")
with open(weights_file, "rb") as f:
matrixs = pickle.load(f)
......@@ -558,5 +560,4 @@ def run(name: str, config: CFG):
result = future.result()
except Exception as e:
print(f"Generating {result} failed with an error: {e}")
file_dir = os.path.join(config.output_dir, name, weights_file_name)
print("Files generated in", file_dir)
......@@ -35,23 +35,24 @@ def process_weight(args):
def run(name: str, config: CFG):
shutil.rmtree(config.output_dir, ignore_errors=True)
os.makedirs(config.output_dir, exist_ok=True)
weights_file = os.path.join(config.mapped_weights_dir, config.run_weights)
weights_file_name = os.path.splitext(os.path.basename(weights_file))[0]
print(f"Processing {weights_file_name}")
file_dir = os.path.join(config.output_dir, name, weights_file_name)
if os.path.exists(file_dir):
shutil.rmtree(file_dir, ignore_errors=True)
os.makedirs(file_dir, exist_ok=True)
print(f"Processing {weights_file}")
with open(weights_file, "rb") as f:
matrixs = pickle.load(f)
matrixs = np.transpose(matrixs, (1, 0, 2))
VN, L, H = matrixs.shape
# print(VN, L, H)
path_dir = os.path.join(config.output_dir, name, weights_file_name)
print("Generating color graph")
args = [
(i, weight, L, H, config.value_range, weights_file_name, path_dir)
(i, weight, L, H, config.value_range, weights_file_name, file_dir)
for i, weight in enumerate(matrixs)
]
with Pool(config.num_workers) as pool:
list(tqdm(pool.imap(process_weight, args), total=VN))
print("Generating color graph at", path_dir)
print("Generating color graph at", file_dir)
......@@ -107,10 +107,12 @@ def process_task(i, name, weights_file_name, matrix, H, L, config: CFG):
def run(name: str, config: CFG):
shutil.rmtree(config.output_dir, ignore_errors=True)
os.makedirs(config.output_dir, exist_ok=True)
weights_file = os.path.join(config.weights_dir, config.run_weights)
weights_file_name = os.path.splitext(os.path.basename(weights_file))[0]
file_dir = os.path.join(config.output_dir, name, weights_file_name)
if os.path.exists(file_dir):
shutil.rmtree(file_dir, ignore_errors=True)
os.makedirs(file_dir, exist_ok=True)
print(f"Processing {weights_file}")
with open(weights_file, "rb") as f:
matrixs = pickle.load(f)
......@@ -128,5 +130,4 @@ def run(name: str, config: CFG):
result = future.result()
except Exception as e:
print(f"Generating {result} failed with an error: {e}")
file_dir = os.path.join(config.output_dir, name, weights_file_name)
print("Files generated in", file_dir)
......@@ -147,10 +147,12 @@ def process_task(i, name, weights_file_name, ww_list, H, L, VN, config: CFG):
def run(name: str, config: CFG):
shutil.rmtree(config.output_dir, ignore_errors=True)
os.makedirs(config.output_dir, exist_ok=True)
weights_file = os.path.join(config.weights_dir, config.run_weights)
weights_file_name = os.path.splitext(os.path.basename(weights_file))[0]
file_dir = os.path.join(config.output_dir, name, weights_file_name)
if os.path.exists(file_dir):
shutil.rmtree(file_dir, ignore_errors=True)
os.makedirs(file_dir, exist_ok=True)
print(f"Processing {weights_file}")
with open(weights_file, "rb") as f:
matrixs = pickle.load(f)
......@@ -185,5 +187,4 @@ def run(name: str, config: CFG):
result = future.result()
except Exception as e:
print(f"Generating {result} failed with an error: {e}")
file_dir = os.path.join(config.output_dir, name, weights_file_name)
print("Files generated in", file_dir)
......@@ -120,10 +120,12 @@ def process_task(i, name, weights_file_name, matrix, H, L, config: CFG):
def run(name: str, config: CFG):
shutil.rmtree(config.output_dir, ignore_errors=True)
os.makedirs(config.output_dir, exist_ok=True)
weights_file = os.path.join(config.mapped_weights_dir, config.run_weights)
weights_file_name = os.path.splitext(os.path.basename(weights_file))[0]
file_dir = os.path.join(config.output_dir, name, weights_file_name)
if os.path.exists(file_dir):
shutil.rmtree(file_dir, ignore_errors=True)
os.makedirs(file_dir, exist_ok=True)
print(f"Processing {weights_file_name}")
with open(weights_file, "rb") as f:
matrixs = pickle.load(f)
......@@ -141,7 +143,6 @@ def run(name: str, config: CFG):
result = future.result()
except Exception as e:
print(f"Generating {result} failed with an error: {e}")
file_dir = os.path.join(config.output_dir, name, weights_file_name)
print(f"Generated {name} at {file_dir}")
......
......@@ -106,10 +106,12 @@ def process_task(i, name, weights_file_name, H, L, config: CFG):
def run(name: str, config: CFG):
shutil.rmtree(config.output_dir, ignore_errors=True)
os.makedirs(config.output_dir, exist_ok=True)
weights_file = os.path.join(config.mapped_weights_dir, config.run_weights)
weights_file_name = os.path.splitext(os.path.basename(weights_file))[0]
file_dir = os.path.join(config.output_dir, name, weights_file_name)
if os.path.exists(file_dir):
shutil.rmtree(file_dir, ignore_errors=True)
os.makedirs(file_dir, exist_ok=True)
print(f"Processing {weights_file}")
with open(weights_file, "rb") as f:
matrixs = pickle.load(f)
......@@ -125,5 +127,4 @@ def run(name: str, config: CFG):
result = future.result()
except Exception as e:
print(f"Generating {result} failed with an error: {e}")
file_dir = os.path.join(config.output_dir, name, weights_file_name)
print(f"Generated {name} at {file_dir}")
# %%
import shutil
import sys
import numpy as np
from pyrilog import (
......@@ -125,6 +126,10 @@ def process_task(i, name, weights_file_name, matrix, H, L, config: CFG):
def run(name, config: CFG):
weights_file = os.path.join(config.mapped_weights_dir, config.run_weights)
weights_file_name = os.path.splitext(os.path.basename(weights_file))[0]
file_dir = os.path.join(config.output_dir, name, weights_file_name)
if os.path.exists(file_dir):
shutil.rmtree(file_dir, ignore_errors=True)
os.makedirs(file_dir, exist_ok=True)
print(f"Processing {weights_file_name}")
with open(weights_file, "rb") as f:
matrixs = pickle.load(f)
......@@ -142,5 +147,4 @@ def run(name, config: CFG):
result = future.result()
except Exception as e:
print(f"Generating {result} failed with an error: {e}")
file_dir = os.path.join(config.output_dir, name, weights_file_name)
print(f"Generated {name} at {file_dir}")
# %%
import shutil
import sys
import numpy as np
from pyrilog import (
......@@ -60,8 +61,8 @@ def generate_module(
"LM_sel": "LM_sel",
}
for j in range(len(value_range)):
sw_ports[f"WT_{j}_out_S"] = f"WT_{j}_out_S[{i*GN+GN-1}:{i*GN}]"
sw_ports[f"WT_{j}_out_C"] = f"WT_{j}_out_C[{i*GN+GN-1}:{i*GN}]"
sw_ports[f"WT_{j}_out_S"] = f"WT_{j}_out_S[{i * GN + GN - 1}:{i * GN}]"
sw_ports[f"WT_{j}_out_C"] = f"WT_{j}_out_C[{i * GN + GN - 1}:{i * GN}]"
add_instance(
f"Mid_wrapper_tp_{weights_file_name}_gp_{i}",
f"Mid_wrapper_{i}",
......@@ -99,6 +100,10 @@ def process_task(i, name, weights_file_name, H, L, VN, config: CFG):
def run(name: str, config: CFG):
weights_file = os.path.join(config.weights_dir, config.run_weights)
weights_file_name = os.path.splitext(os.path.basename(weights_file))[0]
file_dir = os.path.join(config.output_dir, name, weights_file_name)
if os.path.exists(file_dir):
shutil.rmtree(file_dir, ignore_errors=True)
os.makedirs(file_dir, exist_ok=True)
print(f"Processing {weights_file}")
with open(weights_file, "rb") as f:
matrixs = pickle.load(f)
......@@ -123,5 +128,4 @@ def run(name: str, config: CFG):
result = future.result()
except Exception as e:
print(f"Generating {result} failed with an error: {e}")
file_dir = os.path.join(config.output_dir, name, weights_file_name)
print("Files generated in", file_dir)
# %%
import shutil
import sys
import numpy as np
......@@ -106,9 +107,12 @@ def process_task(i, name, weights_file_name, H, L, config: CFG):
def run(name: str, config: CFG):
os.makedirs(config.output_dir, exist_ok=True)
weights_file = os.path.join(config.mapped_weights_dir, config.run_weights)
weights_file_name = os.path.splitext(os.path.basename(weights_file))[0]
file_dir = os.path.join(config.output_dir, name, weights_file_name)
if os.path.exists(file_dir):
shutil.rmtree(file_dir, ignore_errors=True)
os.makedirs(file_dir, exist_ok=True)
print(f"Processing {weights_file_name}")
with open(weights_file, "rb") as f:
print(f"Loading {weights_file_name}")
......@@ -125,7 +129,6 @@ def run(name: str, config: CFG):
result = future.result()
except Exception as e:
print(f"Generating {result} failed with an error: {e}")
file_dir = os.path.join(config.output_dir, name, weights_file_name)
print(f"Generated {name} at {file_dir}")
......
......@@ -539,10 +539,9 @@ def run(name: str, config: CFG):
weights_file = os.path.join(config.weights_dir, config.run_weights)
weights_file_name = os.path.splitext(os.path.basename(weights_file))[0]
output_dir = os.path.join(config.output_dir, name, weights_file_name)
shutil.rmtree(output_dir, ignore_errors=True)
if os.path.exists(output_dir):
shutil.rmtree(output_dir, ignore_errors=True)
os.makedirs(output_dir, exist_ok=True)
print(f"Processing {weights_file}")
with open(weights_file, "rb") as f:
matrixs = pickle.load(f)
......
......@@ -86,10 +86,12 @@ def process_task(i, name, weights_file_name, matrix, H, L, config: CFG):
def run(name: str, config: CFG):
shutil.rmtree(config.output_dir, ignore_errors=True)
os.makedirs(config.output_dir, exist_ok=True)
weights_file = os.path.join(config.weights_dir, config.run_weights)
weights_file_name = os.path.splitext(os.path.basename(weights_file))[0]
file_dir = os.path.join(config.output_dir, name, weights_file_name)
if os.path.exists(file_dir):
shutil.rmtree(file_dir, ignore_errors=True)
os.makedirs(file_dir, exist_ok=True)
print(f"Processing {weights_file}")
with open(weights_file, "rb") as f:
matrixs = pickle.load(f)
......@@ -107,5 +109,4 @@ def run(name: str, config: CFG):
result = future.result()
except Exception as e:
print(f"Generating {result} failed with an error: {e}")
file_dir = os.path.join(config.output_dir, name, weights_file_name)
print("Files generated in", file_dir)
......@@ -106,10 +106,12 @@ def process_task(i, name, weights_file_name, matrix, H, L, config: CFG):
def run(name: str, config: CFG):
shutil.rmtree(config.output_dir, ignore_errors=True)
os.makedirs(config.output_dir, exist_ok=True)
weights_file = os.path.join(config.weights_dir, config.run_weights)
weights_file_name = os.path.splitext(os.path.basename(weights_file))[0]
file_dir = os.path.join(config.output_dir, name, weights_file_name)
if os.path.exists(file_dir):
shutil.rmtree(file_dir, ignore_errors=True)
os.makedirs(file_dir, exist_ok=True)
print(f"Processing {weights_file}")
with open(weights_file, "rb") as f:
matrixs = pickle.load(f)
......@@ -127,5 +129,4 @@ def run(name: str, config: CFG):
result = future.result()
except Exception as e:
print(f"Generating {result} failed with an error: {e}")
file_dir = os.path.join(config.output_dir, name, weights_file_name)
print("Files generated in", file_dir)
......@@ -139,10 +139,12 @@ def process_task(i, name, weights_file_name, ww_list, H, L, VN, config: CFG):
def run(name: str, config: CFG):
shutil.rmtree(config.output_dir, ignore_errors=True)
os.makedirs(config.output_dir, exist_ok=True)
weights_file = os.path.join(config.weights_dir, config.run_weights)
weights_file_name = os.path.splitext(os.path.basename(weights_file))[0]
file_dir = os.path.join(config.output_dir, name, weights_file_name)
if os.path.exists(file_dir):
shutil.rmtree(file_dir, ignore_errors=True)
os.makedirs(file_dir, exist_ok=True)
print(f"Processing {weights_file}")
with open(weights_file, "rb") as f:
matrixs = pickle.load(f)
......@@ -179,5 +181,4 @@ def run(name: str, config: CFG):
result = future.result()
except Exception as e:
print(f"Generating {result} failed with an error: {e}")
file_dir = os.path.join(config.output_dir, name, weights_file_name)
print("Files generated in", file_dir)
# %%
import shutil
import numpy as np
from pyrilog import (
ModuleBlock,
......@@ -133,6 +134,10 @@ def process_task(i, name, weights_file_name, matrix, H, L, config):
def run(name: str, config: CFG):
weights_file = os.path.join(config.weights_dir, config.run_weights)
weights_file_name = os.path.splitext(os.path.basename(weights_file))[0]
file_dir = os.path.join(config.output_dir, name, weights_file_name)
if os.path.exists(file_dir):
shutil.rmtree(file_dir, ignore_errors=True)
os.makedirs(file_dir, exist_ok=True)
print(f"Processing {weights_file}")
with open(weights_file, "rb") as f:
matrixs = pickle.load(f)
......@@ -150,5 +155,4 @@ def run(name: str, config: CFG):
result = future.result()
except Exception as e:
print(f"Generating {result} failed with an error: {e}")
file_dir = os.path.join(config.output_dir, name)
print("Files generated in", file_dir)
import argparse
import shutil
import sys
import math
import os
......@@ -50,7 +51,7 @@ def gen_wallacetree(num_addends, full_adder_list, remainder_list, total_input_li
for i, full_adder_count in enumerate(full_adder_list):
if i != len(full_adder_list) - 1: # the final bit we will manually manage
cout_cin_code += f" output [{full_adder_count} - 1 : 0] L{i}_Cout,\n"
cout_cin_code += f" input [{full_adder_count} - 1 : 0] L{i+1}_Cin,\n"
cout_cin_code += f" input [{full_adder_count} - 1 : 0] L{i + 1}_Cin,\n"
module_head_code = f"""module WallaceTree{num_addends}Input(
input [{num_addends} - 1 : 0] addends,
......@@ -70,9 +71,9 @@ def gen_wallacetree(num_addends, full_adder_list, remainder_list, total_input_li
else:
last_remainder_count = remainder_list[i - 1]
if last_remainder_count == 0:
concat_code = f"{{L{i-1}_S, L{i}_Cin}}"
concat_code = f"{{L{i - 1}_S, L{i}_Cin}}"
else:
concat_code = f"{{L{i-1}_S, L{i}_Cin, L{i-1}_remainder}}"
concat_code = f"{{L{i - 1}_S, L{i}_Cin, L{i - 1}_remainder}}"
code += f" assign L{i}_all_inputs = {concat_code};\n"
......@@ -121,15 +122,17 @@ def gen_serialwallacetree(num_addends, full_adder_list):
for i, full_adder_count in enumerate(full_adder_list):
if i != len(full_adder_list) - 1:
code += f" wire [{full_adder_count} - 1 : 0] L{i}_Cout;\n"
code += f" wire [{full_adder_count} - 1 : 0] L{i+1}_Cin;\n"
code += f" reg [{full_adder_count} - 1 : 0] L{i}_Cout_L{i+1}_Cin_reg;\n"
code += f" assign L{i+1}_Cin = L{i}_Cout_L{i+1}_Cin_reg;\n\n"
code += f" wire [{full_adder_count} - 1 : 0] L{i + 1}_Cin;\n"
code += (
f" reg [{full_adder_count} - 1 : 0] L{i}_Cout_L{i + 1}_Cin_reg;\n"
)
code += f" assign L{i + 1}_Cin = L{i}_Cout_L{i + 1}_Cin_reg;\n\n"
cin_cout_assign_code = ""
for i, full_adder_count in enumerate(full_adder_list):
if i != len(full_adder_list) - 1:
cin_cout_assign_code += f" .L{i}_Cout(L{i}_Cout),\n"
cin_cout_assign_code += f" .L{i+1}_Cin(L{i+1}_Cin),\n"
cin_cout_assign_code += f" .L{i + 1}_Cin(L{i + 1}_Cin),\n"
code += " wire final_S, final_Cout;\n"
code += " assign out_S = final_S & valid;\n"
......@@ -149,9 +152,9 @@ def gen_serialwallacetree(num_addends, full_adder_list):
for i, full_adder_count in enumerate(full_adder_list):
if i != len(full_adder_list) - 1:
reset_code += (
f" L{i}_Cout_L{i+1}_Cin_reg <= {full_adder_count}'b0;\n"
f" L{i}_Cout_L{i + 1}_Cin_reg <= {full_adder_count}'b0;\n"
)
reg_assign_code += f" L{i}_Cout_L{i+1}_Cin_reg <= L{i}_Cout&{{{full_adder_count}{{valid}}}};\n"
reg_assign_code += f" L{i}_Cout_L{i + 1}_Cin_reg <= L{i}_Cout&{{{full_adder_count}{{valid}}}};\n"
code += f"""\
always @ (posedge clk or negedge rstn) begin
......@@ -196,11 +199,11 @@ def run(name: str, config: CFG):
else:
output_code = wallace_tree_code + serial_wallace_tree_code
# Output the code to a file
os.makedirs(os.path.join(config.output_dir, name), exist_ok=True)
output_filename = os.path.join(
config.output_dir, name, f"SerialWallaceTree{i}Input.v"
)
output_dir = os.path.join(config.output_dir, name)
if os.path.exists(output_dir):
shutil.rmtree(output_dir, ignore_errors=True)
os.makedirs(output_dir, exist_ok=True)
output_filename = os.path.join(output_dir, f"SerialWallaceTree{i}Input.v")
with open(output_filename, "w") as file:
file.write(output_code)
file_dir = os.path.join(config.output_dir, name)
print("Files generated in", file_dir)
print("Files generated in", output_dir)
# %%
import shutil
import sys
import numpy as np
from pyrilog import (
......@@ -59,8 +60,8 @@ def generate_module(
"LM_sel": "LM_sel",
}
for j in range(len(value_range)):
sw_ports[f"WT_{j}_out_S"] = f"WT_{j}_out_S[{i*GN+GN-1}:{i*GN}]"
sw_ports[f"WT_{j}_out_C"] = f"WT_{j}_out_C[{i*GN+GN-1}:{i*GN}]"
sw_ports[f"WT_{j}_out_S"] = f"WT_{j}_out_S[{i * GN + GN - 1}:{i * GN}]"
sw_ports[f"WT_{j}_out_C"] = f"WT_{j}_out_C[{i * GN + GN - 1}:{i * GN}]"
add_instance(
f"Mid_wrapper_tp_{weights_file_name}_gp_{i}",
f"Mid_wrapper_{i}",
......@@ -98,6 +99,10 @@ def process_task(i, name, weights_file_name, H, L, VN, config: CFG):
def run(name: str, config: CFG):
weights_file = os.path.join(config.weights_dir, config.run_weights)
weights_file_name = os.path.splitext(os.path.basename(weights_file))[0]
file_dir = os.path.join(config.output_dir, name, weights_file_name)
if os.path.exists(file_dir):
shutil.rmtree(file_dir, ignore_errors=True)
os.makedirs(file_dir, exist_ok=True)
print(f"Processing {weights_file}")
with open(weights_file, "rb") as f:
matrixs = pickle.load(f)
......@@ -122,5 +127,4 @@ def run(name: str, config: CFG):
result = future.result()
except Exception as e:
print(f"Generating {result} failed with an error: {e}")
file_dir = os.path.join(config.output_dir, name, weights_file_name)
print("Files generated in", file_dir)
# %%
import sys
import shutil
import numpy as np
from pyrilog import (
VerilogGenerator,
......@@ -90,6 +91,10 @@ def process_task(i, name, weights_file_name, matrix, H, L, config: CFG):
def run(name: str, config: CFG):
weights_file = os.path.join(config.weights_dir, config.run_weights)
weights_file_name = os.path.splitext(os.path.basename(weights_file))[0]
file_dir = os.path.join(config.output_dir, name, weights_file_name)
if os.path.exists(file_dir):
shutil.rmtree(file_dir, ignore_errors=True)
os.makedirs(file_dir, exist_ok=True)
print(f"Processing {weights_file}")
with open(weights_file, "rb") as f:
matrixs = pickle.load(f)
......@@ -107,5 +112,4 @@ def run(name: str, config: CFG):
result = future.result()
except Exception as e:
print(f"Generating {result} failed with an error: {e}")
file_dir = os.path.join(config.output_dir, name, weights_file_name)
print("Files generated in", file_dir)
import sys
import shutil
import numpy as np
from pyrilog import (
VerilogGenerator,
......@@ -37,6 +38,10 @@ def process_task(i, name, weights_file_name, matrix, H, L, config: CFG = None):
def run(name: str, config: CFG):
weights_file = os.path.join(config.weights_dir, config.run_weights)
weights_file_name = os.path.splitext(os.path.basename(weights_file))[0]
file_dir = os.path.join(config.output_dir, name, weights_file_name)
if os.path.exists(file_dir):
shutil.rmtree(file_dir, ignore_errors=True)
os.makedirs(file_dir, exist_ok=True)
print(f"Processing {weights_file}")
with open(weights_file, "rb") as f:
matrixs = pickle.load(f)
......@@ -54,5 +59,4 @@ def run(name: str, config: CFG):
result = future.result()
except Exception as e:
print(f"Generating {result} failed with an error: {e}")
file_dir = os.path.join(config.output_dir, name, weights_file_name)
print("Files generated in", file_dir)
module FSM_wrapper_tp_k
#(
parameter H = 896, // hidden layer dim, down: 4864 other: 896
parameter L = 24, // layer num
parameter VN = 32, // vector num per group, down: 8 logits: 116 other: 32
parameter TTVN = 128, // total vector num, vary in kqv...
parameter WP = 5, // weight precision
parameter AP = 8, // activation precision
parameter SCW = 19, // related to global max WW, AP+log2(WW_max)
parameter SCWB = 5, // log2(SCW)
parameter TTW = 28 // MAC output total width, SCW + WP + 4
)
(
input clk,
input core_valid,
input rstn,
input [L - 1 : 0] LM_sel,
input [AP - 1 : 0] Top_in[H - 1 : 0],
// 下面的输出直接传出去,所以不是reg类型
output [TTW - 1 : 0] WT_result_acc[TTVN - 1 : 0],
output result_valid
);
endmodule
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment