Skip to content

Commit 1fd211b

Browse files
Arm backend: Add TOSA PAD dialect op (pytorch#17940)
Adds new TOSA dialect op for PAD. aten.constant_pad_nd nodes are replaced by tosa.PAD in RewritePadPass. op_constant_pad_nd node visitor is replaced by a simpler node vistior for tosa.PAD. cc @digantdesai @SS-JIA @freddan80 @per @zingo @mansnils @Sebastian-Larsson @robell Signed-off-by: Oscar Andersson <oscar.andersson@arm.com> Co-authored-by: Per Åstrand <per.astrand@arm.com>
1 parent 4839b28 commit 1fd211b

9 files changed

Lines changed: 191 additions & 115 deletions

File tree

‎backends/arm/_passes/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
from .rewrite_index_put_pass import RewriteIndexPutPass # noqa
136136
from .rewrite_le_lt_to_ge_gt_pass import RewriteLeLtToGeGtPass # noqa
137137
from .rewrite_matmul import RewriteMatmulPass # noqa
138+
from .rewrite_pad import RewritePadPass # noqa
138139
from .rewrite_upsample import RewriteUpsamplePass # noqa
139140
from .scalars_to_attribute_pass import ScalarsToAttributePass # noqa
140141
from .size_adjust_input_pass import SizeAdjustInputPass # noqa

‎backends/arm/_passes/arm_pass_manager.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
RewriteIndexPutPass,
121121
RewriteLeLtToGeGtPass,
122122
RewriteMatmulPass,
123+
RewritePadPass,
123124
RewriteUpsamplePass,
124125
ScalarsToAttributePass,
125126
SizeAdjustInputPass,
@@ -372,6 +373,7 @@ def _tosa_pipeline(
372373
RewriteUpsamplePass(),
373374
RewriteConvPass(exported_program),
374375
RewriteMatmulPass(),
376+
RewritePadPass(),
375377
]
376378
)
377379

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
10+
from executorch.backends.arm._passes import ArmPass
11+
from executorch.exir.dialects._ops import ops as exir_ops
12+
from executorch.exir.pass_base import ExportPass
13+
14+
15+
class RewritePadPass(ArmPass):
16+
"""Rewrite constant_pad_nd operator to TOSA Pad operator with constant
17+
mode.
18+
"""
19+
20+
_passes_required_after: Set[Type[ExportPass]] = set()
21+
targeted_ops = {
22+
exir_ops.edge.aten.constant_pad_nd.default,
23+
}
24+
25+
def call_operator(self, op, args, kwargs, meta, updated=False):
26+
if op not in self.targeted_ops:
27+
return super().call_operator(op, args, kwargs, meta)
28+
29+
if len(args) == 3:
30+
input_tensor, pad, value = args
31+
else:
32+
input_tensor, pad = args
33+
value = 0
34+
35+
output_dtype = meta["val"].dtype
36+
if output_dtype in (torch.int8, torch.int16):
37+
input_qparams = meta.data.get("input_qparams", {})
38+
if len(input_qparams) == 0:
39+
raise ValueError(
40+
f"No input quantization parameters found in metadata for constant_pad_nd with output dtype {output_dtype}"
41+
)
42+
value = input_qparams[0].quantize_value(value).item()
43+
44+
# Each dim needs 2 padding values. For example, to pad the last dimension, the pad has the form
45+
# (padding_left, padding_right); to pad the last two dimensions, the pad has the form
46+
# (padding_left, padding_right, padding_top, padding_bottom), and so on. We want to reverse the padding
47+
# so that we get (N_before, N_after, C_before, C_after, H_before, H_after, W_before, W_after) for a 4D
48+
# input tensor.
49+
pad_pairs = [[pad[i], pad[i + 1]] for i in range(0, len(pad), 2)]
50+
input_pad = []
51+
for pair in reversed(pad_pairs):
52+
input_pad.extend(pair)
53+
input_rank = len(input_tensor.data.shape)
54+
# Place spatial dimensions last and pad non-spatial dimensions with 0 padding
55+
shape = [0] * ((input_rank * 2 - len(pad))) + input_pad
56+
57+
pad_shape = super().call_shape_operator(
58+
exir_ops.backend.tosa.CONST_SHAPE.default, (shape,), {}, meta, True
59+
)
60+
61+
return super().call_operator(
62+
exir_ops.backend.tosa.PAD.default,
63+
(input_tensor, pad_shape),
64+
{"value": value},
65+
meta,
66+
True,
67+
)

‎backends/arm/_passes/to_tosa_memory_format_pass.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,16 @@ def _propagate_dim_order_to_shape_args(self, node: torch.fx.Node) -> None:
413413
# shape nodes depending on the order of user traversal.
414414
old_dim_order = arg.meta.get("tosa_dim_order", None) is not None
415415
dim_order = node.meta["tosa_dim_order"]
416+
# The shape node may have a different rank than the dim_order being propagated from its users
416417
if len(dim_order) != len(arg.meta["val"]):
417-
dim_order = tuple(range(len(arg.meta["val"])))
418+
# For pad shape nodes, the rank is always 2x of the input tensor rank, and the dim order needs to be adjusted accordingly.
419+
# For other shape nodes, we assume the dim order is the same as the order of dimensions in the shape.
420+
if node.target == exir_ops.backend.tosa.PAD.default:
421+
dim_order = tuple(
422+
i for axis in dim_order for i in (2 * axis, 2 * axis + 1)
423+
)
424+
else:
425+
dim_order = tuple(range(len(arg.meta["val"])))
418426
if old_dim_order and arg.meta["tosa_dim_order"] != dim_order:
419427
raise RuntimeError(
420428
f"Conflicting dim orders {arg.meta['tosa_dim_order']} and {dim_order} for shape node {arg.name}"

‎backends/arm/operators/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
op_ceil,
2424
op_clamp,
2525
op_cond_if,
26-
op_constant_pad_nd,
2726
op_cos,
2827
op_eq,
2928
op_erf,
@@ -57,6 +56,7 @@
5756
op_tosa_depthwise_conv2d,
5857
op_tosa_gather,
5958
op_tosa_matmul,
59+
op_tosa_pad,
6060
op_tosa_rescale,
6161
op_tosa_resize,
6262
op_tosa_scatter,

‎backends/arm/operators/op_constant_pad_nd.py‎

Lines changed: 0 additions & 113 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 Any, List
7+
8+
import torch
9+
10+
import tosa_serializer as ts
11+
12+
from executorch.backends.arm.operators.node_visitor import (
13+
NodeVisitor,
14+
register_node_visitor,
15+
)
16+
from executorch.backends.arm.tosa.mapping import TosaArg
17+
18+
19+
@register_node_visitor
20+
class TosaPadVisitor(NodeVisitor):
21+
target = "tosa.PAD.default"
22+
23+
tosa_specs = NodeVisitor.tosa_specs
24+
25+
def define_node(
26+
self,
27+
node: torch.fx.Node,
28+
tosa_graph: Any,
29+
inputs: List[TosaArg],
30+
output: TosaArg,
31+
) -> None:
32+
pad_const = tosa_graph.addConst(
33+
[1],
34+
output.dtype,
35+
[node.kwargs.get("value", 0)],
36+
name=node.name + "_padding_value",
37+
)
38+
39+
attr = ts.TosaSerializerAttribute()
40+
attr.PadAttribute()
41+
42+
self._serialize_operator(
43+
node,
44+
tosa_graph,
45+
ts.Op.PAD,
46+
[
47+
inputs[0].name,
48+
inputs[1].name,
49+
pad_const.name,
50+
],
51+
[output.name],
52+
attr,
53+
)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
depthwise_conv2d,
1010
gather,
1111
matmul,
12+
pad,
1213
rescale,
1314
resize,
1415
scatter,
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 List
7+
8+
import torch
9+
10+
from executorch.backends.arm.tosa.dialect.lib import TosaValueError
11+
from executorch.backends.arm.tosa.dialect.ops_registration import register_fake_tosa_op
12+
13+
from executorch.backends.arm.tosa.specification import (
14+
get_context_spec,
15+
TosaSpecification,
16+
)
17+
18+
19+
@register_fake_tosa_op(
20+
"PAD(Tensor input1, SymInt[] padding, *, Scalar value) -> Tensor", # schema
21+
(
22+
TosaSpecification.create_from_string("TOSA-1.0+INT"),
23+
TosaSpecification.create_from_string("TOSA-1.0+FP"),
24+
), # target TOSA specifications
25+
)
26+
def PAD(a: torch.Tensor, padding: List[int | torch.SymInt], *, value):
27+
tosa_spec = get_context_spec()
28+
29+
supported_dtypes = {torch.bool}
30+
if tosa_spec.support_integer():
31+
supported_dtypes.update({torch.int8, torch.int16, torch.int32})
32+
if tosa_spec.support_float():
33+
supported_dtypes.update({torch.float16, torch.float32})
34+
if tosa_spec.support_extension("bf16"):
35+
supported_dtypes.add(torch.bfloat16)
36+
if a.dtype not in supported_dtypes:
37+
raise TosaValueError(
38+
f"Input tensor dtype {a.dtype} is not supported by the target TOSA specification."
39+
f" Supported dtypes are: {supported_dtypes}",
40+
op="PAD",
41+
)
42+
43+
if len(padding) != 2 * len(a.shape):
44+
raise TosaValueError(
45+
f"Padding length {len(padding)} is not compatible with input rank {len(a.shape)}",
46+
op="PAD",
47+
)
48+
49+
# new shape:
50+
new_shape: List[int | torch.SymInt] = []
51+
for i, d in enumerate(a.shape):
52+
pad_before = padding[i * 2]
53+
pad_after = padding[i * 2 + 1]
54+
new_shape.append(pad_before + d + pad_after)
55+
56+
# return a new tensor with the new shape
57+
return torch.empty(size=new_shape, dtype=a.dtype)

0 commit comments

Comments
 (0)