|
| 1 | +# Copyright 2025 NXP |
| 2 | +# |
| 3 | +# This source code is licensed under the BSD-style license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +import copy |
| 7 | + |
| 8 | +from torch import nn |
| 9 | +from torch.export import ExportedProgram |
| 10 | +from torch.fx.passes.infra.pass_base import PassResult |
| 11 | +from torch.fx.passes.infra.pass_manager import PassManager |
| 12 | + |
| 13 | +from executorch.backends.nxp.edge_passes.move_auxiliary_operator_into_separate_qdq_cluster_pass import ( |
| 14 | + MoveLeadingAuxiliaryOperatorIntoSeparateQDQClusterPass, |
| 15 | + MoveTrailingAuxiliaryOperatorIntoSeparateQDQClusterPass, |
| 16 | +) |
| 17 | +from executorch.backends.nxp.edge_passes.neutron_edge_pass import NeutronEdgePass |
| 18 | +from executorch.exir import EdgeProgramManager |
| 19 | +from executorch.exir.program._program import _get_updated_graph_signature, _get_updated_range_constraints |
| 20 | + |
| 21 | + |
| 22 | +class NeutronEdgePassManager(PassManager): |
| 23 | + |
| 24 | + def __init__(self, passes: list[NeutronEdgePass] = None): |
| 25 | + passes: list[NeutronEdgePass] = passes or [ |
| 26 | + ] |
| 27 | + |
| 28 | + super().__init__( |
| 29 | + passes, |
| 30 | + steps=10 # Empirical value. At most 10 cycles of passes will be run. |
| 31 | + ) |
| 32 | + |
| 33 | + def _transform_graph_module(self, module: nn.Module) -> PassResult: |
| 34 | + """ Apply the passes to a single graph module. """ |
| 35 | + pass_result: PassResult = super().__call__(module) |
| 36 | + |
| 37 | + graph_module = pass_result.graph_module |
| 38 | + graph_module.graph.eliminate_dead_code() |
| 39 | + graph_module.recompile() |
| 40 | + |
| 41 | + return pass_result |
| 42 | + |
| 43 | + def __call__(self, epm: EdgeProgramManager) -> EdgeProgramManager: |
| 44 | + """ Apply the passes to all graph modules in the edge program. """ |
| 45 | + new_programs: dict[str, ExportedProgram] = {} |
| 46 | + |
| 47 | + for name, program in epm._edge_programs.items(): |
| 48 | + pass_result = self._transform_graph_module(program.graph_module) |
| 49 | + |
| 50 | + if pass_result.modified: |
| 51 | + # Create a new exported program. |
| 52 | + new_program = ExportedProgram( |
| 53 | + root=pass_result.graph_module, |
| 54 | + graph=pass_result.graph_module.graph, |
| 55 | + graph_signature=_get_updated_graph_signature( |
| 56 | + program.graph_signature, pass_result.graph_module |
| 57 | + ), |
| 58 | + state_dict=program.state_dict, |
| 59 | + range_constraints=_get_updated_range_constraints(pass_result.graph_module), |
| 60 | + module_call_graph=copy.deepcopy(program._module_call_graph), |
| 61 | + example_inputs=program.example_inputs, |
| 62 | + constants=program.constants, |
| 63 | + verifiers=[program.verifier], |
| 64 | + ) |
| 65 | + new_program.graph_module.meta.update(program.graph_module.meta) |
| 66 | + new_program.graph_module.meta.update(pass_result.graph_module.meta) |
| 67 | + |
| 68 | + else: |
| 69 | + # Keep the old exported program. |
| 70 | + new_program = program |
| 71 | + |
| 72 | + new_programs[name] = new_program |
| 73 | + |
| 74 | + if len(new_programs) == 0: |
| 75 | + # No passes were run, return the old EdgeProgramManager. |
| 76 | + return epm |
| 77 | + |
| 78 | + else: |
| 79 | + # Return a new EdgeProgramManager with the updated programs. |
| 80 | + return EdgeProgramManager(new_programs, copy.deepcopy(epm._config_methods), epm.compile_config) |
0 commit comments