code: def setup_common_training_handlers(
      trainer: Engine,
      train_sampler: Optional[DistributedSampler] = None,
      to_save: Optional[Mapping] = None,
      save_every_iters: int = 1000,
      output_path: Optional[str] = None,
      lr_scheduler: Optional[
          Union[ParamScheduler, PyTorchLRScheduler]
      ] = None,
      with_gpu_stats: bool = False,
      output_names: Optional[Iterable[str]] = None,
      with_pbars: bool = True,
      with_pbar_on_iters: bool = True,
      log_every_iters: int = 100,
      stop_on_nan: bool = True,
      clear_cuda_cache: bool = True,
      save_handler: Optional[Union[Callable, BaseSaveHandler]] = None,
      **kwargs: Any,
  ) -> None:
      """Helper method to setup trainer with common handlers (it also supports distributed configuration):
  
          - :class:`~ignite.handlers.terminate_on_nan.TerminateOnNan`
          - handler to setup learning rate scheduling
          - :class:`~ignite.handlers.checkpoint.ModelCheckpoint`
          - :class:`~ignite.metrics.RunningAverage` on `update_function` output
          - Two progress bars on epochs and optionally on iterations
  
      Args:
          trainer: trainer engine. Output of trainer's `update_function` should be a dictionary
              or sequence or a single tensor.
          train_sampler: Optional distributed sampler used to call
              `set_epoch` method on epoch started event.
          to_save: dictionary with objects to save in the checkpoint. This argument is passed to
              :class:`~ignite.handlers.checkpoint.Checkpoint` instance.
          save_every_iters: saving interval. By default, `to_save` objects are stored
              each 1000 iterations.
          output_path: output path to indicate where `to_save` objects are stored. Argument is mutually
              exclusive with ``save_handler``.
          lr_scheduler: learning rate scheduler
              as native torch LRScheduler or ignite's parameter scheduler.
          with_gpu_stats: if True, :class:`~ignite.contrib.metrics.GpuInfo` is attached to the
              trainer. This requires `pynvml` package to be installed.
          output_names: list of names associated with `update_function` output dictionary.
          with_pbars: if True, two progress bars on epochs and optionally on iterations are attached.
              Default, True.
          with_pbar_on_iters: if True, a progress bar on iterations is attached to the trainer.
              Default, True.
          log_every_iters: logging interval for :class:`~ignite.contrib.metrics.GpuInfo` and for
              epoch-wise progress bar. Default, 100.
          stop_on_nan: if True, :class:`~ignite.handlers.terminate_on_nan.TerminateOnNan` handler is added to the trainer.
              Default, True.
          clear_cuda_cache: if True, `torch.cuda.empty_cache()` is called every end of epoch.
              Default, True.
          save_handler: Method or callable
              class to use to store ``to_save``. See :class:`~ignite.handlers.checkpoint.Checkpoint` for more details.
              Argument is mutually exclusive with ``output_path``.
          kwargs: optional keyword args to be passed to construct :class:`~ignite.handlers.checkpoint.Checkpoint`.
      """
  
      if idist.get_world_size() > 1:
          _setup_common_distrib_training_handlers(
              trainer,
              train_sampler=train_sampler,
              to_save=to_save,
              save_every_iters=save_every_iters,
              output_path=output_path,
              lr_scheduler=lr_scheduler,
              with_gpu_stats=with_gpu_stats,
              output_names=output_names,
              with_pbars=with_pbars,
              with_pbar_on_iters=with_pbar_on_iters,
              log_every_iters=log_every_iters,
              stop_on_nan=stop_on_nan,
              clear_cuda_cache=clear_cuda_cache,
              save_handler=save_handler,
              **kwargs,
          )
      else:
          if train_sampler is not None and isinstance(
              train_sampler, DistributedSampler
          ):
              warnings.warn(
                  "Argument train_sampler is a distributed sampler,"
                  " but either there is no distributed setting or world size is < 2. "
                  "Train sampler argument will be ignored",
                  UserWarning,
              )
          _setup_common_training_handlers(
              trainer,
              to_save=to_save,
              save_every_iters=save_every_iters,
              output_path=output_path,
              lr_scheduler=lr_scheduler,
              with_gpu_stats=with_gpu_stats,
              output_names=output_names,
              with_pbars=with_pbars,
              with_pbar_on_iters=with_pbar_on_iters,
              log_every_iters=log_every_iters,
              stop_on_nan=stop_on_nan,
              clear_cuda_cache=clear_cuda_cache,
              save_handler=save_handler,
              **kwargs,
          )
  
imports: import numbers
  import warnings
  from functools import partial
  from typing import Any, Callable, cast, Dict, Iterable, Mapping, Optional, Sequence, Union
  import torch
  import torch.nn
  from torch.optim.optimizer import Optimizer
  from torch.utils.data.distributed import DistributedSampler
  import ignite.distributed
  from ignite.contrib.handlers import ClearMLLogger, global_step_from_engine, MLflowLogger, NeptuneLogger, PolyaxonLogger, ProgressBar, TensorboardLogger, VisdomLogger, WandBLogger
  from ignite.contrib.handlers.base_logger import BaseLogger
  from ignite.contrib.metrics import GpuInfo
  from ignite.engine import Engine, Events
  from ignite.handlers import Checkpoint, DiskSaver, EarlyStopping, TerminateOnNan
  from ignite.handlers.checkpoint import BaseSaveHandler
  from ignite.handlers.param_scheduler import ParamScheduler
  from ignite.metrics import RunningAverage
  from ignite.metrics.metric import RunningBatchWise
  from ignite.utils import deprecated
label_desc: Use image node_id 1 for calling a global function with example usage: setup_common_training_handlers(trainer, train_sampler, to_save, save_every_iters, output_path, lr_scheduler, with_gpu_stats, output_names, with_pbars, with_pbar_on_iters, log_every_iters, stop_on_nan, clear_cuda_cache, save_handler) without return types