Skip to content

Commit 1dffefd

Browse files
committed
Arm backend: Add TOSA dialect op for CONST_SHAPE
Add a TOSA dialect op for CONST_SHAPE together with a method in ArmPass to create it. Signed-off-by: Per Åstrand <per.astrand@arm.com> Change-Id: Ie082f4e1345b4baaecb00e2cfdb3d34fd8a5b123
1 parent 957dc2c commit 1dffefd

8 files changed

Lines changed: 162 additions & 2 deletions

File tree

‎backends/arm/_passes/arm_pass.py‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
# LICENSE file in the root directory of this source tree.
55

66

7+
import copy
78
import traceback
89
from abc import abstractmethod
910
from typing import Any, List, Optional, Set, Type
1011

1112
from executorch.backends.arm.constants import DISALLOW_TFA_META_KEY
12-
from executorch.exir.pass_base import ExportPass, NodeMetadata
13+
from executorch.backends.arm.tosa.mapping import TosaSpecialDtype
14+
from executorch.exir.pass_base import ExportPass, NodeMetadata, ProxyValue
1315
from torch.fx import GraphModule
1416
from torch.fx.passes.infra.pass_base import PassResult
1517

@@ -101,3 +103,19 @@ def call_submodule(
101103
self.call_operator = _call_operator_fn # type: ignore
102104
self.submodule_depth -= 1
103105
return result
106+
107+
def call_shape_operator(
108+
self, op, args: tuple, kwargs: dict, meta: NodeMetadata, update: bool
109+
) -> ProxyValue:
110+
"""
111+
Call operator for shape-producing operators. This function is responsible for marking the output of the operator
112+
with the TosaSpecialDtype of SHAPE, so that later passes can identify it as a shape-producing operator and handle it accordingly.
113+
"""
114+
# Copy meta and set TosaSpecialDtype to SHAPE
115+
if not isinstance(meta, NodeMetadata):
116+
raise TypeError("Expected meta to be of type NodeMetadata")
117+
shape_meta = copy.copy(meta)
118+
shape_meta.data = dict(meta.data)
119+
shape_meta.data[TosaSpecialDtype.meta_key()] = TosaSpecialDtype.SHAPE
120+
# Call the super (ArmPass) call operator with updated meta
121+
return self.call_operator(op, args, kwargs, shape_meta, update)

‎backends/arm/operators/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
op_tosa_rescale,
6363
op_tosa_resize,
6464
op_tosa_scatter,
65+
op_tosa_shapes,
6566
op_tosa_table,
6667
op_tosa_transpose,
6768
op_tosa_transpose_conv2d,
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2026 Arm Limited and/or its affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
# pyre-unsafe
8+
from typing import Any, cast, List
9+
10+
import torch
11+
12+
import tosa_serializer as ts # type: ignore
13+
14+
from executorch.backends.arm.operators.node_visitor import (
15+
NodeVisitor,
16+
register_node_visitor,
17+
)
18+
from executorch.backends.arm.tosa.mapping import TosaArg
19+
20+
21+
@register_node_visitor
22+
class TosaConstShapeVisitor(NodeVisitor):
23+
target = "tosa.CONST_SHAPE.default"
24+
25+
def __init__(self, *args):
26+
super().__init__(*args)
27+
28+
def define_node(
29+
self,
30+
node: torch.fx.Node,
31+
tosa_graph: Any,
32+
inputs: List[TosaArg],
33+
output: TosaArg,
34+
) -> None:
35+
shape_input = inputs[0].special
36+
tosa_graph = cast(ts.TosaSerializer, tosa_graph)
37+
tosa_graph.addConst(
38+
shape_input,
39+
dtype=ts.DType.SHAPE,
40+
vals=node.meta["val"],
41+
name=output.name,
42+
)

‎backends/arm/process_node.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import tosa_serializer as ts
1414

1515
from executorch.backends.arm.operators.node_visitor import NodeVisitor
16+
from executorch.backends.arm.tosa.dialect.shape import is_shape_op_node
1617
from executorch.backends.arm.tosa.mapping import TosaArg
1718
from executorch.backends.arm.tosa.specification import TosaSpecification
1819
from executorch.backends.arm.tosa.utils import tosa_shape
@@ -65,7 +66,8 @@ def process_call_function(
6566
"Is the original torch function supported?"
6667
) from e
6768

68-
if not output.multiple_output_names:
69+
tosa_graph = cast(ts.TosaSerializer, tosa_graph)
70+
if not output.multiple_output_names and not is_shape_op_node(node):
6971
tosa_graph.currRegion.currBasicBlock.addTensor(
7072
output.name, tosa_shape(output.shape, output.dim_order), output.dtype
7173
)
@@ -92,6 +94,7 @@ def process_inputs(
9294
tosa_spec: TosaSpecification,
9395
):
9496
"""Serialize an input node."""
97+
9598
try:
9699
tosa_arg = TosaArg(node, tosa_spec)
97100
except ValueError as e:
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2026 Arm Limited and/or its affiliates.
2+
#
3+
# This source code is licensed under the BSD-style license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
from typing import Set, Type
7+
8+
import torch
9+
from executorch.backends.arm._passes.arm_pass import ArmPass
10+
from executorch.backends.arm.tosa.mapping import TosaSpecialDtype
11+
from executorch.exir import to_edge
12+
from executorch.exir.dialects._ops import ops as exir_ops
13+
from executorch.exir.pass_base import ExportPass
14+
15+
16+
class _EmitShapePass(ArmPass):
17+
@property
18+
def _passes_required_after(self) -> Set[Type[ExportPass]]:
19+
return set()
20+
21+
def call_operator(self, op, args, kwargs, meta, updated: bool | None = False):
22+
# Inject a CONST_SHAPE once, then proceed normally.
23+
print(f"op: {op}")
24+
if op == exir_ops.edge.aten.add.Tensor:
25+
print("Injecting CONST_SHAPE")
26+
shape = super().call_shape_operator(
27+
exir_ops.backend.tosa.CONST_SHAPE.default,
28+
([1, 3],),
29+
{},
30+
meta,
31+
True,
32+
)
33+
return shape
34+
else:
35+
return super().call_operator(op, args, kwargs, meta, updated)
36+
37+
38+
def test_const_shape_injects_meta_no_target():
39+
class M(torch.nn.Module):
40+
def forward(self, x):
41+
return x + 1
42+
43+
exported = torch.export.export(M(), (torch.randn(1),))
44+
edge = to_edge(exported).transform([_EmitShapePass()])
45+
46+
gm = edge.exported_program().graph_module
47+
48+
const_shape_nodes = [
49+
n
50+
for n in gm.graph.nodes
51+
if n.target == exir_ops.backend.tosa.CONST_SHAPE.default
52+
]
53+
54+
assert const_shape_nodes
55+
for n in const_shape_nodes:
56+
assert n.meta[TosaSpecialDtype.meta_key()] == TosaSpecialDtype.SHAPE

‎backends/arm/tosa/dialect/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
rescale,
1313
resize,
1414
scatter,
15+
shape_ops,
1516
table,
1617
transpose,
1718
transpose_conv2d,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2026 Arm Limited and/or its affiliates.
2+
#
3+
# This source code is licensed under the BSD-style license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
7+
from executorch.backends.arm.tosa.dialect.ops_registration import register_fake_tosa_op
8+
from executorch.backends.arm.tosa.specification import TosaSpecification
9+
10+
11+
@register_fake_tosa_op(
12+
"CONST_SHAPE(int[] shape) -> int[]", # schema
13+
TosaSpecification.all_versions_and_profiles(),
14+
)
15+
def CONST_SHAPE(shape: list[int]) -> list[int]:
16+
"""CONST_SHAPE operator creates a constant shape tensor."""
17+
18+
return shape

‎backends/arm/tosa/dialect/shape.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2026 Arm Limited and/or its affiliates.
2+
#
3+
# This source code is licensed under the BSD-style license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
import torch
7+
8+
from executorch.backends.arm.tosa.mapping import TosaSpecialDtype
9+
10+
11+
def is_shape_op_node(node: torch.fx.Node) -> bool:
12+
"""Check if a node is a shape operation node based on TosaSpecialDtype
13+
metadata.
14+
"""
15+
return meta_has_shape_mark(node.meta)
16+
17+
18+
def meta_has_shape_mark(meta: dict) -> bool:
19+
"""Check if NodeMetadata has TosaSpecialDtype set to SHAPE."""
20+
tosa_special_dtype_val = meta.get(TosaSpecialDtype.meta_key())
21+
return tosa_special_dtype_val == TosaSpecialDtype.SHAPE

0 commit comments

Comments
 (0)