| 91 | |
| 92 | |
| 93 | class TemporalTransformer3DModel(nn.Module): |
| 94 | def __init__( |
| 95 | self, |
| 96 | in_channels, |
| 97 | num_attention_heads, |
| 98 | attention_head_dim, |
| 99 | num_layers, |
| 100 | attention_block_types=( |
| 101 | "Temporal_Self", |
| 102 | "Temporal_Self", |
| 103 | ), |
| 104 | dropout=0.0, |
| 105 | norm_num_groups=32, |
| 106 | cross_attention_dim=768, |
| 107 | activation_fn="geglu", |
| 108 | attention_bias=False, |
| 109 | upcast_attention=False, |
| 110 | cross_frame_attention_mode=None, |
| 111 | temporal_position_encoding=False, |
| 112 | temporal_position_encoding_max_len=24, |
| 113 | ): |
| 114 | super().__init__() |
| 115 | |
| 116 | inner_dim = num_attention_heads * attention_head_dim |
| 117 | |
| 118 | self.norm = torch.nn.GroupNorm( |
| 119 | num_groups=norm_num_groups, num_channels=in_channels, eps=1e-6, affine=True |
| 120 | ) |
| 121 | self.proj_in = nn.Linear(in_channels, inner_dim) |
| 122 | |
| 123 | self.transformer_blocks = nn.ModuleList( |
| 124 | [ |
| 125 | TemporalTransformerBlock( |
| 126 | dim=inner_dim, |
| 127 | num_attention_heads=num_attention_heads, |
| 128 | attention_head_dim=attention_head_dim, |
| 129 | attention_block_types=attention_block_types, |
| 130 | dropout=dropout, |
| 131 | norm_num_groups=norm_num_groups, |
| 132 | cross_attention_dim=cross_attention_dim, |
| 133 | activation_fn=activation_fn, |
| 134 | attention_bias=attention_bias, |
| 135 | upcast_attention=upcast_attention, |
| 136 | cross_frame_attention_mode=cross_frame_attention_mode, |
| 137 | temporal_position_encoding=temporal_position_encoding, |
| 138 | temporal_position_encoding_max_len=temporal_position_encoding_max_len, |
| 139 | ) |
| 140 | for d in range(num_layers) |
| 141 | ] |
| 142 | ) |
| 143 | self.proj_out = nn.Linear(inner_dim, in_channels) |
| 144 | |
| 145 | def forward(self, hidden_states, encoder_hidden_states=None, attention_mask=None): |
| 146 | assert ( |
| 147 | hidden_states.dim() == 5 |
| 148 | ), f"Expected hidden_states to have ndim=5, but got ndim={hidden_states.dim()}." |
| 149 | video_length = hidden_states.shape[2] |
| 150 | hidden_states = rearrange(hidden_states, "b c f h w -> (b f) c h w") |