class_docstr: Cross frame attention processor with scaled_dot_product attention of Pytorch 2.0.
  
  Args:
      batch_size: The number that represents actual batch size, other than the frames.
          For example, calling unet with a single prompt and num_images_per_prompt=1, batch_size should be equal to
          2, due to classifier-free guidance.
class_docstr_tok: ["Cross","frame","attention","processor","with","scaled_dot_product","attention","of","Pytorch","2.0",".","Args",":","batch_size",":","The","number","that","represents","actual","batch","size",",","other","than","the","frames",".","For","example",",","calling","unet","with","a","single","prompt","and","num_images_per_prompt=1",",","batch_size","should","be","equal","to","2",",","due","to","classifier-free","guidance","."]
code: def __call__(
      self,
      attn,
      hidden_states,
      encoder_hidden_states=None,
      attention_mask=None,
  ):
      batch_size, sequence_length, _ = (
          hidden_states.shape
          if encoder_hidden_states is None
          else encoder_hidden_states.shape
      )
      inner_dim = hidden_states.shape[-1]
  
      if attention_mask is not None:
          attention_mask = attn.prepare_attention_mask(
              attention_mask, sequence_length, batch_size
          )
          # scaled_dot_product_attention expects attention_mask shape to be
          # (batch, heads, source_length, target_length)
          attention_mask = attention_mask.view(
              batch_size, attn.heads, -1, attention_mask.shape[-1]
          )
  
      query = attn.to_q(hidden_states)
  
      is_cross_attention = encoder_hidden_states is not None
      if encoder_hidden_states is None:
          encoder_hidden_states = hidden_states
      elif attn.norm_cross:
          encoder_hidden_states = attn.norm_encoder_hidden_states(
              encoder_hidden_states
          )
  
      key = attn.to_k(encoder_hidden_states)
      value = attn.to_v(encoder_hidden_states)
  
      # Cross Frame Attention
      if not is_cross_attention:
          video_length = key.size()[0] // self.batch_size
          first_frame_index = [0] * video_length
  
          # rearrange keys to have batch and frames in the 1st and 2nd dims respectively
          key = rearrange_3(key, video_length)
          key = key[:, first_frame_index]
          # rearrange values to have batch and frames in the 1st and 2nd dims respectively
          value = rearrange_3(value, video_length)
          value = value[:, first_frame_index]
  
          # rearrange back to original shape
          key = rearrange_4(key)
          value = rearrange_4(value)
  
      head_dim = inner_dim // attn.heads
      query = query.view(
          batch_size, -1, attn.heads, head_dim
      ).transpose(1, 2)
      key = key.view(batch_size, -1, attn.heads, head_dim).transpose(
          1, 2
      )
      value = value.view(
          batch_size, -1, attn.heads, head_dim
      ).transpose(1, 2)
  
      # the output of sdp = (batch, num_heads, seq_len, head_dim)
      # TODO: add support for attn.scale when we move to Torch 2.1
      hidden_states = F.scaled_dot_product_attention(
          query,
          key,
          value,
          attn_mask=attention_mask,
          dropout_p=0.0,
          is_causal=False,
      )
  
      hidden_states = hidden_states.transpose(1, 2).reshape(
          batch_size, -1, attn.heads * head_dim
      )
      hidden_states = hidden_states.to(query.dtype)
  
      # linear proj
      hidden_states = attn.to_out[0](hidden_states)
      # dropout
      hidden_states = attn.to_out[1](hidden_states)
      return hidden_states
  
imports: import copy
  from dataclasses import dataclass
  from typing import Callable, List, Optional, Union
  import numpy
  import PIL.Image
  import torch
  import torch.nn.functional
  from torch.nn.functional import grid_sample
  from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer
  from diffusers.models import AutoencoderKL, UNet2DConditionModel
  from diffusers.pipelines.stable_diffusion import StableDiffusionPipeline, StableDiffusionSafetyChecker
  from diffusers.schedulers import KarrasDiffusionSchedulers
  from diffusers.utils import BaseOutput
label_desc: Use image node_id 2 for calling the CrossFrameAttnProcessor2_0 obj's underlying member method code with example usage: obj.__call__(attn, hidden_states, encoder_hidden_states, attention_mask) and returns: hidden_states