Skip to content

Arm backend: Add support alias_copy operator #10199

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 1 commit into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ def is_node_supported(
exir_ops.edge.aten.__lshift__.Scalar,
torch.ops.aten.scalar_tensor.default,
exir_ops.edge.aten.gelu.default,
exir_ops.edge.aten.alias_copy.default,
]

return supported
Expand Down
2 changes: 1 addition & 1 deletion backends/arm/operators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
op_erf,
op_exp,
op_ge,
op_get_item,
op_gt,
op_le,
op_log,
Expand Down Expand Up @@ -51,5 +50,6 @@
op_view,
op_where,
ops_binary,
ops_identity,
ops_unary,
)
35 changes: 0 additions & 35 deletions backends/arm/operators/op_get_item.py

This file was deleted.

47 changes: 47 additions & 0 deletions backends/arm/operators/ops_identity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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.

# pyre-unsafe

from typing import List

import torch
import torch.fx

import tosa_tools.v0_80.serializer.tosa_serializer as ts

from executorch.backends.arm.operators.node_visitor import (
NodeVisitor,
register_node_visitor,
)
from executorch.backends.arm.tosa_mapping import TosaArg


def identity_operator_factory(identity_target: str):
"""
Creates and registers NodeVisitors for operators that map directly
to a TOSA IDENTITY op.
"""

class IdentityOperatorVisitor(NodeVisitor):
target = identity_target

def define_node(
self,
node: torch.fx.Node,
tosa_graph: ts.TosaSerializer,
inputs: List[TosaArg],
output: TosaArg,
) -> None:
# Simply add an identityOp
tosa_graph.addOperator(
ts.TosaOp.Op().IDENTITY, [inputs[0].name], [output.name]
)

register_node_visitor(IdentityOperatorVisitor)


identity_operator_factory("getitem")
identity_operator_factory("aten.alias_copy.default")
7 changes: 6 additions & 1 deletion backends/arm/quantizer/quantization_annotator.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ def _match_pattern(
operator.getitem,
]

_one_to_one_shared_input_or_input_act_qspec = [
torch.ops.aten.adaptive_avg_pool2d.default,
torch.ops.aten.alias_copy.default,
]


def get_quant_properties( # noqa: C901
node: Node, gm: torch.fx.GraphModule, quantization_config
Expand Down Expand Up @@ -332,7 +337,7 @@ def any_or_hardtanh_min_zero(n: Node):
_QuantProperty(2, shared_qspec), # type: ignore[arg-type]
]
quant_properties.quant_output = _QuantProperty(0, shared_qspec) # type: ignore[arg-type]
elif node.target == torch.ops.aten.adaptive_avg_pool2d.default:
elif node.target in _one_to_one_shared_input_or_input_act_qspec:
input_qspec = (
SharedQuantizationSpec(node.args[0]) # type: ignore[arg-type]
if arm_quantizer_utils.is_output_annotated(node.args[0]) # type: ignore
Expand Down
83 changes: 83 additions & 0 deletions backends/arm/test/ops/test_alias_copy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# 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 torch
from executorch.backends.arm.test import common
from executorch.backends.arm.test.tester.test_pipeline import (
EthosU55PipelineBI,
EthosU85PipelineBI,
TosaPipelineBI,
TosaPipelineMI,
)

input_t1 = Tuple[torch.Tensor]


class AliasCopy(torch.nn.Module):
"""
Tests proper handling of alias_copy when used directly.

alias_copy can also appear from PyTorch/ExecuTorch optimizations
such as `x.transpose(0, 0)`. This is optimized to an alias_copy but
not before dq/q operators are added.
"""

aten_op = "torch.ops.aten.alias_copy.default"
exir_op = "executorch_exir_dialects_edge__ops_aten_alias_copy_default"

test_data: dict[input_t1] = {
"1d_ramp": (torch.arange(-16, 16, 0.2),),
"2d_ones": (torch.ones(5, 5),),
"3d_rand": (torch.rand(3, 5, 5),),
"4d_zeros": (torch.zeros(1, 10, 10, 10),),
}

def __init__(self):
super().__init__()

def forward(self, x: torch.Tensor):
return torch.alias_copy(x)


@common.parametrize("test_data", AliasCopy.test_data)
def test_alias_copy_tosa_MI(test_data: input_t1):
TosaPipelineMI[input_t1](
AliasCopy(),
test_data,
AliasCopy.aten_op,
AliasCopy.exir_op,
).run()


@common.parametrize("test_data", AliasCopy.test_data)
def test_alias_copy_tosa_BI(test_data: input_t1):
TosaPipelineBI[input_t1](
AliasCopy(),
test_data,
AliasCopy.aten_op,
AliasCopy.exir_op,
).run()


@common.parametrize("test_data", AliasCopy.test_data)
def test_alias_copy_u55_BI(test_data: input_t1):
EthosU55PipelineBI[input_t1](
AliasCopy(),
test_data,
AliasCopy.aten_op,
AliasCopy.exir_op,
).run()


@common.parametrize("test_data", AliasCopy.test_data)
def test_alias_copy_u85_BI(test_data: input_t1):
EthosU85PipelineBI[input_t1](
AliasCopy(),
test_data,
AliasCopy.aten_op,
AliasCopy.exir_op,
).run()
Loading