|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +# pyre-strict |
| 8 | + |
| 9 | +import torch |
| 10 | + |
| 11 | +from torch.export import ExportedProgram |
| 12 | +from torch.utils import _pytree as pytree |
| 13 | + |
| 14 | +# TODO: These should probably be in pytorch |
| 15 | + |
| 16 | + |
| 17 | +class AOTInductorRunnerWrapper(torch.nn.Module): |
| 18 | + # pyre-fixme[2]: Parameter must be annotated. |
| 19 | + def __init__(self, aoti_runner) -> None: |
| 20 | + super().__init__() |
| 21 | + # pyre-fixme[4]: Attribute must be annotated. |
| 22 | + self.aoti_runner = aoti_runner |
| 23 | + |
| 24 | + # pyre-fixme[3]: Return type must be annotated. |
| 25 | + # pyre-fixme[2]: Parameter must be annotated. |
| 26 | + def forward(self, *flat_inputs): |
| 27 | + return self.aoti_runner.run(flat_inputs) |
| 28 | + |
| 29 | + |
| 30 | +class AOTIDelegateModule(torch.nn.Module): |
| 31 | + """ |
| 32 | + This module is the primary artifact produced by AOTInductor lowering. |
| 33 | + It is eagerly runnable in Python and traceable by torch.export. |
| 34 | + It also contains all necessary information and metadata to be pacakged and consumed |
| 35 | + by the delegate executor in runtime later. |
| 36 | +
|
| 37 | + """ |
| 38 | + |
| 39 | + def __init__(self, exported_program: ExportedProgram, so_path: str) -> None: |
| 40 | + super().__init__() |
| 41 | + self.so_path = so_path |
| 42 | + self.exported_program = exported_program |
| 43 | + self.exported_program.graph_module.recompile() |
| 44 | + |
| 45 | + # register parameters |
| 46 | + for name, parameter in self.exported_program.named_parameters(): |
| 47 | + normalized_name = name.replace(".", "_") |
| 48 | + self.register_parameter(normalized_name, parameter) |
| 49 | + |
| 50 | + # register buffers |
| 51 | + non_persistent_buffer_names = ( |
| 52 | + exported_program.graph_signature.non_persistent_buffers |
| 53 | + ) |
| 54 | + for name, buffer in self.exported_program.named_buffers(): |
| 55 | + normalized_name = name.replace(".", "_") |
| 56 | + if name in non_persistent_buffer_names: |
| 57 | + self.register_buffer(normalized_name, buffer, persistent=False) |
| 58 | + else: |
| 59 | + self.register_buffer(normalized_name, buffer, persistent=True) |
| 60 | + |
| 61 | + # handle tensor constants |
| 62 | + self.constant_names: list[str] = [] |
| 63 | + for name, constant in self.exported_program.tensor_constants.items(): |
| 64 | + # skip non-persistent buffers |
| 65 | + if name in non_persistent_buffer_names: |
| 66 | + continue |
| 67 | + normalized_name = name.replace(".", "_") |
| 68 | + setattr(self, normalized_name, constant) |
| 69 | + self.constant_names.append(normalized_name) |
| 70 | + |
| 71 | + # pyre-ignore[4]: Missing attribute annotation |
| 72 | + # pyre-ignore[16]: Undefined attribute |
| 73 | + # TODO: CPU only for now. Add GPU |
| 74 | + self.engine = torch._C._aoti.AOTIModelContainerRunnerCpu(so_path, 1) |
| 75 | + self.aoti_runner_wrapper = AOTInductorRunnerWrapper(self.engine) |
| 76 | + |
| 77 | + # pyre-fixme[3]: Return type must be annotated. |
| 78 | + # pyre-fixme[2]: Parameter must be annotated. |
| 79 | + def forward(self, *inputs): |
| 80 | + weights_args = [ |
| 81 | + *self.parameters(), |
| 82 | + *self.buffers(), |
| 83 | + ] + [getattr(self, const_name) for const_name in self.constant_names] |
| 84 | + |
| 85 | + flat_inputs = pytree.tree_flatten((inputs, {}))[0] |
| 86 | + flat_outputs = torch._higher_order_ops.aoti_call_delegate( |
| 87 | + self.aoti_runner_wrapper, |
| 88 | + self.exported_program.graph_module, |
| 89 | + weights_args, |
| 90 | + flat_inputs, |
| 91 | + ) |
| 92 | + return pytree.tree_unflatten( |
| 93 | + flat_outputs, self.exported_program.call_spec.out_spec |
| 94 | + ) |
0 commit comments