| 182 | |
| 183 | |
| 184 | class TemporalTransformerBlock(nn.Module): |
| 185 | def __init__( |
| 186 | self, |
| 187 | dim, |
| 188 | num_attention_heads, |
| 189 | attention_head_dim, |
| 190 | attention_block_types=( |
| 191 | "Temporal_Self", |
| 192 | "Temporal_Self", |
| 193 | ), |
| 194 | dropout=0.0, |
| 195 | norm_num_groups=32, |
| 196 | cross_attention_dim=768, |
| 197 | activation_fn="geglu", |
| 198 | attention_bias=False, |
| 199 | upcast_attention=False, |
| 200 | cross_frame_attention_mode=None, |
| 201 | temporal_position_encoding=False, |
| 202 | temporal_position_encoding_max_len=24, |
| 203 | ): |
| 204 | super().__init__() |
| 205 | |
| 206 | attention_blocks = [] |
| 207 | norms = [] |
| 208 | |
| 209 | for block_name in attention_block_types: |
| 210 | attention_blocks.append( |
| 211 | VersatileAttention( |
| 212 | attention_mode=block_name.split("_")[0], |
| 213 | cross_attention_dim=cross_attention_dim |
| 214 | if block_name.endswith("_Cross") |
| 215 | else None, |
| 216 | query_dim=dim, |
| 217 | heads=num_attention_heads, |
| 218 | dim_head=attention_head_dim, |
| 219 | dropout=dropout, |
| 220 | bias=attention_bias, |
| 221 | upcast_attention=upcast_attention, |
| 222 | cross_frame_attention_mode=cross_frame_attention_mode, |
| 223 | temporal_position_encoding=temporal_position_encoding, |
| 224 | temporal_position_encoding_max_len=temporal_position_encoding_max_len, |
| 225 | ) |
| 226 | ) |
| 227 | norms.append(nn.LayerNorm(dim)) |
| 228 | |
| 229 | self.attention_blocks = nn.ModuleList(attention_blocks) |
| 230 | self.norms = nn.ModuleList(norms) |
| 231 | |
| 232 | self.ff = FeedForward(dim, dropout=dropout, activation_fn=activation_fn) |
| 233 | self.ff_norm = nn.LayerNorm(dim) |
| 234 | |
| 235 | def forward( |
| 236 | self, |
| 237 | hidden_states, |
| 238 | encoder_hidden_states=None, |
| 239 | attention_mask=None, |
| 240 | video_length=None, |
| 241 | ): |