Repository: Lightning-AI/pytorch-lightning
Pull request: Add check for verbose attribute of ModelCheckpoint
## What does this PR do?
Fixes #6408

## Before submitting
- [x] Was this discussed/approved via a GitHub issue? (not for typos and docs)
- [x] Did you read the [contributor guideline](https://github.com/PyTorchLightning/pytorch-lightning/blob/master/.github/CONTRIBUTING.md), **Pull Request** section?
- [x] Did you make sure your PR does only one thing, instead of bundling different changes together?
- [x] Did you make sure to update the documentation with your changes? (if necessary)
- [x] Did you write any new necessary tests? (not for typos and docs)
- [x] Did you verify new and existing tests pass locally with your changes?
- [x] Did you update the [CHANGELOG](https://github.com/PyTorchLightning/pytorch-lightning/blob/master/CHANGELOG.md)? (not for typos, docs, test updates, or internal minor changes/refactorings)

<!-- For CHANGELOG separate each item in the unreleased section by a blank line to reduce collisions -->

## PR review
Anyone in the community is free to review the PR once the tests have passed.
Before you start reviewing make sure you have read [Review guidelines](https://github.com/PyTorchLightning/pytorch-lightning/wiki/Review-guidelines). In short, see the following bullet-list:

 - [x] Is this pull request ready for review? (if not, please submit in draft mode)
 - [x] Check that all items from **Before submitting** are resolved
 - [x] Make sure the title is self-explanatory and the description concisely explains the PR
 - [x] Add labels and milestones (and optionally projects) to the PR so it can be classified

## Did you have fun?
Make sure you had fun coding 🙃


Problem statement:
Make verbose=False prevent showing "Saving latest checkpoint..."
## 🚀 Feature
Currently Lightning prints:
```
[lightning][INFO] - Saving latest checkpoint...
```
when running training with `ModelCheckpoint(verbose=False)`.

Not sure if it's a bug or intended...

Can we make it to not log that when passing `verbose=False`?

Hints from issue discussion:
Sure, just need to check the verbose attribute of the checkpoints here:

https://github.com/PyTorchLightning/pytorch-lightning/blob/38a5fe7af1ba300aa95d7716b5bdee35d52d032b/pytorch_lightning/trainer/training_loop.py#L157-L158

Do you want to open a PR with the fix?
@carmocca I made a PR https://github.com/PyTorchLightning/pytorch-lightning/pull/6419.

Commit message: Fix test_model_checkpoint_save_last_warning

Patch to review:
diff --git a/pytorch_lightning/trainer/training_loop.py b/pytorch_lightning/trainer/training_loop.py
index 7ad035e1f56b4..88b87afcb9358 100644
--- a/pytorch_lightning/trainer/training_loop.py
+++ b/pytorch_lightning/trainer/training_loop.py
@@ -154,7 +154,7 @@ def check_checkpoint_callback(self, should_update, is_last=False):
         if should_update and self.trainer.checkpoint_connector.has_trained:
             callbacks = self.trainer.checkpoint_callbacks
 
-            if is_last and any(cb.save_last for cb in callbacks):
+            if is_last and any(cb.save_last and cb.verbose for cb in callbacks):
                 rank_zero_info("Saving latest checkpoint...")
 
             model = self.trainer.lightning_module
diff --git a/tests/checkpointing/test_model_checkpoint.py b/tests/checkpointing/test_model_checkpoint.py
index 3d5cddc4537a7..abbe9e4e8b0cc 100644
--- a/tests/checkpointing/test_model_checkpoint.py
+++ b/tests/checkpointing/test_model_checkpoint.py
@@ -672,19 +672,27 @@ def test_default_checkpoint_behavior(tmpdir):
 @pytest.mark.parametrize('max_epochs', [1, 2])
 @pytest.mark.parametrize('should_validate', [True, False])
 @pytest.mark.parametrize('save_last', [True, False])
-def test_model_checkpoint_save_last_warning(tmpdir, caplog, max_epochs, should_validate, save_last):
+@pytest.mark.parametrize('verbose', [True, False])
+def test_model_checkpoint_save_last_warning(tmpdir, caplog, max_epochs, should_validate, save_last, verbose):
     """Tests 'Saving latest checkpoint...' log"""
     model = LogInTwoMethods()
     if not should_validate:
         model.validation_step = None
     trainer = Trainer(
         default_root_dir=tmpdir,
-        callbacks=[ModelCheckpoint(monitor='early_stop_on', dirpath=tmpdir, save_top_k=0, save_last=save_last)],
+        callbacks=[
+            ModelCheckpoint(
+                monitor='early_stop_on', dirpath=tmpdir, save_top_k=0, save_last=save_last, verbose=verbose
+            )
+        ],
         max_epochs=max_epochs,
     )
     with caplog.at_level(logging.INFO):
         trainer.fit(model)
-    assert caplog.messages.count('Saving latest checkpoint...') == save_last
+    if verbose and save_last:
+        assert caplog.messages.count('Saving latest checkpoint...') == 1
+    else:
+        assert caplog.messages.count('Saving latest checkpoint...') == 0
 
 
 def test_model_checkpoint_save_last_checkpoint_contents(tmpdir):


Code review comments:
diff_hunk: @@ -672,19 +672,27 @@ def test_default_checkpoint_behavior(tmpdir):
 @pytest.mark.parametrize('max_epochs', [1, 2])
 @pytest.mark.parametrize('should_validate', [True, False])
 @pytest.mark.parametrize('save_last', [True, False])
-def test_model_checkpoint_save_last_warning(tmpdir, caplog, max_epochs, should_validate, save_last):
+@pytest.mark.parametrize('verbose', [True, False])
+def test_model_checkpoint_save_last_warning(tmpdir, caplog, max_epochs, should_validate, save_last, verbose):
     """Tests 'Saving latest checkpoint...' log"""
     model = LogInTwoMethods()
     if not should_validate:
         model.validation_step = None
     trainer = Trainer(
         default_root_dir=tmpdir,
-        callbacks=[ModelCheckpoint(monitor='early_stop_on', dirpath=tmpdir, save_top_k=0, save_last=save_last)],
+        callbacks=[
+            ModelCheckpoint(
+                monitor='early_stop_on', dirpath=tmpdir, save_top_k=0, save_last=save_last, verbose=verbose
+            )
+        ],
         max_epochs=max_epochs,
     )
     with caplog.at_level(logging.INFO):
         trainer.fit(model)
-    assert caplog.messages.count('Saving latest checkpoint...') == save_last
+    if verbose and save_last:
+        assert caplog.messages.count('Saving latest checkpoint...') == 1
+    else:
+        assert caplog.messages.count('Saving latest checkpoint...') == 0
path: tests/checkpointing/test_model_checkpoint.py
text: @user1:
```suggestion
    assert caplog.messages.count('Saving latest checkpoint...') == (verbose and save_last)
```