code: def forward(
      self,
      hidden_states: torch.FloatTensor,
      temb: Optional[torch.FloatTensor] = None,
      encoder_hidden_states: Optional[torch.FloatTensor] = None,
      attention_mask: Optional[torch.FloatTensor] = None,
      cross_attention_kwargs: Optional[Dict[str, Any]] = None,
      encoder_attention_mask: Optional[torch.FloatTensor] = None,
  ):
      cross_attention_kwargs = (
          cross_attention_kwargs
          if cross_attention_kwargs is not None
          else {}
      )
      lora_scale = cross_attention_kwargs.get("scale", 1.0)
  
      if attention_mask is None:
          # if encoder_hidden_states is defined: we are doing cross-attn, so we should use cross-attn mask.
          mask = (
              None
              if encoder_hidden_states is None
              else encoder_attention_mask
          )
      else:
          # when attention_mask is defined: we don't even check for encoder_attention_mask.
          # this is to maintain compatibility with UnCLIP, which uses 'attention_mask' param for cross-attn masks.
          # TODO: UnCLIP should express cross-attn mask via encoder_attention_mask param instead of via attention_mask.
          #       then we can simplify this whole if/else block to:
          #         mask = attention_mask if encoder_hidden_states is None else encoder_attention_mask
          mask = attention_mask
  
      hidden_states = self.resnets[0](
          hidden_states, temb, scale=lora_scale
      )
      for attn, resnet in zip(self.attentions, self.resnets[1:]):
          # attn
          hidden_states = attn(
              hidden_states,
              encoder_hidden_states=encoder_hidden_states,
              attention_mask=mask,
              **cross_attention_kwargs,
          )
  
          # resnet
          hidden_states = resnet(hidden_states, temb, scale=lora_scale)
  
      return hidden_states
  
imports: from typing import Any, Dict, List, Optional, Tuple, Union
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from ...configuration_utils import ConfigMixin, register_to_config
  from ...models import ModelMixin
  from ...models.activations import get_activation
  from ...models.attention import Attention
  from ...models.attention_processor import ADDED_KV_ATTENTION_PROCESSORS, CROSS_ATTENTION_PROCESSORS, AttentionProcessor, AttnAddedKVProcessor, AttnAddedKVProcessor2_0, AttnProcessor
  from ...models.dual_transformer_2d import DualTransformer2DModel
  from ...models.embeddings import GaussianFourierProjection, ImageHintTimeEmbedding, ImageProjection, ImageTimeEmbedding, TextImageProjection, TextImageTimeEmbedding, TextTimeEmbedding, TimestepEmbedding, Timesteps
  from ...models.transformer_2d import Transformer2DModel
  from ...models.unet_2d_condition import UNet2DConditionOutput
  from ...utils import USE_PEFT_BACKEND, is_torch_version, logging, scale_lora_layers, unscale_lora_layers
  from ...utils.torch_utils import apply_freeu
label_desc: Use image node_id 2 for calling the UNetMidBlockFlatSimpleCrossAttn obj's underlying member method code with example usage: obj.forward(hidden_states, temb, encoder_hidden_states, attention_mask, cross_attention_kwargs, encoder_attention_mask) and returns: hidden_states