code: def sync_bool(
      val: bool,
      pg: Optional[dist.ProcessGroup] = None,
      coherence_mode: Union[
          Literal["any", "all", "rank_zero"], int, float
      ] = "any",
  ) -> bool:
      """Utility to synchronize a boolean value across members of a provided process group.
  
      In the case ``torch.distributed`` is not available or initialized, the input ``val`` is returned.
  
      Args:
          val (bool): boolean value to synchronize
          pg: process group to use for synchronization. If not specified, the default process group is used.
          coherence_mode Union[str, int, float]: the manner in which the boolean value should be synchronized. 5 options are currently supported:
              1. any (default): If any rank provides a True value, all ranks should receive True.
              2. all: Only if all ranks provide a True value should all ranks receive True.
              3. rank_zero: Makes rank 0 process's value the source of truth and broadcasts the result to all other processes.
              4. If an integer N is provided, return True only if at least N processes provide a True value.
              5. If a float F is provided, return True only if at least this ratio of processes provide a True value. The ratio provided should be in the range [0, 1].
  
      Returns:
          The synchronized boolean value.
  
      Example::
  
          >>> val = True
          >>> # synced_val is True iff all ranks provide a True value to the function
          >>> synced_val = sync_bool(val, coherence_mode="all")
          >>> if synced_val:
          >>>     print("success")
  
      """
      if not dist.is_available() or not dist.is_initialized():
          return val
  
      pg = pg or dist.group.WORLD
      device = torch.device(
          torch.cuda.current_device()
          if dist.get_backend(pg) == "nccl"
          else "cpu"
      )
      pg_wrapper = PGWrapper(pg)
  
      dtype = torch.uint8
      if pg_wrapper.get_world_size() > 256:
          dtype = torch.int
  
      indicator = (
          torch.ones(1, device=device, dtype=dtype)
          if val
          else torch.zeros(1, device=device, dtype=dtype)
      )
  
      if coherence_mode == "rank_zero":
          # Broadcast from rank 0 to all other ranks
          dist.broadcast(indicator, src=0, group=pg)
          return bool(indicator[0].item())
      elif coherence_mode == "any":
          # sum up the indicators across all the ranks.
          dist.all_reduce(indicator, op=dist.ReduceOp.SUM)
          return indicator.item() > 0
      elif coherence_mode == "all":
          dist.all_reduce(indicator, op=dist.ReduceOp.SUM)
          return indicator.item() == pg_wrapper.get_world_size()
      elif isinstance(coherence_mode, int):
          # if >= int(coherence_mode) processes signal to stop, all processes stop
          dist.all_reduce(indicator, op=dist.ReduceOp.SUM)
          return indicator.item() >= coherence_mode
      elif isinstance(coherence_mode, float):
          dist.all_reduce(indicator, op=dist.ReduceOp.SUM)
          return (
              indicator.item() / pg_wrapper.get_world_size()
          ) >= coherence_mode
      else:
          raise TypeError(
              f'Invalid value for `coherence_mode` provided: Expected type int, float, or one of ("any", "all", "rank_zero"), but received {coherence_mode}.'
          )
  
imports: import os
  import tempfile
  from functools import wraps
  from typing import Any, Callable, cast, List, Optional, TypeVar, Union
  import torch
  import torch.distributed
  import torch.nn.functional
  from torch import Tensor
  from torch.distributed.elastic.utils.distributed import get_free_port
  from typing_extensions import Literal
label_desc: Use image node_id 15 for calling a global function with example usage: sync_bool(val, pg, coherence_mode) and returns: val, bool, unknown, unknown, unknown, unknown