r""" A basic Transformer block. Parameters: dim (`int`): The number of channels in the input and output. num_attention_heads (`int`): The number of heads to use for multi-head attention. attention_head_dim (`int`): The number of channels in each head. dropout
| 618 | |
| 619 | |
| 620 | class StepVideoTransformerBlock(nn.Module): |
| 621 | r""" |
| 622 | A basic Transformer block. |
| 623 | |
| 624 | Parameters: |
| 625 | dim (`int`): The number of channels in the input and output. |
| 626 | num_attention_heads (`int`): The number of heads to use for multi-head attention. |
| 627 | attention_head_dim (`int`): The number of channels in each head. |
| 628 | dropout (`float`, *optional*, defaults to 0.0): The dropout probability to use. |
| 629 | cross_attention_dim (`int`, *optional*): The size of the encoder_hidden_states vector for cross attention. |
| 630 | activation_fn (`str`, *optional*, defaults to `"geglu"`): Activation function to be used in feed-forward. |
| 631 | num_embeds_ada_norm (: |
| 632 | obj: `int`, *optional*): The number of diffusion steps used during training. See `Transformer2DModel`. |
| 633 | attention_bias (: |
| 634 | obj: `bool`, *optional*, defaults to `False`): Configure if the attentions should contain a bias parameter. |
| 635 | only_cross_attention (`bool`, *optional*): |
| 636 | Whether to use only cross-attention layers. In this case two cross attention layers are used. |
| 637 | double_self_attention (`bool`, *optional*): |
| 638 | Whether to use two self-attention layers. In this case no cross attention layers are used. |
| 639 | upcast_attention (`bool`, *optional*): |
| 640 | Whether to upcast the attention computation to float32. This is useful for mixed precision training. |
| 641 | norm_elementwise_affine (`bool`, *optional*, defaults to `True`): |
| 642 | Whether to use learnable elementwise affine parameters for normalization. |
| 643 | norm_type (`str`, *optional*, defaults to `"layer_norm"`): |
| 644 | The normalization layer to use. Can be `"layer_norm"`, `"ada_norm"` or `"ada_norm_zero"`. |
| 645 | final_dropout (`bool` *optional*, defaults to False): |
| 646 | Whether to apply a final dropout after the last feed-forward layer. |
| 647 | attention_type (`str`, *optional*, defaults to `"default"`): |
| 648 | The type of attention to use. Can be `"default"` or `"gated"` or `"gated-text-image"`. |
| 649 | positional_embeddings (`str`, *optional*, defaults to `None`): |
| 650 | The type of positional embeddings to apply to. |
| 651 | num_positional_embeddings (`int`, *optional*, defaults to `None`): |
| 652 | The maximum number of positional embeddings to apply. |
| 653 | """ |
| 654 | |
| 655 | def __init__( |
| 656 | self, |
| 657 | dim: int, |
| 658 | attention_head_dim: int, |
| 659 | norm_eps: float = 1e-5, |
| 660 | ff_inner_dim: Optional[int] = None, |
| 661 | ff_bias: bool = False, |
| 662 | attention_type: str = 'parallel' |
| 663 | ): |
| 664 | super().__init__() |
| 665 | self.dim = dim |
| 666 | self.norm1 = nn.LayerNorm(dim, eps=norm_eps) |
| 667 | self.attn1 = SelfAttention(dim, attention_head_dim, bias=False, with_rope=True, with_qk_norm=True, attn_type=attention_type) |
| 668 | |
| 669 | self.norm2 = nn.LayerNorm(dim, eps=norm_eps) |
| 670 | self.attn2 = CrossAttention(dim, attention_head_dim, bias=False, with_qk_norm=True, attn_type='torch') |
| 671 | |
| 672 | self.ff = FeedForward(dim=dim, inner_dim=ff_inner_dim, dim_out=dim, bias=ff_bias) |
| 673 | |
| 674 | self.scale_shift_table = nn.Parameter(torch.randn(6, dim) /dim**0.5) |
| 675 | |
| 676 | @torch.no_grad() |
| 677 | def forward( |