class_docstr: DistributedContext provides useful methods for effective distributed training.
  
  A DistributedContext has the following required args:
   - rank: the index of this worker in the entire job
   - size: the number of workers in the entire job
   - local_rank: the index of this worker on this machine
   - local_size: the number of workers on this machine
   - cross_rank: the index of this machine in the entire job
   - cross_size: the number of machines in the entire job
  
  Additionally, any time that cross_size > 1, you must also provide:
   - chief_ip: the ip address to reach the chief worker (where rank==0)
  
  .. note::
  
     DistributedContext has ``.allgather()``, ``.gather()``, and ``.broadcast()`` methods, which
     are easy to use and which can be useful for coordinating work across workers, but it is not a
     replacement for the allgather/gather/broadcast operations in your particular distributed
     training framework.
class_docstr_tok: ["DistributedContext","provides","useful","methods","for","effective","distributed","training",".","A","DistributedContext","has","the","following","required","args",":","-","rank",":","the","index","of","this","worker","in","the","entire","job","-","size",":","the","number","of","workers","in","the","entire","job","-","local_rank",":","the","index","of","this","worker","on","this","machine","-","local_size",":","the","number","of","workers","on","this","machine","-","cross_rank",":","the","index","of","this","machine","in","the","entire","job","-","cross_size",":","the","number","of","machines","in","the","entire","job","Additionally",",","any","time","that","cross_size",">","1",",","you","must","also","provide",":","-","chief_ip",":","the","ip","address","to","reach","the","chief","worker","(","where","rank==0",")","..","note",":",":","DistributedContext","has","``",".allgather","(",")","``",",","``",".gather","(",")","``",",","and","``",".broadcast","(",")","``","methods",",","which","are","easy","to","use","and","which","can","be","useful","for","coordinating","work","across","workers",",","but","it","is","not","a","replacement","for","the","allgather\/gather\/broadcast","operations","in","your","particular","distributed","training","framework","."]
code: def __init__(
      self,
      *,
      rank: int,
      size: int,
      local_rank: int,
      local_size: int,
      cross_rank: int,
      cross_size: int,
      chief_ip: Optional[str] = None,
      pub_port: int = constants.INTER_TRAIN_PROCESS_COMM_PORT_1,
      pull_port: int = constants.INTER_TRAIN_PROCESS_COMM_PORT_2,
      port_offset: int = 0,
      force_tcp: bool = False,
  ) -> None:
      rank_args = (
          rank,
          size,
          local_rank,
          local_size,
          cross_rank,
          cross_size,
      )
      if sum(x is not None for x in rank_args) not in (0, 6):
          raise ValueError(
              "rank, size, local_rank, local_size, cross_rank, and cross_size must all be "
              "provided if any are provided"
          )
  
      self.rank = rank if rank is not None else 0
      self.size = size if size is not None else 1
      self.local_rank = local_rank if local_rank is not None else 0
      self.local_size = local_size if local_size is not None else 1
      self.cross_rank = cross_rank if cross_rank is not None else 0
      self.cross_size = cross_size if cross_size is not None else 1
  
      self._pub_port = pub_port
      self._pull_port = pull_port
      self._chief_ip = chief_ip
  
      self._is_chief = self.rank == 0
      self._is_local_chief = self.local_rank == 0
  
      if self.local_size != self.size:
          if chief_ip is None:
              raise AssertionError(
                  f"This is a distributed job (local_size {self.local_size} != size {self.size}) "
                  "but chief_ip was none provided.  When cross_size > 1, the chief_ip parameter "
                  "is required."
              )
          self._chief_ip = chief_ip
      else:
          # When cross_size == 1, always contact the chief as localhost.
          self._chief_ip = "127.0.0.1"
  
      self._closed = False
  
      self._init_ipc(force_tcp)
  
imports: import logging
  import os
  import socket
  import tempfile
  from typing import Any, List, Optional
  from determined import constants, ipc, util
label_desc: Use image node_id 1 to create a new DistributedContext object  with example: obj = DistributedContext()