class_docstr: A conditional 2D UNet model that takes a noisy sample, conditional state, and a timestep and returns a sample
  shaped output.
  
  This model inherits from [`ModelMixin`]. Check the superclass documentation for it's generic methods implemented
  for all models (such as downloading or saving).
  
  Parameters:
      sample_size (`int` or `Tuple[int, int]`, *optional*, defaults to `None`):
          Height and width of input/output sample.
      in_channels (`int`, *optional*, defaults to 4): Number of channels in the input sample.
      out_channels (`int`, *optional*, defaults to 4): Number of channels in the output.
      center_input_sample (`bool`, *optional*, defaults to `False`): Whether to center the input sample.
      flip_sin_to_cos (`bool`, *optional*, defaults to `False`):
          Whether to flip the sin to cos in the time embedding.
      freq_shift (`int`, *optional*, defaults to 0): The frequency shift to apply to the time embedding.
      down_block_types (`Tuple[str]`, *optional*, defaults to `("CrossAttnDownBlockFlat", "CrossAttnDownBlockFlat", "CrossAttnDownBlockFlat", "DownBlockFlat")`):
          The tuple of downsample blocks to use.
      mid_block_type (`str`, *optional*, defaults to `"UNetMidBlockFlatCrossAttn"`):
          Block type for middle of UNet, it can be either `UNetMidBlockFlatCrossAttn` or
          `UNetMidBlockFlatSimpleCrossAttn`. If `None`, the mid block layer is skipped.
      up_block_types (`Tuple[str]`, *optional*, defaults to `("UpBlockFlat", "CrossAttnUpBlockFlat", "CrossAttnUpBlockFlat", "CrossAttnUpBlockFlat")`):
          The tuple of upsample blocks to use.
      only_cross_attention(`bool` or `Tuple[bool]`, *optional*, default to `False`):
          Whether to include self-attention in the basic transformer blocks, see
          [`~models.attention.BasicTransformerBlock`].
      block_out_channels (`Tuple[int]`, *optional*, defaults to `(320, 640, 1280, 1280)`):
          The tuple of output channels for each block.
      layers_per_block (`int`, *optional*, defaults to 2): The number of layers per block.
      downsample_padding (`int`, *optional*, defaults to 1): The padding to use for the downsampling convolution.
      mid_block_scale_factor (`float`, *optional*, defaults to 1.0): The scale factor to use for the mid block.
      dropout (`float`, *optional*, defaults to 0.0): The dropout probability to use.
      act_fn (`str`, *optional*, defaults to `"silu"`): The activation function to use.
      norm_num_groups (`int`, *optional*, defaults to 32): The number of groups to use for the normalization.
          If `None`, normalization and activation layers is skipped in post-processing.
      norm_eps (`float`, *optional*, defaults to 1e-5): The epsilon to use for the normalization.
      cross_attention_dim (`int` or `Tuple[int]`, *optional*, defaults to 1280):
          The dimension of the cross attention features.
      transformer_layers_per_block (`int` or `Tuple[int]`, *optional*, defaults to 1):
          The number of transformer blocks of type [`~models.attention.BasicTransformerBlock`]. Only relevant for
          [`~models.unet_2d_blocks.CrossAttnDownBlockFlat`], [`~models.unet_2d_blocks.CrossAttnUpBlockFlat`],
          [`~models.unet_2d_blocks.UNetMidBlockFlatCrossAttn`].
      encoder_hid_dim (`int`, *optional*, defaults to None):
          If `encoder_hid_dim_type` is defined, `encoder_hidden_states` will be projected from `encoder_hid_dim`
          dimension to `cross_attention_dim`.
      encoder_hid_dim_type (`str`, *optional*, defaults to `None`):
          If given, the `encoder_hidden_states` and potentially other embeddings are down-projected to text
          embeddings of dimension `cross_attention` according to `encoder_hid_dim_type`.
      attention_head_dim (`int`, *optional*, defaults to 8): The dimension of the attention heads.
      num_attention_heads (`int`, *optional*):
          The number of attention heads. If not defined, defaults to `attention_head_dim`
      resnet_time_scale_shift (`str`, *optional*, defaults to `"default"`): Time scale shift config
          for ResNet blocks (see [`~models.resnet.ResnetBlockFlat`]). Choose from `default` or `scale_shift`.
      class_embed_type (`str`, *optional*, defaults to `None`):
          The type of class embedding to use which is ultimately summed with the time embeddings. Choose from `None`,
          `"timestep"`, `"identity"`, `"projection"`, or `"simple_projection"`.
      addition_embed_type (`str`, *optional*, defaults to `None`):
          Configures an optional embedding which will be summed with the time embeddings. Choose from `None` or
          "text". "text" will use the `TextTimeEmbedding` layer.
      addition_time_embed_dim: (`int`, *optional*, defaults to `None`):
          Dimension for the timestep embeddings.
      num_class_embeds (`int`, *optional*, defaults to `None`):
          Input dimension of the learnable embedding matrix to be projected to `time_embed_dim`, when performing
          class conditioning with `class_embed_type` equal to `None`.
      time_embedding_type (`str`, *optional*, defaults to `positional`):
          The type of position embedding to use for timesteps. Choose from `positional` or `fourier`.
      time_embedding_dim (`int`, *optional*, defaults to `None`):
          An optional override for the dimension of the projected time embedding.
      time_embedding_act_fn (`str`, *optional*, defaults to `None`):
          Optional activation function to use only once on the time embeddings before they are passed to the rest of
          the UNet. Choose from `silu`, `mish`, `gelu`, and `swish`.
      timestep_post_act (`str`, *optional*, defaults to `None`):
          The second activation function to use in timestep embedding. Choose from `silu`, `mish` and `gelu`.
      time_cond_proj_dim (`int`, *optional*, defaults to `None`):
          The dimension of `cond_proj` layer in the timestep embedding.
      conv_in_kernel (`int`, *optional*, default to `3`): The kernel size of `conv_in` layer.
      conv_out_kernel (`int`, *optional*, default to `3`): The kernel size of `conv_out` layer.
      projection_class_embeddings_input_dim (`int`, *optional*): The dimension of the `class_labels` input when
          `class_embed_type="projection"`. Required when `class_embed_type="projection"`.
      class_embeddings_concat (`bool`, *optional*, defaults to `False`): Whether to concatenate the time
          embeddings with the class embeddings.
      mid_block_only_cross_attention (`bool`, *optional*, defaults to `None`):
          Whether to use cross attention with the mid block when using the `UNetMidBlockFlatSimpleCrossAttn`. If
          `only_cross_attention` is given as a single boolean and `mid_block_only_cross_attention` is `None`, the
          `only_cross_attention` value is used as the value for `mid_block_only_cross_attention`. Default to `False`
          otherwise.
class_docstr_tok: ["A","conditional","2D","UNet","model","that","takes","a","noisy","sample",",","conditional","state",",","and","a","timestep","and","returns","a","sample","shaped","output",".","This","model","inherits","from","[","`","ModelMixin","`","]",".","Check","the","superclass","documentation","for","it","'s","generic","methods","implemented","for","all","models","(","such","as","downloading","or","saving",")",".","Parameters",":","sample_size","(","`","int","`","or","`","Tuple","[","int",",","int","]","`",",","*","optional","*",",","defaults","to","`","None","`",")",":","Height","and","width","of","input\/output","sample",".","in_channels","(","`","int","`",",","*","optional","*",",","defaults","to","4",")",":","Number","of","channels","in","the","input","sample",".","out_channels","(","`","int","`",",","*","optional","*",",","defaults","to","4",")",":","Number","of","channels","in","the","output",".","center_input_sample","(","`","bool","`",",","*","optional","*",",","defaults","to","`","False","`",")",":","Whether","to","center","the","input","sample",".","flip_sin_to_cos","(","`","bool","`",",","*","optional","*",",","defaults","to","`","False","`",")",":","Whether","to","flip","the","sin","to","cos","in","the","time","embedding",".","freq_shift","(","`","int","`",",","*","optional","*",",","defaults","to","0",")",":","The","frequency","shift","to","apply","to","the","time","embedding",".","down_block_types","(","`","Tuple","[","str","]","`",",","*","optional","*",",","defaults","to","`","(","``","CrossAttnDownBlockFlat","''",",","``","CrossAttnDownBlockFlat","''",",","``","CrossAttnDownBlockFlat","''",",","``","DownBlockFlat","''",")","`",")",":","The","tuple","of","downsample","blocks","to","use",".","mid_block_type","(","`","str","`",",","*","optional","*",",","defaults","to","`","``","UNetMidBlockFlatCrossAttn","''","`",")",":","Block","type","for","middle","of","UNet",",","it","can","be","either","`","UNetMidBlockFlatCrossAttn","`","or","`","UNetMidBlockFlatSimpleCrossAttn","`",".","If","`","None","`",",","the","mid","block","layer","is","skipped",".","up_block_types","(","`","Tuple","[","str","]","`",",","*","optional","*",",","defaults","to","`","(","``","UpBlockFlat","''",",","``","CrossAttnUpBlockFlat","''",",","``","CrossAttnUpBlockFlat","''",",","``","CrossAttnUpBlockFlat","''",")","`",")",":","The","tuple","of","upsample","blocks","to","use",".","only_cross_attention","(","`","bool","`","or","`","Tuple","[","bool","]","`",",","*","optional","*",",","default","to","`","False","`",")",":","Whether","to","include","self-attention","in","the","basic","transformer","blocks",",","see","[","`","~models.attention.BasicTransformerBlock","`","]",".","block_out_channels","(","`","Tuple","[","int","]","`",",","*","optional","*",",","defaults","to","`","(","320",",","640",",","1280",",","1280",")","`",")",":","The","tuple","of","output","channels","for","each","block",".","layers_per_block","(","`","int","`",",","*","optional","*",",","defaults","to","2",")",":","The","number","of","layers","per","block",".","downsample_padding","(","`","int","`",",","*","optional","*",",","defaults","to","1",")",":","The","padding","to","use","for","the","downsampling","convolution",".","mid_block_scale_factor","(","`","float","`",",","*","optional","*",",","defaults","to","1.0",")",":","The","scale","factor","to","use","for","the","mid","block",".","dropout","(","`","float","`",",","*","optional","*",",","defaults","to","0.0",")",":","The","dropout","probability","to","use",".","act_fn","(","`","str","`",",","*","optional","*",",","defaults","to","`","``","silu","''","`",")",":","The","activation","function","to","use",".","norm_num_groups","(","`","int","`",",","*","optional","*",",","defaults","to","32",")",":","The","number","of","groups","to","use","for","the","normalization",".","If","`","None","`",",","normalization","and","activation","layers","is","skipped","in","post-processing",".","norm_eps","(","`","float","`",",","*","optional","*",",","defaults","to","1e-5",")",":","The","epsilon","to","use","for","the","normalization",".","cross_attention_dim","(","`","int","`","or","`","Tuple","[","int","]","`",",","*","optional","*",",","defaults","to","1280",")",":","The","dimension","of","the","cross","attention","features",".","transformer_layers_per_block","(","`","int","`","or","`","Tuple","[","int","]","`",",","*","optional","*",",","defaults","to","1",")",":","The","number","of","transformer","blocks","of","type","[","`","~models.attention.BasicTransformerBlock","`","]",".","Only","relevant","for","[","`","~models.unet_2d_blocks.CrossAttnDownBlockFlat","`","]",",","[","`","~models.unet_2d_blocks.CrossAttnUpBlockFlat","`","]",",","[","`","~models.unet_2d_blocks.UNetMidBlockFlatCrossAttn","`","]",".","encoder_hid_dim","(","`","int","`",",","*","optional","*",",","defaults","to","None",")",":","If","`","encoder_hid_dim_type","`","is","defined",",","`","encoder_hidden_states","`","will","be","projected","from","`","encoder_hid_dim","`","dimension","to","`","cross_attention_dim","`",".","encoder_hid_dim_type","(","`","str","`",",","*","optional","*",",","defaults","to","`","None","`",")",":","If","given",",","the","`","encoder_hidden_states","`","and","potentially","other","embeddings","are","down-projected","to","text","embeddings","of","dimension","`","cross_attention","`","according","to","`","encoder_hid_dim_type","`",".","attention_head_dim","(","`","int","`",",","*","optional","*",",","defaults","to","8",")",":","The","dimension","of","the","attention","heads",".","num_attention_heads","(","`","int","`",",","*","optional","*",")",":","The","number","of","attention","heads",".","If","not","defined",",","defaults","to","`","attention_head_dim","`","resnet_time_scale_shift","(","`","str","`",",","*","optional","*",",","defaults","to","`","``","default","''","`",")",":","Time","scale","shift","config","for","ResNet","blocks","(","see","[","`","~models.resnet.ResnetBlockFlat","`","]",")",".","Choose","from","`","default","`","or","`","scale_shift","`",".","class_embed_type","(","`","str","`",",","*","optional","*",",","defaults","to","`","None","`",")",":","The","type","of","class","embedding","to","use","which","is","ultimately","summed","with","the","time","embeddings",".","Choose","from","`","None","`",",","`","``","timestep","''","`",",","`","``","identity","''","`",",","`","``","projection","''","`",",","or","`","``","simple_projection","''","`",".","addition_embed_type","(","`","str","`",",","*","optional","*",",","defaults","to","`","None","`",")",":","Configures","an","optional","embedding","which","will","be","summed","with","the","time","embeddings",".","Choose","from","`","None","`","or","``","text","''",".","``","text","''","will","use","the","`","TextTimeEmbedding","`","layer",".","addition_time_embed_dim",":","(","`","int","`",",","*","optional","*",",","defaults","to","`","None","`",")",":","Dimension","for","the","timestep","embeddings",".","num_class_embeds","(","`","int","`",",","*","optional","*",",","defaults","to","`","None","`",")",":","Input","dimension","of","the","learnable","embedding","matrix","to","be","projected","to","`","time_embed_dim","`",",","when","performing","class","conditioning","with","`","class_embed_type","`","equal","to","`","None","`",".","time_embedding_type","(","`","str","`",",","*","optional","*",",","defaults","to","`","positional","`",")",":","The","type","of","position","embedding","to","use","for","timesteps",".","Choose","from","`","positional","`","or","`","fourier","`",".","time_embedding_dim","(","`","int","`",",","*","optional","*",",","defaults","to","`","None","`",")",":","An","optional","override","for","the","dimension","of","the","projected","time","embedding",".","time_embedding_act_fn","(","`","str","`",",","*","optional","*",",","defaults","to","`","None","`",")",":","Optional","activation","function","to","use","only","once","on","the","time","embeddings","before","they","are","passed","to","the","rest","of","the","UNet",".","Choose","from","`","silu","`",",","`","mish","`",",","`","gelu","`",",","and","`","swish","`",".","timestep_post_act","(","`","str","`",",","*","optional","*",",","defaults","to","`","None","`",")",":","The","second","activation","function","to","use","in","timestep","embedding",".","Choose","from","`","silu","`",",","`","mish","`","and","`","gelu","`",".","time_cond_proj_dim","(","`","int","`",",","*","optional","*",",","defaults","to","`","None","`",")",":","The","dimension","of","`","cond_proj","`","layer","in","the","timestep","embedding",".","conv_in_kernel","(","`","int","`",",","*","optional","*",",","default","to","`","3","`",")",":","The","kernel","size","of","`","conv_in","`","layer",".","conv_out_kernel","(","`","int","`",",","*","optional","*",",","default","to","`","3","`",")",":","The","kernel","size","of","`","conv_out","`","layer",".","projection_class_embeddings_input_dim","(","`","int","`",",","*","optional","*",")",":","The","dimension","of","the","`","class_labels","`","input","when","`","class_embed_type=","''","projection","''","`",".","Required","when","`","class_embed_type=","''","projection","''","`",".","class_embeddings_concat","(","`","bool","`",",","*","optional","*",",","defaults","to","`","False","`",")",":","Whether","to","concatenate","the","time","embeddings","with","the","class","embeddings",".","mid_block_only_cross_attention","(","`","bool","`",",","*","optional","*",",","defaults","to","`","None","`",")",":","Whether","to","use","cross","attention","with","the","mid","block","when","using","the","`","UNetMidBlockFlatSimpleCrossAttn","`",".","If","`","only_cross_attention","`","is","given","as","a","single","boolean","and","`","mid_block_only_cross_attention","`","is","`","None","`",",","the","`","only_cross_attention","`","value","is","used","as","the","value","for","`","mid_block_only_cross_attention","`",".","Default","to","`","False","`","otherwise","."]
docstr: Enable sliced attention computation.
  
  When this option is enabled, the attention module splits the input tensor in slices to compute attention in
  several steps. This is useful for saving some memory in exchange for a small decrease in speed.
  
  Args:
      slice_size (`str` or `int` or `list(int)`, *optional*, defaults to `"auto"`):
          When `"auto"`, input to the attention heads is halved, so attention is computed in two steps. If
          `"max"`, maximum amount of memory is saved by running only one slice at a time. If a number is
          provided, uses as many slices as `attention_head_dim // slice_size`. In this case, `attention_head_dim`
          must be a multiple of `slice_size`.
docstr_tok: ["Enable","sliced","attention","computation",".","When","this","option","is","enabled",",","the","attention","module","splits","the","input","tensor","in","slices","to","compute","attention","in","several","steps",".","This","is","useful","for","saving","some","memory","in","exchange","for","a","small","decrease","in","speed",".","Args",":","slice_size","(","`","str","`","or","`","int","`","or","`","list","(","int",")","`",",","*","optional","*",",","defaults","to","`","``","auto","''","`",")",":","When","`","``","auto","''","`",",","input","to","the","attention","heads","is","halved",",","so","attention","is","computed","in","two","steps",".","If","`","``","max","''","`",",","maximum","amount","of","memory","is","saved","by","running","only","one","slice","at","a","time",".","If","a","number","is","provided",",","uses","as","many","slices","as","`","attention_head_dim","\/\/","slice_size","`",".","In","this","case",",","`","attention_head_dim","`","must","be","a","multiple","of","`","slice_size","`","."]
code: def set_attention_slice(self, slice_size):
      r"""
      Enable sliced attention computation.
  
      When this option is enabled, the attention module splits the input tensor in slices to compute attention in
      several steps. This is useful for saving some memory in exchange for a small decrease in speed.
  
      Args:
          slice_size (`str` or `int` or `list(int)`, *optional*, defaults to `"auto"`):
              When `"auto"`, input to the attention heads is halved, so attention is computed in two steps. If
              `"max"`, maximum amount of memory is saved by running only one slice at a time. If a number is
              provided, uses as many slices as `attention_head_dim // slice_size`. In this case, `attention_head_dim`
              must be a multiple of `slice_size`.
      """
      sliceable_head_dims = []
  
      def fn_recursive_retrieve_sliceable_dims(module: torch.nn.Module):
          if hasattr(module, "set_attention_slice"):
              sliceable_head_dims.append(module.sliceable_head_dim)
  
          for child in module.children():
              fn_recursive_retrieve_sliceable_dims(child)
  
      # retrieve number of attention layers
      for module in self.children():
          fn_recursive_retrieve_sliceable_dims(module)
  
      num_sliceable_layers = len(sliceable_head_dims)
  
      if slice_size == "auto":
          # half the attention head size is usually a good trade-off between
          # speed and memory
          slice_size = [dim // 2 for dim in sliceable_head_dims]
      elif slice_size == "max":
          # make smallest slice possible
          slice_size = num_sliceable_layers * [1]
  
      slice_size = (
          num_sliceable_layers * [slice_size]
          if not isinstance(slice_size, list)
          else slice_size
      )
  
      if len(slice_size) != len(sliceable_head_dims):
          raise ValueError(
              f"You have provided {len(slice_size)}, but {self.config} has {len(sliceable_head_dims)} different"
              f" attention layers. Make sure to match `len(slice_size)` to be {len(sliceable_head_dims)}."
          )
  
      for i in range(len(slice_size)):
          size = slice_size[i]
          dim = sliceable_head_dims[i]
          if size is not None and size > dim:
              raise ValueError(
                  f"size {size} has to be smaller or equal to {dim}."
              )
  
      # Recursively walk through all the children.
      # Any children which exposes the set_attention_slice method
      # gets the message
      def fn_recursive_set_attention_slice(
          module: torch.nn.Module, slice_size: List[int]
      ):
          if hasattr(module, "set_attention_slice"):
              module.set_attention_slice(slice_size.pop())
  
          for child in module.children():
              fn_recursive_set_attention_slice(child, slice_size)
  
      reversed_slice_size = list(reversed(slice_size))
      for module in self.children():
          fn_recursive_set_attention_slice(module, reversed_slice_size)
  
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 5 for calling the UNetFlatConditionModel obj's underlying member method code with example usage: obj.set_attention_slice(slice_size) without return types