docstr: The training loop running a single training epoch.
  
  Args:
      model: the LightningModule to train
      optimizer: the optimizer, optimizing the LightningModule.
      train_loader: The dataloader yielding the training batches.
      limit_batches: Limits the batches during this training epoch.
          If greater than the number of batches in the ``train_loader``, this has no effect.
      scheduler_cfg: The learning rate scheduler configuration.
          Have a look at :meth:`~lightning.pytorch.core.LightningModule.configure_optimizers`
          for supported values.
docstr_tok: ["The","training","loop","running","a","single","training","epoch",".","Args",":","model",":","the","LightningModule","to","train","optimizer",":","the","optimizer",",","optimizing","the","LightningModule",".","train_loader",":","The","dataloader","yielding","the","training","batches",".","limit_batches",":","Limits","the","batches","during","this","training","epoch",".","If","greater","than","the","number","of","batches","in","the","``","train_loader","``",",","this","has","no","effect",".","scheduler_cfg",":","The","learning","rate","scheduler","configuration",".","Have","a","look","at",":","meth",":","`","~lightning.pytorch.core.LightningModule.configure_optimizers","`","for","supported","values","."]
code: def train_loop(
      self,
      model: L.LightningModule,
      optimizer: torch.optim.Optimizer,
      train_loader: torch.utils.data.DataLoader,
      limit_batches: Union[int, float] = float("inf"),
      scheduler_cfg: Optional[
          Mapping[
              str,
              Union[
                  L.fabric.utilities.types.LRScheduler, bool, str, int
              ],
          ]
      ] = None,
  ):
      """The training loop running a single training epoch.
  
      Args:
          model: the LightningModule to train
          optimizer: the optimizer, optimizing the LightningModule.
          train_loader: The dataloader yielding the training batches.
          limit_batches: Limits the batches during this training epoch.
              If greater than the number of batches in the ``train_loader``, this has no effect.
          scheduler_cfg: The learning rate scheduler configuration.
              Have a look at :meth:`~lightning.pytorch.core.LightningModule.configure_optimizers`
              for supported values.
  
      """
      self.fabric.call("on_train_epoch_start")
      iterable = self.progbar_wrapper(
          train_loader,
          total=min(len(train_loader), limit_batches),
          desc=f"Epoch {self.current_epoch}",
      )
  
      for batch_idx, batch in enumerate(iterable):
          # end epoch if stopping training completely or max batches for this epoch reached
          if self.should_stop or batch_idx >= limit_batches:
              break
  
          self.fabric.call("on_train_batch_start", batch, batch_idx)
  
          # check if optimizer should step in gradient accumulation
          should_optim_step = (
              self.global_step % self.grad_accum_steps == 0
          )
          if should_optim_step:
              # currently only supports a single optimizer
              self.fabric.call("on_before_optimizer_step", optimizer, 0)
  
              # optimizer step runs train step internally through closure
              optimizer.step(
                  partial(
                      self.training_step,
                      model=model,
                      batch=batch,
                      batch_idx=batch_idx,
                  )
              )
              self.fabric.call("on_before_zero_grad", optimizer)
  
              optimizer.zero_grad()
  
          else:
              # gradient accumulation -> no optimizer step
              self.training_step(
                  model=model, batch=batch, batch_idx=batch_idx
              )
  
          self.fabric.call(
              "on_train_batch_end",
              self._current_train_return,
              batch,
              batch_idx,
          )
  
          # this guard ensures, we only step the scheduler once per global step
          if should_optim_step:
              self.step_scheduler(
                  model,
                  scheduler_cfg,
                  level="step",
                  current_value=self.global_step,
              )
  
          # add output values to progress bar
          self._format_iterable(
              iterable, self._current_train_return, "train"
          )
  
          # only increase global step if optimizer stepped
          self.global_step += int(should_optim_step)
  
          # stopping criterion on step level
          if (
              self.max_steps is not None
              and self.global_step >= self.max_steps
          ):
              self.should_stop = True
              break
  
      self.fabric.call("on_train_epoch_end")
  
imports: import os
  from collections.abc import Mapping
  from functools import partial
  from typing import Any, Iterable, List, Literal, Optional, Tuple, Union, cast
  import lightning
  import torch
  from lightning.fabric.accelerators import Accelerator
  from lightning.fabric.loggers import Logger
  from lightning.fabric.strategies import Strategy
  from lightning.fabric.wrappers import _unwrap_objects
  from lightning.pytorch.utilities.model_helpers import is_overridden
  from lightning_utilities import apply_to_collection
  from tqdm import tqdm
label_desc: Use image node_id 3 for calling the MyCustomTrainer obj's underlying member method code with example usage: obj.train_loop(model, optimizer, train_loader, limit_batches, scheduler_cfg) without return types