code: def _infer_slurm_init(
      cfg: DistributedTrainingConfig, num_pipelines_per_node
  ):
      node_list = os.environ.get("SLURM_STEP_NODELIST")
      if node_list is None:
          node_list = os.environ.get("SLURM_JOB_NODELIST")
      if node_list is not None:
          try:
              hostnames = subprocess.check_output(
                  ["scontrol", "show", "hostnames", node_list]
              )
              cfg.distributed_init_method = (
                  "tcp://{host}:{port}".format(
                      host=hostnames.split()[0].decode("utf-8"),
                      port=cfg.distributed_port,
                  )
              )
              nnodes = int(os.environ.get("SLURM_NNODES"))
              ntasks_per_node = os.environ.get("SLURM_NTASKS_PER_NODE")
              if ntasks_per_node is not None:
                  ntasks_per_node = int(ntasks_per_node)
              else:
                  ntasks = int(os.environ.get("SLURM_NTASKS"))
                  nnodes = int(os.environ.get("SLURM_NNODES"))
                  assert ntasks % nnodes == 0
                  ntasks_per_node = int(ntasks / nnodes)
              if ntasks_per_node == 1:
                  gpus_per_node = torch.cuda.device_count()
                  node_id = int(os.environ.get("SLURM_NODEID"))
                  cfg.distributed_rank = node_id * gpus_per_node
                  cfg.distributed_world_size = nnodes * gpus_per_node
              elif cfg.pipeline_model_parallel:
                  assert ntasks_per_node == num_pipelines_per_node, (
                      "SLURM --ntasks-per-node must match number of pipelines per "
                      "node (={})".format(num_pipelines_per_node)
                  )
                  cfg.distributed_no_spawn = True
                  # For 4-way MP on nodes with 8 GPUs, ranks will be [0, 1] on
                  # the first node, [1, 2] on the second node, etc. This
                  # matches torch.distributed.launch.
                  node_id = int(os.environ.get("SLURM_NODEID"))
                  local_id = int(os.environ.get("SLURM_LOCALID"))
                  cfg.distributed_rank = (
                      node_id * num_pipelines_per_node + local_id
                  )
                  # In the above example, device_id will always be in [0, 1],
                  # which also matches torch.distributed.launch.
                  cfg.device_id = local_id
                  # We also want to set distributed_world_size to be the total
                  # number of pipelines across all nodes.
                  cfg.distributed_world_size = (
                      nnodes * num_pipelines_per_node
                  )
              else:
                  assert (
                      ntasks_per_node
                      == cfg.distributed_world_size // nnodes
                  ), f"{ntasks_per_node}, {cfg.distributed_world_size}, {nnodes}"
                  cfg.distributed_no_spawn = True
                  cfg.distributed_rank = int(
                      os.environ.get("SLURM_PROCID")
                  )
                  cfg.device_id = int(os.environ.get("SLURM_LOCALID"))
              logger.info(
                  f"Rank {cfg.distributed_rank}, device_id: {cfg.device_id}"
              )
              return True
          except subprocess.CalledProcessError as e:  # scontrol failed
              raise e
          except FileNotFoundError:  # Slurm is not installed
              pass
  
      return False
  
imports: import io
  import logging
  import os
  import pickle
  import random
  import socket
  import struct
  import subprocess
  import warnings
  from argparse import Namespace
  from collections import OrderedDict
  from dataclasses import dataclass
  from typing import Any, Dict, List, Mapping, Optional
  import torch
  import torch.distributed
  from fairseq.dataclass.configs import DistributedTrainingConfig, FairseqConfig
  from omegaconf import open_dict
label_desc: Use image node_id 4 for calling a global function with example usage: _infer_slurm_init(cfg, num_pipelines_per_node) and returns: False, True