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,
      encoder_hidden_states_1: Optional[torch.FloatTensor] = None,
      encoder_attention_mask_1: Optional[torch.FloatTensor] = None,
  ):
      output_states = ()
      num_layers = len(self.resnets)
      num_attention_per_layer = len(self.attentions) // num_layers
  
      encoder_hidden_states_1 = (
          encoder_hidden_states_1
          if encoder_hidden_states_1 is not None
          else encoder_hidden_states
      )
      encoder_attention_mask_1 = (
          encoder_attention_mask_1
          if encoder_hidden_states_1 is not None
          else encoder_attention_mask
      )
  
      for i in range(num_layers):
          if self.training and self.gradient_checkpointing:
  
              def create_custom_forward(module, return_dict=None):
                  def custom_forward(*inputs):
                      if return_dict is not None:
                          return module(
                              *inputs, return_dict=return_dict
                          )
                      else:
                          return module(*inputs)
  
                  return custom_forward
  
              ckpt_kwargs: Dict[str, Any] = (
                  {"use_reentrant": False}
                  if is_torch_version(">=", "1.11.0")
                  else {}
              )
              hidden_states = torch.utils.checkpoint.checkpoint(
                  create_custom_forward(self.resnets[i]),
                  hidden_states,
                  temb,
                  **ckpt_kwargs,
              )
              for idx, cross_attention_dim in enumerate(
                  self.cross_attention_dim
              ):
                  if cross_attention_dim is not None and idx <= 1:
                      forward_encoder_hidden_states = (
                          encoder_hidden_states
                      )
                      forward_encoder_attention_mask = (
                          encoder_attention_mask
                      )
                  elif cross_attention_dim is not None and idx > 1:
                      forward_encoder_hidden_states = (
                          encoder_hidden_states_1
                      )
                      forward_encoder_attention_mask = (
                          encoder_attention_mask_1
                      )
                  else:
                      forward_encoder_hidden_states = None
                      forward_encoder_attention_mask = None
                  hidden_states = torch.utils.checkpoint.checkpoint(
                      create_custom_forward(
                          self.attentions[
                              i * num_attention_per_layer + idx
                          ],
                          return_dict=False,
                      ),
                      hidden_states,
                      forward_encoder_hidden_states,
                      None,  # timestep
                      None,  # class_labels
                      cross_attention_kwargs,
                      attention_mask,
                      forward_encoder_attention_mask,
                      **ckpt_kwargs,
                  )[0]
          else:
              hidden_states = self.resnets[i](hidden_states, temb)
              for idx, cross_attention_dim in enumerate(
                  self.cross_attention_dim
              ):
                  if cross_attention_dim is not None and idx <= 1:
                      forward_encoder_hidden_states = (
                          encoder_hidden_states
                      )
                      forward_encoder_attention_mask = (
                          encoder_attention_mask
                      )
                  elif cross_attention_dim is not None and idx > 1:
                      forward_encoder_hidden_states = (
                          encoder_hidden_states_1
                      )
                      forward_encoder_attention_mask = (
                          encoder_attention_mask_1
                      )
                  else:
                      forward_encoder_hidden_states = None
                      forward_encoder_attention_mask = None
                  hidden_states = self.attentions[
                      i * num_attention_per_layer + idx
                  ](
                      hidden_states,
                      attention_mask=attention_mask,
                      encoder_hidden_states=forward_encoder_hidden_states,
                      encoder_attention_mask=forward_encoder_attention_mask,
                      return_dict=False,
                  )[
                      0
                  ]
  
          output_states = output_states + (hidden_states,)
  
      if self.downsamplers is not None:
          for downsampler in self.downsamplers:
              hidden_states = downsampler(hidden_states)
  
          output_states = output_states + (hidden_states,)
  
      return hidden_states, output_states
  
imports: from dataclasses import dataclass
  from typing import Any, Dict, List, Optional, Tuple, Union
  import torch
  import torch.nn
  import torch.utils.checkpoint
  from ...configuration_utils import ConfigMixin, register_to_config
  from ...loaders import UNet2DConditionLoadersMixin
  from ...models.activations import get_activation
  from ...models.attention_processor import ADDED_KV_ATTENTION_PROCESSORS, CROSS_ATTENTION_PROCESSORS, AttentionProcessor, AttnAddedKVProcessor, AttnProcessor
  from ...models.embeddings import TimestepEmbedding, Timesteps
  from ...models.modeling_utils import ModelMixin
  from ...models.resnet import Downsample2D, ResnetBlock2D, Upsample2D
  from ...models.transformer_2d import Transformer2DModel
  from ...models.unet_2d_blocks import DownBlock2D, UpBlock2D
  from ...models.unet_2d_condition import UNet2DConditionOutput
  from ...utils import BaseOutput, is_torch_version, logging
label_desc: Use image node_id 2 for calling the CrossAttnDownBlock2D 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, encoder_hidden_states_1, encoder_attention_mask_1) and returns: hidden_states,  output_states, custom_forward, module, module