66import numpy as np
77
88from executorch .backends .nxp .backend .edge_helper import (
9+ get_non_qdq_users ,
910 input_tensor ,
1011 output_tensor ,
1112 tensor_rank ,
1213)
1314from executorch .backends .nxp .backend .ir .converter import quantization_utils
1415from executorch .backends .nxp .backend .ir .converter .conversion .common import OpsList
16+ from executorch .backends .nxp .backend .ir .converter .conversion .translator import (
17+ apply_permutation_to ,
18+ create_channels_first_to_channels_last_permutation ,
19+ create_channels_last_to_channels_first_permutation ,
20+ )
1521from executorch .backends .nxp .backend .ir .converter .node_converter import (
1622 CustomDelegationOptions ,
1723 is_not_qdq_node ,
2329from executorch .backends .nxp .backend .ir .tflite_generator .builtin_options import (
2430 reshape_options ,
2531)
32+ from executorch .backends .nxp .backend .neutron_operator_support import (
33+ transposition_is_supported_on_neutron ,
34+ )
35+ from executorch .backends .nxp .backend .neutron_target_spec import NeutronTargetSpec
36+ from executorch .backends .nxp .backend .node_format import NXP_NODE_FORMAT
37+ from executorch .exir .dialects ._ops import ops as exir_ops
2638from torch .fx import Node
2739from torch .fx .passes .infra .partitioner import Partition
2840from torch .nn import Parameter
@@ -53,6 +65,8 @@ def supports_partitioning_result(
5365 node : Node ,
5466 partition_list : list [Partition ],
5567 custom_delegation_options : CustomDelegationOptions ,
68+ neutron_target_spec : NeutronTargetSpec ,
69+ parameters_mapping : dict [str , Parameter ],
5670 ):
5771 view_copy_partitions = [
5872 partition for partition in partition_list if node in partition .nodes
@@ -66,6 +80,76 @@ def supports_partitioning_result(
6680 # The `view_copy` cannot be the only node in a partition.
6781 return False
6882
83+ input_format = node .args [0 ].meta [NXP_NODE_FORMAT ]
84+ output_format = node .meta [NXP_NODE_FORMAT ]
85+ input_shape = list (node .args [0 ].meta ["val" ].shape )
86+ output_shape = list (node .meta ["val" ].shape )
87+ to_nchw_perm = create_channels_last_to_channels_first_permutation (
88+ len (input_shape ), True
89+ )
90+ to_nhwc_perm = create_channels_first_to_channels_last_permutation (
91+ len (output_shape ), True
92+ )
93+ channels_last_input_shape = apply_permutation_to (
94+ input_shape ,
95+ create_channels_first_to_channels_last_permutation (len (input_shape ), True ),
96+ )
97+
98+ if input_format .is_channels_first () and (not output_format .is_channels_first ()):
99+ # The `view_copy` removes node format. Conversion will require the addition of a `Transpose` operator.
100+ # Make sure the `Transpose` will be supported.
101+
102+ if not transposition_is_supported_on_neutron (
103+ channels_last_input_shape , to_nchw_perm , neutron_target_spec
104+ ):
105+ # The `Transpose` would have to be removed by the `PermuteFullyConnectedWeightsAfterReshape` pass.
106+ # Make sure it will be applied.
107+ users = get_non_qdq_users (node )
108+ if len (users ) != 1 or (linear_node := users [0 ]).target not in [
109+ exir_ops .edge .aten .addmm .default ,
110+ exir_ops .edge .aten .mm .default ,
111+ ]:
112+ return False
113+
114+ if linear_node not in view_copy_partitions [0 ].nodes :
115+ # The `mm` / `addmm` node will not be delegated within this partition.
116+ return False
117+
118+ # Make sure the specific requirements of the `PermuteFullyConnectedWeightsAfterReshape` are satisfied.
119+ weights_index = (
120+ 2 if linear_node .target == exir_ops .edge .aten .addmm .default else 1
121+ )
122+ if not (
123+ input_shape [0 ] == output_shape [0 ] # Preserve batch.
124+ and len (output_shape ) == 2
125+ and output_shape [1 ]
126+ == linear_node .args [weights_index ].meta ["val" ].shape [0 ]
127+ ):
128+ return False
129+
130+ elif (
131+ not input_format .is_channels_first ()
132+ ) and output_format .is_channels_first ():
133+ # The `view_copy` introduces node format. Conversion will require the addition of a `Transpose` operator.
134+ # Make sure the `Transpose` will be supported.
135+ if not transposition_is_supported_on_neutron (
136+ output_shape , to_nhwc_perm , neutron_target_spec
137+ ):
138+ return False
139+
140+ elif input_format .is_channels_first () and output_format .is_channels_first ():
141+ # The `view_copy` works with the channels first format, so both tensors will end up being transposed.
142+ # Make sure these transpositions are supported.
143+ if not (
144+ transposition_is_supported_on_neutron (
145+ channels_last_input_shape , to_nchw_perm , neutron_target_spec
146+ )
147+ and transposition_is_supported_on_neutron (
148+ output_shape , to_nhwc_perm , neutron_target_spec
149+ )
150+ ):
151+ return False
152+
69153 return True
70154
71155 @staticmethod
0 commit comments