Spaces:
Paused
Paused
Fix: un-ignore TRELLIS.2/trellis2/models/ — was blocked by 'models/' in .gitignore
Browse filesThe .gitignore rule 'models/' matched trellis2/models/ (the Python source
package), preventing all 7 model files from being committed and deployed.
HF container had no models/ directory, causing ModuleNotFoundError on import.
Changed 'models/' to '/models/' (root-only) and added explicit negation
'!TRELLIS.2/trellis2/models/' to ensure the package is always tracked.
Files changed (26)
- .gitignore+2-1
- Hunyuan3D-2.1/hy3dshape/hy3dshape/models/__init__.pyAdded+28
- Hunyuan3D-2.1/hy3dshape/hy3dshape/models/autoencoders/__init__.pyAdded+20
- Hunyuan3D-2.1/hy3dshape/hy3dshape/models/autoencoders/attention_blocks.pyAdded+716
- Hunyuan3D-2.1/hy3dshape/hy3dshape/models/autoencoders/attention_processors.pyAdded+96
- Hunyuan3D-2.1/hy3dshape/hy3dshape/models/autoencoders/model.pyAdded+339
- Hunyuan3D-2.1/hy3dshape/hy3dshape/models/autoencoders/surface_extractors.pyAdded+164
- Hunyuan3D-2.1/hy3dshape/hy3dshape/models/autoencoders/volume_decoders.pyAdded+435
- Hunyuan3D-2.1/hy3dshape/hy3dshape/models/conditioner.pyAdded+257
- Hunyuan3D-2.1/hy3dshape/hy3dshape/models/denoisers/__init__.pyAdded+15
- Hunyuan3D-2.1/hy3dshape/hy3dshape/models/denoisers/hunyuan3ddit.pyAdded+404
- Hunyuan3D-2.1/hy3dshape/hy3dshape/models/denoisers/hunyuandit.pyAdded+596
- Hunyuan3D-2.1/hy3dshape/hy3dshape/models/denoisers/moe_layers.pyAdded+177
- Hunyuan3D-2.1/hy3dshape/hy3dshape/models/diffusion/flow_matching_sit.pyAdded+354
- Hunyuan3D-2.1/hy3dshape/hy3dshape/models/diffusion/transport/__init__.pyAdded+97
- Hunyuan3D-2.1/hy3dshape/hy3dshape/models/diffusion/transport/integrators.pyAdded+142
- Hunyuan3D-2.1/hy3dshape/hy3dshape/models/diffusion/transport/path.pyAdded+220
- Hunyuan3D-2.1/hy3dshape/hy3dshape/models/diffusion/transport/transport.pyAdded+534
- Hunyuan3D-2.1/hy3dshape/hy3dshape/models/diffusion/transport/utils.pyAdded+54
- TRELLIS.2/trellis2/models/__init__.pyAdded+78
- TRELLIS.2/trellis2/models/sc_vaes/fdg_vae.pyAdded+110
- TRELLIS.2/trellis2/models/sc_vaes/sparse_unet_vae.pyAdded+522
- TRELLIS.2/trellis2/models/sparse_elastic_mixin.pyAdded+24
- TRELLIS.2/trellis2/models/sparse_structure_flow.pyAdded+247
- TRELLIS.2/trellis2/models/sparse_structure_vae.pyAdded+306
- TRELLIS.2/trellis2/models/structured_latent_flow.pyAdded+207
18 unmodified lines1920212223242518 unmodified lines# Model weights (downloaded at runtime, not committed)weights/models/*.safetensors*.bin*.pt18 unmodified lines192021222324252618 unmodified lines# Model weights (downloaded at runtime, not committed)weights//models/!TRELLIS.2/trellis2/models/*.safetensors*.bin*.pt
12345678910111213141516171819202122232425262728# Open Source Model Licensed under the Apache License Version 2.0# and Other Licenses of the Third-Party Components therein:# The below Model in this distribution may have been modified by THL A29 Limited# ("Tencent Modifications"). All Tencent Modifications are Copyright (C) 2024 THL A29 Limited.
# Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.# The below software and/or models in this distribution may have been# modified by THL A29 Limited ("Tencent Modifications").# All Tencent Modifications are Copyright (C) THL A29 Limited.
# Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT# except for the third-party components listed below.# Hunyuan 3D does not impose any additional limitations beyond what is outlined# in the repsective licenses of these third-party components.# Users must comply with all terms and conditions of original licenses of these third-party# components and must ensure that the usage of the third party components adheres to# all relevant laws and regulations.
# For avoidance of doubts, Hunyuan 3D means the large language models and# their software and algorithms, including trained model weights, parameters (including# optimizer states), machine-learning model code, inference-enabling code, training-enabling code,# fine-tuning enabling code and other elements of the foregoing made publicly available# by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.
from .autoencoders import ShapeVAEfrom .conditioner import DualImageEncoder, SingleImageEncoder, DinoImageEncoder, CLIPImageEncoderfrom .denoisers import Hunyuan3DDiT1234567891011121314151617181920# Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT# except for the third-party components listed below.# Hunyuan 3D does not impose any additional limitations beyond what is outlined# in the repsective licenses of these third-party components.# Users must comply with all terms and conditions of original licenses of these third-party# components and must ensure that the usage of the third party components adheres to# all relevant laws and regulations.
# For avoidance of doubts, Hunyuan 3D means the large language models and# their software and algorithms, including trained model weights, parameters (including# optimizer states), machine-learning model code, inference-enabling code, training-enabling code,# fine-tuning enabling code and other elements of the foregoing made publicly available# by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.
from .attention_blocks import CrossAttentionDecoderfrom .attention_processors import FlashVDMCrossAttentionProcessor, CrossAttentionProcessor, \ FlashVDMTopMCrossAttentionProcessorfrom .model import ShapeVAE, VectsetVAEfrom .surface_extractors import SurfaceExtractors, MCSurfaceExtractor, DMCSurfaceExtractor, Latent2MeshOutputfrom .volume_decoders import HierarchicalVolumeDecoding, FlashVDMVolumeDecoding, VanillaVolumeDecoder123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716# Open Source Model Licensed under the Apache License Version 2.0# and Other Licenses of the Third-Party Components therein:# The below Model in this distribution may have been modified by THL A29 Limited# ("Tencent Modifications"). All Tencent Modifications are Copyright (C) 2024 THL A29 Limited.
# Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.# The below software and/or models in this distribution may have been# modified by THL A29 Limited ("Tencent Modifications").# All Tencent Modifications are Copyright (C) THL A29 Limited.
# Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT# except for the third-party components listed below.# Hunyuan 3D does not impose any additional limitations beyond what is outlined# in the repsective licenses of these third-party components.# Users must comply with all terms and conditions of original licenses of these third-party# components and must ensure that the usage of the third party components adheres to# all relevant laws and regulations.
# For avoidance of doubts, Hunyuan 3D means the large language models and# their software and algorithms, including trained model weights, parameters (including# optimizer states), machine-learning model code, inference-enabling code, training-enabling code,# fine-tuning enabling code and other elements of the foregoing made publicly available# by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.
import osfrom typing import Optional, Union, List
import torchimport torch.nn as nnfrom einops import rearrangefrom torch import Tensor
from .attention_processors import CrossAttentionProcessorfrom ...utils import logger
scaled_dot_product_attention = nn.functional.scaled_dot_product_attention
if os.environ.get('USE_SAGEATTN', '0') == '1': try: from sageattention import sageattn except ImportError: raise ImportError('Please install the package "sageattention" to use this USE_SAGEATTN.') scaled_dot_product_attention = sageattn
class FourierEmbedder(nn.Module): """The sin/cosine positional embedding. Given an input tensor `x` of shape [n_batch, ..., c_dim], it converts each feature dimension of `x[..., i]` into: [ sin(x[..., i]), sin(f_1*x[..., i]), sin(f_2*x[..., i]), ... sin(f_N * x[..., i]), cos(x[..., i]), cos(f_1*x[..., i]), cos(f_2*x[..., i]), ... cos(f_N * x[..., i]), x[..., i] # only present if include_input is True. ], here f_i is the frequency.
Denote the space is [0 / num_freqs, 1 / num_freqs, 2 / num_freqs, 3 / num_freqs, ..., (num_freqs - 1) / num_freqs]. If logspace is True, then the frequency f_i is [2^(0 / num_freqs), ..., 2^(i / num_freqs), ...]; Otherwise, the frequencies are linearly spaced between [1.0, 2^(num_freqs - 1)].
Args: num_freqs (int): the number of frequencies, default is 6; logspace (bool): If logspace is True, then the frequency f_i is [..., 2^(i / num_freqs), ...], otherwise, the frequencies are linearly spaced between [1.0, 2^(num_freqs - 1)]; input_dim (int): the input dimension, default is 3; include_input (bool): include the input tensor or not, default is True.
Attributes: frequencies (torch.Tensor): If logspace is True, then the frequency f_i is [..., 2^(i / num_freqs), ...], otherwise, the frequencies are linearly spaced between [1.0, 2^(num_freqs - 1);
out_dim (int): the embedding size, if include_input is True, it is input_dim * (num_freqs * 2 + 1), otherwise, it is input_dim * num_freqs * 2.
"""
def __init__(self, num_freqs: int = 6, logspace: bool = True, input_dim: int = 3, include_input: bool = True, include_pi: bool = True) -> None:
"""The initialization"""
super().__init__()
if logspace: frequencies = 2.0 ** torch.arange( num_freqs, dtype=torch.float32 ) else: frequencies = torch.linspace( 1.0, 2.0 ** (num_freqs - 1), num_freqs, dtype=torch.float32 )
if include_pi: frequencies *= torch.pi
self.register_buffer("frequencies", frequencies, persistent=False) self.include_input = include_input self.num_freqs = num_freqs
self.out_dim = self.get_dims(input_dim)
def get_dims(self, input_dim): temp = 1 if self.include_input or self.num_freqs == 0 else 0 out_dim = input_dim * (self.num_freqs * 2 + temp)
return out_dim
def forward(self, x: torch.Tensor) -> torch.Tensor: """ Forward process.
Args: x: tensor of shape [..., dim]
Returns: embedding: an embedding of `x` of shape [..., dim * (num_freqs * 2 + temp)] where temp is 1 if include_input is True and 0 otherwise. """
if self.num_freqs > 0: embed = (x[..., None].contiguous() * self.frequencies).view(*x.shape[:-1], -1) if self.include_input: return torch.cat((x, embed.sin(), embed.cos()), dim=-1) else: return torch.cat((embed.sin(), embed.cos()), dim=-1) else: return x
class DropPath(nn.Module): """Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks). """
def __init__(self, drop_prob: float = 0., scale_by_keep: bool = True): super(DropPath, self).__init__() self.drop_prob = drop_prob self.scale_by_keep = scale_by_keep
def forward(self, x): """Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).
This is the same as the DropConnect impl I created for EfficientNet, etc networks, however, the original name is misleading as 'Drop Connect' is a different form of dropout in a separate paper... See discussion: https://github.com/tensorflow/tpu/issues/494#issuecomment-532968956 ... I've opted for changing the layer and argument names to 'drop path' rather than mix DropConnect as a layer name and use 'survival rate' as the argument.
""" if self.drop_prob == 0. or not self.training: return x keep_prob = 1 - self.drop_prob shape = (x.shape[0],) + (1,) * (x.ndim - 1) # work with diff dim tensors, not just 2D ConvNets random_tensor = x.new_empty(shape).bernoulli_(keep_prob) if keep_prob > 0.0 and self.scale_by_keep: random_tensor.div_(keep_prob) return x * random_tensor
def extra_repr(self): return f'drop_prob={round(self.drop_prob, 3):0.3f}'
class MLP(nn.Module): def __init__( self, *, width: int, expand_ratio: int = 4, output_width: int = None, drop_path_rate: float = 0.0 ): super().__init__() self.width = width self.c_fc = nn.Linear(width, width * expand_ratio) self.c_proj = nn.Linear(width * expand_ratio, output_width if output_width is not None else width) self.gelu = nn.GELU() self.drop_path = DropPath(drop_path_rate) if drop_path_rate > 0. else nn.Identity()
def forward(self, x): return self.drop_path(self.c_proj(self.gelu(self.c_fc(x))))
class QKVMultiheadCrossAttention(nn.Module): def __init__( self, *, heads: int, n_data: Optional[int] = None, width=None, qk_norm=False, norm_layer=nn.LayerNorm ): super().__init__() self.heads = heads self.n_data = n_data self.q_norm = norm_layer(width // heads, elementwise_affine=True, eps=1e-6) if qk_norm else nn.Identity() self.k_norm = norm_layer(width // heads, elementwise_affine=True, eps=1e-6) if qk_norm else nn.Identity()
self.attn_processor = CrossAttentionProcessor()
def forward(self, q, kv): _, n_ctx, _ = q.shape bs, n_data, width = kv.shape attn_ch = width // self.heads // 2 q = q.view(bs, n_ctx, self.heads, -1) kv = kv.view(bs, n_data, self.heads, -1) k, v = torch.split(kv, attn_ch, dim=-1)
q = self.q_norm(q) k = self.k_norm(k) q, k, v = map(lambda t: rearrange(t, 'b n h d -> b h n d', h=self.heads), (q, k, v)) out = self.attn_processor(self, q, k, v) out = out.transpose(1, 2).reshape(bs, n_ctx, -1) return out
class MultiheadCrossAttention(nn.Module): def __init__( self, *, width: int, heads: int, qkv_bias: bool = True, n_data: Optional[int] = None, data_width: Optional[int] = None, norm_layer=nn.LayerNorm, qk_norm: bool = False, kv_cache: bool = False, ): super().__init__() self.n_data = n_data self.width = width self.heads = heads self.data_width = width if data_width is None else data_width self.c_q = nn.Linear(width, width, bias=qkv_bias) self.c_kv = nn.Linear(self.data_width, width * 2, bias=qkv_bias) self.c_proj = nn.Linear(width, width) self.attention = QKVMultiheadCrossAttention( heads=heads, n_data=n_data, width=width, norm_layer=norm_layer, qk_norm=qk_norm ) self.kv_cache = kv_cache self.data = None
def forward(self, x, data): x = self.c_q(x) if self.kv_cache: if self.data is None: self.data = self.c_kv(data) logger.info('Save kv cache,this should be called only once for one mesh') data = self.data else: data = self.c_kv(data) x = self.attention(x, data) x = self.c_proj(x) return x
class ResidualCrossAttentionBlock(nn.Module): def __init__( self, *, n_data: Optional[int] = None, width: int, heads: int, mlp_expand_ratio: int = 4, data_width: Optional[int] = None, qkv_bias: bool = True, norm_layer=nn.LayerNorm, qk_norm: bool = False ): super().__init__()
if data_width is None: data_width = width
self.attn = MultiheadCrossAttention( n_data=n_data, width=width, heads=heads, data_width=data_width, qkv_bias=qkv_bias, norm_layer=norm_layer, qk_norm=qk_norm ) self.ln_1 = norm_layer(width, elementwise_affine=True, eps=1e-6) self.ln_2 = norm_layer(data_width, elementwise_affine=True, eps=1e-6) self.ln_3 = norm_layer(width, elementwise_affine=True, eps=1e-6) self.mlp = MLP(width=width, expand_ratio=mlp_expand_ratio)
def forward(self, x: torch.Tensor, data: torch.Tensor): x = x + self.attn(self.ln_1(x), self.ln_2(data)) x = x + self.mlp(self.ln_3(x)) return x
class QKVMultiheadAttention(nn.Module): def __init__( self, *, heads: int, n_ctx: int, width=None, qk_norm=False, norm_layer=nn.LayerNorm ): super().__init__() self.heads = heads self.n_ctx = n_ctx self.q_norm = norm_layer(width // heads, elementwise_affine=True, eps=1e-6) if qk_norm else nn.Identity() self.k_norm = norm_layer(width // heads, elementwise_affine=True, eps=1e-6) if qk_norm else nn.Identity()
def forward(self, qkv): bs, n_ctx, width = qkv.shape attn_ch = width // self.heads // 3 qkv = qkv.view(bs, n_ctx, self.heads, -1) q, k, v = torch.split(qkv, attn_ch, dim=-1)
q = self.q_norm(q) k = self.k_norm(k)
q, k, v = map(lambda t: rearrange(t, 'b n h d -> b h n d', h=self.heads), (q, k, v)) out = scaled_dot_product_attention(q, k, v).transpose(1, 2).reshape(bs, n_ctx, -1) return out
class MultiheadAttention(nn.Module): def __init__( self, *, n_ctx: int, width: int, heads: int, qkv_bias: bool, norm_layer=nn.LayerNorm, qk_norm: bool = False, drop_path_rate: float = 0.0 ): super().__init__() self.n_ctx = n_ctx self.width = width self.heads = heads self.c_qkv = nn.Linear(width, width * 3, bias=qkv_bias) self.c_proj = nn.Linear(width, width) self.attention = QKVMultiheadAttention( heads=heads, n_ctx=n_ctx, width=width, norm_layer=norm_layer, qk_norm=qk_norm ) self.drop_path = DropPath(drop_path_rate) if drop_path_rate > 0. else nn.Identity()
def forward(self, x): x = self.c_qkv(x) x = self.attention(x) x = self.drop_path(self.c_proj(x)) return x
class ResidualAttentionBlock(nn.Module): def __init__( self, *, n_ctx: int, width: int, heads: int, qkv_bias: bool = True, norm_layer=nn.LayerNorm, qk_norm: bool = False, drop_path_rate: float = 0.0, ): super().__init__() self.attn = MultiheadAttention( n_ctx=n_ctx, width=width, heads=heads, qkv_bias=qkv_bias, norm_layer=norm_layer, qk_norm=qk_norm, drop_path_rate=drop_path_rate ) self.ln_1 = norm_layer(width, elementwise_affine=True, eps=1e-6) self.mlp = MLP(width=width, drop_path_rate=drop_path_rate) self.ln_2 = norm_layer(width, elementwise_affine=True, eps=1e-6)
def forward(self, x: torch.Tensor): x = x + self.attn(self.ln_1(x)) x = x + self.mlp(self.ln_2(x)) return x
class Transformer(nn.Module): def __init__( self, *, n_ctx: int, width: int, layers: int, heads: int, qkv_bias: bool = True, norm_layer=nn.LayerNorm, qk_norm: bool = False, drop_path_rate: float = 0.0 ): super().__init__() self.n_ctx = n_ctx self.width = width self.layers = layers self.resblocks = nn.ModuleList( [ ResidualAttentionBlock( n_ctx=n_ctx, width=width, heads=heads, qkv_bias=qkv_bias, norm_layer=norm_layer, qk_norm=qk_norm, drop_path_rate=drop_path_rate ) for _ in range(layers) ] )
def forward(self, x: torch.Tensor): for block in self.resblocks: x = block(x) return x
class CrossAttentionDecoder(nn.Module):
def __init__( self, *, num_latents: int, out_channels: int, fourier_embedder: FourierEmbedder, width: int, heads: int, mlp_expand_ratio: int = 4, downsample_ratio: int = 1, enable_ln_post: bool = True, qkv_bias: bool = True, qk_norm: bool = False, label_type: str = "binary" ): super().__init__()
self.enable_ln_post = enable_ln_post self.fourier_embedder = fourier_embedder self.downsample_ratio = downsample_ratio self.query_proj = nn.Linear(self.fourier_embedder.out_dim, width) if self.downsample_ratio != 1: self.latents_proj = nn.Linear(width * downsample_ratio, width) if self.enable_ln_post == False: qk_norm = False self.cross_attn_decoder = ResidualCrossAttentionBlock( n_data=num_latents, width=width, mlp_expand_ratio=mlp_expand_ratio, heads=heads, qkv_bias=qkv_bias, qk_norm=qk_norm )
if self.enable_ln_post: self.ln_post = nn.LayerNorm(width) self.output_proj = nn.Linear(width, out_channels) self.label_type = label_type self.count = 0
def set_cross_attention_processor(self, processor): self.cross_attn_decoder.attn.attention.attn_processor = processor
def set_default_cross_attention_processor(self): self.cross_attn_decoder.attn.attention.attn_processor = CrossAttentionProcessor
def forward(self, queries=None, query_embeddings=None, latents=None): if query_embeddings is None: query_embeddings = self.query_proj(self.fourier_embedder(queries).to(latents.dtype)) self.count += query_embeddings.shape[1] if self.downsample_ratio != 1: latents = self.latents_proj(latents) x = self.cross_attn_decoder(query_embeddings, latents) if self.enable_ln_post: x = self.ln_post(x) occ = self.output_proj(x) return occ
def fps( src: torch.Tensor, batch: Optional[Tensor] = None, ratio: Optional[Union[Tensor, float]] = None, random_start: bool = True, batch_size: Optional[int] = None, ptr: Optional[Union[Tensor, List[int]]] = None,): src = src.float() from torch_cluster import fps as fps_fn output = fps_fn(src, batch, ratio, random_start, batch_size, ptr) return output
class PointCrossAttentionEncoder(nn.Module):
def __init__( self, *, num_latents: int, downsample_ratio: float, pc_size: int, pc_sharpedge_size: int, fourier_embedder: FourierEmbedder, point_feats: int, width: int, heads: int, layers: int, normal_pe: bool = False, qkv_bias: bool = True, use_ln_post: bool = False, use_checkpoint: bool = False, qk_norm: bool = False ):
super().__init__()
self.use_checkpoint = use_checkpoint self.num_latents = num_latents self.downsample_ratio = downsample_ratio self.point_feats = point_feats self.normal_pe = normal_pe
if pc_sharpedge_size == 0: print( f'PointCrossAttentionEncoder INFO: pc_sharpedge_size is not given, using pc_size as pc_sharpedge_size') else: print( f'PointCrossAttentionEncoder INFO: pc_sharpedge_size is given, using pc_size={pc_size}, pc_sharpedge_size={pc_sharpedge_size}')
self.pc_size = pc_size self.pc_sharpedge_size = pc_sharpedge_size
self.fourier_embedder = fourier_embedder
self.input_proj = nn.Linear(self.fourier_embedder.out_dim + point_feats, width) self.cross_attn = ResidualCrossAttentionBlock( width=width, heads=heads, qkv_bias=qkv_bias, qk_norm=qk_norm )
self.self_attn = None if layers > 0: self.self_attn = Transformer( n_ctx=num_latents, width=width, layers=layers, heads=heads, qkv_bias=qkv_bias, qk_norm=qk_norm )
if use_ln_post: self.ln_post = nn.LayerNorm(width) else: self.ln_post = None
def sample_points_and_latents(self, pc: torch.FloatTensor, feats: Optional[torch.FloatTensor] = None): B, N, D = pc.shape num_pts = self.num_latents * self.downsample_ratio
# Compute number of latents num_latents = int(num_pts / self.downsample_ratio)
# Compute the number of random and sharpedge latents num_random_query = self.pc_size / (self.pc_size + self.pc_sharpedge_size) * num_latents num_sharpedge_query = num_latents - num_random_query
# Split random and sharpedge surface points random_pc, sharpedge_pc = torch.split(pc, [self.pc_size, self.pc_sharpedge_size], dim=1) assert random_pc.shape[1] <= self.pc_size, "Random surface points size must be less than or equal to pc_size" assert sharpedge_pc.shape[ 1] <= self.pc_sharpedge_size, "Sharpedge surface points size must be less than or equal to pc_sharpedge_size"
# Randomly select random surface points and random query points input_random_pc_size = int(num_random_query * self.downsample_ratio) random_query_ratio = num_random_query / input_random_pc_size idx_random_pc = torch.randperm(random_pc.shape[1], device=random_pc.device)[:input_random_pc_size] input_random_pc = random_pc[:, idx_random_pc, :] flatten_input_random_pc = input_random_pc.view(B * input_random_pc_size, D) N_down = int(flatten_input_random_pc.shape[0] / B) batch_down = torch.arange(B).to(pc.device) batch_down = torch.repeat_interleave(batch_down, N_down) idx_query_random = fps(flatten_input_random_pc, batch_down, ratio=random_query_ratio) query_random_pc = flatten_input_random_pc[idx_query_random].view(B, -1, D)
# Randomly select sharpedge surface points and sharpedge query points input_sharpedge_pc_size = int(num_sharpedge_query * self.downsample_ratio) if input_sharpedge_pc_size == 0: input_sharpedge_pc = torch.zeros(B, 0, D, dtype=input_random_pc.dtype).to(pc.device) query_sharpedge_pc = torch.zeros(B, 0, D, dtype=query_random_pc.dtype).to(pc.device) else: sharpedge_query_ratio = num_sharpedge_query / input_sharpedge_pc_size idx_sharpedge_pc = torch.randperm(sharpedge_pc.shape[1], device=sharpedge_pc.device)[ :input_sharpedge_pc_size] input_sharpedge_pc = sharpedge_pc[:, idx_sharpedge_pc, :] flatten_input_sharpedge_surface_points = input_sharpedge_pc.view(B * input_sharpedge_pc_size, D) N_down = int(flatten_input_sharpedge_surface_points.shape[0] / B) batch_down = torch.arange(B).to(pc.device) batch_down = torch.repeat_interleave(batch_down, N_down) idx_query_sharpedge = fps(flatten_input_sharpedge_surface_points, batch_down, ratio=sharpedge_query_ratio) query_sharpedge_pc = flatten_input_sharpedge_surface_points[idx_query_sharpedge].view(B, -1, D)
# Concatenate random and sharpedge surface points and query points query_pc = torch.cat([query_random_pc, query_sharpedge_pc], dim=1) input_pc = torch.cat([input_random_pc, input_sharpedge_pc], dim=1)
# PE query = self.fourier_embedder(query_pc) data = self.fourier_embedder(input_pc)
# Concat normal if given if self.point_feats != 0:
random_surface_feats, sharpedge_surface_feats = torch.split(feats, [self.pc_size, self.pc_sharpedge_size], dim=1) input_random_surface_feats = random_surface_feats[:, idx_random_pc, :] flatten_input_random_surface_feats = input_random_surface_feats.view(B * input_random_pc_size, -1) query_random_feats = flatten_input_random_surface_feats[idx_query_random].view(B, -1, flatten_input_random_surface_feats.shape[ -1])
if input_sharpedge_pc_size == 0: input_sharpedge_surface_feats = torch.zeros(B, 0, self.point_feats, dtype=input_random_surface_feats.dtype).to(pc.device) query_sharpedge_feats = torch.zeros(B, 0, self.point_feats, dtype=query_random_feats.dtype).to( pc.device) else: input_sharpedge_surface_feats = sharpedge_surface_feats[:, idx_sharpedge_pc, :] flatten_input_sharpedge_surface_feats = input_sharpedge_surface_feats.view(B * input_sharpedge_pc_size, -1) query_sharpedge_feats = flatten_input_sharpedge_surface_feats[idx_query_sharpedge].view(B, -1, flatten_input_sharpedge_surface_feats.shape[ -1])
query_feats = torch.cat([query_random_feats, query_sharpedge_feats], dim=1) input_feats = torch.cat([input_random_surface_feats, input_sharpedge_surface_feats], dim=1)
if self.normal_pe: query_normal_pe = self.fourier_embedder(query_feats[..., :3]) input_normal_pe = self.fourier_embedder(input_feats[..., :3]) query_feats = torch.cat([query_normal_pe, query_feats[..., 3:]], dim=-1) input_feats = torch.cat([input_normal_pe, input_feats[..., 3:]], dim=-1)
query = torch.cat([query, query_feats], dim=-1) data = torch.cat([data, input_feats], dim=-1)
if input_sharpedge_pc_size == 0: query_sharpedge_pc = torch.zeros(B, 1, D).to(pc.device) input_sharpedge_pc = torch.zeros(B, 1, D).to(pc.device)
# print(f'query_pc: {query_pc.shape}') # print(f'input_pc: {input_pc.shape}') # print(f'query_random_pc: {query_random_pc.shape}') # print(f'input_random_pc: {input_random_pc.shape}') # print(f'query_sharpedge_pc: {query_sharpedge_pc.shape}') # print(f'input_sharpedge_pc: {input_sharpedge_pc.shape}')
return query.view(B, -1, query.shape[-1]), data.view(B, -1, data.shape[-1]), [query_pc, input_pc, query_random_pc, input_random_pc, query_sharpedge_pc, input_sharpedge_pc]
def forward(self, pc, feats): """
Args: pc (torch.FloatTensor): [B, N, 3] feats (torch.FloatTensor or None): [B, N, C]
Returns:
"""
query, data, pc_infos = self.sample_points_and_latents(pc, feats)
query = self.input_proj(query) query = query data = self.input_proj(data) data = data
latents = self.cross_attn(query, data) if self.self_attn is not None: latents = self.self_attn(latents)
if self.ln_post is not None: latents = self.ln_post(latents)
return latents, pc_infos123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596# Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT# except for the third-party components listed below.# Hunyuan 3D does not impose any additional limitations beyond what is outlined# in the repsective licenses of these third-party components.# Users must comply with all terms and conditions of original licenses of these third-party# components and must ensure that the usage of the third party components adheres to# all relevant laws and regulations.
# For avoidance of doubts, Hunyuan 3D means the large language models and# their software and algorithms, including trained model weights, parameters (including# optimizer states), machine-learning model code, inference-enabling code, training-enabling code,# fine-tuning enabling code and other elements of the foregoing made publicly available# by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.
import os
import torchimport torch.nn.functional as F
scaled_dot_product_attention = F.scaled_dot_product_attentionif os.environ.get('CA_USE_SAGEATTN', '0') == '1': try: from sageattention import sageattn except ImportError: raise ImportError('Please install the package "sageattention" to use this USE_SAGEATTN.') scaled_dot_product_attention = sageattn
class CrossAttentionProcessor: def __call__(self, attn, q, k, v): out = scaled_dot_product_attention(q, k, v) return out
class FlashVDMCrossAttentionProcessor: def __init__(self, topk=None): self.topk = topk
def __call__(self, attn, q, k, v): if k.shape[-2] == 3072: topk = 1024 elif k.shape[-2] == 512: topk = 256 else: topk = k.shape[-2] // 3
if self.topk is True: q1 = q[:, :, ::100, :] sim = q1 @ k.transpose(-1, -2) sim = torch.mean(sim, -2) topk_ind = torch.topk(sim, dim=-1, k=topk).indices.squeeze(-2).unsqueeze(-1) topk_ind = topk_ind.expand(-1, -1, -1, v.shape[-1]) v0 = torch.gather(v, dim=-2, index=topk_ind) k0 = torch.gather(k, dim=-2, index=topk_ind) out = scaled_dot_product_attention(q, k0, v0) elif self.topk is False: out = scaled_dot_product_attention(q, k, v) else: idx, counts = self.topk start = 0 outs = [] for grid_coord, count in zip(idx, counts): end = start + count q_chunk = q[:, :, start:end, :] k0, v0 = self.select_topkv(q_chunk, k, v, topk) out = scaled_dot_product_attention(q_chunk, k0, v0) outs.append(out) start += count out = torch.cat(outs, dim=-2) self.topk = False return out
def select_topkv(self, q_chunk, k, v, topk): q1 = q_chunk[:, :, ::50, :] sim = q1 @ k.transpose(-1, -2) sim = torch.mean(sim, -2) topk_ind = torch.topk(sim, dim=-1, k=topk).indices.squeeze(-2).unsqueeze(-1) topk_ind = topk_ind.expand(-1, -1, -1, v.shape[-1]) v0 = torch.gather(v, dim=-2, index=topk_ind) k0 = torch.gather(k, dim=-2, index=topk_ind) return k0, v0
class FlashVDMTopMCrossAttentionProcessor(FlashVDMCrossAttentionProcessor): def select_topkv(self, q_chunk, k, v, topk): q1 = q_chunk[:, :, ::30, :] sim = q1 @ k.transpose(-1, -2) # sim = sim.to(torch.float32) sim = sim.softmax(-1) sim = torch.mean(sim, 1) activated_token = torch.where(sim > 1e-6)[2] index = torch.unique(activated_token, return_counts=True)[0].unsqueeze(0).unsqueeze(0).unsqueeze(-1) index = index.expand(-1, v.shape[1], -1, v.shape[-1]) v0 = torch.gather(v, dim=-2, index=index) k0 = torch.gather(k, dim=-2, index=index) return k0, v0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339# Open Source Model Licensed under the Apache License Version 2.0# and Other Licenses of the Third-Party Components therein:# The below Model in this distribution may have been modified by THL A29 Limited# ("Tencent Modifications"). All Tencent Modifications are Copyright (C) 2024 THL A29 Limited.
# Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.# The below software and/or models in this distribution may have been# modified by THL A29 Limited ("Tencent Modifications").# All Tencent Modifications are Copyright (C) THL A29 Limited.
# Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT# except for the third-party components listed below.# Hunyuan 3D does not impose any additional limitations beyond what is outlined# in the repsective licenses of these third-party components.# Users must comply with all terms and conditions of original licenses of these third-party# components and must ensure that the usage of the third party components adheres to# all relevant laws and regulations.
# For avoidance of doubts, Hunyuan 3D means the large language models and# their software and algorithms, including trained model weights, parameters (including# optimizer states), machine-learning model code, inference-enabling code, training-enabling code,# fine-tuning enabling code and other elements of the foregoing made publicly available# by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.
import osfrom typing import Union, List
import numpy as npimport torchimport torch.nn as nnimport yaml
from .attention_blocks import FourierEmbedder, Transformer, CrossAttentionDecoder, PointCrossAttentionEncoderfrom .surface_extractors import MCSurfaceExtractor, SurfaceExtractorsfrom .volume_decoders import VanillaVolumeDecoder, FlashVDMVolumeDecoding, HierarchicalVolumeDecodingfrom ...utils import logger, synchronize_timer, smart_load_model
class DiagonalGaussianDistribution(object): def __init__(self, parameters: Union[torch.Tensor, List[torch.Tensor]], deterministic=False, feat_dim=1): """ Initialize a diagonal Gaussian distribution with mean and log-variance parameters.
Args: parameters (Union[torch.Tensor, List[torch.Tensor]]): Either a single tensor containing concatenated mean and log-variance along `feat_dim`, or a list of two tensors [mean, logvar]. deterministic (bool, optional): If True, the distribution is deterministic (zero variance). Default is False. feat_dim (int, optional): Dimension along which mean and logvar are concatenated if parameters is a single tensor. Default is 1. """ self.feat_dim = feat_dim self.parameters = parameters
if isinstance(parameters, list): self.mean = parameters[0] self.logvar = parameters[1] else: self.mean, self.logvar = torch.chunk(parameters, 2, dim=feat_dim)
self.logvar = torch.clamp(self.logvar, -30.0, 20.0) self.deterministic = deterministic self.std = torch.exp(0.5 * self.logvar) self.var = torch.exp(self.logvar) if self.deterministic: self.var = self.std = torch.zeros_like(self.mean)
def sample(self): """ Sample from the diagonal Gaussian distribution.
Returns: torch.Tensor: A sample tensor with the same shape as the mean. """ x = self.mean + self.std * torch.randn_like(self.mean) return x
def kl(self, other=None, dims=(1, 2, 3)): """ Compute the Kullback-Leibler (KL) divergence between this distribution and another.
If `other` is None, compute KL divergence to a standard normal distribution N(0, I).
Args: other (DiagonalGaussianDistribution, optional): Another diagonal Gaussian distribution. dims (tuple, optional): Dimensions along which to compute the mean KL divergence. Default is (1, 2, 3).
Returns: torch.Tensor: The mean KL divergence value. """ if self.deterministic: return torch.Tensor([0.]) else: if other is None: return 0.5 * torch.mean(torch.pow(self.mean, 2) + self.var - 1.0 - self.logvar, dim=dims) else: return 0.5 * torch.mean( torch.pow(self.mean - other.mean, 2) / other.var + self.var / other.var - 1.0 - self.logvar + other.logvar, dim=dims)
def nll(self, sample, dims=(1, 2, 3)): if self.deterministic: return torch.Tensor([0.]) logtwopi = np.log(2.0 * np.pi) return 0.5 * torch.sum( logtwopi + self.logvar + torch.pow(sample - self.mean, 2) / self.var, dim=dims)
def mode(self): return self.mean
class VectsetVAE(nn.Module):
@classmethod @synchronize_timer('VectsetVAE Model Loading') def from_single_file( cls, ckpt_path, config_path, device='cuda', dtype=torch.float16, use_safetensors=None, **kwargs, ): # load config with open(config_path, 'r') as f: config = yaml.safe_load(f)
# load ckpt if use_safetensors: ckpt_path = ckpt_path.replace('.ckpt', '.safetensors') if not os.path.exists(ckpt_path): raise FileNotFoundError(f"Model file {ckpt_path} not found")
logger.info(f"Loading model from {ckpt_path}") if use_safetensors: import safetensors.torch ckpt = safetensors.torch.load_file(ckpt_path, device='cpu') else: ckpt = torch.load(ckpt_path, map_location='cpu', weights_only=True)
model_kwargs = config['params'] model_kwargs.update(kwargs)
model = cls(**model_kwargs) model.load_state_dict(ckpt) model.to(device=device, dtype=dtype) return model
@classmethod def from_pretrained( cls, model_path, device='cuda', dtype=torch.float16, use_safetensors=False, variant='fp16', subfolder='hunyuan3d-vae-v2-1', **kwargs, ): config_path, ckpt_path = smart_load_model( model_path, subfolder=subfolder, use_safetensors=use_safetensors, variant=variant )
return cls.from_single_file( ckpt_path, config_path, device=device, dtype=dtype, use_safetensors=use_safetensors, **kwargs ) def init_from_ckpt(self, path, ignore_keys=()): state_dict = torch.load(path, map_location="cpu") state_dict = state_dict.get("state_dict", state_dict) keys = list(state_dict.keys()) for k in keys: for ik in ignore_keys: if k.startswith(ik): print("Deleting key {} from state_dict.".format(k)) del state_dict[k] missing, unexpected = self.load_state_dict(state_dict, strict=False) print(f"Restored from {path} with {len(missing)} missing and {len(unexpected)} unexpected keys") if len(missing) > 0: print(f"Missing Keys: {missing}") print(f"Unexpected Keys: {unexpected}")
def __init__( self, volume_decoder=None, surface_extractor=None ): super().__init__() if volume_decoder is None: volume_decoder = VanillaVolumeDecoder() if surface_extractor is None: surface_extractor = MCSurfaceExtractor() self.volume_decoder = volume_decoder self.surface_extractor = surface_extractor
def latents2mesh(self, latents: torch.FloatTensor, **kwargs): with synchronize_timer('Volume decoding'): grid_logits = self.volume_decoder(latents, self.geo_decoder, **kwargs) with synchronize_timer('Surface extraction'): outputs = self.surface_extractor(grid_logits, **kwargs) return outputs
def enable_flashvdm_decoder( self, enabled: bool = True, adaptive_kv_selection=True, topk_mode='mean', mc_algo='dmc', ): if enabled: if adaptive_kv_selection: self.volume_decoder = FlashVDMVolumeDecoding(topk_mode) else: self.volume_decoder = HierarchicalVolumeDecoding() if mc_algo not in SurfaceExtractors.keys(): raise ValueError(f'Unsupported mc_algo {mc_algo}, available:{list(SurfaceExtractors.keys())}') self.surface_extractor = SurfaceExtractors[mc_algo]() else: self.volume_decoder = VanillaVolumeDecoder() self.surface_extractor = MCSurfaceExtractor()
class ShapeVAE(VectsetVAE): def __init__( self, *, num_latents: int, embed_dim: int, width: int, heads: int, num_decoder_layers: int, num_encoder_layers: int = 8, pc_size: int = 5120, pc_sharpedge_size: int = 5120, point_feats: int = 3, downsample_ratio: int = 20, geo_decoder_downsample_ratio: int = 1, geo_decoder_mlp_expand_ratio: int = 4, geo_decoder_ln_post: bool = True, num_freqs: int = 8, include_pi: bool = True, qkv_bias: bool = True, qk_norm: bool = False, label_type: str = "binary", drop_path_rate: float = 0.0, scale_factor: float = 1.0, use_ln_post: bool = True, ckpt_path = None ): super().__init__() self.geo_decoder_ln_post = geo_decoder_ln_post self.downsample_ratio = downsample_ratio
self.fourier_embedder = FourierEmbedder(num_freqs=num_freqs, include_pi=include_pi)
self.encoder = PointCrossAttentionEncoder( fourier_embedder=self.fourier_embedder, num_latents=num_latents, downsample_ratio=self.downsample_ratio, pc_size=pc_size, pc_sharpedge_size=pc_sharpedge_size, point_feats=point_feats, width=width, heads=heads, layers=num_encoder_layers, qkv_bias=qkv_bias, use_ln_post=use_ln_post, qk_norm=qk_norm )
self.pre_kl = nn.Linear(width, embed_dim * 2) self.post_kl = nn.Linear(embed_dim, width)
self.transformer = Transformer( n_ctx=num_latents, width=width, layers=num_decoder_layers, heads=heads, qkv_bias=qkv_bias, qk_norm=qk_norm, drop_path_rate=drop_path_rate )
self.geo_decoder = CrossAttentionDecoder( fourier_embedder=self.fourier_embedder, out_channels=1, num_latents=num_latents, mlp_expand_ratio=geo_decoder_mlp_expand_ratio, downsample_ratio=geo_decoder_downsample_ratio, enable_ln_post=self.geo_decoder_ln_post, width=width // geo_decoder_downsample_ratio, heads=heads // geo_decoder_downsample_ratio, qkv_bias=qkv_bias, qk_norm=qk_norm, label_type=label_type, )
self.scale_factor = scale_factor self.latent_shape = (num_latents, embed_dim)
if ckpt_path is not None: self.init_from_ckpt(ckpt_path)
def forward(self, latents): latents = self.post_kl(latents) latents = self.transformer(latents) return latents
def encode(self, surface, sample_posterior=True): pc, feats = surface[:, :, :3], surface[:, :, 3:] latents, _ = self.encoder(pc, feats) # print(latents.shape, self.pre_kl.weight.shape) moments = self.pre_kl(latents) posterior = DiagonalGaussianDistribution(moments, feat_dim=-1) if sample_posterior: latents = posterior.sample() else: latents = posterior.mode() return latents
def decode(self, latents): latents = self.post_kl(latents) latents = self.transformer(latents) return latents123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164# Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT# except for the third-party components listed below.# Hunyuan 3D does not impose any additional limitations beyond what is outlined# in the repsective licenses of these third-party components.# Users must comply with all terms and conditions of original licenses of these third-party# components and must ensure that the usage of the third party components adheres to# all relevant laws and regulations.
# For avoidance of doubts, Hunyuan 3D means the large language models and# their software and algorithms, including trained model weights, parameters (including# optimizer states), machine-learning model code, inference-enabling code, training-enabling code,# fine-tuning enabling code and other elements of the foregoing made publicly available# by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.
from typing import Union, Tuple, List
import numpy as npimport torchfrom skimage import measure
class Latent2MeshOutput: def __init__(self, mesh_v=None, mesh_f=None): self.mesh_v = mesh_v self.mesh_f = mesh_f
def center_vertices(vertices): """Translate the vertices so that bounding box is centered at zero.""" vert_min = vertices.min(dim=0)[0] vert_max = vertices.max(dim=0)[0] vert_center = 0.5 * (vert_min + vert_max) return vertices - vert_center
class SurfaceExtractor: def _compute_box_stat(self, bounds: Union[Tuple[float], List[float], float], octree_resolution: int): """ Compute grid size, bounding box minimum coordinates, and bounding box size based on input bounds and resolution.
Args: bounds (Union[Tuple[float], List[float], float]): Bounding box coordinates or a single float representing half side length. If float, bounds are assumed symmetric around zero in all axes. Expected format if list/tuple: [xmin, ymin, zmin, xmax, ymax, zmax]. octree_resolution (int): Resolution of the octree grid.
Returns: grid_size (List[int]): Grid size along each axis (x, y, z), each equal to octree_resolution + 1. bbox_min (np.ndarray): Minimum coordinates of the bounding box (xmin, ymin, zmin). bbox_size (np.ndarray): Size of the bounding box along each axis (xmax - xmin, etc.). """ if isinstance(bounds, float): bounds = [-bounds, -bounds, -bounds, bounds, bounds, bounds]
bbox_min, bbox_max = np.array(bounds[0:3]), np.array(bounds[3:6]) bbox_size = bbox_max - bbox_min grid_size = [int(octree_resolution) + 1, int(octree_resolution) + 1, int(octree_resolution) + 1] return grid_size, bbox_min, bbox_size
def run(self, *args, **kwargs): """ Abstract method to extract surface mesh from grid logits.
This method should be implemented by subclasses.
Raises: NotImplementedError: Always, since this is an abstract method. """ return NotImplementedError
def __call__(self, grid_logits, **kwargs): """ Process a batch of grid logits to extract surface meshes.
Args: grid_logits (torch.Tensor): Batch of grid logits with shape (batch_size, ...). **kwargs: Additional keyword arguments passed to the `run` method.
Returns: List[Optional[Latent2MeshOutput]]: List of mesh outputs for each grid in the batch. If extraction fails for a grid, None is appended at that position. """ outputs = [] for i in range(grid_logits.shape[0]): try: vertices, faces = self.run(grid_logits[i], **kwargs) vertices = vertices.astype(np.float32) faces = np.ascontiguousarray(faces) outputs.append(Latent2MeshOutput(mesh_v=vertices, mesh_f=faces))
except Exception: import traceback traceback.print_exc() outputs.append(None)
return outputs
class MCSurfaceExtractor(SurfaceExtractor): def run(self, grid_logit, *, mc_level, bounds, octree_resolution, **kwargs): """ Extract surface mesh using the Marching Cubes algorithm.
Args: grid_logit (torch.Tensor): 3D grid logits tensor representing the scalar field. mc_level (float): The level (iso-value) at which to extract the surface. bounds (Union[Tuple[float], List[float], float]): Bounding box coordinates or half side length. octree_resolution (int): Resolution of the octree grid. **kwargs: Additional keyword arguments (ignored).
Returns: Tuple[np.ndarray, np.ndarray]: Tuple containing: - vertices (np.ndarray): Extracted mesh vertices, scaled and translated to bounding box coordinates. - faces (np.ndarray): Extracted mesh faces (triangles). """ vertices, faces, normals, _ = measure.marching_cubes(grid_logit.cpu().numpy(), mc_level, method="lewiner") grid_size, bbox_min, bbox_size = self._compute_box_stat(bounds, octree_resolution) vertices = vertices / grid_size * bbox_size + bbox_min return vertices, faces
class DMCSurfaceExtractor(SurfaceExtractor): def run(self, grid_logit, *, octree_resolution, **kwargs): """ Extract surface mesh using Differentiable Marching Cubes (DMC) algorithm.
Args: grid_logit (torch.Tensor): 3D grid logits tensor representing the scalar field. octree_resolution (int): Resolution of the octree grid. **kwargs: Additional keyword arguments (ignored).
Returns: Tuple[np.ndarray, np.ndarray]: Tuple containing: - vertices (np.ndarray): Extracted mesh vertices, centered and converted to numpy. - faces (np.ndarray): Extracted mesh faces (triangles), with reversed vertex order. Raises: ImportError: If the 'diso' package is not installed. """ device = grid_logit.device if not hasattr(self, 'dmc'): try: from diso import DiffDMC self.dmc = DiffDMC(dtype=torch.float32).to(device) except: raise ImportError("Please install diso via `pip install diso`, or set mc_algo to 'mc'") sdf = -grid_logit / octree_resolution sdf = sdf.to(torch.float32).contiguous() verts, faces = self.dmc(sdf, deform=None, return_quads=False, normalize=True) verts = center_vertices(verts) vertices = verts.detach().cpu().numpy() faces = faces.detach().cpu().numpy()[:, ::-1] return vertices, faces
SurfaceExtractors = { 'mc': MCSurfaceExtractor, 'dmc': DMCSurfaceExtractor,}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435# Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT# except for the third-party components listed below.# Hunyuan 3D does not impose any additional limitations beyond what is outlined# in the repsective licenses of these third-party components.# Users must comply with all terms and conditions of original licenses of these third-party# components and must ensure that the usage of the third party components adheres to# all relevant laws and regulations.
# For avoidance of doubts, Hunyuan 3D means the large language models and# their software and algorithms, including trained model weights, parameters (including# optimizer states), machine-learning model code, inference-enabling code, training-enabling code,# fine-tuning enabling code and other elements of the foregoing made publicly available# by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.
from typing import Union, Tuple, List, Callable
import numpy as npimport torchimport torch.nn as nnimport torch.nn.functional as Ffrom einops import repeatfrom tqdm import tqdm
from .attention_blocks import CrossAttentionDecoderfrom .attention_processors import FlashVDMCrossAttentionProcessor, FlashVDMTopMCrossAttentionProcessorfrom ...utils import logger
def extract_near_surface_volume_fn(input_tensor: torch.Tensor, alpha: float): device = input_tensor.device D = input_tensor.shape[0] signed_val = 0.0
# 添加偏移并处理无效值 val = input_tensor + alpha valid_mask = val > -9000 # 假设-9000是无效值
# 改进的邻居获取函数(保持维度一致) def get_neighbor(t, shift, axis): """根据指定轴进行位移并保持维度一致""" if shift == 0: return t.clone()
# 确定填充轴(输入为[D, D, D]对应z,y,x轴) pad_dims = [0, 0, 0, 0, 0, 0] # 格式:[x前,x后,y前,y后,z前,z后]
# 根据轴类型设置填充 if axis == 0: # x轴(最后一个维度) pad_idx = 0 if shift > 0 else 1 pad_dims[pad_idx] = abs(shift) elif axis == 1: # y轴(中间维度) pad_idx = 2 if shift > 0 else 3 pad_dims[pad_idx] = abs(shift) elif axis == 2: # z轴(第一个维度) pad_idx = 4 if shift > 0 else 5 pad_dims[pad_idx] = abs(shift)
# 执行填充(添加batch和channel维度适配F.pad) padded = F.pad(t.unsqueeze(0).unsqueeze(0), pad_dims[::-1], mode='replicate') # 反转顺序适配F.pad
# 构建动态切片索引 slice_dims = [slice(None)] * 3 # 初始化为全切片 if axis == 0: # x轴(dim=2) if shift > 0: slice_dims[0] = slice(shift, None) else: slice_dims[0] = slice(None, shift) elif axis == 1: # y轴(dim=1) if shift > 0: slice_dims[1] = slice(shift, None) else: slice_dims[1] = slice(None, shift) elif axis == 2: # z轴(dim=0) if shift > 0: slice_dims[2] = slice(shift, None) else: slice_dims[2] = slice(None, shift)
# 应用切片并恢复维度 padded = padded.squeeze(0).squeeze(0) sliced = padded[slice_dims] return sliced
# 获取各方向邻居(确保维度一致) left = get_neighbor(val, 1, axis=0) # x方向 right = get_neighbor(val, -1, axis=0) back = get_neighbor(val, 1, axis=1) # y方向 front = get_neighbor(val, -1, axis=1) down = get_neighbor(val, 1, axis=2) # z方向 up = get_neighbor(val, -1, axis=2)
# 处理边界无效值(使用where保持维度一致) def safe_where(neighbor): return torch.where(neighbor > -9000, neighbor, val)
left = safe_where(left) right = safe_where(right) back = safe_where(back) front = safe_where(front) down = safe_where(down) up = safe_where(up)
# 计算符号一致性(转换为float32确保精度) sign = torch.sign(val.to(torch.float32)) neighbors_sign = torch.stack([ torch.sign(left.to(torch.float32)), torch.sign(right.to(torch.float32)), torch.sign(back.to(torch.float32)), torch.sign(front.to(torch.float32)), torch.sign(down.to(torch.float32)), torch.sign(up.to(torch.float32)) ], dim=0)
# 检查所有符号是否一致 same_sign = torch.all(neighbors_sign == sign, dim=0)
# 生成最终掩码 mask = (~same_sign).to(torch.int32) return mask * valid_mask.to(torch.int32)
def generate_dense_grid_points( bbox_min: np.ndarray, bbox_max: np.ndarray, octree_resolution: int, indexing: str = "ij",): length = bbox_max - bbox_min num_cells = octree_resolution
x = np.linspace(bbox_min[0], bbox_max[0], int(num_cells) + 1, dtype=np.float32) y = np.linspace(bbox_min[1], bbox_max[1], int(num_cells) + 1, dtype=np.float32) z = np.linspace(bbox_min[2], bbox_max[2], int(num_cells) + 1, dtype=np.float32) [xs, ys, zs] = np.meshgrid(x, y, z, indexing=indexing) xyz = np.stack((xs, ys, zs), axis=-1) grid_size = [int(num_cells) + 1, int(num_cells) + 1, int(num_cells) + 1]
return xyz, grid_size, length
class VanillaVolumeDecoder: @torch.no_grad() def __call__( self, latents: torch.FloatTensor, geo_decoder: Callable, bounds: Union[Tuple[float], List[float], float] = 1.01, num_chunks: int = 10000, octree_resolution: int = None, enable_pbar: bool = True, **kwargs, ): device = latents.device dtype = latents.dtype batch_size = latents.shape[0]
# 1. generate query points if isinstance(bounds, float): bounds = [-bounds, -bounds, -bounds, bounds, bounds, bounds]
bbox_min, bbox_max = np.array(bounds[0:3]), np.array(bounds[3:6]) xyz_samples, grid_size, length = generate_dense_grid_points( bbox_min=bbox_min, bbox_max=bbox_max, octree_resolution=octree_resolution, indexing="ij" ) xyz_samples = torch.from_numpy(xyz_samples).to(device, dtype=dtype).contiguous().reshape(-1, 3)
# 2. latents to 3d volume batch_logits = [] for start in tqdm(range(0, xyz_samples.shape[0], num_chunks), desc=f"Volume Decoding", disable=not enable_pbar): chunk_queries = xyz_samples[start: start + num_chunks, :] chunk_queries = repeat(chunk_queries, "p c -> b p c", b=batch_size) logits = geo_decoder(queries=chunk_queries, latents=latents) batch_logits.append(logits)
grid_logits = torch.cat(batch_logits, dim=1) grid_logits = grid_logits.view((batch_size, *grid_size)).float()
return grid_logits
class HierarchicalVolumeDecoding: @torch.no_grad() def __call__( self, latents: torch.FloatTensor, geo_decoder: Callable, bounds: Union[Tuple[float], List[float], float] = 1.01, num_chunks: int = 10000, mc_level: float = 0.0, octree_resolution: int = None, min_resolution: int = 63, enable_pbar: bool = True, **kwargs, ): device = latents.device dtype = latents.dtype
resolutions = [] if octree_resolution < min_resolution: resolutions.append(octree_resolution) while octree_resolution >= min_resolution: resolutions.append(octree_resolution) octree_resolution = octree_resolution // 2 resolutions.reverse()
# 1. generate query points if isinstance(bounds, float): bounds = [-bounds, -bounds, -bounds, bounds, bounds, bounds] bbox_min = np.array(bounds[0:3]) bbox_max = np.array(bounds[3:6]) bbox_size = bbox_max - bbox_min
xyz_samples, grid_size, length = generate_dense_grid_points( bbox_min=bbox_min, bbox_max=bbox_max, octree_resolution=resolutions[0], indexing="ij" )
dilate = nn.Conv3d(1, 1, 3, padding=1, bias=False, device=device, dtype=dtype) dilate.weight = torch.nn.Parameter(torch.ones(dilate.weight.shape, dtype=dtype, device=device))
grid_size = np.array(grid_size) xyz_samples = torch.from_numpy(xyz_samples).to(device, dtype=dtype).contiguous().reshape(-1, 3)
# 2. latents to 3d volume batch_logits = [] batch_size = latents.shape[0] for start in tqdm(range(0, xyz_samples.shape[0], num_chunks), desc=f"Hierarchical Volume Decoding [r{resolutions[0] + 1}]"): queries = xyz_samples[start: start + num_chunks, :] batch_queries = repeat(queries, "p c -> b p c", b=batch_size) logits = geo_decoder(queries=batch_queries, latents=latents) batch_logits.append(logits)
grid_logits = torch.cat(batch_logits, dim=1).view((batch_size, grid_size[0], grid_size[1], grid_size[2]))
for octree_depth_now in resolutions[1:]: grid_size = np.array([octree_depth_now + 1] * 3) resolution = bbox_size / octree_depth_now next_index = torch.zeros(tuple(grid_size), dtype=dtype, device=device) next_logits = torch.full(next_index.shape, -10000., dtype=dtype, device=device) curr_points = extract_near_surface_volume_fn(grid_logits.squeeze(0), mc_level) curr_points += grid_logits.squeeze(0).abs() < 0.95
if octree_depth_now == resolutions[-1]: expand_num = 0 else: expand_num = 1 for i in range(expand_num): curr_points = dilate(curr_points.unsqueeze(0).to(dtype)).squeeze(0) (cidx_x, cidx_y, cidx_z) = torch.where(curr_points > 0) next_index[cidx_x * 2, cidx_y * 2, cidx_z * 2] = 1 for i in range(2 - expand_num): next_index = dilate(next_index.unsqueeze(0)).squeeze(0) nidx = torch.where(next_index > 0)
next_points = torch.stack(nidx, dim=1) next_points = (next_points * torch.tensor(resolution, dtype=next_points.dtype, device=device) + torch.tensor(bbox_min, dtype=next_points.dtype, device=device)) batch_logits = [] for start in tqdm(range(0, next_points.shape[0], num_chunks), desc=f"Hierarchical Volume Decoding [r{octree_depth_now + 1}]"): queries = next_points[start: start + num_chunks, :] batch_queries = repeat(queries, "p c -> b p c", b=batch_size) logits = geo_decoder(queries=batch_queries.to(latents.dtype), latents=latents) batch_logits.append(logits) grid_logits = torch.cat(batch_logits, dim=1) next_logits[nidx] = grid_logits[0, ..., 0] grid_logits = next_logits.unsqueeze(0) grid_logits[grid_logits == -10000.] = float('nan')
return grid_logits
class FlashVDMVolumeDecoding: def __init__(self, topk_mode='mean'): if topk_mode not in ['mean', 'merge']: raise ValueError(f'Unsupported topk_mode {topk_mode}, available: {["mean", "merge"]}')
if topk_mode == 'mean': self.processor = FlashVDMCrossAttentionProcessor() else: self.processor = FlashVDMTopMCrossAttentionProcessor()
@torch.no_grad() def __call__( self, latents: torch.FloatTensor, geo_decoder: CrossAttentionDecoder, bounds: Union[Tuple[float], List[float], float] = 1.01, num_chunks: int = 10000, mc_level: float = 0.0, octree_resolution: int = None, min_resolution: int = 63, mini_grid_num: int = 4, enable_pbar: bool = True, **kwargs, ): processor = self.processor geo_decoder.set_cross_attention_processor(processor)
device = latents.device dtype = latents.dtype
resolutions = [] if octree_resolution < min_resolution: resolutions.append(octree_resolution) while octree_resolution >= min_resolution: resolutions.append(octree_resolution) octree_resolution = octree_resolution // 2 resolutions.reverse() resolutions[0] = round(resolutions[0] / mini_grid_num) * mini_grid_num - 1 for i, resolution in enumerate(resolutions[1:]): resolutions[i + 1] = resolutions[0] * 2 ** (i + 1)
logger.info(f"FlashVDMVolumeDecoding Resolution: {resolutions}")
# 1. generate query points if isinstance(bounds, float): bounds = [-bounds, -bounds, -bounds, bounds, bounds, bounds] bbox_min = np.array(bounds[0:3]) bbox_max = np.array(bounds[3:6]) bbox_size = bbox_max - bbox_min
xyz_samples, grid_size, length = generate_dense_grid_points( bbox_min=bbox_min, bbox_max=bbox_max, octree_resolution=resolutions[0], indexing="ij" )
dilate = nn.Conv3d(1, 1, 3, padding=1, bias=False, device=device, dtype=dtype) dilate.weight = torch.nn.Parameter(torch.ones(dilate.weight.shape, dtype=dtype, device=device))
grid_size = np.array(grid_size)
# 2. latents to 3d volume xyz_samples = torch.from_numpy(xyz_samples).to(device, dtype=dtype) batch_size = latents.shape[0] mini_grid_size = xyz_samples.shape[0] // mini_grid_num xyz_samples = xyz_samples.view( mini_grid_num, mini_grid_size, mini_grid_num, mini_grid_size, mini_grid_num, mini_grid_size, 3 ).permute( 0, 2, 4, 1, 3, 5, 6 ).reshape( -1, mini_grid_size * mini_grid_size * mini_grid_size, 3 ) batch_logits = [] num_batchs = max(num_chunks // xyz_samples.shape[1], 1) for start in tqdm(range(0, xyz_samples.shape[0], num_batchs), desc=f"FlashVDM Volume Decoding", disable=not enable_pbar): queries = xyz_samples[start: start + num_batchs, :] batch = queries.shape[0] batch_latents = repeat(latents.squeeze(0), "p c -> b p c", b=batch) processor.topk = True logits = geo_decoder(queries=queries, latents=batch_latents) batch_logits.append(logits) grid_logits = torch.cat(batch_logits, dim=0).reshape( mini_grid_num, mini_grid_num, mini_grid_num, mini_grid_size, mini_grid_size, mini_grid_size ).permute(0, 3, 1, 4, 2, 5).contiguous().view( (batch_size, grid_size[0], grid_size[1], grid_size[2]) )
for octree_depth_now in resolutions[1:]: grid_size = np.array([octree_depth_now + 1] * 3) resolution = bbox_size / octree_depth_now next_index = torch.zeros(tuple(grid_size), dtype=dtype, device=device) next_logits = torch.full(next_index.shape, -10000., dtype=dtype, device=device) curr_points = extract_near_surface_volume_fn(grid_logits.squeeze(0), mc_level) curr_points += grid_logits.squeeze(0).abs() < 0.95
if octree_depth_now == resolutions[-1]: expand_num = 0 else: expand_num = 1 for i in range(expand_num): curr_points = dilate(curr_points.unsqueeze(0).to(dtype)).squeeze(0) (cidx_x, cidx_y, cidx_z) = torch.where(curr_points > 0)
next_index[cidx_x * 2, cidx_y * 2, cidx_z * 2] = 1 for i in range(2 - expand_num): next_index = dilate(next_index.unsqueeze(0)).squeeze(0) nidx = torch.where(next_index > 0)
next_points = torch.stack(nidx, dim=1) next_points = (next_points * torch.tensor(resolution, dtype=torch.float32, device=device) + torch.tensor(bbox_min, dtype=torch.float32, device=device))
query_grid_num = 6 min_val = next_points.min(axis=0).values max_val = next_points.max(axis=0).values vol_queries_index = (next_points - min_val) / (max_val - min_val) * (query_grid_num - 0.001) index = torch.floor(vol_queries_index).long() index = index[..., 0] * (query_grid_num ** 2) + index[..., 1] * query_grid_num + index[..., 2] index = index.sort() next_points = next_points[index.indices].unsqueeze(0).contiguous() unique_values = torch.unique(index.values, return_counts=True) grid_logits = torch.zeros((next_points.shape[1]), dtype=latents.dtype, device=latents.device) input_grid = [[], []] logits_grid_list = [] start_num = 0 sum_num = 0 for grid_index, count in zip(unique_values[0].cpu().tolist(), unique_values[1].cpu().tolist()): if sum_num + count < num_chunks or sum_num == 0: sum_num += count input_grid[0].append(grid_index) input_grid[1].append(count) else: processor.topk = input_grid logits_grid = geo_decoder(queries=next_points[:, start_num:start_num + sum_num], latents=latents) start_num = start_num + sum_num logits_grid_list.append(logits_grid) input_grid = [[grid_index], [count]] sum_num = count if sum_num > 0: processor.topk = input_grid logits_grid = geo_decoder(queries=next_points[:, start_num:start_num + sum_num], latents=latents) logits_grid_list.append(logits_grid) logits_grid = torch.cat(logits_grid_list, dim=1) grid_logits[index.indices] = logits_grid.squeeze(0).squeeze(-1) next_logits[nidx] = grid_logits grid_logits = next_logits.unsqueeze(0)
grid_logits[grid_logits == -10000.] = float('nan')
return grid_logits123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257# Open Source Model Licensed under the Apache License Version 2.0# and Other Licenses of the Third-Party Components therein:# The below Model in this distribution may have been modified by THL A29 Limited# ("Tencent Modifications"). All Tencent Modifications are Copyright (C) 2024 THL A29 Limited.
# Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.# The below software and/or models in this distribution may have been# modified by THL A29 Limited ("Tencent Modifications").# All Tencent Modifications are Copyright (C) THL A29 Limited.
# Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT# except for the third-party components listed below.# Hunyuan 3D does not impose any additional limitations beyond what is outlined# in the repsective licenses of these third-party components.# Users must comply with all terms and conditions of original licenses of these third-party# components and must ensure that the usage of the third party components adheres to# all relevant laws and regulations.
# For avoidance of doubts, Hunyuan 3D means the large language models and# their software and algorithms, including trained model weights, parameters (including# optimizer states), machine-learning model code, inference-enabling code, training-enabling code,# fine-tuning enabling code and other elements of the foregoing made publicly available# by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.
import numpy as npimport torchimport torch.nn as nnfrom torchvision import transformsfrom transformers import ( CLIPVisionModelWithProjection, CLIPVisionConfig, Dinov2Model, Dinov2Config,)
def get_1d_sincos_pos_embed_from_grid(embed_dim, pos): """ embed_dim: output dimension for each position pos: a list of positions to be encoded: size (M,) out: (M, D) """ assert embed_dim % 2 == 0 omega = np.arange(embed_dim // 2, dtype=np.float64) omega /= embed_dim / 2. omega = 1. / 10000 ** omega # (D/2,)
pos = pos.reshape(-1) # (M,) out = np.einsum('m,d->md', pos, omega) # (M, D/2), outer product
emb_sin = np.sin(out) # (M, D/2) emb_cos = np.cos(out) # (M, D/2)
return np.concatenate([emb_sin, emb_cos], axis=1)
class ImageEncoder(nn.Module): def __init__( self, version=None, config=None, use_cls_token=True, image_size=224, **kwargs, ): super().__init__()
if config is None: self.model = self.MODEL_CLASS.from_pretrained(version) else: self.model = self.MODEL_CLASS(self.MODEL_CONFIG_CLASS.from_dict(config)) self.model.eval() self.model.requires_grad_(False) self.use_cls_token = use_cls_token self.size = image_size // 14 self.num_patches = (image_size // 14) ** 2 if self.use_cls_token: self.num_patches += 1
self.transform = transforms.Compose( [ transforms.Resize(image_size, transforms.InterpolationMode.BILINEAR, antialias=True), transforms.CenterCrop(image_size), transforms.Normalize( mean=self.mean, std=self.std, ), ] )
def forward(self, image, mask=None, value_range=(-1, 1), **kwargs): if value_range is not None: low, high = value_range image = (image - low) / (high - low)
image = image.to(self.model.device, dtype=self.model.dtype) inputs = self.transform(image) outputs = self.model(inputs)
last_hidden_state = outputs.last_hidden_state if not self.use_cls_token: last_hidden_state = last_hidden_state[:, 1:, :]
return last_hidden_state
def unconditional_embedding(self, batch_size, **kwargs): device = next(self.model.parameters()).device dtype = next(self.model.parameters()).dtype zero = torch.zeros( batch_size, self.num_patches, self.model.config.hidden_size, device=device, dtype=dtype, )
return zero
class CLIPImageEncoder(ImageEncoder): MODEL_CLASS = CLIPVisionModelWithProjection MODEL_CONFIG_CLASS = CLIPVisionConfig mean = [0.48145466, 0.4578275, 0.40821073] std = [0.26862954, 0.26130258, 0.27577711]
class DinoImageEncoder(ImageEncoder): MODEL_CLASS = Dinov2Model MODEL_CONFIG_CLASS = Dinov2Config mean = [0.485, 0.456, 0.406] std = [0.229, 0.224, 0.225]
class DinoImageEncoderMV(DinoImageEncoder): def __init__( self, version=None, config=None, use_cls_token=True, image_size=224, view_num=4, **kwargs, ): super().__init__(version, config, use_cls_token, image_size, **kwargs) self.view_num = view_num self.num_patches = self.num_patches pos = np.arange(self.view_num, dtype=np.float32) view_embedding = torch.from_numpy( get_1d_sincos_pos_embed_from_grid(self.model.config.hidden_size, pos)).float()
view_embedding = view_embedding.unsqueeze(1).repeat(1, self.num_patches, 1) self.view_embed = view_embedding.unsqueeze(0)
def forward(self, image, mask=None, value_range=(-1, 1), view_idxs=None): if value_range is not None: low, high = value_range image = (image - low) / (high - low)
image = image.to(self.model.device, dtype=self.model.dtype)
bs, num_views, c, h, w = image.shape image = image.view(bs * num_views, c, h, w)
inputs = self.transform(image) outputs = self.model(inputs)
last_hidden_state = outputs.last_hidden_state last_hidden_state = last_hidden_state.view( bs, num_views, last_hidden_state.shape[-2], last_hidden_state.shape[-1] )
view_embedding = self.view_embed.to(last_hidden_state.dtype).to(last_hidden_state.device) if view_idxs is not None: assert len(view_idxs) == bs view_embeddings = [] for i in range(bs): view_idx = view_idxs[i] assert num_views == len(view_idx) view_embeddings.append(self.view_embed[:, view_idx, ...]) view_embedding = torch.cat(view_embeddings, 0).to(last_hidden_state.dtype).to(last_hidden_state.device)
if num_views != self.view_num: view_embedding = view_embedding[:, :num_views, ...] last_hidden_state = last_hidden_state + view_embedding last_hidden_state = last_hidden_state.view(bs, num_views * last_hidden_state.shape[-2], last_hidden_state.shape[-1]) return last_hidden_state
def unconditional_embedding(self, batch_size, view_idxs=None, **kwargs): device = next(self.model.parameters()).device dtype = next(self.model.parameters()).dtype zero = torch.zeros( batch_size, self.num_patches * len(view_idxs[0]), self.model.config.hidden_size, device=device, dtype=dtype, ) return zero
def build_image_encoder(config): if config['type'] == 'CLIPImageEncoder': return CLIPImageEncoder(**config['kwargs']) elif config['type'] == 'DinoImageEncoder': return DinoImageEncoder(**config['kwargs']) elif config['type'] == 'DinoImageEncoderMV': return DinoImageEncoderMV(**config['kwargs']) else: raise ValueError(f'Unknown image encoder type: {config["type"]}')
class DualImageEncoder(nn.Module): def __init__( self, main_image_encoder, additional_image_encoder, ): super().__init__() self.main_image_encoder = build_image_encoder(main_image_encoder) self.additional_image_encoder = build_image_encoder(additional_image_encoder)
def forward(self, image, mask=None, **kwargs): outputs = { 'main': self.main_image_encoder(image, mask=mask, **kwargs), 'additional': self.additional_image_encoder(image, mask=mask, **kwargs), } return outputs
def unconditional_embedding(self, batch_size, **kwargs): outputs = { 'main': self.main_image_encoder.unconditional_embedding(batch_size, **kwargs), 'additional': self.additional_image_encoder.unconditional_embedding(batch_size, **kwargs), } return outputs
class SingleImageEncoder(nn.Module): def __init__( self, main_image_encoder, ): super().__init__() self.main_image_encoder = build_image_encoder(main_image_encoder)
def forward(self, image, mask=None, **kwargs): outputs = { 'main': self.main_image_encoder(image, mask=mask, **kwargs), } return outputs
def unconditional_embedding(self, batch_size, **kwargs): outputs = { 'main': self.main_image_encoder.unconditional_embedding(batch_size, **kwargs), } return outputs123456789101112131415# Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT# except for the third-party components listed below.# Hunyuan 3D does not impose any additional limitations beyond what is outlined# in the repsective licenses of these third-party components.# Users must comply with all terms and conditions of original licenses of these third-party# components and must ensure that the usage of the third party components adheres to# all relevant laws and regulations.
# For avoidance of doubts, Hunyuan 3D means the large language models and# their software and algorithms, including trained model weights, parameters (including# optimizer states), machine-learning model code, inference-enabling code, training-enabling code,# fine-tuning enabling code and other elements of the foregoing made publicly available# by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.
from .hunyuan3ddit import Hunyuan3DDiT123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404# Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT# except for the third-party components listed below.# Hunyuan 3D does not impose any additional limitations beyond what is outlined# in the repsective licenses of these third-party components.# Users must comply with all terms and conditions of original licenses of these third-party# components and must ensure that the usage of the third party components adheres to# all relevant laws and regulations.
# For avoidance of doubts, Hunyuan 3D means the large language models and# their software and algorithms, including trained model weights, parameters (including# optimizer states), machine-learning model code, inference-enabling code, training-enabling code,# fine-tuning enabling code and other elements of the foregoing made publicly available# by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.
import osimport mathfrom dataclasses import dataclassfrom typing import List, Tuple, Optional
import torchfrom torch import Tensor, nnfrom einops import rearrange
# set up attention backendscaled_dot_product_attention = nn.functional.scaled_dot_product_attentionif os.environ.get('USE_SAGEATTN', '0') == '1': try: from sageattention import sageattn except ImportError: raise ImportError('Please install the package "sageattention" to use this USE_SAGEATTN.') scaled_dot_product_attention = sageattn
def attention(q: Tensor, k: Tensor, v: Tensor, **kwargs) -> Tensor: x = scaled_dot_product_attention(q, k, v) x = rearrange(x, "B H L D -> B L (H D)") return x
def timestep_embedding(t: Tensor, dim, max_period=10000, time_factor: float = 1000.0): """ Create sinusoidal timestep embeddings. :param t: a 1-D Tensor of N indices, one per batch element. These may be fractional. :param dim: the dimension of the output. :param max_period: controls the minimum frequency of the embeddings. :return: an (N, D) Tensor of positional embeddings. """ t = time_factor * t half = dim // 2 freqs = torch.exp(-math.log(max_period) * torch.arange(start=0, end=half, dtype=torch.float32) / half) freqs = freqs.to(t.device)
args = t[:, None].float() * freqs[None] embedding = torch.cat([torch.cos(args), torch.sin(args)], dim=-1) if dim % 2: embedding = torch.cat([embedding, torch.zeros_like(embedding[:, :1])], dim=-1) if torch.is_floating_point(t): embedding = embedding.to(t) return embedding
class GELU(nn.Module): def __init__(self, approximate='tanh'): super().__init__() self.approximate = approximate
def forward(self, x: Tensor) -> Tensor: return nn.functional.gelu(x.contiguous(), approximate=self.approximate)
class MLPEmbedder(nn.Module): def __init__(self, in_dim: int, hidden_dim: int): super().__init__() self.in_layer = nn.Linear(in_dim, hidden_dim, bias=True) self.silu = nn.SiLU() self.out_layer = nn.Linear(hidden_dim, hidden_dim, bias=True)
def forward(self, x: Tensor) -> Tensor: return self.out_layer(self.silu(self.in_layer(x)))
class RMSNorm(torch.nn.Module): def __init__(self, dim: int): super().__init__() self.scale = nn.Parameter(torch.ones(dim))
def forward(self, x: Tensor): x_dtype = x.dtype x = x.float() rrms = torch.rsqrt(torch.mean(x ** 2, dim=-1, keepdim=True) + 1e-6) return (x * rrms).to(dtype=x_dtype) * self.scale
class QKNorm(torch.nn.Module): def __init__(self, dim: int): super().__init__() self.query_norm = RMSNorm(dim) self.key_norm = RMSNorm(dim)
def forward(self, q: Tensor, k: Tensor, v: Tensor) -> Tuple[Tensor, Tensor]: q = self.query_norm(q) k = self.key_norm(k) return q.to(v), k.to(v)
class SelfAttention(nn.Module): def __init__( self, dim: int, num_heads: int = 8, qkv_bias: bool = False, ): super().__init__() self.num_heads = num_heads head_dim = dim // num_heads
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias) self.norm = QKNorm(head_dim) self.proj = nn.Linear(dim, dim)
def forward(self, x: Tensor, pe: Tensor) -> Tensor: qkv = self.qkv(x) q, k, v = rearrange(qkv, "B L (K H D) -> K B H L D", K=3, H=self.num_heads) q, k = self.norm(q, k, v) x = attention(q, k, v, pe=pe) x = self.proj(x) return x
@dataclassclass ModulationOut: shift: Tensor scale: Tensor gate: Tensor
class Modulation(nn.Module): def __init__(self, dim: int, double: bool): super().__init__() self.is_double = double self.multiplier = 6 if double else 3 self.lin = nn.Linear(dim, self.multiplier * dim, bias=True)
def forward(self, vec: Tensor) -> Tuple[ModulationOut, Optional[ModulationOut]]: out = self.lin(nn.functional.silu(vec))[:, None, :] out = out.chunk(self.multiplier, dim=-1)
return ( ModulationOut(*out[:3]), ModulationOut(*out[3:]) if self.is_double else None, )
class DoubleStreamBlock(nn.Module): def __init__( self, hidden_size: int, num_heads: int, mlp_ratio: float, qkv_bias: bool = False, ): super().__init__() mlp_hidden_dim = int(hidden_size * mlp_ratio) self.num_heads = num_heads self.hidden_size = hidden_size self.img_mod = Modulation(hidden_size, double=True) self.img_norm1 = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6) self.img_attn = SelfAttention(dim=hidden_size, num_heads=num_heads, qkv_bias=qkv_bias)
self.img_norm2 = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6) self.img_mlp = nn.Sequential( nn.Linear(hidden_size, mlp_hidden_dim, bias=True), GELU(approximate="tanh"), nn.Linear(mlp_hidden_dim, hidden_size, bias=True), )
self.txt_mod = Modulation(hidden_size, double=True) self.txt_norm1 = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6) self.txt_attn = SelfAttention(dim=hidden_size, num_heads=num_heads, qkv_bias=qkv_bias)
self.txt_norm2 = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6) self.txt_mlp = nn.Sequential( nn.Linear(hidden_size, mlp_hidden_dim, bias=True), GELU(approximate="tanh"), nn.Linear(mlp_hidden_dim, hidden_size, bias=True), )
def forward(self, img: Tensor, txt: Tensor, vec: Tensor, pe: Tensor) -> Tuple[Tensor, Tensor]: img_mod1, img_mod2 = self.img_mod(vec) txt_mod1, txt_mod2 = self.txt_mod(vec)
img_modulated = self.img_norm1(img) img_modulated = (1 + img_mod1.scale) * img_modulated + img_mod1.shift img_qkv = self.img_attn.qkv(img_modulated) img_q, img_k, img_v = rearrange(img_qkv, "B L (K H D) -> K B H L D", K=3, H=self.num_heads) img_q, img_k = self.img_attn.norm(img_q, img_k, img_v)
txt_modulated = self.txt_norm1(txt) txt_modulated = (1 + txt_mod1.scale) * txt_modulated + txt_mod1.shift txt_qkv = self.txt_attn.qkv(txt_modulated) txt_q, txt_k, txt_v = rearrange(txt_qkv, "B L (K H D) -> K B H L D", K=3, H=self.num_heads) txt_q, txt_k = self.txt_attn.norm(txt_q, txt_k, txt_v)
q = torch.cat((txt_q, img_q), dim=2) k = torch.cat((txt_k, img_k), dim=2) v = torch.cat((txt_v, img_v), dim=2)
attn = attention(q, k, v, pe=pe) txt_attn, img_attn = attn[:, : txt.shape[1]], attn[:, txt.shape[1]:]
img = img + img_mod1.gate * self.img_attn.proj(img_attn) img = img + img_mod2.gate * self.img_mlp((1 + img_mod2.scale) * self.img_norm2(img) + img_mod2.shift)
txt = txt + txt_mod1.gate * self.txt_attn.proj(txt_attn) txt = txt + txt_mod2.gate * self.txt_mlp((1 + txt_mod2.scale) * self.txt_norm2(txt) + txt_mod2.shift) return img, txt
class SingleStreamBlock(nn.Module): """ A DiT block with parallel linear layers as described in https://arxiv.org/abs/2302.05442 and adapted modulation interface. """
def __init__( self, hidden_size: int, num_heads: int, mlp_ratio: float = 4.0, qk_scale: Optional[float] = None, ): super().__init__()
self.hidden_dim = hidden_size self.num_heads = num_heads head_dim = hidden_size // num_heads self.scale = qk_scale or head_dim ** -0.5
self.mlp_hidden_dim = int(hidden_size * mlp_ratio) # qkv and mlp_in self.linear1 = nn.Linear(hidden_size, hidden_size * 3 + self.mlp_hidden_dim) # proj and mlp_out self.linear2 = nn.Linear(hidden_size + self.mlp_hidden_dim, hidden_size)
self.norm = QKNorm(head_dim)
self.hidden_size = hidden_size self.pre_norm = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6)
self.mlp_act = GELU(approximate="tanh") self.modulation = Modulation(hidden_size, double=False)
def forward(self, x: Tensor, vec: Tensor, pe: Tensor) -> Tensor: mod, _ = self.modulation(vec)
x_mod = (1 + mod.scale) * self.pre_norm(x) + mod.shift qkv, mlp = torch.split(self.linear1(x_mod), [3 * self.hidden_size, self.mlp_hidden_dim], dim=-1)
q, k, v = rearrange(qkv, "B L (K H D) -> K B H L D", K=3, H=self.num_heads) q, k = self.norm(q, k, v)
# compute attention attn = attention(q, k, v, pe=pe) # compute activation in mlp stream, cat again and run second linear layer output = self.linear2(torch.cat((attn, self.mlp_act(mlp)), 2)) return x + mod.gate * output
class LastLayer(nn.Module): def __init__(self, hidden_size: int, patch_size: int, out_channels: int): super().__init__() self.norm_final = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6) self.linear = nn.Linear(hidden_size, patch_size * patch_size * out_channels, bias=True) self.adaLN_modulation = nn.Sequential(nn.SiLU(), nn.Linear(hidden_size, 2 * hidden_size, bias=True))
def forward(self, x: Tensor, vec: Tensor) -> Tensor: shift, scale = self.adaLN_modulation(vec).chunk(2, dim=1) x = (1 + scale[:, None, :]) * self.norm_final(x) + shift[:, None, :] x = self.linear(x) return x
class Hunyuan3DDiT(nn.Module): def __init__( self, in_channels: int = 64, context_in_dim: int = 1536, hidden_size: int = 1024, mlp_ratio: float = 4.0, num_heads: int = 16, depth: int = 16, depth_single_blocks: int = 32, axes_dim: List[int] = [64], theta: int = 10_000, qkv_bias: bool = True, time_factor: float = 1000, guidance_embed: bool = False, ckpt_path: Optional[str] = None, **kwargs, ): super().__init__() self.in_channels = in_channels self.context_in_dim = context_in_dim self.hidden_size = hidden_size self.mlp_ratio = mlp_ratio self.num_heads = num_heads self.depth = depth self.depth_single_blocks = depth_single_blocks self.axes_dim = axes_dim self.theta = theta self.qkv_bias = qkv_bias self.time_factor = time_factor self.out_channels = self.in_channels self.guidance_embed = guidance_embed
if hidden_size % num_heads != 0: raise ValueError( f"Hidden size {hidden_size} must be divisible by num_heads {num_heads}" ) pe_dim = hidden_size // num_heads if sum(axes_dim) != pe_dim: raise ValueError(f"Got {axes_dim} but expected positional dim {pe_dim}") self.hidden_size = hidden_size self.num_heads = num_heads self.latent_in = nn.Linear(self.in_channels, self.hidden_size, bias=True) self.time_in = MLPEmbedder(in_dim=256, hidden_dim=self.hidden_size) self.cond_in = nn.Linear(context_in_dim, self.hidden_size) self.guidance_in = ( MLPEmbedder(in_dim=256, hidden_dim=self.hidden_size) if guidance_embed else nn.Identity() )
self.double_blocks = nn.ModuleList( [ DoubleStreamBlock( self.hidden_size, self.num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias, ) for _ in range(depth) ] )
self.single_blocks = nn.ModuleList( [ SingleStreamBlock( self.hidden_size, self.num_heads, mlp_ratio=mlp_ratio, ) for _ in range(depth_single_blocks) ] )
self.final_layer = LastLayer(self.hidden_size, 1, self.out_channels)
if ckpt_path is not None: print('restored denoiser ckpt', ckpt_path)
ckpt = torch.load(ckpt_path, map_location="cpu") if 'state_dict' not in ckpt: # deepspeed ckpt state_dict = {} for k in ckpt.keys(): new_k = k.replace('_forward_module.', '') state_dict[new_k] = ckpt[k] else: state_dict = ckpt["state_dict"]
final_state_dict = {} for k, v in state_dict.items(): if k.startswith('model.'): final_state_dict[k.replace('model.', '')] = v else: final_state_dict[k] = v missing, unexpected = self.load_state_dict(final_state_dict, strict=False) print('unexpected keys:', unexpected) print('missing keys:', missing)
def forward(self, x, t, contexts, **kwargs) -> Tensor: cond = contexts['main'] latent = self.latent_in(x)
vec = self.time_in(timestep_embedding(t, 256, self.time_factor).to(dtype=latent.dtype)) if self.guidance_embed: guidance = kwargs.get('guidance', None) if guidance is None: raise ValueError("Didn't get guidance strength for guidance distilled model.") vec = vec + self.guidance_in(timestep_embedding(guidance, 256, self.time_factor))
cond = self.cond_in(cond) pe = None
for block in self.double_blocks: latent, cond = block(img=latent, txt=cond, vec=vec, pe=pe)
latent = torch.cat((cond, latent), 1) for block in self.single_blocks: latent = block(latent, vec=vec, pe=pe)
latent = latent[:, cond.shape[1]:, ...] latent = self.final_layer(latent, vec) return latent123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596# Open Source Model Licensed under the Apache License Version 2.0# and Other Licenses of the Third-Party Components therein:# The below Model in this distribution may have been modified by THL A29 Limited# ("Tencent Modifications"). All Tencent Modifications are Copyright (C) 2024 THL A29 Limited.
# Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.# The below software and/or models in this distribution may have been# modified by THL A29 Limited ("Tencent Modifications").# All Tencent Modifications are Copyright (C) THL A29 Limited.
# Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT# except for the third-party components listed below.# Hunyuan 3D does not impose any additional limitations beyond what is outlined# in the repsective licenses of these third-party components.# Users must comply with all terms and conditions of original licenses of these third-party# components and must ensure that the usage of the third party components adheres to# all relevant laws and regulations.
# For avoidance of doubts, Hunyuan 3D means the large language models and# their software and algorithms, including trained model weights, parameters (including# optimizer states), machine-learning model code, inference-enabling code, training-enabling code,# fine-tuning enabling code and other elements of the foregoing made publicly available# by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.
import math
import numpy as npimport torchimport torch.nn as nnimport torch.nn.functional as Ffrom einops import rearrange
from .moe_layers import MoEBlock
def modulate(x, shift, scale): return x * (1 + scale.unsqueeze(1)) + shift.unsqueeze(1)
def get_1d_sincos_pos_embed_from_grid(embed_dim, pos): """ embed_dim: output dimension for each position pos: a list of positions to be encoded: size (M,) out: (M, D) """ assert embed_dim % 2 == 0 omega = np.arange(embed_dim // 2, dtype=np.float64) omega /= embed_dim / 2. omega = 1. / 10000 ** omega # (D/2,)
pos = pos.reshape(-1) # (M,) out = np.einsum('m,d->md', pos, omega) # (M, D/2), outer product
emb_sin = np.sin(out) # (M, D/2) emb_cos = np.cos(out) # (M, D/2)
return np.concatenate([emb_sin, emb_cos], axis=1)
class Timesteps(nn.Module): def __init__(self, num_channels: int, downscale_freq_shift: float = 0.0, scale: int = 1, max_period: int = 10000 ): super().__init__() self.num_channels = num_channels self.downscale_freq_shift = downscale_freq_shift self.scale = scale self.max_period = max_period
def forward(self, timesteps): assert len(timesteps.shape) == 1, "Timesteps should be a 1d-array" embedding_dim = self.num_channels half_dim = embedding_dim // 2 exponent = -math.log(self.max_period) * torch.arange( start=0, end=half_dim, dtype=torch.float32, device=timesteps.device) exponent = exponent / (half_dim - self.downscale_freq_shift) emb = torch.exp(exponent) emb = timesteps[:, None].float() * emb[None, :] emb = self.scale * emb emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=-1) if embedding_dim % 2 == 1: emb = torch.nn.functional.pad(emb, (0, 1, 0, 0)) return emb
class TimestepEmbedder(nn.Module): """ Embeds scalar timesteps into vector representations. """
def __init__(self, hidden_size, frequency_embedding_size=256, cond_proj_dim=None, out_size=None): super().__init__() if out_size is None: out_size = hidden_size self.mlp = nn.Sequential( nn.Linear(hidden_size, frequency_embedding_size, bias=True), nn.GELU(), nn.Linear(frequency_embedding_size, out_size, bias=True), ) self.frequency_embedding_size = frequency_embedding_size
if cond_proj_dim is not None: self.cond_proj = nn.Linear(cond_proj_dim, frequency_embedding_size, bias=False)
self.time_embed = Timesteps(hidden_size)
def forward(self, t, condition):
t_freq = self.time_embed(t).type(self.mlp[0].weight.dtype)
# t_freq = timestep_embedding(t, self.frequency_embedding_size).type(self.mlp[0].weight.dtype) if condition is not None: t_freq = t_freq + self.cond_proj(condition)
t = self.mlp(t_freq) t = t.unsqueeze(dim=1) return t
class MLP(nn.Module): def __init__(self, *, width: int): super().__init__() self.width = width self.fc1 = nn.Linear(width, width * 4) self.fc2 = nn.Linear(width * 4, width) self.gelu = nn.GELU()
def forward(self, x): return self.fc2(self.gelu(self.fc1(x)))
class CrossAttention(nn.Module): def __init__( self, qdim, kdim, num_heads, qkv_bias=True, qk_norm=False, norm_layer=nn.LayerNorm, with_decoupled_ca=False, decoupled_ca_dim=16, decoupled_ca_weight=1.0, **kwargs, ): super().__init__() self.qdim = qdim self.kdim = kdim self.num_heads = num_heads assert self.qdim % num_heads == 0, "self.qdim must be divisible by num_heads" self.head_dim = self.qdim // num_heads assert self.head_dim % 8 == 0 and self.head_dim <= 128, "Only support head_dim <= 128 and divisible by 8" self.scale = self.head_dim ** -0.5
self.to_q = nn.Linear(qdim, qdim, bias=qkv_bias) self.to_k = nn.Linear(kdim, qdim, bias=qkv_bias) self.to_v = nn.Linear(kdim, qdim, bias=qkv_bias)
# TODO: eps should be 1 / 65530 if using fp16 self.q_norm = norm_layer(self.head_dim, elementwise_affine=True, eps=1e-6) if qk_norm else nn.Identity() self.k_norm = norm_layer(self.head_dim, elementwise_affine=True, eps=1e-6) if qk_norm else nn.Identity() self.out_proj = nn.Linear(qdim, qdim, bias=True)
self.with_dca = with_decoupled_ca if self.with_dca: self.kv_proj_dca = nn.Linear(kdim, 2 * qdim, bias=qkv_bias) self.k_norm_dca = norm_layer(self.head_dim, elementwise_affine=True, eps=1e-6) if qk_norm else nn.Identity() self.dca_dim = decoupled_ca_dim self.dca_weight = decoupled_ca_weight
def forward(self, x, y): """ Parameters ---------- x: torch.Tensor (batch, seqlen1, hidden_dim) (where hidden_dim = num heads * head dim) y: torch.Tensor (batch, seqlen2, hidden_dim2) freqs_cis_img: torch.Tensor (batch, hidden_dim // 2), RoPE for image """ b, s1, c = x.shape # [b, s1, D]
if self.with_dca: token_len = y.shape[1] context_dca = y[:, -self.dca_dim:, :] kv_dca = self.kv_proj_dca(context_dca).view(b, self.dca_dim, 2, self.num_heads, self.head_dim) k_dca, v_dca = kv_dca.unbind(dim=2) # [b, s, h, d] k_dca = self.k_norm_dca(k_dca) y = y[:, :(token_len - self.dca_dim), :]
_, s2, c = y.shape # [b, s2, 1024] q = self.to_q(x) k = self.to_k(y) v = self.to_v(y)
kv = torch.cat((k, v), dim=-1) split_size = kv.shape[-1] // self.num_heads // 2 kv = kv.view(1, -1, self.num_heads, split_size * 2) k, v = torch.split(kv, split_size, dim=-1)
q = q.view(b, s1, self.num_heads, self.head_dim) # [b, s1, h, d] k = k.view(b, s2, self.num_heads, self.head_dim) # [b, s2, h, d] v = v.view(b, s2, self.num_heads, self.head_dim) # [b, s2, h, d]
q = self.q_norm(q) k = self.k_norm(k)
with torch.backends.cuda.sdp_kernel( enable_flash=True, enable_math=False, enable_mem_efficient=True ): q, k, v = map(lambda t: rearrange(t, 'b n h d -> b h n d', h=self.num_heads), (q, k, v)) context = F.scaled_dot_product_attention( q, k, v ).transpose(1, 2).reshape(b, s1, -1)
if self.with_dca: with torch.backends.cuda.sdp_kernel( enable_flash=True, enable_math=False, enable_mem_efficient=True ): k_dca, v_dca = map(lambda t: rearrange(t, 'b n h d -> b h n d', h=self.num_heads), (k_dca, v_dca)) context_dca = F.scaled_dot_product_attention( q, k_dca, v_dca).transpose(1, 2).reshape(b, s1, -1)
context = context + self.dca_weight * context_dca
out = self.out_proj(context) # context.reshape - B, L1, -1
return out
class Attention(nn.Module): """ We rename some layer names to align with flash attention """
def __init__( self, dim, num_heads, qkv_bias=True, qk_norm=False, norm_layer=nn.LayerNorm, ): super().__init__() self.dim = dim self.num_heads = num_heads assert self.dim % num_heads == 0, 'dim should be divisible by num_heads' self.head_dim = self.dim // num_heads # This assertion is aligned with flash attention assert self.head_dim % 8 == 0 and self.head_dim <= 128, "Only support head_dim <= 128 and divisible by 8" self.scale = self.head_dim ** -0.5
self.to_q = nn.Linear(dim, dim, bias=qkv_bias) self.to_k = nn.Linear(dim, dim, bias=qkv_bias) self.to_v = nn.Linear(dim, dim, bias=qkv_bias) # TODO: eps should be 1 / 65530 if using fp16 self.q_norm = norm_layer(self.head_dim, elementwise_affine=True, eps=1e-6) if qk_norm else nn.Identity() self.k_norm = norm_layer(self.head_dim, elementwise_affine=True, eps=1e-6) if qk_norm else nn.Identity() self.out_proj = nn.Linear(dim, dim)
def forward(self, x): B, N, C = x.shape
q = self.to_q(x) k = self.to_k(x) v = self.to_v(x)
qkv = torch.cat((q, k, v), dim=-1) split_size = qkv.shape[-1] // self.num_heads // 3 qkv = qkv.view(1, -1, self.num_heads, split_size * 3) q, k, v = torch.split(qkv, split_size, dim=-1)
q = q.reshape(B, N, self.num_heads, self.head_dim).transpose(1, 2) # [b, h, s, d] k = k.reshape(B, N, self.num_heads, self.head_dim).transpose(1, 2) # [b, h, s, d] v = v.reshape(B, N, self.num_heads, self.head_dim).transpose(1, 2)
q = self.q_norm(q) # [b, h, s, d] k = self.k_norm(k) # [b, h, s, d]
with torch.backends.cuda.sdp_kernel( enable_flash=True, enable_math=False, enable_mem_efficient=True ): x = F.scaled_dot_product_attention(q, k, v) x = x.transpose(1, 2).reshape(B, N, -1)
x = self.out_proj(x) return x
class HunYuanDiTBlock(nn.Module): def __init__( self, hidden_size, c_emb_size, num_heads, text_states_dim=1024, use_flash_attn=False, qk_norm=False, norm_layer=nn.LayerNorm, qk_norm_layer=nn.RMSNorm, with_decoupled_ca=False, decoupled_ca_dim=16, decoupled_ca_weight=1.0, init_scale=1.0, qkv_bias=True, skip_connection=True, timested_modulate=False, use_moe: bool = False, num_experts: int = 8, moe_top_k: int = 2, **kwargs, ): super().__init__() self.use_flash_attn = use_flash_attn use_ele_affine = True
# ========================= Self-Attention ========================= self.norm1 = norm_layer(hidden_size, elementwise_affine=use_ele_affine, eps=1e-6) self.attn1 = Attention(hidden_size, num_heads=num_heads, qkv_bias=qkv_bias, qk_norm=qk_norm, norm_layer=qk_norm_layer)
# ========================= FFN ========================= self.norm2 = norm_layer(hidden_size, elementwise_affine=use_ele_affine, eps=1e-6)
# ========================= Add ========================= # Simply use add like SDXL. self.timested_modulate = timested_modulate if self.timested_modulate: self.default_modulation = nn.Sequential( nn.SiLU(), nn.Linear(c_emb_size, hidden_size, bias=True) )
# ========================= Cross-Attention ========================= self.attn2 = CrossAttention(hidden_size, text_states_dim, num_heads=num_heads, qkv_bias=qkv_bias, qk_norm=qk_norm, norm_layer=qk_norm_layer, with_decoupled_ca=with_decoupled_ca, decoupled_ca_dim=decoupled_ca_dim, decoupled_ca_weight=decoupled_ca_weight, init_scale=init_scale, ) self.norm3 = norm_layer(hidden_size, elementwise_affine=True, eps=1e-6)
if skip_connection: self.skip_norm = norm_layer(hidden_size, elementwise_affine=True, eps=1e-6) self.skip_linear = nn.Linear(2 * hidden_size, hidden_size) else: self.skip_linear = None
self.use_moe = use_moe if self.use_moe: print("using moe") self.moe = MoEBlock( hidden_size, num_experts=num_experts, moe_top_k=moe_top_k, dropout=0.0, activation_fn="gelu", final_dropout=False, ff_inner_dim=int(hidden_size * 4.0), ff_bias=True, ) else: self.mlp = MLP(width=hidden_size)
def forward(self, x, c=None, text_states=None, skip_value=None):
if self.skip_linear is not None: cat = torch.cat([skip_value, x], dim=-1) x = self.skip_linear(cat) x = self.skip_norm(x)
# Self-Attention if self.timested_modulate: shift_msa = self.default_modulation(c).unsqueeze(dim=1) x = x + shift_msa
attn_out = self.attn1(self.norm1(x))
x = x + attn_out
# Cross-Attention x = x + self.attn2(self.norm2(x), text_states)
# FFN Layer mlp_inputs = self.norm3(x)
if self.use_moe: x = x + self.moe(mlp_inputs) else: x = x + self.mlp(mlp_inputs)
return x
class AttentionPool(nn.Module): def __init__(self, spacial_dim: int, embed_dim: int, num_heads: int, output_dim: int = None): super().__init__() self.positional_embedding = nn.Parameter(torch.randn(spacial_dim + 1, embed_dim) / embed_dim ** 0.5) self.k_proj = nn.Linear(embed_dim, embed_dim) self.q_proj = nn.Linear(embed_dim, embed_dim) self.v_proj = nn.Linear(embed_dim, embed_dim) self.c_proj = nn.Linear(embed_dim, output_dim or embed_dim) self.num_heads = num_heads
def forward(self, x, attention_mask=None): x = x.permute(1, 0, 2) # NLC -> LNC if attention_mask is not None: attention_mask = attention_mask.unsqueeze(-1).permute(1, 0, 2) global_emb = (x * attention_mask).sum(dim=0) / attention_mask.sum(dim=0) x = torch.cat([global_emb[None,], x], dim=0)
else: x = torch.cat([x.mean(dim=0, keepdim=True), x], dim=0) # (L+1)NC x = x + self.positional_embedding[:, None, :].to(x.dtype) # (L+1)NC x, _ = F.multi_head_attention_forward( query=x[:1], key=x, value=x, embed_dim_to_check=x.shape[-1], num_heads=self.num_heads, q_proj_weight=self.q_proj.weight, k_proj_weight=self.k_proj.weight, v_proj_weight=self.v_proj.weight, in_proj_weight=None, in_proj_bias=torch.cat([self.q_proj.bias, self.k_proj.bias, self.v_proj.bias]), bias_k=None, bias_v=None, add_zero_attn=False, dropout_p=0, out_proj_weight=self.c_proj.weight, out_proj_bias=self.c_proj.bias, use_separate_proj_weight=True, training=self.training, need_weights=False ) return x.squeeze(0)
class FinalLayer(nn.Module): """ The final layer of HunYuanDiT. """
def __init__(self, final_hidden_size, out_channels): super().__init__() self.final_hidden_size = final_hidden_size self.norm_final = nn.LayerNorm(final_hidden_size, elementwise_affine=True, eps=1e-6) self.linear = nn.Linear(final_hidden_size, out_channels, bias=True)
def forward(self, x): x = self.norm_final(x) x = x[:, 1:] x = self.linear(x) return x
class HunYuanDiTPlain(nn.Module):
def __init__( self, input_size=1024, in_channels=4, hidden_size=1024, context_dim=1024, depth=24, num_heads=16, mlp_ratio=4.0, norm_type='layer', qk_norm_type='rms', qk_norm=False, text_len=257, with_decoupled_ca=False, additional_cond_hidden_state=768, decoupled_ca_dim=16, decoupled_ca_weight=1.0, use_pos_emb=False, use_attention_pooling=True, guidance_cond_proj_dim=None, qkv_bias=True, num_moe_layers: int = 6, num_experts: int = 8, moe_top_k: int = 2, **kwargs ): super().__init__() self.input_size = input_size self.depth = depth self.in_channels = in_channels self.out_channels = in_channels self.num_heads = num_heads
self.hidden_size = hidden_size self.norm = nn.LayerNorm if norm_type == 'layer' else nn.RMSNorm self.qk_norm = nn.RMSNorm if qk_norm_type == 'rms' else nn.LayerNorm self.context_dim = context_dim
self.with_decoupled_ca = with_decoupled_ca self.decoupled_ca_dim = decoupled_ca_dim self.decoupled_ca_weight = decoupled_ca_weight self.use_pos_emb = use_pos_emb self.use_attention_pooling = use_attention_pooling self.guidance_cond_proj_dim = guidance_cond_proj_dim
self.text_len = text_len
self.x_embedder = nn.Linear(in_channels, hidden_size, bias=True) self.t_embedder = TimestepEmbedder(hidden_size, hidden_size * 4, cond_proj_dim=guidance_cond_proj_dim)
# Will use fixed sin-cos embedding: if self.use_pos_emb: self.register_buffer("pos_embed", torch.zeros(1, input_size, hidden_size)) pos = np.arange(self.input_size, dtype=np.float32) pos_embed = get_1d_sincos_pos_embed_from_grid(self.pos_embed.shape[-1], pos) self.pos_embed.data.copy_(torch.from_numpy(pos_embed).float().unsqueeze(0))
self.use_attention_pooling = use_attention_pooling if use_attention_pooling: self.pooler = AttentionPool(self.text_len, context_dim, num_heads=8, output_dim=1024) self.extra_embedder = nn.Sequential( nn.Linear(1024, hidden_size * 4), nn.SiLU(), nn.Linear(hidden_size * 4, hidden_size, bias=True), )
if with_decoupled_ca: self.additional_cond_hidden_state = additional_cond_hidden_state self.additional_cond_proj = nn.Sequential( nn.Linear(additional_cond_hidden_state, hidden_size * 4), nn.SiLU(), nn.Linear(hidden_size * 4, 1024, bias=True), )
# HUnYuanDiT Blocks self.blocks = nn.ModuleList([ HunYuanDiTBlock(hidden_size=hidden_size, c_emb_size=hidden_size, num_heads=num_heads, mlp_ratio=mlp_ratio, text_states_dim=context_dim, qk_norm=qk_norm, norm_layer=self.norm, qk_norm_layer=self.qk_norm, skip_connection=layer > depth // 2, with_decoupled_ca=with_decoupled_ca, decoupled_ca_dim=decoupled_ca_dim, decoupled_ca_weight=decoupled_ca_weight, qkv_bias=qkv_bias, use_moe=True if depth - layer <= num_moe_layers else False, num_experts=num_experts, moe_top_k=moe_top_k ) for layer in range(depth) ]) self.depth = depth
self.final_layer = FinalLayer(hidden_size, self.out_channels)
def forward(self, x, t, contexts, **kwargs): cond = contexts['main']
t = self.t_embedder(t, condition=kwargs.get('guidance_cond')) x = self.x_embedder(x)
if self.use_pos_emb: pos_embed = self.pos_embed.to(x.dtype) x = x + pos_embed
if self.use_attention_pooling: extra_vec = self.pooler(cond, None) c = t + self.extra_embedder(extra_vec) # [B, D] else: c = t
if self.with_decoupled_ca: additional_cond = self.additional_cond_proj(contexts['additional']) cond = torch.cat([cond, additional_cond], dim=1)
x = torch.cat([c, x], dim=1)
skip_value_list = [] for layer, block in enumerate(self.blocks): skip_value = None if layer <= self.depth // 2 else skip_value_list.pop() x = block(x, c, cond, skip_value=skip_value) if layer < self.depth // 2: skip_value_list.append(x)
x = self.final_layer(x) return x123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177# Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT# except for the third-party components listed below.# Hunyuan 3D does not impose any additional limitations beyond what is outlined# in the repsective licenses of these third-party components.# Users must comply with all terms and conditions of original licenses of these third-party# components and must ensure that the usage of the third party components adheres to# all relevant laws and regulations.
# For avoidance of doubts, Hunyuan 3D means the large language models and# their software and algorithms, including trained model weights, parameters (including# optimizer states), machine-learning model code, inference-enabling code, training-enabling code,# fine-tuning enabling code and other elements of the foregoing made publicly available# by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.
import torchimport torch.nn as nnimport numpy as npimport mathfrom timm.models.vision_transformer import PatchEmbed, Attention, Mlp
import torch.nn.functional as Ffrom diffusers.models.attention import FeedForward
class AddAuxiliaryLoss(torch.autograd.Function): """ The trick function of adding auxiliary (aux) loss, which includes the gradient of the aux loss during backpropagation. """ @staticmethod def forward(ctx, x, loss): assert loss.numel() == 1 ctx.dtype = loss.dtype ctx.required_aux_loss = loss.requires_grad return x
@staticmethod def backward(ctx, grad_output): grad_loss = None if ctx.required_aux_loss: grad_loss = torch.ones(1, dtype=ctx.dtype, device=grad_output.device) return grad_output, grad_loss
class MoEGate(nn.Module): def __init__(self, embed_dim, num_experts=16, num_experts_per_tok=2, aux_loss_alpha=0.01): super().__init__() self.top_k = num_experts_per_tok self.n_routed_experts = num_experts
self.scoring_func = 'softmax' self.alpha = aux_loss_alpha self.seq_aux = False
# topk selection algorithm self.norm_topk_prob = False self.gating_dim = embed_dim self.weight = nn.Parameter(torch.empty((self.n_routed_experts, self.gating_dim))) self.reset_parameters()
def reset_parameters(self) -> None: import torch.nn.init as init init.kaiming_uniform_(self.weight, a=math.sqrt(5)) def forward(self, hidden_states): bsz, seq_len, h = hidden_states.shape # print(bsz, seq_len, h) ### compute gating score hidden_states = hidden_states.view(-1, h) logits = F.linear(hidden_states, self.weight, None) if self.scoring_func == 'softmax': scores = logits.softmax(dim=-1) else: raise NotImplementedError(f'insupportable scoring function for MoE gating: {self.scoring_func}') ### select top-k experts topk_weight, topk_idx = torch.topk(scores, k=self.top_k, dim=-1, sorted=False) ### norm gate to sum 1 if self.top_k > 1 and self.norm_topk_prob: denominator = topk_weight.sum(dim=-1, keepdim=True) + 1e-20 topk_weight = topk_weight / denominator
### expert-level computation auxiliary loss if self.training and self.alpha > 0.0: scores_for_aux = scores aux_topk = self.top_k # always compute aux loss based on the naive greedy topk method topk_idx_for_aux_loss = topk_idx.view(bsz, -1) if self.seq_aux: scores_for_seq_aux = scores_for_aux.view(bsz, seq_len, -1) ce = torch.zeros(bsz, self.n_routed_experts, device=hidden_states.device) ce.scatter_add_( 1, topk_idx_for_aux_loss, torch.ones( bsz, seq_len * aux_topk, device=hidden_states.device ) ).div_(seq_len * aux_topk / self.n_routed_experts) aux_loss = (ce * scores_for_seq_aux.mean(dim = 1)).sum(dim = 1).mean() aux_loss = aux_loss * self.alpha else: mask_ce = F.one_hot(topk_idx_for_aux_loss.view(-1), num_classes=self.n_routed_experts) ce = mask_ce.float().mean(0) Pi = scores_for_aux.mean(0) fi = ce * self.n_routed_experts aux_loss = (Pi * fi).sum() * self.alpha else: aux_loss = None return topk_idx, topk_weight, aux_loss
class MoEBlock(nn.Module): def __init__(self, dim, num_experts=8, moe_top_k=2, activation_fn = "gelu", dropout=0.0, final_dropout = False, ff_inner_dim = None, ff_bias = True): super().__init__() self.moe_top_k = moe_top_k self.experts = nn.ModuleList([ FeedForward(dim,dropout=dropout, activation_fn=activation_fn, final_dropout=final_dropout, inner_dim=ff_inner_dim, bias=ff_bias) for i in range(num_experts)]) self.gate = MoEGate(embed_dim=dim, num_experts=num_experts, num_experts_per_tok=moe_top_k)
self.shared_experts = FeedForward(dim,dropout=dropout, activation_fn=activation_fn, final_dropout=final_dropout, inner_dim=ff_inner_dim, bias=ff_bias)
def initialize_weight(self): pass def forward(self, hidden_states): identity = hidden_states orig_shape = hidden_states.shape topk_idx, topk_weight, aux_loss = self.gate(hidden_states)
hidden_states = hidden_states.view(-1, hidden_states.shape[-1]) flat_topk_idx = topk_idx.view(-1) if self.training: hidden_states = hidden_states.repeat_interleave(self.moe_top_k, dim=0) y = torch.empty_like(hidden_states, dtype=hidden_states.dtype) for i, expert in enumerate(self.experts): tmp = expert(hidden_states[flat_topk_idx == i]) y[flat_topk_idx == i] = tmp.to(hidden_states.dtype) y = (y.view(*topk_weight.shape, -1) * topk_weight.unsqueeze(-1)).sum(dim=1) y = y.view(*orig_shape) y = AddAuxiliaryLoss.apply(y, aux_loss) else: y = self.moe_infer(hidden_states, flat_topk_idx, topk_weight.view(-1, 1)).view(*orig_shape) y = y + self.shared_experts(identity) return y
@torch.no_grad() def moe_infer(self, x, flat_expert_indices, flat_expert_weights): expert_cache = torch.zeros_like(x) idxs = flat_expert_indices.argsort() tokens_per_expert = flat_expert_indices.bincount().cpu().numpy().cumsum(0) token_idxs = idxs // self.moe_top_k for i, end_idx in enumerate(tokens_per_expert): start_idx = 0 if i == 0 else tokens_per_expert[i-1] if start_idx == end_idx: continue expert = self.experts[i] exp_token_idx = token_idxs[start_idx:end_idx] expert_tokens = x[exp_token_idx] expert_out = expert(expert_tokens) expert_out.mul_(flat_expert_weights[idxs[start_idx:end_idx]]) # for fp16 and other dtype expert_cache = expert_cache.to(expert_out.dtype) expert_cache.scatter_reduce_(0, exp_token_idx.view(-1, 1).repeat(1, x.shape[-1]), expert_out, reduce='sum') return expert_cache123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354import osfrom contextlib import contextmanagerfrom typing import List, Tuple, Optional, Union
import torchimport torch.nn as nnfrom torch.optim import lr_schedulerimport pytorch_lightning as plfrom pytorch_lightning.utilities import rank_zero_infofrom pytorch_lightning.utilities import rank_zero_only
from ...utils.ema import LitEmafrom ...utils.misc import instantiate_from_config, instantiate_non_trainable_model
class Diffuser(pl.LightningModule): def __init__( self, *, first_stage_config, cond_stage_config, denoiser_cfg, scheduler_cfg, optimizer_cfg, pipeline_cfg=None, image_processor_cfg=None, lora_config=None, ema_config=None, first_stage_key: str = "surface", cond_stage_key: str = "image", scale_by_std: bool = False, z_scale_factor: float = 1.0, ckpt_path: Optional[str] = None, ignore_keys: Union[Tuple[str], List[str]] = (), torch_compile: bool = False, ): super().__init__() self.first_stage_key = first_stage_key self.cond_stage_key = cond_stage_key
# ========= init optimizer config ========= # self.optimizer_cfg = optimizer_cfg
# ========= init diffusion scheduler ========= # self.scheduler_cfg = scheduler_cfg self.sampler = None if 'transport' in scheduler_cfg: self.transport = instantiate_from_config(scheduler_cfg.transport) self.sampler = instantiate_from_config(scheduler_cfg.sampler, transport=self.transport) self.sample_fn = self.sampler.sample_ode(**scheduler_cfg.sampler.ode_params)
# ========= init the model ========= # self.denoiser_cfg = denoiser_cfg self.model = instantiate_from_config(denoiser_cfg, device=None, dtype=None) self.cond_stage_model = instantiate_from_config(cond_stage_config)
self.ckpt_path = ckpt_path if ckpt_path is not None: self.init_from_ckpt(ckpt_path, ignore_keys=ignore_keys)
# ========= config lora model ========= # if lora_config is not None: from peft import LoraConfig, get_peft_model loraconfig = LoraConfig( r=lora_config.rank, lora_alpha=lora_config.rank, target_modules=lora_config.get('target_modules') ) self.model = get_peft_model(self.model, loraconfig)
# ========= config ema model ========= # self.ema_config = ema_config if self.ema_config is not None: if self.ema_config.ema_model == 'DSEma': # from michelangelo.models.modules.ema_deepspeed import DSEma from ..utils.ema_deepspeed import DSEma self.model_ema = DSEma(self.model, decay=self.ema_config.ema_decay) else: self.model_ema = LitEma(self.model, decay=self.ema_config.ema_decay) #do not initilize EMA weight from ckpt path, since I need to change moe layers if ckpt_path is not None: self.init_from_ckpt(ckpt_path, ignore_keys=ignore_keys) print(f"Keeping EMAs of {len(list(self.model_ema.buffers()))}.")
# ========= init vae at last to prevent it is overridden by loaded ckpt ========= # self.first_stage_model = instantiate_non_trainable_model(first_stage_config)
self.scale_by_std = scale_by_std if scale_by_std: self.register_buffer("z_scale_factor", torch.tensor(z_scale_factor)) else: self.z_scale_factor = z_scale_factor
# ========= init pipeline for inference ========= # self.image_processor_cfg = image_processor_cfg self.image_processor = None if self.image_processor_cfg is not None: self.image_processor = instantiate_from_config(self.image_processor_cfg) self.pipeline_cfg = pipeline_cfg from ...schedulers import FlowMatchEulerDiscreteScheduler scheduler = FlowMatchEulerDiscreteScheduler(num_train_timesteps=1000) self.pipeline = instantiate_from_config( pipeline_cfg, vae=self.first_stage_model, model=self.model, scheduler=scheduler, # self.sampler, conditioner=self.cond_stage_model, image_processor=self.image_processor, )
# ========= torch compile to accelerate ========= # self.torch_compile = torch_compile if self.torch_compile: torch.nn.Module.compile(self.model) torch.nn.Module.compile(self.first_stage_model) torch.nn.Module.compile(self.cond_stage_model) print(f'*' * 100) print(f'Compile model for acceleration') print(f'*' * 100)
@contextmanager def ema_scope(self, context=None): if self.ema_config is not None and self.ema_config.get('ema_inference', False): self.model_ema.store(self.model) self.model_ema.copy_to(self.model) if context is not None: print(f"{context}: Switched to EMA weights") try: yield None finally: if self.ema_config is not None and self.ema_config.get('ema_inference', False): self.model_ema.restore(self.model) if context is not None: print(f"{context}: Restored training weights")
def init_from_ckpt(self, path, ignore_keys=()): ckpt = torch.load(path, map_location="cpu") if 'state_dict' not in ckpt: # deepspeed ckpt state_dict = {} for k in ckpt.keys(): new_k = k.replace('_forward_module.', '') state_dict[new_k] = ckpt[k] else: state_dict = ckpt["state_dict"]
keys = list(state_dict.keys()) for k in keys: for ik in ignore_keys: if ik in k: print("Deleting key {} from state_dict.".format(k)) del state_dict[k]
missing, unexpected = self.load_state_dict(state_dict, strict=False) print(f"Restored from {path} with {len(missing)} missing and {len(unexpected)} unexpected keys") if len(missing) > 0: print(f"Missing Keys: {missing}") print(f"Unexpected Keys: {unexpected}")
def on_load_checkpoint(self, checkpoint): """ The pt_model is trained separately, so we already have access to its checkpoint and load it separately with `self.set_pt_model`.
However, the PL Trainer is strict about checkpoint loading (not configurable), so it expects the loaded state_dict to match exactly the keys in the model state_dict.
So, when loading the checkpoint, before matching keys, we add all pt_model keys from self.state_dict() to the checkpoint state dict, so that they match """ for key in self.state_dict().keys(): if key.startswith("model_ema") and key not in checkpoint["state_dict"]: checkpoint["state_dict"][key] = self.state_dict()[key]
def configure_optimizers(self) -> Tuple[List, List]: lr = self.learning_rate
params_list = [] trainable_parameters = list(self.model.parameters()) params_list.append({'params': trainable_parameters, 'lr': lr})
no_decay = ['bias', 'norm.weight', 'norm.bias', 'norm1.weight', 'norm1.bias', 'norm2.weight', 'norm2.bias']
if self.optimizer_cfg.get('train_image_encoder', False): image_encoder_parameters = list(self.cond_stage_model.named_parameters()) image_encoder_parameters_decay = [param for name, param in image_encoder_parameters if not any((no_decay_name in name) for no_decay_name in no_decay)] image_encoder_parameters_nodecay = [param for name, param in image_encoder_parameters if any((no_decay_name in name) for no_decay_name in no_decay)] # filter trainable params image_encoder_parameters_decay = [param for param in image_encoder_parameters_decay if param.requires_grad] image_encoder_parameters_nodecay = [param for param in image_encoder_parameters_nodecay if param.requires_grad]
print(f"Image Encoder Params: {len(image_encoder_parameters_decay)} decay, ") print(f"Image Encoder Params: {len(image_encoder_parameters_nodecay)} nodecay, ")
image_encoder_lr = self.optimizer_cfg['image_encoder_lr'] image_encoder_lr_multiply = self.optimizer_cfg.get('image_encoder_lr_multiply', 1.0) image_encoder_lr = image_encoder_lr if image_encoder_lr is not None else lr * image_encoder_lr_multiply params_list.append( {'params': image_encoder_parameters_decay, 'lr': image_encoder_lr, 'weight_decay': 0.05}) params_list.append( {'params': image_encoder_parameters_nodecay, 'lr': image_encoder_lr, 'weight_decay': 0.})
optimizer = instantiate_from_config(self.optimizer_cfg.optimizer, params=params_list, lr=lr) if hasattr(self.optimizer_cfg, 'scheduler'): scheduler_func = instantiate_from_config( self.optimizer_cfg.scheduler, max_decay_steps=self.trainer.max_steps, lr_max=lr ) scheduler = { "scheduler": lr_scheduler.LambdaLR(optimizer, lr_lambda=scheduler_func.schedule), "interval": "step", "frequency": 1 } schedulers = [scheduler] else: schedulers = [] optimizers = [optimizer]
return optimizers, schedulers
@rank_zero_only @torch.no_grad() def on_train_batch_start(self, batch, batch_idx): # only for very first batch if self.scale_by_std and self.current_epoch == 0 and self.global_step == 0 \ and batch_idx == 0 and self.ckpt_path is None: # set rescale weight to 1./std of encodings print("### USING STD-RESCALING ###")
z_q = self.encode_first_stage(batch[self.first_stage_key]) z = z_q.detach()
del self.z_scale_factor self.register_buffer("z_scale_factor", 1. / z.flatten().std()) print(f"setting self.z_scale_factor to {self.z_scale_factor}")
print("### USING STD-RESCALING ###")
def on_train_batch_end(self, *args, **kwargs): if self.ema_config is not None: self.model_ema(self.model)
def on_train_epoch_start(self) -> None: pl.seed_everything(self.trainer.global_rank)
def forward(self, batch): with torch.autocast(device_type="cuda", dtype=torch.bfloat16): #float32 for text contexts = self.cond_stage_model(image=batch.get('image'), text=batch.get('text'), mask=batch.get('mask')) # t5_text = contexts['t5_text']['prompt_embeds'] # nan_count = torch.isnan(t5_text).sum() # if nan_count > 0: # print("t5_text has %d NaN values"%(nan_count)) with torch.autocast(device_type="cuda", dtype=torch.float16): with torch.no_grad(): latents = self.first_stage_model.encode(batch[self.first_stage_key], sample_posterior=True) latents = self.z_scale_factor * latents # print(latents.shape)
# check vae encode and decode is ok? answer is ok ! # import time # from hy3dshape.pipelines import export_to_trimesh # latents = 1. / self.z_scale_factor * latents # latents = self.first_stage_model(latents) # outputs = self.first_stage_model.latents2mesh( # latents, # bounds=1.01, # mc_level=0.0, # num_chunks=20000, # octree_resolution=256, # mc_algo='mc', # enable_pbar=True # ) # mesh = export_to_trimesh(outputs) # if isinstance(mesh, list): # for midx, m in enumerate(mesh): # m.export(f"check_{midx}_{time.time()}.glb") # else: # mesh.export(f"check_{time.time()}.glb") with torch.autocast(device_type="cuda", dtype=torch.bfloat16): loss = self.transport.training_losses(self.model, latents, dict(contexts=contexts))["loss"].mean() return loss
def training_step(self, batch, batch_idx, optimizer_idx=0): loss = self.forward(batch) split = 'train' loss_dict = { f"{split}/simple": loss.detach(), f"{split}/total_loss": loss.detach(), f"{split}/lr_abs": self.optimizers().param_groups[0]['lr'], } self.log_dict(loss_dict, prog_bar=True, logger=True, sync_dist=False, rank_zero_only=True)
return loss
def validation_step(self, batch, batch_idx, optimizer_idx=0): loss = self.forward(batch) split = 'val' loss_dict = { f"{split}/simple": loss.detach(), f"{split}/total_loss": loss.detach(), f"{split}/lr_abs": self.optimizers().param_groups[0]['lr'], } self.log_dict(loss_dict, prog_bar=True, logger=True, sync_dist=False, rank_zero_only=True)
return loss
@torch.no_grad() def sample(self, batch, output_type='trimesh', **kwargs): self.cond_stage_model.disable_drop = True
generator = torch.Generator().manual_seed(0)
with self.ema_scope("Sample"): with torch.amp.autocast(device_type='cuda'): try: self.pipeline.device = self.device self.pipeline.dtype = self.dtype print("### USING PIPELINE ###") print(f'device: {self.device} dtype : {self.dtype}') additional_params = {'output_type':output_type}
image = batch.get("image", None) mask = batch.get('mask', None) # if not isinstance(image, torch.Tensor): print(image.shape) # if isinstance(mask, torch.Tensor): print(mask.shape) outputs = self.pipeline(image=image, mask=mask, generator=generator, **additional_params)
except Exception as e: import traceback traceback.print_exc() print(f"Unexpected {e=}, {type(e)=}") with open("error.txt", "a") as f: f.write(str(e)) f.write(traceback.format_exc()) f.write("\n") outputs = [None] self.cond_stage_model.disable_drop = False return [outputs]12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697# This file includes code derived from the SiT project (https://github.com/willisma/SiT),# which is licensed under the MIT License.## MIT License## Copyright (c) Meta Platforms, Inc. and affiliates.## Permission is hereby granted, free of charge, to any person obtaining a copy# of this software and associated documentation files (the "Software"), to deal# in the Software without restriction, including without limitation the rights# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell# copies of the Software, and to permit persons to whom the Software is# furnished to do so, subject to the following conditions:## The above copyright notice and this permission notice shall be included in all# copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE# SOFTWARE.
from .transport import Transport, ModelType, WeightType, PathType, Sampler
def create_transport( path_type='Linear', prediction="velocity", loss_weight=None, train_eps=None, sample_eps=None, train_sample_type="uniform", mean = 0.0, std = 1.0, shift_scale = 1.0,): """function for creating Transport object **Note**: model prediction defaults to velocity Args: - path_type: type of path to use; default to linear - learn_score: set model prediction to score - learn_noise: set model prediction to noise - velocity_weighted: weight loss by velocity weight - likelihood_weighted: weight loss by likelihood weight - train_eps: small epsilon for avoiding instability during training - sample_eps: small epsilon for avoiding instability during sampling """
if prediction == "noise": model_type = ModelType.NOISE elif prediction == "score": model_type = ModelType.SCORE else: model_type = ModelType.VELOCITY
if loss_weight == "velocity": loss_type = WeightType.VELOCITY elif loss_weight == "likelihood": loss_type = WeightType.LIKELIHOOD else: loss_type = WeightType.NONE
path_choice = { "Linear": PathType.LINEAR, "GVP": PathType.GVP, "VP": PathType.VP, }
path_type = path_choice[path_type]
if (path_type in [PathType.VP]): train_eps = 1e-5 if train_eps is None else train_eps sample_eps = 1e-3 if train_eps is None else sample_eps elif (path_type in [PathType.GVP, PathType.LINEAR] and model_type != ModelType.VELOCITY): train_eps = 1e-3 if train_eps is None else train_eps sample_eps = 1e-3 if train_eps is None else sample_eps else: # velocity & [GVP, LINEAR] is stable everywhere train_eps = 0 sample_eps = 0
# create flow state state = Transport( model_type=model_type, path_type=path_type, loss_type=loss_type, train_eps=train_eps, sample_eps=sample_eps, train_sample_type=train_sample_type, mean=mean, std=std, shift_scale =shift_scale, )
return state123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142# This file includes code derived from the SiT project (https://github.com/willisma/SiT),# which is licensed under the MIT License.## MIT License## Copyright (c) Meta Platforms, Inc. and affiliates.## Permission is hereby granted, free of charge, to any person obtaining a copy# of this software and associated documentation files (the "Software"), to deal# in the Software without restriction, including without limitation the rights# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell# copies of the Software, and to permit persons to whom the Software is# furnished to do so, subject to the following conditions:## The above copyright notice and this permission notice shall be included in all# copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE# SOFTWARE.
import numpy as npimport torch as thimport torch.nn as nnfrom torchdiffeq import odeintfrom functools import partialfrom tqdm import tqdm
class sde: """SDE solver class""" def __init__( self, drift, diffusion, *, t0, t1, num_steps, sampler_type, ): assert t0 < t1, "SDE sampler has to be in forward time"
self.num_timesteps = num_steps self.t = th.linspace(t0, t1, num_steps) self.dt = self.t[1] - self.t[0] self.drift = drift self.diffusion = diffusion self.sampler_type = sampler_type
def __Euler_Maruyama_step(self, x, mean_x, t, model, **model_kwargs): w_cur = th.randn(x.size()).to(x) t = th.ones(x.size(0)).to(x) * t dw = w_cur * th.sqrt(self.dt) drift = self.drift(x, t, model, **model_kwargs) diffusion = self.diffusion(x, t) mean_x = x + drift * self.dt x = mean_x + th.sqrt(2 * diffusion) * dw return x, mean_x def __Heun_step(self, x, _, t, model, **model_kwargs): w_cur = th.randn(x.size()).to(x) dw = w_cur * th.sqrt(self.dt) t_cur = th.ones(x.size(0)).to(x) * t diffusion = self.diffusion(x, t_cur) xhat = x + th.sqrt(2 * diffusion) * dw K1 = self.drift(xhat, t_cur, model, **model_kwargs) xp = xhat + self.dt * K1 K2 = self.drift(xp, t_cur + self.dt, model, **model_kwargs) return xhat + 0.5 * self.dt * (K1 + K2), xhat # at last time point we do not perform the heun step
def __forward_fn(self): """TODO: generalize here by adding all private functions ending with steps to it""" sampler_dict = { "Euler": self.__Euler_Maruyama_step, "Heun": self.__Heun_step, }
try: sampler = sampler_dict[self.sampler_type] except: raise NotImplementedError("Smapler type not implemented.") return sampler
def sample(self, init, model, **model_kwargs): """forward loop of sde""" x = init mean_x = init samples = [] sampler = self.__forward_fn() for ti in self.t[:-1]: with th.no_grad(): x, mean_x = sampler(x, mean_x, ti, model, **model_kwargs) samples.append(x)
return samples
class ode: """ODE solver class""" def __init__( self, drift, *, t0, t1, sampler_type, num_steps, atol, rtol, ): assert t0 < t1, "ODE sampler has to be in forward time"
self.drift = drift self.t = th.linspace(t0, t1, num_steps) self.atol = atol self.rtol = rtol self.sampler_type = sampler_type
def sample(self, x, model, **model_kwargs): device = x[0].device if isinstance(x, tuple) else x.device def _fn(t, x): t = th.ones(x[0].size(0)).to(device) * t if isinstance(x, tuple) else th.ones(x.size(0)).to(device) * t model_output = self.drift(x, t, model, **model_kwargs) return model_output
t = self.t.to(device) atol = [self.atol] * len(x) if isinstance(x, tuple) else [self.atol] rtol = [self.rtol] * len(x) if isinstance(x, tuple) else [self.rtol] samples = odeint( _fn, x, t, method=self.sampler_type, atol=atol, rtol=rtol ) return samples123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220# This file includes code derived from the SiT project (https://github.com/willisma/SiT),# which is licensed under the MIT License.## MIT License## Copyright (c) Meta Platforms, Inc. and affiliates.## Permission is hereby granted, free of charge, to any person obtaining a copy# of this software and associated documentation files (the "Software"), to deal# in the Software without restriction, including without limitation the rights# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell# copies of the Software, and to permit persons to whom the Software is# furnished to do so, subject to the following conditions:## The above copyright notice and this permission notice shall be included in all# copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE# SOFTWARE.
import torch as thimport numpy as npfrom functools import partial
def expand_t_like_x(t, x): """Function to reshape time t to broadcastable dimension of x Args: t: [batch_dim,], time vector x: [batch_dim,...], data point """ dims = [1] * (len(x.size()) - 1) t = t.view(t.size(0), *dims) return t
#################### Coupling Plans ####################
class ICPlan: """Linear Coupling Plan""" def __init__(self, sigma=0.0): self.sigma = sigma
def compute_alpha_t(self, t): """Compute the data coefficient along the path""" return t, 1 def compute_sigma_t(self, t): """Compute the noise coefficient along the path""" return 1 - t, -1 def compute_d_alpha_alpha_ratio_t(self, t): """Compute the ratio between d_alpha and alpha""" return 1 / t
def compute_drift(self, x, t): """We always output sde according to score parametrization; """ t = expand_t_like_x(t, x) alpha_ratio = self.compute_d_alpha_alpha_ratio_t(t) sigma_t, d_sigma_t = self.compute_sigma_t(t) drift = alpha_ratio * x diffusion = alpha_ratio * (sigma_t ** 2) - sigma_t * d_sigma_t
return -drift, diffusion
def compute_diffusion(self, x, t, form="constant", norm=1.0): """Compute the diffusion term of the SDE Args: x: [batch_dim, ...], data point t: [batch_dim,], time vector form: str, form of the diffusion term norm: float, norm of the diffusion term """ t = expand_t_like_x(t, x) choices = { "constant": norm, "SBDM": norm * self.compute_drift(x, t)[1], "sigma": norm * self.compute_sigma_t(t)[0], "linear": norm * (1 - t), "decreasing": 0.25 * (norm * th.cos(np.pi * t) + 1) ** 2, "inccreasing-decreasing": norm * th.sin(np.pi * t) ** 2, }
try: diffusion = choices[form] except KeyError: raise NotImplementedError(f"Diffusion form {form} not implemented") return diffusion
def get_score_from_velocity(self, velocity, x, t): """Wrapper function: transfrom velocity prediction model to score Args: velocity: [batch_dim, ...] shaped tensor; velocity model output x: [batch_dim, ...] shaped tensor; x_t data point t: [batch_dim,] time tensor """ t = expand_t_like_x(t, x) alpha_t, d_alpha_t = self.compute_alpha_t(t) sigma_t, d_sigma_t = self.compute_sigma_t(t) mean = x reverse_alpha_ratio = alpha_t / d_alpha_t var = sigma_t**2 - reverse_alpha_ratio * d_sigma_t * sigma_t score = (reverse_alpha_ratio * velocity - mean) / var return score def get_noise_from_velocity(self, velocity, x, t): """Wrapper function: transfrom velocity prediction model to denoiser Args: velocity: [batch_dim, ...] shaped tensor; velocity model output x: [batch_dim, ...] shaped tensor; x_t data point t: [batch_dim,] time tensor """ t = expand_t_like_x(t, x) alpha_t, d_alpha_t = self.compute_alpha_t(t) sigma_t, d_sigma_t = self.compute_sigma_t(t) mean = x reverse_alpha_ratio = alpha_t / d_alpha_t var = reverse_alpha_ratio * d_sigma_t - sigma_t noise = (reverse_alpha_ratio * velocity - mean) / var return noise
def get_velocity_from_score(self, score, x, t): """Wrapper function: transfrom score prediction model to velocity Args: score: [batch_dim, ...] shaped tensor; score model output x: [batch_dim, ...] shaped tensor; x_t data point t: [batch_dim,] time tensor """ t = expand_t_like_x(t, x) drift, var = self.compute_drift(x, t) velocity = var * score - drift return velocity
def compute_mu_t(self, t, x0, x1): """Compute the mean of time-dependent density p_t""" t = expand_t_like_x(t, x1) alpha_t, _ = self.compute_alpha_t(t) sigma_t, _ = self.compute_sigma_t(t) # t*x1 + (1-t)*x0 ; t=0 x0; t=1 x1 return alpha_t * x1 + sigma_t * x0 def compute_xt(self, t, x0, x1): """Sample xt from time-dependent density p_t; rng is required""" xt = self.compute_mu_t(t, x0, x1) return xt def compute_ut(self, t, x0, x1, xt): """Compute the vector field corresponding to p_t""" t = expand_t_like_x(t, x1) _, d_alpha_t = self.compute_alpha_t(t) _, d_sigma_t = self.compute_sigma_t(t) return d_alpha_t * x1 + d_sigma_t * x0 def plan(self, t, x0, x1): xt = self.compute_xt(t, x0, x1) ut = self.compute_ut(t, x0, x1, xt) return t, xt, ut
class VPCPlan(ICPlan): """class for VP path flow matching"""
def __init__(self, sigma_min=0.1, sigma_max=20.0): self.sigma_min = sigma_min self.sigma_max = sigma_max self.log_mean_coeff = lambda t: -0.25 * ((1 - t) ** 2) * \ (self.sigma_max - self.sigma_min) - 0.5 * (1 - t) * self.sigma_min self.d_log_mean_coeff = lambda t: 0.5 * (1 - t) * \ (self.sigma_max - self.sigma_min) + 0.5 * self.sigma_min
def compute_alpha_t(self, t): """Compute coefficient of x1""" alpha_t = self.log_mean_coeff(t) alpha_t = th.exp(alpha_t) d_alpha_t = alpha_t * self.d_log_mean_coeff(t) return alpha_t, d_alpha_t def compute_sigma_t(self, t): """Compute coefficient of x0""" p_sigma_t = 2 * self.log_mean_coeff(t) sigma_t = th.sqrt(1 - th.exp(p_sigma_t)) d_sigma_t = th.exp(p_sigma_t) * (2 * self.d_log_mean_coeff(t)) / (-2 * sigma_t) return sigma_t, d_sigma_t def compute_d_alpha_alpha_ratio_t(self, t): """Special purposed function for computing numerical stabled d_alpha_t / alpha_t""" return self.d_log_mean_coeff(t)
def compute_drift(self, x, t): """Compute the drift term of the SDE""" t = expand_t_like_x(t, x) beta_t = self.sigma_min + (1 - t) * (self.sigma_max - self.sigma_min) return -0.5 * beta_t * x, beta_t / 2
class GVPCPlan(ICPlan): def __init__(self, sigma=0.0): super().__init__(sigma) def compute_alpha_t(self, t): """Compute coefficient of x1""" alpha_t = th.sin(t * np.pi / 2) d_alpha_t = np.pi / 2 * th.cos(t * np.pi / 2) return alpha_t, d_alpha_t def compute_sigma_t(self, t): """Compute coefficient of x0""" sigma_t = th.cos(t * np.pi / 2) d_sigma_t = -np.pi / 2 * th.sin(t * np.pi / 2) return sigma_t, d_sigma_t def compute_d_alpha_alpha_ratio_t(self, t): """Special purposed function for computing numerical stabled d_alpha_t / alpha_t""" return np.pi / (2 * th.tan(t * np.pi / 2))123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534# This file includes code derived from the SiT project (https://github.com/willisma/SiT),# which is licensed under the MIT License.## MIT License## Copyright (c) Meta Platforms, Inc. and affiliates.## Permission is hereby granted, free of charge, to any person obtaining a copy# of this software and associated documentation files (the "Software"), to deal# in the Software without restriction, including without limitation the rights# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell# copies of the Software, and to permit persons to whom the Software is# furnished to do so, subject to the following conditions:## The above copyright notice and this permission notice shall be included in all# copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE# SOFTWARE.
import torch as thimport numpy as npimport logging
import enum
from . import pathfrom .utils import EasyDict, log_state, mean_flatfrom .integrators import ode, sde
class ModelType(enum.Enum): """ Which type of output the model predicts. """
NOISE = enum.auto() # the model predicts epsilon SCORE = enum.auto() # the model predicts \nabla \log p(x) VELOCITY = enum.auto() # the model predicts v(x)
class PathType(enum.Enum): """ Which type of path to use. """
LINEAR = enum.auto() GVP = enum.auto() VP = enum.auto()
class WeightType(enum.Enum): """ Which type of weighting to use. """
NONE = enum.auto() VELOCITY = enum.auto() LIKELIHOOD = enum.auto()
class Transport:
def __init__( self, *, model_type, path_type, loss_type, train_eps, sample_eps, train_sample_type = "uniform", **kwargs, ): path_options = { PathType.LINEAR: path.ICPlan, PathType.GVP: path.GVPCPlan, PathType.VP: path.VPCPlan, }
self.loss_type = loss_type self.model_type = model_type self.path_sampler = path_options[path_type]() self.train_eps = train_eps self.sample_eps = sample_eps self.train_sample_type = train_sample_type if self.train_sample_type == "logit_normal": self.mean = kwargs['mean'] self.std = kwargs['std'] self.shift_scale = kwargs['shift_scale'] print(f"using logit normal sample, shift scale is {self.shift_scale}")
def prior_logp(self, z): ''' Standard multivariate normal prior Assume z is batched ''' shape = th.tensor(z.size()) N = th.prod(shape[1:]) _fn = lambda x: -N / 2. * np.log(2 * np.pi) - th.sum(x ** 2) / 2. return th.vmap(_fn)(z)
def check_interval( self, train_eps, sample_eps, *, diffusion_form="SBDM", sde=False, reverse=False, eval=False, last_step_size=0.0, ): t0 = 0 t1 = 1 eps = train_eps if not eval else sample_eps if (type(self.path_sampler) in [path.VPCPlan]):
t1 = 1 - eps if (not sde or last_step_size == 0) else 1 - last_step_size
elif (type(self.path_sampler) in [path.ICPlan, path.GVPCPlan]) \ and ( self.model_type != ModelType.VELOCITY or sde): # avoid numerical issue by taking a first semi-implicit step
t0 = eps if (diffusion_form == "SBDM" and sde) or self.model_type != ModelType.VELOCITY else 0 t1 = 1 - eps if (not sde or last_step_size == 0) else 1 - last_step_size
if reverse: t0, t1 = 1 - t0, 1 - t1
return t0, t1
def sample(self, x1): """Sampling x0 & t based on shape of x1 (if needed) Args: x1 - data point; [batch, *dim] """
x0 = th.randn_like(x1) if self.train_sample_type=="uniform": t0, t1 = self.check_interval(self.train_eps, self.sample_eps) t = th.rand((x1.shape[0],)) * (t1 - t0) + t0 t = t.to(x1) elif self.train_sample_type=="logit_normal": t = th.randn((x1.shape[0],)) * self.std + self.mean t = t.to(x1) t = 1/(1+th.exp(-t))
t = np.sqrt(self.shift_scale)*t/(1+(np.sqrt(self.shift_scale)-1)*t)
return t, x0, x1
def training_losses( self, model, x1, model_kwargs=None ): """Loss for training the score model Args: - model: backbone model; could be score, noise, or velocity - x1: datapoint - model_kwargs: additional arguments for the model """ if model_kwargs == None: model_kwargs = {}
t, x0, x1 = self.sample(x1) t, xt, ut = self.path_sampler.plan(t, x0, x1) model_output = model(xt, t, **model_kwargs) B, *_, C = xt.shape assert model_output.size() == (B, *xt.size()[1:-1], C)
terms = {} terms['pred'] = model_output if self.model_type == ModelType.VELOCITY: terms['loss'] = mean_flat(((model_output - ut) ** 2)) else: _, drift_var = self.path_sampler.compute_drift(xt, t) sigma_t, _ = self.path_sampler.compute_sigma_t(path.expand_t_like_x(t, xt)) if self.loss_type in [WeightType.VELOCITY]: weight = (drift_var / sigma_t) ** 2 elif self.loss_type in [WeightType.LIKELIHOOD]: weight = drift_var / (sigma_t ** 2) elif self.loss_type in [WeightType.NONE]: weight = 1 else: raise NotImplementedError()
if self.model_type == ModelType.NOISE: terms['loss'] = mean_flat(weight * ((model_output - x0) ** 2)) else: terms['loss'] = mean_flat(weight * ((model_output * sigma_t + x0) ** 2))
return terms
def get_drift( self ): """member function for obtaining the drift of the probability flow ODE"""
def score_ode(x, t, model, **model_kwargs): drift_mean, drift_var = self.path_sampler.compute_drift(x, t) model_output = model(x, t, **model_kwargs) return (-drift_mean + drift_var * model_output) # by change of variable
def noise_ode(x, t, model, **model_kwargs): drift_mean, drift_var = self.path_sampler.compute_drift(x, t) sigma_t, _ = self.path_sampler.compute_sigma_t(path.expand_t_like_x(t, x)) model_output = model(x, t, **model_kwargs) score = model_output / -sigma_t return (-drift_mean + drift_var * score)
def velocity_ode(x, t, model, **model_kwargs): model_output = model(x, t, **model_kwargs) return model_output
if self.model_type == ModelType.NOISE: drift_fn = noise_ode elif self.model_type == ModelType.SCORE: drift_fn = score_ode else: drift_fn = velocity_ode
def body_fn(x, t, model, **model_kwargs): model_output = drift_fn(x, t, model, **model_kwargs) assert model_output.shape == x.shape, "Output shape from ODE solver must match input shape" return model_output
return body_fn
def get_score( self, ): """member function for obtaining score of x_t = alpha_t * x + sigma_t * eps""" if self.model_type == ModelType.NOISE: score_fn = lambda x, t, model, **kwargs: model(x, t, **kwargs) / - \ self.path_sampler.compute_sigma_t(path.expand_t_like_x(t, x))[0] elif self.model_type == ModelType.SCORE: score_fn = lambda x, t, model, **kwagrs: model(x, t, **kwagrs) elif self.model_type == ModelType.VELOCITY: score_fn = lambda x, t, model, **kwargs: self.path_sampler.get_score_from_velocity(model(x, t, **kwargs), x, t) else: raise NotImplementedError()
return score_fn
class Sampler: """Sampler class for the transport model"""
def __init__( self, transport, ): """Constructor for a general sampler; supporting different sampling methods Args: - transport: an tranport object specify model prediction & interpolant type """
self.transport = transport self.drift = self.transport.get_drift() self.score = self.transport.get_score()
def __get_sde_diffusion_and_drift( self, *, diffusion_form="SBDM", diffusion_norm=1.0, ):
def diffusion_fn(x, t): diffusion = self.transport.path_sampler.compute_diffusion(x, t, form=diffusion_form, norm=diffusion_norm) return diffusion
sde_drift = \ lambda x, t, model, **kwargs: \ self.drift(x, t, model, **kwargs) + diffusion_fn(x, t) * self.score(x, t, model, **kwargs)
sde_diffusion = diffusion_fn
return sde_drift, sde_diffusion
def __get_last_step( self, sde_drift, *, last_step, last_step_size, ): """Get the last step function of the SDE solver"""
if last_step is None: last_step_fn = \ lambda x, t, model, **model_kwargs: \ x elif last_step == "Mean": last_step_fn = \ lambda x, t, model, **model_kwargs: \ x + sde_drift(x, t, model, **model_kwargs) * last_step_size elif last_step == "Tweedie": alpha = self.transport.path_sampler.compute_alpha_t # simple aliasing; the original name was too long sigma = self.transport.path_sampler.compute_sigma_t last_step_fn = \ lambda x, t, model, **model_kwargs: \ x / alpha(t)[0][0] + (sigma(t)[0][0] ** 2) / alpha(t)[0][0] * self.score(x, t, model, **model_kwargs) elif last_step == "Euler": last_step_fn = \ lambda x, t, model, **model_kwargs: \ x + self.drift(x, t, model, **model_kwargs) * last_step_size else: raise NotImplementedError()
return last_step_fn
def sample_sde( self, *, sampling_method="Euler", diffusion_form="SBDM", diffusion_norm=1.0, last_step="Mean", last_step_size=0.04, num_steps=250, ): """returns a sampling function with given SDE settings Args: - sampling_method: type of sampler used in solving the SDE; default to be Euler-Maruyama - diffusion_form: function form of diffusion coefficient; default to be matching SBDM - diffusion_norm: function magnitude of diffusion coefficient; default to 1 - last_step: type of the last step; default to identity - last_step_size: size of the last step; default to match the stride of 250 steps over [0,1] - num_steps: total integration step of SDE """
if last_step is None: last_step_size = 0.0
sde_drift, sde_diffusion = self.__get_sde_diffusion_and_drift( diffusion_form=diffusion_form, diffusion_norm=diffusion_norm, )
t0, t1 = self.transport.check_interval( self.transport.train_eps, self.transport.sample_eps, diffusion_form=diffusion_form, sde=True, eval=True, reverse=False, last_step_size=last_step_size, )
_sde = sde( sde_drift, sde_diffusion, t0=t0, t1=t1, num_steps=num_steps, sampler_type=sampling_method )
last_step_fn = self.__get_last_step(sde_drift, last_step=last_step, last_step_size=last_step_size)
def _sample(init, model, **model_kwargs): xs = _sde.sample(init, model, **model_kwargs) ts = th.ones(init.size(0), device=init.device) * t1 x = last_step_fn(xs[-1], ts, model, **model_kwargs) xs.append(x)
assert len(xs) == num_steps, "Samples does not match the number of steps"
return xs
return _sample
def sample_ode( self, *, sampling_method="dopri5", num_steps=50, atol=1e-6, rtol=1e-3, reverse=False, ): """returns a sampling function with given ODE settings Args: - sampling_method: type of sampler used in solving the ODE; default to be Dopri5 - num_steps: - fixed solver (Euler, Heun): the actual number of integration steps performed - adaptive solver (Dopri5): the number of datapoints saved during integration; produced by interpolation - atol: absolute error tolerance for the solver - rtol: relative error tolerance for the solver - reverse: whether solving the ODE in reverse (data to noise); default to False """ if reverse: drift = lambda x, t, model, **kwargs: self.drift(x, th.ones_like(t) * (1 - t), model, **kwargs) else: drift = self.drift
t0, t1 = self.transport.check_interval( self.transport.train_eps, self.transport.sample_eps, sde=False, eval=True, reverse=reverse, last_step_size=0.0, )
_ode = ode( drift=drift, t0=t0, t1=t1, sampler_type=sampling_method, num_steps=num_steps, atol=atol, rtol=rtol, )
return _ode.sample
def sample_ode_intermediate( self, *, sampling_method="dopri5", num_steps=50, atol=1e-6, rtol=1e-3, t=0.5, reverse=False, ): """returns a sampling function with given ODE settings Args: - sampling_method: type of sampler used in solving the ODE; default to be Dopri5 - num_steps: - fixed solver (Euler, Heun): the actual number of integration steps performed - adaptive solver (Dopri5): the number of datapoints saved during integration; produced by interpolation - atol: absolute error tolerance for the solver - rtol: relative error tolerance for the solver - reverse: whether solving the ODE in reverse (data to noise); default to False """ if reverse: drift = lambda x, t, model, **kwargs: self.drift(x, th.ones_like(t) * (1 - t), model, **kwargs) else: drift = self.drift
t0, t1 = self.transport.check_interval( self.transport.train_eps, self.transport.sample_eps, sde=False, eval=True, reverse=reverse, last_step_size=0.0, )
_ode = ode( drift=drift, t0=t, t1=t1, sampler_type=sampling_method, num_steps=num_steps, atol=atol, rtol=rtol, )
return _ode.sample
def sample_ode_likelihood( self, *, sampling_method="dopri5", num_steps=50, atol=1e-6, rtol=1e-3, ):
"""returns a sampling function for calculating likelihood with given ODE settings Args: - sampling_method: type of sampler used in solving the ODE; default to be Dopri5 - num_steps: - fixed solver (Euler, Heun): the actual number of integration steps performed - adaptive solver (Dopri5): the number of datapoints saved during integration; produced by interpolation - atol: absolute error tolerance for the solver - rtol: relative error tolerance for the solver """
def _likelihood_drift(x, t, model, **model_kwargs): x, _ = x eps = th.randint(2, x.size(), dtype=th.float, device=x.device) * 2 - 1 t = th.ones_like(t) * (1 - t) with th.enable_grad(): x.requires_grad = True grad = th.autograd.grad(th.sum(self.drift(x, t, model, **model_kwargs) * eps), x)[0] logp_grad = th.sum(grad * eps, dim=tuple(range(1, len(x.size())))) drift = self.drift(x, t, model, **model_kwargs) return (-drift, logp_grad)
t0, t1 = self.transport.check_interval( self.transport.train_eps, self.transport.sample_eps, sde=False, eval=True, reverse=False, last_step_size=0.0, )
_ode = ode( drift=_likelihood_drift, t0=t0, t1=t1, sampler_type=sampling_method, num_steps=num_steps, atol=atol, rtol=rtol, )
def _sample_fn(x, model, **model_kwargs): init_logp = th.zeros(x.size(0)).to(x) input = (x, init_logp) drift, delta_logp = _ode.sample(input, model, **model_kwargs) drift, delta_logp = drift[-1], delta_logp[-1] prior_logp = self.transport.prior_logp(drift) logp = prior_logp - delta_logp return logp, drift
return _sample_fn123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354# This file includes code derived from the SiT project (https://github.com/willisma/SiT),# which is licensed under the MIT License.## MIT License## Copyright (c) Meta Platforms, Inc. and affiliates.## Permission is hereby granted, free of charge, to any person obtaining a copy# of this software and associated documentation files (the "Software"), to deal# in the Software without restriction, including without limitation the rights# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell# copies of the Software, and to permit persons to whom the Software is# furnished to do so, subject to the following conditions:## The above copyright notice and this permission notice shall be included in all# copies or substantial portions of the Software.## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE# SOFTWARE.
import torch as th
class EasyDict:
def __init__(self, sub_dict): for k, v in sub_dict.items(): setattr(self, k, v)
def __getitem__(self, key): return getattr(self, key)
def mean_flat(x): """ Take the mean over all non-batch dimensions. """ return th.mean(x, dim=list(range(1, len(x.size()))))
def log_state(state): result = [] sorted_state = dict(sorted(state.items())) for key, value in sorted_state.items(): # Check if the value is an instance of a class if "<object" in str(value) or "object at" in str(value): result.append(f"{key}: [{value.__class__.__name__}]") else: result.append(f"{key}: {value}") return '\n'.join(result)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778import importlib
__attributes = { # Sparse Structure 'SparseStructureEncoder': 'sparse_structure_vae', 'SparseStructureDecoder': 'sparse_structure_vae', 'SparseStructureFlowModel': 'sparse_structure_flow', # SLat Generation 'SLatFlowModel': 'structured_latent_flow', 'ElasticSLatFlowModel': 'structured_latent_flow', # SC-VAEs 'SparseUnetVaeEncoder': 'sc_vaes.sparse_unet_vae', 'SparseUnetVaeDecoder': 'sc_vaes.sparse_unet_vae', 'FlexiDualGridVaeEncoder': 'sc_vaes.fdg_vae', 'FlexiDualGridVaeDecoder': 'sc_vaes.fdg_vae'}
__submodules = []
__all__ = list(__attributes.keys()) + __submodules
def __getattr__(name): if name not in globals(): if name in __attributes: module_name = __attributes[name] module = importlib.import_module(f".{module_name}", __name__) globals()[name] = getattr(module, name) elif name in __submodules: module = importlib.import_module(f".{name}", __name__) globals()[name] = module else: raise AttributeError(f"module {__name__} has no attribute {name}") return globals()[name]
def from_pretrained(path: str, **kwargs): """ Load a model from a pretrained checkpoint.
Args: path: The path to the checkpoint. Can be either local path or a Hugging Face model name. NOTE: config file and model file should take the name f'{path}.json' and f'{path}.safetensors' respectively. **kwargs: Additional arguments for the model constructor. """ import os import json from safetensors.torch import load_file is_local = os.path.exists(f"{path}.json") and os.path.exists(f"{path}.safetensors")
if is_local: config_file = f"{path}.json" model_file = f"{path}.safetensors" else: from huggingface_hub import hf_hub_download path_parts = path.split('/') repo_id = f'{path_parts[0]}/{path_parts[1]}' model_name = '/'.join(path_parts[2:]) config_file = hf_hub_download(repo_id, f"{model_name}.json") model_file = hf_hub_download(repo_id, f"{model_name}.safetensors")
with open(config_file, 'r') as f: config = json.load(f) model = __getattr__(config['name'])(**config['args'], **kwargs) model.load_state_dict(load_file(model_file), strict=False)
return model
# For Pylanceif __name__ == '__main__': from .sparse_structure_vae import SparseStructureEncoder, SparseStructureDecoder from .sparse_structure_flow import SparseStructureFlowModel from .structured_latent_flow import SLatFlowModel, ElasticSLatFlowModel from .sc_vaes.sparse_unet_vae import SparseUnetVaeEncoder, SparseUnetVaeDecoder from .sc_vaes.fdg_vae import FlexiDualGridVaeEncoder, FlexiDualGridVaeDecoder123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110from typing import *import torchimport torch.nn as nnimport torch.nn.functional as Ffrom ...modules import sparse as spfrom .sparse_unet_vae import ( SparseResBlock3d, SparseConvNeXtBlock3d, SparseResBlockDownsample3d, SparseResBlockUpsample3d, SparseResBlockS2C3d, SparseResBlockC2S3d,)from .sparse_unet_vae import ( SparseUnetVaeEncoder, SparseUnetVaeDecoder,)from ...representations import Meshfrom o_voxel.convert import flexible_dual_grid_to_mesh
class FlexiDualGridVaeEncoder(SparseUnetVaeEncoder): def __init__( self, model_channels: List[int], latent_channels: int, num_blocks: List[int], block_type: List[str], down_block_type: List[str], block_args: List[Dict[str, Any]], use_fp16: bool = False, ): super().__init__( 6, model_channels, latent_channels, num_blocks, block_type, down_block_type, block_args, use_fp16, ) def forward(self, vertices: sp.SparseTensor, intersected: sp.SparseTensor, sample_posterior=False, return_raw=False): x = vertices.replace(torch.cat([ vertices.feats - 0.5, intersected.feats.float() - 0.5, ], dim=1)) return super().forward(x, sample_posterior, return_raw) class FlexiDualGridVaeDecoder(SparseUnetVaeDecoder): def __init__( self, resolution: int, model_channels: List[int], latent_channels: int, num_blocks: List[int], block_type: List[str], up_block_type: List[str], block_args: List[Dict[str, Any]], voxel_margin: float = 0.5, use_fp16: bool = False, ): self.resolution = resolution self.voxel_margin = voxel_margin super().__init__( 7, model_channels, latent_channels, num_blocks, block_type, up_block_type, block_args, use_fp16, )
def set_resolution(self, resolution: int) -> None: self.resolution = resolution def forward(self, x: sp.SparseTensor, gt_intersected: sp.SparseTensor = None, **kwargs): decoded = super().forward(x, **kwargs) if self.training: h, subs_gt, subs = decoded vertices = h.replace((1 + 2 * self.voxel_margin) * F.sigmoid(h.feats[..., 0:3]) - self.voxel_margin) intersected_logits = h.replace(h.feats[..., 3:6]) quad_lerp = h.replace(F.softplus(h.feats[..., 6:7])) mesh = [Mesh(flexible_dual_grid_to_mesh( h.coords[:, 1:], v.feats, i.feats, q.feats, aabb=[[-0.5, -0.5, -0.5], [0.5, 0.5, 0.5]], grid_size=self.resolution, train=True )) for v, i, q in zip(vertices, gt_intersected, quad_lerp)] return mesh, vertices, intersected_logits, subs_gt, subs else: out_list = list(decoded) if isinstance(decoded, tuple) else [decoded] h = out_list[0] vertices = h.replace((1 + 2 * self.voxel_margin) * F.sigmoid(h.feats[..., 0:3]) - self.voxel_margin) intersected = h.replace(h.feats[..., 3:6] > 0) quad_lerp = h.replace(F.softplus(h.feats[..., 6:7])) mesh = [Mesh(*flexible_dual_grid_to_mesh( h.coords[:, 1:], v.feats, i.feats, q.feats, aabb=[[-0.5, -0.5, -0.5], [0.5, 0.5, 0.5]], grid_size=self.resolution, train=False )) for v, i, q in zip(vertices, intersected, quad_lerp)] out_list[0] = mesh return out_list[0] if len(out_list) == 1 else tuple(out_list)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522from typing import *import torchimport torch.nn as nnimport torch.nn.functional as Fimport torch.utils.checkpointfrom ...modules.utils import convert_module_to_f16, convert_module_to_f32, zero_modulefrom ...modules import sparse as spfrom ...modules.norm import LayerNorm32
class SparseResBlock3d(nn.Module): def __init__( self, channels: int, out_channels: Optional[int] = None, downsample: bool = False, upsample: bool = False, resample_mode: Literal['nearest', 'spatial2channel'] = 'nearest', use_checkpoint: bool = False, ): super().__init__() self.channels = channels self.out_channels = out_channels or channels self.downsample = downsample self.upsample = upsample self.resample_mode = resample_mode self.use_checkpoint = use_checkpoint assert not (downsample and upsample), "Cannot downsample and upsample at the same time"
self.norm1 = LayerNorm32(channels, elementwise_affine=True, eps=1e-6) self.norm2 = LayerNorm32(self.out_channels, elementwise_affine=False, eps=1e-6) if resample_mode == 'nearest': self.conv1 = sp.SparseConv3d(channels, self.out_channels, 3) elif resample_mode =='spatial2channel' and not self.downsample: self.conv1 = sp.SparseConv3d(channels, self.out_channels * 8, 3) elif resample_mode =='spatial2channel' and self.downsample: self.conv1 = sp.SparseConv3d(channels, self.out_channels // 8, 3) self.conv2 = zero_module(sp.SparseConv3d(self.out_channels, self.out_channels, 3)) if resample_mode == 'nearest': self.skip_connection = sp.SparseLinear(channels, self.out_channels) if channels != self.out_channels else nn.Identity() elif resample_mode =='spatial2channel' and self.downsample: self.skip_connection = lambda x: x.replace(x.feats.reshape(x.feats.shape[0], out_channels, channels * 8 // out_channels).mean(dim=-1)) elif resample_mode =='spatial2channel' and not self.downsample: self.skip_connection = lambda x: x.replace(x.feats.repeat_interleave(out_channels // (channels // 8), dim=1)) self.updown = None if self.downsample: if resample_mode == 'nearest': self.updown = sp.SparseDownsample(2) elif resample_mode =='spatial2channel': self.updown = sp.SparseSpatial2Channel(2) elif self.upsample: self.to_subdiv = sp.SparseLinear(channels, 8) if resample_mode == 'nearest': self.updown = sp.SparseUpsample(2) elif resample_mode =='spatial2channel': self.updown = sp.SparseChannel2Spatial(2)
def _updown(self, x: sp.SparseTensor, subdiv: sp.SparseTensor = None) -> sp.SparseTensor: if self.downsample: x = self.updown(x) elif self.upsample: x = self.updown(x, subdiv.replace(subdiv.feats > 0)) return x
def _forward(self, x: sp.SparseTensor) -> sp.SparseTensor: subdiv = None if self.upsample: subdiv = self.to_subdiv(x) h = x.replace(self.norm1(x.feats)) h = h.replace(F.silu(h.feats)) if self.resample_mode == 'spatial2channel': h = self.conv1(h) h = self._updown(h, subdiv) x = self._updown(x, subdiv) if self.resample_mode == 'nearest': h = self.conv1(h) h = h.replace(self.norm2(h.feats)) h = h.replace(F.silu(h.feats)) h = self.conv2(h) h = h + self.skip_connection(x) if self.upsample: return h, subdiv return h def forward(self, x: sp.SparseTensor) -> sp.SparseTensor: if self.use_checkpoint: return torch.utils.checkpoint.checkpoint(self._forward, x, use_reentrant=False) else: return self._forward(x)
class SparseResBlockDownsample3d(nn.Module): def __init__( self, channels: int, out_channels: Optional[int] = None, use_checkpoint: bool = False, ): super().__init__() self.channels = channels self.out_channels = out_channels or channels self.use_checkpoint = use_checkpoint self.norm1 = LayerNorm32(channels, elementwise_affine=True, eps=1e-6) self.norm2 = LayerNorm32(self.out_channels, elementwise_affine=False, eps=1e-6) self.conv1 = sp.SparseConv3d(channels, self.out_channels, 3) self.conv2 = zero_module(sp.SparseConv3d(self.out_channels, self.out_channels, 3)) self.skip_connection = sp.SparseLinear(channels, self.out_channels) if channels != self.out_channels else nn.Identity() self.updown = sp.SparseDownsample(2)
def _forward(self, x: sp.SparseTensor) -> sp.SparseTensor: h = x.replace(self.norm1(x.feats)) h = h.replace(F.silu(h.feats)) h = self.updown(h) x = self.updown(x) h = self.conv1(h) h = h.replace(self.norm2(h.feats)) h = h.replace(F.silu(h.feats)) h = self.conv2(h) h = h + self.skip_connection(x) return h def forward(self, x: sp.SparseTensor) -> sp.SparseTensor: if self.use_checkpoint: return torch.utils.checkpoint.checkpoint(self._forward, x, use_reentrant=False) else: return self._forward(x)
class SparseResBlockUpsample3d(nn.Module): def __init__( self, channels: int, out_channels: Optional[int] = None, use_checkpoint: bool = False, pred_subdiv: bool = True, ): super().__init__() self.channels = channels self.out_channels = out_channels or channels self.use_checkpoint = use_checkpoint self.pred_subdiv = pred_subdiv self.norm1 = LayerNorm32(channels, elementwise_affine=True, eps=1e-6) self.norm2 = LayerNorm32(self.out_channels, elementwise_affine=False, eps=1e-6) self.conv1 = sp.SparseConv3d(channels, self.out_channels, 3) self.conv2 = zero_module(sp.SparseConv3d(self.out_channels, self.out_channels, 3)) self.skip_connection = sp.SparseLinear(channels, self.out_channels) if channels != self.out_channels else nn.Identity() if self.pred_subdiv: self.to_subdiv = sp.SparseLinear(channels, 8) self.updown = sp.SparseUpsample(2)
def _forward(self, x: sp.SparseTensor, subdiv: sp.SparseTensor = None) -> sp.SparseTensor: if self.pred_subdiv: subdiv = self.to_subdiv(x) h = x.replace(self.norm1(x.feats)) h = h.replace(F.silu(h.feats)) subdiv_binarized = subdiv.replace(subdiv.feats > 0) if subdiv is not None else None h = self.updown(h, subdiv_binarized) x = self.updown(x, subdiv_binarized) h = self.conv1(h) h = h.replace(self.norm2(h.feats)) h = h.replace(F.silu(h.feats)) h = self.conv2(h) h = h + self.skip_connection(x) if self.pred_subdiv: return h, subdiv else: return h def forward(self, x: sp.SparseTensor) -> sp.SparseTensor: if self.use_checkpoint: return torch.utils.checkpoint.checkpoint(self._forward, x, use_reentrant=False) else: return self._forward(x)
class SparseResBlockS2C3d(nn.Module): def __init__( self, channels: int, out_channels: Optional[int] = None, use_checkpoint: bool = False, ): super().__init__() self.channels = channels self.out_channels = out_channels or channels self.use_checkpoint = use_checkpoint self.norm1 = LayerNorm32(channels, elementwise_affine=True, eps=1e-6) self.norm2 = LayerNorm32(self.out_channels, elementwise_affine=False, eps=1e-6) self.conv1 = sp.SparseConv3d(channels, self.out_channels // 8, 3) self.conv2 = zero_module(sp.SparseConv3d(self.out_channels, self.out_channels, 3)) self.skip_connection = lambda x: x.replace(x.feats.reshape(x.feats.shape[0], out_channels, channels * 8 // out_channels).mean(dim=-1)) self.updown = sp.SparseSpatial2Channel(2)
def _forward(self, x: sp.SparseTensor) -> sp.SparseTensor: h = x.replace(self.norm1(x.feats)) h = h.replace(F.silu(h.feats)) h = self.conv1(h) h = self.updown(h) x = self.updown(x) h = h.replace(self.norm2(h.feats)) h = h.replace(F.silu(h.feats)) h = self.conv2(h) h = h + self.skip_connection(x) return h def forward(self, x: sp.SparseTensor) -> sp.SparseTensor: if self.use_checkpoint: return torch.utils.checkpoint.checkpoint(self._forward, x, use_reentrant=False) else: return self._forward(x)
class SparseResBlockC2S3d(nn.Module): def __init__( self, channels: int, out_channels: Optional[int] = None, use_checkpoint: bool = False, pred_subdiv: bool = True, ): super().__init__() self.channels = channels self.out_channels = out_channels or channels self.use_checkpoint = use_checkpoint self.pred_subdiv = pred_subdiv self.norm1 = LayerNorm32(channels, elementwise_affine=True, eps=1e-6) self.norm2 = LayerNorm32(self.out_channels, elementwise_affine=False, eps=1e-6) self.conv1 = sp.SparseConv3d(channels, self.out_channels * 8, 3) self.conv2 = zero_module(sp.SparseConv3d(self.out_channels, self.out_channels, 3)) self.skip_connection = lambda x: x.replace(x.feats.repeat_interleave(out_channels // (channels // 8), dim=1)) if pred_subdiv: self.to_subdiv = sp.SparseLinear(channels, 8) self.updown = sp.SparseChannel2Spatial(2)
def _forward(self, x: sp.SparseTensor, subdiv: sp.SparseTensor = None) -> sp.SparseTensor: if self.pred_subdiv: subdiv = self.to_subdiv(x) h = x.replace(self.norm1(x.feats)) h = h.replace(F.silu(h.feats)) h = self.conv1(h) subdiv_binarized = subdiv.replace(subdiv.feats > 0) if subdiv is not None else None h = self.updown(h, subdiv_binarized) x = self.updown(x, subdiv_binarized) h = h.replace(self.norm2(h.feats)) h = h.replace(F.silu(h.feats)) h = self.conv2(h) h = h + self.skip_connection(x) if self.pred_subdiv: return h, subdiv else: return h def forward(self, x: sp.SparseTensor, subdiv: sp.SparseTensor = None) -> sp.SparseTensor: if self.use_checkpoint: return torch.utils.checkpoint.checkpoint(self._forward, x, subdiv, use_reentrant=False) else: return self._forward(x, subdiv) class SparseConvNeXtBlock3d(nn.Module): def __init__( self, channels: int, mlp_ratio: float = 4.0, use_checkpoint: bool = False, ): super().__init__() self.channels = channels self.use_checkpoint = use_checkpoint self.norm = LayerNorm32(channels, elementwise_affine=True, eps=1e-6) self.conv = sp.SparseConv3d(channels, channels, 3) self.mlp = nn.Sequential( nn.Linear(channels, int(channels * mlp_ratio)), nn.SiLU(), zero_module(nn.Linear(int(channels * mlp_ratio), channels)), )
def _forward(self, x: sp.SparseTensor) -> sp.SparseTensor: h = self.conv(x) h = h.replace(self.norm(h.feats)) h = h.replace(self.mlp(h.feats)) return h + x def forward(self, x: sp.SparseTensor) -> sp.SparseTensor: if self.use_checkpoint: return torch.utils.checkpoint.checkpoint(self._forward, x, use_reentrant=False) else: return self._forward(x)
class SparseUnetVaeEncoder(nn.Module): """ Sparse Swin Transformer Unet VAE model. """ def __init__( self, in_channels: int, model_channels: List[int], latent_channels: int, num_blocks: List[int], block_type: List[str], down_block_type: List[str], block_args: List[Dict[str, Any]], use_fp16: bool = False, ): super().__init__() self.in_channels = in_channels self.model_channels = model_channels self.num_blocks = num_blocks self.dtype = torch.float16 if use_fp16 else torch.float32 self.dtype = torch.float16 if use_fp16 else torch.float32
self.input_layer = sp.SparseLinear(in_channels, model_channels[0]) self.to_latent = sp.SparseLinear(model_channels[-1], 2 * latent_channels) self.blocks = nn.ModuleList([]) for i in range(len(num_blocks)): self.blocks.append(nn.ModuleList([])) for j in range(num_blocks[i]): self.blocks[-1].append( globals()[block_type[i]]( model_channels[i], **block_args[i], ) ) if i < len(num_blocks) - 1: self.blocks[-1].append( globals()[down_block_type[i]]( model_channels[i], model_channels[i+1], **block_args[i], ) ) self.initialize_weights() if use_fp16: self.convert_to_fp16()
@property def device(self) -> torch.device: """ Return the device of the model. """ return next(self.parameters()).device
def convert_to_fp16(self) -> None: """ Convert the torso of the model to float16. """ self.blocks.apply(convert_module_to_f16)
def convert_to_fp32(self) -> None: """ Convert the torso of the model to float32. """ self.blocks.apply(convert_module_to_f32)
def initialize_weights(self) -> None: # Initialize transformer layers: def _basic_init(module): if isinstance(module, nn.Linear): torch.nn.init.xavier_uniform_(module.weight) if module.bias is not None: nn.init.constant_(module.bias, 0) self.apply(_basic_init)
def forward(self, x: sp.SparseTensor, sample_posterior=False, return_raw=False): h = self.input_layer(x) h = h.type(self.dtype) for i, res in enumerate(self.blocks): for j, block in enumerate(res): h = block(h) h = h.type(x.dtype) h = h.replace(F.layer_norm(h.feats, h.feats.shape[-1:])) h = self.to_latent(h) # Sample from the posterior distribution mean, logvar = h.feats.chunk(2, dim=-1) if sample_posterior: std = torch.exp(0.5 * logvar) z = mean + std * torch.randn_like(std) else: z = mean z = h.replace(z) if return_raw: return z, mean, logvar else: return z class SparseUnetVaeDecoder(nn.Module): """ Sparse Swin Transformer Unet VAE model. """ def __init__( self, out_channels: int, model_channels: List[int], latent_channels: int, num_blocks: List[int], block_type: List[str], up_block_type: List[str], block_args: List[Dict[str, Any]], use_fp16: bool = False, pred_subdiv: bool = True, ): super().__init__() self.out_channels = out_channels self.model_channels = model_channels self.num_blocks = num_blocks self.use_fp16 = use_fp16 self.pred_subdiv = pred_subdiv self.dtype = torch.float16 if use_fp16 else torch.float32 self.low_vram = False self.output_layer = sp.SparseLinear(model_channels[-1], out_channels) self.from_latent = sp.SparseLinear(latent_channels, model_channels[0]) self.blocks = nn.ModuleList([]) for i in range(len(num_blocks)): self.blocks.append(nn.ModuleList([])) for j in range(num_blocks[i]): self.blocks[-1].append( globals()[block_type[i]]( model_channels[i], **block_args[i], ) ) if i < len(num_blocks) - 1: self.blocks[-1].append( globals()[up_block_type[i]]( model_channels[i], model_channels[i+1], pred_subdiv=pred_subdiv, **block_args[i], ) ) self.initialize_weights() if use_fp16: self.convert_to_fp16() @property def device(self) -> torch.device: """ Return the device of the model. """ return next(self.parameters()).device
def convert_to_fp16(self) -> None: """ Convert the torso of the model to float16. """ self.blocks.apply(convert_module_to_f16)
def convert_to_fp32(self) -> None: """ Convert the torso of the model to float32. """ self.blocks.apply(convert_module_to_f32)
def initialize_weights(self) -> None: # Initialize transformer layers: def _basic_init(module): if isinstance(module, nn.Linear): torch.nn.init.xavier_uniform_(module.weight) if module.bias is not None: nn.init.constant_(module.bias, 0) self.apply(_basic_init)
def forward(self, x: sp.SparseTensor, guide_subs: Optional[List[sp.SparseTensor]] = None, return_subs: bool = False) -> sp.SparseTensor: assert guide_subs is None or self.pred_subdiv == False, "Only decoders with pred_subdiv=False can be used with guide_subs" assert return_subs == False or self.pred_subdiv == True, "Only decoders with pred_subdiv=True can be used with return_subs" h = self.from_latent(x) h = h.type(self.dtype) subs_gt = [] subs = [] for i, res in enumerate(self.blocks): for j, block in enumerate(res): if i < len(self.blocks) - 1 and j == len(res) - 1: if self.pred_subdiv: if self.training: subs_gt.append(h.get_spatial_cache('subdivision')) h, sub = block(h) subs.append(sub) else: h = block(h, subdiv=guide_subs[i] if guide_subs is not None else None) else: h = block(h) h = h.type(x.dtype) h = h.replace(F.layer_norm(h.feats, h.feats.shape[-1:])) h = self.output_layer(h) if self.training and self.pred_subdiv: return h, subs_gt, subs else: if return_subs: return h, subs else: return h def upsample(self, x: sp.SparseTensor, upsample_times: int) -> torch.Tensor: assert self.pred_subdiv == True, "Only decoders with pred_subdiv=True can be used with upsampling" h = self.from_latent(x) h = h.type(self.dtype) for i, res in enumerate(self.blocks): if i == upsample_times: return h.coords for j, block in enumerate(res): if i < len(self.blocks) - 1 and j == len(res) - 1: h, sub = block(h) else: h = block(h) 123456789101112131415161718192021222324from contextlib import contextmanagerfrom typing import *import mathfrom ..modules import sparse as spfrom ..utils.elastic_utils import ElasticModuleMixin
class SparseTransformerElasticMixin(ElasticModuleMixin): def _get_input_size(self, x: sp.SparseTensor, *args, **kwargs): return x.feats.shape[0] @contextmanager def with_mem_ratio(self, mem_ratio=1.0): if mem_ratio == 1.0: yield 1.0 return num_blocks = len(self.blocks) num_checkpoint_blocks = min(math.ceil((1 - mem_ratio) * num_blocks) + 1, num_blocks) exact_mem_ratio = 1 - (num_checkpoint_blocks - 1) / num_blocks for i in range(num_blocks): self.blocks[i].use_checkpoint = i < num_checkpoint_blocks yield exact_mem_ratio for i in range(num_blocks): self.blocks[i].use_checkpoint = False123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247from typing import *from functools import partialimport torchimport torch.nn as nnimport torch.nn.functional as Fimport numpy as npfrom ..modules.utils import convert_module_to, manual_cast, str_to_dtypefrom ..modules.transformer import AbsolutePositionEmbedder, ModulatedTransformerCrossBlockfrom ..modules.attention import RotaryPositionEmbedder
class TimestepEmbedder(nn.Module): """ Embeds scalar timesteps into vector representations. """ def __init__(self, hidden_size, frequency_embedding_size=256): super().__init__() self.mlp = nn.Sequential( nn.Linear(frequency_embedding_size, hidden_size, bias=True), nn.SiLU(), nn.Linear(hidden_size, hidden_size, bias=True), ) self.frequency_embedding_size = frequency_embedding_size
@staticmethod def timestep_embedding(t, dim, max_period=10000): """ Create sinusoidal timestep embeddings.
Args: t: a 1-D Tensor of N indices, one per batch element. These may be fractional. dim: the dimension of the output. max_period: controls the minimum frequency of the embeddings.
Returns: an (N, D) Tensor of positional embeddings. """ # https://github.com/openai/glide-text2im/blob/main/glide_text2im/nn.py half = dim // 2 freqs = torch.exp( -np.log(max_period) * torch.arange(start=0, end=half, dtype=torch.float32) / half ).to(device=t.device) args = t[:, None].float() * freqs[None] embedding = torch.cat([torch.cos(args), torch.sin(args)], dim=-1) if dim % 2: embedding = torch.cat([embedding, torch.zeros_like(embedding[:, :1])], dim=-1) return embedding
def forward(self, t): t_freq = self.timestep_embedding(t, self.frequency_embedding_size) t_emb = self.mlp(t_freq) return t_emb
class SparseStructureFlowModel(nn.Module): def __init__( self, resolution: int, in_channels: int, model_channels: int, cond_channels: int, out_channels: int, num_blocks: int, num_heads: Optional[int] = None, num_head_channels: Optional[int] = 64, mlp_ratio: float = 4, pe_mode: Literal["ape", "rope"] = "ape", rope_freq: Tuple[float, float] = (1.0, 10000.0), dtype: str = 'float32', use_checkpoint: bool = False, share_mod: bool = False, initialization: str = 'vanilla', qk_rms_norm: bool = False, qk_rms_norm_cross: bool = False, **kwargs ): super().__init__() self.resolution = resolution self.in_channels = in_channels self.model_channels = model_channels self.cond_channels = cond_channels self.out_channels = out_channels self.num_blocks = num_blocks self.num_heads = num_heads or model_channels // num_head_channels self.mlp_ratio = mlp_ratio self.pe_mode = pe_mode self.use_checkpoint = use_checkpoint self.share_mod = share_mod self.initialization = initialization self.qk_rms_norm = qk_rms_norm self.qk_rms_norm_cross = qk_rms_norm_cross self.dtype = str_to_dtype(dtype)
self.t_embedder = TimestepEmbedder(model_channels) if share_mod: self.adaLN_modulation = nn.Sequential( nn.SiLU(), nn.Linear(model_channels, 6 * model_channels, bias=True) )
if pe_mode == "ape": pos_embedder = AbsolutePositionEmbedder(model_channels, 3) coords = torch.meshgrid(*[torch.arange(res, device=self.device) for res in [resolution] * 3], indexing='ij') coords = torch.stack(coords, dim=-1).reshape(-1, 3) pos_emb = pos_embedder(coords) self.register_buffer("pos_emb", pos_emb) elif pe_mode == "rope": pos_embedder = RotaryPositionEmbedder(self.model_channels // self.num_heads, 3) coords = torch.meshgrid(*[torch.arange(res, device=self.device) for res in [resolution] * 3], indexing='ij') coords = torch.stack(coords, dim=-1).reshape(-1, 3) rope_phases = pos_embedder(coords) self.register_buffer("rope_phases", rope_phases) if pe_mode != "rope": self.rope_phases = None
self.input_layer = nn.Linear(in_channels, model_channels) self.blocks = nn.ModuleList([ ModulatedTransformerCrossBlock( model_channels, cond_channels, num_heads=self.num_heads, mlp_ratio=self.mlp_ratio, attn_mode='full', use_checkpoint=self.use_checkpoint, use_rope=(pe_mode == "rope"), rope_freq=rope_freq, share_mod=share_mod, qk_rms_norm=self.qk_rms_norm, qk_rms_norm_cross=self.qk_rms_norm_cross, ) for _ in range(num_blocks) ])
self.out_layer = nn.Linear(model_channels, out_channels)
self.initialize_weights() self.convert_to(self.dtype)
@property def device(self) -> torch.device: """ Return the device of the model. """ return next(self.parameters()).device
def convert_to(self, dtype: torch.dtype) -> None: """ Convert the torso of the model to the specified dtype. """ self.dtype = dtype self.blocks.apply(partial(convert_module_to, dtype=dtype))
def initialize_weights(self) -> None: if self.initialization == 'vanilla': # Initialize transformer layers: def _basic_init(module): if isinstance(module, nn.Linear): torch.nn.init.xavier_uniform_(module.weight) if module.bias is not None: nn.init.constant_(module.bias, 0) self.apply(_basic_init)
# Initialize timestep embedding MLP: nn.init.normal_(self.t_embedder.mlp[0].weight, std=0.02) nn.init.normal_(self.t_embedder.mlp[2].weight, std=0.02)
# Zero-out adaLN modulation layers in DiT blocks: if self.share_mod: nn.init.constant_(self.adaLN_modulation[-1].weight, 0) nn.init.constant_(self.adaLN_modulation[-1].bias, 0) else: for block in self.blocks: nn.init.constant_(block.adaLN_modulation[-1].weight, 0) nn.init.constant_(block.adaLN_modulation[-1].bias, 0)
# Zero-out output layers: nn.init.constant_(self.out_layer.weight, 0) nn.init.constant_(self.out_layer.bias, 0) elif self.initialization == 'scaled': # Initialize transformer layers: def _basic_init(module): if isinstance(module, nn.Linear): torch.nn.init.normal_(module.weight, std=np.sqrt(2.0 / (5.0 * self.model_channels))) if module.bias is not None: nn.init.constant_(module.bias, 0) self.apply(_basic_init) # Scaled init for to_out and ffn2 def _scaled_init(module): if isinstance(module, nn.Linear): torch.nn.init.normal_(module.weight, std=1.0 / np.sqrt(5 * self.num_blocks * self.model_channels)) if module.bias is not None: nn.init.constant_(module.bias, 0) for block in self.blocks: block.self_attn.to_out.apply(_scaled_init) block.cross_attn.to_out.apply(_scaled_init) block.mlp.mlp[2].apply(_scaled_init) # Initialize input layer to make the initial representation have variance 1 nn.init.normal_(self.input_layer.weight, std=1.0 / np.sqrt(self.in_channels)) nn.init.zeros_(self.input_layer.bias) # Initialize timestep embedding MLP: nn.init.normal_(self.t_embedder.mlp[0].weight, std=0.02) nn.init.normal_(self.t_embedder.mlp[2].weight, std=0.02) # Zero-out adaLN modulation layers in DiT blocks: if self.share_mod: nn.init.constant_(self.adaLN_modulation[-1].weight, 0) nn.init.constant_(self.adaLN_modulation[-1].bias, 0) else: for block in self.blocks: nn.init.constant_(block.adaLN_modulation[-1].weight, 0) nn.init.constant_(block.adaLN_modulation[-1].bias, 0)
# Zero-out output layers: nn.init.constant_(self.out_layer.weight, 0) nn.init.constant_(self.out_layer.bias, 0)
def forward(self, x: torch.Tensor, t: torch.Tensor, cond: torch.Tensor) -> torch.Tensor: assert [*x.shape] == [x.shape[0], self.in_channels, *[self.resolution] * 3], \ f"Input shape mismatch, got {x.shape}, expected {[x.shape[0], self.in_channels, *[self.resolution] * 3]}"
h = x.view(*x.shape[:2], -1).permute(0, 2, 1).contiguous()
h = self.input_layer(h) if self.pe_mode == "ape": h = h + self.pos_emb[None] t_emb = self.t_embedder(t) if self.share_mod: t_emb = self.adaLN_modulation(t_emb) t_emb = manual_cast(t_emb, self.dtype) h = manual_cast(h, self.dtype) cond = manual_cast(cond, self.dtype) for block in self.blocks: h = block(h, t_emb, cond, self.rope_phases) h = manual_cast(h, x.dtype) h = F.layer_norm(h, h.shape[-1:]) h = self.out_layer(h)
h = h.permute(0, 2, 1).view(h.shape[0], h.shape[2], *[self.resolution] * 3).contiguous()
return h123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306from typing import *import torchimport torch.nn as nnimport torch.nn.functional as Ffrom ..modules.norm import GroupNorm32, ChannelLayerNorm32from ..modules.spatial import pixel_shuffle_3dfrom ..modules.utils import zero_module, convert_module_to_f16, convert_module_to_f32
def norm_layer(norm_type: str, *args, **kwargs) -> nn.Module: """ Return a normalization layer. """ if norm_type == "group": return GroupNorm32(32, *args, **kwargs) elif norm_type == "layer": return ChannelLayerNorm32(*args, **kwargs) else: raise ValueError(f"Invalid norm type {norm_type}")
class ResBlock3d(nn.Module): def __init__( self, channels: int, out_channels: Optional[int] = None, norm_type: Literal["group", "layer"] = "layer", ): super().__init__() self.channels = channels self.out_channels = out_channels or channels
self.norm1 = norm_layer(norm_type, channels) self.norm2 = norm_layer(norm_type, self.out_channels) self.conv1 = nn.Conv3d(channels, self.out_channels, 3, padding=1) self.conv2 = zero_module(nn.Conv3d(self.out_channels, self.out_channels, 3, padding=1)) self.skip_connection = nn.Conv3d(channels, self.out_channels, 1) if channels != self.out_channels else nn.Identity() def forward(self, x: torch.Tensor) -> torch.Tensor: h = self.norm1(x) h = F.silu(h) h = self.conv1(h) h = self.norm2(h) h = F.silu(h) h = self.conv2(h) h = h + self.skip_connection(x) return h
class DownsampleBlock3d(nn.Module): def __init__( self, in_channels: int, out_channels: int, mode: Literal["conv", "avgpool"] = "conv", ): assert mode in ["conv", "avgpool"], f"Invalid mode {mode}"
super().__init__() self.in_channels = in_channels self.out_channels = out_channels
if mode == "conv": self.conv = nn.Conv3d(in_channels, out_channels, 2, stride=2) elif mode == "avgpool": assert in_channels == out_channels, "Pooling mode requires in_channels to be equal to out_channels"
def forward(self, x: torch.Tensor) -> torch.Tensor: if hasattr(self, "conv"): return self.conv(x) else: return F.avg_pool3d(x, 2)
class UpsampleBlock3d(nn.Module): def __init__( self, in_channels: int, out_channels: int, mode: Literal["conv", "nearest"] = "conv", ): assert mode in ["conv", "nearest"], f"Invalid mode {mode}"
super().__init__() self.in_channels = in_channels self.out_channels = out_channels
if mode == "conv": self.conv = nn.Conv3d(in_channels, out_channels*8, 3, padding=1) elif mode == "nearest": assert in_channels == out_channels, "Nearest mode requires in_channels to be equal to out_channels"
def forward(self, x: torch.Tensor) -> torch.Tensor: if hasattr(self, "conv"): x = self.conv(x) return pixel_shuffle_3d(x, 2) else: return F.interpolate(x, scale_factor=2, mode="nearest")
class SparseStructureEncoder(nn.Module): """ Encoder for Sparse Structure (\mathcal{E}_S in the paper Sec. 3.3). Args: in_channels (int): Channels of the input. latent_channels (int): Channels of the latent representation. num_res_blocks (int): Number of residual blocks at each resolution. channels (List[int]): Channels of the encoder blocks. num_res_blocks_middle (int): Number of residual blocks in the middle. norm_type (Literal["group", "layer"]): Type of normalization layer. use_fp16 (bool): Whether to use FP16. """ def __init__( self, in_channels: int, latent_channels: int, num_res_blocks: int, channels: List[int], num_res_blocks_middle: int = 2, norm_type: Literal["group", "layer"] = "layer", use_fp16: bool = False, ): super().__init__() self.in_channels = in_channels self.latent_channels = latent_channels self.num_res_blocks = num_res_blocks self.channels = channels self.num_res_blocks_middle = num_res_blocks_middle self.norm_type = norm_type self.use_fp16 = use_fp16 self.dtype = torch.float16 if use_fp16 else torch.float32
self.input_layer = nn.Conv3d(in_channels, channels[0], 3, padding=1)
self.blocks = nn.ModuleList([]) for i, ch in enumerate(channels): self.blocks.extend([ ResBlock3d(ch, ch) for _ in range(num_res_blocks) ]) if i < len(channels) - 1: self.blocks.append( DownsampleBlock3d(ch, channels[i+1]) ) self.middle_block = nn.Sequential(*[ ResBlock3d(channels[-1], channels[-1]) for _ in range(num_res_blocks_middle) ])
self.out_layer = nn.Sequential( norm_layer(norm_type, channels[-1]), nn.SiLU(), nn.Conv3d(channels[-1], latent_channels*2, 3, padding=1) )
if use_fp16: self.convert_to_fp16()
@property def device(self) -> torch.device: """ Return the device of the model. """ return next(self.parameters()).device
def convert_to_fp16(self) -> None: """ Convert the torso of the model to float16. """ self.use_fp16 = True self.dtype = torch.float16 self.blocks.apply(convert_module_to_f16) self.middle_block.apply(convert_module_to_f16)
def convert_to_fp32(self) -> None: """ Convert the torso of the model to float32. """ self.use_fp16 = False self.dtype = torch.float32 self.blocks.apply(convert_module_to_f32) self.middle_block.apply(convert_module_to_f32)
def forward(self, x: torch.Tensor, sample_posterior: bool = False, return_raw: bool = False) -> torch.Tensor: h = self.input_layer(x) h = h.type(self.dtype)
for block in self.blocks: h = block(h) h = self.middle_block(h)
h = h.type(x.dtype) h = self.out_layer(h)
mean, logvar = h.chunk(2, dim=1)
if sample_posterior: std = torch.exp(0.5 * logvar) z = mean + std * torch.randn_like(std) else: z = mean if return_raw: return z, mean, logvar return z
class SparseStructureDecoder(nn.Module): """ Decoder for Sparse Structure (\mathcal{D}_S in the paper Sec. 3.3). Args: out_channels (int): Channels of the output. latent_channels (int): Channels of the latent representation. num_res_blocks (int): Number of residual blocks at each resolution. channels (List[int]): Channels of the decoder blocks. num_res_blocks_middle (int): Number of residual blocks in the middle. norm_type (Literal["group", "layer"]): Type of normalization layer. use_fp16 (bool): Whether to use FP16. """ def __init__( self, out_channels: int, latent_channels: int, num_res_blocks: int, channels: List[int], num_res_blocks_middle: int = 2, norm_type: Literal["group", "layer"] = "layer", use_fp16: bool = False, ): super().__init__() self.out_channels = out_channels self.latent_channels = latent_channels self.num_res_blocks = num_res_blocks self.channels = channels self.num_res_blocks_middle = num_res_blocks_middle self.norm_type = norm_type self.use_fp16 = use_fp16 self.dtype = torch.float16 if use_fp16 else torch.float32
self.input_layer = nn.Conv3d(latent_channels, channels[0], 3, padding=1)
self.middle_block = nn.Sequential(*[ ResBlock3d(channels[0], channels[0]) for _ in range(num_res_blocks_middle) ])
self.blocks = nn.ModuleList([]) for i, ch in enumerate(channels): self.blocks.extend([ ResBlock3d(ch, ch) for _ in range(num_res_blocks) ]) if i < len(channels) - 1: self.blocks.append( UpsampleBlock3d(ch, channels[i+1]) )
self.out_layer = nn.Sequential( norm_layer(norm_type, channels[-1]), nn.SiLU(), nn.Conv3d(channels[-1], out_channels, 3, padding=1) )
if use_fp16: self.convert_to_fp16()
@property def device(self) -> torch.device: """ Return the device of the model. """ return next(self.parameters()).device def convert_to_fp16(self) -> None: """ Convert the torso of the model to float16. """ self.use_fp16 = True self.dtype = torch.float16 self.blocks.apply(convert_module_to_f16) self.middle_block.apply(convert_module_to_f16)
def convert_to_fp32(self) -> None: """ Convert the torso of the model to float32. """ self.use_fp16 = False self.dtype = torch.float32 self.blocks.apply(convert_module_to_f32) self.middle_block.apply(convert_module_to_f32) def forward(self, x: torch.Tensor) -> torch.Tensor: h = self.input_layer(x) h = h.type(self.dtype) h = self.middle_block(h) for block in self.blocks: h = block(h)
h = h.type(x.dtype) h = self.out_layer(h) return h123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207from typing import *from functools import partialimport torchimport torch.nn as nnimport torch.nn.functional as Fimport numpy as npfrom ..modules.utils import convert_module_to, manual_cast, str_to_dtypefrom ..modules.transformer import AbsolutePositionEmbedderfrom ..modules import sparse as spfrom ..modules.sparse.transformer import ModulatedSparseTransformerCrossBlockfrom .sparse_structure_flow import TimestepEmbedderfrom .sparse_elastic_mixin import SparseTransformerElasticMixin
class SLatFlowModel(nn.Module): def __init__( self, resolution: int, in_channels: int, model_channels: int, cond_channels: int, out_channels: int, num_blocks: int, num_heads: Optional[int] = None, num_head_channels: Optional[int] = 64, mlp_ratio: float = 4, pe_mode: Literal["ape", "rope"] = "ape", rope_freq: Tuple[float, float] = (1.0, 10000.0), dtype: str = 'float32', use_checkpoint: bool = False, share_mod: bool = False, initialization: str = 'vanilla', qk_rms_norm: bool = False, qk_rms_norm_cross: bool = False, ): super().__init__() self.resolution = resolution self.in_channels = in_channels self.model_channels = model_channels self.cond_channels = cond_channels self.out_channels = out_channels self.num_blocks = num_blocks self.num_heads = num_heads or model_channels // num_head_channels self.mlp_ratio = mlp_ratio self.pe_mode = pe_mode self.use_checkpoint = use_checkpoint self.share_mod = share_mod self.initialization = initialization self.qk_rms_norm = qk_rms_norm self.qk_rms_norm_cross = qk_rms_norm_cross self.dtype = str_to_dtype(dtype)
self.t_embedder = TimestepEmbedder(model_channels) if share_mod: self.adaLN_modulation = nn.Sequential( nn.SiLU(), nn.Linear(model_channels, 6 * model_channels, bias=True) )
if pe_mode == "ape": self.pos_embedder = AbsolutePositionEmbedder(model_channels)
self.input_layer = sp.SparseLinear(in_channels, model_channels) self.blocks = nn.ModuleList([ ModulatedSparseTransformerCrossBlock( model_channels, cond_channels, num_heads=self.num_heads, mlp_ratio=self.mlp_ratio, attn_mode='full', use_checkpoint=self.use_checkpoint, use_rope=(pe_mode == "rope"), rope_freq=rope_freq, share_mod=self.share_mod, qk_rms_norm=self.qk_rms_norm, qk_rms_norm_cross=self.qk_rms_norm_cross, ) for _ in range(num_blocks) ]) self.out_layer = sp.SparseLinear(model_channels, out_channels)
self.initialize_weights() self.convert_to(self.dtype)
@property def device(self) -> torch.device: """ Return the device of the model. """ return next(self.parameters()).device
def convert_to(self, dtype: torch.dtype) -> None: """ Convert the torso of the model to the specified dtype. """ self.dtype = dtype self.blocks.apply(partial(convert_module_to, dtype=dtype))
def initialize_weights(self) -> None: if self.initialization == 'vanilla': # Initialize transformer layers: def _basic_init(module): if isinstance(module, nn.Linear): torch.nn.init.xavier_uniform_(module.weight) if module.bias is not None: nn.init.constant_(module.bias, 0) self.apply(_basic_init)
# Initialize timestep embedding MLP: nn.init.normal_(self.t_embedder.mlp[0].weight, std=0.02) nn.init.normal_(self.t_embedder.mlp[2].weight, std=0.02)
# Zero-out adaLN modulation layers in DiT blocks: if self.share_mod: nn.init.constant_(self.adaLN_modulation[-1].weight, 0) nn.init.constant_(self.adaLN_modulation[-1].bias, 0) else: for block in self.blocks: nn.init.constant_(block.adaLN_modulation[-1].weight, 0) nn.init.constant_(block.adaLN_modulation[-1].bias, 0)
# Zero-out output layers: nn.init.constant_(self.out_layer.weight, 0) nn.init.constant_(self.out_layer.bias, 0) elif self.initialization == 'scaled': # Initialize transformer layers: def _basic_init(module): if isinstance(module, nn.Linear): torch.nn.init.normal_(module.weight, std=np.sqrt(2.0 / (5.0 * self.model_channels))) if module.bias is not None: nn.init.constant_(module.bias, 0) self.apply(_basic_init) # Scaled init for to_out and ffn2 def _scaled_init(module): if isinstance(module, nn.Linear): torch.nn.init.normal_(module.weight, std=1.0 / np.sqrt(5 * self.num_blocks * self.model_channels)) if module.bias is not None: nn.init.constant_(module.bias, 0) for block in self.blocks: block.self_attn.to_out.apply(_scaled_init) block.cross_attn.to_out.apply(_scaled_init) block.mlp.mlp[2].apply(_scaled_init) # Initialize input layer to make the initial representation have variance 1 nn.init.normal_(self.input_layer.weight, std=1.0 / np.sqrt(self.in_channels)) nn.init.zeros_(self.input_layer.bias) # Initialize timestep embedding MLP: nn.init.normal_(self.t_embedder.mlp[0].weight, std=0.02) nn.init.normal_(self.t_embedder.mlp[2].weight, std=0.02) # Zero-out adaLN modulation layers in DiT blocks: if self.share_mod: nn.init.constant_(self.adaLN_modulation[-1].weight, 0) nn.init.constant_(self.adaLN_modulation[-1].bias, 0) else: for block in self.blocks: nn.init.constant_(block.adaLN_modulation[-1].weight, 0) nn.init.constant_(block.adaLN_modulation[-1].bias, 0)
# Zero-out output layers: nn.init.constant_(self.out_layer.weight, 0) nn.init.constant_(self.out_layer.bias, 0)
def forward( self, x: sp.SparseTensor, t: torch.Tensor, cond: Union[torch.Tensor, List[torch.Tensor]], concat_cond: Optional[sp.SparseTensor] = None, **kwargs ) -> sp.SparseTensor: if concat_cond is not None: x = sp.sparse_cat([x, concat_cond], dim=-1) if isinstance(cond, list): cond = sp.VarLenTensor.from_tensor_list(cond)
h = self.input_layer(x) h = manual_cast(h, self.dtype) t_emb = self.t_embedder(t) if self.share_mod: t_emb = self.adaLN_modulation(t_emb) t_emb = manual_cast(t_emb, self.dtype) cond = manual_cast(cond, self.dtype)
if self.pe_mode == "ape": pe = self.pos_embedder(h.coords[:, 1:]) h = h + manual_cast(pe, self.dtype) for block in self.blocks: h = block(h, t_emb, cond)
h = manual_cast(h, x.dtype) h = h.replace(F.layer_norm(h.feats, h.feats.shape[-1:])) h = self.out_layer(h) return h
class ElasticSLatFlowModel(SparseTransformerElasticMixin, SLatFlowModel): """ SLat Flow Model with elastic memory management. Used for training with low VRAM. """ pass.gitignore+2-1
Hunyuan3D-2.1/hy3dshape/hy3dshape/models/__init__.pyAdded+28
Hunyuan3D-2.1/hy3dshape/hy3dshape/models/autoencoders/__init__.pyAdded+20
Hunyuan3D-2.1/hy3dshape/hy3dshape/models/autoencoders/attention_blocks.pyAdded+716
Hunyuan3D-2.1/hy3dshape/hy3dshape/models/autoencoders/attention_processors.pyAdded+96
Hunyuan3D-2.1/hy3dshape/hy3dshape/models/autoencoders/model.pyAdded+339
Hunyuan3D-2.1/hy3dshape/hy3dshape/models/autoencoders/surface_extractors.pyAdded+164
Hunyuan3D-2.1/hy3dshape/hy3dshape/models/autoencoders/volume_decoders.pyAdded+435
Hunyuan3D-2.1/hy3dshape/hy3dshape/models/conditioner.pyAdded+257
Hunyuan3D-2.1/hy3dshape/hy3dshape/models/denoisers/__init__.pyAdded+15
Hunyuan3D-2.1/hy3dshape/hy3dshape/models/denoisers/hunyuan3ddit.pyAdded+404
Hunyuan3D-2.1/hy3dshape/hy3dshape/models/denoisers/hunyuandit.pyAdded+596
Hunyuan3D-2.1/hy3dshape/hy3dshape/models/denoisers/moe_layers.pyAdded+177
Hunyuan3D-2.1/hy3dshape/hy3dshape/models/diffusion/flow_matching_sit.pyAdded+354
Hunyuan3D-2.1/hy3dshape/hy3dshape/models/diffusion/transport/__init__.pyAdded+97
Hunyuan3D-2.1/hy3dshape/hy3dshape/models/diffusion/transport/integrators.pyAdded+142
Hunyuan3D-2.1/hy3dshape/hy3dshape/models/diffusion/transport/path.pyAdded+220
Hunyuan3D-2.1/hy3dshape/hy3dshape/models/diffusion/transport/transport.pyAdded+534
Hunyuan3D-2.1/hy3dshape/hy3dshape/models/diffusion/transport/utils.pyAdded+54
TRELLIS.2/trellis2/models/__init__.pyAdded+78
TRELLIS.2/trellis2/models/sc_vaes/fdg_vae.pyAdded+110
TRELLIS.2/trellis2/models/sc_vaes/sparse_unet_vae.pyAdded+522
TRELLIS.2/trellis2/models/sparse_elastic_mixin.pyAdded+24
TRELLIS.2/trellis2/models/sparse_structure_flow.pyAdded+247
TRELLIS.2/trellis2/models/sparse_structure_vae.pyAdded+306
TRELLIS.2/trellis2/models/structured_latent_flow.pyAdded+207