docstr: The main entrypoint of the trainer, triggering the actual training.
  
  Args:
      model: the LightningModule to train.
          Can have the same hooks as :attr:`callbacks` (see :meth:`MyCustomTrainer.__init__`).
      train_loader: the training dataloader. Has to be an iterable returning batches.
      val_loader: the validation dataloader. Has to be an iterable returning batches.
          If not specified, no validation will run.
      ckpt_path: Path to previous checkpoints to resume training from.
          If specified, will always look for the latest checkpoint within the given directory.
docstr_tok: ["The","main","entrypoint","of","the","trainer",",","triggering","the","actual","training",".","Args",":","model",":","the","LightningModule","to","train",".","Can","have","the","same","hooks","as",":","attr",":","`","callbacks","`","(","see",":","meth",":","`","MyCustomTrainer.__init__","`",")",".","train_loader",":","the","training","dataloader",".","Has","to","be","an","iterable","returning","batches",".","val_loader",":","the","validation","dataloader",".","Has","to","be","an","iterable","returning","batches",".","If","not","specified",",","no","validation","will","run",".","ckpt_path",":","Path","to","previous","checkpoints","to","resume","training","from",".","If","specified",",","will","always","look","for","the","latest","checkpoint","within","the","given","directory","."]
code: def fit(
      self,
      model: L.LightningModule,
      train_loader: torch.utils.data.DataLoader,
      val_loader: torch.utils.data.DataLoader,
      ckpt_path: Optional[str] = None,
  ):
      """The main entrypoint of the trainer, triggering the actual training.
  
      Args:
          model: the LightningModule to train.
              Can have the same hooks as :attr:`callbacks` (see :meth:`MyCustomTrainer.__init__`).
          train_loader: the training dataloader. Has to be an iterable returning batches.
          val_loader: the validation dataloader. Has to be an iterable returning batches.
              If not specified, no validation will run.
          ckpt_path: Path to previous checkpoints to resume training from.
              If specified, will always look for the latest checkpoint within the given directory.
  
      """
      self.fabric.launch()
  
      # setup dataloaders
      train_loader = self.fabric.setup_dataloaders(
          train_loader,
          use_distributed_sampler=self.use_distributed_sampler,
      )
      if val_loader is not None:
          val_loader = self.fabric.setup_dataloaders(
              val_loader,
              use_distributed_sampler=self.use_distributed_sampler,
          )
  
      # setup model and optimizer
      if isinstance(
          self.fabric.strategy, L.fabric.strategies.fsdp.FSDPStrategy
      ):
          # currently, there is no way to support fsdp with model.configure_optimizers in fabric
          # as it would require fabric to hold a reference to the model, which we don't want to.
          raise NotImplementedError(
              "BYOT currently does not support FSDP"
          )
  
      optimizer, scheduler_cfg = self._parse_optimizers_schedulers(
          model.configure_optimizers()
      )
      assert optimizer is not None
      model, optimizer = self.fabric.setup(model, optimizer)
  
      # assemble state (current epoch and global step will be added in save)
      state = {
          "model": model,
          "optim": optimizer,
          "scheduler": scheduler_cfg,
      }
  
      # load last checkpoint if available
      if ckpt_path is not None and os.path.isdir(ckpt_path):
          latest_checkpoint_path = self.get_latest_checkpoint(
              self.checkpoint_dir
          )
          if latest_checkpoint_path is not None:
              self.load(state, latest_checkpoint_path)
  
              # check if we even need to train here
              if (
                  self.max_epochs is not None
                  and self.current_epoch >= self.max_epochs
              ):
                  self.should_stop = True
  
      while not self.should_stop:
          self.train_loop(
              model,
              optimizer,
              train_loader,
              limit_batches=self.limit_train_batches,
              scheduler_cfg=scheduler_cfg,
          )
  
          if self.should_validate:
              self.val_loop(
                  model,
                  val_loader,
                  limit_batches=self.limit_val_batches,
              )
  
          self.step_scheduler(
              model,
              scheduler_cfg,
              level="epoch",
              current_value=self.current_epoch,
          )
  
          self.current_epoch += 1
  
          # stopping condition on epoch level
          if (
              self.max_epochs is not None
              and self.current_epoch >= self.max_epochs
          ):
              self.should_stop = True
  
          self.save(state)
  
      # reset for next fit call
      self.should_stop = False
  
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 2 for calling the MyCustomTrainer obj's underlying member method code with example usage: obj.fit(model, train_loader, val_loader, ckpt_path) without return types