-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Remove unused exported tensor constants after Cortex-M lowering #22862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rascani
wants to merge
9
commits into
main
Choose a base branch
from
gh/rascani/45/head
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f420caa
Update
rascani 57305bb
Update
rascani 4859b8e
Update
rascani 7bc14d3
Update
rascani a698091
Preserve the original program when pruning unused constants
rascani c400265
Rebase remaining Cortex-M cleanup PRs after the first two landed
rascani cc18f11
Rebase remaining Cortex-M cleanup PRs after the first two landed
rascani ed50df6
Merge branch 'main' into gh/rascani/45/head
rascani 63a91a2
Preserve source programs when lifting constants
rascani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| import copy | ||
|
|
||
| from executorch.exir.pass_base import ExportedProgramPassBase, ExportedProgramPassResult | ||
| from torch.export import ExportedProgram | ||
| from torch.export.graph_signature import ExportGraphSignature, InputKind, TensorArgument | ||
| from torch.fx import GraphModule | ||
|
|
||
|
|
||
| class RemoveUnusedConstantsPass(ExportedProgramPassBase): | ||
| """Retire unused tensor constants after backend graph rewrites.""" | ||
|
|
||
| def call(self, exported_program: ExportedProgram) -> ExportedProgramPassResult: | ||
| signature = exported_program.graph_signature | ||
| placeholders = { | ||
| node.name: node | ||
| for node in exported_program.graph.nodes | ||
| if node.op == "placeholder" | ||
| } | ||
| protected_targets = { | ||
| spec.target for spec in signature.output_specs if spec.target is not None | ||
| } | ||
| preserved_names = { | ||
| argument.name | ||
| for entry in exported_program.module_call_graph | ||
| if entry.signature is not None | ||
| for argument in (*entry.signature.inputs, *entry.signature.outputs) | ||
| if isinstance(argument, TensorArgument) | ||
| } | ||
| unused = [ | ||
| spec | ||
| for spec in signature.input_specs | ||
| if spec.kind | ||
| in (InputKind.PARAMETER, InputKind.BUFFER, InputKind.CONSTANT_TENSOR) | ||
| and spec.target not in protected_targets | ||
| and spec.arg.name not in preserved_names | ||
| and not placeholders[spec.arg.name].users | ||
| ] | ||
| if not unused: | ||
| return ExportedProgramPassResult(exported_program, False) | ||
|
|
||
| unused_names = {spec.arg.name for spec in unused} | ||
| signature = ExportGraphSignature( | ||
| input_specs=[ | ||
| spec | ||
| for spec in signature.input_specs | ||
| if spec.arg.name not in unused_names | ||
| ], | ||
| output_specs=list(signature.output_specs), | ||
| ) | ||
| # The pass manager shallow-copies programs, so their graph is still shared. | ||
| graph = copy.deepcopy(exported_program.graph) | ||
| for original_node, node in zip(exported_program.graph.nodes, list(graph.nodes)): | ||
| # FX copying can rename built-ins such as "input", used by the signature. | ||
| node.name = original_node.name | ||
| if node.op == "placeholder" and node.name in unused_names: | ||
| graph.erase_node(node) | ||
| graph_module = GraphModule(exported_program.graph_module, graph) | ||
| graph_module.meta = exported_program.graph_module.meta.copy() | ||
|
|
||
| remaining_targets = {spec.target for spec in signature.input_specs} | ||
| # Entry points can share state dictionaries; retain their tensor identities. | ||
| state_dict = exported_program.state_dict.copy() | ||
| constants = exported_program.constants.copy() | ||
| for spec in unused: | ||
| if spec.target not in remaining_targets: | ||
| state_dict.pop(spec.target, None) | ||
| constants.pop(spec.target, None) | ||
|
|
||
| exported_program._state_dict = state_dict | ||
| exported_program._constants = constants | ||
| exported_program._graph_signature = signature | ||
| exported_program._graph_module = graph_module | ||
| return ExportedProgramPassResult(exported_program, True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.