-
Notifications
You must be signed in to change notification settings - Fork 607
Clean up lowering APIs #3690
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
Closed
Closed
Clean up lowering APIs #3690
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# 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. | ||
|
||
load("@fbcode_macros//build_defs:python_library.bzl", "python_library") | ||
|
||
oncall("odai_jarvis") | ||
|
||
python_library( | ||
name = "compiler", | ||
srcs = [ | ||
"compiler.py", | ||
], | ||
deps = [ | ||
":passes", | ||
"//caffe2:torch", | ||
"//executorch/exir:lib", | ||
], | ||
) | ||
|
||
python_library( | ||
name = "passes", | ||
srcs = [ | ||
"passes.py", | ||
], | ||
deps = [ | ||
"//executorch/exir:pass_base", | ||
"//executorch/exir/dialects:lib", | ||
], | ||
) |
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 |
---|---|---|
|
@@ -9,15 +9,20 @@ | |
|
||
import torch | ||
|
||
from executorch.backends.cadence.aot.passes import ( | ||
ReplacePT2DequantWithCadenceDequant, | ||
ReplacePT2QuantWithCadenceQuant, | ||
) | ||
from executorch.exir import EdgeCompileConfig, EdgeProgramManager, to_edge | ||
|
||
from torch.export import export | ||
from torch.export.exported_program import ExportedProgram | ||
|
||
|
||
# Export the model and lower it to an ExportedProgram (in aten IR) | ||
def export_program( | ||
model: torch.nn.Module, | ||
inputs: Any, | ||
inputs: Tuple[Any, ...], | ||
) -> ExportedProgram: | ||
assert isinstance(model, torch.nn.Module), "model should be an nn.Module" | ||
|
||
|
@@ -37,26 +42,49 @@ def export_program( | |
return export(model, inputs) | ||
|
||
|
||
# Export the model and lower it it edge IR. | ||
# Export the model and lower it to an EdgeProgramManager (in edge IR). | ||
def export_to_edge( | ||
model: torch.nn.Module, | ||
inputs: Any, | ||
inputs: Tuple[Any, ...], | ||
dump_graphs: bool = False, | ||
) -> Tuple[EdgeProgramManager, ExportedProgram]: | ||
) -> EdgeProgramManager: | ||
assert isinstance(model, torch.nn.Module), "model should be an nn.Module" | ||
|
||
# Export the model into an ExportedProgram. | ||
expo_program = export_program(model, inputs) | ||
|
||
if dump_graphs: | ||
logging.info(f"Exported graph:\n{expo_program.graph_module.graph}") | ||
|
||
# Call to_edge to convert the graph to edge IR. | ||
# Note: dim_order is skipped (https://github.com/pytorch/executorch/issues/3704) | ||
edge_prog_manager = to_edge( | ||
expo_program, compile_config=EdgeCompileConfig(_check_ir_validity=False) | ||
expo_program, | ||
compile_config=EdgeCompileConfig( | ||
_check_ir_validity=False, _skip_dim_order=True | ||
), | ||
) | ||
|
||
if dump_graphs: | ||
logging.info( | ||
f"Edge graph:\n{edge_prog_manager.exported_program().graph_module.graph}" | ||
) | ||
|
||
return edge_prog_manager, expo_program | ||
return edge_prog_manager | ||
|
||
|
||
# Export the model and lower it to an EdgeProgramManager (in edge IR), and | ||
# apply passes specific to Cadence DSP execution. | ||
def export_to_cadence( | ||
model: torch.nn.Module, | ||
inputs: Tuple[Any, ...], | ||
dump_graphs: bool = False, | ||
) -> EdgeProgramManager: | ||
edge_program_manager = export_to_edge(model, inputs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this just take in an EdgeProgramManager that separately had |
||
|
||
# Run a couple required passes for quant/dequant ops | ||
cadence_program_manager = edge_program_manager.transform( | ||
[ReplacePT2QuantWithCadenceQuant(), ReplacePT2DequantWithCadenceDequant()] | ||
) | ||
|
||
return cadence_program_manager |
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 |
---|---|---|
|
@@ -71,29 +71,15 @@ def get_ops_count(graph_module: torch.fx.GraphModule) -> Dict[str, int]: | |
# from export, from to_edge, and from Jarvis. Print the available | ||
# implementations for each op, and error out if the op is not supported. | ||
def print_ops_info( | ||
export_gm: torch.fx.GraphModule, | ||
to_edge_gm: torch.fx.GraphModule, | ||
jarvis_gm: torch.fx.GraphModule, | ||
): | ||
export_ops_count = get_ops_count(export_gm) | ||
to_edge_ops_count = get_ops_count(to_edge_gm) | ||
jarvis_ops_count = get_ops_count(jarvis_gm) | ||
|
||
# De-duplicate the "<op>" and "<op>_copy" ops | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why remove this part? |
||
keys_to_delete_and_add = [] | ||
for k1 in export_ops_count: | ||
for k2 in {**to_edge_ops_count, **jarvis_ops_count}: | ||
if k2.startswith(k1): | ||
keys_to_delete_and_add.append((k1, k2)) | ||
break | ||
|
||
for k in keys_to_delete_and_add: | ||
export_ops_count[k[1]] = export_ops_count[k[0]] | ||
del export_ops_count[k[0]] | ||
|
||
removed_ops = [] | ||
# Get the counts of the ops that are removed from the final graph | ||
for k in {**export_ops_count, **to_edge_ops_count}: | ||
for k in to_edge_ops_count: | ||
if k not in jarvis_ops_count: | ||
removed_ops.append(k) | ||
|
||
|
@@ -103,7 +89,6 @@ def print_ops_info( | |
op, | ||
jarvis_ops_count[op], | ||
to_edge_ops_count[op] if op in to_edge_ops_count else 0, | ||
export_ops_count[op] if op in export_ops_count else 0, | ||
] | ||
for op in jarvis_ops_count | ||
] | ||
|
@@ -115,7 +100,6 @@ def print_ops_info( | |
op, | ||
0, | ||
to_edge_ops_count[op] if op in to_edge_ops_count else 0, | ||
export_ops_count[op] if op in export_ops_count else 0, | ||
] | ||
for op in removed_ops | ||
] | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's this added
_skip_dim_order
kwarg for?