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

Code before:
    def _validate_monitor_key(self, trainer):
        metrics = trainer.logger_connector.callback_metrics

        # validate metric
        if self.monitor is not None and not self._is_valid_monitor_key(metrics):
            m = (
                f"ModelCheckpoint(monitor='{self.monitor}') not found in the returned metrics:"
                f" {list(metrics.keys())}. "
                f"HINT: Did you call self.log('{self.monitor}', tensor) in the LightningModule?"
            )
            raise MisconfigurationException(m)

    def _get_metric_interpolated_filepath_name(
        self,
        ckpt_name_metrics: Dict[str, Any],
        epoch: int,
        step: int,
        del_filepath: Optional[str] = None
    ) -> str:
        filepath = self.format_checkpoint_name(epoch, step, ckpt_name_metrics)

        version_cnt = 0
        while self._fs.exists(filepath):
            if filepath == del_filepath:
                break

            filepath = self.format_checkpoint_name(epoch, step, ckpt_name_metrics, ver=version_cnt)
            version_cnt += 1

        return filepath

    def _monitor_candidates(self, trainer):
        ckpt_name_metrics = deepcopy(trainer.logger_connector.logged_metrics)
        ckpt_name_metrics.update(trainer.logger_connector.callback_metrics)
        ckpt_name_metrics.update(trainer.logger_connector.progress_bar_metrics)
        ckpt_name_metrics.update({"step": trainer.global_step, "epoch": trainer.current_epoch})
        return ckpt_name_metrics

    def _save_last_checkpoint(self, trainer, pl_module, ckpt_name_metrics):
        should_save_last = self.monitor is None or self.save_last
        if not should_save_last:
            return

        last_filepath = self._get_metric_interpolated_filepath_name(
            ckpt_name_metrics, trainer.current_epoch, trainer.global_step
        )

        # when user ALSO asked for the 'last.ckpt' change the name
        if self.save_last:
            last_filepath = self._format_checkpoint_name(
                self.CHECKPOINT_NAME_LAST,

Review comment (suggestion):
```suggestion
        while self._fs.exists(filepath) and filepath != del_filepath:
```

Code after:
        self.dirpath = ckpt_path

        if trainer.is_global_zero:
            self._fs.makedirs(self.dirpath, exist_ok=True)

    def _add_backward_monitor_support(self, trainer):
        metrics = trainer.logger_connector.callback_metrics

        # backward compatibility... need to deprecate
        if self.monitor is None and 'val_loss' in metrics:
            self.monitor = 'val_loss'

        if self.monitor is None and 'checkpoint_on' in metrics:
            self.monitor = 'checkpoint_on'

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

    def _validate_monitor_key(self, trainer):
        metrics = trainer.logger_connector.callback_metrics

        # validate metric
        if self.monitor is not None and not self._is_valid_monitor_key(metrics):
            m = (
                f"ModelCheckpoint(monitor='{self.monitor}') not found in the returned metrics:"
                f" {list(metrics.keys())}. "
                f"HINT: Did you call self.log('{self.monitor}', tensor) in the LightningModule?"
            )
            raise MisconfigurationException(m)

    def _get_metric_interpolated_filepath_name(
        self,
        ckpt_name_metrics: Dict[str, Any],
        epoch: int,
        step: int,
        del_filepath: Optional[str] = None
    ) -> str:
        filepath = self.format_checkpoint_name(epoch, step, ckpt_name_metrics)

        version_cnt = 0
        while self._fs.exists(filepath) and filepath != del_filepath:
            filepath = self.format_checkpoint_name(epoch, step, ckpt_name_metrics, ver=version_cnt)
            version_cnt += 1

        return filepath

    def _monitor_candidates(self, trainer):
        ckpt_name_metrics = deepcopy(trainer.logger_connector.logged_metrics)
        ckpt_name_metrics.update(trainer.logger_connector.callback_metrics)
        ckpt_name_metrics.update(trainer.logger_connector.progress_bar_metrics)
        ckpt_name_metrics.update({"step": trainer.global_step, "epoch": trainer.current_epoch})