MCPcopy Create free account
hub / github.com/huggingface/diffusers / Attention

Class Attention

src/diffusers/models/attention_processor.py:52–837  ·  view source on GitHub ↗

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`

Source from the content-addressed store, hash-verified

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

Callers 15

__init__Method · 0.90
__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.70
__init__Method · 0.50
__init__Method · 0.50

Calls

no outgoing calls

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…