class_docstr: Represents a gang that wraps a process group.
class_docstr_tok: ["Represents","a","gang","that","wraps","a","process","group","."]
docstr: Initialize the default process group and wrap it as a gang.
  
  :param device:
      If ``None``; if CUDA is available, the process group will be
      initialized on an automatically selected CUDA device; otherwise,
      it will be initialized on CPU.
  :param timeout:
      The timeout for operations executed against the process group.
  :param num_threads:
      The number of threads used for interaop parallelism.
  :param warn_only:
      If ``True``, logs a warning instead of raising an error if the
      process group is not set up reliably.
  :param ok_initialized:
      If ``True``, does not raise an error if the process group is already
      initialized.
docstr_tok: ["Initialize","the","default","process","group","and","wrap","it","as","a","gang",".",":","param","device",":","If","``","None","``",";","if","CUDA","is","available",",","the","process","group","will","be","initialized","on","an","automatically","selected","CUDA","device",";","otherwise",",","it","will","be","initialized","on","CPU",".",":","param","timeout",":","The","timeout","for","operations","executed","against","the","process","group",".",":","param","num_threads",":","The","number","of","threads","used","for","interaop","parallelism",".",":","param","warn_only",":","If","``","True","``",",","logs","a","warning","instead","of","raising","an","error","if","the","process","group","is","not","set","up","reliably",".",":","param","ok_initialized",":","If","``","True","``",",","does","not","raise","an","error","if","the","process","group","is","already","initialized","."]
code: def init_default_process_group(
      *,
      device: Optional[Device] = None,
      timeout: Optional[timedelta] = None,
      num_threads: Optional[int] = None,
      warn_only: bool = False,
      ok_initialized: bool = False,
  ) -> Gang:
      """Initialize the default process group and wrap it as a gang.
  
      :param device:
          If ``None``; if CUDA is available, the process group will be
          initialized on an automatically selected CUDA device; otherwise,
          it will be initialized on CPU.
      :param timeout:
          The timeout for operations executed against the process group.
      :param num_threads:
          The number of threads used for interaop parallelism.
      :param warn_only:
          If ``True``, logs a warning instead of raising an error if the
          process group is not set up reliably.
      :param ok_initialized:
          If ``True``, does not raise an error if the process group is already
          initialized.
      """
      if not dist.is_available():
          raise RuntimeError("`torch.distributed` is not available.")
  
      if dist.is_initialized():
          if ok_initialized:
              return ProcessGroupGang.from_default_process_group()
  
          raise RuntimeError(
              "The default process group is already initialized."
          )
  
      num_procs = _get_num_processes()
  
      if num_threads is None:
          if num_procs > 1 and "OMP_NUM_THREADS" not in os.environ:
              # To prevent thread oversubscription, we distribute cores evenly
              # across workers.
              num_threads = _get_num_cpus(num_procs)
  
      if num_threads is not None:
          torch.set_num_threads(num_threads)
  
          logger.info("Setting the number of threads used for intraop parallelism to %d.", num_threads)  # fmt: skip
  
      if device is None:
          device = _determine_default_device()
  
          assert device.type == "cpu" or device.type == "cuda"
  
      if device.type == "cpu":
          backend = "gloo"
      elif device.type == "cuda":
          backend = "nccl"
      else:
          raise RuntimeError(
              f"Only CPU and CUDA devices are supported, but `device` is a {device.type.upper()} device."
          )
  
      if (
          device.type == "cuda"
          and "NCCL_ASYNC_ERROR_HANDLING" not in os.environ
      ):
          if warn_only:
              logger.warning("The default process group will use the NCCL backend, but the `NCCL_ASYNC_ERROR_HANDLING` environment variable is not set. Your collective communication calls can hang indefinitely. Learn more at https://github.com/pytorch/pytorch/issues/46874.")  # fmt: skip
          else:
              raise RuntimeError(
                  "The default process group will use the NCCL backend, but the `NCCL_ASYNC_ERROR_HANDLING` environment variable is not set. Learn more at https://github.com/pytorch/pytorch/issues/46874."
              )
  
      if timeout is None:
          timeout = timedelta(minutes=15)
  
      dist.init_process_group(backend, timeout=timeout)
  
      if dist.group.WORLD is None:
          raise RuntimeError(
              "The default process group is not available. Please file a bug report."
          )
  
      return ProcessGroupGang(dist.group.WORLD, device)
  
imports: import logging
  import os
  from abc import ABC, abstractmethod
  from datetime import timedelta
  from enum import Enum
  from typing import Optional, final
  import torch
  import torch.distributed
  from torch import Tensor
  from torch.distributed import ProcessGroup, ReduceOp
  from fairseq2.typing import CPU, Device, finaloverride
label_desc: Use image node_id 2 for calling the ProcessGroupGang obj's underlying member method code with example usage: obj.init_default_process_group() and returns: ProcessGroupGang, ProcessGroupGang