|
| 1 | +import logging |
| 2 | +from typing import Sequence |
| 3 | + |
| 4 | +import torch |
| 5 | +from torch.fx.passes.shape_prop import ShapeProp |
| 6 | +from torch_tensorrt.dynamo.lowering.passes.pass_utils import ( |
| 7 | + clean_up_graph_after_modifications, |
| 8 | +) |
| 9 | + |
| 10 | +logger = logging.getLogger(__name__) |
| 11 | + |
| 12 | + |
| 13 | +# TODO: Add relevant prims to this fusion |
| 14 | +def fuse_prims_broadcast( |
| 15 | + gm: torch.fx.GraphModule, sample_inputs: Sequence[torch.Tensor] |
| 16 | +) -> torch.fx.GraphModule: |
| 17 | + """Fuses prim nodes which are effectively the ATen equivalents with keep_dim=True""" |
| 18 | + modified_graph = False |
| 19 | + |
| 20 | + # Propagate shapes through the graph to determine if broadcast can be resolved |
| 21 | + try: |
| 22 | + ShapeProp(gm).propagate(*sample_inputs) |
| 23 | + except (RuntimeError, AssertionError): |
| 24 | + logger.warning( |
| 25 | + "Shape Propagation Failed on Graph, skipping fuse_prims_broadcast lowering pass", |
| 26 | + exc_info=True, |
| 27 | + ) |
| 28 | + return gm |
| 29 | + |
| 30 | + for node in gm.graph.nodes: |
| 31 | + # If the node is a sum prims operator, with broadcast_in_dim being the only consumer |
| 32 | + # it is a candidate for fusing |
| 33 | + if ( |
| 34 | + node.target in (torch.ops.prims.sum.default,) |
| 35 | + and len(node.users) == 1 |
| 36 | + and list(node.users)[0].target == torch.ops.prims.broadcast_in_dim.default |
| 37 | + ): |
| 38 | + # Get broadcasted shape, reduced dimensions, and original tensor shape |
| 39 | + broadcast_node = list(node.users)[0] |
| 40 | + broadcasted_shape = broadcast_node.args[1] |
| 41 | + reduced_dims = node.args[1] |
| 42 | + original_shape = node.args[0].meta["tensor_meta"].shape |
| 43 | + |
| 44 | + # If the rank of the broadcasted shape is the same as the original |
| 45 | + # and the broadcasts are all singletons for the reduced dimensions |
| 46 | + # and all of the non-reduced dimensions are identical to the originals |
| 47 | + |
| 48 | + # Then the broadcast is effectively performing a "keep_dim=True" operation |
| 49 | + if ( |
| 50 | + len(broadcasted_shape) == len(original_shape) |
| 51 | + and all(broadcasted_shape[i] == 1 for i in reduced_dims) |
| 52 | + and all( |
| 53 | + broadcasted_shape[j] == original_shape[j] |
| 54 | + for j in range(len(original_shape)) |
| 55 | + if j not in reduced_dims |
| 56 | + ) |
| 57 | + ): |
| 58 | + # Fuse the operator to its convertible alternative |
| 59 | + with gm.graph.inserting_after(broadcast_node): |
| 60 | + modified_graph = True |
| 61 | + |
| 62 | + if node.target == torch.ops.prims.sum.default: |
| 63 | + fused_node = gm.graph.call_function( |
| 64 | + torch.ops.aten.sum.dim_IntList, |
| 65 | + args=(node.args[0], reduced_dims, True), |
| 66 | + ) |
| 67 | + |
| 68 | + # Replace all uses of the placeholder except the cloned node |
| 69 | + # with the cloned placeholder |
| 70 | + broadcast_node.replace_all_uses_with( |
| 71 | + fused_node, |
| 72 | + ) |
| 73 | + |
| 74 | + # Erase uses of the broadcast node and original |
| 75 | + gm.graph.erase_node(broadcast_node) |
| 76 | + gm.graph.erase_node(node) |
| 77 | + |
| 78 | + if modified_graph: |
| 79 | + gm = clean_up_graph_after_modifications(gm) |
| 80 | + logger.debug(f"Fused prims-broadcast paradigm:\n{gm.graph}") |
| 81 | + |
| 82 | + return gm |
0 commit comments