code: def _ensure_topologically_sorted(
      nodes: Sequence[node_proto_view.NodeProtoView],
  ):
      """Raises ValueError if nodes are not topologically sorted.
  
      If the pipeline comes from the compiler, it should already be
      topologically sorted. This is just in case the IR proto was modified or
      created in another way.
  
      Args:
        nodes: The input nodes.
  
      Raises:
        ValueError: If the pipeline is not topologically sorted.
      """
      # Upstream check
      visited = set()
      for node in nodes:
          for upstream_node in node.upstream_nodes:
              if upstream_node not in visited:
                  raise ValueError(
                      "Input pipeline is not topologically sorted. "
                      f"node {node.node_info.id} has upstream_node {upstream_node}, but "
                      f"{upstream_node} does not appear before {node.node_info.id}"
                  )
          visited.add(node.node_info.id)
      # Downstream check
      visited.clear()
      for node in reversed(nodes):
          for downstream_node in node.downstream_nodes:
              if downstream_node not in visited:
                  raise ValueError(
                      "Input pipeline is not topologically sorted. "
                      f"node {node.node_info.id} has downstream_node {downstream_node}, "
                      f"but {downstream_node} does not appear after {node.node_info.id}"
                  )
          visited.add(node.node_info.id)
  
imports: import collections
  import concurrent.futures
  import enum
  from typing import Collection, Dict, Final, List, Mapping, Optional, OrderedDict, Sequence, Set, Tuple
  import uuid
  from absl import logging
  from tfx.dsl.compiler import compiler_utils
  from tfx.dsl.compiler import constants
  from tfx.orchestration import metadata
  from tfx.orchestration import node_proto_view
  from tfx.orchestration.portable import execution_publish_utils
  from tfx.orchestration.portable.mlmd import context_lib
  from tfx.orchestration.portable.mlmd import execution_lib
  from tfx.proto.orchestration import pipeline_pb2
  from ml_metadata.proto import metadata_store_pb2
label_desc: Use image node_id 12 for calling a global function with example usage: _ensure_topologically_sorted(nodes) without return types