Run this model with HF Jobs
Run xixircc/MetaRigCapture on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/xixircc/MetaRigCapture. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/xixircc/MetaRigCapture on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use xixircc/MetaRigCapture with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use xixircc/MetaRigCapture with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("xixircc/MetaRigCapture", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
Delete folder models with huggingface_hub
Browse filesFiles changed (2)
- models/unet_3d.pyDeleted-727
- models/unet_3d_blocks.pyDeleted-1121
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727# *************************************************************************# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates# SPDX-License-Identifier: Apache-2.0 ## This file has been modified by ByteDance Ltd. and/or its affiliates.## Original file was released under Aniportrait, with the full license text# available at https://github.com/Zejun-Yang/AniPortrait/blob/main/LICENSE.## This modified file is released under the same license.# *************************************************************************from collections import OrderedDictfrom dataclasses import dataclassimport pdbfrom os import PathLikefrom pathlib import Pathfrom typing import Dict, List, Optional, Tuple, Union
import torchimport torch.nn as nnimport torch.utils.checkpointimport torch.nn.functional as Ffrom diffusers.configuration_utils import ConfigMixin, register_to_configfrom diffusers.models.attention_processor import AttentionProcessorfrom diffusers.models.embeddings import TimestepEmbedding, Timestepsfrom diffusers.models.modeling_utils import ModelMixinfrom diffusers.utils import SAFETENSORS_WEIGHTS_NAME, WEIGHTS_NAME, BaseOutput, loggingfrom safetensors.torch import load_file
from .resnet import InflatedConv3d, InflatedGroupNormfrom .unet_3d_blocks import UNetMidBlock3DCrossAttn, get_down_block, get_up_block
logger = logging.get_logger(__name__) # pylint: disable=invalid-name
@dataclassclass UNet3DConditionOutput(BaseOutput): sample: torch.FloatTensor
class UNet3DConditionModel(ModelMixin, ConfigMixin): _supports_gradient_checkpointing = True
@register_to_config def __init__( self, sample_size: Optional[int] = None, in_channels: int = 4, out_channels: int = 4, center_input_sample: bool = False, flip_sin_to_cos: bool = True, freq_shift: int = 0, down_block_types: Tuple[str] = ( "CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "DownBlock3D", ), mid_block_type: str = "UNetMidBlock3DCrossAttn", up_block_types: Tuple[str] = ( "UpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D", ), only_cross_attention: Union[bool, Tuple[bool]] = False, block_out_channels: Tuple[int] = (320, 640, 1280, 1280), layers_per_block: int = 2, downsample_padding: int = 1, mid_block_scale_factor: float = 1, act_fn: str = "silu", norm_num_groups: int = 32, norm_eps: float = 1e-5, cross_attention_dim: int = 1280, attention_head_dim: Union[int, Tuple[int]] = 8, dual_cross_attention: bool = False, use_linear_projection: bool = False, class_embed_type: Optional[str] = None, num_class_embeds: Optional[int] = None, upcast_attention: bool = False, resnet_time_scale_shift: str = "default", use_inflated_groupnorm=False, # Additional use_motion_module=False, use_temporal_module=False, motion_module_resolutions=(1, 2, 4, 8), motion_module_mid_block=False, motion_module_decoder_only=False, motion_module_type=None, temporal_module_type=None, motion_module_kwargs={}, temporal_module_kwargs={}, unet_use_cross_frame_attention=None, unet_use_temporal_attention=None, ): super().__init__()
self.sample_size = sample_size time_embed_dim = block_out_channels[0] * 4
# input self.conv_in = InflatedConv3d( in_channels, block_out_channels[0], kernel_size=3, padding=(1, 1) )
# time self.time_proj = Timesteps(block_out_channels[0], flip_sin_to_cos, freq_shift) timestep_input_dim = block_out_channels[0]
self.time_embedding = TimestepEmbedding(timestep_input_dim, time_embed_dim)
# class embedding if class_embed_type is None and num_class_embeds is not None: self.class_embedding = nn.Embedding(num_class_embeds, time_embed_dim) elif class_embed_type == "timestep": self.class_embedding = TimestepEmbedding(timestep_input_dim, time_embed_dim) elif class_embed_type == "identity": self.class_embedding = nn.Identity(time_embed_dim, time_embed_dim) else: self.class_embedding = None
self.down_blocks = nn.ModuleList([]) self.mid_block = None self.up_blocks = nn.ModuleList([])
if isinstance(only_cross_attention, bool): only_cross_attention = [only_cross_attention] * len(down_block_types)
if isinstance(attention_head_dim, int): attention_head_dim = (attention_head_dim,) * len(down_block_types)
# down output_channel = block_out_channels[0] for i, down_block_type in enumerate(down_block_types): res = 2**i input_channel = output_channel output_channel = block_out_channels[i] is_final_block = i == len(block_out_channels) - 1
down_block = get_down_block( down_block_type, num_layers=layers_per_block, in_channels=input_channel, out_channels=output_channel, temb_channels=time_embed_dim, add_downsample=not is_final_block, resnet_eps=norm_eps, resnet_act_fn=act_fn, resnet_groups=norm_num_groups, cross_attention_dim=cross_attention_dim, attn_num_head_channels=attention_head_dim[i], downsample_padding=downsample_padding, dual_cross_attention=dual_cross_attention, use_linear_projection=use_linear_projection, only_cross_attention=only_cross_attention[i], 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 and (res in motion_module_resolutions) and (not motion_module_decoder_only), use_temporal_module=use_temporal_module and (res in motion_module_resolutions) and (not motion_module_decoder_only), motion_module_type=motion_module_type, temporal_module_type=temporal_module_type, motion_module_kwargs=motion_module_kwargs, temporal_module_kwargs=temporal_module_kwargs ) self.down_blocks.append(down_block)
# mid if mid_block_type == "UNetMidBlock3DCrossAttn": self.mid_block = UNetMidBlock3DCrossAttn( in_channels=block_out_channels[-1], temb_channels=time_embed_dim, resnet_eps=norm_eps, resnet_act_fn=act_fn, output_scale_factor=mid_block_scale_factor, resnet_time_scale_shift=resnet_time_scale_shift, cross_attention_dim=cross_attention_dim, attn_num_head_channels=attention_head_dim[-1], resnet_groups=norm_num_groups, dual_cross_attention=dual_cross_attention, 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, use_inflated_groupnorm=use_inflated_groupnorm, use_motion_module=use_motion_module and motion_module_mid_block, use_temporal_module=use_temporal_module and motion_module_mid_block, motion_module_type=motion_module_type, temporal_module_type=temporal_module_type, motion_module_kwargs=motion_module_kwargs, temporal_module_kwargs=temporal_module_kwargs, ) else: raise ValueError(f"unknown mid_block_type : {mid_block_type}")
# count how many layers upsample the videos self.num_upsamplers = 0
# up reversed_block_out_channels = list(reversed(block_out_channels)) reversed_attention_head_dim = list(reversed(attention_head_dim)) only_cross_attention = list(reversed(only_cross_attention)) output_channel = reversed_block_out_channels[0] for i, up_block_type in enumerate(up_block_types): res = 2 ** (3 - i) is_final_block = i == len(block_out_channels) - 1
prev_output_channel = output_channel output_channel = reversed_block_out_channels[i] input_channel = reversed_block_out_channels[ min(i + 1, len(block_out_channels) - 1) ]
# add upsample block for all BUT final layer if not is_final_block: add_upsample = True self.num_upsamplers += 1 else: add_upsample = False
up_block = get_up_block( up_block_type, num_layers=layers_per_block + 1, in_channels=input_channel, out_channels=output_channel, prev_output_channel=prev_output_channel, temb_channels=time_embed_dim, add_upsample=add_upsample, resnet_eps=norm_eps, resnet_act_fn=act_fn, resnet_groups=norm_num_groups, cross_attention_dim=cross_attention_dim, attn_num_head_channels=reversed_attention_head_dim[i], dual_cross_attention=dual_cross_attention, use_linear_projection=use_linear_projection, only_cross_attention=only_cross_attention[i], 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 and (res in motion_module_resolutions), use_temporal_module=use_temporal_module and (res in motion_module_resolutions), motion_module_type=motion_module_type, temporal_module_type=temporal_module_type, motion_module_kwargs=motion_module_kwargs, temporal_module_kwargs=temporal_module_kwargs, ) self.up_blocks.append(up_block) prev_output_channel = output_channel
# out if use_inflated_groupnorm: self.conv_norm_out = InflatedGroupNorm( num_channels=block_out_channels[0], num_groups=norm_num_groups, eps=norm_eps, ) else: self.conv_norm_out = nn.GroupNorm( num_channels=block_out_channels[0], num_groups=norm_num_groups, eps=norm_eps, ) self.conv_act = nn.SiLU() self.conv_out = InflatedConv3d( block_out_channels[0], out_channels, kernel_size=3, padding=1 )
@property # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors def attn_processors(self) -> Dict[str, AttentionProcessor]: r""" Returns: `dict` of attention processors: A dictionary containing all attention processors used in the model with indexed by its weight name. """ # set recursively processors = {}
def fn_recursive_add_processors( name: str, module: torch.nn.Module, processors: Dict[str, AttentionProcessor], ): if hasattr(module, "set_processor"): processors[f"{name}.processor"] = module.processor
for sub_name, child in module.named_children(): if "temporal_transformer" not in sub_name: fn_recursive_add_processors(f"{name}.{sub_name}", child, processors)
return processors
for name, module in self.named_children(): if "temporal_transformer" not in name: fn_recursive_add_processors(name, module, processors)
return processors
def set_attention_slice(self, slice_size): r""" Enable sliced attention computation.
When this option is enabled, the attention module will split the input tensor in slices, to compute attention in several steps. This is useful to save some memory in exchange for a small speed decrease.
Args: slice_size (`str` or `int` or `list(int)`, *optional*, defaults to `"auto"`): When `"auto"`, halves the input to the attention heads, so attention will be computed in two steps. If `"max"`, maxium amount of memory will be saved by running only one slice at a time. If a number is provided, uses as many slices as `attention_head_dim // slice_size`. In this case, `attention_head_dim` must be a multiple of `slice_size`. """ sliceable_head_dims = []
def fn_recursive_retrieve_slicable_dims(module: torch.nn.Module): if hasattr(module, "set_attention_slice"): sliceable_head_dims.append(module.sliceable_head_dim)
for child in module.children(): fn_recursive_retrieve_slicable_dims(child)
# retrieve number of attention layers for module in self.children(): fn_recursive_retrieve_slicable_dims(module)
num_slicable_layers = len(sliceable_head_dims)
if slice_size == "auto": # half the attention head size is usually a good trade-off between # speed and memory slice_size = [dim // 2 for dim in sliceable_head_dims] elif slice_size == "max": # make smallest slice possible slice_size = num_slicable_layers * [1]
slice_size = ( num_slicable_layers * [slice_size] if not isinstance(slice_size, list) else slice_size )
if len(slice_size) != len(sliceable_head_dims): raise ValueError( f"You have provided {len(slice_size)}, but {self.config} has {len(sliceable_head_dims)} different" f" attention layers. Make sure to match `len(slice_size)` to be {len(sliceable_head_dims)}." )
for i in range(len(slice_size)): size = slice_size[i] dim = sliceable_head_dims[i] if size is not None and size > dim: raise ValueError(f"size {size} has to be smaller or equal to {dim}.")
# Recursively walk through all the children. # Any children which exposes the set_attention_slice method # gets the message def fn_recursive_set_attention_slice( module: torch.nn.Module, slice_size: List[int] ): if hasattr(module, "set_attention_slice"): module.set_attention_slice(slice_size.pop())
for child in module.children(): fn_recursive_set_attention_slice(child, slice_size)
reversed_slice_size = list(reversed(slice_size)) for module in self.children(): fn_recursive_set_attention_slice(module, reversed_slice_size)
def set_use_cross_frame_attention(self, value):
def fn_recursive_set_use_cf_att(module: torch.nn.Module, value): if hasattr(module, "set_use_cross_frame_attention"): module.set_use_cross_frame_attention(value)
for child in module.children(): fn_recursive_set_use_cf_att(child, value)
for module in self.children(): fn_recursive_set_use_cf_att(module, value)
def _set_gradient_checkpointing(self, module, value=False): if hasattr(module, "gradient_checkpointing"): module.gradient_checkpointing = value
# Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.set_attn_processor def set_attn_processor( self, processor: Union[AttentionProcessor, Dict[str, AttentionProcessor]] ): r""" Sets the attention processor to use to compute attention.
Parameters: processor (`dict` of `AttentionProcessor` or only `AttentionProcessor`): The instantiated processor class or a dictionary of processor classes that will be set as the processor for **all** `Attention` layers.
If `processor` is a dict, the key needs to define the path to the corresponding cross attention processor. This is strongly recommended when setting trainable attention processors.
""" count = len(self.attn_processors.keys())
if isinstance(processor, dict) and len(processor) != count: raise ValueError( f"A dict of processors was passed, but the number of processors {len(processor)} does not match the" f" number of attention layers: {count}. Please make sure to pass {count} processor classes." )
def fn_recursive_attn_processor(name: str, module: torch.nn.Module, processor): if hasattr(module, "set_processor"): if not isinstance(processor, dict): module.set_processor(processor) else: module.set_processor(processor.pop(f"{name}.processor"))
for sub_name, child in module.named_children(): if "temporal_transformer" not in sub_name: fn_recursive_attn_processor(f"{name}.{sub_name}", child, processor)
for name, module in self.named_children(): if "temporal_transformer" not in name: fn_recursive_attn_processor(name, module, processor)
def forward( self, sample: torch.FloatTensor, timestep: Union[torch.Tensor, float, int], encoder_hidden_states: torch.Tensor, class_labels: Optional[torch.Tensor] = None, pose_cond_fea = None, attention_mask: Optional[torch.Tensor] = None, down_block_additional_residuals: Optional[Tuple[torch.Tensor]] = None, mid_block_additional_residual: Optional[torch.Tensor] = None, return_dict: bool = True, skip_mm: bool = False, ) -> Union[UNet3DConditionOutput, Tuple]: r""" Args: sample (`torch.FloatTensor`): (batch, channel, height, width) noisy inputs tensor timestep (`torch.FloatTensor` or `float` or `int`): (batch) timesteps encoder_hidden_states (`torch.FloatTensor`): (batch, sequence_length, feature_dim) encoder hidden states return_dict (`bool`, *optional*, defaults to `True`): Whether or not to return a [`models.unet_2d_condition.UNet2DConditionOutput`] instead of a plain tuple.
Returns: [`~models.unet_2d_condition.UNet2DConditionOutput`] or `tuple`: [`~models.unet_2d_condition.UNet2DConditionOutput`] if `return_dict` is True, otherwise a `tuple`. When returning a tuple, the first element is the sample tensor. """ # By default samples have to be AT least a multiple of the overall upsampling factor. # The overall upsampling factor is equal to 2 ** (# num of upsampling layears). # However, the upsampling interpolation output size can be forced to fit any upsampling size # on the fly if necessary.
default_overall_up_factor = 2**self.num_upsamplers
# upsample size should be forwarded when sample is not a multiple of `default_overall_up_factor` forward_upsample_size = False upsample_size = None
if any(s % default_overall_up_factor != 0 for s in sample.shape[-2:]): logger.info("Forward upsample size to force interpolation output size.") forward_upsample_size = True
# prepare attention_mask if attention_mask is not None: attention_mask = (1 - attention_mask.to(sample.dtype)) * -10000.0 attention_mask = attention_mask.unsqueeze(1)
# center input if necessary if self.config.center_input_sample: sample = 2 * sample - 1.0
# time timesteps = timestep if not torch.is_tensor(timesteps): # This would be a good case for the `match` statement (Python 3.10+) is_mps = sample.device.type == "mps" if isinstance(timestep, float): dtype = torch.float32 if is_mps else torch.float64 else: dtype = torch.int32 if is_mps else torch.int64 timesteps = torch.tensor([timesteps], dtype=dtype, device=sample.device) elif len(timesteps.shape) == 0: timesteps = timesteps[None].to(sample.device)
# broadcast to batch dimension in a way that's compatible with ONNX/Core ML timesteps = timesteps.expand(sample.shape[0])
t_emb = self.time_proj(timesteps)
# timesteps does not contain any weights and will always return f32 tensors # but time_embedding might actually be running in fp16. so we need to cast here. # there might be better ways to encapsulate this. t_emb = t_emb.to(dtype=self.dtype) emb = self.time_embedding(t_emb) if self.class_embedding is not None: if class_labels is None: raise ValueError( "class_labels should be provided when num_class_embeds > 0" )
if self.config.class_embed_type == "timestep": class_labels = self.time_proj(class_labels)
class_emb = self.class_embedding(class_labels).to(dtype=self.dtype) emb = emb + class_emb
# pre-process sample = self.conv_in(sample) if pose_cond_fea is not None: sample = sample + pose_cond_fea[0]
# down down_block_res_samples = (sample,) block_count = 1 for downsample_block in self.down_blocks: if ( hasattr(downsample_block, "has_cross_attention") and downsample_block.has_cross_attention ): sample, res_samples = downsample_block( hidden_states=sample, temb=emb, encoder_hidden_states=encoder_hidden_states, attention_mask=attention_mask, skip_mm=skip_mm, ) else: sample, res_samples = downsample_block( hidden_states=sample, temb=emb, encoder_hidden_states=encoder_hidden_states, skip_mm=skip_mm, ) if pose_cond_fea is not None: sample = sample + pose_cond_fea[block_count] block_count += 1 down_block_res_samples += res_samples
if down_block_additional_residuals is not None: new_down_block_res_samples = ()
for down_block_res_sample, down_block_additional_residual in zip( down_block_res_samples, down_block_additional_residuals ): down_block_res_sample = ( down_block_res_sample + down_block_additional_residual ) new_down_block_res_samples += (down_block_res_sample,)
down_block_res_samples = new_down_block_res_samples
# mid sample = self.mid_block( sample, emb, encoder_hidden_states=encoder_hidden_states, attention_mask=attention_mask, skip_mm=skip_mm, )
if mid_block_additional_residual is not None: sample = sample + mid_block_additional_residual
# up for i, upsample_block in enumerate(self.up_blocks): is_final_block = i == len(self.up_blocks) - 1
res_samples = down_block_res_samples[-len(upsample_block.resnets) :] down_block_res_samples = down_block_res_samples[ : -len(upsample_block.resnets) ]
# if we have not reached the final block and need to forward the # upsample size, we do it here if not is_final_block and forward_upsample_size: upsample_size = down_block_res_samples[-1].shape[2:]
if ( hasattr(upsample_block, "has_cross_attention") and upsample_block.has_cross_attention ): sample = upsample_block( hidden_states=sample, temb=emb, res_hidden_states_tuple=res_samples, encoder_hidden_states=encoder_hidden_states, upsample_size=upsample_size, attention_mask=attention_mask, skip_mm=skip_mm, ) else: sample = upsample_block( hidden_states=sample, temb=emb, res_hidden_states_tuple=res_samples, upsample_size=upsample_size, encoder_hidden_states=encoder_hidden_states, skip_mm=skip_mm, )
# post-process sample = self.conv_norm_out(sample) sample = self.conv_act(sample) sample = self.conv_out(sample)
if not return_dict: return (sample,)
return UNet3DConditionOutput(sample=sample)
@classmethod def from_pretrained_2d( cls, pretrained_model_path: PathLike, motion_module_path: PathLike, subfolder=None, unet_additional_kwargs=None, mm_zero_proj_out=False, ): pretrained_model_path = Path(pretrained_model_path) motion_module_path = Path(motion_module_path) if subfolder is not None: pretrained_model_path = pretrained_model_path.joinpath(subfolder) logger.info( f"loaded temporal unet's pretrained weights from {pretrained_model_path} ..." )
config_file = pretrained_model_path / "config.json" if not (config_file.exists() and config_file.is_file()): raise RuntimeError(f"{config_file} does not exist or is not a file")
unet_config = cls.load_config(config_file) unet_config["_class_name"] = cls.__name__ unet_config["down_block_types"] = [ "CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "DownBlock3D", ] unet_config["up_block_types"] = [ "UpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D", ] unet_config["mid_block_type"] = "UNetMidBlock3DCrossAttn"
model = cls.from_config(unet_config, **unet_additional_kwargs) # load the vanilla weights if pretrained_model_path.joinpath(SAFETENSORS_WEIGHTS_NAME).exists(): logger.debug( f"loading safeTensors weights from {pretrained_model_path} ..." ) state_dict = load_file( pretrained_model_path.joinpath(SAFETENSORS_WEIGHTS_NAME), device="cpu" )
elif pretrained_model_path.joinpath(WEIGHTS_NAME).exists(): logger.debug(f"loading weights from {pretrained_model_path} ...") state_dict = torch.load( pretrained_model_path.joinpath(WEIGHTS_NAME), map_location="cpu", weights_only=True, ) else: raise FileNotFoundError(f"no weights file found in {pretrained_model_path}")
# load the motion module weights if motion_module_path.exists() and motion_module_path.is_file(): if motion_module_path.suffix.lower() in [".pth", ".pt", ".ckpt"]: logger.info(f"Load motion module params from {motion_module_path}") motion_state_dict = torch.load( motion_module_path, map_location="cpu", weights_only=True ) elif motion_module_path.suffix.lower() == ".safetensors": motion_state_dict = load_file(motion_module_path, device="cpu") else: raise RuntimeError( f"unknown file format for motion module weights: {motion_module_path.suffix}" )
motion_state_dict = { k.replace('motion_modules.', 'temporal_modules.'): v for k, v in motion_state_dict.items() if not "pos_encoder" in k }
if mm_zero_proj_out: logger.info(f"Zero initialize proj_out layers in motion module...") new_motion_state_dict = OrderedDict() for k in motion_state_dict: if "proj_out" in k: continue new_motion_state_dict[k] = motion_state_dict[k] motion_state_dict = new_motion_state_dict
# merge the state dicts state_dict.update(motion_state_dict)
# load the weights into the model m, u = model.load_state_dict(state_dict, strict=False) logger.debug(f"### missing keys: {len(m)}; \n### unexpected keys: {len(u)};")
params = [ p.numel() if "temporal_modules" in n else 0 for n, p in model.named_parameters() ] mm_params = [ p.numel() if "motion_modules" in n else 0 for n, p in model.named_parameters() ] logger.info( f"Loaded {sum(mm_params) / 1e6}M-parameter motion module, Loaded {sum(params) / 1e6}M-parameter temporal module" )
return model12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121# *************************************************************************# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates# SPDX-License-Identifier: Apache-2.0 ## This file has been modified by ByteDance Ltd. and/or its affiliates.## Original file was released under Aniportrait, with the full license text# available at https://github.com/Zejun-Yang/AniPortrait/blob/main/LICENSE.## This modified file is released under the same license.# *************************************************************************import pdbfrom typing import Dict, Optionalimport torchfrom torch import nn
from src.models.motion_module import get_motion_module
# from .motion_module import get_motion_modulefrom src.models.resnet import Downsample3D, ResnetBlock3D, Upsample3Dfrom .transformer_3d import Transformer3DModel
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=None, unet_use_temporal_attention=None, use_inflated_groupnorm=None, use_motion_module=None, motion_module_type=None, motion_module_kwargs=None, use_temporal_module=None, temporal_module_type=None, temporal_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, use_temporal_module=use_temporal_module, temporal_module_type=temporal_module_type, temporal_module_kwargs=temporal_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, use_temporal_module=use_temporal_module, temporal_module_type=temporal_module_type, temporal_module_kwargs=temporal_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=None, unet_use_temporal_attention=None, use_inflated_groupnorm=None, use_motion_module=None, motion_module_type=None, motion_module_kwargs=None, use_temporal_module=None, temporal_module_type=None, temporal_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, use_temporal_module=use_temporal_module, temporal_module_type=temporal_module_type, temporal_module_kwargs=temporal_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, use_temporal_module=use_temporal_module, temporal_module_type=temporal_module_type, temporal_module_kwargs=temporal_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=None, unet_use_temporal_attention=None, use_inflated_groupnorm=None, use_motion_module=None, motion_module_type=None, motion_module_kwargs=None, use_temporal_module=None, temporal_module_type=None, temporal_module_kwargs=None, **transformer_kwargs, ): 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) )
# there is always at least one resnet 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, **transformer_kwargs ) ) 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) self.temporal_modules = nn.ModuleList( [ ( get_motion_module( in_channels=in_channels, motion_module_type=temporal_module_type, motion_module_kwargs=temporal_module_kwargs, ) if use_temporal_module else None ) for _ in range(num_layers) ] ) self.gradient_checkpointing = False
def forward( self, hidden_states, temb=None, encoder_hidden_states=None, attention_mask=None, skip_mm=False, ): if isinstance(encoder_hidden_states, list): encoder_hidden_states, motion_hidden_states = encoder_hidden_states else: motion_hidden_states = encoder_hidden_states
hidden_states = self.resnets[0](hidden_states, temb) for attn, resnet, motion_module, temporal_module in zip( self.attentions, self.resnets[1:], self.motion_modules, self.temporal_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(attn, return_dict=False), hidden_states, encoder_hidden_states, )[0] if (motion_module is not None) and not skip_mm: hidden_states = torch.utils.checkpoint.checkpoint( create_custom_forward(motion_module), hidden_states, temb, motion_hidden_states, ) if (temporal_module is not None) and not skip_mm: hidden_states = torch.utils.checkpoint.checkpoint( create_custom_forward(temporal_module), hidden_states.requires_grad_(), temb, None, ) # hidden_states = ( # temporal_module(hidden_states, temb, encoder_hidden_states=None) # if (temporal_module is not None) and not skip_mm # else hidden_states # ) hidden_states = torch.utils.checkpoint.checkpoint( create_custom_forward(resnet), hidden_states, temb ) else: hidden_states = attn( hidden_states, encoder_hidden_states=encoder_hidden_states, ).sample hidden_states = ( motion_module( hidden_states, temb, encoder_hidden_states=motion_hidden_states ) if (motion_module is not None) and not skip_mm else hidden_states ) hidden_states = ( temporal_module(hidden_states, temb, encoder_hidden_states=None, debug=True) if (temporal_module is not None) and not skip_mm 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=None, unet_use_temporal_attention=None, use_inflated_groupnorm=None, use_motion_module=None, motion_module_type=None, motion_module_kwargs=None, use_temporal_module=None, temporal_module_type=None, temporal_module_kwargs=None, **transformer_kwargs, ): 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, **transformer_kwargs, ) ) 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) self.temporal_modules = nn.ModuleList( [ ( get_motion_module( in_channels=out_channels, motion_module_type=temporal_module_type, motion_module_kwargs=temporal_module_kwargs, ) if use_temporal_module else None ) for _ in range(num_layers) ] )
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, skip_mm=False ): if isinstance(encoder_hidden_states, list): encoder_hidden_states, motion_hidden_states = encoder_hidden_states else: motion_hidden_states = encoder_hidden_states
output_states = ()
for i, (resnet, attn, motion_module, temporal_module) in enumerate( zip(self.resnets, self.attentions, self.motion_modules, self.temporal_modules) ):
# self.gradient_checkpointing = False 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]
# add motion module if (motion_module is not None) and not skip_mm: hidden_states = torch.utils.checkpoint.checkpoint( create_custom_forward(motion_module), hidden_states, temb, motion_hidden_states, ) if (temporal_module is not None) and not skip_mm: # hidden_states = torch.utils.checkpoint.checkpoint( # create_custom_forward(temporal_module), # hidden_states.requires_grad_(), # temb, # None, # ) hidden_states = ( temporal_module(hidden_states, temb, encoder_hidden_states=None) if (temporal_module is not None) and not skip_mm else hidden_states )
else: hidden_states = resnet(hidden_states, temb) hidden_states = attn( hidden_states, encoder_hidden_states=encoder_hidden_states, ).sample
# add motion module hidden_states = ( motion_module( hidden_states, temb, encoder_hidden_states=motion_hidden_states ) if (motion_module is not None) and not skip_mm else hidden_states ) hidden_states = ( temporal_module(hidden_states, temb, encoder_hidden_states=None, debug=True) if (temporal_module is not None) and not skip_mm 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=None, use_motion_module=None, motion_module_type=None, motion_module_kwargs=None, use_temporal_module=None, temporal_module_type=None, temporal_module_kwargs=None, ): super().__init__() resnets = [] motion_modules = []
# use_motion_module = False 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) self.temporal_modules = nn.ModuleList( [ ( get_motion_module( in_channels=out_channels, motion_module_type=temporal_module_type, motion_module_kwargs=temporal_module_kwargs, ) if use_temporal_module else None ) for _ in range(num_layers) ] )
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, skip_mm=False): output_states = () if isinstance(encoder_hidden_states, list): encoder_hidden_states, motion_hidden_states = encoder_hidden_states else: motion_hidden_states = encoder_hidden_states for resnet, motion_module, temporal_module in zip( self.resnets, self.motion_modules, self.temporal_modules ): # print(f"DownBlock3D {self.gradient_checkpointing = }") 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) and not skip_mm: hidden_states = torch.utils.checkpoint.checkpoint( create_custom_forward(motion_module), hidden_states, temb, motion_hidden_states, )
if (temporal_module is not None) and not skip_mm: hidden_states = torch.utils.checkpoint.checkpoint( create_custom_forward(temporal_module), hidden_states.requires_grad_(), temb, None, ) else: hidden_states = resnet(hidden_states, temb)
# add motion module hidden_states = ( motion_module( hidden_states, temb, encoder_hidden_states=motion_hidden_states ) if (motion_module is not None) and not skip_mm else hidden_states ) hidden_states = ( temporal_module( hidden_states, temb, encoder_hidden_states=None, debug=True ) if (temporal_module is not None) and not skip_mm 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=None, unet_use_temporal_attention=None, use_motion_module=None, use_inflated_groupnorm=None, motion_module_type=None, motion_module_kwargs=None, use_temporal_module=None, temporal_module_type=None, temporal_module_kwargs=None, **transformer_kwargs, ): 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, **transformer_kwargs, ) ) 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) self.temporal_modules = nn.ModuleList( [ ( get_motion_module( in_channels=out_channels, motion_module_type=temporal_module_type, motion_module_kwargs=temporal_module_kwargs, ) if use_temporal_module else None ) for _ in range(num_layers) ] )
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, skip_mm=False, ): if isinstance(encoder_hidden_states, list): encoder_hidden_states, motion_hidden_states = encoder_hidden_states else: motion_hidden_states = encoder_hidden_states for i, (resnet, attn, motion_module, temporal_module) in enumerate( zip(self.resnets, self.attentions, self.motion_modules, self.temporal_modules) ): # pop res hidden states 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 = attn( # hidden_states, # encoder_hidden_states=encoder_hidden_states, # ).sample 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) and not skip_mm: hidden_states = torch.utils.checkpoint.checkpoint( create_custom_forward(motion_module), hidden_states, temb, motion_hidden_states, ) if (temporal_module is not None) and not skip_mm: hidden_states = torch.utils.checkpoint.checkpoint( create_custom_forward(temporal_module), hidden_states.requires_grad_(), temb, None, )
else: hidden_states = resnet(hidden_states, temb) hidden_states = attn( hidden_states, encoder_hidden_states=encoder_hidden_states, ).sample
# add motion module hidden_states = ( motion_module( hidden_states, temb, encoder_hidden_states=motion_hidden_states ) if (motion_module is not None) and not skip_mm else hidden_states )
# add temporal_module hidden_states = ( temporal_module(hidden_states, temb, encoder_hidden_states=None, debug=True) if (temporal_module is not None) and not skip_mm 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=None, use_motion_module=None, motion_module_type=None, motion_module_kwargs=None, use_temporal_module=None, temporal_module_type=None, temporal_module_kwargs=None, ): super().__init__() resnets = [] motion_modules = []
# use_motion_module = False 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) self.temporal_modules = nn.ModuleList( [ ( get_motion_module( in_channels=out_channels, motion_module_type=temporal_module_type, motion_module_kwargs=temporal_module_kwargs, ) if use_temporal_module else None ) for _ in range(num_layers) ] )
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, skip_mm=False, ): if isinstance(encoder_hidden_states, list): encoder_hidden_states, motion_hidden_states = encoder_hidden_states else: motion_hidden_states = encoder_hidden_states for resnet, motion_module, temporal_module in zip(self.resnets, self.motion_modules, self.temporal_modules): # pop res hidden states 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)
# print(f"UpBlock3D {self.gradient_checkpointing = }") 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) and not skip_mm: hidden_states = torch.utils.checkpoint.checkpoint( create_custom_forward(motion_module), hidden_states, temb, motion_hidden_states, ) if (temporal_module is not None) and not skip_mm: hidden_states = torch.utils.checkpoint.checkpoint( create_custom_forward(temporal_module), hidden_states.requires_grad_(), temb, None, )
else: hidden_states = resnet(hidden_states, temb) hidden_states = ( motion_module( hidden_states, temb, encoder_hidden_states=motion_hidden_states ) if (motion_module is not None) and not skip_mm else hidden_states ) hidden_states = ( temporal_module(hidden_states, temb, encoder_hidden_states=None, debug=True) if (temporal_module is not None) and not skip_mm else hidden_states )
if self.upsamplers is not None: for upsampler in self.upsamplers: hidden_states = upsampler(hidden_states, upsample_size)
return hidden_statesmodels/unet_3d.pyDeleted-727
models/unet_3d_blocks.pyDeleted-1121