|
| 1 | +# Copyright 2026 Arm Limited and/or its affiliates. |
| 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 | +from typing import cast, Set, Type |
| 7 | + |
| 8 | +import torch |
| 9 | +from executorch.backends.arm._passes.arm_pass import ArmPass |
| 10 | +from executorch.backends.arm._passes.arm_pass_utils import create_node |
| 11 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 12 | +from executorch.exir.pass_base import ExportPass |
| 13 | +from torch.fx import GraphModule, Node |
| 14 | +from torch.fx.passes.infra.pass_base import PassResult |
| 15 | + |
| 16 | + |
| 17 | +class FuseConsecutiveRescalesPass(ArmPass): |
| 18 | + """Fuse consecutive RESCALE(INT32->INT8/INT16) -> |
| 19 | + RESCALE(INT8/INT16->INT32) pairs. |
| 20 | +
|
| 21 | + InsertRescaleInt32Pass wraps each add/mul/sub with input rescales |
| 22 | + (INT8/INT16->INT32) and an output rescale (INT32->INT8/INT16). When |
| 23 | + two such ops are chained (e.g., add1 -> add2), the output rescale |
| 24 | + of add1 feeds directly into an input rescale of add2, creating a |
| 25 | + redundant INT32->INT8/INT16->INT32 round-trip that loses precision. |
| 26 | +
|
| 27 | + This pass detects such pairs and either: |
| 28 | + - Removes both if the composed scale is ~1.0 and zero points match |
| 29 | + - Replaces both with a single INT32->INT32 RESCALE with composed |
| 30 | + scale |
| 31 | +
|
| 32 | + Handles multi-user R1 nodes: when R1 feeds both RESCALE and |
| 33 | + non-RESCALE users, each R1->R2 RESCALE pair is fused individually |
| 34 | + while preserving R1 for its non-RESCALE users. |
| 35 | +
|
| 36 | + """ |
| 37 | + |
| 38 | + _passes_required_after: Set[Type[ExportPass]] = set() |
| 39 | + |
| 40 | + def call(self, graph_module: GraphModule) -> PassResult: |
| 41 | + graph = graph_module.graph |
| 42 | + modified = False |
| 43 | + nodes_to_erase = [] |
| 44 | + |
| 45 | + for node in list(graph.nodes): |
| 46 | + node = cast(Node, node) |
| 47 | + if not _is_rescale(node): |
| 48 | + continue |
| 49 | + |
| 50 | + # R1 = node: output rescale (INT32 -> INT8/INT16) |
| 51 | + r1_output_dtype = node.args[1] |
| 52 | + if r1_output_dtype not in (torch.int8, torch.int16): |
| 53 | + continue |
| 54 | + |
| 55 | + r1_input = node.args[0] |
| 56 | + r1_input_zp = node.args[3] |
| 57 | + r1_output_zp = node.args[4] |
| 58 | + r1_scale = float(node.args[2][0]) |
| 59 | + |
| 60 | + # Check each user individually (handles multi-user R1) |
| 61 | + for user in list(node.users): |
| 62 | + if not _is_rescale(user): |
| 63 | + continue |
| 64 | + |
| 65 | + # R2 = user: input rescale (INT8/INT16 -> INT32) |
| 66 | + r2_output_dtype = user.args[1] |
| 67 | + if r2_output_dtype != torch.int32: |
| 68 | + continue |
| 69 | + |
| 70 | + r2_input_zp = user.args[3] |
| 71 | + |
| 72 | + # Guard: intermediate zero points must match for correct |
| 73 | + # composition. Without this, the offset term |
| 74 | + # (r1_output_zp - r2_input_zp) * r2_scale is silently lost. |
| 75 | + if r1_output_zp != r2_input_zp: |
| 76 | + continue |
| 77 | + |
| 78 | + r2_scale = float(user.args[2][0]) |
| 79 | + composed_scale = r1_scale * r2_scale |
| 80 | + r2_output_zp = user.args[4] |
| 81 | + |
| 82 | + if abs(composed_scale - 1.0) < 1e-6 and r1_input_zp == r2_output_zp: |
| 83 | + # Identity: wire R1's input directly to R2's users |
| 84 | + user.replace_all_uses_with(r1_input) |
| 85 | + nodes_to_erase.append(user) |
| 86 | + else: |
| 87 | + # Non-identity: replace with single INT32->INT32 RESCALE |
| 88 | + with graph.inserting_before(user): |
| 89 | + composed_node = create_node( |
| 90 | + graph, |
| 91 | + exir_ops.backend.tosa.RESCALE.default, |
| 92 | + ( |
| 93 | + r1_input, |
| 94 | + r2_output_dtype, |
| 95 | + [composed_scale], |
| 96 | + r1_input_zp, |
| 97 | + r2_output_zp, |
| 98 | + ), |
| 99 | + from_node=user, |
| 100 | + ) |
| 101 | + user.replace_all_uses_with(composed_node) |
| 102 | + nodes_to_erase.append(user) |
| 103 | + |
| 104 | + modified = True |
| 105 | + |
| 106 | + # Always consider R1 for removal; actual erasure is guarded below |
| 107 | + nodes_to_erase.append(node) |
| 108 | + |
| 109 | + for node in nodes_to_erase: |
| 110 | + if len(node.users) == 0: |
| 111 | + graph.erase_node(node) |
| 112 | + |
| 113 | + if modified: |
| 114 | + graph_module = super().call(graph_module).graph_module |
| 115 | + graph_module.recompile() |
| 116 | + |
| 117 | + return PassResult(graph_module, modified) |
| 118 | + |
| 119 | + |
| 120 | +def _is_rescale(node: Node) -> bool: |
| 121 | + return ( |
| 122 | + node.op == "call_function" |
| 123 | + and node.target == exir_ops.backend.tosa.RESCALE.default |
| 124 | + ) |
0 commit comments