Skip to content

Commit 2198973

Browse files
authored
Update ReplaceConvWithChannelLastConvPass and MakeSliceAndCatDimOutermostPass to correctly set modified bit
Differential Revision: D87880891 Pull Request resolved: pytorch#16185
1 parent 306cefc commit 2198973

2 files changed

Lines changed: 238 additions & 104 deletions

File tree

‎backends/cadence/aot/replace_ops.py‎

Lines changed: 161 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@
3939
)
4040
from executorch.exir.dialects._ops import ops as exir_ops
4141
from executorch.exir.dialects.edge._ops import EdgeOpOverload
42-
from executorch.exir.pass_base import ExportPass, NodeMetadata, PassResult, ProxyValue
43-
from torch.fx.node import Argument
42+
from executorch.exir.pass_base import ExportPass, PassResult
4443

4544
# A map to represent ops that:
4645
# (a) are functionally equivalent; and
@@ -1022,131 +1021,198 @@ def canonicalize_transposed_dim(dim: int, shape: Sequence[int]) -> int:
10221021
return dim
10231022

10241023

1025-
class ExportPassWithTransposeHelper(ExportPass):
1026-
def transpose_dims(
1027-
self: ExportPass, proxy: ProxyValue, meta: NodeMetadata, dim0: int, dim1: int
1028-
) -> ProxyValue:
1029-
"""Helper function to transpose dims of a `proxy` with given `meta`."""
1030-
shape = proxy.data.shape
1024+
@register_cadence_pass(CadencePassAttribute(opt_level=3))
1025+
class ReplaceConvWithChannelLastConvPass(RemoveOrReplacePassInterface):
1026+
"""
1027+
Replace NCHW convolutions with NHWC (channel-last) convolutions by adding
1028+
transpose operations before and after the convolution.
1029+
"""
1030+
1031+
@property
1032+
def targets(self) -> list[EdgeOpOverload]:
1033+
return [
1034+
exir_ops.edge.cadence.conv1d.default,
1035+
exir_ops.edge.cadence.conv2d.default,
1036+
exir_ops.edge.cadence.conv3d.default,
1037+
exir_ops.edge.cadence.quantized_conv2d_nchw.per_tensor,
1038+
]
1039+
1040+
def _transpose_dims(
1041+
self, graph: torch.fx.Graph, node: torch.fx.Node, dim0: int, dim1: int
1042+
) -> torch.fx.Node:
1043+
"""Helper function to transpose dims of a node."""
1044+
shape = node.meta["val"].shape
10311045
dim0, dim1 = (
10321046
canonicalize_transposed_dim(dim0, shape),
10331047
canonicalize_transposed_dim(dim1, shape),
10341048
)
10351049
dim0, dim1 = min(dim0, dim1), max(dim0, dim1)
1036-
return super().call_operator(
1037-
exir_ops.edge.aten.transpose_copy.int, (proxy, dim0, dim1), {}, meta
1050+
transpose_node = graph.call_function(
1051+
exir_ops.edge.aten.transpose_copy.int, (node, dim0, dim1), {}
10381052
)
1039-
1040-
1041-
@register_cadence_pass(CadencePassAttribute(opt_level=3))
1042-
class ReplaceConvWithChannelLastConvPass(ExportPassWithTransposeHelper):
1043-
def change_nchw_to_nhwc(self, proxy: ProxyValue, meta: NodeMetadata) -> ProxyValue:
1044-
shape = proxy.to_tensor().shape
1053+
transpose_node.meta = node.meta
1054+
return transpose_node
1055+
1056+
def _change_nchw_to_nhwc(
1057+
self, graph: torch.fx.Graph, node: torch.fx.Node
1058+
) -> torch.fx.Node:
1059+
"""Convert NCHW format to NHWC format."""
1060+
shape = node.meta["val"].shape
10451061
if len(shape) == 3:
1046-
return self.transpose_dims(proxy, meta, 1, -1)
1062+
return self._transpose_dims(graph, node, 1, -1)
10471063
indices = list(range(len(shape)))
10481064
permute_indices = [indices[0]] + indices[2:] + [indices[1]]
1049-
return super().call_operator(
1050-
exir_ops.edge.aten.permute_copy.default, (proxy, permute_indices), {}, meta
1065+
permute_node = graph.call_function(
1066+
exir_ops.edge.aten.permute_copy.default, (node, permute_indices), {}
10511067
)
1052-
1053-
def change_nhwc_to_nchw(self, proxy: ProxyValue, meta: NodeMetadata) -> ProxyValue:
1054-
shape = proxy.to_tensor().shape
1068+
permute_node.meta = node.meta
1069+
return permute_node
1070+
1071+
def _change_nhwc_to_nchw(
1072+
self, graph: torch.fx.Graph, node: torch.fx.Node
1073+
) -> torch.fx.Node:
1074+
"""Convert NHWC format to NCHW format."""
1075+
shape = node.meta["val"].shape
10551076
if len(shape) == 3:
1056-
return self.transpose_dims(proxy, meta, 1, -1)
1077+
return self._transpose_dims(graph, node, 1, -1)
10571078
indices = list(range(len(shape)))
10581079
permute_indices = [indices[0], indices[-1]] + indices[1:-1]
1059-
return super().call_operator(
1060-
exir_ops.edge.aten.permute_copy.default, (proxy, permute_indices), {}, meta
1080+
permute_node = graph.call_function(
1081+
exir_ops.edge.aten.permute_copy.default, (node, permute_indices), {}
10611082
)
1083+
permute_node.meta = node.meta
1084+
return permute_node
10621085

1063-
def call_operator(
1064-
self,
1065-
op,
1066-
args: tuple[Argument, ...],
1067-
kwargs: dict[str, Argument],
1068-
meta: NodeMetadata,
1069-
) -> ProxyValue:
1070-
if op not in {
1071-
exir_ops.edge.cadence.conv1d.default,
1072-
exir_ops.edge.cadence.conv2d.default,
1073-
exir_ops.edge.cadence.conv3d.default,
1074-
exir_ops.edge.cadence.quantized_conv2d_nchw.per_tensor,
1075-
}:
1076-
return super().call_operator(op, args, kwargs, meta)
1077-
1078-
quantized_op = op == exir_ops.edge.cadence.quantized_conv2d_nchw.per_tensor
1086+
def maybe_remove_or_replace(self, node: torch.fx.Node) -> bool:
1087+
assert isinstance(node.target, EdgeOpOverload)
1088+
quantized_op = (
1089+
node.target == exir_ops.edge.cadence.quantized_conv2d_nchw.per_tensor
1090+
)
10791091

1080-
if not quantized_op and len(args) == 8 and args[-1] is True:
1081-
# Already in NHWC layout.
1082-
return super().call_operator(op, args, kwargs, meta)
1092+
# Check if already in NHWC layout
1093+
if not quantized_op and len(node.args) == 8 and node.args[-1] is True:
1094+
return False
10831095

1096+
# Determine the new op target
10841097
if quantized_op:
10851098
new_op = exir_ops.edge.cadence.quantized_conv2d_nhwc.per_tensor
10861099
else:
1087-
# Determine if 1D or 2D convolution based on op
1088-
new_op = op
1100+
new_op = node.target
10891101

1090-
input_proxy = cast(ProxyValue, args[0])
1091-
weight_proxy = cast(ProxyValue, args[1])
1092-
input_proxy = self.change_nchw_to_nhwc(input_proxy, meta)
1093-
weight_proxy = self.change_nchw_to_nhwc(weight_proxy, meta)
1102+
graph = node.graph
10941103

1095-
# Non-quantized ops still need to set the last optional argument to True.
1096-
channel_last_arg = [] if quantized_op else [True]
1104+
# Get input and weight nodes
1105+
input_node = cast(torch.fx.Node, node.args[0])
1106+
weight_node = cast(torch.fx.Node, node.args[1])
10971107

1098-
new_args = (
1099-
# Transposed input/weights.
1100-
(input_proxy, weight_proxy)
1101-
# All other args (bias, quant params, etc)
1102-
+ tuple(args[2:])
1103-
+ tuple(channel_last_arg)
1104-
)
1105-
output_proxy = super().call_operator(new_op, new_args, kwargs, meta)
1106-
nchw_proxy = self.change_nhwc_to_nchw(output_proxy, meta)
1107-
return nchw_proxy
1108+
# Insert transpose operations before the node
1109+
with graph.inserting_before(node):
1110+
# Convert input from NCHW to NHWC
1111+
input_nhwc = self._change_nchw_to_nhwc(graph, input_node)
1112+
# Convert weight from NCHW to NHWC
1113+
weight_nhwc = self._change_nchw_to_nhwc(graph, weight_node)
1114+
1115+
# Non-quantized ops need to set the last optional argument to True
1116+
channel_last_arg = [] if quantized_op else [True]
1117+
1118+
# Create new args with transposed input/weights
1119+
new_args = (
1120+
(input_nhwc, weight_nhwc)
1121+
+ tuple(node.args[2:])
1122+
+ tuple(channel_last_arg)
1123+
)
1124+
1125+
# Create the new conv operation
1126+
new_conv = graph.call_function(new_op, new_args, node.kwargs)
1127+
new_conv.meta = node.meta
1128+
1129+
# Convert output back from NHWC to NCHW
1130+
nchw_output = self._change_nhwc_to_nchw(graph, new_conv)
1131+
1132+
# Replace all uses with the final output
1133+
node.replace_all_uses_with(nchw_output)
1134+
return True
11081135

11091136

11101137
@register_cadence_pass(CadencePassAttribute(opt_level=3))
1111-
class MakeSliceAndCatDimOutermostPass(ExportPassWithTransposeHelper):
1112-
def call_operator(
1113-
self,
1114-
op,
1115-
args: tuple[Argument, ...],
1116-
kwargs: dict[str, Argument],
1117-
meta: NodeMetadata,
1118-
) -> ProxyValue:
1119-
if op not in {
1138+
class MakeSliceAndCatDimOutermostPass(RemoveOrReplacePassInterface):
1139+
"""
1140+
Make the slice/cat dimension the outermost dimension by adding transpose
1141+
operations before and after the slice/cat operation.
1142+
"""
1143+
1144+
@property
1145+
def targets(self) -> list[EdgeOpOverload]:
1146+
return [
11201147
exir_ops.edge.aten.cat.default,
11211148
exir_ops.edge.aten.slice_copy.Tensor,
1122-
}:
1123-
return super().call_operator(op, args, kwargs, meta)
1124-
dim = cast(int, args[1]) if len(args) > 1 else 0
1125-
output_shape = meta["val"].shape
1149+
]
1150+
1151+
def _transpose_dims(
1152+
self, graph: torch.fx.Graph, node: torch.fx.Node, dim0: int, dim1: int
1153+
) -> torch.fx.Node:
1154+
"""Helper function to transpose dims of a node."""
1155+
shape = node.meta["val"].shape
1156+
dim0, dim1 = (
1157+
canonicalize_transposed_dim(dim0, shape),
1158+
canonicalize_transposed_dim(dim1, shape),
1159+
)
1160+
dim0, dim1 = min(dim0, dim1), max(dim0, dim1)
1161+
transpose_node = graph.call_function(
1162+
exir_ops.edge.aten.transpose_copy.int, (node, dim0, dim1), {}
1163+
)
1164+
transpose_node.meta = node.meta
1165+
return transpose_node
1166+
1167+
def maybe_remove_or_replace(self, node: torch.fx.Node) -> bool:
1168+
# Get the dimension argument
1169+
dim = cast(int, node.args[1]) if len(node.args) > 1 else 0
1170+
output_shape = node.meta["val"].shape
1171+
1172+
# Canonicalize dim to be positive
11261173
if dim < 0:
1127-
# Keep dim positive.
11281174
dim += len(output_shape)
11291175

1176+
# Not needed if dim is already outermost or all dims before it are 1
11301177
if dim == 0 or math.prod(output_shape[:dim]) == 1:
1131-
# Not needed if dim is already outermost or all dims before it are 1.
1132-
return super().call_operator(op, (args[0], dim) + args[2:], kwargs, meta)
1133-
1134-
if op == exir_ops.edge.aten.slice_copy.Tensor:
1135-
# Transpose -> slice.
1136-
slice_args = (
1137-
self.transpose_dims(cast(ProxyValue, args[0]), meta, dim, 0),
1138-
0,
1139-
) + args[2:]
1140-
new_op = super().call_operator(op, slice_args, kwargs, meta)
1141-
else:
1142-
# (Transpose input0, Transpose input1, ...) -> cat.
1143-
cat_in_tensors = [
1144-
self.transpose_dims(t, meta, dim, 0)
1145-
for t in cast(list[ProxyValue], args[0])
1146-
]
1147-
new_op = super().call_operator(op, (cat_in_tensors, 0), kwargs, meta)
1148-
# slice/cat -> transpose.
1149-
return self.transpose_dims(new_op, meta, 0, dim)
1178+
return False
1179+
1180+
graph = node.graph
1181+
1182+
with graph.inserting_before(node):
1183+
if node.target == exir_ops.edge.aten.slice_copy.Tensor:
1184+
# Transpose input -> slice with dim=0 -> transpose back
1185+
input_node = cast(torch.fx.Node, node.args[0])
1186+
transposed_input = self._transpose_dims(graph, input_node, dim, 0)
1187+
1188+
# Create slice operation with dim=0
1189+
slice_args = (transposed_input, 0) + node.args[2:]
1190+
sliced = graph.call_function(
1191+
exir_ops.edge.aten.slice_copy.Tensor, slice_args, node.kwargs
1192+
)
1193+
sliced.meta = node.meta
1194+
1195+
# Transpose back
1196+
result = self._transpose_dims(graph, sliced, 0, dim)
1197+
else:
1198+
# Cat operation: transpose all inputs -> cat with dim=0 -> transpose back
1199+
cat_inputs = cast(list[torch.fx.Node], node.args[0])
1200+
transposed_inputs = [
1201+
self._transpose_dims(graph, t, dim, 0) for t in cat_inputs
1202+
]
1203+
1204+
# Create cat operation with dim=0
1205+
catted = graph.call_function(
1206+
exir_ops.edge.aten.cat.default, (transposed_inputs, 0), node.kwargs
1207+
)
1208+
catted.meta = node.meta
1209+
1210+
# Transpose back
1211+
result = self._transpose_dims(graph, catted, 0, dim)
1212+
1213+
# Replace all uses with the final result
1214+
node.replace_all_uses_with(result)
1215+
return True
11501216

11511217

11521218
@register_cadence_pass(CadencePassAttribute(opt_level=2))

0 commit comments

Comments
 (0)