MCPcopy Create free account
hub / github.com/TencentARC/BrushNet / Attention

Class Attention

src/diffusers/models/attention_processor.py:40–729  ·  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

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

Callers 15

__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85
__init__Method · 0.85

Calls

no outgoing calls

Tested by 1