def optimize_graph(
    sorted_graph: List[Tensor], workdir: str, optimize=True
) -> List[Tensor]:
    """Applies graph optimizations, including

    - fuse permute and bmm
    - fuse permute and gemm
    - transform odd alignment
    - fuse conv and elementwise
    - fuse gemm and elementwise
    - fuse elementwise ops
    - fuse parallel gemms
    - fuse group ops
    - transform special ops
    - transform strided ops
    - fuse bmm and permute
    - transform memory ops
    - apply padding

    Parameters
    ----------
    sorted_graph : List[Tensor]
        Input graph
    workdir : str
        working directory

    Returns
    -------
    List[Tensor]
        Fused graph
    """

    funcs = [
        remove_elementwise_no_ops,
        dedup_make_jagged_ops,
        fuse_permute_bmm_and_gemm,
        fuse_bmm_permute,
        fuse_expand_bmm,
        transform_odd_alignment,
        fuse_conv_elementwise,
        fuse_single_source_parallel_gemms,
        fuse_mm_elementwise,
        fuse_mm_reshape_permute,
        # make sure we run move_view_op_before_concat before transform_memory_ops
        move_view_op_before_concat,
        merge_view_ops,
        transform_memory_ops,
        fuse_ops,
        fuse_elementwise,
        # need to run before transform_strided_ops to fuse strided ops + concat
        # and transform_memory_ops to fuse split + concat
        fuse_parallel_gemms,
        fuse_group_ops,
        # This needs to be run after fuse_ops() to avoid handling elementwise
        # op directly. After fuse_ops, there are only FusedElementwise ops.
        transform_special_ops,
        apply_padding,
        # apply_padding may introduce new concats that can be fused
        move_view_op_before_concat,
        transform_memory_ops,
        transform_strided_ops,
        split_large_slice_scatter_ops,
        split_large_concat_ops,
        split_large_split_ops,
        transform_permute_to_reshape,
        transform_memory_ops,
        eliminate_permutations,
        # fuse_duplicate_fused_elementwise must run after elementwise fusion and
        # after passes that modify/replace a fused_elementwise's input/output accessor.
        fuse_duplicate_fused_elementwise,
    ]

    if not optimize:
        # 1 - Convert elementwise ops to singleton fused_elementwise ops
        # 2 - Padding also needs to be done for the model to be executable.
        funcs = [
            process_singleton_elementwise,
            apply_padding,
            split_large_slice_scatter_ops,
            split_large_concat_ops,
            split_large_split_ops,
        ]

    for i, func in enumerate(funcs):
        sorted_graph = func(sorted_graph, workdir)
        graph_utils.dump_graph_debug_str_to_file(
            sorted_graph, workdir, f"{i:02}-{func.__name__}"
        )

    return sorted_graph
Applies graph optimizations, including

    - fuse permute and bmm
    - fuse permute and gemm
    - transform odd alignment
    - fuse conv and elementwise
    - fuse gemm and elementwise
    - fuse elementwise ops
    - fuse parallel gemms
    - fuse group ops
    - transform special ops
    - transform strided ops
    - fuse bmm and permute
    - transform memory ops
    - apply padding

    Parameters
    ----------
    sorted_graph : List[Tensor]
        Input graph
    workdir : str
        working directory

    Returns
    -------
    List[Tensor]
        Fused graph
    
python/aitemplate/compiler/transform/optimize_graph.py