code: def init_distributed(
      cuda: bool,
      local_rank: int,
      node_rank: int = 0,
      n_nodes: int = 1,
      n_procs: int = 0,
      master_addr: str = "localhost",
      master_port: int = 60066,
  ):
      if not dist.is_available():
          return
      cuda = pt.cuda.is_available() and cuda
      if cuda:
          if n_procs <= 0:
              n_procs = pt.cuda.device_count()
          else:
              assert (
                  n_procs <= pt.cuda.device_count()
              ), f"# processes per node exceeds # available GPUs"
      else:
          if n_procs <= 0:
              n_procs = os.cpu_count()
          else:
              assert (
                  n_procs <= os.cpu_count()
              ), f"# processes per node exceeds # available CPUs"
      assert (
          0 <= node_rank < n_nodes
      ), f"rank {node_rank} is not one of [0,...,{n_nodes-1}]"
      assert (
          0 <= local_rank < n_procs
      ), f"local_rank {local_rank} is not one of [0, ..., {n_procs-1}]"
  
      world_size = n_nodes * n_procs
      if world_size == 1:
          return
      os.environ["MASTER_ADDR"] = master_addr
      os.environ["MASTER_PORT"] = str(master_port)
      os.environ["WORLD_SIZE"] = str(world_size)
      rank = node_rank * n_procs + local_rank
      dist.init_process_group("nccl" if cuda else "gloo", rank=rank)
  
file_path: gluonts/src/gluonts/nursery/daf/tslib/engine/distributed.py
imports: from typing import Optional
  import os
  import torch
  from torch import Tensor
  from torch import distributed
label_desc: Use image node_id 7 for calling a global function with example usage: init_distributed(cuda, local_rank, node_rank, n_nodes, n_procs, master_addr, master_port) without return types