|
|
|
import torch |
|
from torch import nn |
|
import torch.nn.functional as F |
|
|
|
import math |
|
from typing import Optional |
|
from einops import rearrange, repeat |
|
from dataclasses import dataclass |
|
|
|
from diffusers.configuration_utils import ConfigMixin, register_to_config |
|
from diffusers.modeling_utils import ModelMixin |
|
from diffusers.utils import BaseOutput |
|
from diffusers.utils.import_utils import is_xformers_available |
|
from diffusers.models.attention import CrossAttention, FeedForward, AdaLayerNorm |
|
|
|
|
|
@dataclass |
|
class Transformer3DModelOutput(BaseOutput): |
|
sample: torch.FloatTensor |
|
|
|
|
|
if is_xformers_available(): |
|
import xformers |
|
import xformers.ops |
|
else: |
|
xformers = None |
|
|
|
|
|
class Transformer3DModel(ModelMixin, ConfigMixin): |
|
@register_to_config |
|
def __init__( |
|
self, |
|
num_attention_heads: int = 16, |
|
attention_head_dim: int = 88, |
|
in_channels: Optional[int] = None, |
|
num_layers: int = 1, |
|
dropout: float = 0.0, |
|
norm_num_groups: int = 32, |
|
cross_attention_dim: Optional[int] = None, |
|
attention_bias: bool = False, |
|
activation_fn: str = "geglu", |
|
num_embeds_ada_norm: Optional[int] = None, |
|
use_linear_projection: bool = False, |
|
only_cross_attention: bool = False, |
|
upcast_attention: bool = False, |
|
|
|
unet_use_cross_frame_attention=None, |
|
unet_use_temporal_attention=None, |
|
): |
|
super().__init__() |
|
self.use_linear_projection = use_linear_projection |
|
self.num_attention_heads = num_attention_heads |
|
self.attention_head_dim = attention_head_dim |
|
inner_dim = num_attention_heads * attention_head_dim |
|
|
|
|
|
self.in_channels = in_channels |
|
|
|
self.norm = torch.nn.GroupNorm(num_groups=norm_num_groups, num_channels=in_channels, eps=1e-6, affine=True) |
|
if use_linear_projection: |
|
self.proj_in = nn.Linear(in_channels, inner_dim) |
|
else: |
|
self.proj_in = nn.Conv2d(in_channels, inner_dim, kernel_size=1, stride=1, padding=0) |
|
|
|
|
|
self.transformer_blocks = nn.ModuleList( |
|
[ |
|
BasicTransformerBlock( |
|
inner_dim, |
|
num_attention_heads, |
|
attention_head_dim, |
|
dropout=dropout, |
|
cross_attention_dim=cross_attention_dim, |
|
activation_fn=activation_fn, |
|
num_embeds_ada_norm=num_embeds_ada_norm, |
|
attention_bias=attention_bias, |
|
only_cross_attention=only_cross_attention, |
|
upcast_attention=upcast_attention, |
|
|
|
unet_use_cross_frame_attention=unet_use_cross_frame_attention, |
|
unet_use_temporal_attention=unet_use_temporal_attention, |
|
) |
|
for d in range(num_layers) |
|
] |
|
) |
|
|
|
|
|
if use_linear_projection: |
|
self.proj_out = nn.Linear(in_channels, inner_dim) |
|
else: |
|
self.proj_out = nn.Conv2d(inner_dim, in_channels, kernel_size=1, stride=1, padding=0) |
|
|
|
def forward(self, hidden_states, encoder_hidden_states=None, timestep=None, return_dict: bool = True): |
|
|
|
assert hidden_states.dim() == 5, f"Expected hidden_states to have ndim=5, but got ndim={hidden_states.dim()}." |
|
video_length = hidden_states.shape[2] |
|
hidden_states = rearrange(hidden_states, "b c f h w -> (b f) c h w") |
|
encoder_hidden_states = repeat(encoder_hidden_states, 'b n c -> (b f) n c', f=video_length) |
|
|
|
batch, channel, height, weight = hidden_states.shape |
|
residual = hidden_states |
|
|
|
hidden_states = self.norm(hidden_states) |
|
if not self.use_linear_projection: |
|
hidden_states = self.proj_in(hidden_states) |
|
inner_dim = hidden_states.shape[1] |
|
hidden_states = hidden_states.permute(0, 2, 3, 1).reshape(batch, height * weight, inner_dim) |
|
else: |
|
inner_dim = hidden_states.shape[1] |
|
hidden_states = hidden_states.permute(0, 2, 3, 1).reshape(batch, height * weight, inner_dim) |
|
hidden_states = self.proj_in(hidden_states) |
|
|
|
|
|
for block in self.transformer_blocks: |
|
hidden_states = block( |
|
hidden_states, |
|
encoder_hidden_states=encoder_hidden_states, |
|
timestep=timestep, |
|
video_length=video_length |
|
) |
|
|
|
|
|
if not self.use_linear_projection: |
|
hidden_states = ( |
|
hidden_states.reshape(batch, height, weight, inner_dim).permute(0, 3, 1, 2).contiguous() |
|
) |
|
hidden_states = self.proj_out(hidden_states) |
|
else: |
|
hidden_states = self.proj_out(hidden_states) |
|
hidden_states = ( |
|
hidden_states.reshape(batch, height, weight, inner_dim).permute(0, 3, 1, 2).contiguous() |
|
) |
|
|
|
output = hidden_states + residual |
|
|
|
output = rearrange(output, "(b f) c h w -> b c f h w", f=video_length) |
|
if not return_dict: |
|
return (output,) |
|
|
|
return Transformer3DModelOutput(sample=output) |
|
|
|
|
|
class BasicTransformerBlock(nn.Module): |
|
def __init__( |
|
self, |
|
dim: int, |
|
num_attention_heads: int, |
|
attention_head_dim: int, |
|
dropout=0.0, |
|
cross_attention_dim: Optional[int] = None, |
|
activation_fn: str = "geglu", |
|
num_embeds_ada_norm: Optional[int] = None, |
|
attention_bias: bool = False, |
|
only_cross_attention: bool = False, |
|
upcast_attention: bool = False, |
|
|
|
unet_use_cross_frame_attention = None, |
|
unet_use_temporal_attention = None, |
|
): |
|
super().__init__() |
|
self.only_cross_attention = only_cross_attention |
|
self.use_ada_layer_norm = num_embeds_ada_norm is not None |
|
self.unet_use_cross_frame_attention = unet_use_cross_frame_attention |
|
self.unet_use_temporal_attention = unet_use_temporal_attention |
|
|
|
|
|
assert unet_use_cross_frame_attention is not None |
|
self.attn1 = CrossAttention( |
|
query_dim=dim, |
|
heads=num_attention_heads, |
|
dim_head=attention_head_dim, |
|
dropout=dropout, |
|
bias=attention_bias, |
|
upcast_attention=upcast_attention, |
|
) |
|
|
|
self.norm1 = AdaLayerNorm(dim, num_embeds_ada_norm) if self.use_ada_layer_norm else nn.LayerNorm(dim) |
|
|
|
|
|
if cross_attention_dim is not None: |
|
self.attn2 = CrossAttention( |
|
query_dim=dim, |
|
cross_attention_dim=cross_attention_dim, |
|
heads=num_attention_heads, |
|
dim_head=attention_head_dim, |
|
dropout=dropout, |
|
bias=attention_bias, |
|
upcast_attention=upcast_attention, |
|
) |
|
else: |
|
self.attn2 = None |
|
|
|
if cross_attention_dim is not None: |
|
self.norm2 = AdaLayerNorm(dim, num_embeds_ada_norm) if self.use_ada_layer_norm else nn.LayerNorm(dim) |
|
else: |
|
self.norm2 = None |
|
|
|
|
|
self.ff = FeedForward(dim, dropout=dropout, activation_fn=activation_fn) |
|
self.norm3 = nn.LayerNorm(dim) |
|
|
|
|
|
assert unet_use_temporal_attention is not None |
|
if unet_use_temporal_attention: |
|
self.attn_temp = CrossAttention( |
|
query_dim=dim, |
|
heads=num_attention_heads, |
|
dim_head=attention_head_dim, |
|
dropout=dropout, |
|
bias=attention_bias, |
|
upcast_attention=upcast_attention, |
|
) |
|
nn.init.zeros_(self.attn_temp.to_out[0].weight.data) |
|
self.norm_temp = AdaLayerNorm(dim, num_embeds_ada_norm) if self.use_ada_layer_norm else nn.LayerNorm(dim) |
|
|
|
def set_use_memory_efficient_attention_xformers(self, use_memory_efficient_attention_xformers: bool): |
|
if not is_xformers_available(): |
|
print("Here is how to install it") |
|
raise ModuleNotFoundError( |
|
"Refer to https://github.com/facebookresearch/xformers for more information on how to install" |
|
" xformers", |
|
name="xformers", |
|
) |
|
elif not torch.cuda.is_available(): |
|
raise ValueError( |
|
"torch.cuda.is_available() should be True but is False. xformers' memory efficient attention is only" |
|
" available for GPU " |
|
) |
|
else: |
|
try: |
|
|
|
_ = xformers.ops.memory_efficient_attention( |
|
torch.randn((1, 2, 40), device="cuda"), |
|
torch.randn((1, 2, 40), device="cuda"), |
|
torch.randn((1, 2, 40), device="cuda"), |
|
) |
|
except Exception as e: |
|
raise e |
|
self.attn1._use_memory_efficient_attention_xformers = use_memory_efficient_attention_xformers |
|
if self.attn2 is not None: |
|
self.attn2._use_memory_efficient_attention_xformers = use_memory_efficient_attention_xformers |
|
|
|
def forward(self, hidden_states, encoder_hidden_states=None, timestep=None, attention_mask=None, video_length=None): |
|
|
|
norm_hidden_states = ( |
|
self.norm1(hidden_states, timestep) if self.use_ada_layer_norm else self.norm1(hidden_states) |
|
) |
|
|
|
if self.unet_use_cross_frame_attention: |
|
hidden_states = self.attn1(norm_hidden_states, attention_mask=attention_mask, video_length=video_length) + hidden_states |
|
else: |
|
hidden_states = self.attn1(norm_hidden_states, attention_mask=attention_mask) + hidden_states |
|
|
|
if self.attn2 is not None: |
|
|
|
norm_hidden_states = ( |
|
self.norm2(hidden_states, timestep) if self.use_ada_layer_norm else self.norm2(hidden_states) |
|
) |
|
hidden_states = ( |
|
self.attn2( |
|
norm_hidden_states, encoder_hidden_states=encoder_hidden_states, attention_mask=attention_mask |
|
) |
|
+ hidden_states |
|
) |
|
|
|
|
|
hidden_states = self.ff(self.norm3(hidden_states)) + hidden_states |
|
|
|
|
|
if self.unet_use_temporal_attention: |
|
d = hidden_states.shape[1] |
|
hidden_states = rearrange(hidden_states, "(b f) d c -> (b d) f c", f=video_length) |
|
norm_hidden_states = ( |
|
self.norm_temp(hidden_states, timestep) if self.use_ada_layer_norm else self.norm_temp(hidden_states) |
|
) |
|
hidden_states = self.attn_temp(norm_hidden_states) + hidden_states |
|
hidden_states = rearrange(hidden_states, "(b d) f c -> (b f) d c", d=d) |
|
|
|
return hidden_states |
|
|
|
|
|
class InflatedConv3d(nn.Conv2d): |
|
def forward(self, x): |
|
video_length = x.shape[2] |
|
|
|
x = rearrange(x, "b c f h w -> (b f) c h w") |
|
x = super().forward(x) |
|
x = rearrange(x, "(b f) c h w -> b c f h w", f=video_length) |
|
|
|
return x |
|
|
|
|
|
class InflatedGroupNorm(nn.GroupNorm): |
|
def forward(self, x): |
|
video_length = x.shape[2] |
|
|
|
x = rearrange(x, "b c f h w -> (b f) c h w") |
|
x = super().forward(x) |
|
x = rearrange(x, "(b f) c h w -> b c f h w", f=video_length) |
|
|
|
return x |
|
|
|
|
|
class Upsample3D(nn.Module): |
|
def __init__(self, channels, use_conv=False, use_conv_transpose=False, out_channels=None, name="conv"): |
|
super().__init__() |
|
self.channels = channels |
|
self.out_channels = out_channels or channels |
|
self.use_conv = use_conv |
|
self.use_conv_transpose = use_conv_transpose |
|
self.name = name |
|
|
|
conv = None |
|
if use_conv_transpose: |
|
raise NotImplementedError |
|
elif use_conv: |
|
self.conv = InflatedConv3d(self.channels, self.out_channels, 3, padding=1) |
|
|
|
def forward(self, hidden_states, output_size=None): |
|
assert hidden_states.shape[1] == self.channels |
|
|
|
if self.use_conv_transpose: |
|
raise NotImplementedError |
|
|
|
|
|
dtype = hidden_states.dtype |
|
if dtype == torch.bfloat16: |
|
hidden_states = hidden_states.to(torch.float32) |
|
|
|
|
|
if hidden_states.shape[0] >= 64: |
|
hidden_states = hidden_states.contiguous() |
|
|
|
|
|
|
|
if output_size is None: |
|
hidden_states = F.interpolate(hidden_states, scale_factor=[1.0, 2.0, 2.0], mode="nearest") |
|
else: |
|
hidden_states = F.interpolate(hidden_states, size=output_size, mode="nearest") |
|
|
|
|
|
if dtype == torch.bfloat16: |
|
hidden_states = hidden_states.to(dtype) |
|
|
|
|
|
|
|
|
|
|
|
|
|
hidden_states = self.conv(hidden_states) |
|
|
|
return hidden_states |
|
|
|
|
|
class Downsample3D(nn.Module): |
|
def __init__(self, channels, use_conv=False, out_channels=None, padding=1, name="conv"): |
|
super().__init__() |
|
self.channels = channels |
|
self.out_channels = out_channels or channels |
|
self.use_conv = use_conv |
|
self.padding = padding |
|
stride = 2 |
|
self.name = name |
|
|
|
if use_conv: |
|
self.conv = InflatedConv3d(self.channels, self.out_channels, 3, stride=stride, padding=padding) |
|
else: |
|
raise NotImplementedError |
|
|
|
def forward(self, hidden_states): |
|
assert hidden_states.shape[1] == self.channels |
|
if self.use_conv and self.padding == 0: |
|
raise NotImplementedError |
|
|
|
assert hidden_states.shape[1] == self.channels |
|
hidden_states = self.conv(hidden_states) |
|
|
|
return hidden_states |
|
|
|
|
|
class ResnetBlock3D(nn.Module): |
|
def __init__( |
|
self, |
|
*, |
|
in_channels, |
|
out_channels=None, |
|
conv_shortcut=False, |
|
dropout=0.0, |
|
temb_channels=512, |
|
groups=32, |
|
groups_out=None, |
|
pre_norm=True, |
|
eps=1e-6, |
|
non_linearity="swish", |
|
time_embedding_norm="default", |
|
output_scale_factor=1.0, |
|
use_in_shortcut=None, |
|
use_inflated_groupnorm=False, |
|
): |
|
super().__init__() |
|
self.pre_norm = pre_norm |
|
self.pre_norm = True |
|
self.in_channels = in_channels |
|
out_channels = in_channels if out_channels is None else out_channels |
|
self.out_channels = out_channels |
|
self.use_conv_shortcut = conv_shortcut |
|
self.time_embedding_norm = time_embedding_norm |
|
self.output_scale_factor = output_scale_factor |
|
|
|
if groups_out is None: |
|
groups_out = groups |
|
|
|
assert use_inflated_groupnorm != None |
|
if use_inflated_groupnorm: |
|
self.norm1 = InflatedGroupNorm(num_groups=groups, num_channels=in_channels, eps=eps, affine=True) |
|
else: |
|
self.norm1 = torch.nn.GroupNorm(num_groups=groups, num_channels=in_channels, eps=eps, affine=True) |
|
|
|
self.conv1 = InflatedConv3d(in_channels, out_channels, kernel_size=3, stride=1, padding=1) |
|
|
|
if temb_channels is not None: |
|
if self.time_embedding_norm == "default": |
|
time_emb_proj_out_channels = out_channels |
|
elif self.time_embedding_norm == "scale_shift": |
|
time_emb_proj_out_channels = out_channels * 2 |
|
else: |
|
raise ValueError(f"unknown time_embedding_norm : {self.time_embedding_norm} ") |
|
|
|
self.time_emb_proj = torch.nn.Linear(temb_channels, time_emb_proj_out_channels) |
|
else: |
|
self.time_emb_proj = None |
|
|
|
if use_inflated_groupnorm: |
|
self.norm2 = InflatedGroupNorm(num_groups=groups_out, num_channels=out_channels, eps=eps, affine=True) |
|
else: |
|
self.norm2 = torch.nn.GroupNorm(num_groups=groups_out, num_channels=out_channels, eps=eps, affine=True) |
|
|
|
self.dropout = torch.nn.Dropout(dropout) |
|
self.conv2 = InflatedConv3d(out_channels, out_channels, kernel_size=3, stride=1, padding=1) |
|
|
|
if non_linearity == "swish": |
|
self.nonlinearity = lambda x: F.silu(x) |
|
elif non_linearity == "mish": |
|
self.nonlinearity = Mish() |
|
elif non_linearity == "silu": |
|
self.nonlinearity = nn.SiLU() |
|
|
|
self.use_in_shortcut = self.in_channels != self.out_channels if use_in_shortcut is None else use_in_shortcut |
|
|
|
self.conv_shortcut = None |
|
if self.use_in_shortcut: |
|
self.conv_shortcut = InflatedConv3d(in_channels, out_channels, kernel_size=1, stride=1, padding=0) |
|
|
|
def forward(self, input_tensor, temb): |
|
hidden_states = input_tensor |
|
|
|
hidden_states = self.norm1(hidden_states) |
|
hidden_states = self.nonlinearity(hidden_states) |
|
|
|
hidden_states = self.conv1(hidden_states) |
|
|
|
if temb is not None: |
|
temb = self.time_emb_proj(self.nonlinearity(temb))[:, :, None, None, None] |
|
|
|
if temb is not None and self.time_embedding_norm == "default": |
|
hidden_states = hidden_states + temb |
|
|
|
hidden_states = self.norm2(hidden_states) |
|
|
|
if temb is not None and self.time_embedding_norm == "scale_shift": |
|
scale, shift = torch.chunk(temb, 2, dim=1) |
|
hidden_states = hidden_states * (1 + scale) + shift |
|
|
|
hidden_states = self.nonlinearity(hidden_states) |
|
|
|
hidden_states = self.dropout(hidden_states) |
|
hidden_states = self.conv2(hidden_states) |
|
|
|
if self.conv_shortcut is not None: |
|
input_tensor = self.conv_shortcut(input_tensor) |
|
|
|
output_tensor = (input_tensor + hidden_states) / self.output_scale_factor |
|
|
|
return output_tensor |
|
|
|
|
|
class Mish(torch.nn.Module): |
|
def forward(self, hidden_states): |
|
return hidden_states * torch.tanh(torch.nn.functional.softplus(hidden_states)) |
|
|
|
|
|
|
|
def zero_module(module): |
|
|
|
for p in module.parameters(): |
|
p.detach().zero_() |
|
return module |
|
|
|
|
|
@dataclass |
|
class TemporalTransformer3DModelOutput(BaseOutput): |
|
sample: torch.FloatTensor |
|
|
|
|
|
def get_motion_module( |
|
in_channels, |
|
motion_module_type: str, |
|
motion_module_kwargs: dict |
|
): |
|
if motion_module_type == "Vanilla": |
|
return VanillaTemporalModule(in_channels=in_channels, **motion_module_kwargs,) |
|
else: |
|
raise ValueError |
|
|
|
|
|
class VanillaTemporalModule(nn.Module): |
|
def __init__( |
|
self, |
|
in_channels, |
|
num_attention_heads = 8, |
|
num_transformer_block = 2, |
|
attention_block_types =( "Temporal_Self", "Temporal_Self" ), |
|
cross_frame_attention_mode = None, |
|
temporal_position_encoding = False, |
|
temporal_position_encoding_max_len = 24, |
|
temporal_attention_dim_div = 1, |
|
zero_initialize = True, |
|
): |
|
super().__init__() |
|
|
|
self.temporal_transformer = TemporalTransformer3DModel( |
|
in_channels=in_channels, |
|
num_attention_heads=num_attention_heads, |
|
attention_head_dim=in_channels // num_attention_heads // temporal_attention_dim_div, |
|
num_layers=num_transformer_block, |
|
attention_block_types=attention_block_types, |
|
cross_frame_attention_mode=cross_frame_attention_mode, |
|
temporal_position_encoding=temporal_position_encoding, |
|
temporal_position_encoding_max_len=temporal_position_encoding_max_len, |
|
) |
|
|
|
if zero_initialize: |
|
self.temporal_transformer.proj_out = zero_module(self.temporal_transformer.proj_out) |
|
|
|
def forward(self, input_tensor, temb, encoder_hidden_states, attention_mask=None, anchor_frame_idx=None): |
|
hidden_states = input_tensor |
|
hidden_states = self.temporal_transformer(hidden_states, encoder_hidden_states, attention_mask) |
|
|
|
output = hidden_states |
|
return output |
|
|
|
class TemporalTransformer3DModel(nn.Module): |
|
def __init__( |
|
self, |
|
in_channels, |
|
num_attention_heads, |
|
attention_head_dim, |
|
|
|
num_layers, |
|
attention_block_types = ( "Temporal_Self", "Temporal_Self", ), |
|
dropout = 0.0, |
|
norm_num_groups = 32, |
|
cross_attention_dim = 768, |
|
activation_fn = "geglu", |
|
attention_bias = False, |
|
upcast_attention = False, |
|
|
|
cross_frame_attention_mode = None, |
|
temporal_position_encoding = False, |
|
temporal_position_encoding_max_len = 24, |
|
): |
|
super().__init__() |
|
|
|
inner_dim = num_attention_heads * attention_head_dim |
|
|
|
self.norm = torch.nn.GroupNorm(num_groups=norm_num_groups, num_channels=in_channels, eps=1e-6, affine=True) |
|
self.proj_in = nn.Linear(in_channels, inner_dim) |
|
|
|
self.transformer_blocks = nn.ModuleList( |
|
[ |
|
TemporalTransformerBlock( |
|
dim=inner_dim, |
|
num_attention_heads=num_attention_heads, |
|
attention_head_dim=attention_head_dim, |
|
attention_block_types=attention_block_types, |
|
dropout=dropout, |
|
norm_num_groups=norm_num_groups, |
|
cross_attention_dim=cross_attention_dim, |
|
activation_fn=activation_fn, |
|
attention_bias=attention_bias, |
|
upcast_attention=upcast_attention, |
|
cross_frame_attention_mode=cross_frame_attention_mode, |
|
temporal_position_encoding=temporal_position_encoding, |
|
temporal_position_encoding_max_len=temporal_position_encoding_max_len, |
|
) |
|
for d in range(num_layers) |
|
] |
|
) |
|
self.proj_out = nn.Linear(inner_dim, in_channels) |
|
|
|
def forward(self, hidden_states, encoder_hidden_states=None, attention_mask=None): |
|
assert hidden_states.dim() == 5, f"Expected hidden_states to have ndim=5, but got ndim={hidden_states.dim()}." |
|
video_length = hidden_states.shape[2] |
|
hidden_states = rearrange(hidden_states, "b c f h w -> (b f) c h w") |
|
|
|
batch, channel, height, weight = hidden_states.shape |
|
residual = hidden_states |
|
|
|
hidden_states = self.norm(hidden_states) |
|
inner_dim = hidden_states.shape[1] |
|
hidden_states = hidden_states.permute(0, 2, 3, 1).reshape(batch, height * weight, inner_dim) |
|
hidden_states = self.proj_in(hidden_states) |
|
|
|
|
|
for block in self.transformer_blocks: |
|
hidden_states = block(hidden_states, encoder_hidden_states=encoder_hidden_states, video_length=video_length) |
|
|
|
|
|
hidden_states = self.proj_out(hidden_states) |
|
hidden_states = hidden_states.reshape(batch, height, weight, inner_dim).permute(0, 3, 1, 2).contiguous() |
|
|
|
output = hidden_states + residual |
|
output = rearrange(output, "(b f) c h w -> b c f h w", f=video_length) |
|
|
|
return output |
|
|
|
|
|
class TemporalTransformerBlock(nn.Module): |
|
def __init__( |
|
self, |
|
dim, |
|
num_attention_heads, |
|
attention_head_dim, |
|
attention_block_types = ( "Temporal_Self", "Temporal_Self", ), |
|
dropout = 0.0, |
|
norm_num_groups = 32, |
|
cross_attention_dim = 768, |
|
activation_fn = "geglu", |
|
attention_bias = False, |
|
upcast_attention = False, |
|
cross_frame_attention_mode = None, |
|
temporal_position_encoding = False, |
|
temporal_position_encoding_max_len = 24, |
|
): |
|
super().__init__() |
|
|
|
attention_blocks = [] |
|
norms = [] |
|
|
|
for block_name in attention_block_types: |
|
attention_blocks.append( |
|
VersatileAttention( |
|
attention_mode=block_name.split("_")[0], |
|
cross_attention_dim=cross_attention_dim if block_name.endswith("_Cross") else None, |
|
|
|
query_dim=dim, |
|
heads=num_attention_heads, |
|
dim_head=attention_head_dim, |
|
dropout=dropout, |
|
bias=attention_bias, |
|
upcast_attention=upcast_attention, |
|
|
|
cross_frame_attention_mode=cross_frame_attention_mode, |
|
temporal_position_encoding=temporal_position_encoding, |
|
temporal_position_encoding_max_len=temporal_position_encoding_max_len, |
|
) |
|
) |
|
norms.append(nn.LayerNorm(dim)) |
|
|
|
self.attention_blocks = nn.ModuleList(attention_blocks) |
|
self.norms = nn.ModuleList(norms) |
|
|
|
self.ff = FeedForward(dim, dropout=dropout, activation_fn=activation_fn) |
|
self.ff_norm = nn.LayerNorm(dim) |
|
|
|
|
|
def forward(self, hidden_states, encoder_hidden_states=None, attention_mask=None, video_length=None): |
|
for attention_block, norm in zip(self.attention_blocks, self.norms): |
|
norm_hidden_states = norm(hidden_states) |
|
hidden_states = attention_block( |
|
norm_hidden_states, |
|
encoder_hidden_states=encoder_hidden_states if attention_block.is_cross_attention else None, |
|
video_length=video_length, |
|
) + hidden_states |
|
|
|
hidden_states = self.ff(self.ff_norm(hidden_states)) + hidden_states |
|
|
|
output = hidden_states |
|
return output |
|
|
|
|
|
class PositionalEncoding(nn.Module): |
|
def __init__( |
|
self, |
|
d_model, |
|
dropout = 0., |
|
max_len = 24 |
|
): |
|
super().__init__() |
|
self.dropout = nn.Dropout(p=dropout) |
|
position = torch.arange(max_len).unsqueeze(1) |
|
div_term = torch.exp(torch.arange(0, d_model, 2) * (-math.log(10000.0) / d_model)) |
|
pe = torch.zeros(1, max_len, d_model) |
|
pe[0, :, 0::2] = torch.sin(position * div_term) |
|
pe[0, :, 1::2] = torch.cos(position * div_term) |
|
self.register_buffer('pe', pe) |
|
|
|
def forward(self, x): |
|
x = x + self.pe[:, :x.size(1)] |
|
return self.dropout(x) |
|
|
|
|
|
class VersatileAttention(CrossAttention): |
|
def __init__( |
|
self, |
|
attention_mode = None, |
|
cross_frame_attention_mode = None, |
|
temporal_position_encoding = False, |
|
temporal_position_encoding_max_len = 24, |
|
*args, **kwargs |
|
): |
|
super().__init__(*args, **kwargs) |
|
assert attention_mode == "Temporal" |
|
|
|
self.attention_mode = attention_mode |
|
self.is_cross_attention = kwargs["cross_attention_dim"] is not None |
|
|
|
self.pos_encoder = PositionalEncoding( |
|
kwargs["query_dim"], |
|
dropout=0., |
|
max_len=temporal_position_encoding_max_len |
|
) if (temporal_position_encoding and attention_mode == "Temporal") else None |
|
|
|
def extra_repr(self): |
|
return f"(Module Info) Attention_Mode: {self.attention_mode}, Is_Cross_Attention: {self.is_cross_attention}" |
|
|
|
def forward(self, hidden_states, encoder_hidden_states=None, attention_mask=None, video_length=None): |
|
batch_size, sequence_length, _ = hidden_states.shape |
|
|
|
if self.attention_mode == "Temporal": |
|
d = hidden_states.shape[1] |
|
hidden_states = rearrange(hidden_states, "(b f) d c -> (b d) f c", f=video_length) |
|
|
|
if self.pos_encoder is not None: |
|
hidden_states = self.pos_encoder(hidden_states) |
|
|
|
encoder_hidden_states = repeat(encoder_hidden_states, "b n c -> (b d) n c", d=d) if encoder_hidden_states is not None else encoder_hidden_states |
|
else: |
|
raise NotImplementedError |
|
|
|
encoder_hidden_states = encoder_hidden_states |
|
|
|
if self.group_norm is not None: |
|
hidden_states = self.group_norm(hidden_states.transpose(1, 2)).transpose(1, 2) |
|
|
|
query = self.to_q(hidden_states) |
|
dim = query.shape[-1] |
|
query = self.reshape_heads_to_batch_dim(query) |
|
|
|
if self.added_kv_proj_dim is not None: |
|
raise NotImplementedError |
|
|
|
encoder_hidden_states = encoder_hidden_states if encoder_hidden_states is not None else hidden_states |
|
key = self.to_k(encoder_hidden_states) |
|
value = self.to_v(encoder_hidden_states) |
|
|
|
key = self.reshape_heads_to_batch_dim(key) |
|
value = self.reshape_heads_to_batch_dim(value) |
|
|
|
if attention_mask is not None: |
|
if attention_mask.shape[-1] != query.shape[1]: |
|
target_length = query.shape[1] |
|
attention_mask = F.pad(attention_mask, (0, target_length), value=0.0) |
|
attention_mask = attention_mask.repeat_interleave(self.heads, dim=0) |
|
|
|
|
|
if self._use_memory_efficient_attention_xformers: |
|
hidden_states = self._memory_efficient_attention_xformers(query, key, value, attention_mask) |
|
|
|
hidden_states = hidden_states.to(query.dtype) |
|
else: |
|
if self._slice_size is None or query.shape[0] // self._slice_size == 1: |
|
hidden_states = self._attention(query, key, value, attention_mask) |
|
else: |
|
hidden_states = self._sliced_attention(query, key, value, sequence_length, dim, attention_mask) |
|
|
|
|
|
hidden_states = self.to_out[0](hidden_states) |
|
|
|
|
|
hidden_states = self.to_out[1](hidden_states) |
|
|
|
if self.attention_mode == "Temporal": |
|
hidden_states = rearrange(hidden_states, "(b d) f c -> (b f) d c", d=d) |
|
|
|
return hidden_states |
|
|
|
|
|
|
|
def get_down_block( |
|
down_block_type, |
|
num_layers, |
|
in_channels, |
|
out_channels, |
|
temb_channels, |
|
add_downsample, |
|
resnet_eps, |
|
resnet_act_fn, |
|
attn_num_head_channels, |
|
resnet_groups=None, |
|
cross_attention_dim=None, |
|
downsample_padding=None, |
|
dual_cross_attention=False, |
|
use_linear_projection=False, |
|
only_cross_attention=False, |
|
upcast_attention=False, |
|
resnet_time_scale_shift="default", |
|
|
|
unet_use_cross_frame_attention=False, |
|
unet_use_temporal_attention=False, |
|
use_inflated_groupnorm=False, |
|
|
|
use_motion_module=None, |
|
|
|
motion_module_type=None, |
|
motion_module_kwargs=None, |
|
): |
|
down_block_type = down_block_type[7:] if down_block_type.startswith("UNetRes") else down_block_type |
|
if down_block_type == "DownBlock3D": |
|
return DownBlock3D( |
|
num_layers=num_layers, |
|
in_channels=in_channels, |
|
out_channels=out_channels, |
|
temb_channels=temb_channels, |
|
add_downsample=add_downsample, |
|
resnet_eps=resnet_eps, |
|
resnet_act_fn=resnet_act_fn, |
|
resnet_groups=resnet_groups, |
|
downsample_padding=downsample_padding, |
|
resnet_time_scale_shift=resnet_time_scale_shift, |
|
|
|
use_inflated_groupnorm=use_inflated_groupnorm, |
|
|
|
use_motion_module=use_motion_module, |
|
motion_module_type=motion_module_type, |
|
motion_module_kwargs=motion_module_kwargs, |
|
) |
|
elif down_block_type == "CrossAttnDownBlock3D": |
|
if cross_attention_dim is None: |
|
raise ValueError("cross_attention_dim must be specified for CrossAttnDownBlock3D") |
|
return CrossAttnDownBlock3D( |
|
num_layers=num_layers, |
|
in_channels=in_channels, |
|
out_channels=out_channels, |
|
temb_channels=temb_channels, |
|
add_downsample=add_downsample, |
|
resnet_eps=resnet_eps, |
|
resnet_act_fn=resnet_act_fn, |
|
resnet_groups=resnet_groups, |
|
downsample_padding=downsample_padding, |
|
cross_attention_dim=cross_attention_dim, |
|
attn_num_head_channels=attn_num_head_channels, |
|
dual_cross_attention=dual_cross_attention, |
|
use_linear_projection=use_linear_projection, |
|
only_cross_attention=only_cross_attention, |
|
upcast_attention=upcast_attention, |
|
resnet_time_scale_shift=resnet_time_scale_shift, |
|
|
|
unet_use_cross_frame_attention=unet_use_cross_frame_attention, |
|
unet_use_temporal_attention=unet_use_temporal_attention, |
|
use_inflated_groupnorm=use_inflated_groupnorm, |
|
|
|
use_motion_module=use_motion_module, |
|
motion_module_type=motion_module_type, |
|
motion_module_kwargs=motion_module_kwargs, |
|
) |
|
raise ValueError(f"{down_block_type} does not exist.") |
|
|
|
|
|
def get_up_block( |
|
up_block_type, |
|
num_layers, |
|
in_channels, |
|
out_channels, |
|
prev_output_channel, |
|
temb_channels, |
|
add_upsample, |
|
resnet_eps, |
|
resnet_act_fn, |
|
attn_num_head_channels, |
|
resnet_groups=None, |
|
cross_attention_dim=None, |
|
dual_cross_attention=False, |
|
use_linear_projection=False, |
|
only_cross_attention=False, |
|
upcast_attention=False, |
|
resnet_time_scale_shift="default", |
|
|
|
unet_use_cross_frame_attention=False, |
|
unet_use_temporal_attention=False, |
|
use_inflated_groupnorm=False, |
|
|
|
use_motion_module=None, |
|
motion_module_type=None, |
|
motion_module_kwargs=None, |
|
): |
|
up_block_type = up_block_type[7:] if up_block_type.startswith("UNetRes") else up_block_type |
|
if up_block_type == "UpBlock3D": |
|
return UpBlock3D( |
|
num_layers=num_layers, |
|
in_channels=in_channels, |
|
out_channels=out_channels, |
|
prev_output_channel=prev_output_channel, |
|
temb_channels=temb_channels, |
|
add_upsample=add_upsample, |
|
resnet_eps=resnet_eps, |
|
resnet_act_fn=resnet_act_fn, |
|
resnet_groups=resnet_groups, |
|
resnet_time_scale_shift=resnet_time_scale_shift, |
|
|
|
use_inflated_groupnorm=use_inflated_groupnorm, |
|
|
|
use_motion_module=use_motion_module, |
|
motion_module_type=motion_module_type, |
|
motion_module_kwargs=motion_module_kwargs, |
|
) |
|
elif up_block_type == "CrossAttnUpBlock3D": |
|
if cross_attention_dim is None: |
|
raise ValueError("cross_attention_dim must be specified for CrossAttnUpBlock3D") |
|
return CrossAttnUpBlock3D( |
|
num_layers=num_layers, |
|
in_channels=in_channels, |
|
out_channels=out_channels, |
|
prev_output_channel=prev_output_channel, |
|
temb_channels=temb_channels, |
|
add_upsample=add_upsample, |
|
resnet_eps=resnet_eps, |
|
resnet_act_fn=resnet_act_fn, |
|
resnet_groups=resnet_groups, |
|
cross_attention_dim=cross_attention_dim, |
|
attn_num_head_channels=attn_num_head_channels, |
|
dual_cross_attention=dual_cross_attention, |
|
use_linear_projection=use_linear_projection, |
|
only_cross_attention=only_cross_attention, |
|
upcast_attention=upcast_attention, |
|
resnet_time_scale_shift=resnet_time_scale_shift, |
|
|
|
unet_use_cross_frame_attention=unet_use_cross_frame_attention, |
|
unet_use_temporal_attention=unet_use_temporal_attention, |
|
use_inflated_groupnorm=use_inflated_groupnorm, |
|
|
|
use_motion_module=use_motion_module, |
|
motion_module_type=motion_module_type, |
|
motion_module_kwargs=motion_module_kwargs, |
|
) |
|
raise ValueError(f"{up_block_type} does not exist.") |
|
|
|
|
|
class UNetMidBlock3DCrossAttn(nn.Module): |
|
def __init__( |
|
self, |
|
in_channels: int, |
|
temb_channels: int, |
|
dropout: float = 0.0, |
|
num_layers: int = 1, |
|
resnet_eps: float = 1e-6, |
|
resnet_time_scale_shift: str = "default", |
|
resnet_act_fn: str = "swish", |
|
resnet_groups: int = 32, |
|
resnet_pre_norm: bool = True, |
|
attn_num_head_channels=1, |
|
output_scale_factor=1.0, |
|
cross_attention_dim=1280, |
|
dual_cross_attention=False, |
|
use_linear_projection=False, |
|
upcast_attention=False, |
|
|
|
unet_use_cross_frame_attention=False, |
|
unet_use_temporal_attention=False, |
|
use_inflated_groupnorm=False, |
|
|
|
use_motion_module=None, |
|
|
|
motion_module_type=None, |
|
motion_module_kwargs=None, |
|
): |
|
super().__init__() |
|
|
|
self.has_cross_attention = True |
|
self.attn_num_head_channels = attn_num_head_channels |
|
resnet_groups = resnet_groups if resnet_groups is not None else min(in_channels // 4, 32) |
|
|
|
|
|
resnets = [ |
|
ResnetBlock3D( |
|
in_channels=in_channels, |
|
out_channels=in_channels, |
|
temb_channels=temb_channels, |
|
eps=resnet_eps, |
|
groups=resnet_groups, |
|
dropout=dropout, |
|
time_embedding_norm=resnet_time_scale_shift, |
|
non_linearity=resnet_act_fn, |
|
output_scale_factor=output_scale_factor, |
|
pre_norm=resnet_pre_norm, |
|
|
|
use_inflated_groupnorm=use_inflated_groupnorm, |
|
) |
|
] |
|
attentions = [] |
|
motion_modules = [] |
|
|
|
for _ in range(num_layers): |
|
if dual_cross_attention: |
|
raise NotImplementedError |
|
attentions.append( |
|
Transformer3DModel( |
|
attn_num_head_channels, |
|
in_channels // attn_num_head_channels, |
|
in_channels=in_channels, |
|
num_layers=1, |
|
cross_attention_dim=cross_attention_dim, |
|
norm_num_groups=resnet_groups, |
|
use_linear_projection=use_linear_projection, |
|
upcast_attention=upcast_attention, |
|
|
|
unet_use_cross_frame_attention=unet_use_cross_frame_attention, |
|
unet_use_temporal_attention=unet_use_temporal_attention, |
|
) |
|
) |
|
motion_modules.append( |
|
get_motion_module( |
|
in_channels=in_channels, |
|
motion_module_type=motion_module_type, |
|
motion_module_kwargs=motion_module_kwargs, |
|
) if use_motion_module else None |
|
) |
|
resnets.append( |
|
ResnetBlock3D( |
|
in_channels=in_channels, |
|
out_channels=in_channels, |
|
temb_channels=temb_channels, |
|
eps=resnet_eps, |
|
groups=resnet_groups, |
|
dropout=dropout, |
|
time_embedding_norm=resnet_time_scale_shift, |
|
non_linearity=resnet_act_fn, |
|
output_scale_factor=output_scale_factor, |
|
pre_norm=resnet_pre_norm, |
|
|
|
use_inflated_groupnorm=use_inflated_groupnorm, |
|
) |
|
) |
|
|
|
self.attentions = nn.ModuleList(attentions) |
|
self.resnets = nn.ModuleList(resnets) |
|
self.motion_modules = nn.ModuleList(motion_modules) |
|
|
|
def forward(self, hidden_states, temb=None, encoder_hidden_states=None, attention_mask=None): |
|
hidden_states = self.resnets[0](hidden_states, temb) |
|
for attn, resnet, motion_module in zip(self.attentions, self.resnets[1:], self.motion_modules): |
|
hidden_states = attn(hidden_states, encoder_hidden_states=encoder_hidden_states).sample |
|
hidden_states = motion_module(hidden_states, temb, encoder_hidden_states=encoder_hidden_states) if motion_module is not None else hidden_states |
|
hidden_states = resnet(hidden_states, temb) |
|
|
|
return hidden_states |
|
|
|
|
|
class CrossAttnDownBlock3D(nn.Module): |
|
def __init__( |
|
self, |
|
in_channels: int, |
|
out_channels: int, |
|
temb_channels: int, |
|
dropout: float = 0.0, |
|
num_layers: int = 1, |
|
resnet_eps: float = 1e-6, |
|
resnet_time_scale_shift: str = "default", |
|
resnet_act_fn: str = "swish", |
|
resnet_groups: int = 32, |
|
resnet_pre_norm: bool = True, |
|
attn_num_head_channels=1, |
|
cross_attention_dim=1280, |
|
output_scale_factor=1.0, |
|
downsample_padding=1, |
|
add_downsample=True, |
|
dual_cross_attention=False, |
|
use_linear_projection=False, |
|
only_cross_attention=False, |
|
upcast_attention=False, |
|
|
|
unet_use_cross_frame_attention=False, |
|
unet_use_temporal_attention=False, |
|
use_inflated_groupnorm=False, |
|
|
|
use_motion_module=None, |
|
|
|
motion_module_type=None, |
|
motion_module_kwargs=None, |
|
): |
|
super().__init__() |
|
resnets = [] |
|
attentions = [] |
|
motion_modules = [] |
|
|
|
self.has_cross_attention = True |
|
self.attn_num_head_channels = attn_num_head_channels |
|
|
|
for i in range(num_layers): |
|
in_channels = in_channels if i == 0 else out_channels |
|
resnets.append( |
|
ResnetBlock3D( |
|
in_channels=in_channels, |
|
out_channels=out_channels, |
|
temb_channels=temb_channels, |
|
eps=resnet_eps, |
|
groups=resnet_groups, |
|
dropout=dropout, |
|
time_embedding_norm=resnet_time_scale_shift, |
|
non_linearity=resnet_act_fn, |
|
output_scale_factor=output_scale_factor, |
|
pre_norm=resnet_pre_norm, |
|
|
|
use_inflated_groupnorm=use_inflated_groupnorm, |
|
) |
|
) |
|
if dual_cross_attention: |
|
raise NotImplementedError |
|
attentions.append( |
|
Transformer3DModel( |
|
attn_num_head_channels, |
|
out_channels // attn_num_head_channels, |
|
in_channels=out_channels, |
|
num_layers=1, |
|
cross_attention_dim=cross_attention_dim, |
|
norm_num_groups=resnet_groups, |
|
use_linear_projection=use_linear_projection, |
|
only_cross_attention=only_cross_attention, |
|
upcast_attention=upcast_attention, |
|
|
|
unet_use_cross_frame_attention=unet_use_cross_frame_attention, |
|
unet_use_temporal_attention=unet_use_temporal_attention, |
|
) |
|
) |
|
motion_modules.append( |
|
get_motion_module( |
|
in_channels=out_channels, |
|
motion_module_type=motion_module_type, |
|
motion_module_kwargs=motion_module_kwargs, |
|
) if use_motion_module else None |
|
) |
|
|
|
self.attentions = nn.ModuleList(attentions) |
|
self.resnets = nn.ModuleList(resnets) |
|
self.motion_modules = nn.ModuleList(motion_modules) |
|
|
|
if add_downsample: |
|
self.downsamplers = nn.ModuleList( |
|
[ |
|
Downsample3D( |
|
out_channels, use_conv=True, out_channels=out_channels, padding=downsample_padding, name="op" |
|
) |
|
] |
|
) |
|
else: |
|
self.downsamplers = None |
|
|
|
self.gradient_checkpointing = False |
|
|
|
def forward(self, hidden_states, temb=None, encoder_hidden_states=None, attention_mask=None): |
|
output_states = () |
|
|
|
for resnet, attn, motion_module in zip(self.resnets, self.attentions, self.motion_modules): |
|
if self.training and self.gradient_checkpointing: |
|
|
|
def create_custom_forward(module, return_dict=None): |
|
def custom_forward(*inputs): |
|
if return_dict is not None: |
|
return module(*inputs, return_dict=return_dict) |
|
else: |
|
return module(*inputs) |
|
|
|
return custom_forward |
|
|
|
hidden_states = torch.utils.checkpoint.checkpoint(create_custom_forward(resnet), hidden_states, temb) |
|
hidden_states = torch.utils.checkpoint.checkpoint( |
|
create_custom_forward(attn, return_dict=False), |
|
hidden_states, |
|
encoder_hidden_states, |
|
)[0] |
|
if motion_module is not None: |
|
hidden_states = torch.utils.checkpoint.checkpoint(create_custom_forward(motion_module), hidden_states.requires_grad_(), temb, encoder_hidden_states) |
|
|
|
else: |
|
hidden_states = resnet(hidden_states, temb) |
|
hidden_states = attn(hidden_states, encoder_hidden_states=encoder_hidden_states).sample |
|
|
|
|
|
hidden_states = motion_module(hidden_states, temb, encoder_hidden_states=encoder_hidden_states) if motion_module is not None else hidden_states |
|
|
|
output_states += (hidden_states,) |
|
|
|
if self.downsamplers is not None: |
|
for downsampler in self.downsamplers: |
|
hidden_states = downsampler(hidden_states) |
|
|
|
output_states += (hidden_states,) |
|
|
|
return hidden_states, output_states |
|
|
|
|
|
class DownBlock3D(nn.Module): |
|
def __init__( |
|
self, |
|
in_channels: int, |
|
out_channels: int, |
|
temb_channels: int, |
|
dropout: float = 0.0, |
|
num_layers: int = 1, |
|
resnet_eps: float = 1e-6, |
|
resnet_time_scale_shift: str = "default", |
|
resnet_act_fn: str = "swish", |
|
resnet_groups: int = 32, |
|
resnet_pre_norm: bool = True, |
|
output_scale_factor=1.0, |
|
add_downsample=True, |
|
downsample_padding=1, |
|
|
|
use_inflated_groupnorm=False, |
|
|
|
use_motion_module=None, |
|
motion_module_type=None, |
|
motion_module_kwargs=None, |
|
): |
|
super().__init__() |
|
resnets = [] |
|
motion_modules = [] |
|
|
|
for i in range(num_layers): |
|
in_channels = in_channels if i == 0 else out_channels |
|
resnets.append( |
|
ResnetBlock3D( |
|
in_channels=in_channels, |
|
out_channels=out_channels, |
|
temb_channels=temb_channels, |
|
eps=resnet_eps, |
|
groups=resnet_groups, |
|
dropout=dropout, |
|
time_embedding_norm=resnet_time_scale_shift, |
|
non_linearity=resnet_act_fn, |
|
output_scale_factor=output_scale_factor, |
|
pre_norm=resnet_pre_norm, |
|
|
|
use_inflated_groupnorm=use_inflated_groupnorm, |
|
) |
|
) |
|
motion_modules.append( |
|
get_motion_module( |
|
in_channels=out_channels, |
|
motion_module_type=motion_module_type, |
|
motion_module_kwargs=motion_module_kwargs, |
|
) if use_motion_module else None |
|
) |
|
|
|
self.resnets = nn.ModuleList(resnets) |
|
self.motion_modules = nn.ModuleList(motion_modules) |
|
|
|
if add_downsample: |
|
self.downsamplers = nn.ModuleList( |
|
[ |
|
Downsample3D( |
|
out_channels, use_conv=True, out_channels=out_channels, padding=downsample_padding, name="op" |
|
) |
|
] |
|
) |
|
else: |
|
self.downsamplers = None |
|
|
|
self.gradient_checkpointing = False |
|
|
|
def forward(self, hidden_states, temb=None, encoder_hidden_states=None): |
|
output_states = () |
|
|
|
for resnet, motion_module in zip(self.resnets, self.motion_modules): |
|
if self.training and self.gradient_checkpointing: |
|
def create_custom_forward(module): |
|
def custom_forward(*inputs): |
|
return module(*inputs) |
|
|
|
return custom_forward |
|
|
|
hidden_states = torch.utils.checkpoint.checkpoint(create_custom_forward(resnet), hidden_states, temb) |
|
if motion_module is not None: |
|
hidden_states = torch.utils.checkpoint.checkpoint(create_custom_forward(motion_module), hidden_states.requires_grad_(), temb, encoder_hidden_states) |
|
else: |
|
hidden_states = resnet(hidden_states, temb) |
|
|
|
|
|
hidden_states = motion_module(hidden_states, temb, encoder_hidden_states=encoder_hidden_states) if motion_module is not None else hidden_states |
|
|
|
output_states += (hidden_states,) |
|
|
|
if self.downsamplers is not None: |
|
for downsampler in self.downsamplers: |
|
hidden_states = downsampler(hidden_states) |
|
|
|
output_states += (hidden_states,) |
|
|
|
return hidden_states, output_states |
|
|
|
|
|
class CrossAttnUpBlock3D(nn.Module): |
|
def __init__( |
|
self, |
|
in_channels: int, |
|
out_channels: int, |
|
prev_output_channel: int, |
|
temb_channels: int, |
|
dropout: float = 0.0, |
|
num_layers: int = 1, |
|
resnet_eps: float = 1e-6, |
|
resnet_time_scale_shift: str = "default", |
|
resnet_act_fn: str = "swish", |
|
resnet_groups: int = 32, |
|
resnet_pre_norm: bool = True, |
|
attn_num_head_channels=1, |
|
cross_attention_dim=1280, |
|
output_scale_factor=1.0, |
|
add_upsample=True, |
|
dual_cross_attention=False, |
|
use_linear_projection=False, |
|
only_cross_attention=False, |
|
upcast_attention=False, |
|
|
|
unet_use_cross_frame_attention=False, |
|
unet_use_temporal_attention=False, |
|
use_inflated_groupnorm=False, |
|
|
|
use_motion_module=None, |
|
|
|
motion_module_type=None, |
|
motion_module_kwargs=None, |
|
): |
|
super().__init__() |
|
resnets = [] |
|
attentions = [] |
|
motion_modules = [] |
|
|
|
self.has_cross_attention = True |
|
self.attn_num_head_channels = attn_num_head_channels |
|
|
|
for i in range(num_layers): |
|
res_skip_channels = in_channels if (i == num_layers - 1) else out_channels |
|
resnet_in_channels = prev_output_channel if i == 0 else out_channels |
|
|
|
resnets.append( |
|
ResnetBlock3D( |
|
in_channels=resnet_in_channels + res_skip_channels, |
|
out_channels=out_channels, |
|
temb_channels=temb_channels, |
|
eps=resnet_eps, |
|
groups=resnet_groups, |
|
dropout=dropout, |
|
time_embedding_norm=resnet_time_scale_shift, |
|
non_linearity=resnet_act_fn, |
|
output_scale_factor=output_scale_factor, |
|
pre_norm=resnet_pre_norm, |
|
|
|
use_inflated_groupnorm=use_inflated_groupnorm, |
|
) |
|
) |
|
if dual_cross_attention: |
|
raise NotImplementedError |
|
attentions.append( |
|
Transformer3DModel( |
|
attn_num_head_channels, |
|
out_channels // attn_num_head_channels, |
|
in_channels=out_channels, |
|
num_layers=1, |
|
cross_attention_dim=cross_attention_dim, |
|
norm_num_groups=resnet_groups, |
|
use_linear_projection=use_linear_projection, |
|
only_cross_attention=only_cross_attention, |
|
upcast_attention=upcast_attention, |
|
|
|
unet_use_cross_frame_attention=unet_use_cross_frame_attention, |
|
unet_use_temporal_attention=unet_use_temporal_attention, |
|
) |
|
) |
|
motion_modules.append( |
|
get_motion_module( |
|
in_channels=out_channels, |
|
motion_module_type=motion_module_type, |
|
motion_module_kwargs=motion_module_kwargs, |
|
) if use_motion_module else None |
|
) |
|
|
|
self.attentions = nn.ModuleList(attentions) |
|
self.resnets = nn.ModuleList(resnets) |
|
self.motion_modules = nn.ModuleList(motion_modules) |
|
|
|
if add_upsample: |
|
self.upsamplers = nn.ModuleList([Upsample3D(out_channels, use_conv=True, out_channels=out_channels)]) |
|
else: |
|
self.upsamplers = None |
|
|
|
self.gradient_checkpointing = False |
|
|
|
def forward( |
|
self, |
|
hidden_states, |
|
res_hidden_states_tuple, |
|
temb=None, |
|
encoder_hidden_states=None, |
|
upsample_size=None, |
|
attention_mask=None, |
|
): |
|
for resnet, attn, motion_module in zip(self.resnets, self.attentions, self.motion_modules): |
|
|
|
res_hidden_states = res_hidden_states_tuple[-1] |
|
res_hidden_states_tuple = res_hidden_states_tuple[:-1] |
|
hidden_states = torch.cat([hidden_states, res_hidden_states], dim=1) |
|
|
|
if self.training and self.gradient_checkpointing: |
|
|
|
def create_custom_forward(module, return_dict=None): |
|
def custom_forward(*inputs): |
|
if return_dict is not None: |
|
return module(*inputs, return_dict=return_dict) |
|
else: |
|
return module(*inputs) |
|
|
|
return custom_forward |
|
|
|
hidden_states = torch.utils.checkpoint.checkpoint(create_custom_forward(resnet), hidden_states, temb) |
|
hidden_states = torch.utils.checkpoint.checkpoint( |
|
create_custom_forward(attn, return_dict=False), |
|
hidden_states, |
|
encoder_hidden_states, |
|
)[0] |
|
if motion_module is not None: |
|
hidden_states = torch.utils.checkpoint.checkpoint(create_custom_forward(motion_module), hidden_states.requires_grad_(), temb, encoder_hidden_states) |
|
|
|
else: |
|
hidden_states = resnet(hidden_states, temb) |
|
hidden_states = attn(hidden_states, encoder_hidden_states=encoder_hidden_states).sample |
|
|
|
|
|
hidden_states = motion_module(hidden_states, temb, encoder_hidden_states=encoder_hidden_states) if motion_module is not None else hidden_states |
|
|
|
if self.upsamplers is not None: |
|
for upsampler in self.upsamplers: |
|
hidden_states = upsampler(hidden_states, upsample_size) |
|
|
|
return hidden_states |
|
|
|
|
|
class UpBlock3D(nn.Module): |
|
def __init__( |
|
self, |
|
in_channels: int, |
|
prev_output_channel: int, |
|
out_channels: int, |
|
temb_channels: int, |
|
dropout: float = 0.0, |
|
num_layers: int = 1, |
|
resnet_eps: float = 1e-6, |
|
resnet_time_scale_shift: str = "default", |
|
resnet_act_fn: str = "swish", |
|
resnet_groups: int = 32, |
|
resnet_pre_norm: bool = True, |
|
output_scale_factor=1.0, |
|
add_upsample=True, |
|
|
|
use_inflated_groupnorm=False, |
|
|
|
use_motion_module=None, |
|
motion_module_type=None, |
|
motion_module_kwargs=None, |
|
): |
|
super().__init__() |
|
resnets = [] |
|
motion_modules = [] |
|
|
|
for i in range(num_layers): |
|
res_skip_channels = in_channels if (i == num_layers - 1) else out_channels |
|
resnet_in_channels = prev_output_channel if i == 0 else out_channels |
|
|
|
resnets.append( |
|
ResnetBlock3D( |
|
in_channels=resnet_in_channels + res_skip_channels, |
|
out_channels=out_channels, |
|
temb_channels=temb_channels, |
|
eps=resnet_eps, |
|
groups=resnet_groups, |
|
dropout=dropout, |
|
time_embedding_norm=resnet_time_scale_shift, |
|
non_linearity=resnet_act_fn, |
|
output_scale_factor=output_scale_factor, |
|
pre_norm=resnet_pre_norm, |
|
|
|
use_inflated_groupnorm=use_inflated_groupnorm, |
|
) |
|
) |
|
motion_modules.append( |
|
get_motion_module( |
|
in_channels=out_channels, |
|
motion_module_type=motion_module_type, |
|
motion_module_kwargs=motion_module_kwargs, |
|
) if use_motion_module else None |
|
) |
|
|
|
self.resnets = nn.ModuleList(resnets) |
|
self.motion_modules = nn.ModuleList(motion_modules) |
|
|
|
if add_upsample: |
|
self.upsamplers = nn.ModuleList([Upsample3D(out_channels, use_conv=True, out_channels=out_channels)]) |
|
else: |
|
self.upsamplers = None |
|
|
|
self.gradient_checkpointing = False |
|
|
|
def forward(self, hidden_states, res_hidden_states_tuple, temb=None, upsample_size=None, encoder_hidden_states=None,): |
|
for resnet, motion_module in zip(self.resnets, self.motion_modules): |
|
|
|
res_hidden_states = res_hidden_states_tuple[-1] |
|
res_hidden_states_tuple = res_hidden_states_tuple[:-1] |
|
hidden_states = torch.cat([hidden_states, res_hidden_states], dim=1) |
|
|
|
if self.training and self.gradient_checkpointing: |
|
def create_custom_forward(module): |
|
def custom_forward(*inputs): |
|
return module(*inputs) |
|
|
|
return custom_forward |
|
|
|
hidden_states = torch.utils.checkpoint.checkpoint(create_custom_forward(resnet), hidden_states, temb) |
|
if motion_module is not None: |
|
hidden_states = torch.utils.checkpoint.checkpoint(create_custom_forward(motion_module), hidden_states.requires_grad_(), temb, encoder_hidden_states) |
|
else: |
|
hidden_states = resnet(hidden_states, temb) |
|
hidden_states = motion_module(hidden_states, temb, encoder_hidden_states=encoder_hidden_states) if motion_module is not None else hidden_states |
|
|
|
if self.upsamplers is not None: |
|
for upsampler in self.upsamplers: |
|
hidden_states = upsampler(hidden_states, upsample_size) |
|
|
|
return hidden_states |
|
|