|
from typing import Optional, Union, Tuple, Dict, Any |
|
from torch import nn |
|
from .ResnetBlock2D import ResnetBlock2D |
|
from diffusers.models.transformer_2d import Transformer2DModel |
|
from diffusers.models.unet_2d_blocks import DualTransformer2DModel |
|
from diffusers.utils import is_torch_version |
|
import torch |
|
|
|
class UNetMidBlock2DCrossAttn(nn.Module): |
|
def __init__( |
|
self, |
|
in_channels: int, |
|
temb_channels: int, |
|
dropout: float = 0.0, |
|
num_layers: int = 1, |
|
transformer_layers_per_block: Union[int, Tuple[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, |
|
num_attention_heads: int = 1, |
|
output_scale_factor: float = 1.0, |
|
cross_attention_dim: int = 1280, |
|
dual_cross_attention: bool = False, |
|
use_linear_projection: bool = False, |
|
upcast_attention: bool = False, |
|
attention_type: str = "default", |
|
normalization_type = None |
|
): |
|
super().__init__() |
|
|
|
self.has_cross_attention = True |
|
self.num_attention_heads = num_attention_heads |
|
resnet_groups = resnet_groups if resnet_groups is not None else min(in_channels // 4, 32) |
|
|
|
|
|
if isinstance(transformer_layers_per_block, int): |
|
transformer_layers_per_block = [transformer_layers_per_block] * num_layers |
|
|
|
|
|
resnets = [ |
|
ResnetBlock2D( |
|
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, |
|
normalization_type=normalization_type, |
|
is_crossAttn=False |
|
) |
|
] |
|
attentions = [] |
|
|
|
for i in range(num_layers): |
|
if not dual_cross_attention: |
|
attentions.append( |
|
Transformer2DModel( |
|
num_attention_heads, |
|
in_channels // num_attention_heads, |
|
in_channels=in_channels, |
|
num_layers=transformer_layers_per_block[i], |
|
cross_attention_dim=cross_attention_dim, |
|
norm_num_groups=resnet_groups, |
|
use_linear_projection=use_linear_projection, |
|
upcast_attention=upcast_attention, |
|
attention_type=attention_type, |
|
) |
|
) |
|
else: |
|
attentions.append( |
|
DualTransformer2DModel( |
|
num_attention_heads, |
|
in_channels // num_attention_heads, |
|
in_channels=in_channels, |
|
num_layers=1, |
|
cross_attention_dim=cross_attention_dim, |
|
norm_num_groups=resnet_groups, |
|
) |
|
) |
|
resnets.append( |
|
ResnetBlock2D( |
|
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, |
|
normalization_type=normalization_type, |
|
is_crossAttn=False, |
|
) |
|
) |
|
|
|
self.attentions = nn.ModuleList(attentions) |
|
self.resnets = nn.ModuleList(resnets) |
|
|
|
self.gradient_checkpointing = False |
|
|
|
def forward( |
|
self, |
|
hidden_states: torch.FloatTensor, |
|
temb: Optional[torch.FloatTensor] = None, |
|
encoder_hidden_states: Optional[torch.FloatTensor] = None, |
|
attention_mask: Optional[torch.FloatTensor] = None, |
|
cross_attention_kwargs: Optional[Dict[str, Any]] = None, |
|
encoder_attention_mask: Optional[torch.FloatTensor] = None, |
|
segmap = None |
|
) -> torch.FloatTensor: |
|
lora_scale = cross_attention_kwargs.get("scale", 1.0) if cross_attention_kwargs is not None else 1.0 |
|
hidden_states = self.resnets[0](hidden_states, temb, segmaps = segmap,scale=lora_scale) |
|
for attn, resnet in zip(self.attentions, self.resnets[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 |
|
|
|
ckpt_kwargs: Dict[str, Any] = {"use_reentrant": False} if is_torch_version(">=", "1.11.0") else {} |
|
hidden_states = attn( |
|
hidden_states, |
|
encoder_hidden_states=encoder_hidden_states, |
|
cross_attention_kwargs=cross_attention_kwargs, |
|
attention_mask=attention_mask, |
|
encoder_attention_mask=encoder_attention_mask, |
|
return_dict=False, |
|
)[0] |
|
hidden_states = torch.utils.checkpoint.checkpoint( |
|
create_custom_forward(resnet), |
|
hidden_states, |
|
temb, |
|
**ckpt_kwargs, |
|
) |
|
else: |
|
hidden_states = attn( |
|
hidden_states, |
|
encoder_hidden_states=encoder_hidden_states, |
|
cross_attention_kwargs=cross_attention_kwargs, |
|
attention_mask=attention_mask, |
|
encoder_attention_mask=encoder_attention_mask, |
|
return_dict=False, |
|
)[0] |
|
hidden_states = resnet(hidden_states, temb, segmaps = segmap, scale=lora_scale) |
|
|
|
return hidden_states |