r""" A cross attention layer. Parameters: query_dim (`int`): The number of channels in the query. cross_attention_dim (`int`, *optional*): The number of channels in the encoder_hidden_states. If not given, defaults to `query_dim`. heads (`int`
| 36 | |
| 37 | @maybe_allow_in_graph |
| 38 | class Attention(nn.Module): |
| 39 | r""" |
| 40 | A cross attention layer. |
| 41 | |
| 42 | Parameters: |
| 43 | query_dim (`int`): |
| 44 | The number of channels in the query. |
| 45 | cross_attention_dim (`int`, *optional*): |
| 46 | The number of channels in the encoder_hidden_states. If not given, defaults to `query_dim`. |
| 47 | heads (`int`, *optional*, defaults to 8): |
| 48 | The number of heads to use for multi-head attention. |
| 49 | dim_head (`int`, *optional*, defaults to 64): |
| 50 | The number of channels in each head. |
| 51 | dropout (`float`, *optional*, defaults to 0.0): |
| 52 | The dropout probability to use. |
| 53 | bias (`bool`, *optional*, defaults to False): |
| 54 | Set to `True` for the query, key, and value linear layers to contain a bias parameter. |
| 55 | upcast_attention (`bool`, *optional*, defaults to False): |
| 56 | Set to `True` to upcast the attention computation to `float32`. |
| 57 | upcast_softmax (`bool`, *optional*, defaults to False): |
| 58 | Set to `True` to upcast the softmax computation to `float32`. |
| 59 | cross_attention_norm (`str`, *optional*, defaults to `None`): |
| 60 | The type of normalization to use for the cross attention. Can be `None`, `layer_norm`, or `group_norm`. |
| 61 | cross_attention_norm_num_groups (`int`, *optional*, defaults to 32): |
| 62 | The number of groups to use for the group norm in the cross attention. |
| 63 | added_kv_proj_dim (`int`, *optional*, defaults to `None`): |
| 64 | The number of channels to use for the added key and value projections. If `None`, no projection is used. |
| 65 | norm_num_groups (`int`, *optional*, defaults to `None`): |
| 66 | The number of groups to use for the group norm in the attention. |
| 67 | spatial_norm_dim (`int`, *optional*, defaults to `None`): |
| 68 | The number of channels to use for the spatial normalization. |
| 69 | out_bias (`bool`, *optional*, defaults to `True`): |
| 70 | Set to `True` to use a bias in the output linear layer. |
| 71 | scale_qk (`bool`, *optional*, defaults to `True`): |
| 72 | Set to `True` to scale the query and key by `1 / sqrt(dim_head)`. |
| 73 | only_cross_attention (`bool`, *optional*, defaults to `False`): |
| 74 | Set to `True` to only use cross attention and not added_kv_proj_dim. Can only be set to `True` if |
| 75 | `added_kv_proj_dim` is not `None`. |
| 76 | eps (`float`, *optional*, defaults to 1e-5): |
| 77 | An additional value added to the denominator in group normalization that is used for numerical stability. |
| 78 | rescale_output_factor (`float`, *optional*, defaults to 1.0): |
| 79 | A factor to rescale the output by dividing it with this value. |
| 80 | residual_connection (`bool`, *optional*, defaults to `False`): |
| 81 | Set to `True` to add the residual connection to the output. |
| 82 | _from_deprecated_attn_block (`bool`, *optional*, defaults to `False`): |
| 83 | Set to `True` if the attention block is loaded from a deprecated state dict. |
| 84 | processor (`AttnProcessor`, *optional*, defaults to `None`): |
| 85 | The attention processor to use. If `None`, defaults to `AttnProcessor2_0` if `torch 2.x` is used and |
| 86 | `AttnProcessor` otherwise. |
| 87 | """ |
| 88 | |
| 89 | def __init__( |
| 90 | self, |
| 91 | query_dim: int, |
| 92 | cross_attention_dim: Optional[int] = None, |
| 93 | heads: int = 8, |
| 94 | dim_head: int = 64, |
| 95 | dropout: float = 0.0, |