Lightning-AI/pytorch-lightning · error · MisconfigurationException

m.format("on_step", on_step, fx_name, fx_config["allowed_on_

Error message

m.format("on_step", on_step, fx_name, fx_config["allowed_on_step"])

What it means

Each logging-enabled hook defines allowed_on_step / allowed_on_epoch sets. When self.log(..., on_step=X) is called with a value not permitted for the current hook (e.g. on_step=True in validation_step, whose default machinery is epoch-based), check_logging_levels raises this MisconfigurationException with the allowed values listed in the message.

Source

Thrown at src/lightning/pytorch/trainer/connectors/logger_connector/fx_validator.py:185

    def get_default_logging_levels(
        cls, fx_name: str, on_step: Optional[bool], on_epoch: Optional[bool]
    ) -> tuple[bool, bool]:
        """Return default logging levels for given hook."""
        fx_config = cls.functions[fx_name]
        assert fx_config is not None
        on_step = fx_config["default_on_step"] if on_step is None else on_step
        on_epoch = fx_config["default_on_epoch"] if on_epoch is None else on_epoch
        return on_step, on_epoch

    @classmethod
    def check_logging_levels(cls, fx_name: str, on_step: bool, on_epoch: bool) -> None:
        """Check if the logging levels are allowed in the given hook."""
        fx_config = cls.functions[fx_name]
        assert fx_config is not None
        m = "You can't `self.log({}={})` inside `{}`, must be one of {}."
        if on_step not in fx_config["allowed_on_step"]:
            msg = m.format("on_step", on_step, fx_name, fx_config["allowed_on_step"])
            raise MisconfigurationException(msg)

        if on_epoch not in fx_config["allowed_on_epoch"]:
            msg = m.format("on_epoch", on_epoch, fx_name, fx_config["allowed_on_epoch"])
            raise MisconfigurationException(msg)

    @classmethod
    def check_logging_and_get_default_levels(
        cls, fx_name: str, on_step: Optional[bool], on_epoch: Optional[bool]
    ) -> tuple[bool, bool]:
        """Check if the given hook name is allowed to log and return logging levels."""
        cls.check_logging(fx_name)
        on_step, on_epoch = cls.get_default_logging_levels(fx_name, on_step, on_epoch)
        cls.check_logging_levels(fx_name, on_step, on_epoch)
        return on_step, on_epoch

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Use the value from the error message's allowed set, e.g. in validation_step: self.log('x', x, on_step=False, on_epoch=True)
  2. Omit on_step/on_epoch to accept the hook's defaults via check_logging_and_get_default_levels
  3. Move step-level logging of that metric into training_step where on_step=True is allowed

Example fix

# before
def validation_step(self, batch, batch_idx):
    self.log('val_loss', loss, on_step=True)  # not allowed

# after
def validation_step(self, batch, batch_idx):
    self.log('val_loss', loss, on_step=False, on_epoch=True)
Defensive patterns

Strategy: validation

Validate before calling

from lightning.pytorch.trainer.connectors.logger_connector.fx_validator import _FxValidator
cfg = _FxValidator.functions[hook_name]
assert on_step in cfg["allowed_on_step"], f"allowed: {cfg['allowed_on_step']}"

Type guard

def step_allowed(hook_name: str, on_step: bool) -> bool:
    from lightning.pytorch.trainer.connectors.logger_connector import fx_validator
    cfg = fx_validator._FxValidator.functions[hook_name]
    return cfg is not None and on_step in cfg["allowed_on_step"]

Prevention

When it happens

Trigger: self.log('x', x, on_step=True) inside validation_step or test_step where allowed_on_step is False; more generally any on_step value outside fx_config['allowed_on_step'] for the active hook.

Common situations: Copying a training_step logging line into validation_step and forgetting to flip on_step; setting on_step=True, on_epoch=False for metrics that Lightning only supports as epoch aggregates; predict_step logging.

Related errors


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