class_docstr: Spike Detection Callback.
  
  Terminates training with a ``TrainingSpikeException`` when a loss-spike was detected and
  saves the batches to skip when resuming to a file.
  
  We skip the current and the previous batch since it is unclear whether the previous batch
  altered the weights in a way that it causes the spike or just the current batch is corrupted somehow.
  
  Args:
      mode: Whether to minimize or maximize the tracked metric
      window: A running mean of metrics with ``window`` size. Serves as reference value for spikes.
      warmup: After how many batches spike-tracking should start
      atol: An absolute tolerance.  Every diff between the running mean and the current value,
          that's not an improvement and above ``atol`` will be considered a spike
      rtol: A relative tolerance. Every diff between the running mean and the current value,
          that's higher than ``rtol * running_mean`` is considered a spike
      exclude_batches_path: Where to save the file that contains the batches to exclude.
          Will default to current directory.
      finite_only: If set to ``False``, consider non-finite values like NaN, inf and -inf a spike as well.
code: def __init__(
      self,
      mode: Literal["min", "max"] = "min",
      window: int = 10,
      warmup: int = 1,
      atol: Optional[float] = None,
      rtol: Optional[float] = 2.0,
      exclude_batches_path: Optional[_PATH] = None,
      finite_only: bool = True,
  ):
      if _TORCHMETRICS_GREATER_EQUAL_1_0_0:
          from torchmetrics.aggregation import MeanMetric
          from torchmetrics.wrappers import Running
      else:
          raise RuntimeError(
              "SpikeDetection requires torchmetrics>=1.0.0! Please upgrade your version!"
          )
      super().__init__()
  
      self.last_val: Union[torch.Tensor, float] = 0.0
      # spike detection happens individually on each machine
      self.running_mean = Running(
          MeanMetric(dist_sync_on_step=False, sync_on_compute=False),
          window=window,
      )
      # workaround for https://github.com/Lightning-AI/torchmetrics/issues/1899
      self.running_mean.dist_sync_on_step = False
      self.running_mean.sync_on_compute = False
  
      self.mode = mode
      self.warmup = warmup
      self.atol = atol
      self.rtol = rtol
      self.bad_batches: List[int] = []
      self.exclude_batches_path = exclude_batches_path
      self.finite_only = finite_only
  
imports: import json
  import operator
  import os
  import warnings
  from typing import TYPE_CHECKING, Any, Dict, List, Literal, Optional, Union
  import torch
  from lightning_utilities.core.imports import compare_version
  from lightning.fabric.utilities.types import _PATH
label_desc: Use image node_id 1 to create a new SpikeDetection object  with example: obj = SpikeDetection(mode, window, warmup, atol, rtol, exclude_batches_path, finite_only)