class_docstr: Attention processor for IP-Adapater.
  Args:
      hidden_size (`int`):
          The hidden size of the attention layer.
      cross_attention_dim (`int`):
          The number of channels in the `encoder_hidden_states`.
      rank (`int`, defaults to 4):
          The dimension of the LoRA update matrices.
      network_alpha (`int`, *optional*):
          Equivalent to `alpha` but it's usage is specific to Kohya (A1111) style LoRAs.
      lora_scale (`float`, defaults to 1.0):
          the weight scale of LoRA.
      scale (`float`, defaults to 1.0):
          the weight scale of image prompt.
      num_tokens (`int`, defaults to 4 when do ip_adapter_plus it should be 16):
          The context length of the image features.
class_docstr_tok: ["Attention","processor","for","IP-Adapater",".","Args",":","hidden_size","(","`","int","`",")",":","The","hidden","size","of","the","attention","layer",".","cross_attention_dim","(","`","int","`",")",":","The","number","of","channels","in","the","`","encoder_hidden_states","`",".","rank","(","`","int","`",",","defaults","to","4",")",":","The","dimension","of","the","LoRA","update","matrices",".","network_alpha","(","`","int","`",",","*","optional","*",")",":","Equivalent","to","`","alpha","`","but","it","'s","usage","is","specific","to","Kohya","(","A1111",")","style","LoRAs",".","lora_scale","(","`","float","`",",","defaults","to","1.0",")",":","the","weight","scale","of","LoRA",".","scale","(","`","float","`",",","defaults","to","1.0",")",":","the","weight","scale","of","image","prompt",".","num_tokens","(","`","int","`",",","defaults","to","4","when","do","ip_adapter_plus","it","should","be","16",")",":","The","context","length","of","the","image","features","."]
code: def __call__(
      self,
      attn,
      hidden_states,
      encoder_hidden_states=None,
      attention_mask=None,
      temb=None,
  ):
      residual = hidden_states
  
      if attn.spatial_norm is not None:
          hidden_states = attn.spatial_norm(hidden_states, temb)
  
      input_ndim = hidden_states.ndim
  
      if input_ndim == 4:
          batch_size, channel, height, width = hidden_states.shape
          hidden_states = hidden_states.view(
              batch_size, channel, height * width
          ).transpose(1, 2)
  
      batch_size, sequence_length, _ = (
          hidden_states.shape
          if encoder_hidden_states is None
          else encoder_hidden_states.shape
      )
      attention_mask = attn.prepare_attention_mask(
          attention_mask, sequence_length, batch_size
      )
  
      if attn.group_norm is not None:
          hidden_states = attn.group_norm(
              hidden_states.transpose(1, 2)
          ).transpose(1, 2)
  
      query = attn.to_q(
          hidden_states
      ) + self.lora_scale * self.to_q_lora(hidden_states)
  
      if encoder_hidden_states is None:
          encoder_hidden_states = hidden_states
      else:
          # get encoder_hidden_states, ip_hidden_states
          end_pos = encoder_hidden_states.shape[1] - self.num_tokens
          encoder_hidden_states, ip_hidden_states = (
              encoder_hidden_states[:, :end_pos, :],
              encoder_hidden_states[:, end_pos:, :],
          )
          if attn.norm_cross:
              encoder_hidden_states = attn.norm_encoder_hidden_states(
                  encoder_hidden_states
              )
  
      key = attn.to_k(
          encoder_hidden_states
      ) + self.lora_scale * self.to_k_lora(encoder_hidden_states)
      value = attn.to_v(
          encoder_hidden_states
      ) + self.lora_scale * self.to_v_lora(encoder_hidden_states)
  
      query = attn.head_to_batch_dim(query)
      key = attn.head_to_batch_dim(key)
      value = attn.head_to_batch_dim(value)
  
      attention_probs = attn.get_attention_scores(
          query, key, attention_mask
      )
      hidden_states = torch.bmm(attention_probs, value)
      hidden_states = attn.batch_to_head_dim(hidden_states)
  
      # for ip-adapter
      ip_key = self.to_k_ip(ip_hidden_states)
      ip_value = self.to_v_ip(ip_hidden_states)
  
      ip_key = attn.head_to_batch_dim(ip_key)
      ip_value = attn.head_to_batch_dim(ip_value)
  
      ip_attention_probs = attn.get_attention_scores(
          query, ip_key, None
      )
      ip_hidden_states = torch.bmm(ip_attention_probs, ip_value)
      ip_hidden_states = attn.batch_to_head_dim(ip_hidden_states)
  
      hidden_states = hidden_states + self.scale * ip_hidden_states
  
      # linear proj
      hidden_states = attn.to_out[0](
          hidden_states
      ) + self.lora_scale * self.to_out_lora(hidden_states)
      # dropout
      hidden_states = attn.to_out[1](hidden_states)
  
      if input_ndim == 4:
          hidden_states = hidden_states.transpose(-1, -2).reshape(
              batch_size, channel, height, width
          )
  
      if attn.residual_connection:
          hidden_states = hidden_states + residual
  
      hidden_states = hidden_states / attn.rescale_output_factor
  
      return hidden_states
  
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 2 for calling the LoRAIPAdapterAttnProcessor obj's underlying member method code with example usage: obj.__call__(attn, hidden_states, encoder_hidden_states, attention_mask, temb) and returns: hidden_states