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

Method __call__

src/diffusers/models/attention_processor.py:1213–1286  ·  view source on GitHub ↗
(
        self,
        attn: Attention,
        hidden_states: torch.FloatTensor,
        encoder_hidden_states: Optional[torch.FloatTensor] = None,
        attention_mask: Optional[torch.FloatTensor] = None,
        temb: Optional[torch.FloatTensor] = None,
        scale: float = 1.0,
    )

Source from the content-addressed store, hash-verified

1211 raise ImportError("AttnProcessor2_0 requires PyTorch 2.0, to use it, please upgrade PyTorch to 2.0.")
1212
1213 def __call__(
1214 self,
1215 attn: Attention,
1216 hidden_states: torch.FloatTensor,
1217 encoder_hidden_states: Optional[torch.FloatTensor] = None,
1218 attention_mask: Optional[torch.FloatTensor] = None,
1219 temb: Optional[torch.FloatTensor] = None,
1220 scale: float = 1.0,
1221 ) -> torch.FloatTensor:
1222 residual = hidden_states
1223 if attn.spatial_norm is not None:
1224 hidden_states = attn.spatial_norm(hidden_states, temb)
1225
1226 input_ndim = hidden_states.ndim
1227
1228 if input_ndim == 4:
1229 batch_size, channel, height, width = hidden_states.shape
1230 hidden_states = hidden_states.view(batch_size, channel, height * width).transpose(1, 2)
1231
1232 batch_size, sequence_length, _ = (
1233 hidden_states.shape if encoder_hidden_states is None else encoder_hidden_states.shape
1234 )
1235
1236 if attention_mask is not None:
1237 attention_mask = attn.prepare_attention_mask(attention_mask, sequence_length, batch_size)
1238 # scaled_dot_product_attention expects attention_mask shape to be
1239 # (batch, heads, source_length, target_length)
1240 attention_mask = attention_mask.view(batch_size, attn.heads, -1, attention_mask.shape[-1])
1241
1242 if attn.group_norm is not None:
1243 hidden_states = attn.group_norm(hidden_states.transpose(1, 2)).transpose(1, 2)
1244
1245 args = () if USE_PEFT_BACKEND else (scale,)
1246 query = attn.to_q(hidden_states, *args)
1247
1248 if encoder_hidden_states is None:
1249 encoder_hidden_states = hidden_states
1250 elif attn.norm_cross:
1251 encoder_hidden_states = attn.norm_encoder_hidden_states(encoder_hidden_states)
1252
1253 key = attn.to_k(encoder_hidden_states, *args)
1254 value = attn.to_v(encoder_hidden_states, *args)
1255
1256 inner_dim = key.shape[-1]
1257 head_dim = inner_dim // attn.heads
1258
1259 query = query.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
1260
1261 key = key.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
1262 value = value.view(batch_size, -1, attn.heads, head_dim).transpose(1, 2)
1263
1264 # the output of sdp = (batch, num_heads, seq_len, head_dim)
1265 # TODO: add support for attn.scale when we move to Torch 2.1
1266 hidden_states = F.scaled_dot_product_attention(
1267 query, key, value, attn_mask=attention_mask, dropout_p=0.0, is_causal=False
1268 )
1269
1270 hidden_states = hidden_states.transpose(1, 2).reshape(batch_size, -1, attn.heads * head_dim)

Callers

nothing calls this directly

Calls 4

transposeMethod · 0.80
toMethod · 0.45

Tested by

no test coverage detected