| 22 | |
| 23 | |
| 24 | class MicroCodeGen: |
| 25 | def __init__(self): |
| 26 | pass |
| 27 | |
| 28 | def gen_micro_ops_list_from_bytes(self, model_tag, op_src_path_list, |
| 29 | op_class_name_list, |
| 30 | jinja_file_name, output_path): |
| 31 | cwd = os.path.dirname(__file__) |
| 32 | j2_env = Environment( |
| 33 | loader=FileSystemLoader(cwd), |
| 34 | trim_blocks=True, keep_trailing_newline=True) |
| 35 | |
| 36 | template_name = JINJA2_DIR + jinja_file_name |
| 37 | source = j2_env.get_template(template_name).render( |
| 38 | model_tag=model_tag, |
| 39 | op_src_path_list=op_src_path_list, |
| 40 | op_class_name_list=op_class_name_list, |
| 41 | op_class_name_list_size=len(op_class_name_list) |
| 42 | ) |
| 43 | with open(output_path, "w") as f: |
| 44 | f.write(source) |
| 45 | |
| 46 | def gen_micro_source_from_bytes(self, model_tag, embed_data, |
| 47 | jinja_file_name, output_path): |
| 48 | cwd = os.path.dirname(__file__) |
| 49 | j2_env = Environment( |
| 50 | loader=FileSystemLoader(cwd), |
| 51 | trim_blocks=True, keep_trailing_newline=True) |
| 52 | |
| 53 | template_name = JINJA2_DIR + jinja_file_name |
| 54 | source = j2_env.get_template(template_name).render( |
| 55 | model_tag=model_tag, |
| 56 | embed_data=embed_data, |
| 57 | data_size=len(embed_data), |
| 58 | ) |
| 59 | with open(output_path, "w") as f: |
| 60 | f.write(source) |
| 61 | |
| 62 | def gen_micro_source_from_array(self, model_tag, embed_data, |
| 63 | jinja_file_name, output_path): |
| 64 | cwd = os.path.dirname(__file__) |
| 65 | j2_env = Environment(loader=FileSystemLoader( |
| 66 | cwd), trim_blocks=True, keep_trailing_newline=True) |
| 67 | |
| 68 | template_name = JINJA2_DIR + jinja_file_name |
| 69 | |
| 70 | hex_bytes_string = ", ".join(map(hex, embed_data)) |
| 71 | |
| 72 | source = j2_env.get_template(template_name).render( |
| 73 | model_tag=model_tag, |
| 74 | hex_bytes_string=hex_bytes_string, |
| 75 | data_size=len(embed_data), |
| 76 | ) |
| 77 | with open(output_path, "w") as f: |
| 78 | f.write(source) |
| 79 | |
| 80 | def gen_net_def_data(self, model_tag, model_def_data, output_path): |
| 81 | embed_data = np.frombuffer(model_def_data, dtype=np.uint8) |