From 2c35c164d2f2b3fb6dea8023aaf31a1d2846f6de Mon Sep 17 00:00:00 2001 From: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com> Date: Mon, 12 Jun 2023 22:35:07 +0800 Subject: [PATCH] [Fix] CheckpointHook should check whether file exists before removing it (#1198) --- mmengine/hooks/checkpoint_hook.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/mmengine/hooks/checkpoint_hook.py b/mmengine/hooks/checkpoint_hook.py index 6b74c52a..0b0f8d9a 100644 --- a/mmengine/hooks/checkpoint_hook.py +++ b/mmengine/hooks/checkpoint_hook.py @@ -518,15 +518,19 @@ class CheckpointHook(Hook): runner.message_hub.update_info(best_score_key, best_score) if best_ckpt_path and is_main_process(): + is_removed = False if self.file_backend.isfile(best_ckpt_path): self.file_backend.remove(best_ckpt_path) - else: + is_removed = True + elif self.file_backend.isdir(best_ckpt_path): # checkpoints saved by deepspeed are directories self.file_backend.rmtree(best_ckpt_path) + is_removed = True - runner.logger.info( - f'The previous best checkpoint {best_ckpt_path} ' - 'is removed') + if is_removed: + runner.logger.info( + f'The previous best checkpoint {best_ckpt_path} ' + 'is removed') best_ckpt_name = f'best_{key_indicator}_{ckpt_filename}' # Replace illegal characters for filename with `_`