def test_model_checkpoint_topk_all(tmp_path):
    """Test that save_top_k=-1 tracks the best models when monitor key is provided."""
    seed_everything(1000)
    epochs = 3

    model = BoringModel()
    checkpoint_callback = ModelCheckpoint(
        dirpath=tmp_path, filename="{epoch}", monitor="epoch", mode="max", save_top_k=-1
    )
    trainer = Trainer(
        default_root_dir=tmp_path,
        callbacks=[checkpoint_callback],
        max_epochs=epochs,
        logger=False,
        val_check_interval=1.0,
    )
    trainer.fit(model)

    assert checkpoint_callback.monitor == "epoch"
    assert checkpoint_callback.best_model_path == str(tmp_path / "epoch=2.ckpt")
    assert checkpoint_callback.best_model_score == epochs - 1
    assert len(os.listdir(tmp_path)) == len(checkpoint_callback.best_k_models) == epochs
    assert set(checkpoint_callback.best_k_models.keys()) == {str(tmp_path / f"epoch={i}.ckpt") for i in range(epochs)}
    assert checkpoint_callback.kth_best_model_path == str(tmp_path / "epoch=0.ckpt")
Test that save_top_k=-1 tracks the best models when monitor key is provided.
tests/tests_pytorch/checkpointing/test_model_checkpoint.py