| 48 | |
| 49 | |
| 50 | class VanillaTemporalModule(nn.Module): |
| 51 | def __init__( |
| 52 | self, |
| 53 | in_channels, |
| 54 | num_attention_heads = 8, |
| 55 | num_transformer_block = 2, |
| 56 | attention_block_types =( "Temporal_Self", "Temporal_Self" ), |
| 57 | cross_frame_attention_mode = None, |
| 58 | temporal_position_encoding = False, |
| 59 | temporal_position_encoding_max_len = 24, |
| 60 | temporal_attention_dim_div = 1, |
| 61 | zero_initialize = True, |
| 62 | ): |
| 63 | super().__init__() |
| 64 | |
| 65 | self.temporal_transformer = TemporalTransformer3DModel( |
| 66 | in_channels=in_channels, |
| 67 | num_attention_heads=num_attention_heads, |
| 68 | attention_head_dim=in_channels // num_attention_heads // temporal_attention_dim_div, |
| 69 | num_layers=num_transformer_block, |
| 70 | attention_block_types=attention_block_types, |
| 71 | norm_num_groups=11 if in_channels==77 else 32, |
| 72 | cross_frame_attention_mode=cross_frame_attention_mode, |
| 73 | temporal_position_encoding=temporal_position_encoding, |
| 74 | temporal_position_encoding_max_len=temporal_position_encoding_max_len, |
| 75 | ) |
| 76 | |
| 77 | if zero_initialize: |
| 78 | self.temporal_transformer.proj_out = zero_module(self.temporal_transformer.proj_out) |
| 79 | |
| 80 | def forward(self, input_tensor, temb, encoder_hidden_states, attention_mask=None, anchor_frame_idx=None): |
| 81 | hidden_states = input_tensor |
| 82 | hidden_states = self.temporal_transformer(hidden_states, encoder_hidden_states, attention_mask) |
| 83 | |
| 84 | output = hidden_states |
| 85 | return output |
| 86 | |
| 87 | |
| 88 | class TemporalTransformer3DModel(nn.Module): |