def check_graph_validity(sorted_graph: List[Tensor], raiseError: bool = False) -> bool:
    """
    Check whether all tensor/op in the AIT graph matches.
    If raiseError == False, returns true if graph is a valid graph, else false.
       raiseError == True, assert and raise at point of failure.
    """

    def handleError(msg: str):
        if raiseError:
            _LOGGER.info("check_graph_validity() error! Graph:")
            _LOGGER.info(graph_utils.sorted_graph_debug_str(sorted_graph))
            raise RuntimeError(msg)
        else:
            return False

    valid = True
    visited_tensors = set()
    for tensor in sorted_graph:
        tname = tensor._attrs.get("name", None)
        for op in tensor.src_ops():
            if op is None:
                continue
            oname = op._attrs.get("name", None)
            for input_tensor in op._attrs["inputs"]:
                if not isinstance(input_tensor, Tensor):
                    continue
                iname = input_tensor._attrs.get("name", None)
                if input_tensor not in visited_tensors:
                    valid = handleError(
                        "Input tensor {} not established in graph for op {}".format(
                            iname, oname
                        )
                    )
                if op not in input_tensor._attrs["dst_ops"]:
                    valid = handleError(
                        "Op {} not designated as dst_op for tensor {}".format(
                            oname, iname
                        )
                    )
            if tensor not in op._attrs["outputs"]:
                valid = handleError(
                    "Tensor {} not in outputs for op {}".format(tname, oname)
                )
        visited_tensors.add(tensor)

    visited_tensors = set()
    for tensor in sorted_graph[::-1]:
        tname = tensor._attrs.get("name", None)
        for op in tensor.dst_ops():
            if op is None:
                continue
            oname = op._attrs.get("name", None)
            outputs = op._attrs["outputs"]
            if not isinstance(outputs, list):
                outputs = [outputs]
            for output_tensor in outputs:
                if not isinstance(output_tensor, Tensor):
                    continue
                otname = output_tensor._attrs.get("name", None)
                if output_tensor not in visited_tensors:
                    valid = handleError(
                        "Output tensor {} not established in graph for op {}".format(
                            otname, oname
                        )
                    )
                if op not in output_tensor._attrs["src_ops"]:
                    valid = handleError(
                        "Op {} not designated as src_op for tensor {}".format(
                            oname, otname
                        )
                    )
            if tensor not in op._attrs["inputs"]:
                valid = handleError(
                    "Tensor {} not in inputs for op {}".format(tname, oname)
                )
        visited_tensors.add(tensor)

    return valid

    Check whether all tensor/op in the AIT graph matches.
    If raiseError == False, returns true if graph is a valid graph, else false.
       raiseError == True, assert and raise at point of failure.
    
python/aitemplate/compiler/transform/transform_utils.py