Repository: Lightning-AI/pytorch-lightning
File: tests/checkpointing/test_model_checkpoint.py (Python)

Code before:
    ModelCheckpoint.CHECKPOINT_JOIN_CHAR = char_org

    # no dirpath set
    ckpt_name = ModelCheckpoint(monitor='early_stop_on', dirpath=None).format_checkpoint_name(3, {})
    assert ckpt_name == 'epoch=3.ckpt'
    ckpt_name = ModelCheckpoint(monitor='early_stop_on', dirpath='').format_checkpoint_name(5, {})
    assert ckpt_name == 'epoch=5.ckpt'

    # CWD
    ckpt_name = ModelCheckpoint(monitor='early_stop_on', dirpath='.').format_checkpoint_name(3, {})
    assert Path(ckpt_name) == Path('.') / 'epoch=3.ckpt'

    # dir does not exist so it is used as filename
    ckpt_name = ModelCheckpoint(
        monitor='early_stop_on', dirpath=tmpdir, filename='dir', prefix='test'
    ).format_checkpoint_name(3, {})
    assert ckpt_name == tmpdir / 'test-dir.ckpt'

    # now, dir exists
    filepath = tmpdir / 'dir'
    os.mkdir(filepath)
    ckpt_name = ModelCheckpoint(
        monitor='early_stop_on', dirpath=filepath, prefix='test'
    ).format_checkpoint_name(3, {})
    assert ckpt_name == filepath / 'test-epoch=3.ckpt'

    # with ver
    ckpt_name = ModelCheckpoint(
        monitor='early_stop_on', dirpath=tmpdir, filename='name', prefix='test'
    ).format_checkpoint_name(4, {}, ver=3)
    assert ckpt_name == tmpdir / 'test-name-v3.ckpt'


def test_model_checkpoint_save_last(tmpdir):
    """Tests that save_last produces only one last checkpoint."""
    model = EvalModelTemplate()
    epochs = 3
    ModelCheckpoint.CHECKPOINT_NAME_LAST = 'last-{epoch}'
    model_checkpoint = ModelCheckpoint(monitor='early_stop_on', dirpath=tmpdir, save_top_k=-1, save_last=True)
    trainer = Trainer(
        default_root_dir=tmpdir,
        checkpoint_callback=model_checkpoint,
        max_epochs=epochs,
        logger=False,
    )
    trainer.fit(model)
    last_filename = model_checkpoint._format_checkpoint_name(ModelCheckpoint.CHECKPOINT_NAME_LAST, epochs - 1, {})
    last_filename = last_filename + '.ckpt'
    assert str(tmpdir / last_filename) == model_checkpoint.last_model_path
    assert set(os.listdir(tmpdir)) == set([f'epoch={i}.ckpt' for i in range(epochs)] + [last_filename])
    ModelCheckpoint.CHECKPOINT_NAME_LAST = 'last'

Review comment (suggestion):
These two test cases are not necessary anymore if you use `dirpath`

Code after:

    # prefix
    char_org = ModelCheckpoint.CHECKPOINT_JOIN_CHAR
    ModelCheckpoint.CHECKPOINT_JOIN_CHAR = '@'
    ckpt_name = ModelCheckpoint._format_checkpoint_name('{epoch},{acc:.5f}', 3, {'acc': 0.03}, prefix='test')
    assert ckpt_name == 'test@epoch=3,acc=0.03000'
    ModelCheckpoint.CHECKPOINT_JOIN_CHAR = char_org

    # no dirpath set
    ckpt_name = ModelCheckpoint(monitor='early_stop_on', dirpath=None).format_checkpoint_name(3, {})
    assert ckpt_name == 'epoch=3.ckpt'
    ckpt_name = ModelCheckpoint(monitor='early_stop_on', dirpath='').format_checkpoint_name(5, {})
    assert ckpt_name == 'epoch=5.ckpt'

    # CWD
    ckpt_name = ModelCheckpoint(monitor='early_stop_on', dirpath='.').format_checkpoint_name(3, {})
    assert ckpt_name == str(Path('.').resolve() / 'epoch=3.ckpt')

    # with ver
    ckpt_name = ModelCheckpoint(
        monitor='early_stop_on', dirpath=tmpdir, filename='name', prefix='test'
    ).format_checkpoint_name(3, {}, ver=3)
    assert ckpt_name == tmpdir / 'test-name-v3.ckpt'

    # using slashes
    ckpt_name = ModelCheckpoint(
        monitor='early_stop_on', dirpath=None, filename='{epoch}_{val/loss:.5f}'
    ).format_checkpoint_name(4, {'val/loss': 0.03})
    assert ckpt_name == 'epoch=4_val/loss=0.03000.ckpt'

    # TODO: Checks with filepath. To be removed in v1.2
    # CWD
    ckpt_name = ModelCheckpoint(monitor='early_stop_on', filepath='.').format_checkpoint_name(3, {})
    assert ckpt_name == str(Path('.').resolve() / 'epoch=3.ckpt')

    # dir does not exist so it is used as filename
    filepath = tmpdir / 'dir'
    ckpt_name = ModelCheckpoint(monitor='early_stop_on', filepath=filepath, prefix='test').format_checkpoint_name(3, {})
    assert ckpt_name == tmpdir / 'test-dir.ckpt'

    # now, dir exists
    os.mkdir(filepath)
    ckpt_name = ModelCheckpoint(monitor='early_stop_on', filepath=filepath, prefix='test').format_checkpoint_name(3, {})
    assert ckpt_name == filepath / 'test-epoch=3.ckpt'


def test_model_checkpoint_save_last(tmpdir):
    """Tests that save_last produces only one last checkpoint."""
    model = EvalModelTemplate()
    epochs = 3
    ModelCheckpoint.CHECKPOINT_NAME_LAST = 'last-{epoch}'