code: def _init_dist_slurm(backend, port=None):
      """Initialize slurm distributed training environment.
  
      If argument ``port`` is not specified, then the master port will be system
      environment variable ``MASTER_PORT``. If ``MASTER_PORT`` is not in system
      environment variable, then a default port ``29500`` will be used.
  
      Args:
          backend (str): Backend of torch.distributed.
          port (int, optional): Master port. Defaults to None.
      """
      proc_id = int(os.environ["SLURM_PROCID"])
      ntasks = int(os.environ["SLURM_NTASKS"])
      node_list = os.environ["SLURM_NODELIST"]
      num_gpus = torch.cuda.device_count()
      torch.cuda.set_device(proc_id % num_gpus)
      addr = subprocess.getoutput(
          f"scontrol show hostname {node_list} | head -n1"
      )
      # specify master port
      if port is not None:
          os.environ["MASTER_PORT"] = str(port)
      elif "MASTER_PORT" in os.environ:
          pass  # use MASTER_PORT in the environment variable
      else:
          # 29500 is torch.distributed default port
          os.environ["MASTER_PORT"] = "29500"
      # use MASTER_ADDR in the environment variable if it already exists
      if "MASTER_ADDR" not in os.environ:
          os.environ["MASTER_ADDR"] = addr
      os.environ["WORLD_SIZE"] = str(ntasks)
      os.environ["LOCAL_RANK"] = str(proc_id % num_gpus)
      os.environ["RANK"] = str(proc_id)
      dist.init_process_group(backend=backend)
  
file_path: lavis/lavis/common/annotator/uniformer/mmcv/runner/dist_utils.py
imports: import functools
  import os
  import subprocess
  from collections import OrderedDict
  import torch
  import torch.multiprocessing
  from torch import distributed
  from torch._utils import _flatten_dense_tensors, _take_tensors, _unflatten_dense_tensors
label_desc: Use image node_id 4 for calling a global function with example usage: _init_dist_slurm(backend, port) without return types