Repository: Lightning-AI/pytorch-lightning
File: pytorch_lightning/callbacks/model_checkpoint.py (Python)

Code before:
        self.save_function = None
        self.warned_result_obj = False

        if save_top_k is None and monitor is not None:
            self.save_top_k = 1

        self.__init_monitor_mode(monitor, mode)
        self.__init_ckpt_dir(filepath, dirpath, filename, save_top_k)
        self.__validate_init_configuration()

    def on_pretrain_routine_start(self, trainer, pl_module):
        """
        When pretrain routine starts we build the ckpt dir on the fly
        """
        self.__resolve_ckpt_dir(trainer, pl_module)
        self.save_function = trainer.save_checkpoint

    def on_validation_end(self, trainer, pl_module):
        """
        checkpoints can be saved at the end of the val loop
        """
        self.save_checkpoint(trainer, pl_module)

    def on_save_checkpoint(self, trainer, pl_module) -> Dict[str, Any]:
        return {
            "best_model_monitor": self.monitor,
            "best_model_score": self.best_model_score,
            "best_model_path": self.best_model_path,
        }

    def on_load_checkpoint(self, checkpointed_state: Dict[str, Any]):
        self.best_model_score = checkpointed_state["best_model_score"]
        self.best_model_path = checkpointed_state["best_model_path"]

    def save_checkpoint(self, trainer, pl_module):
        """
        Performs the main logic around saving a checkpoint.
        This method runs on all ranks, it is the responsibility of `self.save_function`
        to handle correct behaviour in distributed training, i.e., saving only on rank 0.
        """
        epoch = trainer.current_epoch
        global_step = trainer.global_step

        if (
            self.save_top_k == 0  # no models are saved
            or self.period < 1  # no models are saved
            or (epoch + 1) % self.period  # skip epoch
            or trainer.running_sanity_check  # don't save anything during sanity check
            or self.last_global_step_saved == global_step  # already saved at the last step
        ):
            return

Review comment (suggestion):
this can just be `monitor` right? not sure what makes it the "best" since it'll be the same value in each checkpoint
```suggestion
            "monitor": self.monitor,
```

Code after:
        self.save_function = None
        self.warned_result_obj = False

        if save_top_k is None and monitor is not None:
            self.save_top_k = 1

        self.__init_monitor_mode(monitor, mode)
        self.__init_ckpt_dir(filepath, dirpath, filename, save_top_k)
        self.__validate_init_configuration()

    def on_pretrain_routine_start(self, trainer, pl_module):
        """
        When pretrain routine starts we build the ckpt dir on the fly
        """
        self.__resolve_ckpt_dir(trainer, pl_module)
        self.save_function = trainer.save_checkpoint

    def on_validation_end(self, trainer, pl_module):
        """
        checkpoints can be saved at the end of the val loop
        """
        self.save_checkpoint(trainer, pl_module)

    def on_save_checkpoint(self, trainer, pl_module) -> Dict[str, Any]:
        return {
            "monitor": self.monitor,
            "best_model_score": self.best_model_score,
            "best_model_path": self.best_model_path,
        }

    def on_load_checkpoint(self, checkpointed_state: Dict[str, Any]):
        self.best_model_score = checkpointed_state["best_model_score"]
        self.best_model_path = checkpointed_state["best_model_path"]

    def save_checkpoint(self, trainer, pl_module):
        """
        Performs the main logic around saving a checkpoint.
        This method runs on all ranks, it is the responsibility of `self.save_function`
        to handle correct behaviour in distributed training, i.e., saving only on rank 0.
        """
        epoch = trainer.current_epoch
        global_step = trainer.global_step

        if (
            self.save_top_k == 0  # no models are saved
            or self.period < 1  # no models are saved
            or (epoch + 1) % self.period  # skip epoch
            or trainer.running_sanity_check  # don't save anything during sanity check
            or self.last_global_step_saved == global_step  # already saved at the last step
        ):
            return