-
Notifications
You must be signed in to change notification settings - Fork 749
Support more ops and verify more models #14106
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
Merged
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6d929ae
change docker image for using android ndk
Jiseong-oh 93dff05
update batchmul, div, max/min/rsqrt/slice_copy/sqrt/to_copy op
Jiseong-oh 49345f8
add squeeze, sub ops
Jiseong-oh 4d92c32
Support MV3 float model
Jiseong-oh 3058d75
Add more ops not to decompose
Jiseong-oh d643e22
Support torchvision VIT float model
Jiseong-oh 9172064
Support w2l float model
Jiseong-oh 35b2072
Support bert float model (finetune)
Jiseong-oh 5a909e1
Support ops test
Jiseong-oh 09d10d6
Add model test
Jiseong-oh 49e54c6
fix lint errors
Jiseong-oh 5f1734e
enable bmm op test
Jiseong-oh ec28b28
Merge branch 'main' into expands_more_ops
Jiseong-oh 4ae638b
fix lint issue for url-issue
Jiseong-oh 37ab8ec
Merge branch 'expands_more_ops' of https://github.com/Jiseong-oh/exec…
Jiseong-oh 20780f2
fix lint error
Jiseong-oh 3437e4c
apply review comments
Jiseong-oh 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| # Copyright (c) 2025 Samsung Electronics Co. LTD | ||
| # 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 torch | ||
| from executorch.exir import ExportedProgram | ||
| from executorch.exir.dialects._ops import ops as exir_ops | ||
| from executorch.exir.pass_base import ExportPass, PassResult | ||
| from torch._export.utils import get_param | ||
|
|
||
|
|
||
| class Conv1dToConv2d(ExportPass): | ||
|
|
||
| def __init__(self, edge_program: ExportedProgram): | ||
| super().__init__() | ||
| self.edge_program = edge_program | ||
|
|
||
| def call(self, graph_module: torch.fx.GraphModule): | ||
| graph = graph_module.graph | ||
| node_list = list(graph.nodes) | ||
| for node in node_list: | ||
| if node.op == "call_function": | ||
| if node.target == exir_ops.edge.aten.convolution.default: | ||
| stride = list(node.args[3]) | ||
| if len(stride) != 1: | ||
| continue | ||
|
|
||
| # convert 3dim weight to 4dim | ||
| weight_node = node.args[1] | ||
| weight_3dim = get_param(self.edge_program, weight_node) | ||
| weight_4dim = torch.nn.Parameter( | ||
| data=weight_3dim.data.contiguous().unsqueeze(dim=-1), | ||
| requires_grad=False, | ||
| ) | ||
| parameter_name = ( | ||
| self.edge_program.graph_signature.inputs_to_parameters[ | ||
| weight_node.name | ||
| ] | ||
| ) | ||
| self.edge_program.state_dict[parameter_name] = weight_4dim | ||
| weight_node.meta["val"] = weight_node.meta["val"].data.unsqueeze( | ||
| dim=-1 | ||
| ) | ||
|
|
||
| # Extend stride, padding, and dilation | ||
| node.args = ( | ||
| node.args[0], | ||
| node.args[1], | ||
| node.args[2], | ||
| node.args[3] + [1], # stride | ||
| node.args[4] + [0], # padding | ||
| node.args[5] + [1], # dilation | ||
| node.args[6], | ||
| node.args[7], | ||
| node.args[8], | ||
| ) | ||
|
|
||
| # unsqueeze -> conv2d -> squeeze | ||
| with graph.inserting_before(node): | ||
| input_node = node.args[0] | ||
| unsqueeze_before = graph.create_node( | ||
| "call_function", exir_ops.edge.aten.unsqueeze_copy.default | ||
| ) | ||
| unsqueeze_before.args = ( | ||
| input_node, | ||
| -1, | ||
| ) | ||
| node.replace_input_with(input_node, unsqueeze_before) | ||
|
|
||
| with graph.inserting_after(node): | ||
| squeeze_after = graph.create_node( | ||
| "call_function", exir_ops.edge.aten.squeeze_copy.dims | ||
| ) | ||
| squeeze_after.args = ( | ||
| node, | ||
| [-1], | ||
| ) | ||
| original_users = [ | ||
| user for user in node.users if user != squeeze_after | ||
| ] | ||
| for user in original_users: | ||
| user.replace_input_with(node, squeeze_after) | ||
|
|
||
| graph_module.recompile() | ||
| graph_module = super().call(graph_module).graph_module | ||
| return PassResult(graph_module, 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # Copyright (c) 2025 Samsung Electronics Co. LTD | ||
| # 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 executorch.exir.passes.constant_prop_pass as constant_prop_module | ||
| from executorch.exir import ExportedProgram | ||
| from executorch.exir.pass_base import ExportPass, PassResult | ||
| from executorch.exir.passes.constant_prop_pass import constant_prop_pass | ||
| from torch.fx import GraphModule | ||
|
|
||
|
|
||
| class _constant_prop_context: | ||
| def __init__(self): | ||
| self.backup = constant_prop_module._DEFAULT_SKIP_TARGETS | ||
|
|
||
| def __enter__(self): | ||
| constant_prop_module._DEFAULT_SKIP_TARGETS = ( | ||
| constant_prop_module._DEFAULT_SKIP_TARGETS_NO_QUANT | ||
| ) | ||
|
|
||
| def __exit__(self, exc_type, exc_val, exc_tb): | ||
| constant_prop_module._DEFAULT_SKIP_TARGETS = self.backup | ||
|
|
||
|
|
||
| class ConstantPropPass(ExportPass): | ||
| """ | ||
| Official constant_prop_pass will not fold Q-DQ | ||
| But we need to fold quantized constant tensor as well as non-quantized one | ||
| """ | ||
|
|
||
| def __init__(self, edge_program: ExportedProgram): | ||
| super().__init__() | ||
| self.edge_program = edge_program | ||
|
|
||
| def call(self, graph_module: GraphModule): | ||
| with _constant_prop_context(): | ||
| _ = constant_prop_pass(self.edge_program) | ||
| return PassResult(graph_module, 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # Copyright (c) 2025 Samsung Electronics Co. LTD | ||
| # 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. | ||
|
|
||
| from typing import Dict, Tuple | ||
|
|
||
| import torch | ||
| from executorch.exir.dialects._ops import ops as exir_ops | ||
| from executorch.exir.pass_base import ExportPass | ||
| from torch._export.pass_base import Argument | ||
| from torch._export.pass_infra.node_metadata import NodeMetadata | ||
| from torch._export.pass_infra.proxy_value import ProxyValue | ||
|
|
||
|
|
||
| class ReplaceOpsWithScalar(ExportPass): | ||
| # Replace binary ops with scalar into binary ops with tensor. | ||
| # Ops list below. | ||
| _ops_with_scalar = { | ||
| exir_ops.edge.aten.add.Scalar: exir_ops.edge.aten.add.Tensor, | ||
| exir_ops.edge.aten.sub.Scalar: exir_ops.edge.aten.sub.Tensor, | ||
| exir_ops.edge.aten.div.Scalar: exir_ops.edge.aten.div.Tensor, | ||
| exir_ops.edge.aten.mul.Scalar: exir_ops.edge.aten.mul.Tensor, | ||
| exir_ops.edge.aten.pow.Tensor_Scalar: exir_ops.edge.aten.pow.Tensor_Tensor, | ||
| } | ||
|
|
||
| def __init__(self): | ||
| super(ReplaceOpsWithScalar, self).__init__() | ||
|
|
||
| def call_operator( | ||
| self, | ||
| op, | ||
| args: Tuple[Argument, ...], | ||
| kwargs: Dict[str, Argument], | ||
| meta: NodeMetadata, | ||
| ) -> ProxyValue: | ||
| if op not in self._ops_with_scalar: | ||
| return super().call_operator(op, args, kwargs, meta) | ||
|
|
||
| return super().call_operator( | ||
| op=self._ops_with_scalar.get(op, op), | ||
| args=(args[0], torch.tensor(args[1])), | ||
| kwargs=kwargs, | ||
| meta=meta, | ||
| ) |
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,40 @@ | ||
| # Copyright (c) 2025 Samsung Electronics Co. LTD | ||
| # 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. | ||
|
|
||
| from typing import Dict | ||
|
|
||
| import torch | ||
| from executorch.backends.samsung.builders.node_visitor import ( | ||
| NodeVisitor, | ||
| register_node_visitor, | ||
| ) | ||
| from executorch.backends.samsung.serialization.enn_graph_schema import EnnGraph | ||
|
|
||
|
|
||
| @register_node_visitor | ||
| class BMMVisitor(NodeVisitor): | ||
| target = "aten.bmm.default" | ||
|
|
||
| def __init__(self, *args) -> None: | ||
| super().__init__(*args) | ||
|
|
||
| def define_node( | ||
| self, | ||
| node: torch.fx.Node, | ||
| enn_graph: EnnGraph, | ||
| vals_to_ids: Dict[torch.Tensor, int], | ||
| ) -> None: | ||
| input1 = node.args[0] | ||
| input_id_1 = self.define_tensor(input1, enn_graph, vals_to_ids) | ||
| input2 = node.args[1] | ||
| input_id_2 = self.define_tensor(input2, enn_graph, vals_to_ids) | ||
|
|
||
| # output | ||
| output_id = self.define_tensor(node, enn_graph, vals_to_ids) | ||
|
|
||
| enn_graph.define_op( | ||
| node.name, "BATCH_MATMUL", [input_id_1, input_id_2], [output_id] | ||
| ) | ||
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,56 @@ | ||
| # Copyright (c) 2025 Samsung Electronics Co. LTD | ||
| # 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. | ||
|
|
||
| from typing import cast, Dict, List | ||
|
|
||
| import numpy as np | ||
|
|
||
| import torch | ||
| from executorch.backends.samsung.builders.node_visitor import ( | ||
| NodeVisitor, | ||
| register_node_visitor, | ||
| ) | ||
| from executorch.backends.samsung.serialization.enn_graph_schema import EnnGraph | ||
| from executorch.backends.transforms import get_shape | ||
|
|
||
|
|
||
| @register_node_visitor | ||
| class ConstantPadNDVisitor(NodeVisitor): | ||
| target = "aten.constant_pad_nd.default" | ||
|
|
||
| def __init__(self, *args) -> None: | ||
| super().__init__(*args) | ||
|
Comment on lines
+24
to
+25
Contributor
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. I think |
||
|
|
||
| def define_node( | ||
| self, | ||
| node: torch.fx.Node, | ||
| enn_graph: EnnGraph, | ||
| vals_to_ids: Dict[torch.Tensor, int], | ||
| ) -> None: | ||
| input = node.args[0] | ||
| input_id = self.define_tensor(input, enn_graph, vals_to_ids) | ||
|
|
||
| # torch padding order starts from the last axis, change the order to fit samsung lite-core | ||
| paddings = np.reshape(cast(List[int], node.args[1]), (-1, 2))[::-1].astype( | ||
| np.uint32 | ||
| ) | ||
| in_shape = get_shape(input) | ||
| paddings = paddings.reshape(-1).tolist() | ||
| paddings = [0] * (2 * len(in_shape) - len(paddings)) + paddings | ||
| paddings = paddings[::2] + paddings[1::2] | ||
|
|
||
| padding_value = node.args[2] | ||
| assert padding_value == 0.0, "Only Support pad constant 0 now." | ||
| # output | ||
| output_id = self.define_tensor(node, enn_graph, vals_to_ids) | ||
|
|
||
| params = { | ||
| "explicit_padding": paddings, | ||
| "padding": "EXPLICIT", | ||
| "padding_type": "CONSTANT", | ||
| } | ||
|
|
||
| enn_graph.define_op(node.name, "PAD", [input_id], [output_id], params) | ||
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.
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.
it seems like there's a lot of boilerplate in these visitor definitions. You could package up a few helper subclasses like UnaryOpVisitor, BinaryOpVisitor, etc. that get the operator name ("BATCH_MATMUL" etc.) from a class property similar to the existing
targetproperty, and then also accommodate the ones with params by having the helper subclass call self.get_params() (default implementation that returns None on the helper subclass) and pass the result to define_op if it isn't None.