code: def test_all_callback_states_saved_before_checkpoint_callback(tmpdir):
      """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=tmpdir, filename="all_states"
      )
      model = BoringModel()
      trainer = Trainer(
          default_root_dir=tmpdir,
          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(tmpdir / "all_states.ckpt"))
      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"]
      )
  
imports: import contextlib
  import logging
  from unittest import mock
  from unittest.mock import Mock
  import pytest
  import torch
  from lightning.fabric.utilities.imports import _PYTHON_GREATER_EQUAL_3_8_0, _PYTHON_GREATER_EQUAL_3_10_0
  from lightning.pytorch import Callback, LightningModule, Trainer
  from lightning.pytorch.callbacks import EarlyStopping, GradientAccumulationScheduler, LearningRateMonitor, ModelCheckpoint, ModelSummary, ProgressBar, TQDMProgressBar
  from lightning.pytorch.callbacks.batch_size_finder import BatchSizeFinder
  from lightning.pytorch.demos.boring_classes import BoringModel
  from lightning.pytorch.trainer.connectors.callback_connector import _CallbackConnector
label_desc: Use image node_id 2 for calling a global function with example usage: test_all_callback_states_saved_before_checkpoint_callback(tmpdir) without return types