def fuse_duplicate_fused_elementwise(
    sorted_graph: List[Tensor], _workdir: str
) -> List[Tensor]:
    """This pass finds all duplicate fused elementwise ops and fuses them once
    more. It assumes any fuse elementwise passes are complete.

    We do the fusion by taking all the duplicate fused elementwise ops and
    effectively deleting all but one. We make sure to transfer the outputs and
    output_accessors of the duplicate ops to the remaining op. That means, the
    newly fused op will have multiple outputs.

    Parameters
    ----------
    sorted_graph : List[Tensor]
        Input graph

    _workdir : str
        Required by optimize_graph.py

    Returns
    ----------
    sorted_graph : List[Tensor]
        Modified input graph with duplicate fused elementwise ops fused together.
    """

    fusion_groups = find_duplicate_fused_elementwise(sorted_graph)
    for primary_op, duplicate_ops in fusion_groups.items():
        # Primary op inherits the outputs from the duplicate ops.

        for key in ("outputs", "output_accessors"):
            duplicate_ops_outputs = [
                output for op in duplicate_ops for output in op._attrs[key]
            ]
            primary_op._attrs[key] += duplicate_ops_outputs
            if key != "outputs":
                continue

            # Make sure to update src_ops in the output tensors.
            for output_tensor in duplicate_ops_outputs:
                old_src_ops = output_tensor._attrs["src_ops"]
                output_tensor._attrs["src_ops"] = set(old_src_ops) - set(
                    duplicate_ops
                ) | {primary_op}

            # Make sure to update dst_ops in the input tensors.
            for input_tensor in primary_op._attrs["inputs"]:
                input_tensor._attrs["dst_ops"] = set(
                    input_tensor._attrs["dst_ops"]
                ) - set(duplicate_ops)

        # Assumption: If the input accessors are the same, then the output's
        # original shape must be the same.
        prev_shape = primary_op._attrs["output_accessors"][0].original_shapes
        for output_accessor in primary_op._attrs["output_accessors"]:
            shape = output_accessor.original_shapes
            assert (
                prev_shape == shape
            ), "Output shapes mismatch in fuse_duplicate_fused_elementwise: {}, {}".format(
                prev_shape, shape
            )
            prev_shape = shape

        _LOGGER.info(
            "Fusing {} with {}".format(
                primary_op._attrs["name"],
                ", ".join([dup_op._attrs["name"] for dup_op in duplicate_ops]),
            )
        )

    return transform_utils.sanitize_sorted_graph(sorted_graph)
This pass finds all duplicate fused elementwise ops and fuses them once
more. It assumes any fuse elementwise passes are complete.

We do the fusion by taking all the duplicate fused elementwise ops and
effectively deleting all but one. We make sure to transfer the outputs and
output_accessors of the duplicate ops to the remaining op. That means, the
newly fused op will have multiple outputs.

Parameters
----------
sorted_graph : List[Tensor]
Input graph

_workdir : str
Required by optimize_graph.py

Returns
----------
sorted_graph : List[Tensor]
Modified input graph with duplicate fused elementwise ops fused together.
python/aitemplate/compiler/transform/fuse_duplicate_fused_elementwise.py