def _move_view_op_before_concat(
    sorted_graph: List[Tensor],
) -> Tuple[bool, List[Tensor]]:
    """
    Return a tuple of (bool, List[Tensor]), where True indicates the
    graph has been successfully changed.
    """
    changed = False
    for tensor in sorted_graph:
        src_ops = tensor._attrs["src_ops"]
        if len(src_ops) == 0:
            continue
        first_cat = list(src_ops)[0]
        if not _is_valid_cat_op(first_cat):
            continue
        first_cat_outputs = first_cat._attrs["outputs"]
        if len(first_cat_outputs) != 1:
            continue
        first_cat_output = first_cat_outputs[0]
        # If the first cat is a graph output, we cannot fuse it
        if first_cat_output._attrs["is_output"]:
            continue
        next_ops = first_cat_output._attrs["dst_ops"]
        if len(next_ops) == 0:
            continue
        # skip cases where the first cat op is directly connected with another cat op,
        # because moving a view op between other two cat ops would insert a view op
        # between the directly-connected cat ops. The transformed graph would contain
        # a valid rewrite pattern which could trigger another re-write, and so on.
        # Consequently, we would end up with an infinite rewriting loop, e.g.
        # cat1 + reshape + cat2, cat1 + cat3 => cat1 + cat2, cat1 + reshape + cat3 =>
        # cat1 + reshape + cat2, cat1 + cat3 => ...
        concat_ops = [op for op in next_ops if op._attrs["op"] == "concatenate"]
        if len(concat_ops) > 0:
            continue
        view_ops = [op for op in next_ops if op._attrs["op"] in _SUPPORTED_VIEW_OPS]
        # skip if none of the next ops is one of the supported view ops
        if len(view_ops) == 0:
            continue
        a_view_op = view_ops[0]
        view_output_shape = a_view_op._attrs["outputs"][0].shape()
        # handle a special case where the all view_ops have the same output shape
        if len(view_ops) > 1 and not all(
            shape_utils.is_same_shape(
                vop._attrs["outputs"][0].shape(), view_output_shape
            )
            for vop in view_ops
        ):
            continue
        if any(vop._attrs["outputs"][0]._attrs["is_output"] for vop in view_ops):
            continue
        view_op, second_cat = _get_valid_view_op_and_second_cat(view_ops)
        if second_cat is None:
            continue
        if _try_move_view_op(first_cat, second_cat, view_op):
            changed = True
    return (changed, sorted_graph)
Return a tuple of (bool, List[Tensor]), where True indicates the
graph has been successfully changed.
python/aitemplate/compiler/transform/move_view_ops.py