Lightning-AI/pytorch-lightning · error · MisconfigurationException
The `avg_fn` should be callable.
Error message
The `avg_fn` should be callable.
What it means
StochasticWeightAveraging accepts an optional custom averaging function avg_fn (passed to torch's SWA utils), which must be callable. Passing anything non-callable (a lambda result, a string, None-check passes) raises this MisconfigurationException in __init__.
Source
Thrown at src/lightning/pytorch/callbacks/stochastic_weight_avg.py:113
When None is provided, it will infer the `device` from ``pl_module``.
(default: ``"cpu"``)
"""
err_msg = "swa_epoch_start should be a >0 integer or a float between 0 and 1."
if isinstance(swa_epoch_start, int) and swa_epoch_start < 1:
raise MisconfigurationException(err_msg)
if isinstance(swa_epoch_start, float) and not (0 <= swa_epoch_start <= 1):
raise MisconfigurationException(err_msg)
wrong_type = not isinstance(swa_lrs, (float, list))
wrong_float = isinstance(swa_lrs, float) and swa_lrs <= 0
wrong_list = isinstance(swa_lrs, list) and not all(lr > 0 and isinstance(lr, float) for lr in swa_lrs)
if wrong_type or wrong_float or wrong_list:
raise MisconfigurationException("The `swa_lrs` should a positive float, or a list of positive floats")
if avg_fn is not None and not callable(avg_fn):
raise MisconfigurationException("The `avg_fn` should be callable.")
if device is not None and not isinstance(device, (torch.device, str)):
raise MisconfigurationException(f"device is expected to be a torch.device or a str. Found {device}")
self.n_averaged: Optional[Tensor] = None
self._swa_epoch_start = swa_epoch_start
self._swa_lrs = swa_lrs
self._annealing_epochs = annealing_epochs
self._annealing_strategy = annealing_strategy
self._avg_fn = avg_fn or self.avg_fn
self._device = device
self._model_contains_batch_norm: Optional[bool] = None
self._average_model: Optional[pl.LightningModule] = None
self._initialized = False
self._swa_scheduler: Optional[LRScheduler] = None
self._scheduler_state: Optional[dict] = None
self._init_n_averaged = 0
self._latest_update_epoch = -1View on GitHub (pinned to 9fed5c27d2)
Solutions
- Pass a callable with signature avg_fn(averaged_model_parameter, model_parameter, num_averaged) -> tensor, e.g. torch.optim.swa_utils.get_ema_multi_avg_fn(0.9)
- Or omit avg_fn to use the default equal averaging
Example fix
# before swa = SWA(avg_fn=0.9) # after from torch.optim.swa_utils import get_ema_multi_avg_fn swa = SWA(avg_fn=get_ema_multi_avg_fn(0.9))
Defensive patterns
Strategy: type-guard
Validate before calling
import torch
avg_fn = None if cfg.ema is None else __import__('torch.optim.swa_utils', fromlist=['x']).get_ema_multi_avg_fn(cfg.ema)
assert avg_fn is None or callable(avg_fn)
swa = SWA(avg_fn=avg_fn) Type guard
def is_valid_avg_fn(fn) -> bool:
return fn is None or callable(fn) Prevention
- Use factory helpers (get_ema_multi_avg_fn) to build avg_fn
- Keep avg_fn as a top-level or lambda callable, never a scalar
When it happens
Trigger: SWA(avg_fn=0.5) or avg_fn=some_object that isn't a function/callable class instance.
Common situations: Confusing avg_fn parameters with a float weight; passing an unpickled/serialized function reference that lost callability.
Related errors
- swa_epoch_start should be a >0 integer or a float between 0
- device is expected to be a torch.device or a str. Found {dev
- The `swa_lrs` should a positive float, or a list of positive
- SWA currently works with 1 `optimizer`.
- SWA currently not supported for more than 1 `lr_scheduler`.
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/1d5a92f7240eb98f.
Report an issue: GitHub.