docstr: Exemplary Trainer with Fabric. This is a very simple trainer focused on readablity but with reduced
  featureset. As a trainer with more included features, we recommend using the
  :class:`lightning.pytorch.Trainer`.
  
  Args:
      accelerator: The hardware to run on. Possible choices are:
          ``"cpu"``, ``"cuda"``, ``"mps"``, ``"gpu"``, ``"tpu"``, ``"auto"``.
      strategy: Strategy for how to run across multiple devices. Possible choices are:
          ``"dp"``, ``"ddp"``, ``"ddp_spawn"``, ``"deepspeed"``, ``"fsdp"``.
      devices: Number of devices to train on (``int``),
          which GPUs to train on (``list`` or ``str``), or ``"auto"``.
          The value applies per node.
      precision: Double precision (``"64"``), full precision (``"32"``), half precision AMP (``"16-mixed"``),
          or bfloat16 precision AMP (``"bf16-mixed"``).
      plugins: One or several custom plugins
      callbacks: A single callback or a list of callbacks. The following hooks are supported:
          - on_train_epoch_start
          - on train_epoch_end
          - on_train_batch_start
          - on_train_batch_end
          - on_before_backward
          - on_after_backward
          - on_before_zero_grad
          - on_before_optimizer_step
          - on_validation_model_eval
          - on_validation_model_train
          - on_validation_epoch_start
          - on_validation_epoch_end
          - on_validation_batch_start
          - on_validation_batch_end
  
      loggers: A single logger or a list of loggers. See :meth:`~lightning.fabric.fabric.Fabric.log` for more
          information.
  
      max_epochs: The maximum number of epochs to train
      max_steps: The maximum number of (optimizer) steps to train
      grad_accum_steps: How many batches to process before each optimizer step
      limit_train_batches: Limits the number of train batches per epoch
          If greater than number of batches in the dataloader, this has no effect.
      limit_val_batches: Limits the number of validation batches per epoch.
          If greater than number of batches in the dataloader, this has no effect.
      validation_frequency: How many epochs to run before each validation epoch.
      use_distributed_sampler: Wraps the sampler of each dataloader with a respective distributed-aware sampler
          in case of distributed training.
      checkpoint_dir: Directory to store checkpoints to.
      checkpoint_frequency: How many epochs to run before each checkpoint is written.
  
  Warning:
      callbacks written for the lightning trainer (especially making assumptions on the trainer), won't work!
docstr_tok: ["Exemplary","Trainer","with","Fabric",".","This","is","a","very","simple","trainer","focused","on","readablity","but","with","reduced","featureset",".","As","a","trainer","with","more","included","features",",","we","recommend","using","the",":","class",":","`","lightning.pytorch.Trainer","`",".","Args",":","accelerator",":","The","hardware","to","run","on",".","Possible","choices","are",":","``","``","cpu","''","``",",","``","``","cuda","''","``",",","``","``","mps","''","``",",","``","``","gpu","''","``",",","``","``","tpu","''","``",",","``","``","auto","''","``",".","strategy",":","Strategy","for","how","to","run","across","multiple","devices",".","Possible","choices","are",":","``","``","dp","''","``",",","``","``","ddp","''","``",",","``","``","ddp_spawn","''","``",",","``","``","deepspeed","''","``",",","``","``","fsdp","''","``",".","devices",":","Number","of","devices","to","train","on","(","``","int","``",")",",","which","GPUs","to","train","on","(","``","list","``","or","``","str","``",")",",","or","``","``","auto","''","``",".","The","value","applies","per","node",".","precision",":","Double","precision","(","``","``","64","''","``",")",",","full","precision","(","``","``","32","''","``",")",",","half","precision","AMP","(","``","``","16-mixed","''","``",")",",","or","bfloat16","precision","AMP","(","``","``","bf16-mixed","''","``",")",".","plugins",":","One","or","several","custom","plugins","callbacks",":","A","single","callback","or","a","list","of","callbacks",".","The","following","hooks","are","supported",":","-","on_train_epoch_start","-","on","train_epoch_end","-","on_train_batch_start","-","on_train_batch_end","-","on_before_backward","-","on_after_backward","-","on_before_zero_grad","-","on_before_optimizer_step","-","on_validation_model_eval","-","on_validation_model_train","-","on_validation_epoch_start","-","on_validation_epoch_end","-","on_validation_batch_start","-","on_validation_batch_end","loggers",":","A","single","logger","or","a","list","of","loggers",".","See",":","meth",":","`","~lightning.fabric.fabric.Fabric.log","`","for","more","information",".","max_epochs",":","The","maximum","number","of","epochs","to","train","max_steps",":","The","maximum","number","of","(","optimizer",")","steps","to","train","grad_accum_steps",":","How","many","batches","to","process","before","each","optimizer","step","limit_train_batches",":","Limits","the","number","of","train","batches","per","epoch","If","greater","than","number","of","batches","in","the","dataloader",",","this","has","no","effect",".","limit_val_batches",":","Limits","the","number","of","validation","batches","per","epoch",".","If","greater","than","number","of","batches","in","the","dataloader",",","this","has","no","effect",".","validation_frequency",":","How","many","epochs","to","run","before","each","validation","epoch",".","use_distributed_sampler",":","Wraps","the","sampler","of","each","dataloader","with","a","respective","distributed-aware","sampler","in","case","of","distributed","training",".","checkpoint_dir",":","Directory","to","store","checkpoints","to",".","checkpoint_frequency",":","How","many","epochs","to","run","before","each","checkpoint","is","written",".","Warning",":","callbacks","written","for","the","lightning","trainer","(","especially","making","assumptions","on","the","trainer",")",",","wo","n't","work","!"]
code: def __init__(
      self,
      accelerator: Union[str, Accelerator] = "auto",
      strategy: Union[str, Strategy] = "auto",
      devices: Union[List[int], str, int] = "auto",
      precision: Union[str, int] = "32-true",
      plugins: Optional[Union[str, Any]] = None,
      callbacks: Optional[Union[List[Any], Any]] = None,
      loggers: Optional[Union[Logger, List[Logger]]] = None,
      max_epochs: Optional[int] = 1000,
      max_steps: Optional[int] = None,
      grad_accum_steps: int = 1,
      limit_train_batches: Union[int, float] = float("inf"),
      limit_val_batches: Union[int, float] = float("inf"),
      validation_frequency: int = 1,
      use_distributed_sampler: bool = True,
      checkpoint_dir: str = "./checkpoints",
      checkpoint_frequency: int = 1,
  ) -> None:
      """Exemplary Trainer with Fabric. This is a very simple trainer focused on readablity but with reduced
      featureset. As a trainer with more included features, we recommend using the
      :class:`lightning.pytorch.Trainer`.
  
      Args:
          accelerator: The hardware to run on. Possible choices are:
              ``"cpu"``, ``"cuda"``, ``"mps"``, ``"gpu"``, ``"tpu"``, ``"auto"``.
          strategy: Strategy for how to run across multiple devices. Possible choices are:
              ``"dp"``, ``"ddp"``, ``"ddp_spawn"``, ``"deepspeed"``, ``"fsdp"``.
          devices: Number of devices to train on (``int``),
              which GPUs to train on (``list`` or ``str``), or ``"auto"``.
              The value applies per node.
          precision: Double precision (``"64"``), full precision (``"32"``), half precision AMP (``"16-mixed"``),
              or bfloat16 precision AMP (``"bf16-mixed"``).
          plugins: One or several custom plugins
          callbacks: A single callback or a list of callbacks. The following hooks are supported:
              - on_train_epoch_start
              - on train_epoch_end
              - on_train_batch_start
              - on_train_batch_end
              - on_before_backward
              - on_after_backward
              - on_before_zero_grad
              - on_before_optimizer_step
              - on_validation_model_eval
              - on_validation_model_train
              - on_validation_epoch_start
              - on_validation_epoch_end
              - on_validation_batch_start
              - on_validation_batch_end
  
          loggers: A single logger or a list of loggers. See :meth:`~lightning.fabric.fabric.Fabric.log` for more
              information.
  
          max_epochs: The maximum number of epochs to train
          max_steps: The maximum number of (optimizer) steps to train
          grad_accum_steps: How many batches to process before each optimizer step
          limit_train_batches: Limits the number of train batches per epoch
              If greater than number of batches in the dataloader, this has no effect.
          limit_val_batches: Limits the number of validation batches per epoch.
              If greater than number of batches in the dataloader, this has no effect.
          validation_frequency: How many epochs to run before each validation epoch.
          use_distributed_sampler: Wraps the sampler of each dataloader with a respective distributed-aware sampler
              in case of distributed training.
          checkpoint_dir: Directory to store checkpoints to.
          checkpoint_frequency: How many epochs to run before each checkpoint is written.
  
      Warning:
          callbacks written for the lightning trainer (especially making assumptions on the trainer), won't work!
  
      """
  
      self.fabric = L.Fabric(
          accelerator=accelerator,
          strategy=strategy,
          devices=devices,
          precision=precision,
          plugins=plugins,
          callbacks=callbacks,
          loggers=loggers,
      )
      self.global_step = 0
      self.grad_accum_steps: int = grad_accum_steps
      self.current_epoch = 0
  
      self.max_epochs = max_epochs
      self.max_steps = max_steps
      self.should_stop = False
  
      # ensures limit_X_batches is either int or inf
      if not isinstance(limit_train_batches, int):
          assert limit_train_batches == float("inf")
  
      if not isinstance(limit_val_batches, int):
          assert limit_val_batches == float("inf")
  
      self.limit_train_batches = limit_train_batches
      self.limit_val_batches = limit_val_batches
      self.validation_frequency = validation_frequency
      self.use_distributed_sampler = use_distributed_sampler
      self._current_train_return: Union[
          torch.Tensor, Mapping[str, Any]
      ] = {}
      self._current_val_return: Optional[
          Union[torch.Tensor, Mapping[str, Any]]
      ] = {}
  
      self.checkpoint_dir = checkpoint_dir
      self.checkpoint_frequency = checkpoint_frequency
  
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 1 to create a new MyCustomTrainer object  with example: obj = MyCustomTrainer(accelerator, strategy, devices, precision, plugins, callbacks, loggers, max_epochs, max_steps, grad_accum_steps, limit_train_batches, limit_val_batches, validation_frequency, use_distributed_sampler, checkpoint_dir, checkpoint_frequency)