Lightning-AI/pytorch-lightning · error · RuntimeError

DataFetcher is unsupported for {trainer.state.stage}

Error message

DataFetcher is unsupported for {trainer.state.stage}

What it means

Raised by _select_data_fetcher when the current trainer running stage is not one of TRAINING, VALIDATING, SANITY_CHECKING, or PREDICTING. The data fetcher must be chosen based on the step function name for a known stage; an unrecognized/None stage (e.g. testing or an unset state) cannot be mapped to a step hook.

Source

Thrown at src/lightning/pytorch/loops/utilities.py:146

    for v in vars(loop).values():
        if isinstance(v, _BaseProgress):
            v.reset()
        elif isinstance(v, _Loop):
            _reset_progress(v)


def _select_data_fetcher(trainer: "pl.Trainer", stage: RunningStage) -> _DataFetcher:
    lightning_module = trainer.lightning_module
    if stage == RunningStage.TESTING:
        step_fx_name = "test_step"
    elif stage == RunningStage.TRAINING:
        step_fx_name = "training_step"
    elif stage in (RunningStage.VALIDATING, RunningStage.SANITY_CHECKING):
        step_fx_name = "validation_step"
    elif stage == RunningStage.PREDICTING:
        step_fx_name = "predict_step"
    else:
        raise RuntimeError(f"DataFetcher is unsupported for {trainer.state.stage}")
    step_fx = getattr(lightning_module, step_fx_name)
    if is_param_in_hook_signature(step_fx, "dataloader_iter", explicit=True):
        rank_zero_warn(
            f"Found `dataloader_iter` argument in the `{step_fx_name}`. Note that the support for "
            "this signature is experimental and the behavior is subject to change."
        )
        return _DataLoaderIterDataFetcher()
    return _PrefetchDataFetcher()


def _no_grad_context(loop_run: Callable) -> Callable:
    def _decorator(self: _Loop, *args: Any, **kwargs: Any) -> Any:
        if not isinstance(self, _Loop):
            raise TypeError(f"`{type(self).__name__}` needs to be a Loop.")
        if not hasattr(self, "inference_mode"):
            raise TypeError(f"`{type(self).__name__}.inference_mode` needs to be defined")
        context_manager: type[AbstractContextManager]
        if _distributed_is_initialized() and dist.get_backend() == "gloo":

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Use the public entry points trainer.fit/validate/predict rather than driving loops manually
  2. If writing a custom loop, set trainer.state.stage to a supported RunningStage before data setup
  3. Align lightning package versions (pip check; reinstall matching versions)
Defensive patterns

Strategy: validation

Validate before calling

from lightning.pytorch.trainer.states import RunningStage
assert trainer.state.stage in (
    RunningStage.TRAINING, RunningStage.VALIDATING,
    RunningStage.SANITY_CHECKING, RunningStage.PREDICTING,
), f'unsupported stage {trainer.state.stage}'

Type guard

def stage_supported(stage) -> bool:
    return stage in {RunningStage.TRAINING, RunningStage.VALIDATING, RunningStage.SANITY_CHECKING, RunningStage.PREDICTING}

Prevention

When it happens

Trigger: Internal/library misuse such as invoking a loop's reset/setup_data while trainer.state.stage is None or TESTING; custom loops running outside the four supported stages; calling internal loop APIs directly instead of via trainer.fit/validate/predict.

Common situations: Subclassing Lightning loops or calling private APIs in plugins/callbacks; version mismatches between lightning core and a plugin expecting different stage enumeration.

Related errors


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