{"record":{"id":"79ed1da618db5d1d","repo":"Lightning-AI/pytorch-lightning","slug":"invalid-value-for-every-n-epochs-self-every-n-ep","errorCode":null,"errorMessage":"Invalid value for every_n_epochs={self._every_n_epochs}. Must be >= 0","messagePattern":"Invalid value for every_n_epochs=(.+?)\\. Must be >= 0","errorType":"validation","errorClass":"MisconfigurationException","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/callbacks/model_checkpoint.py","lineNumber":674,"sourceCode":"        num_val_batches = (\n            sum(trainer.num_val_batches) if isinstance(trainer.num_val_batches, list) else trainer.num_val_batches\n        )\n        if num_val_batches == 0:\n            return True\n\n        # if the user runs validation multiple times per training epoch, then we run after validation\n        # instead of on train epoch end\n        return trainer.val_check_interval == 1.0\n\n    def __validate_init_configuration(self) -> None:\n        if self.save_top_k < -1:\n            raise MisconfigurationException(f\"Invalid value for save_top_k={self.save_top_k}. Must be >= -1\")\n        if self._every_n_train_steps < 0:\n            raise MisconfigurationException(\n                f\"Invalid value for every_n_train_steps={self._every_n_train_steps}. Must be >= 0\"\n            )\n        if self._every_n_epochs < 0:\n            raise MisconfigurationException(f\"Invalid value for every_n_epochs={self._every_n_epochs}. Must be >= 0\")\n\n        every_n_train_steps_triggered = self._every_n_train_steps >= 1\n        every_n_epochs_triggered = self._every_n_epochs >= 1\n        train_time_interval_triggered = self._train_time_interval is not None\n        if every_n_train_steps_triggered + every_n_epochs_triggered + train_time_interval_triggered > 1:\n            raise MisconfigurationException(\n                f\"Combination of parameters every_n_train_steps={self._every_n_train_steps}, \"\n                f\"every_n_epochs={self._every_n_epochs} and train_time_interval={self._train_time_interval} \"\n                \"should be mutually exclusive.\"\n            )\n\n        if self.monitor is None and self.save_top_k not in (-1, 0, 1):\n            # -1: save all epochs, 0: nothing is saved, 1: save last epoch\n            raise MisconfigurationException(\n                f\"ModelCheckpoint(save_top_k={self.save_top_k}, monitor=None) is not a valid\"\n                \" configuration. No quantity for top_k to track.\"\n            )\n","sourceCodeStart":656,"sourceCodeEnd":692,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/callbacks/model_checkpoint.py#L656-L692","documentation":"ModelCheckpoint raises this when every_n_epochs, the epoch-based checkpointing frequency, is negative. Like the other frequency parameters, 0 means 'disabled' and any value below 0 is rejected during __init__ validation (__validate_init_configuration).","triggerScenarios":"Passing every_n_epochs=-1 or any negative number to ModelCheckpoint; frequently the result of arithmetic like val_check_interval-derived values, config templating mistakes, or intending save_top_k=-1 semantics.","commonSituations":"Config files with -1 placeholders; scripts computing every_n_epochs from dataset size and accidentally producing a negative number; confusion between save_top_k's -1 convention and frequency parameters.","solutions":["Set every_n_epochs to a positive integer (e.g., 1) or 0 to disable epoch-based saving","Use save_top_k=-1 if the intent was to keep all checkpoints","Validate numeric config values before constructing the callback"],"exampleFix":"# before\nModelCheckpoint(every_n_epochs=-1)\n# after\nModelCheckpoint(every_n_epochs=1, save_top_k=-1)","handlingStrategy":"validation","validationCode":"assert cfg.get('every_n_epochs', 0) >= 0","typeGuard":"def valid_epoch_interval(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool) and v >= 0","tryCatchPattern":null,"preventionTips":["Remember -1 conventions apply only to save_top_k","Run a small config sanity check function before Trainer setup"],"tags":["pytorch-lightning","modelcheckpoint","config-validation","argument-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}