docstr: Export all the environment variables required to properly setup
  PyTorch distributed (with the default env:// initialization method) i.e.
  MASTER_ADDR, MASTER_PORT, RANK, WORLD_SIZE (to which LOCAL_RANK and
  LOCAL_WORLD_SIZE are added).
  
  Parameter
  ----------
  set_cuda_visible_device: bool
      if True, updates CUDA_VISIBLE_DEVICES to use only the device
      matching the local rank.
  overwrite: bool
      if True, overwrites the environment variables if they exist;
      this can be useful when launching a job from another job.
  
  Returns
  --------
  TorchDistributedEnvironment
      the current instance
docstr_tok: ["Export","all","the","environment","variables","required","to","properly","setup","PyTorch","distributed","(","with","the","default","env",":","\/\/","initialization","method",")","i.e",".","MASTER_ADDR",",","MASTER_PORT",",","RANK",",","WORLD_SIZE","(","to","which","LOCAL_RANK","and","LOCAL_WORLD_SIZE","are","added",")",".","Parameter","--","--","--","--","--","set_cuda_visible_device",":","bool","if","True",",","updates","CUDA_VISIBLE_DEVICES","to","use","only","the","device","matching","the","local","rank",".","overwrite",":","bool","if","True",",","overwrites","the","environment","variables","if","they","exist",";","this","can","be","useful","when","launching","a","job","from","another","job",".","Returns","--","--","--","--","TorchDistributedEnvironment","the","current","instance"]
code: def export(
      self,
      set_cuda_visible_devices: bool = True,
      overwrite: bool = False,
  ) -> "TorchDistributedEnvironment":
      """Export all the environment variables required to properly setup
      PyTorch distributed (with the default env:// initialization method) i.e.
      MASTER_ADDR, MASTER_PORT, RANK, WORLD_SIZE (to which LOCAL_RANK and
      LOCAL_WORLD_SIZE are added).
  
      Parameter
      ----------
      set_cuda_visible_device: bool
          if True, updates CUDA_VISIBLE_DEVICES to use only the device
          matching the local rank.
      overwrite: bool
          if True, overwrites the environment variables if they exist;
          this can be useful when launching a job from another job.
  
      Returns
      --------
      TorchDistributedEnvironment
          the current instance
      """
      # See the "Environment variable initialization" section from
      # https://pytorch.org/docs/stable/distributed.html for the complete list of
      # environment variables required for the env:// initialization method.
      env_vars = {
          "MASTER_ADDR": self.master_addr,
          "MASTER_PORT": str(self.master_port),
          "RANK": str(self.rank),
          "WORLD_SIZE": str(self.world_size),
          "LOCAL_RANK": str(self.local_rank),  # Not required
          "LOCAL_WORLD_SIZE": str(
              self.local_world_size
          ),  # Not required
      }
      if not overwrite:
          for key in env_vars:
              if key in os.environ:
                  raise RuntimeError(
                      f"Cannot export environment variables as {key} is already set"
                  )
      # Note: CUDA_VISIBLE_DEVICES may already be set with all available GPUs
      if set_cuda_visible_devices:
          env_vars["CUDA_VISIBLE_DEVICES"] = str(self.local_rank)
      os.environ.update(env_vars)
      return self
  
imports: import collections
  import contextlib
  import datetime
  import itertools
  import os
  import random
  import shutil
  import subprocess
  import tempfile
  import time
  import typing
  from pathlib import Path
  from .core import core
  from .core.job_environment import JobEnvironment
  from .core.utils import CommandFunction
  from .core.utils import DelayedSubmission
  from .core.utils import environment_variables
label_desc: Use image node_id 3 for calling the TorchDistributedEnvironment obj's underlying member method code with example usage: obj.export(set_cuda_visible_devices, overwrite) and returns: self