code: def auto_optim(optimizer: Optimizer, **kwargs: Any) -> Optimizer:
      """Helper method to adapt optimizer for non-distributed and distributed configurations (supporting
      all available backends from :meth:`~ignite.distributed.utils.available_backends()`).
  
      Internally, this method is no-op for non-distributed and torch native distributed configuration.
  
      For XLA distributed configuration, we create a new class that inherits from provided optimizer.
      The goal is to override the `step()` method with specific `xm.optimizer_step`_ implementation.
  
      For Horovod distributed configuration, optimizer is wrapped with Horovod Distributed Optimizer and
      its state is broadcasted from rank 0 to all other processes.
  
      Args:
          optimizer: input torch optimizer
          kwargs: kwargs to Horovod backend's DistributedOptimizer.
  
      Returns:
          Optimizer
  
      Examples:
          .. code-block:: python
  
              import ignite.distributed as idist
  
              optimizer = idist.auto_optim(optimizer)
  
      .. _xm.optimizer_step: https://pytorch.org/xla/release/1.5/index.html#torch_xla.core.xla_model.optimizer_step
  
      .. versionchanged:: 0.4.2
          Added Horovod distributed optimizer.
  
      .. versionchanged:: 0.4.7
          Added kwargs to ``idist.auto_optim``.
      """
      bnd = idist.backend()
      if idist.has_xla_support and bnd == idist_xla.XLA_TPU:
          cls = type(
              optimizer.__class__.__name__,
              (optimizer.__class__,),
              dict(_XLADistributedOptimizer.__dict__),
          )
          return cls(optimizer)
  
      if idist.has_hvd_support and bnd == idist_hvd.HOROVOD:
          import horovod.torch as hvd
  
          optimizer = hvd.DistributedOptimizer(optimizer, **kwargs)
          hvd.broadcast_optimizer_state(optimizer, root_rank=0)
          return optimizer
  
      return optimizer
  
file_path: ignite/ignite/distributed/auto.py
imports: import warnings
  from typing import Any, Iterator, List, Optional, Union
  import torch
  import torch.nn
  from torch.optim.optimizer import Optimizer
  from torch.utils.data import DataLoader, Dataset, IterableDataset
  from torch.utils.data.distributed import DistributedSampler
  from torch.utils.data.sampler import Sampler
  from ignite.distributed import utils
  from ignite.distributed.comp_models import horovod, native, xla
  from ignite.utils import setup_logger
label_desc: Use image node_id 3 for calling a global function with example usage: auto_optim(optimizer) and returns: optimizer, cls, optimizer