|
| 1 | +from graph_net.sample_pass.sample_pass import SamplePass |
| 2 | +from graph_net.sample_pass.resumable_sample_pass_mixin import ResumableSamplePassMixin |
| 3 | +from pathlib import Path |
| 4 | +import json |
| 5 | +from itertools import groupby |
| 6 | + |
| 7 | + |
| 8 | +class FusibleSubgraphRangesGenerator(SamplePass, ResumableSamplePassMixin): |
| 9 | + def __init__(self, config): |
| 10 | + super().__init__(config) |
| 11 | + |
| 12 | + def declare_config( |
| 13 | + self, |
| 14 | + model_path_prefix: str, |
| 15 | + output_dir: str, |
| 16 | + input_json_file_name: str, |
| 17 | + resume: bool = False, |
| 18 | + limits_handled_models: int = None, |
| 19 | + output_json_file_name: str = "fusible_subgraph_ranges.json", |
| 20 | + ): |
| 21 | + pass |
| 22 | + |
| 23 | + def __call__(self, rel_model_path: str): |
| 24 | + self.resumable_handle_sample(rel_model_path) |
| 25 | + |
| 26 | + def sample_handled(self, rel_model_path: str) -> bool: |
| 27 | + file_name = self.config["output_json_file_name"] |
| 28 | + return self.naive_sample_handled(rel_model_path, search_file_name=file_name) |
| 29 | + |
| 30 | + def resume(self, rel_model_path: str): |
| 31 | + analyzer = self._make_analyzer(rel_model_path) |
| 32 | + output_obj = analyzer.analyze() |
| 33 | + self._save_output(rel_model_path, output_obj) |
| 34 | + |
| 35 | + def _save_output(self, rel_model_path, output_obj): |
| 36 | + output_json = json.dumps(output_obj, indent=4) |
| 37 | + output_dir_path = Path(self.config["output_dir"]) / rel_model_path |
| 38 | + output_dir_path.mkdir(parents=True, exist_ok=True) |
| 39 | + output_file_path = output_dir_path / self.config["output_json_file_name"] |
| 40 | + output_file_path.write_text(output_json) |
| 41 | + |
| 42 | + def _make_analyzer(self, rel_model_path: str): |
| 43 | + model_path = ( |
| 44 | + Path(self.config["model_path_prefix"]) |
| 45 | + / rel_model_path |
| 46 | + / self.config["input_json_file_name"] |
| 47 | + ) |
| 48 | + json_ctx = self._make_json_ctx(model_path) |
| 49 | + return FusibleSubgraphRangesAnalyzer( |
| 50 | + num_subgraph_kernels_list=self._get_num_subgraph_kernels_list(json_ctx), |
| 51 | + num_subgraph_ops_list=self._get_num_subgraph_ops_list(json_ctx), |
| 52 | + start_offset_in_original_graph=self._get_start_offset_in_original_graph( |
| 53 | + json_ctx |
| 54 | + ), |
| 55 | + ) |
| 56 | + |
| 57 | + def _get_start_offset_in_original_graph(self, json_ctx): |
| 58 | + return json_ctx["start_offset_in_original_graph"] |
| 59 | + |
| 60 | + def _get_num_subgraph_kernels_list(self, json_ctx): |
| 61 | + return json_ctx["num_subgraph_kernels"] |
| 62 | + |
| 63 | + def _get_num_subgraph_ops_list(self, json_ctx): |
| 64 | + return json_ctx["num_subgraph_ops"] |
| 65 | + |
| 66 | + def _make_json_ctx(self, model_path: Path): |
| 67 | + obj = json.loads(model_path.read_text()) |
| 68 | + assert len(obj["num_subgraph_kernels"]) == len(obj["num_subgraph_ops"]) |
| 69 | + return obj |
| 70 | + |
| 71 | + |
| 72 | +class FusibleSubgraphRangesAnalyzer: |
| 73 | + def __init__( |
| 74 | + self, |
| 75 | + num_subgraph_kernels_list: list[int], |
| 76 | + num_subgraph_ops_list: list[int], |
| 77 | + start_offset_in_original_graph: int, |
| 78 | + ): |
| 79 | + assert len(num_subgraph_kernels_list) == len(num_subgraph_ops_list) |
| 80 | + self.num_subgraph_kernels_list = num_subgraph_kernels_list |
| 81 | + self.num_subgraph_ops_list = num_subgraph_ops_list |
| 82 | + self.start_offset_in_original_graph = start_offset_in_original_graph |
| 83 | + |
| 84 | + def analyze(self): |
| 85 | + num_kernels_and_num_ops_list: list[ |
| 86 | + (int, list[int]) |
| 87 | + ] = self._make_num_kernels_and_num_ops_list() |
| 88 | + num_kernels_and_num_ops_list = sorted( |
| 89 | + num_kernels_and_num_ops_list, key=lambda pair: pair[0] |
| 90 | + ) |
| 91 | + num_ops_lists = [ |
| 92 | + sorted(num_ops_list) |
| 93 | + for _, num_ops_list in num_kernels_and_num_ops_list |
| 94 | + if len(set(num_ops_list)) > 1 |
| 95 | + ] |
| 96 | + fusible_subgraph_ranges = [ |
| 97 | + (start, end) |
| 98 | + for num_ops_list in num_ops_lists |
| 99 | + for start in [num_ops_list[0] - 1] |
| 100 | + for end in [num_ops_list[-1]] |
| 101 | + ] |
| 102 | + # sorted by `start` |
| 103 | + fusible_subgraph_ranges = sorted( |
| 104 | + fusible_subgraph_ranges, key=lambda pair: pair[0] |
| 105 | + ) |
| 106 | + # remove shadowed |
| 107 | + fusible_subgraph_ranges = [ |
| 108 | + fusible_subgraph_ranges[i] |
| 109 | + for i in range(len(fusible_subgraph_ranges)) |
| 110 | + if i == 0 |
| 111 | + or (fusible_subgraph_ranges[i][0] >= fusible_subgraph_ranges[i - 1][1]) |
| 112 | + ] |
| 113 | + return fusible_subgraph_ranges |
| 114 | + |
| 115 | + def _make_num_kernels_and_num_ops_list(self): |
| 116 | + num_kernels_and_num_ops = zip( |
| 117 | + self.num_subgraph_kernels_list, |
| 118 | + self.num_subgraph_ops_list, |
| 119 | + ) |
| 120 | + |
| 121 | + def get_num_kernels(pair): |
| 122 | + return pair[0] |
| 123 | + |
| 124 | + num_kernels_and_num_ops = sorted(num_kernels_and_num_ops, key=get_num_kernels) |
| 125 | + grouped_num_kernels_and_num_ops = groupby( |
| 126 | + num_kernels_and_num_ops, key=get_num_kernels |
| 127 | + ) |
| 128 | + num_kernels_and_num_ops_list = [ |
| 129 | + (num_kernels, [num_ops for _, num_ops in group]) |
| 130 | + for num_kernels, group in grouped_num_kernels_and_num_ops |
| 131 | + ] |
| 132 | + return num_kernels_and_num_ops_list |
0 commit comments