code: def run_subtests(
      cls_inst,
      subtest_config: Dict[str, List[Any]],
      test_fn: Callable,
      *test_args,
      **test_kwargs: Any,
  ):
      """
      Runs a test function given by ``test_fn`` as a subtest according to the
      configurations specified by ``subtest_config``. This amortizes the
      costly setup overhead (including process spawn and initializing the
      process group) over the subtests.
  
      Args:
          subtest_config (Dict[str, List[Any]]): A mapping from subtest
              keyword argument name to a list of its possible values.
          test_fn (Callable): A callable that runs the actual test.
          test_args: Positional arguments to pass to ``test_fn``.
          test_kwargs: Keyword arguments to pass to ``test_fn``.
      """
      # Convert the config mapping to a list to have a fixed order
      subtest_config_items: List[Tuple[str, List[Any]]] = list(
          subtest_config.items()
      )
      subtest_config_keys: List[str] = [
          item[0] for item in subtest_config_items
      ]
      subtest_config_values: List[List[Any]] = [
          item[1] for item in subtest_config_items
      ]
      for values in itertools.product(*subtest_config_values):
          # Map keyword to chosen value
          subtest_kwargs = dict(zip(subtest_config_keys, values))
          with cls_inst.subTest(**subtest_kwargs):
              test_fn(*test_args, **test_kwargs, **subtest_kwargs)
          dist.barrier()
  
imports: import itertools
  from dataclasses import dataclass
  import sys
  from functools import wraps
  from typing import Any, Callable, Iterator, Tuple, Dict, List, Sequence, TypeVar, cast
  import torch
  import torch.distributed
  import torch.nn
  import torch.nn.functional
  from torch.utils._pytree import tree_flatten, tree_unflatten, TreeSpec
  from torch.testing._internal.common_distributed import MultiProcessTestCase, MultiThreadedTestCase, TEST_SKIPS, skip_if_lt_x_gpu
  from torch.distributed._tensor import DeviceMesh, Shard, Replicate, distribute_tensor
  from torch.distributed._tensor.placement_types import Placement
label_desc: Use image node_id 3 for calling a global function with example usage: run_subtests(cls_inst, subtest_config, test_fn) without return types