|
| 1 | +# Copyright (c) 2025 Samsung Electronics Co. LTD |
| 2 | +# All rights reserved |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import torch |
| 8 | +from executorch.exir import ExportedProgram |
| 9 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 10 | +from executorch.exir.pass_base import ExportPass, PassResult |
| 11 | +from torch._export.utils import get_param |
| 12 | + |
| 13 | + |
| 14 | +class Conv1dToConv2d(ExportPass): |
| 15 | + |
| 16 | + def __init__(self, edge_program: ExportedProgram): |
| 17 | + super().__init__() |
| 18 | + self.edge_program = edge_program |
| 19 | + |
| 20 | + def call(self, graph_module: torch.fx.GraphModule): |
| 21 | + graph = graph_module.graph |
| 22 | + node_list = list(graph.nodes) |
| 23 | + for node in node_list: |
| 24 | + if node.op == "call_function": |
| 25 | + if node.target == exir_ops.edge.aten.convolution.default: |
| 26 | + stride = list(node.args[3]) |
| 27 | + if len(stride) != 1: |
| 28 | + continue |
| 29 | + |
| 30 | + # convert 3dim weight to 4dim |
| 31 | + weight_node = node.args[1] |
| 32 | + weight_3dim = get_param(self.edge_program, weight_node) |
| 33 | + weight_4dim = torch.nn.Parameter( |
| 34 | + data=weight_3dim.data.contiguous().unsqueeze(dim=-1), |
| 35 | + requires_grad=False, |
| 36 | + ) |
| 37 | + parameter_name = ( |
| 38 | + self.edge_program.graph_signature.inputs_to_parameters[ |
| 39 | + weight_node.name |
| 40 | + ] |
| 41 | + ) |
| 42 | + self.edge_program.state_dict[parameter_name] = weight_4dim |
| 43 | + weight_node.meta["val"] = weight_node.meta["val"].data.unsqueeze( |
| 44 | + dim=-1 |
| 45 | + ) |
| 46 | + |
| 47 | + # Extend stride, padding, and dilation |
| 48 | + node.args = ( |
| 49 | + node.args[0], |
| 50 | + node.args[1], |
| 51 | + node.args[2], |
| 52 | + node.args[3] + [1], # stride |
| 53 | + node.args[4] + [0], # padding |
| 54 | + node.args[5] + [1], # dilation |
| 55 | + node.args[6], |
| 56 | + node.args[7], |
| 57 | + node.args[8], |
| 58 | + ) |
| 59 | + |
| 60 | + # unsqueeze -> conv2d -> squeeze |
| 61 | + with graph.inserting_before(node): |
| 62 | + input_node = node.args[0] |
| 63 | + unsqueeze_before = graph.create_node( |
| 64 | + "call_function", exir_ops.edge.aten.unsqueeze_copy.default |
| 65 | + ) |
| 66 | + unsqueeze_before.args = ( |
| 67 | + input_node, |
| 68 | + -1, |
| 69 | + ) |
| 70 | + node.replace_input_with(input_node, unsqueeze_before) |
| 71 | + |
| 72 | + with graph.inserting_after(node): |
| 73 | + squeeze_after = graph.create_node( |
| 74 | + "call_function", exir_ops.edge.aten.squeeze_copy.dims |
| 75 | + ) |
| 76 | + squeeze_after.args = ( |
| 77 | + node, |
| 78 | + [-1], |
| 79 | + ) |
| 80 | + original_users = [ |
| 81 | + user for user in node.users if user != squeeze_after |
| 82 | + ] |
| 83 | + for user in original_users: |
| 84 | + user.replace_input_with(node, squeeze_after) |
| 85 | + |
| 86 | + graph_module.recompile() |
| 87 | + graph_module = super().call(graph_module).graph_module |
| 88 | + return PassResult(graph_module, True) |
0 commit comments