docstr: Attaches the callbacks defined in the model.
  
  If a callback returned by the model's configure_callback method has the same type as one or several
  callbacks already present in the trainer callbacks list, it will replace them.
  In addition, all :class:`~lightning.pytorch.callbacks.model_checkpoint.ModelCheckpoint` callbacks
  will be pushed to the end of the list, ensuring they run last.
docstr_tok: ["Attaches","the","callbacks","defined","in","the","model",".","If","a","callback","returned","by","the","model","'s","configure_callback","method","has","the","same","type","as","one","or","several","callbacks","already","present","in","the","trainer","callbacks","list",",","it","will","replace","them",".","In","addition",",","all",":","class",":","`","~lightning.pytorch.callbacks.model_checkpoint.ModelCheckpoint","`","callbacks","will","be","pushed","to","the","end","of","the","list",",","ensuring","they","run","last","."]
code: def _attach_model_callbacks(self) -> None:
      """Attaches the callbacks defined in the model.
  
      If a callback returned by the model's configure_callback method has the same type as one or several
      callbacks already present in the trainer callbacks list, it will replace them.
      In addition, all :class:`~lightning.pytorch.callbacks.model_checkpoint.ModelCheckpoint` callbacks
      will be pushed to the end of the list, ensuring they run last.
  
      """
      trainer = self.trainer
  
      model_callbacks = call._call_lightning_module_hook(
          trainer, "configure_callbacks"
      )
      if not model_callbacks:
          return
  
      model_callbacks = (
          [model_callbacks]
          if not isinstance(model_callbacks, Sequence)
          else model_callbacks
      )
      model_callback_types = {type(c) for c in model_callbacks}
      trainer_callback_types = {type(c) for c in trainer.callbacks}
      # edge case: if an unmodified callback was added, the logic below would filter it
      trainer_callback_types.discard(Callback)
      # exclude trainer callbacks of the same class or subclass
      override_types = set()
      for model_cb in model_callback_types:
          for trainer_cb in trainer_callback_types:
              if issubclass(model_cb, trainer_cb):
                  override_types.add(trainer_cb)
                  break
      if override_types:
          rank_zero_info(
              "The following callbacks returned in `LightningModule.configure_callbacks` will override"
              " existing callbacks passed to Trainer:"
              f" {', '.join(sorted(t.__name__ for t in override_types))}"
          )
      # remove all callbacks with a type that occurs in model callbacks
      all_callbacks = [
          c for c in trainer.callbacks if type(c) not in override_types
      ]
      all_callbacks.extend(model_callbacks)
      all_callbacks = _CallbackConnector._reorder_callbacks(
          all_callbacks
      )
      # TODO: connectors refactor: move callbacks list to connector and do not write Trainer state
      trainer.callbacks = all_callbacks
  
imports: import logging
  import os
  from datetime import timedelta
  from typing import Dict, List, Optional, Sequence, Union
  import lightning.pytorch
  from lightning.fabric.utilities.registry import _load_external_callbacks
  from lightning.pytorch.callbacks import Callback, Checkpoint, ModelCheckpoint, ModelSummary, ProgressBar, RichProgressBar, TQDMProgressBar
  from lightning.pytorch.callbacks.batch_size_finder import BatchSizeFinder
  from lightning.pytorch.callbacks.lr_finder import LearningRateFinder
  from lightning.pytorch.callbacks.rich_model_summary import RichModelSummary
  from lightning.pytorch.callbacks.timer import Timer
  from lightning.pytorch.trainer import call
  from lightning.pytorch.utilities.exceptions import MisconfigurationException
  from lightning.pytorch.utilities.model_helpers import is_overridden
  from lightning.pytorch.utilities.rank_zero import rank_zero_info
label_desc: Use image node_id 8 for calling the _CallbackConnector obj's underlying member method code with example usage: obj._attach_model_callbacks() without return types