code: def _call_callbacks_state_dict(
      trainer: "pl.Trainer",
  ) -> Dict[str, dict]:
      """Called when saving a model checkpoint, calls and returns every callback's `state_dict`, keyed by
      `Callback.state_key`."""
      callback_state_dicts = {}
      for callback in trainer.callbacks:
          state_dict = callback.state_dict()
          if state_dict:
              callback_state_dicts[callback.state_key] = state_dict
      return callback_state_dicts
  
imports: import logging
  from copy import deepcopy
  from typing import Any, Callable, Dict, Optional, Type, Union
  from packaging.version import Version
  import lightning.pytorch
  from lightning.fabric.utilities.device_dtype_mixin import _DeviceDtypeModuleMixin
  from lightning.pytorch.callbacks import Checkpoint, EarlyStopping
  from lightning.pytorch.trainer.states import TrainerStatus
  from lightning.pytorch.utilities.exceptions import _TunerExitException
  from lightning.pytorch.utilities.model_helpers import is_overridden
  from lightning.pytorch.utilities.rank_zero import rank_zero_warn
label_desc: Use image node_id 8 for calling a global function with example usage: _call_callbacks_state_dict(trainer) and returns: callback_state_dicts