MCPcopy Index your code
hub / github.com/huggingface/diffusers / AttnProcessor

Class AttnProcessor

src/diffusers/models/attention_processor.py:1103–1172  ·  view source on GitHub ↗

r""" Default processor for performing attention-related computations.

Source from the content-addressed store, hash-verified

1101
1102
1103class AttnProcessor:
1104 r"""
1105 Default processor for performing attention-related computations.
1106 """
1107
1108 def __call__(
1109 self,
1110 attn: Attention,
1111 hidden_states: torch.Tensor,
1112 encoder_hidden_states: torch.Tensor | None = None,
1113 attention_mask: torch.Tensor | None = None,
1114 temb: torch.Tensor | None = None,
1115 *args,
1116 **kwargs,
1117 ) -> torch.Tensor:
1118 if len(args) > 0 or kwargs.get("scale", None) is not None:
1119 deprecation_message = "The `scale` argument is deprecated and will be ignored. Please remove it, as passing it will raise an error in the future. `scale` should directly be passed while calling the underlying pipeline component i.e., via `cross_attention_kwargs`."
1120 deprecate("scale", "1.0.0", deprecation_message)
1121
1122 residual = hidden_states
1123
1124 if attn.spatial_norm is not None:
1125 hidden_states = attn.spatial_norm(hidden_states, temb)
1126
1127 input_ndim = hidden_states.ndim
1128
1129 if input_ndim == 4:
1130 batch_size, channel, height, width = hidden_states.shape
1131 hidden_states = hidden_states.view(batch_size, channel, height * width).transpose(1, 2)
1132
1133 batch_size, sequence_length, _ = (
1134 hidden_states.shape if encoder_hidden_states is None else encoder_hidden_states.shape
1135 )
1136 attention_mask = attn.prepare_attention_mask(attention_mask, sequence_length, batch_size)
1137
1138 if attn.group_norm is not None:
1139 hidden_states = attn.group_norm(hidden_states.transpose(1, 2)).transpose(1, 2)
1140
1141 query = attn.to_q(hidden_states)
1142
1143 if encoder_hidden_states is None:
1144 encoder_hidden_states = hidden_states
1145 elif attn.norm_cross:
1146 encoder_hidden_states = attn.norm_encoder_hidden_states(encoder_hidden_states)
1147
1148 key = attn.to_k(encoder_hidden_states)
1149 value = attn.to_v(encoder_hidden_states)
1150
1151 query = attn.head_to_batch_dim(query)
1152 key = attn.head_to_batch_dim(key)
1153 value = attn.head_to_batch_dim(value)
1154
1155 attention_probs = attn.get_attention_scores(query, key, attention_mask)
1156 hidden_states = torch.bmm(attention_probs, value)
1157 hidden_states = attn.batch_to_head_dim(hidden_states)
1158
1159 # linear proj
1160 hidden_states = attn.to_out[0](hidden_states)

Callers 15

convert_modelsFunction · 0.90
make_transformerFunction · 0.90
mainFunction · 0.90
mainFunction · 0.90
mainFunction · 0.90
__init__Method · 0.90

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…