|
| 1 | +# Copyright (c) Qualcomm Innovation Center, Inc. |
| 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 to_edge |
| 9 | +from executorch.exir.pass_base import ExportPass, PassResult |
| 10 | + |
| 11 | + |
| 12 | +class LinalgVectorNorm(torch.nn.Module): |
| 13 | + def __init__(self, exp, dim, keepdim): |
| 14 | + super().__init__() |
| 15 | + self.exp = exp |
| 16 | + self.dim = tuple(dim) if dim is not None else None |
| 17 | + self.keepdim = keepdim |
| 18 | + |
| 19 | + def forward(self, x): |
| 20 | + if self.dim is None: |
| 21 | + x = torch.flatten(x) |
| 22 | + self.dim = 0 |
| 23 | + |
| 24 | + x = torch.abs(x) |
| 25 | + x = torch.pow(x, self.exp) |
| 26 | + x = torch.sum(x, dim=self.dim, keepdim=self.keepdim) |
| 27 | + return torch.pow(x, 1.0 / self.exp) |
| 28 | + |
| 29 | + |
| 30 | +class DecomposeLinalgVectorNorm(ExportPass): |
| 31 | + """ |
| 32 | + Decompose for math equivalent op. |
| 33 | + """ |
| 34 | + |
| 35 | + def __init__(self, quantization_capture=False) -> None: |
| 36 | + super().__init__() |
| 37 | + self.quantization_capture = quantization_capture |
| 38 | + |
| 39 | + def call(self, graph_module: torch.fx.GraphModule) -> PassResult: |
| 40 | + graph = graph_module.graph |
| 41 | + for node in graph.nodes: |
| 42 | + if "linalg_vector_norm" in str(node.target): |
| 43 | + ord = node.args[1] if len(node.args) > 1 else 2.0 |
| 44 | + dim = node.args[2] if len(node.args) > 2 else None |
| 45 | + keepdim = node.args[3] if len(node.args) > 3 else False |
| 46 | + model = LinalgVectorNorm(ord, dim, keepdim) |
| 47 | + if self.quantization_capture: |
| 48 | + decomposed_module = torch.export.export( |
| 49 | + model, (node.args[0].meta["val"],) |
| 50 | + ).module() |
| 51 | + else: |
| 52 | + edge_mgr = to_edge( |
| 53 | + torch.export.export(model, (node.args[0].meta["val"],)) |
| 54 | + ) |
| 55 | + decomposed_module = edge_mgr.exported_program() |
| 56 | + |
| 57 | + with graph.inserting_before(node): |
| 58 | + # remap is used to map original node values to new node values, |
| 59 | + # which ensures that reference to nodes are correctly updated in the new graph |
| 60 | + remap = {"x": node.args[0]} |
| 61 | + |
| 62 | + for decomposed_node in decomposed_module.graph.nodes: |
| 63 | + # no need to copy existent 'output' |
| 64 | + if decomposed_node.op == "output": |
| 65 | + for user in node.users.copy(): |
| 66 | + # remap |
| 67 | + user.replace_input_with( |
| 68 | + node, |
| 69 | + remap[decomposed_node.args[0][0]], |
| 70 | + ) |
| 71 | + # no need to copy existent placeholders |
| 72 | + elif decomposed_node.op == "placeholder": |
| 73 | + # replace node map from string to graph node |
| 74 | + remap[decomposed_node] = remap.pop(decomposed_node.name) |
| 75 | + else: |
| 76 | + remap[decomposed_node] = graph.node_copy( |
| 77 | + decomposed_node, |
| 78 | + arg_transform=lambda x, remap=remap: remap[x], |
| 79 | + ) |
| 80 | + |
| 81 | + graph.erase_node(node) |
| 82 | + |
| 83 | + graph.eliminate_dead_code() |
| 84 | + graph_module.recompile() |
| 85 | + return PassResult(graph_module, True) |
0 commit comments