| 485 | |
| 486 | @compatibility(is_backward_compatible=False) |
| 487 | class ExirExportedProgram: |
| 488 | def __init__( |
| 489 | self, |
| 490 | exported_program: ExportedProgram, |
| 491 | after_to_edge_passes: bool, |
| 492 | ): |
| 493 | self.exported_program = exported_program |
| 494 | |
| 495 | # Add a flag to denote whehter to_edge is called on this program |
| 496 | # to detect misusage of directly calling to_executorch without to_edge |
| 497 | self.after_to_edge_passes = after_to_edge_passes |
| 498 | |
| 499 | def transform(self, *passes: PassType) -> "ExirExportedProgram": |
| 500 | self.exported_program = _transform(self.exported_program, *passes) |
| 501 | return self |
| 502 | |
| 503 | def __call__(self, *args: Any) -> Any: |
| 504 | return self.exported_program.module()(*args) |
| 505 | |
| 506 | # TODO(ycao): Change this to a composable function. |
| 507 | def to_edge( |
| 508 | self, config: Optional[EdgeCompileConfig] = None |
| 509 | ) -> "ExirExportedProgram": |
| 510 | config = config or EdgeCompileConfig() |
| 511 | assert isinstance( |
| 512 | self.exported_program.graph_module, torch.fx.GraphModule |
| 513 | ), f"type is instead: {type(self.exported_program.graph_module).__name__}" |
| 514 | |
| 515 | return _to_edge(self, config) |
| 516 | |
| 517 | def dump(self) -> None: |
| 518 | print(self.exported_program.graph_module.graph) |
| 519 | |
| 520 | def to_executorch( |
| 521 | self, |
| 522 | config: Optional[ExecutorchBackendConfig] = None, |
| 523 | ) -> "ExecutorchProgram": |
| 524 | if not self.after_to_edge_passes: |
| 525 | raise RuntimeError("Must run to_edge before to_executorch.") |
| 526 | config = config or ExecutorchBackendConfig() |
| 527 | new_gm = self.exported_program.graph_module |
| 528 | for p in edge_to_executorch_passes(config): |
| 529 | new_gm_res = p(new_gm) |
| 530 | assert new_gm_res is not None |
| 531 | new_gm = new_gm_res.graph_module |
| 532 | |
| 533 | # This is tech debt on tech debt. memory planning pass inherits from some pass infra for GMs. |
| 534 | # This isnt enough info now so i cant use call I have to use some new function 'run'. |
| 535 | # Existing user passes dont use run so Im just cheating here because they dont need to work on mutable buffers yet. |
| 536 | # After exir.capture is gone I will clean up the memory planning infra to be consistent. |
| 537 | # Frankly all of exir has big code quality issues because of the migrations that need to be addressed. |
| 538 | new_gm_res = config.memory_planning_pass(new_gm) # pyre-ignore[29] |
| 539 | assert new_gm_res is not None |
| 540 | new_gm = new_gm_res.graph_module |
| 541 | new_prog = ExirExportedProgram( |
| 542 | copy.deepcopy(self.exported_program), self.after_to_edge_passes |
| 543 | ) |
| 544 | _copy_module(new_prog.exported_program.graph_module, new_gm) |
no outgoing calls
no test coverage detected