code: def create_ip_adapter_state_dict(model):
      # "ip_adapter" (cross-attention weights)
      ip_cross_attn_state_dict = {}
      key_id = 1
  
      for name in model.attn_processors.keys():
          cross_attention_dim = (
              None
              if name.endswith("attn1.processor")
              else model.config.cross_attention_dim
          )
          if name.startswith("mid_block"):
              hidden_size = model.config.block_out_channels[-1]
          elif name.startswith("up_blocks"):
              block_id = int(name[len("up_blocks.")])
              hidden_size = list(
                  reversed(model.config.block_out_channels)
              )[block_id]
          elif name.startswith("down_blocks"):
              block_id = int(name[len("down_blocks.")])
              hidden_size = model.config.block_out_channels[block_id]
          if cross_attention_dim is not None:
              sd = IPAdapterAttnProcessor(
                  hidden_size=hidden_size,
                  cross_attention_dim=cross_attention_dim,
                  scale=1.0,
              ).state_dict()
              ip_cross_attn_state_dict.update(
                  {
                      f"{key_id}.to_k_ip.weight": sd["to_k_ip.weight"],
                      f"{key_id}.to_v_ip.weight": sd["to_v_ip.weight"],
                  }
              )
  
              key_id += 2
  
      # "image_proj" (ImageProjection layer weights)
      cross_attention_dim = model.config["cross_attention_dim"]
      image_projection = ImageProjection(
          cross_attention_dim=cross_attention_dim,
          image_embed_dim=cross_attention_dim,
          num_image_text_embeds=4,
      )
  
      ip_image_projection_state_dict = {}
      sd = image_projection.state_dict()
      ip_image_projection_state_dict.update(
          {
              "proj.weight": sd["image_embeds.weight"],
              "proj.bias": sd["image_embeds.bias"],
              "norm.weight": sd["norm.weight"],
              "norm.bias": sd["norm.bias"],
          }
      )
  
      del sd
      ip_state_dict = {}
      ip_state_dict.update(
          {
              "image_proj": ip_image_projection_state_dict,
              "ip_adapter": ip_cross_attn_state_dict,
          }
      )
      return ip_state_dict
  
imports: import copy
  import gc
  import os
  import tempfile
  import unittest
  from collections import OrderedDict
  import torch
  from parameterized import parameterized
  from pytest import mark
  from diffusers import UNet2DConditionModel
  from diffusers.models.attention_processor import CustomDiffusionAttnProcessor, IPAdapterAttnProcessor
  from diffusers.models.embeddings import ImageProjection, IPAdapterPlusImageProjection
  from diffusers.utils import logging
  from diffusers.utils.import_utils import is_xformers_available
  from diffusers.utils.testing_utils import backend_empty_cache, enable_full_determinism, floats_tensor, load_hf_numpy, require_torch_accelerator, require_torch_accelerator_with_fp16, require_torch_accelerator_with_training, require_torch_gpu, skip_mps, slow, torch_all_close, torch_device
  from .test_modeling_common import ModelTesterMixin, UNetTesterMixin
label_desc: Use image node_id 1 for calling a global function with example usage: create_ip_adapter_state_dict(model) and returns: ip_state_dict