-
Notifications
You must be signed in to change notification settings - Fork 712
Arm backend: Add sign decomposition pass and test #12159
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
zingo
merged 2 commits into
pytorch:main
from
tbergkvist:Add-sign-decomposition-pass-and-test
Jul 3, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,73 @@ | ||
| # Copyright 2025 Arm Limited and/or its affiliates. | ||
| # | ||
| # 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.backends.arm._passes import ArmPass | ||
| from executorch.exir.dialects._ops import ops as exir_ops | ||
|
|
||
|
|
||
| # For MI case | ||
| edge_sign = exir_ops.edge.aten.sign.default | ||
| # For BI case | ||
| aten_sign = torch.ops.aten.sign.default | ||
|
|
||
|
|
||
| def get_ops(op): | ||
| """Returns the appropriate operator functions based on the input operator.""" | ||
| if op == edge_sign: | ||
| return ( | ||
| exir_ops.edge.aten.gt.Scalar, | ||
| exir_ops.edge.aten.lt.Scalar, | ||
| exir_ops.edge.aten.where.self, | ||
| exir_ops.edge.aten.neg.default, | ||
| exir_ops.edge.aten.mul.Scalar, | ||
| exir_ops.edge.aten.add.Scalar, | ||
| ) | ||
| elif op == aten_sign: | ||
| return ( | ||
| torch.ops.aten.gt.Scalar, | ||
| torch.ops.aten.lt.Scalar, | ||
| torch.ops.aten.where.self, | ||
| torch.ops.aten.neg.default, | ||
| torch.ops.aten.mul.Scalar, | ||
| torch.ops.aten.add.Scalar, | ||
| ) | ||
| else: | ||
| raise ValueError(f"Unsupported operator: {op}") | ||
|
|
||
|
|
||
| class DecomposeSignPass(ArmPass): | ||
| """Decomposes the sign operator into a sequence of operations that are supported by the Arm backend.""" | ||
|
|
||
| def call_operator(self, op, args, kwargs, meta): | ||
| if op not in (edge_sign, aten_sign): | ||
| return super().call_operator(op, args, kwargs, meta) | ||
|
|
||
| gt_op, lt_op, where_op, neg_op, mul_op, add_op = get_ops(op) | ||
|
|
||
| x = args[0] | ||
|
|
||
| gt_mask = super().call_operator(gt_op, (x, 0.0), {}, meta, updated=True) | ||
| lt_mask = super().call_operator(lt_op, (x, 0.0), {}, meta, updated=True) | ||
|
|
||
| zeros = super().call_operator(mul_op, (x, 0.0), {}, meta, updated=True) | ||
| ones = super().call_operator(add_op, (zeros, 1.0), {}, meta, updated=True) | ||
| neg_ones = super().call_operator(neg_op, (ones,), {}, meta, updated=True) | ||
|
|
||
| negative_tensor = super().call_operator( | ||
| where_op, (lt_mask, neg_ones, zeros), {}, meta, updated=True | ||
| ) | ||
| positive_tensor = super().call_operator( | ||
| where_op, (gt_mask, ones, zeros), {}, meta, updated=True | ||
| ) | ||
|
|
||
| return super().call_operator( | ||
| where_op, | ||
| (lt_mask, negative_tensor, positive_tensor), | ||
| {}, | ||
| meta, | ||
| updated=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
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,86 @@ | ||
| # Copyright 2025 Arm Limited and/or its affiliates. | ||
| # | ||
| # 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 Tuple | ||
|
|
||
| import pytest | ||
| import torch | ||
| from executorch.backends.arm.test import common | ||
| from executorch.backends.arm.test.tester.test_pipeline import ( | ||
| EthosU55PipelineBI, | ||
| EthosU85PipelineBI, | ||
| TosaPipelineBI, | ||
| TosaPipelineMI, | ||
| ) | ||
|
|
||
| aten_op = "torch.ops.aten.sign.default" | ||
| exir_op = "executorch_exir_dialects_edge__ops_aten__sign_default" | ||
|
|
||
| input_t1 = Tuple[torch.Tensor] | ||
|
|
||
| test_data_suite = { | ||
| "zeros": torch.zeros(3, 5), | ||
| "ones": torch.ones(4, 4), | ||
| "neg_ones": -torch.ones(4, 4), | ||
| "mixed_signs": torch.tensor([[-2.0, -1.0, 0.0, 1.0, 2.0]]), | ||
| "positive_ramp": torch.arange(0.1, 1.1, 0.2), | ||
| "negative_ramp": torch.arange(-1.0, -0.1, 0.2), | ||
| "small_values": torch.tensor([-1e-7, 0.0, 1e-7]), | ||
| "rand": torch.rand(10, 10) - 0.5, | ||
| "rand_alt_shape": torch.rand(10, 3, 5) - 0.5, | ||
| "high_magnitude": torch.tensor([-1e6, -10.0, 0.0, 10.0, 1e6]), | ||
| } | ||
|
|
||
|
|
||
| class Sign(torch.nn.Module): | ||
| def forward(self, x: torch.Tensor): | ||
| return torch.sign(x) | ||
|
|
||
|
|
||
| @common.parametrize("test_data", test_data_suite) | ||
| def test_sign_tosa_MI(test_data: Tuple): | ||
| pipeline = TosaPipelineMI[input_t1]( | ||
| Sign(), | ||
| (test_data,), | ||
| aten_op=aten_op, | ||
| exir_op=exir_op, | ||
| ) | ||
| pipeline.run() | ||
|
|
||
|
|
||
| @common.parametrize("test_data", test_data_suite) | ||
| def test_sign_tosa_BI(test_data: Tuple): | ||
| pipeline = TosaPipelineBI[input_t1]( | ||
| Sign(), | ||
| (test_data,), | ||
| aten_op=[], | ||
| exir_op=exir_op, | ||
| ) | ||
| pipeline.run() | ||
|
|
||
|
|
||
| @common.XfailIfNoCorstone300 | ||
| @common.parametrize("test_data", test_data_suite) | ||
| @pytest.mark.xfail(reason="where.self not supported on U55") | ||
| def test_sign_u55_BI(test_data: Tuple): | ||
| pipeline = EthosU55PipelineBI[input_t1]( | ||
| Sign(), | ||
| (test_data,), | ||
| aten_ops=[], | ||
| exir_ops=exir_op, | ||
| ) | ||
| pipeline.run() | ||
|
|
||
|
|
||
| @common.XfailIfNoCorstone320 | ||
| @common.parametrize("test_data", test_data_suite) | ||
| def test_sign_u85_BI(test_data: Tuple): | ||
| pipeline = EthosU85PipelineBI[input_t1]( | ||
| Sign(), | ||
| (test_data,), | ||
| aten_ops=[], | ||
| exir_ops=exir_op, | ||
| ) | ||
| pipeline.run() | ||
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.
Why don't we stop the sign ops from partition on U55 by adding it in
EthosU55NotSupported?