MCPcopy Create free account
hub / github.com/ali-vilab/ACE_plus / DoubleStreamBlockD

Class DoubleStreamBlockD

modules/layers.py:408–492  ·  view source on GitHub ↗

A DiT block with parallel linear layers as described in https://arxiv.org/abs/2302.05442 and adapted modulation interface.

Source from the content-addressed store, hash-verified

406
407
408class DoubleStreamBlockD(DoubleStreamBlock):
409 """
410 A DiT block with parallel linear layers as described in
411 https://arxiv.org/abs/2302.05442 and adapted modulation interface.
412 """
413
414 def __init__(self, hidden_size: int, num_heads: int, mlp_ratio: float,
415 qkv_bias: bool = False, backend='pytorch'):
416 super().__init__(hidden_size, num_heads, mlp_ratio,
417 qkv_bias, backend)
418 mlp_hidden_dim = int(hidden_size * mlp_ratio)
419 self.edit_mod = Modulation(hidden_size, double=True)
420 self.edit_norm1 = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6)
421 self.edit_attn = SelfAttention(dim=hidden_size, num_heads=num_heads, qkv_bias=qkv_bias)
422
423 self.edit_norm2 = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6)
424 self.edit_mlp = nn.Sequential(
425 nn.Linear(hidden_size, mlp_hidden_dim, bias=True),
426 nn.GELU(approximate="tanh"),
427 nn.Linear(mlp_hidden_dim, hidden_size, bias=True),
428 )
429
430 def forward(self, x: Tensor, vec: Tensor,
431 pe: Tensor, mask: Tensor = None,
432 txt_length=None,
433 edit_length=None):
434 if edit_length is not None:
435 txt, edit, img = x[:, :txt_length], x[:, txt_length:txt_length + edit_length], x[:, txt_length + edit_length:]
436 else:
437 txt, img = x[:, :txt_length], x[:, txt_length:]
438 img_mod1, img_mod2 = self.img_mod(vec)
439 txt_mod1, txt_mod2 = self.txt_mod(vec)
440 # prepare image for attention
441 img_modulated = self.img_norm1(img)
442 img_modulated = (1 + img_mod1.scale) * img_modulated + img_mod1.shift
443 img_qkv = self.img_attn.qkv(img_modulated)
444 img_q, img_k, img_v = rearrange(img_qkv, "B L (K H D) -> K B H L D", K=3, H=self.num_heads)
445 img_q, img_k = self.img_attn.norm(img_q, img_k, img_v)
446 # prepare txt for attention
447 txt_modulated = self.txt_norm1(txt)
448 txt_modulated = (1 + txt_mod1.scale) * txt_modulated + txt_mod1.shift
449 txt_qkv = self.txt_attn.qkv(txt_modulated)
450 txt_q, txt_k, txt_v = rearrange(txt_qkv, "B L (K H D) -> K B H L D", K=3, H=self.num_heads)
451 txt_q, txt_k = self.txt_attn.norm(txt_q, txt_k, txt_v)
452
453 if edit_length is not None:
454 edit_mod1, edit_mod2 = self.edit_mod(vec)
455 # prepare edit for attention
456 edit_modulated = self.edit_norm1(edit)
457 edit_modulated = (1 + edit_mod1.scale) * edit_modulated + edit_mod1.shift
458 edit_qkv = self.edit_attn.qkv(edit_modulated)
459 edit_q, edit_k, edit_v = rearrange(edit_qkv, "B L (K H D) -> K B H L D", K=3, H=self.num_heads)
460 edit_q, edit_k = self.edit_attn.norm(edit_q, edit_k, edit_v)
461 else:
462 edit_q, edit_k, edit_v = None, None, None
463
464
465 # run actual attention

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected