class_docstr: Pipeline for text-to-image generation using Stable Diffusion.
  
  This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods
  implemented for all pipelines (downloading, saving, running on a particular device, etc.).
  
  The pipeline also inherits the following loading methods:
      - [`~loaders.TextualInversionLoaderMixin.load_textual_inversion`] for loading textual inversion embeddings
      - [`~loaders.LoraLoaderMixin.load_lora_weights`] for loading LoRA weights
      - [`~loaders.LoraLoaderMixin.save_lora_weights`] for saving LoRA weights
      - [`~loaders.FromSingleFileMixin.from_single_file`] for loading `.ckpt` files
      - [`~loaders.IPAdapterMixin.load_ip_adapter`] for loading IP Adapters
  
  Args:
      vae ([`AutoencoderKL`]):
          Variational Auto-Encoder (VAE) model to encode and decode images to and from latent representations.
      text_encoder ([`~transformers.CLIPTextModel`]):
          Frozen text-encoder ([clip-vit-large-patch14](https://huggingface.co/openai/clip-vit-large-patch14)).
      tokenizer ([`~transformers.CLIPTokenizer`]):
          A `CLIPTokenizer` to tokenize text.
      unet ([`UNet2DConditionModel`]):
          A `UNet2DConditionModel` to denoise the encoded image latents.
      scheduler ([`SchedulerMixin`]):
          A scheduler to be used in combination with `unet` to denoise the encoded image latents. Can be one of
          [`DDIMScheduler`], [`LMSDiscreteScheduler`], or [`PNDMScheduler`].
      safety_checker ([`StableDiffusionSafetyChecker`]):
          Classification module that estimates whether generated images could be considered offensive or harmful.
          Please refer to the [model card](https://huggingface.co/runwayml/stable-diffusion-v1-5) for more details
          about a model's potential harms.
      feature_extractor ([`~transformers.CLIPImageProcessor`]):
          A `CLIPImageProcessor` to extract features from generated images; used as inputs to the `safety_checker`.
class_docstr_tok: ["Pipeline","for","text-to-image","generation","using","Stable","Diffusion",".","This","model","inherits","from","[","`","DiffusionPipeline","`","]",".","Check","the","superclass","documentation","for","the","generic","methods","implemented","for","all","pipelines","(","downloading",",","saving",",","running","on","a","particular","device",",","etc.",")",".","The","pipeline","also","inherits","the","following","loading","methods",":","-","[","`","~loaders.TextualInversionLoaderMixin.load_textual_inversion","`","]","for","loading","textual","inversion","embeddings","-","[","`","~loaders.LoraLoaderMixin.load_lora_weights","`","]","for","loading","LoRA","weights","-","[","`","~loaders.LoraLoaderMixin.save_lora_weights","`","]","for","saving","LoRA","weights","-","[","`","~loaders.FromSingleFileMixin.from_single_file","`","]","for","loading","`",".ckpt","`","files","-","[","`","~loaders.IPAdapterMixin.load_ip_adapter","`","]","for","loading","IP","Adapters","Args",":","vae","(","[","`","AutoencoderKL","`","]",")",":","Variational","Auto-Encoder","(","VAE",")","model","to","encode","and","decode","images","to","and","from","latent","representations",".","text_encoder","(","[","`","~transformers.CLIPTextModel","`","]",")",":","Frozen","text-encoder","(","[","clip-vit-large-patch14","]","(","https",":","\/\/huggingface.co\/openai\/clip-vit-large-patch14",")",")",".","tokenizer","(","[","`","~transformers.CLIPTokenizer","`","]",")",":","A","`","CLIPTokenizer","`","to","tokenize","text",".","unet","(","[","`","UNet2DConditionModel","`","]",")",":","A","`","UNet2DConditionModel","`","to","denoise","the","encoded","image","latents",".","scheduler","(","[","`","SchedulerMixin","`","]",")",":","A","scheduler","to","be","used","in","combination","with","`","unet","`","to","denoise","the","encoded","image","latents",".","Can","be","one","of","[","`","DDIMScheduler","`","]",",","[","`","LMSDiscreteScheduler","`","]",",","or","[","`","PNDMScheduler","`","]",".","safety_checker","(","[","`","StableDiffusionSafetyChecker","`","]",")",":","Classification","module","that","estimates","whether","generated","images","could","be","considered","offensive","or","harmful",".","Please","refer","to","the","[","model","card","]","(","https",":","\/\/huggingface.co\/runwayml\/stable-diffusion-v1-5",")","for","more","details","about","a","model","'s","potential","harms",".","feature_extractor","(","[","`","~transformers.CLIPImageProcessor","`","]",")",":","A","`","CLIPImageProcessor","`","to","extract","features","from","generated","images",";","used","as","inputs","to","the","`","safety_checker","`","."]
code: def _load_ip_adapter_weights(self, state_dict):
      from diffusers.models.attention_processor import (
          AttnProcessor,
          AttnProcessor2_0,
      )
  
      num_image_text_embeds = 4
  
      self.unet.encoder_hid_proj = None
  
      # set ip-adapter cross-attention processors & load state_dict
      attn_procs = {}
      key_id = 0
      for name in self.unet.attn_processors.keys():
          cross_attention_dim = (
              None
              if name.endswith("attn1.processor")
              else self.unet.config.cross_attention_dim
          )
          if name.startswith("mid_block"):
              hidden_size = self.unet.config.block_out_channels[-1]
          elif name.startswith("up_blocks"):
              block_id = int(name[len("up_blocks.")])
              hidden_size = list(
                  reversed(self.unet.config.block_out_channels)
              )[block_id]
          elif name.startswith("down_blocks"):
              block_id = int(name[len("down_blocks.")])
              hidden_size = self.unet.config.block_out_channels[
                  block_id
              ]
          if cross_attention_dim is None or "motion_modules" in name:
              attn_processor_class = (
                  AttnProcessor2_0
                  if hasattr(F, "scaled_dot_product_attention")
                  else AttnProcessor
              )
              attn_procs[name] = attn_processor_class()
              rank = state_dict["ip_adapter"][
                  f"{key_id}.to_q_lora.down.weight"
              ].shape[0]
              attn_module = self.unet
              for n in name.split(".")[:-1]:
                  attn_module = getattr(attn_module, n)
              # Set the `lora_layer` attribute of the attention-related matrices.
              attn_module.to_q.set_lora_layer(
                  LoRALinearLayer(
                      in_features=attn_module.to_q.in_features,
                      out_features=attn_module.to_q.out_features,
                      rank=rank,
                  )
              )
              attn_module.to_k.set_lora_layer(
                  LoRALinearLayer(
                      in_features=attn_module.to_k.in_features,
                      out_features=attn_module.to_k.out_features,
                      rank=rank,
                  )
              )
              attn_module.to_v.set_lora_layer(
                  LoRALinearLayer(
                      in_features=attn_module.to_v.in_features,
                      out_features=attn_module.to_v.out_features,
                      rank=rank,
                  )
              )
              attn_module.to_out[0].set_lora_layer(
                  LoRALinearLayer(
                      in_features=attn_module.to_out[0].in_features,
                      out_features=attn_module.to_out[0].out_features,
                      rank=rank,
                  )
              )
  
              value_dict = {}
              for k, module in attn_module.named_children():
                  index = "."
                  if not hasattr(module, "set_lora_layer"):
                      index = ".0."
                      module = module[0]
                  lora_layer = getattr(module, "lora_layer")
                  for lora_name, w in lora_layer.state_dict().items():
                      value_dict.update(
                          {
                              f"{k}{index}lora_layer.{lora_name}": state_dict[
                                  "ip_adapter"
                              ][
                                  f"{key_id}.{k}_lora.{lora_name}"
                              ]
                          }
                      )
  
              attn_module.load_state_dict(value_dict, strict=False)
              attn_module.to(dtype=self.dtype, device=self.device)
              key_id += 1
          else:
              rank = state_dict["ip_adapter"][
                  f"{key_id}.to_q_lora.down.weight"
              ].shape[0]
              attn_processor_class = (
                  LoRAIPAdapterAttnProcessor2_0
                  if hasattr(F, "scaled_dot_product_attention")
                  else LoRAIPAdapterAttnProcessor
              )
              attn_procs[name] = attn_processor_class(
                  hidden_size=hidden_size,
                  cross_attention_dim=cross_attention_dim,
                  scale=1.0,
                  rank=rank,
                  num_tokens=num_image_text_embeds,
              ).to(dtype=self.dtype, device=self.device)
  
              value_dict = {}
              for k, w in attn_procs[name].state_dict().items():
                  value_dict.update(
                      {
                          f"{k}": state_dict["ip_adapter"][
                              f"{key_id}.{k}"
                          ]
                      }
                  )
  
              attn_procs[name].load_state_dict(value_dict)
              key_id += 1
  
      self.unet.set_attn_processor(attn_procs)
  
      # convert IP-Adapter Image Projection layers to diffusers
      image_projection = (
          self.convert_ip_adapter_image_proj_to_diffusers(
              state_dict["image_proj"]
          )
      )
  
      self.unet.encoder_hid_proj = image_projection.to(
          device=self.device, dtype=self.dtype
      )
      self.unet.config.encoder_hid_dim_type = "ip_image_proj"
  
imports: import inspect
  from typing import Any, Callable, Dict, List, Optional, Union
  import torch
  import torch.nn
  import torch.nn.functional
  from packaging import version
  from safetensors import safe_open
  from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer, CLIPVisionModelWithProjection
  from diffusers.configuration_utils import FrozenDict
  from diffusers.image_processor import VaeImageProcessor
  from diffusers.loaders import FromSingleFileMixin, IPAdapterMixin, LoraLoaderMixin, TextualInversionLoaderMixin
  from diffusers.models import AutoencoderKL, UNet2DConditionModel
  from diffusers.models.attention_processor import FusedAttnProcessor2_0
  from diffusers.models.lora import LoRALinearLayer, adjust_lora_scale_text_encoder
  from diffusers.pipelines.pipeline_utils import DiffusionPipeline
  from diffusers.pipelines.stable_diffusion.pipeline_output import StableDiffusionPipelineOutput
  from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker
  from diffusers.schedulers import KarrasDiffusionSchedulers
  from diffusers.utils import USE_PEFT_BACKEND, _get_model_file, deprecate, logging, scale_lora_layers, unscale_lora_layers
  from diffusers.utils.torch_utils import randn_tensor
label_desc: Use image node_id 4 for calling the IPAdapterFaceIDStableDiffusionPipeline obj's underlying member method code with example usage: obj._load_ip_adapter_weights(state_dict) without return types