|
| 1 | +import copy |
| 2 | +import sys |
| 3 | +from contextlib import contextmanager |
| 4 | +from typing import Any, Callable, Dict, Generator, List, Optional, Set, Tuple, Union |
| 5 | +from packaging import version |
| 6 | + |
| 7 | +import torch |
| 8 | +import torch._dynamo as torchdynamo |
| 9 | + |
| 10 | +from torch_tensorrt.fx.utils import req_torch_version |
| 11 | +from torch_tensorrt.fx.passes.lower_basic_pass_aten import ( |
| 12 | + compose_bmm, |
| 13 | + compose_chunk, |
| 14 | + compose_getitem_slice, |
| 15 | + remove_ops, |
| 16 | + replace_aten_op_with_indices, |
| 17 | + replace_aten_reshape_alias_with_replace, |
| 18 | + replace_builtin_ops, |
| 19 | + replace_inplace_ops, |
| 20 | + replace_native_layernorm_with_layernorm, |
| 21 | + replace_transpose_mm_op_with_linear, |
| 22 | + run_const_fold, |
| 23 | +) |
| 24 | +from typing_extensions import TypeAlias |
| 25 | + |
| 26 | +Value: TypeAlias = Union[ |
| 27 | + Tuple["Value", ...], |
| 28 | + List["Value"], |
| 29 | + Dict[str, "Value"], |
| 30 | +] |
| 31 | + |
| 32 | + |
| 33 | +class DynamoConfig: |
| 34 | + """ |
| 35 | + Manage Exir-specific configurations of Dynamo. |
| 36 | + """ |
| 37 | + |
| 38 | + def __init__( |
| 39 | + self, |
| 40 | + capture_scalar_outputs: bool = True, |
| 41 | + guard_nn_modules: bool = True, |
| 42 | + dynamic_shapes: bool = True, |
| 43 | + specialize_int: bool = True, |
| 44 | + verbose: bool = True, |
| 45 | + ) -> None: |
| 46 | + |
| 47 | + self.capture_scalar_outputs = capture_scalar_outputs |
| 48 | + self.guard_nn_modules = guard_nn_modules |
| 49 | + self.dynamic_shapes = dynamic_shapes |
| 50 | + self.specialize_int = specialize_int |
| 51 | + self.verbose = verbose |
| 52 | + |
| 53 | + def activate(self) -> None: |
| 54 | + torchdynamo.config.capture_scalar_outputs = self.capture_scalar_outputs |
| 55 | + torchdynamo.config.guard_nn_modules = self.guard_nn_modules |
| 56 | + torchdynamo.config.dynamic_shapes = self.dynamic_shapes |
| 57 | + torchdynamo.config.specialize_int = self.specialize_int |
| 58 | + torchdynamo.config.verbose = self.verbose |
| 59 | + |
| 60 | + def deactivate(self) -> None: |
| 61 | + torchdynamo.config.capture_scalar_outputs = True |
| 62 | + torchdynamo.config.guard_nn_modules = True |
| 63 | + torchdynamo.config.dynamic_shapes = True |
| 64 | + torchdynamo.config.specialize_int = True |
| 65 | + torchdynamo.config.verbose = True |
| 66 | + |
| 67 | + |
| 68 | +@contextmanager |
| 69 | +def using_config(config: DynamoConfig) -> Generator[DynamoConfig, None, None]: |
| 70 | + config.activate() |
| 71 | + try: |
| 72 | + yield config |
| 73 | + finally: |
| 74 | + config.deactivate() |
| 75 | + |
| 76 | + |
| 77 | +@contextmanager |
| 78 | +def setting_python_recursive_limit(limit: int = 10000) -> Generator[None, None, None]: |
| 79 | + """ |
| 80 | + Temporarily increase the python interpreter stack recursion limit. |
| 81 | + This is mostly used for pickling large scale modules. |
| 82 | + """ |
| 83 | + default = sys.getrecursionlimit() |
| 84 | + if limit > default: |
| 85 | + sys.setrecursionlimit(limit) |
| 86 | + try: |
| 87 | + yield |
| 88 | + finally: |
| 89 | + sys.setrecursionlimit(default) |
| 90 | + |
| 91 | + |
| 92 | +@req_torch_version("2.dev") |
| 93 | +def dynamo_trace( |
| 94 | + f: Callable[..., Value], |
| 95 | + # pyre-ignore |
| 96 | + args: Tuple[Any, ...], |
| 97 | + aten_graph: bool, |
| 98 | + tracing_mode: str = "real", |
| 99 | + dynamo_config: Optional[DynamoConfig] = None, |
| 100 | +) -> Tuple[torch.fx.GraphModule, Set]: |
| 101 | + """ |
| 102 | + TODO: Once we fully migrate to torchdynamo frontend, we will remove |
| 103 | + this config option alltogether. For now, it helps with quick |
| 104 | + experiments with playing around with TorchDynamo |
| 105 | + """ |
| 106 | + if dynamo_config is None: |
| 107 | + dynamo_config = DynamoConfig() |
| 108 | + with using_config(dynamo_config), setting_python_recursive_limit(2000): |
| 109 | + torchdynamo.reset() |
| 110 | + try: |
| 111 | + return torchdynamo.export( |
| 112 | + f, |
| 113 | + *copy.deepcopy(args), |
| 114 | + aten_graph=aten_graph, |
| 115 | + tracing_mode=tracing_mode, |
| 116 | + ) |
| 117 | + except torchdynamo.exc.Unsupported as exc: |
| 118 | + raise RuntimeError( |
| 119 | + "The user code is using a feature we don't support. " |
| 120 | + "Please try torchdynamo.explain() to get possible the reasons", |
| 121 | + ) from exc |
| 122 | + except Exception as exc: |
| 123 | + raise RuntimeError( |
| 124 | + "torchdynamo internal error occured. Please see above stacktrace" |
| 125 | + ) from exc |
| 126 | + |
| 127 | + |
| 128 | +@req_torch_version("2.dev") |
| 129 | +def trace(model, inputs, **kwargs): |
| 130 | + """ |
| 131 | + Optimized trace with necessary passes which re-compose some ops or replace some ops |
| 132 | + These passes should be general and functional purpose |
| 133 | + """ |
| 134 | + passes_list = [ |
| 135 | + compose_bmm, |
| 136 | + compose_chunk, |
| 137 | + compose_getitem_slice, |
| 138 | + replace_aten_reshape_alias_with_replace, |
| 139 | + replace_aten_op_with_indices, |
| 140 | + replace_transpose_mm_op_with_linear, # after compose_bmm |
| 141 | + replace_native_layernorm_with_layernorm, |
| 142 | + remove_ops, |
| 143 | + replace_builtin_ops, # after replace_native_layernorm_with_layernorm |
| 144 | + replace_inplace_ops, # remove it once functionalization is enabled |
| 145 | + ] |
| 146 | + |
| 147 | + fx_module, __package__ = dynamo_trace(model, inputs, True, "symbolic") |
| 148 | + print(fx_module.graph) |
| 149 | + for passes in passes_list: |
| 150 | + pr: PassResult = passes(fx_module) |
| 151 | + fx_module = pr.graph_module |
| 152 | + |
| 153 | + fx_module(*inputs) |
| 154 | + |
| 155 | + fx_module = run_const_fold(fx_module) |
| 156 | + print(fx_module.graph) |
| 157 | + return fx_module |
0 commit comments