|
| 1 | +import logging |
| 2 | +from graph_net.sample_pass.sample_pass import SamplePass |
| 3 | +from graph_net.sample_pass.resumable_sample_pass_mixin import ResumableSamplePassMixin |
| 4 | +from graph_net.sample_pass.only_model_file_rewrite_sample_pass_mixin import ( |
| 5 | + OnlyModelFileRewriteSamplePassMixin, |
| 6 | +) |
| 7 | +from graph_net.dynamic_dim_constraints import DynamicDimConstraints |
| 8 | +from graph_net.imp_util import load_module |
| 9 | +from graph_net.tensor_meta import TensorMeta |
| 10 | +from graph_net.torch.static_to_dynamic import StaticToDynamic |
| 11 | +import os |
| 12 | +from contextlib import contextmanager |
| 13 | +import tempfile |
| 14 | +import shutil |
| 15 | +from pathlib import Path |
| 16 | +from dataclasses import asdict |
| 17 | +import graph_net.graph_net_json_file_util as gn_json |
| 18 | + |
| 19 | + |
| 20 | +class DimensionGeneralizer( |
| 21 | + SamplePass, ResumableSamplePassMixin, OnlyModelFileRewriteSamplePassMixin |
| 22 | +): |
| 23 | + def __init__(self, config): |
| 24 | + super().__init__(config) |
| 25 | + |
| 26 | + def declare_config( |
| 27 | + self, |
| 28 | + model_path_prefix: str, |
| 29 | + output_dir: str, |
| 30 | + resume: bool = False, |
| 31 | + limits_handled_models: int = None, |
| 32 | + last_model_log_file: str = None, |
| 33 | + ): |
| 34 | + pass |
| 35 | + |
| 36 | + def __call__(self, rel_model_path: str): |
| 37 | + self.resumable_handle_sample(rel_model_path) |
| 38 | + |
| 39 | + def sample_handled(self, rel_model_path: str) -> bool: |
| 40 | + return self.naive_sample_handled(rel_model_path, search_file_name="model.py") |
| 41 | + |
| 42 | + def resume(self, rel_model_path: str): |
| 43 | + return self.copy_sample_and_handle_model_py_file(rel_model_path) |
| 44 | + |
| 45 | + def handle_model_py_file(self, rel_model_path: str) -> str: |
| 46 | + model_path = os.path.join(self.config["model_path_prefix"], rel_model_path) |
| 47 | + output_dir = Path(self.config["output_dir"]) |
| 48 | + generalized_model_path = output_dir / rel_model_path |
| 49 | + generalized_model_path.mkdir(parents=True, exist_ok=True) |
| 50 | + tensor_metas = self._get_tensor_metas(model_path) |
| 51 | + tensor_meta_attrs_list = [asdict(tensor_meta) for tensor_meta in tensor_metas] |
| 52 | + dim_gen_pass_names = self._get_dim_gen_pass_names(model_path) |
| 53 | + dim_generalizer = self._get_dimension_generalizer(dim_gen_pass_names) |
| 54 | + inputs = dim_generalizer.create_inputs_by_metas( |
| 55 | + module=self._get_model(model_path), |
| 56 | + tensor_meta_attrs_list=tensor_meta_attrs_list, |
| 57 | + ) |
| 58 | + dyn_dim_cstrs = DynamicDimConstraints.unserialize_from_py_file( |
| 59 | + os.path.join(model_path, "input_tensor_constraints.py") |
| 60 | + ) |
| 61 | + dim_axes_pairs = self._get_dim_axes_pairs(dyn_dim_cstrs) |
| 62 | + assert len(dim_axes_pairs) > 0, f"No symbolic dims found. {model_path=}" |
| 63 | + |
| 64 | + def get_generalized(): |
| 65 | + return self._get_generalized_model_py_file_path( |
| 66 | + dim_generalizer=dim_generalizer, |
| 67 | + dim_axes_pairs=dim_axes_pairs, |
| 68 | + model_path=model_path, |
| 69 | + inputs=inputs, |
| 70 | + ) |
| 71 | + |
| 72 | + with get_generalized() as tmp_model_py_path: |
| 73 | + return Path(tmp_model_py_path).read_text() |
| 74 | + |
| 75 | + def _get_dim_axes_pairs(self, dyn_dim_cstrs): |
| 76 | + sym_input_shapes = dyn_dim_cstrs.get_sorted_symbolic_input_shapes() |
| 77 | + return [ |
| 78 | + (dim, axes) |
| 79 | + for symbol in dyn_dim_cstrs.symbols |
| 80 | + for dim in [dyn_dim_cstrs.symbol2example_value[symbol]] |
| 81 | + for axes in [ |
| 82 | + [ |
| 83 | + axis |
| 84 | + for shape in sym_input_shapes |
| 85 | + for axis, sym_or_dim in enumerate(shape) |
| 86 | + if sym_or_dim == symbol |
| 87 | + ] |
| 88 | + ] |
| 89 | + ] |
| 90 | + |
| 91 | + def _get_dim_gen_pass_names(self, model_path): |
| 92 | + json_value = gn_json.read_json(model_path) |
| 93 | + return json_value.get(gn_json.kDimensionGeneralizationPasses, []) |
| 94 | + |
| 95 | + def _get_dimension_generalizer(self, dim_gen_pass_names): |
| 96 | + dim_generalizer = StaticToDynamic({"pass_names": dim_gen_pass_names}) |
| 97 | + return dim_generalizer |
| 98 | + |
| 99 | + def _get_model(self, model_path): |
| 100 | + py_module = load_module(os.path.join(model_path, "model.py")) |
| 101 | + GraphModule = getattr(py_module, "GraphModule") |
| 102 | + GraphModule.__graph_net_file_path__ = py_module.__graph_net_file_path__ |
| 103 | + return GraphModule() |
| 104 | + |
| 105 | + @contextmanager |
| 106 | + def _get_generalized_model_py_file_path( |
| 107 | + self, dim_generalizer, dim_axes_pairs, model_path, inputs |
| 108 | + ): |
| 109 | + model = self._get_model(model_path) |
| 110 | + dim_gen_pass = dim_generalizer(model, dim_axes_pairs) |
| 111 | + logging.warning("before need_rewrite") |
| 112 | + need_rewrite = dim_gen_pass.need_rewrite(inputs) |
| 113 | + logging.warning("after need_rewrite") |
| 114 | + if not need_rewrite: |
| 115 | + yield os.path.join(model_path, "model.py") |
| 116 | + return |
| 117 | + logging.warning("before rewrite") |
| 118 | + graph_module = dim_gen_pass.rewrite(inputs) |
| 119 | + logging.warning("after rewrite") |
| 120 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 121 | + shutil.copytree(Path(model_path), Path(tmp_dir), dirs_exist_ok=True) |
| 122 | + dim_gen_pass.save_graph_module(graph_module, tmp_dir) |
| 123 | + yield os.path.join(tmp_dir, "model.py") |
| 124 | + |
| 125 | + def _get_tensor_metas(self, model_path): |
| 126 | + make = TensorMeta.unserialize_from_py_file |
| 127 | + return [ |
| 128 | + *make(os.path.join(model_path, "input_meta.py")), |
| 129 | + *make(os.path.join(model_path, "weight_meta.py")), |
| 130 | + ] |
0 commit comments