Repository: Lightning-AI/pytorch-lightning

Issue:
Allow keeping default save_dir in ModelCheckpointer
##  Feature
Make `filepath` argument of `ModelCheckpointer` optional.

### Motivation
I'm pretty happy with all defaults of `ModelCheckpointer` except `save_top_k`. In case I want to override that parameter I have to write some awkward code related to figuring out the checkpointing path, which is normally only known at runtime:

```python
DEFROOT = Path('/data/models/lightning')
logger = WandbLogger(name=net.hparams.run_name, project=net.hparams.project, save_dir=str(DEFROOT))
logger.watch(net)
_ = logger.experiment  # create an experiment to determine version
cp_template = str(DEFROOT / net.hparams.project / ('version_'+logger.version) / 'checkpoints' / '{epoch:04d}-{val_loss:.2f}-{other_metric:.2f}.pt')

checkpointer = ModelCheckpoint(
    filepath=cp_template,
    save_top_k=10,
    verbose=True,
    monitor='val_loss',
    mode='min',
)

trainer = Trainer(
    gpus=1,
    logger=logger,
    default_root_dir=DEFROOT,
    checkpoint_callback=checkpointer,
    **extra,
)
trainer.fit(net)
```

It would be nice to have an option to allow Lightning to determine runtime-valued save location instead. Additionally, it would be nice to have an option to override checkpoint filename without overriding the whole save path.


Discussion:
Hi! thanks for your contribution!, great first issue!

Resolving patch:
diff --git a/pytorch_lightning/callbacks/model_checkpoint.py b/pytorch_lightning/callbacks/model_checkpoint.py
--- a/pytorch_lightning/callbacks/model_checkpoint.py
+++ b/pytorch_lightning/callbacks/model_checkpoint.py
@@ -86,7 +86,7 @@ def __init__(self, filepath: Optional[str] = None, monitor: str = 'val_loss', ve
                  save_top_k: int = 1, save_weights_only: bool = False,
                  mode: str = 'auto', period: int = 1, prefix: str = ''):
         super().__init__()
-        if save_top_k > 0 and os.path.isdir(filepath) and len(os.listdir(filepath)) > 0:
+        if save_top_k > 0 and filepath is not None and os.path.isdir(filepath) and len(os.listdir(filepath)) > 0:
             rank_zero_warn(
                 f"Checkpoint directory {filepath} exists and is not empty with save_top_k != 0."
                 "All files in this directory will be deleted when a checkpoint is saved!"


Test patch:
diff --git a/tests/callbacks/test_callbacks.py b/tests/callbacks/test_callbacks.py
--- a/tests/callbacks/test_callbacks.py
+++ b/tests/callbacks/test_callbacks.py
@@ -1,3 +1,4 @@
+import pytest
 import tests.base.utils as tutils
 from pytorch_lightning import Callback
 from pytorch_lightning import Trainer, LightningModule
@@ -249,7 +250,8 @@ def test_pickling(tmpdir):
     pickle.dumps(early_stopping)
 
 
-def test_model_checkpoint_with_non_string_input(tmpdir):
+@pytest.mark.parametrize('save_top_k', [-1, 0, 1, 2])
+def test_model_checkpoint_with_non_string_input(tmpdir, save_top_k):
     """ Test that None in checkpoint callback is valid and that chkp_path is
         set correctly """
     tutils.reset_seed()
@@ -260,7 +262,7 @@ class CurrentTestModel(LightTrainDataloader, TestModelBase):
     hparams = tutils.get_default_hparams()
     model = CurrentTestModel(hparams)
 
-    checkpoint = ModelCheckpoint(filepath=None, save_top_k=-1)
+    checkpoint = ModelCheckpoint(filepath=None, save_top_k=save_top_k)
 
     trainer = Trainer(default_root_dir=tmpdir,
                       checkpoint_callback=checkpoint,