Skip to content

Commit 5249a13

Browse files
authored
Fix remaining CompileMode test regressions (pytorch#21801)
Differential Revision: D115780184 Pull Request resolved: pytorch#21801
1 parent c461421 commit 5249a13

2 files changed

Lines changed: 29 additions & 52 deletions

File tree

‎backends/cadence/aot/replace_ops.py‎

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,9 +1001,6 @@ class ReplaceConvWithChannelLastConvPass(RemoveOrReplacePassInterface):
10011001
@property
10021002
def targets(self) -> list[EdgeOpOverload]:
10031003
return [
1004-
exir_ops.edge.cadence.conv1d.default,
1005-
exir_ops.edge.cadence.conv2d.default,
1006-
exir_ops.edge.cadence.conv3d.default,
10071004
exir_ops.edge.cadence.quantized_conv1d_ncl.per_tensor,
10081005
exir_ops.edge.cadence.quantized_depthwise_conv1d_ncl.per_tensor,
10091006
exir_ops.edge.cadence.quantized_conv2d_nchw.per_tensor,
@@ -1088,38 +1085,24 @@ def _change_depthwise_weight_to_hwc(
10881085

10891086
def maybe_remove_or_replace(self, node: torch.fx.Node) -> bool:
10901087
assert isinstance(node.target, EdgeOpOverload)
1091-
quantized_op = node.target in {
1092-
exir_ops.edge.cadence.quantized_conv1d_ncl.per_tensor,
1093-
exir_ops.edge.cadence.quantized_depthwise_conv1d_ncl.per_tensor,
1094-
exir_ops.edge.cadence.quantized_conv2d_nchw.per_tensor,
1095-
}
1096-
1097-
# Check if already in NHWC/NLC layout
1098-
if not quantized_op and len(node.args) == 8 and node.args[-1] is True:
1099-
return False
11001088

11011089
# Get input shape to determine if it's 1D or 2D
11021090
input_node = get_arg(node, "input", torch.fx.Node)
11031091
input_shape = input_node.meta["val"].shape
11041092
is_2d = len(input_shape) == 4
11051093

11061094
# Determine the new op target
1107-
if quantized_op:
1108-
if is_2d:
1109-
new_op = exir_ops.edge.cadence.quantized_conv2d_nhwc.per_tensor
1110-
else:
1111-
assert len(input_shape) == 3
1112-
if (
1113-
node.target
1114-
== exir_ops.edge.cadence.quantized_depthwise_conv1d_ncl.per_tensor
1115-
):
1116-
new_op = (
1117-
exir_ops.edge.cadence.quantized_depthwise_conv1d_nlc.per_tensor
1118-
)
1119-
else:
1120-
new_op = exir_ops.edge.cadence.quantized_conv1d_nlc.per_tensor
1095+
if is_2d:
1096+
new_op = exir_ops.edge.cadence.quantized_conv2d_nhwc.per_tensor
11211097
else:
1122-
new_op = node.target
1098+
assert len(input_shape) == 3
1099+
if (
1100+
node.target
1101+
== exir_ops.edge.cadence.quantized_depthwise_conv1d_ncl.per_tensor
1102+
):
1103+
new_op = exir_ops.edge.cadence.quantized_depthwise_conv1d_nlc.per_tensor
1104+
else:
1105+
new_op = exir_ops.edge.cadence.quantized_conv1d_nlc.per_tensor
11231106

11241107
graph = node.graph
11251108

@@ -1148,15 +1131,8 @@ def maybe_remove_or_replace(self, node: torch.fx.Node) -> bool:
11481131
# For regular conv: [OC, IC, KH, KW] -> [OC, KH, KW, IC]
11491132
weight_nhwc = self._change_nchw_to_nhwc(graph, weight_node)
11501133

1151-
# Non-quantized ops need to set the last optional argument to True
1152-
channel_last_arg = [] if quantized_op else [True]
1153-
11541134
# Create new args with transposed input/weights
1155-
new_args = (
1156-
(input_nhwc, weight_nhwc)
1157-
+ tuple(node.args[2:])
1158-
+ tuple(channel_last_arg)
1159-
)
1135+
new_args = (input_nhwc, weight_nhwc) + tuple(node.args[2:])
11601136

11611137
# Create the new conv operation
11621138
new_conv = graph.call_function(new_op, new_args, node.kwargs)

‎backends/cadence/aot/tests/test_replace_ops_passes.py‎

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2230,31 +2230,32 @@ def create_conv1d_graphmodule(
22302230
args=args,
22312231
)
22322232

2233-
def create_convolution_graph_module(
2234-
self, channels_last: Optional[bool] = None
2235-
) -> torch.fx.GraphModule:
2236-
"""Helper to create a convolution node.
2237-
2238-
convolution(
2239-
Tensor input, Tensor weight, Tensor bias, int[] stride, SymInt[] padding,"
2240-
int[] dilation, int groups, bool channel_last=False) -> (Tensor Y)"
2241-
"""
2242-
if channels_last:
2243-
x = torch.randn(1, 224, 224, 3)
2244-
w = torch.randn(16, 16, 16, 3)
2245-
else:
2246-
x = torch.randn(1, 3, 224, 224)
2247-
w = torch.randn(16, 3, 16, 16)
2233+
def create_convolution_graph_module(self) -> torch.fx.GraphModule:
2234+
"""Helper to create an FP32 NCHW convolution node."""
2235+
x = torch.randn(1, 3, 224, 224)
2236+
w = torch.randn(16, 3, 16, 16)
22482237
b = torch.randn(16)
22492238
args = (x, w, b, (2, 2), (1, 1), (0, 0), 1)
2250-
if channels_last is not None:
2251-
args = args + (channels_last,)
22522239
return single_op_builder(
22532240
placeholders=(x, w, b),
22542241
op=exir_ops.edge.cadence.conv2d.default,
22552242
args=args,
22562243
)
22572244

2245+
def test_fp32_convolution_is_not_replaced(self) -> None:
2246+
gm = self.create_convolution_graph_module()
2247+
2248+
result = ReplaceConvWithChannelLastConvPass().call(gm)
2249+
2250+
self.assertFalse(result.modified)
2251+
self.assertEqual(
2252+
count_node(result.graph_module, exir_ops.edge.cadence.conv2d.default), 1
2253+
)
2254+
self.assertEqual(
2255+
count_node(result.graph_module, exir_ops.edge.aten.permute_copy.default),
2256+
0,
2257+
)
2258+
22582259
def create_quantized_convolution_graph_module(
22592260
self, channels_last: Optional[bool] = None
22602261
) -> tuple[tuple[torch.Tensor, ...], torch.fx.GraphModule]:

0 commit comments

Comments
 (0)