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