Lightning-AI/pytorch-lightning · error · RuntimeError

{self.__class__.__name__} expected {config_path} to NOT exis

Error message

{self.__class__.__name__} expected {config_path} to NOT exist. Aborting to avoid overwriting results of a previous run. You can delete the previous config file, set `LightningCLI(save_config_callback=None)` to disable config saving, or set `LightningCLI(save_config_kwargs={"overwrite": True})` to overwrite the config file.

What it means

SaveConfigCallback.setup writes config.yaml into the log dir and, unless overwrite=True, refuses to clobber an existing file: rank 0 checks existence and broadcasts the result so all ranks raise the same RuntimeError. This protects results of a previous run pointed at by the same version dir.

Source

Thrown at src/lightning/pytorch/cli.py:278

    @override
    def setup(self, trainer: Trainer, pl_module: LightningModule, stage: str) -> None:
        if self.already_saved:
            return

        if self.save_to_log_dir:
            log_dir = trainer.log_dir  # this broadcasts the directory
            assert log_dir is not None
            config_path = os.path.join(log_dir, self.config_filename)
            fs = get_filesystem(log_dir)

            if not self.overwrite:
                # check if the file exists on rank 0
                file_exists = fs.isfile(config_path) if trainer.is_global_zero else False
                # broadcast whether to fail to all ranks
                file_exists = trainer.strategy.broadcast(file_exists)
                if file_exists:
                    raise RuntimeError(
                        f"{self.__class__.__name__} expected {config_path} to NOT exist. Aborting to avoid overwriting"
                        " results of a previous run. You can delete the previous config file,"
                        " set `LightningCLI(save_config_callback=None)` to disable config saving,"
                        ' or set `LightningCLI(save_config_kwargs={"overwrite": True})` to overwrite the config file.'
                    )

            if trainer.is_global_zero:
                # save only on rank zero to avoid race conditions.
                # the `log_dir` needs to be created as we rely on the logger to do it usually
                # but it hasn't logged anything at this point
                fs.makedirs(log_dir, exist_ok=True)
                self.parser.save(
                    self.config, config_path, skip_none=False, overwrite=self.overwrite, multifile=self.multifile
                )

        if trainer.is_global_zero:
            self.save_config(trainer, pl_module, stage)
            self.already_saved = True

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Delete the existing config.yaml (or the whole version dir) before rerunning
  2. Let Lightning auto-version: omit version= so a fresh folder is used each run
  3. Set LightningCLI(save_config_kwargs={'overwrite': True})
  4. Disable saving: LightningCLI(save_config_callback=None)

Example fix

# before
cli = LightningCLI(MyModule, save_config_kwargs={})
# rerun with same log_dir -> RuntimeError
# after
cli = LightningCLI(MyModule, save_config_kwargs={"overwrite": True})
# or: rm -rf lightning_logs/version_123 before rerun
Defensive patterns

Strategy: validation

Validate before calling

from fsspec.implementations.local import LocalFileSystem
import os

def config_exists(log_dir, name='config.yaml') -> bool:
    return os.path.isfile(os.path.join(log_dir, name))

if rerun and config_exists(log_dir):
    os.remove(os.path.join(log_dir, 'config.yaml'))  # or pass overwrite=True

Prevention

When it happens

Trigger: Re-running with Trainer(default_root_dir=..., version=123) or reusing a fixed log_dir (e.g. via resume) where <log_dir>/config.yaml already exists, with LightningCLI defaults (overwrite=False).

Common situations: Manual experiment reruns with a hard-coded version number; shell scripts rerun after a crash that already wrote config.yaml; multi-rank jobs where only rank 0's check matters but all ranks must fail in sync.

Related errors


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