Skip to content
Open
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
36 changes: 32 additions & 4 deletions backends/arm/_passes/aten_to_tosa_tensor_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
# 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

import torch

from executorch.backends.arm._passes.fold_qdq_with_annotated_qparams_pass import (
get_input_qparams,
get_output_qparams,
)
from executorch.backends.arm._passes.tosa_lowering_utils import create_const
from executorch.backends.transforms.aten_to_dialect_pass import (
AtenToDialectPass,
DialectNodeSpec,
Expand All @@ -25,6 +34,29 @@ def rewrite_rfft2(node: Node, pass_: AtenToDialectPass) -> DialectNodeSpec | Non
)


def rewrite_mul(node: Node, pass_: AtenToDialectPass) -> DialectNodeSpec:
with node.graph.inserting_before(node):
shift = create_const(node.graph, 0, torch.int8, node)
return DialectNodeSpec(exir_ops.backend.tosa.MUL.default, (*node.args, shift), {})


def rewrite_negate(node: Node, pass_: AtenToDialectPass) -> DialectNodeSpec:
input_node = cast(Node, node.args[0])
dtype = input_node.meta["val"].dtype
input_zp, output_zp = 0, 0
if dtype == torch.int8:
input_zp = get_input_qparams(node)[0].get_zp_per_tensor()
output_zp = get_output_qparams(node)[0].get_zp_per_tensor()
with node.graph.inserting_before(node):
input_zero_point = create_const(node.graph, input_zp, dtype, input_node)
output_zero_point = create_const(node.graph, output_zp, dtype, input_node)
return DialectNodeSpec(
exir_ops.backend.tosa.NEGATE.default,
(input_node, input_zero_point, output_zero_point),
{},
)


def rewrite_binary_operator(
node: Node, pass_: AtenToDialectPass
) -> DialectNodeSpec | None:
Expand All @@ -51,8 +83,6 @@ def rewrite_binary_operator(
target = exir_ops.backend.tosa.MAXIMUM.default
case exir_ops.edge.aten.minimum.default:
target = exir_ops.backend.tosa.MINIMUM.default
case exir_ops.edge.aten.mul.Tensor:
target = exir_ops.backend.tosa.MUL.default
case exir_ops.edge.aten.pow.Tensor_Tensor:
target = exir_ops.backend.tosa.POW.default
case exir_ops.edge.aten.sub.Tensor:
Expand Down Expand Up @@ -83,8 +113,6 @@ def rewrite_unary_operator(
target = exir_ops.backend.tosa.LOG.default
case exir_ops.edge.aten.logical_not.default:
target = exir_ops.backend.tosa.LOGICAL_NOT.default
case exir_ops.edge.aten.neg.default:
target = exir_ops.backend.tosa.NEGATE.default
case exir_ops.edge.aten.reciprocal.default:
target = exir_ops.backend.tosa.RECIPROCAL.default
case exir_ops.edge.aten.rsqrt.default:
Expand Down
14 changes: 12 additions & 2 deletions backends/arm/_passes/exir_to_tosa_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
)
from executorch.backends.arm._passes.aten_to_tosa_tensor_operators import (
rewrite_binary_operator,
rewrite_mul,
rewrite_negate,
rewrite_rfft2,
rewrite_unary_operator,
)
Expand Down Expand Up @@ -65,6 +67,16 @@ def _get_fft_replacement(
return rewrite_rfft2(node, pass_)


@register_dialect_substitutions(exir_ops.edge.aten.mul.Tensor)
def _get_mul_replacement(node: Node, pass_: AtenToDialectPass) -> DialectNodeSpec:
return rewrite_mul(node, pass_)


@register_dialect_substitutions(exir_ops.edge.aten.neg.default)
def _get_negate_replacement(node: Node, pass_: AtenToDialectPass) -> DialectNodeSpec:
return rewrite_negate(node, pass_)


@register_dialect_substitutions(
exir_ops.edge.aten.add.Tensor,
exir_ops.edge.aten.bitwise_and.Tensor,
Expand All @@ -77,7 +89,6 @@ def _get_fft_replacement(
exir_ops.edge.aten.logical_xor.default,
exir_ops.edge.aten.maximum.default,
exir_ops.edge.aten.minimum.default,
exir_ops.edge.aten.mul.Tensor,
exir_ops.edge.aten.pow.Tensor_Tensor,
exir_ops.edge.aten.sub.Tensor,
)
Expand Down Expand Up @@ -107,7 +118,6 @@ def _get_comparison_operator_replacement(
exir_ops.edge.aten.floor.default,
exir_ops.edge.aten.log.default,
exir_ops.edge.aten.logical_not.default,
exir_ops.edge.aten.neg.default,
exir_ops.edge.aten.reciprocal.default,
exir_ops.edge.aten.rsqrt.default,
exir_ops.edge.aten.sin.default,
Expand Down
14 changes: 11 additions & 3 deletions backends/arm/_passes/insert_dynamic_padding.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,23 @@ def call_operator(self, op, args, kwargs, meta, updated=False) -> ProxyValue:
zero_spatial_padding = [0] * (spatial_rank * 2)
padding_shape = [*zero_padding_pair, *padding, *zero_padding_pair]

pad_value = args[6] if len(args) == 8 else None
if pad_value is None:
pad_value = super().call_operator(
exir_ops.backend.tosa.CONST.default,
([0], [1], input_tensor.data.dtype),
{},
meta,
True,
)
pad_res = super().call_operator(
exir_ops.backend.tosa.PAD.default,
(
input_tensor,
padding_shape,
pad_value,
),
{
"value": 0,
},
{},
meta,
True,
)
Expand Down
11 changes: 9 additions & 2 deletions backends/arm/_passes/rewrite_pad.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,17 @@ def _rewrite_constant_pad(self, input_tensor, pad, value, meta):
exir_ops.backend.tosa.CONST_SHAPE.default, (shape,), {}, meta, True
)

pad_value = super().call_operator(
exir_ops.backend.tosa.CONST.default,
([value], [1], output_dtype),
{},
meta,
True,
)
return super().call_operator(
exir_ops.backend.tosa.PAD.default,
(input_tensor, pad_shape),
{"value": value},
(input_tensor, pad_shape, pad_value),
{},
meta,
True,
)
Expand Down
29 changes: 29 additions & 0 deletions backends/arm/_passes/tosa_lowering_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 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.
"""Build explicit TOSA operands during lowering."""

import torch
from executorch.backends.arm._passes.arm_pass_utils import create_node
from executorch.backends.arm.tosa.mapping import TosaSpecialDtype
from executorch.exir.dialects._ops import ops as exir_ops
from torch.fx import Graph, Node


def create_const(graph: Graph, values, dtype: torch.dtype, from_node: Node) -> Node:
if isinstance(values, torch.Tensor):
values = values.flatten().tolist()
elif not isinstance(values, (tuple, list)):
values = [values]
values = list(values)
node = create_node(
graph,
exir_ops.backend.tosa.CONST.default,
(values, [len(values)], dtype),
from_node=from_node,
)
node.meta.pop(TosaSpecialDtype.meta_key(), None)
fake = from_node.meta["val"]
node.meta["val"] = fake.new_empty((len(values),), dtype=dtype)
return node
13 changes: 8 additions & 5 deletions backends/arm/operators/op_tosa_mul.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# 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
from typing import Any, cast, List

import torch.fx
import tosa_serializer as ts
Expand Down Expand Up @@ -31,11 +31,11 @@ def define_node(
inputs: List[TosaArg],
output: TosaArg,
) -> None:
validate_num_inputs(self.target, inputs, 2)
validate_same_dtype(self.target, [*inputs, output])
validate_num_inputs(self.target, inputs, 3)
validate_same_dtype(self.target, [*inputs[:2], output])
validate_valid_dtype(
self.target,
[*inputs, output],
[*inputs[:2], output],
[
ts.DType.INT8,
ts.DType.INT16,
Expand All @@ -47,7 +47,10 @@ def define_node(
],
)

shift = tosa_graph.addConst([1], ts.DType.INT8, 0, name=f"{output.name}_shift")
shift_node = cast(torch.fx.Node, node.args[2])
shift = tosa_graph.addConst(
[1], ts.DType.INT8, shift_node.args[0], name=f"{output.name}_shift"
)
attr = ts.TosaSerializerAttribute()
attr.MulAttribute()
self._serialize_operator(
Expand Down
2 changes: 1 addition & 1 deletion backends/arm/operators/op_tosa_neg.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def define_node(
if self.tosa_spec.support_extension("int64"):
supported_dtypes.append(ts.DType.INT64)

validate_num_inputs(self.target, inputs, 1)
validate_num_inputs(self.target, inputs, 3)
validate_same_dtype(self.target, [*inputs, output])
validate_valid_dtype(self.target, [*inputs, output], supported_dtypes)

Expand Down
23 changes: 14 additions & 9 deletions backends/arm/operators/op_tosa_pad.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# 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
from typing import Any, cast, List

import torch

Expand All @@ -20,6 +20,7 @@
validate_valid_dtype,
)
from executorch.backends.arm.tosa.mapping import TosaArg
from executorch.exir.dialects._ops import ops as exir_ops


@register_node_visitor
Expand All @@ -37,19 +38,23 @@ def define_node(
) -> None:
supported_dtypes = supported_data_layout_dtypes(self.tosa_spec)

validate_num_inputs(self.target, inputs, 2)
validate_num_inputs(self.target, inputs, 3)
validate_same_dtype(self.target, [inputs[0], output])
validate_valid_dtype(
self.target,
[inputs[0], output],
supported_dtypes,
)
pad_const = tosa_graph.addConst(
[1],
output.dtype,
[node.kwargs.get("value", 0)],
name=node.name + "_padding_value",
)
value_node = cast(torch.fx.Node, node.args[2])
pad_value_name = inputs[2].name
if value_node.target == exir_ops.backend.tosa.CONST.default:
pad_value = tosa_graph.addConst(
[1],
output.dtype,
value_node.args[0],
name=node.name + "_padding_value",
)
pad_value_name = pad_value.name

attr = ts.TosaSerializerAttribute()
attr.PadAttribute()
Expand All @@ -61,7 +66,7 @@ def define_node(
[
inputs[0].name,
inputs[1].name,
pad_const.name,
pad_value_name,
],
[output.name],
attr,
Expand Down
6 changes: 6 additions & 0 deletions backends/arm/process_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from executorch.backends.arm.tosa.mapping import TosaArg
from executorch.backends.arm.tosa.specification import TosaSpecification
from executorch.backends.arm.tosa.utils import normalize_symint
from executorch.exir.dialects._ops import ops as exir_ops
from torch._export.utils import (
get_buffer,
get_lifted_tensor_constant,
Expand Down Expand Up @@ -144,6 +145,11 @@ def process_call_function(
node_visitors: Dict[str, NodeVisitor],
tosa_spec: TosaSpecification,
):
if node.target == exir_ops.backend.tosa.CONST.default:
# Temporarily skip dialect constants: visitors still create serializer
# constants until the node-visitor flow is replaced by direct lowering.
return

# Unpack arguments and convert
try:
inputs = [TosaArg(arg, tosa_spec) for arg in node.args]
Expand Down
Loading
Loading