code: def apply_token_merging(sd_model, token_merging_ratio):
      """
      Applies speed and memory optimizations from tomesd.
      """
  
      current_token_merging_ratio = getattr(
          sd_model, "applied_token_merged_ratio", 0
      )
  
      if current_token_merging_ratio == token_merging_ratio:
          return
  
      if current_token_merging_ratio > 0:
          tomesd.remove_patch(sd_model)
  
      if token_merging_ratio > 0:
          tomesd.apply_patch(
              sd_model,
              ratio=token_merging_ratio,
              use_rand=False,  # can cause issues with some samplers
              merge_attn=True,
              merge_crossattn=False,
              merge_mlp=False,
          )
  
      sd_model.applied_token_merged_ratio = token_merging_ratio
  
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 26 for calling a global function with example usage: apply_token_merging(sd_model, token_merging_ratio) without return types