|
| 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.backends.samsung.utils.constants import QuantConstants |
| 9 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 10 | +from executorch.exir.pass_base import ExportPass, PassResult |
| 11 | +from executorch.exir.passes import dead_code_elimination_pass |
| 12 | +from torch.export import ExportedProgram |
| 13 | +from torch.fx import GraphModule |
| 14 | + |
| 15 | + |
| 16 | +class TransformQuantizedMaskPass(ExportPass): |
| 17 | + def __init__(self, edge_program: ExportedProgram): |
| 18 | + super().__init__() |
| 19 | + self.edge_program = edge_program |
| 20 | + |
| 21 | + def get_mask_mul(self, graph_module: GraphModule): |
| 22 | + """ |
| 23 | + Iterator for each patterns in the graph. |
| 24 | + The obj returned by iterator is the first node of the pattern. |
| 25 | + """ |
| 26 | + nodes_in_pattern = ( |
| 27 | + exir_ops.edge.quantized_decomposed.quantize_per_tensor.default, |
| 28 | + exir_ops.edge.quantized_decomposed.dequantize_per_tensor.default, |
| 29 | + exir_ops.edge.aten.sub.Tensor, |
| 30 | + exir_ops.edge.aten._to_copy.default, |
| 31 | + exir_ops.edge.aten.unsqueeze_copy.default, |
| 32 | + exir_ops.edge.aten.mul.Tensor, |
| 33 | + ) |
| 34 | + mask_node = None |
| 35 | + for node in graph_module.graph.nodes: |
| 36 | + if node.target != "attention_mask": |
| 37 | + continue |
| 38 | + else: |
| 39 | + mask_node = node |
| 40 | + break |
| 41 | + if mask_node is None: |
| 42 | + return None |
| 43 | + while node.target != exir_ops.edge.aten.mul.Tensor: |
| 44 | + find_next = False |
| 45 | + for successor in list(node.users.keys()): |
| 46 | + if successor.target in nodes_in_pattern: |
| 47 | + node = successor |
| 48 | + find_next = True |
| 49 | + break |
| 50 | + if not find_next: |
| 51 | + return None |
| 52 | + return node |
| 53 | + |
| 54 | + def transform( |
| 55 | + self, |
| 56 | + graph_module: GraphModule, |
| 57 | + ): |
| 58 | + mask_mul = self.get_mask_mul(graph_module) |
| 59 | + if mask_mul is None: |
| 60 | + return |
| 61 | + rsub_node = mask_mul.args[0] |
| 62 | + manual_mul_idx = 0 |
| 63 | + for add in list(mask_mul.users.keys()): |
| 64 | + custom_tensor_name = f"_custom_tensor_{manual_mul_idx}" |
| 65 | + div_node = add.args[0] |
| 66 | + if "quantize_attrs" not in div_node.meta: |
| 67 | + return |
| 68 | + div_quant_args = div_node.meta["quantize_attrs"] |
| 69 | + custom_tensor = torch.tensor( |
| 70 | + ( |
| 71 | + div_node.meta["quantize_attrs"][QuantConstants.QUANT_KEY.quant_min] |
| 72 | + - div_node.meta["quantize_attrs"][ |
| 73 | + QuantConstants.QUANT_KEY.zero_point |
| 74 | + ] |
| 75 | + ) |
| 76 | + * div_node.meta["quantize_attrs"][QuantConstants.QUANT_KEY.scale], |
| 77 | + dtype=torch.float32, |
| 78 | + ) |
| 79 | + graph_module.register_buffer(custom_tensor_name, custom_tensor) |
| 80 | + add.meta["quantize_attrs"] = div_quant_args |
| 81 | + with graph_module.graph.inserting_after(rsub_node): |
| 82 | + custom_attr = graph_module.graph.get_attr(custom_tensor_name) |
| 83 | + with graph_module.graph.inserting_after(custom_attr): |
| 84 | + new_mul = graph_module.graph.create_node( |
| 85 | + "call_function", |
| 86 | + exir_ops.edge.aten.mul.Tensor, |
| 87 | + (mask_mul.args[0], custom_attr), |
| 88 | + ) |
| 89 | + new_mul.meta["quantize_attrs"] = div_quant_args |
| 90 | + add.replace_input_with(mask_mul, new_mul) |
| 91 | + |
| 92 | + rsub_in = rsub_node.args[1] |
| 93 | + with graph_module.graph.inserting_before(add): |
| 94 | + new_mul = graph_module.graph.create_node( |
| 95 | + "call_function", exir_ops.edge.aten.mul.Tensor, (div_node, rsub_in) |
| 96 | + ) |
| 97 | + new_mul.meta["quantize_attrs"] = div_quant_args |
| 98 | + add.replace_input_with(div_node, new_mul) |
| 99 | + manual_mul_idx += 1 |
| 100 | + |
| 101 | + def call(self, graph_module: GraphModule): |
| 102 | + self.transform(graph_module) |
| 103 | + graph_module.recompile() |
| 104 | + dead_code_elimination_pass(graph_module) |
| 105 | + return PassResult(graph_module, True) |
0 commit comments