|
12 | 12 | from executorch.backends.arm._passes.arm_pass_utils import ( |
13 | 13 | create_node, |
14 | 14 | get_first_fake_tensor, |
| 15 | + insert_q_dq_pair, |
15 | 16 | ) |
16 | | -from executorch.backends.arm.tosa_quant_utils import dq_op |
| 17 | +from executorch.backends.arm.tosa_quant_utils import dq_op, q_op |
17 | 18 | from executorch.backends.arm.tosa_utils import is_consumer_node_depthwise_conv2d |
18 | 19 | from executorch.exir.dialects._ops import ops as exir_ops |
19 | 20 | from executorch.exir.pass_base import ExportPass, PassResult |
@@ -79,37 +80,89 @@ def is_weight_node_for_depthwise_conv2d(self, node: torch.fx.Node): |
79 | 80 |
|
80 | 81 | return False |
81 | 82 |
|
| 83 | + def insert_input_transpose(self, node, input_node, graph_module): |
| 84 | + quantize = input_node.target == dq_op |
| 85 | + q_params = input_node.args[1:] if quantize else None |
| 86 | + with graph_module.graph.inserting_before(node): |
| 87 | + permute_node = create_node( |
| 88 | + graph_module.graph, |
| 89 | + torch.ops.passthrough_to_tosa._transpose, |
| 90 | + args=(input_node, list(self.NHWC_inverse_order)), |
| 91 | + quantize=quantize, |
| 92 | + q_params=q_params, |
| 93 | + ) |
| 94 | + node.replace_input_with(input_node, permute_node) |
| 95 | + |
| 96 | + permute_node.meta["tosa_dim_order"] = tuple( |
| 97 | + range(len(input_node.meta["val"].size())) |
| 98 | + ) |
| 99 | + |
| 100 | + def insert_output_transpose(self, node, graph_module): |
| 101 | + with graph_module.graph.inserting_after(node): |
| 102 | + permute_node = create_node( |
| 103 | + graph_module.graph, |
| 104 | + torch.ops.passthrough_to_tosa._transpose, |
| 105 | + args=(node, list(self.NHWC_order)), |
| 106 | + ) |
| 107 | + permute_node.meta["tosa_dim_order"] = self.NHWC_order |
| 108 | + node.meta["tosa_dim_order"] = (0, 1, 2, 3) |
| 109 | + users = [user for user in node.users if user != permute_node] |
| 110 | + for user in users: |
| 111 | + user.replace_input_with(node, permute_node) |
| 112 | + |
| 113 | + quantize = node.args[0] == q_op |
| 114 | + if quantize: |
| 115 | + q_params = node.args[0].args[1:] |
| 116 | + insert_q_dq_pair(graph_module.graph, node, q_params) |
| 117 | + |
82 | 118 | def insert_tosa_transposes(self, graph_module: torch.fx.GraphModule): |
| 119 | + """ |
| 120 | + Reshape operations are not equivalent in NCHW and NHWC. |
| 121 | + To get around this, transposes need to be added if the previous or new shape |
| 122 | + fulfil the following condition: |
| 123 | + C > 1 and (H or W > 1) |
| 124 | +
|
| 125 | + This is relevant for the following operations; |
| 126 | + squeeze: 4D -> 3D |
| 127 | + unsqueeze: <4D -> 4D |
| 128 | + view: <4D -> 4D |
| 129 | + view: 4D -> <4D |
| 130 | + view: 4D -> 4D |
| 131 | + """ |
| 132 | + |
| 133 | + def transpose_condition(shape): |
| 134 | + if len(shape) != 4: |
| 135 | + return False |
| 136 | + C = shape[1] |
| 137 | + H = shape[2] |
| 138 | + W = shape[3] |
| 139 | + return C > 1 and (H > 1 or W > 1) |
| 140 | + |
83 | 141 | for node in graph_module.graph.nodes: |
84 | 142 | if node.op != "call_function": |
85 | 143 | continue |
86 | 144 | if node.target == exir_ops.edge.aten.squeeze_copy.dims: |
87 | 145 | input_node = node.args[0] |
88 | | - if input_node.meta["val"].dim() == 4: |
89 | | - with graph_module.graph.inserting_before(node): |
90 | | - permute_node = create_node( |
91 | | - graph_module.graph, |
92 | | - torch.ops.passthrough_to_tosa._transpose, |
93 | | - args=(input_node, list(self.NHWC_inverse_order)), |
94 | | - ) |
95 | | - permute_node.meta["tosa_dim_order"] = tuple( |
96 | | - range(len(input_node.meta["val"].size())) |
97 | | - ) |
98 | | - node.replace_input_with(input_node, permute_node) |
99 | | - |
100 | | - if node.target == exir_ops.edge.aten.unsqueeze_copy.default: |
101 | | - if node.meta["val"].dim() == 4: |
102 | | - with graph_module.graph.inserting_after(node): |
103 | | - permute_node = create_node( |
104 | | - graph_module.graph, |
105 | | - torch.ops.passthrough_to_tosa._transpose, |
106 | | - args=(node, list(self.NHWC_order)), |
107 | | - ) |
108 | | - permute_node.meta["tosa_dim_order"] = self.NHWC_order |
109 | | - node.meta["tosa_dim_order"] = (0, 1, 2, 3) |
110 | | - users = [user for user in node.users if user != permute_node] |
111 | | - for user in users: |
112 | | - user.replace_input_with(node, permute_node) |
| 146 | + input_shape = input_node.meta["val"].shape |
| 147 | + if transpose_condition(input_shape): |
| 148 | + self.insert_input_transpose(node, input_node, graph_module) |
| 149 | + |
| 150 | + elif node.target == exir_ops.edge.aten.unsqueeze_copy.default: |
| 151 | + output_shape = node.meta["val"].shape |
| 152 | + if transpose_condition(output_shape): |
| 153 | + self.insert_output_transpose(node, graph_module) |
| 154 | + |
| 155 | + elif node.target == exir_ops.edge.aten.view_copy.default: |
| 156 | + input_node = node.args[0] |
| 157 | + |
| 158 | + old_shape = input_node.meta["val"].shape |
| 159 | + new_shape = node.meta["val"].shape |
| 160 | + |
| 161 | + if transpose_condition(old_shape): |
| 162 | + self.insert_input_transpose(node, input_node, graph_module) |
| 163 | + |
| 164 | + if transpose_condition(new_shape): |
| 165 | + self.insert_output_transpose(node, graph_module) |
113 | 166 |
|
114 | 167 | def call(self, graph_module: torch.fx.GraphModule): |
115 | 168 | for node in graph_module.graph.nodes: |
|
0 commit comments