def test_all_callback_states_saved_before_checkpoint_callback(tmp_path):
    """Test that all callback states get saved even if the ModelCheckpoint is not given as last and when there are
    multiple callbacks of the same type."""

    callback0 = StatefulCallback0()
    callback1 = StatefulCallback1(unique="one")
    callback2 = StatefulCallback1(unique="two", other=2)
    checkpoint_callback = ModelCheckpoint(dirpath=tmp_path, filename="all_states")
    model = BoringModel()
    trainer = Trainer(
        default_root_dir=tmp_path,
        max_steps=1,
        limit_val_batches=1,
        callbacks=[
            callback0,
            # checkpoint callback does not have to be at the end
            checkpoint_callback,
            # callback2 and callback3 have the same type
            callback1,
            callback2,
        ],
    )
    trainer.fit(model)

    ckpt = torch.load(str(tmp_path / "all_states.ckpt"), weights_only=True)
    state0 = ckpt["callbacks"]["StatefulCallback0"]
    state1 = ckpt["callbacks"]["StatefulCallback1{'unique': 'one'}"]
    state2 = ckpt["callbacks"]["StatefulCallback1{'unique': 'two'}"]
    assert "content0" in state0
    assert state0["content0"] == 0
    assert "content1" in state1
    assert state1["content1"] == "one"
    assert "content1" in state2
    assert state2["content1"] == "two"
    assert (
        "ModelCheckpoint{'monitor': None, 'mode': 'min', 'every_n_train_steps': 0, 'every_n_epochs': 1,"
        " 'train_time_interval': None}" in ckpt["callbacks"]
    )
Test that all callback states get saved even if the ModelCheckpoint is not given as last and when there are
    multiple callbacks of the same type.
tests/tests_pytorch/trainer/connectors/test_callback_connector.py