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