Skip to content

Commit 4647d62

Browse files
authored
Qualcomm AI Engine Direct - Adding QNN backend support for reflection_pad3d core ATen op (pytorch#21502)
### Summary Added support for `aten.reflection_pad3d` on the QNN HTP backend via a decomposition pass into the `index_select` + `cat` operations. Note that QNN HTP's native Pad op with `MIRROR_REFLECT` scheme only supports tensors up to rank 4. Since `reflection_pad3d` operates on 5D tensors `[N, C, D, H, W]`, it cannot be delegated directly. Using `2d` or `1d` variants of this op in a decomposition is complicated, so this PR decomposes `reflection_pad3d` into `index_select` and `cat`, which are both fully supported at 5D rank on HTP. This is done by: 1. `index_select` to gather mirrored indices along the dimension. 2. `cat` to concatenate the reflected slices with the original tensor. ### Test plan ``` pytest backends/qualcomm/tests/rework/htp/op/v68/test.py -k "test_reflection_pad_3d" -v python backends/qualcomm/tests/test_qnn_delegate.py TestQNNQuantizedOperator.test_qnn_backend_reflection_pad3d --soc_model SM8750 --host aisw-vm15-labsd --device 545ee4aa --build_folder build-android python backends/qualcomm/tests/test_qnn_delegate.py TestQNNFloatingPointOperator.test_qnn_backend_reflection_pad3d --soc_model SM8750 --host aisw-vm15-labsd --device 545ee4aa --build_folder build-android ```
1 parent 73b46d3 commit 4647d62

6 files changed

Lines changed: 155 additions & 3 deletions

File tree

‎backends/qualcomm/_passes/decompose_pad.py‎

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,50 @@
88
from executorch.exir.dialects._ops import ops as exir_ops
99
from executorch.exir.dialects.edge._ops import EdgeOpOverload
1010
from executorch.exir.pass_base import ExportPass, PassResult
11+
from executorch.exir.passes import dead_code_elimination_pass
12+
13+
from .utils import merge_decomposed_graph
14+
15+
16+
class ReflectionPad3d(torch.nn.Module):
17+
"""Implement reflection_pad3d using index_select + cat.
18+
19+
reflection_pad3d operates on 5D tensors [N, C, D, H, W] with padding
20+
[left, right, top, bottom, front, back]. QNN HTP's Pad op with
21+
MIRROR_REFLECT scheme only supports max rank 4 tensors, so we implement
22+
it using index_select + cat which are fully supported at 5D rank.
23+
"""
24+
25+
def __init__(self, padding):
26+
super().__init__()
27+
self.padding = padding
28+
29+
@staticmethod
30+
def _pad_dim(x, dim, pad_before, pad_after):
31+
"""Apply reflection padding along a single dimension."""
32+
size = x.shape[dim]
33+
parts = []
34+
if pad_before > 0:
35+
indices = torch.arange(
36+
pad_before, 0, -1, device=x.device, dtype=torch.int32
37+
)
38+
parts.append(torch.index_select(x, dim, indices))
39+
parts.append(x)
40+
if pad_after > 0:
41+
indices = torch.arange(
42+
size - 2, size - 2 - pad_after, -1, device=x.device, dtype=torch.int32
43+
)
44+
parts.append(torch.index_select(x, dim, indices))
45+
if len(parts) > 1:
46+
return torch.cat(parts, dim=dim)
47+
return x
48+
49+
def forward(self, x):
50+
left, right, top, bottom, front, back = self.padding
51+
x = self._pad_dim(x, 4, left, right)
52+
x = self._pad_dim(x, 3, top, bottom)
53+
x = self._pad_dim(x, 2, front, back)
54+
return x
1155

1256

1357
class DecomposePad(ExportPass):
@@ -18,13 +62,19 @@ class DecomposePad(ExportPass):
1862
1963
Supported:
2064
- mode='reflect', 4 padding values -> reflection_pad2d (QNN MIRROR_REFLECT, max rank 4).
65+
- mode='reflect', 6 padding values (3d) -> decomposed into index_select + cat
66+
(QNN MIRROR_REFLECT max rank is 4, so we use Gather + Concat on the 5D tensor directly).
2167
22-
Not supported by QNN (max rank 4 for non-constant schemes):
23-
- mode='reflect', 6 padding values (3d) -> reflection_pad3d (QNN MIRROR_REFLECT max rank is 4)
68+
Not supported:
2469
- mode='replicate' -> QNN EDGE scheme produces incorrect results for FP32 inputs for replication_pad2d
2570
2671
Note: reflection_pad1d is handled by PyTorch's built-in decomposition of aten.pad.default (mode='reflect', 2 padding values)
2772
-> reflection_pad1d, combined with the skip decomp table entry for reflection_pad1d.
73+
74+
Note: This pass only targets aten.pad.default (ATen IR). The 5D reflect decomposition
75+
uses merge_decomposed_graph which inserts ATen-dialect nodes (index_select, cat). This is
76+
correct because the pass runs in the export pipeline before to_edge conversion, so the
77+
graph is still in ATen IR at that point.
2878
"""
2979

3080
_PAD_TARGETS = {
@@ -45,6 +95,27 @@ def call(self, graph_module: torch.fx.GraphModule):
4595

4696
padding = node.args[1]
4797
is_edge = isinstance(node.target, EdgeOpOverload)
98+
99+
# Handle 5D reflect padding (reflection_pad3d) via decomposition into index_select + cat.
100+
# QNN HTP's Pad op with MIRROR_REFLECT only supports max rank 4.
101+
if mode == "reflect" and len(padding) == 6:
102+
model = ReflectionPad3d(list(padding))
103+
decomposed_module = torch.export.export(
104+
model,
105+
(node.args[0].meta["val"],),
106+
strict=True,
107+
).module()
108+
with graph.inserting_before(node):
109+
remap = {"x": node.args[0]}
110+
merge_decomposed_graph(
111+
remap=remap,
112+
target_node=node,
113+
target_graph=graph,
114+
decomposed_graph_module=decomposed_module,
115+
)
116+
graph.erase_node(node)
117+
continue
118+
48119
target_op = self._PAD_OPS.get((mode, len(padding), is_edge))
49120
if target_op is None:
50121
continue
@@ -53,4 +124,5 @@ def call(self, graph_module: torch.fx.GraphModule):
53124
node.args = (node.args[0], list(padding))
54125

55126
graph_module.recompile()
127+
dead_code_elimination_pass(graph_module)
56128
return PassResult(graph_module, True)

‎backends/qualcomm/builders/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ The following PyTorch operators are supported through decomposition or annotatio
524524
| `aten.pdist`, `aten._pdist_forward` | `DecomposePDist` |
525525
| `aten.reciprocal` | `DecomposeReciprocal` |
526526
| `aten.reflection_pad1d` | PyTorch built-in decomposition |
527-
| `aten.reflection_pad2d` | `DecomposePad` |
527+
| `aten.reflection_pad2d`, `aten.reflection_pad3d` | `DecomposePad` |
528528
| `aten.remainder.Scalar`, `aten.remainder.Tensor` | `DecomposeRemainder` |
529529
| `aten.roll` | `DecomposeRoll` |
530530
| `aten.select_scatter` | `DecomposeSelectScatter` |

‎backends/qualcomm/tests/models.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2119,6 +2119,24 @@ def forward(self, x):
21192119
return self.pad(x)
21202120

21212121

2122+
class ReflectionPad3d(torch.nn.Module):
2123+
def __init__(self):
2124+
super().__init__()
2125+
self.pad = torch.nn.ReflectionPad3d((1, 1, 1, 1, 1, 1))
2126+
2127+
def forward(self, x):
2128+
return self.pad(x)
2129+
2130+
2131+
class ReflectionPad3dAsymmetric(torch.nn.Module):
2132+
def __init__(self):
2133+
super().__init__()
2134+
self.pad = torch.nn.ReflectionPad3d((2, 1, 2, 1, 2, 1))
2135+
2136+
def forward(self, x):
2137+
return self.pad(x)
2138+
2139+
21222140
class Relu(torch.nn.Module):
21232141
def __init__(self):
21242142
super().__init__()

‎backends/qualcomm/tests/rework/htp/op/v68/test.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,12 @@ def test_reflection_pad_2d(request, kwargs):
11351135
ReflectionPad.test_4d(request, kwargs) # noqa: F405
11361136

11371137

1138+
@enumerate_activation_dtype([Tolerance(), Tolerance(), Tolerance(rtol=1e-1)])
1139+
@with_htp_context
1140+
def test_reflection_pad_3d(request, kwargs):
1141+
ReflectionPad.test_5d(request, kwargs) # noqa: F405
1142+
1143+
11381144
@enumerate_activation_dtype([Tolerance(), Tolerance(), Tolerance(rtol=1e-1)])
11391145
@with_htp_context
11401146
def test_relu(request, kwargs):

‎backends/qualcomm/tests/rework/src/op.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3754,6 +3754,21 @@ def test_4d(subtests, qnn_config, quantizer, compile_spec, expected):
37543754
metrics=metrics,
37553755
)
37563756

3757+
@staticmethod
3758+
@unpack_fixtures
3759+
def test_5d(qnn_config, quantizer, compile_spec, expected):
3760+
inputs = (torch.randn(1, 3, 6, 8, 8),)
3761+
padding = (1, 1, 1, 1, 1, 1)
3762+
with expected as metrics:
3763+
export_and_verify(
3764+
module=__class__(3, padding),
3765+
inputs=inputs,
3766+
qnn_config=qnn_config,
3767+
quantizer=quantizer,
3768+
compile_specs=compile_spec,
3769+
metrics=metrics,
3770+
)
3771+
37573772

37583773
class Relu(torch.nn.Module):
37593774
def __init__(self):

‎backends/qualcomm/tests/test_qnn_delegate.py‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2074,6 +2074,27 @@ def test_qnn_backend_reflection_pad2d(self):
20742074
index += 1
20752075
self.lower_module_and_test_output(module, sample_input)
20762076

2077+
def test_qnn_backend_reflection_pad3d(self):
2078+
test_comb = [
2079+
{
2080+
QCOM_MODULE: [
2081+
ReflectionPad3d(), # noqa: F405
2082+
ReflectionPad3dAsymmetric(), # noqa: F405
2083+
],
2084+
QCOM_SAMPLE_INPUTS: [
2085+
(torch.randn(1, 3, 6, 8, 8),),
2086+
],
2087+
},
2088+
]
2089+
2090+
index = 0
2091+
for comb in test_comb:
2092+
for module in comb[QCOM_MODULE]:
2093+
for sample_input in comb[QCOM_SAMPLE_INPUTS]:
2094+
with self.subTest(i=index):
2095+
index += 1
2096+
self.lower_module_and_test_output(module, sample_input)
2097+
20772098
def test_qnn_backend_relu(self):
20782099
module = Relu() # noqa: F405
20792100
sample_input = (torch.randn([2, 5, 1, 3]),)
@@ -5286,6 +5307,26 @@ def test_qnn_backend_reflection_pad2d(self):
52865307
qdq_module = self.get_qdq_module(module, sample_input)
52875308
self.lower_module_and_test_output(qdq_module, sample_input)
52885309

5310+
def test_qnn_backend_reflection_pad3d(self):
5311+
test_comb = [
5312+
{
5313+
QCOM_MODULE: [
5314+
ReflectionPad3d(), # noqa: F405
5315+
ReflectionPad3dAsymmetric(), # noqa: F405
5316+
],
5317+
QCOM_SAMPLE_INPUTS: [(torch.randn(1, 3, 6, 8, 8),)],
5318+
},
5319+
]
5320+
5321+
index = 0
5322+
for comb in test_comb:
5323+
for module in comb[QCOM_MODULE]:
5324+
for sample_input in comb[QCOM_SAMPLE_INPUTS]:
5325+
with self.subTest(i=index):
5326+
index += 1
5327+
qdq_module = self.get_qdq_module(module, sample_input)
5328+
self.lower_module_and_test_output(qdq_module, sample_input)
5329+
52895330
def test_qnn_backend_relu(self):
52905331
module = Relu() # noqa: F405
52915332
sample_input = (torch.randn([2, 5, 1, 3]),)

0 commit comments

Comments
 (0)