-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Arm backend: Add aten.flip support via TOSA REVERSE #20592
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 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
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,35 @@ | ||
| # Copyright 2026 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 Set, Type | ||
|
|
||
| from executorch.backends.arm._passes import ArmOpTargetedPass | ||
| from executorch.exir.dialects._ops import ops as exir_ops | ||
| from executorch.exir.pass_base import ExportPass | ||
|
|
||
|
|
||
| class DecomposeFlipPass(ArmOpTargetedPass): | ||
| """Decompose a multi-axis ``aten.flip`` into a chain of single-axis flips. | ||
|
|
||
| TOSA ``REVERSE`` reverses a single ``axis``; a flip over N dims is the | ||
| composition of N independent single-axis reversals. Each single-axis | ||
| ``aten.flip`` is lowered to one ``REVERSE`` by ``FlipVisitor``. ``flip`` is a | ||
| shared-qparam data-movement op, so the chained flips inherit the original | ||
| node's quantization parameters via ``meta``. | ||
|
|
||
| """ | ||
|
|
||
| _passes_required_after: Set[Type[ExportPass]] = set() | ||
| target_ops = {exir_ops.edge.aten.flip.default} | ||
|
|
||
| def call_operator(self, op, args, kwargs, meta, updated=False): | ||
| if op not in self.target_ops or len(args[1]) == 1: | ||
| return super().call_operator(op, args, kwargs, meta, updated) | ||
|
|
||
| # Chain one single-axis flip per dim; empty dims falls through as a no-op. | ||
| out = args[0] | ||
| for dim in args[1]: | ||
| out = super().call_operator(op, (out, [dim]), kwargs, meta, updated=True) | ||
| return out |
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 |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| op_any, | ||
| op_cat, | ||
| op_cond_if, | ||
| op_flip, | ||
| op_permute, | ||
| op_repeat, | ||
| op_sum, | ||
|
|
||
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,77 @@ | ||
| # Copyright 2026 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 Any, List | ||
|
|
||
| import torch | ||
|
|
||
| import tosa_serializer as ts | ||
|
|
||
| from executorch.backends.arm.operators.node_visitor import ( | ||
| NodeVisitor, | ||
| register_node_visitor, | ||
| ) | ||
| from executorch.backends.arm.operators.operator_validation_utils import ( | ||
| validate_num_inputs, | ||
| validate_same_dtype, | ||
| validate_valid_dtype, | ||
| ) | ||
| from executorch.backends.arm.tosa.mapping import TosaArg | ||
|
|
||
|
|
||
| @register_node_visitor | ||
| class FlipVisitor(NodeVisitor): | ||
| target = "aten.flip.default" | ||
|
|
||
| def __init__(self, *args): | ||
| super().__init__(*args) | ||
|
|
||
| def define_node( | ||
| self, | ||
| node: torch.fx.Node, | ||
| tosa_graph: Any, | ||
| inputs: List[TosaArg], | ||
| output: TosaArg, | ||
| ) -> None: | ||
| supported_dtypes = [ts.DType.BOOL] | ||
| if self.tosa_spec.support_integer(): | ||
| supported_dtypes.extend([ts.DType.INT8, ts.DType.INT16, ts.DType.INT32]) | ||
| if self.tosa_spec.support_float(): | ||
| supported_dtypes.extend([ts.DType.FP16, ts.DType.FP32]) | ||
| if self.tosa_spec.support_extension("bf16"): | ||
| supported_dtypes.append(ts.DType.BF16) | ||
| if self.tosa_spec.support_extension("fp8e4m3"): | ||
| supported_dtypes.append(ts.DType.FP8E4M3) | ||
| if self.tosa_spec.support_extension("fp8e5m2"): | ||
| supported_dtypes.append(ts.DType.FP8E5M2) | ||
|
|
||
| validate_num_inputs(self.target, inputs, 2) | ||
| validate_same_dtype(self.target, [inputs[0], output], ts) | ||
| validate_valid_dtype( | ||
| self.target, | ||
| [inputs[0], output], | ||
| supported_dtypes, | ||
| self.tosa_spec, | ||
| ) | ||
|
|
||
| dims = inputs[1].special | ||
| if len(dims) != 1: | ||
| raise ValueError( | ||
| f"{self.target} expects a single flip dim after DecomposeFlipPass, " | ||
| f"got {dims}" | ||
| ) | ||
| axis = dims[0] % len(inputs[0].shape) | ||
|
|
||
| attr = ts.TosaSerializerAttribute() | ||
| attr.ReverseAttribute(axis) | ||
| self._serialize_operator( | ||
| node, | ||
| tosa_graph, | ||
| ts.Op.REVERSE, | ||
| [inputs[0].name], | ||
| [output.name], | ||
| attr, | ||
| ) |
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,226 @@ | ||
| # Copyright 2026 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 torch | ||
|
|
||
| from executorch.backends.arm.quantizer.arm_quantizer import ( | ||
| get_symmetric_a16w8_quantization_config, | ||
| ) | ||
| from executorch.backends.arm.test import common | ||
| from executorch.backends.arm.test.tester.test_pipeline import ( | ||
| EthosU85PipelineINT, | ||
| OpNotSupportedPipeline, | ||
| TosaPipelineFP, | ||
| TosaPipelineINT, | ||
| VgfPipeline, | ||
| ) | ||
|
|
||
| aten_op = "torch.ops.aten.flip.default" | ||
| exir_op = "executorch_exir_dialects_edge__ops_aten_flip_default" | ||
|
|
||
| input_t1 = Tuple[torch.Tensor] # Input x | ||
|
|
||
|
Collaborator
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. Would be nice with coverage for dims=[] |
||
| # (input tensor, dims) — single-axis, negative-axis, and multi-axis cases. | ||
| test_data_suite = { | ||
| "rank1": lambda: (torch.rand(10), [0]), | ||
| "rank2_dim0": lambda: (torch.rand(4, 5), [0]), | ||
| "rank2_dim1_neg": lambda: (torch.rand(4, 5), [-1]), | ||
| "rank3_multi": lambda: (torch.rand(2, 3, 4), [0, 2]), | ||
| "rank4_dim2": lambda: (torch.rand(2, 3, 4, 5), [2]), | ||
| "rank4_multi": lambda: (torch.rand(2, 3, 4, 5), [1, 3]), | ||
| "rank4_all_neg": lambda: (torch.rand(2, 3, 4, 5), [-4, -3, -2, -1]), | ||
| } | ||
|
|
||
| test_data_suite_bf16 = { | ||
| "rank4_multi_bf16": lambda: ( | ||
| torch.rand(2, 3, 4, 5, dtype=torch.bfloat16), | ||
| [1, 3], | ||
| ), | ||
| } | ||
|
|
||
| # The fp8 cases compare the lowered output against eager torch.flip. Eager | ||
| # torch.flip on a float8 CPU tensor raises NotImplementedError ("flip_cpu not | ||
| # implemented for Float8_*") when the flipped dims include the last axis, so | ||
| # these cases flip outer axes only to keep that comparison runnable. | ||
| test_data_suite_fp8 = { | ||
| "rank4_multi_fp8e4m3": lambda: ( | ||
| torch.rand(2, 3, 4, 5).to(torch.float8_e4m3fn), | ||
| [0, 2], | ||
| "fp8e4m3", | ||
| ), | ||
| "rank4_multi_fp8e5m2": lambda: ( | ||
| torch.rand(2, 3, 4, 5).to(torch.float8_e5m2), | ||
| [0, 2], | ||
| "fp8e5m2", | ||
| ), | ||
| } | ||
|
|
||
| # U55 has no REVERSE, so flip must not be delegated there. | ||
| test_data_suite_u55_reject = { | ||
| "rank4_dim2": lambda: (torch.rand(2, 3, 4, 5), [2]), | ||
| } | ||
|
|
||
| # flip over no dims is the identity; | ||
| # the partitioner must prune the trivial partition instead of delegating it. | ||
| test_data_suite_empty = { | ||
| "empty_dims": lambda: (torch.rand(4, 5), []), | ||
| } | ||
|
|
||
|
|
||
| class Flip(torch.nn.Module): | ||
| def __init__(self, dims): | ||
| super().__init__() | ||
| self.dims = dims | ||
|
|
||
| def forward(self, x: torch.Tensor): | ||
| return torch.flip(x, self.dims) | ||
|
|
||
|
|
||
| class FlipBool(torch.nn.Module): | ||
| def forward(self, x: torch.Tensor): | ||
| return torch.flip(x > 0, [1]) | ||
|
|
||
|
|
||
| @common.parametrize("test_data", test_data_suite) | ||
| def test_flip_tosa_FP(test_data): | ||
| data, dims = test_data() | ||
| pipeline = TosaPipelineFP[input_t1](Flip(dims), (data,), aten_op, exir_op) | ||
| pipeline.run() | ||
|
|
||
|
|
||
| @common.parametrize("test_data", test_data_suite) | ||
| def test_flip_tosa_FP_reverse_count(test_data): | ||
| """Each flip dim must lower to exactly one TOSA REVERSE.""" | ||
| data, dims = test_data() | ||
| pipeline = TosaPipelineFP[input_t1](Flip(dims), (data,), aten_op, exir_op) | ||
| pipeline.count_tosa_ops({"REVERSE": len(dims)}) | ||
| pipeline.run() | ||
|
|
||
|
|
||
| @common.parametrize("test_data", test_data_suite_bf16) | ||
| def test_flip_tosa_FP_bf16(test_data): | ||
| data, dims = test_data() | ||
| pipeline = TosaPipelineFP[input_t1]( | ||
| Flip(dims), (data,), aten_op, exir_op, tosa_extensions=["bf16"] | ||
| ) | ||
| pipeline.run() | ||
|
|
||
|
|
||
| @common.parametrize("test_data", test_data_suite_fp8) | ||
| def test_flip_tosa_FP_fp8(test_data): | ||
| data, dims, tosa_extension = test_data() | ||
| pipeline = TosaPipelineFP[input_t1]( | ||
| Flip(dims), | ||
| (data,), | ||
| aten_op, | ||
| exir_op, | ||
| tosa_extensions=[tosa_extension], | ||
| ) | ||
| pipeline.count_tosa_ops({"REVERSE": len(dims)}) | ||
| pipeline.run() | ||
|
|
||
|
|
||
| def test_flip_bool_tosa_FP(): | ||
| """Flip on a bool mask (REVERSE supports bool).""" | ||
| pipeline = TosaPipelineFP[input_t1]( | ||
| FlipBool(), (torch.randn(2, 4),), aten_op, exir_op | ||
| ) | ||
| pipeline.run() | ||
|
|
||
|
|
||
| @common.parametrize("test_data", test_data_suite) | ||
| def test_flip_tosa_INT(test_data): | ||
| data, dims = test_data() | ||
| pipeline = TosaPipelineINT[input_t1](Flip(dims), (data,), aten_op, exir_op) | ||
| pipeline.run() | ||
|
|
||
|
|
||
| @common.parametrize("test_data", test_data_suite) | ||
| def test_flip_16a8w_tosa_INT(test_data): | ||
| """16A8W quantization (16-bit activations, 8-bit weights).""" | ||
| data, dims = test_data() | ||
| pipeline = TosaPipelineINT[input_t1]( | ||
| Flip(dims), | ||
| (data,), | ||
| aten_op, | ||
| exir_op=[], | ||
| per_channel_quantization=False, | ||
| use_to_edge_transform_and_lower=True, | ||
| tosa_extensions=["int16"], | ||
| ) | ||
| pipeline.quantizer.set_global( | ||
| get_symmetric_a16w8_quantization_config(is_per_channel=False) | ||
| ) | ||
| pipeline.run() | ||
|
|
||
|
|
||
| @common.parametrize("test_data", test_data_suite_u55_reject) | ||
| def test_flip_u55_INT_not_delegated(test_data): | ||
| data, dims = test_data() | ||
| pipeline = OpNotSupportedPipeline[input_t1]( | ||
| Flip(dims), | ||
| (data,), | ||
| non_delegated_ops={exir_op: 1}, | ||
| quantize=True, | ||
| u55_subset=True, | ||
| ) | ||
| pipeline.run() | ||
|
|
||
|
|
||
| @common.parametrize("test_data", test_data_suite_empty) | ||
| def test_flip_empty_dims_not_delegated(test_data): | ||
| """Flip over no dims is the identity.""" | ||
| data, dims = test_data() | ||
| pipeline = OpNotSupportedPipeline[input_t1]( | ||
| Flip(dims), | ||
| (data,), | ||
| non_delegated_ops={exir_op: 1}, | ||
| quantize=True, | ||
| ) | ||
| pipeline.run() | ||
|
|
||
|
|
||
| @common.parametrize("test_data", test_data_suite) | ||
| @common.XfailIfNoCorstone320 | ||
| def test_flip_u85_INT(test_data): | ||
| data, dims = test_data() | ||
| pipeline = EthosU85PipelineINT[input_t1]( | ||
| Flip(dims), | ||
| (data,), | ||
| aten_ops=[], | ||
| exir_ops=[], | ||
| ) | ||
| pipeline.run() | ||
|
|
||
|
|
||
| @common.parametrize("test_data", test_data_suite) | ||
| @common.SkipIfNoModelConverter | ||
| def test_flip_vgf_no_quant(test_data): | ||
| data, dims = test_data() | ||
| pipeline = VgfPipeline[input_t1]( | ||
| Flip(dims), | ||
| (data,), | ||
| aten_op, | ||
| exir_op, | ||
| quantize=False, | ||
| ) | ||
| pipeline.run() | ||
|
|
||
|
|
||
| @common.parametrize("test_data", test_data_suite) | ||
| @common.SkipIfNoModelConverter | ||
| def test_flip_vgf_quant(test_data): | ||
| data, dims = test_data() | ||
| pipeline = VgfPipeline[input_t1]( | ||
| Flip(dims), | ||
| (data,), | ||
| aten_op, | ||
| exir_op, | ||
| quantize=True, | ||
| ) | ||
| pipeline.run() | ||
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.
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.
This should probably be added to backends/arm/test/targets.bzl see claude comment for more info :)