class_docstr: Change gradient accumulation factor according to scheduling.
  
  Args:
      scheduling: scheduling in format {epoch: accumulation_factor}
  
  Note:
      The argument scheduling is a dictionary. Each key represent an epoch and
      its associated accumulation factor value.
      Warning: Epoch are zero-indexed c.f it means if you want to change
      the accumulation factor after 4 epochs, set ``Trainer(accumulate_grad_batches={4: factor})``
      or ``GradientAccumulationScheduler(scheduling={4: factor})``.
      For more info check the example below.
  
  Raises:
      TypeError:
          If ``scheduling`` is an empty ``dict``,
          or not all keys and values of ``scheduling`` are integers.
      IndexError:
          If ``minimal_epoch`` is less than 0.
  
  Example::
  
      >>> from lightning.pytorch import Trainer
      >>> from lightning.pytorch.callbacks import GradientAccumulationScheduler
  
      # from epoch 5, it starts accumulating every 2 batches. Here we have 4 instead of 5
      # because epoch (key) should be zero-indexed.
      >>> accumulator = GradientAccumulationScheduler(scheduling={4: 2})
      >>> trainer = Trainer(callbacks=[accumulator])
class_docstr_tok: ["Change","gradient","accumulation","factor","according","to","scheduling",".","Args",":","scheduling",":","scheduling","in","format","{","epoch",":","accumulation_factor","}","Note",":","The","argument","scheduling","is","a","dictionary",".","Each","key","represent","an","epoch","and","its","associated","accumulation","factor","value",".","Warning",":","Epoch","are","zero-indexed","c.f","it","means","if","you","want","to","change","the","accumulation","factor","after","4","epochs",",","set","``","Trainer","(","accumulate_grad_batches=","{","4",":","factor","}",")","``","or","``","GradientAccumulationScheduler","(","scheduling=","{","4",":","factor","}",")","``",".","For","more","info","check","the","example","below",".","Raises",":","TypeError",":","If","``","scheduling","``","is","an","empty","``","dict","``",",","or","not","all","keys","and","values","of","``","scheduling","``","are","integers",".","IndexError",":","If","``","minimal_epoch","``","is","less","than","0",".","Example",":",":",">",">",">","from","lightning.pytorch","import","Trainer",">",">",">","from","lightning.pytorch.callbacks","import","GradientAccumulationScheduler","#","from","epoch","5",",","it","starts","accumulating","every","2","batches",".","Here","we","have","4","instead","of","5","#","because","epoch","(","key",")","should","be","zero-indexed",".",">",">",">","accumulator","=","GradientAccumulationScheduler","(","scheduling=","{","4",":","2","}",")",">",">",">","trainer","=","Trainer","(","callbacks=","[","accumulator","]",")"]
docstr: Performns a configuration validation before training starts and raises errors for incompatible settings.
docstr_tok: ["Performns","a","configuration","validation","before","training","starts","and","raises","errors","for","incompatible","settings","."]
code: def on_train_start(
      self, trainer: "pl.Trainer", pl_module: "pl.LightningModule"
  ) -> None:
      """Performns a configuration validation before training starts and raises errors for incompatible settings."""
  
      if not pl_module.automatic_optimization:
          raise RuntimeError(
              """Automatic gradient accumulation and the `GradientAccumulationScheduler` is not supported for
                  manual optimization. Please remove the callback or switch to automatic optimization."""
          )
  
      overridden_optimizer_step = is_overridden(
          "optimizer_step", pl_module
      )
      overridden_optimizer_zero_grad = is_overridden(
          "optimizer_zero_grad", pl_module
      )
      going_to_accumulate_grad_batches = (
          self.going_to_accumulate_grad_batches()
      )
      has_overridden_optimization_functions = (
          overridden_optimizer_step or overridden_optimizer_zero_grad
      )
      if (
          has_overridden_optimization_functions
          and going_to_accumulate_grad_batches
      ):
          rank_zero_warn(
              "When using `Trainer(accumulate_grad_batches != 1)` and overriding"
              " `LightningModule.optimizer_{step,zero_grad}`, the hooks will not be called on every batch"
              " (rather, they are called on every optimization step)."
          )
  
      # local import to avoid circular import
      from lightning.pytorch.strategies import DeepSpeedStrategy
  
      unsupported_strategies = [DeepSpeedStrategy]
      if _LIGHTNING_COLOSSALAI_AVAILABLE:
          from lightning_colossalai import ColossalAIStrategy
  
          unsupported_strategies.append(ColossalAIStrategy)
  
      if isinstance(trainer.strategy, tuple(unsupported_strategies)):
          raise RuntimeError(
              f"The `{type(trainer.strategy).__name__}` does not support `accumulate_grad_batches` changing"
              " between epochs."
          )
      if trainer.accumulate_grad_batches != 1:
          raise ValueError(
              "You have set `accumulate_grad_batches` and are using the `GradientAccumulationScheduler`"
              " callback. Either remove `accumulate_grad_batches` from the Trainer or remove the callback."
          )
  
file_path: pytorch-lightning/src/lightning/pytorch/callbacks/gradient_accumulation_scheduler.py
imports: from typing import Any, Dict
  from typing_extensions import override
  import lightning.pytorch
  from lightning.pytorch.callbacks.callback import Callback
  from lightning.pytorch.utilities.exceptions import MisconfigurationException
  from lightning.pytorch.utilities.imports import _LIGHTNING_COLOSSALAI_AVAILABLE
  from lightning.pytorch.utilities.model_helpers import is_overridden
  from lightning.pytorch.utilities.rank_zero import rank_zero_warn
label_desc: Use image node_id 4 for calling the GradientAccumulationScheduler obj's underlying member method code with example usage: obj.on_train_start(trainer, pl_module) without return types