Merged
feat(tool): Speed up swimlane-graph generation #5945
sunhaoxuan2024Add on Sep 4
feat(tool): Speed up swimlane-graph generation #5945
Merged
Merged fromsunhaoxuan2024/pyptomasterintocann/pyptomaster
Total: 1 files changed+40-15
| @@ -13,6 +13,7 @@ | |||
| 13 | import argparse | 13 | import argparse |
| 14 | from collections import defaultdict | 14 | from collections import defaultdict |
| 15 | from datetime import datetime, timezone | 15 | from datetime import datetime, timezone |
| 16 | +import gc | ||
| 16 | import json | 17 | import json |
| 17 | import os | 18 | import os |
| 18 | import sys | 19 | import sys |
| @@ -296,7 +297,22 @@ def parse_arguments(): | |||
| 296 | return parser.parse_args() | 297 | return parser.parse_args() |
| 297 | 298 | ||
| 298 | 299 | ||
| 300 | +try: | ||
| 301 | + import orjson as _fast_json | ||
| 302 | +except ImportError: | ||
| 303 | + _fast_json = None | ||
| 304 | + | ||
| 305 | + | ||
| 299 | def load_json(file_path): | 306 | def load_json(file_path): |
| 307 | + if _fast_json is not None: | ||
| 308 | + gc_enabled = gc.isenabled() | ||
| 309 | + gc.disable() | ||
| 310 | + try: | ||
| 311 | + with open(file_path, "rb") as file: | ||
| 312 | + return _fast_json.loads(file.read()) | ||
| 313 | + finally: | ||
| 314 | + if gc_enabled: | ||
| 315 | + gc.enable() | ||
| 300 | with open(file_path, "r") as file: | 316 | with open(file_path, "r") as file: |
| 301 | return json.load(file) | 317 | return json.load(file) |
| 302 | 318 | ||
| @@ -749,7 +765,7 @@ def convert_to_chrome_trace_json(out_path, is_dyn): | |||
| 749 | # 构建chrome trace json 文件 | 765 | # 构建chrome trace json 文件 |
| 750 | # 写入到JSON文件 | 766 | # 写入到JSON文件 |
| 751 | with open(out_path, "w", encoding="utf-8") as f: | 767 | with open(out_path, "w", encoding="utf-8") as f: |
| 752 | - json.dump(res, f, ensure_ascii=False, indent=4) | 768 | + f.write(json.dumps(res, ensure_ascii=False, separators=(",", ":"))) |
| 753 | print("Convert To perfetto trace End") | 769 | print("Convert To perfetto trace End") |
| 754 | 770 | ||
| 755 | 771 | ||
| @@ -767,17 +783,14 @@ def generate_execute_json(path): | |||
| 767 | print("Generate Executable Json:", path) | 783 | print("Generate Executable Json:", path) |
| 768 | 784 | ||
| 769 | 785 | ||
| 770 | -def get_func_index(func_hash, func_data): | ||
| 771 | - i = 0 | ||
| 772 | - while i < len(func_data): | ||
| 773 | - if str(func_hash) == func_data[i].get("hash", "0"): | ||
| 774 | - return i | ||
| 775 | - i += 1 | ||
| 776 | - return i | ||
| 777 | - | ||
| 778 | - | ||
| 779 | def load_dyn_topo(file_path, func_data): | 786 | def load_dyn_topo(file_path, func_data): |
| 780 | # 输入文件由Tensor_Main_2.json改为program.json,处理json数据接入 | 787 | # 输入文件由Tensor_Main_2.json改为program.json,处理json数据接入 |
| 788 | + hash_order_info_cache = {} | ||
| 789 | + func_index_by_hash = {} | ||
| 790 | + tensors_cache = {} | ||
| 791 | + rawtensors_cache = {} | ||
| 792 | + for i, func in enumerate(func_data): | ||
| 793 | + func_index_by_hash.setdefault(str(func.get("hash", "0")), i) | ||
| 781 | func_hash_data = {} | 794 | func_hash_data = {} |
| 782 | for _, func in enumerate(func_data): | 795 | for _, func in enumerate(func_data): |
| 783 | func_hash_data[func['hash']] = func | 796 | func_hash_data[func['hash']] = func |
| @@ -829,10 +842,19 @@ def load_dyn_topo(file_path, func_data): | |||
| 829 | wrap_id, | 842 | wrap_id, |
| 830 | static_succ_count, | 843 | static_succ_count, |
| 831 | ) = fields[:11] | 844 | ) = fields[:11] |
| 832 | - root_index = get_func_index(root_hash, func_data) | 845 | + root_index = func_index_by_hash.get(str(root_hash), len(func_data)) |
| 833 | - leaf_index = get_func_index(func_hash, func_data) | 846 | + leaf_index = func_index_by_hash.get(str(func_hash), len(func_data)) |
| 834 | succs = fields[11:] | 847 | succs = fields[11:] |
| 835 | - l1_info, cube_info, vec_info = fcvt.get_hash_order_info_for_task(root_index, opmagic, leaf_index, func_data) | 848 | + cache_key = (root_index, opmagic, leaf_index) |
| 849 | + if cache_key not in hash_order_info_cache: | ||
| 850 | + hash_order_info_cache[cache_key] = fcvt.get_hash_order_info_for_task( | ||
| 851 | + root_index, opmagic, leaf_index, func_data | ||
| 852 | + ) | ||
| 853 | + l1_info, cube_info, vec_info = hash_order_info_cache[cache_key] | ||
| 854 | + str_func_hash = str(func_hash) | ||
| 855 | + if str_func_hash not in tensors_cache: | ||
| 856 | + tensors_cache[str_func_hash] = fcvt.get_tensors(str_func_hash, func_hash_data) | ||
| 857 | + rawtensors_cache[str_func_hash] = fcvt.get_rawtensors(str_func_hash, func_hash_data) | ||
| 836 | topo.append( | 858 | topo.append( |
| 837 | { | 859 | { |
| 838 | "taskId": seq_no << 32 | task_id, | 860 | "taskId": seq_no << 32 | task_id, |
| @@ -855,8 +877,8 @@ def load_dyn_topo(file_path, func_data): | |||
| 855 | "outoperands": fcvt.get_in_out_operand_str(False, root_index, opmagic, func_data), | 877 | "outoperands": fcvt.get_in_out_operand_str(False, root_index, opmagic, func_data), |
| 856 | "in_operands": fcvt.get_in_out_operands_data(True, root_index, opmagic, func_data), | 878 | "in_operands": fcvt.get_in_out_operands_data(True, root_index, opmagic, func_data), |
| 857 | "out_operands": fcvt.get_in_out_operands_data(False, root_index, opmagic, func_data), | 879 | "out_operands": fcvt.get_in_out_operands_data(False, root_index, opmagic, func_data), |
| 858 | - "tensors": fcvt.get_tensors(str(func_hash), func_hash_data), | 880 | + "tensors": tensors_cache[str_func_hash], |
| 859 | - "rawtensors": fcvt.get_rawtensors(str(func_hash), func_hash_data), | 881 | + "rawtensors": rawtensors_cache[str_func_hash], |
| 860 | } | 882 | } |
| 861 | ) | 883 | ) |
| 862 | return topo | 884 | return topo |
| @@ -1080,6 +1102,7 @@ def calculate_pipe_usage(path): | |||
| 1080 | if __name__ == "__main__": | 1102 | if __name__ == "__main__": |
| 1081 | start_time = time_module.time() | 1103 | start_time = time_module.time() |
| 1082 | start_str = datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S') | 1104 | start_str = datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S') |
| 1105 | + print("========== Swimlane diagram generation start ==========") | ||
| 1083 | print(f"Start time: {start_str}") | 1106 | print(f"Start time: {start_str}") |
| 1084 | 1107 | ||
| 1085 | args = parse_arguments() | 1108 | args = parse_arguments() |
| @@ -1091,6 +1114,7 @@ if __name__ == "__main__": | |||
| 1091 | if not os.path.exists(args.func_table_file): | 1114 | if not os.path.exists(args.func_table_file): |
| 1092 | sys.exit(0) | 1115 | sys.exit(0) |
| 1093 | program_data = load_json(args.func_table_file) | 1116 | program_data = load_json(args.func_table_file) |
| 1117 | + gc.freeze() | ||
| 1094 | func_data = program_data["functions"] | 1118 | func_data = program_data["functions"] |
| 1095 | input_topo_data = load_dyn_topo(args.topo_json_file, func_data) | 1119 | input_topo_data = load_dyn_topo(args.topo_json_file, func_data) |
| 1096 | else: | 1120 | else: |
| @@ -1117,6 +1141,7 @@ if __name__ == "__main__": | |||
| 1117 | end_time = time_module.time() | 1141 | end_time = time_module.time() |
| 1118 | end_str = datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S') | 1142 | end_str = datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S') |
| 1119 | print(f"End time: {end_str}") | 1143 | print(f"End time: {end_str}") |
| 1144 | + print("========== Swimlane diagram generation end ==========") | ||
| 1120 | 1145 | ||
| 1121 | duration = int(end_time - start_time) | 1146 | duration = int(end_time - start_time) |
| 1122 | print(f"Time taken: {duration} secs") | 1147 | print(f"Time taken: {duration} secs") |