code: def __init__(
      self,
      in_channels: int,
      temb_channels: int,
      dropout: float = 0.0,
      num_layers: int = 1,
      transformer_layers_per_block: int = 1,
      resnet_eps: float = 1e-6,
      resnet_time_scale_shift: str = "default",
      resnet_act_fn: str = "swish",
      resnet_groups: int = 32,
      resnet_pre_norm: bool = True,
      num_attention_heads=1,
      output_scale_factor=1.0,
      cross_attention_dim=1280,
      use_linear_projection=False,
      upcast_attention=False,
  ):
      super().__init__()
  
      self.has_cross_attention = True
      self.num_attention_heads = num_attention_heads
      resnet_groups = (
          resnet_groups
          if resnet_groups is not None
          else min(in_channels // 4, 32)
      )
  
      if isinstance(cross_attention_dim, int):
          cross_attention_dim = (cross_attention_dim,)
      if (
          isinstance(cross_attention_dim, (list, tuple))
          and len(cross_attention_dim) > 4
      ):
          raise ValueError(
              "Only up to 4 cross-attention layers are supported. Ensure that the length of cross-attention "
              f"dims is less than or equal to 4. Got cross-attention dims {cross_attention_dim} of length {len(cross_attention_dim)}"
          )
      self.cross_attention_dim = cross_attention_dim
  
      # there is always at least one resnet
      resnets = [
          ResnetBlock2D(
              in_channels=in_channels,
              out_channels=in_channels,
              temb_channels=temb_channels,
              eps=resnet_eps,
              groups=resnet_groups,
              dropout=dropout,
              time_embedding_norm=resnet_time_scale_shift,
              non_linearity=resnet_act_fn,
              output_scale_factor=output_scale_factor,
              pre_norm=resnet_pre_norm,
          )
      ]
      attentions = []
  
      for i in range(num_layers):
          for j in range(len(cross_attention_dim)):
              attentions.append(
                  Transformer2DModel(
                      num_attention_heads,
                      in_channels // num_attention_heads,
                      in_channels=in_channels,
                      num_layers=transformer_layers_per_block,
                      cross_attention_dim=cross_attention_dim[j],
                      norm_num_groups=resnet_groups,
                      use_linear_projection=use_linear_projection,
                      upcast_attention=upcast_attention,
                      double_self_attention=True
                      if cross_attention_dim[j] is None
                      else False,
                  )
              )
          resnets.append(
              ResnetBlock2D(
                  in_channels=in_channels,
                  out_channels=in_channels,
                  temb_channels=temb_channels,
                  eps=resnet_eps,
                  groups=resnet_groups,
                  dropout=dropout,
                  time_embedding_norm=resnet_time_scale_shift,
                  non_linearity=resnet_act_fn,
                  output_scale_factor=output_scale_factor,
                  pre_norm=resnet_pre_norm,
              )
          )
  
      self.attentions = nn.ModuleList(attentions)
      self.resnets = nn.ModuleList(resnets)
  
      self.gradient_checkpointing = False
  
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 1 to create a new UNetMidBlock2DCrossAttn object from inherited base classes: nn with example: obj = UNetMidBlock2DCrossAttn(in_channels, temb_channels, dropout, num_layers, transformer_layers_per_block, resnet_eps, resnet_time_scale_shift, resnet_act_fn, resnet_groups, resnet_pre_norm, num_attention_heads, output_scale_factor, cross_attention_dim, use_linear_projection, upcast_attention)