|
| 1 | +# Copyright 2026 NXP |
| 2 | +# |
| 3 | +# This source code is licensed under the BSD-style license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | + |
| 7 | +import torch |
| 8 | + |
| 9 | +from executorch.backends.nxp.edge_passes.neutron_edge_pass import NeutronEdgePass |
| 10 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 11 | +from torch._subclasses import FakeTensor, FakeTensorMode |
| 12 | +from torch.fx import GraphModule, Node |
| 13 | +from torch.fx.passes.infra.pass_base import PassResult |
| 14 | + |
| 15 | + |
| 16 | +class ConvertReshapingNodesToViewPass(NeutronEdgePass): |
| 17 | + """Replaces: |
| 18 | + - 'aten.squeeze.default', 'aten.squeeze.dims' and 'aten.squeeze.dim' with 'aten.view_copy.default'. |
| 19 | +
|
| 20 | + x x |
| 21 | + │ │ |
| 22 | + ┌──────────────▼──────────────┐ replace with ┌───────────────▼────────────────┐ |
| 23 | + │ aten.[un]squeeze(x, dim) │ ──────────────► │ aten.view_copy.default(x, S) │ |
| 24 | + └──────────────┬──────────────┘ └───────────────┬────────────────┘ |
| 25 | + │ │ |
| 26 | + ▼ ▼ |
| 27 | + out out |
| 28 | +
|
| 29 | + - 'aten.unsqueeze.default' with 'aten.view_copy.default'. |
| 30 | +
|
| 31 | + x x |
| 32 | + │ │ |
| 33 | + ┌─────────────▼─────────────┐ replace with ┌───────────────▼────────────────┐ |
| 34 | + │ aten.unsqueeze(x, dim) │ ──────────────► │ aten.view_copy.default(x, S) │ |
| 35 | + └─────────────┬─────────────┘ └───────────────┬────────────────┘ |
| 36 | + │ │ |
| 37 | + ▼ ▼ |
| 38 | + out out |
| 39 | + """ |
| 40 | + |
| 41 | + graph_module: GraphModule |
| 42 | + |
| 43 | + @staticmethod |
| 44 | + def _is_squeeze(node_: Node) -> bool: |
| 45 | + return node_.op == "call_function" and ( |
| 46 | + node_.target == exir_ops.edge.aten.squeeze_copy.dim |
| 47 | + or node_.target == exir_ops.edge.aten.squeeze_copy.dims |
| 48 | + or node_.target == exir_ops.edge.aten.squeeze_copy.default |
| 49 | + ) |
| 50 | + |
| 51 | + @staticmethod |
| 52 | + def _is_unsqueeze(node_: Node) -> bool: |
| 53 | + return ( |
| 54 | + node_.op == "call_function" |
| 55 | + and node_.target == exir_ops.edge.aten.unsqueeze_copy.default |
| 56 | + ) |
| 57 | + |
| 58 | + def _create_view_copy_node(self, *view_args) -> Node: |
| 59 | + view_target = exir_ops.edge.aten.view_copy.default |
| 60 | + view_node = self.graph_module.graph.call_function(view_target, view_args) |
| 61 | + |
| 62 | + view_node.meta["source_fn_stack"] = [ |
| 63 | + (view_node.name, exir_ops.edge.aten.view_copy.default) |
| 64 | + ] |
| 65 | + |
| 66 | + x_val = view_args[0].meta["val"] |
| 67 | + with FakeTensorMode() as mode: |
| 68 | + fake_input = FakeTensor.from_tensor( |
| 69 | + torch.empty(x_val.shape, dtype=x_val.dtype), mode |
| 70 | + ) |
| 71 | + output_shape = view_target(fake_input, *view_args[1:]).shape |
| 72 | + view_node.meta["val"] = FakeTensor.from_tensor( |
| 73 | + torch.empty(output_shape, dtype=x_val.dtype), mode |
| 74 | + ) |
| 75 | + |
| 76 | + return view_node |
| 77 | + |
| 78 | + def run(self, graph_module: GraphModule) -> PassResult: |
| 79 | + self.graph_module = graph_module |
| 80 | + |
| 81 | + for node in list(graph_module.graph.nodes): |
| 82 | + if not (self._is_squeeze(node) or self._is_unsqueeze(node)): |
| 83 | + continue |
| 84 | + |
| 85 | + input_node = node.all_input_nodes[0] |
| 86 | + target_shape = node.meta["val"].shape |
| 87 | + |
| 88 | + with self.graph_module.graph.inserting_after(node): |
| 89 | + view_copy_node = self._create_view_copy_node(input_node, target_shape) |
| 90 | + |
| 91 | + node.replace_all_uses_with(view_copy_node) |
| 92 | + self.graph_module.graph.erase_node(node) |
| 93 | + |
| 94 | + self.graph_module.graph.eliminate_dead_code() |
| 95 | + self.graph_module.recompile() |
| 96 | + |
| 97 | + # Return immediately to avoid traversing a modified graph. |
| 98 | + # The parent class will call this pass again. |
| 99 | + return PassResult(graph_module, True) |
| 100 | + |
| 101 | + # The graph was not modified. |
| 102 | + return PassResult(graph_module, False) |
0 commit comments