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

Method forward

src/diffusers/models/attention.py:291–412  ·  view source on GitHub ↗
(
        self,
        hidden_states: torch.FloatTensor,
        attention_mask: Optional[torch.FloatTensor] = None,
        encoder_hidden_states: Optional[torch.FloatTensor] = None,
        encoder_attention_mask: Optional[torch.FloatTensor] = None,
        timestep: Optional[torch.LongTensor] = None,
        cross_attention_kwargs: Dict[str, Any] = None,
        class_labels: Optional[torch.LongTensor] = None,
        added_cond_kwargs: Optional[Dict[str, torch.Tensor]] = None,
    )

Source from the content-addressed store, hash-verified

289 self._chunk_dim = dim
290
291 def forward(
292 self,
293 hidden_states: torch.FloatTensor,
294 attention_mask: Optional[torch.FloatTensor] = None,
295 encoder_hidden_states: Optional[torch.FloatTensor] = None,
296 encoder_attention_mask: Optional[torch.FloatTensor] = None,
297 timestep: Optional[torch.LongTensor] = None,
298 cross_attention_kwargs: Dict[str, Any] = None,
299 class_labels: Optional[torch.LongTensor] = None,
300 added_cond_kwargs: Optional[Dict[str, torch.Tensor]] = None,
301 ) -> torch.FloatTensor:
302 # Notice that normalization is always applied before the real computation in the following blocks.
303 # 0. Self-Attention
304 batch_size = hidden_states.shape[0]
305
306 if self.norm_type == "ada_norm":
307 norm_hidden_states = self.norm1(hidden_states, timestep)
308 elif self.norm_type == "ada_norm_zero":
309 norm_hidden_states, gate_msa, shift_mlp, scale_mlp, gate_mlp = self.norm1(
310 hidden_states, timestep, class_labels, hidden_dtype=hidden_states.dtype
311 )
312 elif self.norm_type in ["layer_norm", "layer_norm_i2vgen"]:
313 norm_hidden_states = self.norm1(hidden_states)
314 elif self.norm_type == "ada_norm_continuous":
315 norm_hidden_states = self.norm1(hidden_states, added_cond_kwargs["pooled_text_emb"])
316 elif self.norm_type == "ada_norm_single":
317 shift_msa, scale_msa, gate_msa, shift_mlp, scale_mlp, gate_mlp = (
318 self.scale_shift_table[None] + timestep.reshape(batch_size, 6, -1)
319 ).chunk(6, dim=1)
320 norm_hidden_states = self.norm1(hidden_states)
321 norm_hidden_states = norm_hidden_states * (1 + scale_msa) + shift_msa
322 norm_hidden_states = norm_hidden_states.squeeze(1)
323 else:
324 raise ValueError("Incorrect norm used")
325
326 if self.pos_embed is not None:
327 norm_hidden_states = self.pos_embed(norm_hidden_states)
328
329 # 1. Retrieve lora scale.
330 lora_scale = cross_attention_kwargs.get("scale", 1.0) if cross_attention_kwargs is not None else 1.0
331
332 # 2. Prepare GLIGEN inputs
333 cross_attention_kwargs = cross_attention_kwargs.copy() if cross_attention_kwargs is not None else {}
334 gligen_kwargs = cross_attention_kwargs.pop("gligen", None)
335
336 attn_output = self.attn1(
337 norm_hidden_states,
338 encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None,
339 attention_mask=attention_mask,
340 **cross_attention_kwargs,
341 )
342 if self.norm_type == "ada_norm_zero":
343 attn_output = gate_msa.unsqueeze(1) * attn_output
344 elif self.norm_type == "ada_norm_single":
345 attn_output = gate_msa * attn_output
346
347 hidden_states = attn_output + hidden_states
348 if hidden_states.ndim == 4:

Callers

nothing calls this directly

Calls 2

_chunked_feed_forwardFunction · 0.85
popMethod · 0.45

Tested by

no test coverage detected