Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions backends/xnnpack/_passes/remove_redundant_copy_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ def _try_remove_quantized_redundant_to_copy(self, node, graph):
"""
Try to remove redundant to_copy operations in quantized graphs with pattern dq1 -> to_copy1 -> q1 -> dq2 -> to_copy2 -> q2
"""
if is_dtype_cast(node):
return False

# Check if this to_copy is followed by a quantize node
if len(node.users) != 1:
return False
Expand All @@ -97,6 +100,7 @@ def _try_remove_quantized_redundant_to_copy(self, node, graph):
prev_to_copy = prev_q_node.args[0]
if (
prev_to_copy.target == exir_ops.edge.aten._to_copy.default
and not is_dtype_cast(prev_to_copy)
and ChannelsLastTaggedReshapePass.is_nchw_node(prev_to_copy)
!= ChannelsLastTaggedReshapePass.is_nchw_node(node)
and len(prev_to_copy.users) == 1
Expand Down
2 changes: 2 additions & 0 deletions backends/xnnpack/partition/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
SquareRootConfig,
SubConfig,
TanhConfig,
ToCopyConfig,
ToDimOrderCopyConfig,
UnsqueezeCopyConfig,
UpsampleBilinear2dConfig,
Expand Down Expand Up @@ -115,6 +116,7 @@
ReciprocalSquareRootConfig,
ReLUConfig,
TanhConfig,
ToCopyConfig,
ToDimOrderCopyConfig,
SigmoidConfig,
SiluConfig,
Expand Down
33 changes: 33 additions & 0 deletions backends/xnnpack/partition/config/generic_node_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from executorch.backends.xnnpack.utils.utils import (
get_input_node,
get_param_tensor,
is_dtype_cast,
is_foldable_broadcast_operand,
is_param_node,
normalize_mean_dims,
Expand Down Expand Up @@ -705,10 +706,42 @@ def check_constraints(self, node: torch.fx.Node, ep: ExportedProgram) -> bool:

return True

def get_node_and_deps(
self, node: torch.fx.Node, ep: ExportedProgram
) -> List[torch.fx.Node]:
# A cast is serialized as a convert without quantization parameters, so
# a dq before it and a q after it stay explicit nodes.
if is_dtype_cast(node):
return [node]
return super().get_node_and_deps(node, ep)

def supported_precision_types(self) -> List[ConfigPrecisionType]:
return [ConfigPrecisionType.FP32, ConfigPrecisionType.STATIC_QUANT]


class ToCopyConfig(ToDimOrderCopyConfig):
"""
Programs exported with skip_dim_order keep aten._to_copy for every copy.
Only dtype casts are partitioned, under the same dtype rules as
ToDimOrderCopyConfig: inside the delegate, ChannelsLastTaggedReshapePass,
RemoveRedundantCopyPass and ToCopyVisitor read any other _to_copy as an
NCHW <-> NHWC layout copy.
"""

target_name = "_to_copy.default"

def check_constraints(self, node: torch.fx.Node, ep: ExportedProgram) -> bool:
if not is_dtype_cast(node):
why(node, reason="only dtype casts are supported")
return False
return super().check_constraints(node, ep)

def supported_precision_types(self) -> List[ConfigPrecisionType]:
# A cast never folds the q/dq around it (see get_node_and_deps), so
# static quantization adds nothing.
return [ConfigPrecisionType.FP32]


class MeanDimConfig(GenericNodePartitionerConfig):
target_name = "mean.dim"
supports_bf16 = False
Expand Down
Loading