code: def spawn_multi_process(
      world_size: int,
      backend: str,
      test_method: Callable[TParams, TReturn],
      *test_method_args: Any,
      **test_method_kwargs: Any,
  ) -> List[TReturn]:
      """
      Spawn single node, multi-rank function.
      Uses localhost and free port to communicate.
  
      Args:
          world_size: number of processes
          backend: backend to use. for example, "nccl", "gloo", etc
          test_method: callable to spawn. first 3 arguments are rank, world_size and mp output dict
          test_method_args: args for the test method
          test_method_kwargs: kwargs for the test method
  
      Returns:
          A list, l, where l[i] is the return value of test_method on rank i
      """
      manager = multiprocessing.Manager()
      mp_output_dict = manager.dict()
  
      port = str(get_free_port())
      torch.multiprocessing.spawn(
          # torch.multiprocessing.spawn sends rank as the first param
          # https://pytorch.org/docs/stable/multiprocessing.html#torch.multiprocessing.spawn
          _init_pg_and_rank_and_launch_test,
          args=(
              ProcessGroupSetupParams(
                  backend=backend, port=port, world_size=world_size
              ),
              mp_output_dict,
              test_method,
              test_method_args,
              test_method_kwargs,
          ),
          nprocs=world_size,
      )
  
      output_list = []
      for i in range(world_size):
          output_list.append(mp_output_dict[i])
  
      return output_list
  
imports: import ctypes
  import os
  import sys
  import unittest
  import uuid
  from contextlib import contextmanager
  from dataclasses import dataclass
  from datetime import timedelta
  from functools import wraps
  from io import StringIO
  from typing import Any, Callable, Dict, Generator, List, Optional, TextIO, Tuple, TypeVar
  import torch
  import torch.distributed.launcher
  from pyre_extensions import ParameterSpecification
  from torch import distributed, multiprocessing
  from torch.distributed.elastic.utils.distributed import get_free_port
  from torchtnt.utils.distributed import destroy_process_group
label_desc: Use image node_id 6 for calling a global function with example usage: spawn_multi_process(world_size, backend, test_method) and returns: output_list