code: def reuse_model_from_already_loaded(sd_model, checkpoint_info, timer):
      """
      Checks if the desired checkpoint from checkpoint_info is not already loaded in model_data.loaded_sd_models.
      If it is loaded, returns that (moving it to GPU if necessary, and moving the currently loadded model to CPU if necessary).
      If not, returns the model that can be used to load weights from checkpoint_info's file.
      If no such model exists, returns None.
      Additionaly deletes loaded models that are over the limit set in settings (sd_checkpoints_limit).
      """
  
      already_loaded = None
      for i in reversed(range(len(model_data.loaded_sd_models))):
          loaded_model = model_data.loaded_sd_models[i]
          if (
              loaded_model.sd_checkpoint_info.filename
              == checkpoint_info.filename
          ):
              already_loaded = loaded_model
              continue
  
          if (
              len(model_data.loaded_sd_models)
              > shared.opts.sd_checkpoints_limit
              > 0
          ):
              print(
                  f"Unloading model {len(model_data.loaded_sd_models)} over the limit of {shared.opts.sd_checkpoints_limit}: {loaded_model.sd_checkpoint_info.title}"
              )
              model_data.loaded_sd_models.pop()
              send_model_to_trash(loaded_model)
              timer.record("send model to trash")
  
          if shared.opts.sd_checkpoints_keep_in_cpu:
              send_model_to_cpu(sd_model)
              timer.record("send model to cpu")
  
      if already_loaded is not None:
          send_model_to_device(already_loaded)
          timer.record("send model to device")
  
          model_data.set_sd_model(already_loaded, already_loaded=True)
  
          if not SkipWritingToConfig.skip:
              shared.opts.data[
                  "sd_model_checkpoint"
              ] = already_loaded.sd_checkpoint_info.title
              shared.opts.data[
                  "sd_checkpoint_hash"
              ] = already_loaded.sd_checkpoint_info.sha256
  
          print(
              f"Using already loaded model {already_loaded.sd_checkpoint_info.title}: done in {timer.summary()}"
          )
          sd_vae.reload_vae_weights(already_loaded)
          return model_data.sd_model
      elif (
          shared.opts.sd_checkpoints_limit > 1
          and len(model_data.loaded_sd_models)
          < shared.opts.sd_checkpoints_limit
      ):
          print(
              f"Loading model {checkpoint_info.title} ({len(model_data.loaded_sd_models) + 1} out of {shared.opts.sd_checkpoints_limit})"
          )
  
          model_data.sd_model = None
          load_model(checkpoint_info)
          return model_data.sd_model
      elif len(model_data.loaded_sd_models) > 0:
          sd_model = model_data.loaded_sd_models.pop()
          model_data.sd_model = sd_model
  
          sd_vae.base_vae = getattr(sd_model, "base_vae", None)
          sd_vae.loaded_vae_file = getattr(
              sd_model, "loaded_vae_file", None
          )
          sd_vae.checkpoint_info = sd_model.sd_checkpoint_info
  
          print(
              f"Reusing loaded model {sd_model.sd_checkpoint_info.title} to load {checkpoint_info.title}"
          )
          return sd_model
      else:
          return None
  
imports: import collections
  import os.path
  import sys
  import threading
  import torch
  import re
  import safetensors.torch
  from omegaconf import OmegaConf, ListConfig
  from os import mkdir
  from urllib import request
  import ldm.modules.midas
  from ldm.util import instantiate_from_config
  from modules import paths, shared, modelloader, devices, script_callbacks, sd_vae, sd_disable_initialization, errors, hashes, sd_models_config, sd_unet, sd_models_xl, cache, extra_networks, processing, lowvram, sd_hijack, patches
  from modules.timer import Timer
  import tomesd
  import numpy
label_desc: Use image node_id 23 for calling a global function with example usage: reuse_model_from_already_loaded(sd_model, checkpoint_info, timer) and returns: model_data, model_data, sd_model, None