Lightning-AI/pytorch-lightning · error · RuntimeError

SpikeDetection requires `torchmetrics>=1.0.0` Please upgrade

Error message

SpikeDetection requires `torchmetrics>=1.0.0` Please upgrade your version.

What it means

Lightning's `SpikeDetection` callback/metric uses `torchmetrics.aggregation.MeanMetric` and `torchmetrics.wrappers.Running`, both introduced in torchmetrics 1.0. On import at construction time it checks `_TORCHMETRICS_GREATER_EQUAL_1_0_0` and raises RuntimeError if the installed torchmetrics is older (0.11.x and below).

Source

Thrown at src/lightning/fabric/utilities/spike.py:52

        finite_only: If set to ``False``, consider non-finite values like NaN, inf and -inf a spike as well.

    """

    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

    @torch.no_grad()

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Upgrade: `pip install -U torchmetrics` (>=1.0.0)
  2. Check for conflicting pins: `pip check` / `pip install -U lightning` to let it pull a compatible torchmetrics
  3. If you must stay on torchmetrics 0.x, remove the SpikeDetection callback

Example fix

# before  (torchmetrics 0.11)
trainer = Trainer(callbacks=[SpikeDetection()])  # RuntimeError

# after
# pip install -U "torchmetrics>=1.0.0"
trainer = Trainer(callbacks=[SpikeDetection()])
Defensive patterns

Strategy: validation

Validate before calling

import torchmetrics
from packaging.version import Version
assert Version(torchmetrics.__version__) >= Version("1.0.0")

Prevention

When it happens

Trigger: Instantiating `SpikeDetection(...)` (usually as a Trainer callback) with torchmetrics < 1.0.0 installed, e.g. pinned by an old environment or another package constraint.

Common situations: Old conda/pip environments, `lightning` installed alongside legacy `pytorch-lightning` pins that force torchmetrics 0.x, CI images with stale dependencies.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/7a220b7337b40d37. Report an issue: GitHub.