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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT
|
| 2 |
+
# except for the third-party components listed below.
|
| 3 |
+
# Hunyuan 3D does not impose any additional limitations beyond what is outlined
|
| 4 |
+
# in the repsective licenses of these third-party components.
|
| 5 |
+
# Users must comply with all terms and conditions of original licenses of these third-party
|
| 6 |
+
# components and must ensure that the usage of the third party components adheres to
|
| 7 |
+
# all relevant laws and regulations.
|
| 8 |
+
|
| 9 |
+
# For avoidance of doubts, Hunyuan 3D means the large language models and
|
| 10 |
+
# their software and algorithms, including trained model weights, parameters (including
|
| 11 |
+
# optimizer states), machine-learning model code, inference-enabling code, training-enabling code,
|
| 12 |
+
# fine-tuning enabling code and other elements of the foregoing made publicly available
|
| 13 |
+
# by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.
|
| 14 |
+
|
| 15 |
+
import os
|
| 16 |
+
import json
|
| 17 |
+
import copy
|
| 18 |
+
import numpy as np
|
| 19 |
+
import torch
|
| 20 |
+
import torch.nn as nn
|
| 21 |
+
from einops import rearrange
|
| 22 |
+
from typing import Any, Callable, Dict, List, Optional, Union, Tuple, Literal
|
| 23 |
+
import diffusers
|
| 24 |
+
from diffusers.utils import deprecate
|
| 25 |
+
from diffusers import (
|
| 26 |
+
DDPMScheduler,
|
| 27 |
+
EulerAncestralDiscreteScheduler,
|
| 28 |
+
UNet2DConditionModel,
|
| 29 |
+
)
|
| 30 |
+
from diffusers.models import UNet2DConditionModel
|
| 31 |
+
from diffusers.models.attention_processor import Attention, AttnProcessor
|
| 32 |
+
from diffusers.models.transformers.transformer_2d import BasicTransformerBlock
|
| 33 |
+
from .attn_processor import SelfAttnProcessor2_0, RefAttnProcessor2_0, PoseRoPEAttnProcessor2_0
|
| 34 |
+
|
| 35 |
+
from transformers import AutoImageProcessor, AutoModel
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class Dino_v2(nn.Module):
|
| 39 |
+
|
| 40 |
+
"""Wrapper for DINOv2 vision transformer (frozen weights).
|
| 41 |
+
|
| 42 |
+
Provides feature extraction for reference images.
|
| 43 |
+
|
| 44 |
+
Args:
|
| 45 |
+
dino_v2_path: Custom path to DINOv2 model weights (uses default if None)
|
| 46 |
+
"""
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def __init__(self, dino_v2_path):
|
| 50 |
+
super(Dino_v2, self).__init__()
|
| 51 |
+
self.dino_processor = AutoImageProcessor.from_pretrained(dino_v2_path)
|
| 52 |
+
self.dino_v2 = AutoModel.from_pretrained(dino_v2_path)
|
| 53 |
+
|
| 54 |
+
for param in self.parameters():
|
| 55 |
+
param.requires_grad = False
|
| 56 |
+
|
| 57 |
+
self.dino_v2.eval()
|
| 58 |
+
|
| 59 |
+
def forward(self, images):
|
| 60 |
+
|
| 61 |
+
"""Processes input images through DINOv2 ViT.
|
| 62 |
+
|
| 63 |
+
Handles both tensor input (B, N, C, H, W) and PIL image lists.
|
| 64 |
+
Extracts patch embeddings and flattens spatial dimensions.
|
| 65 |
+
|
| 66 |
+
Returns:
|
| 67 |
+
torch.Tensor: Feature vectors [B, N*(num_patches), feature_dim]
|
| 68 |
+
"""
|
| 69 |
+
|
| 70 |
+
if isinstance(images, torch.Tensor):
|
| 71 |
+
batch_size = images.shape[0]
|
| 72 |
+
dino_proceesed_images = self.dino_processor(
|
| 73 |
+
images=rearrange(images, "b n c h w -> (b n) c h w"), return_tensors="pt", do_rescale=False
|
| 74 |
+
).pixel_values
|
| 75 |
+
else:
|
| 76 |
+
batch_size = 1
|
| 77 |
+
dino_proceesed_images = self.dino_processor(images=images, return_tensors="pt").pixel_values
|
| 78 |
+
dino_proceesed_images = torch.stack(
|
| 79 |
+
[torch.from_numpy(np.array(image)) for image in dino_proceesed_images], dim=0
|
| 80 |
+
)
|
| 81 |
+
dino_param = next(self.dino_v2.parameters())
|
| 82 |
+
dino_proceesed_images = dino_proceesed_images.to(dino_param)
|
| 83 |
+
dino_hidden_states = self.dino_v2(dino_proceesed_images)[0]
|
| 84 |
+
dino_hidden_states = rearrange(dino_hidden_states.to(dino_param), "(b n) l c -> b (n l) c", b=batch_size)
|
| 85 |
+
|
| 86 |
+
return dino_hidden_states
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def _chunked_feed_forward(ff: nn.Module, hidden_states: torch.Tensor, chunk_dim: int, chunk_size: int):
|
| 90 |
+
# "feed_forward_chunk_size" can be used to save memory
|
| 91 |
+
|
| 92 |
+
"""Memory-efficient feedforward execution via chunking.
|
| 93 |
+
|
| 94 |
+
Divides input along specified dimension for sequential processing.
|
| 95 |
+
|
| 96 |
+
Args:
|
| 97 |
+
ff: Feedforward module to apply
|
| 98 |
+
hidden_states: Input tensor
|
| 99 |
+
chunk_dim: Dimension to split
|
| 100 |
+
chunk_size: Size of each chunk
|
| 101 |
+
|
| 102 |
+
Returns:
|
| 103 |
+
torch.Tensor: Reassembled output tensor
|
| 104 |
+
"""
|
| 105 |
+
|
| 106 |
+
if hidden_states.shape[chunk_dim] % chunk_size != 0:
|
| 107 |
+
raise ValueError(
|
| 108 |
+
f"`hidden_states` dimension to be chunked: {hidden_states.shape[chunk_dim]}"
|
| 109 |
+
f"has to be divisible by chunk size: {chunk_size}."
|
| 110 |
+
"Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`."
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
num_chunks = hidden_states.shape[chunk_dim] // chunk_size
|
| 114 |
+
ff_output = torch.cat(
|
| 115 |
+
[ff(hid_slice) for hid_slice in hidden_states.chunk(num_chunks, dim=chunk_dim)],
|
| 116 |
+
dim=chunk_dim,
|
| 117 |
+
)
|
| 118 |
+
return ff_output
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
@torch.no_grad()
|
| 122 |
+
def compute_voxel_grid_mask(position, grid_resolution=8):
|
| 123 |
+
|
| 124 |
+
"""Generates view-to-view attention mask based on 3D position similarity.
|
| 125 |
+
|
| 126 |
+
Uses voxel grid downsampling to determine spatially adjacent regions.
|
| 127 |
+
Mask indicates where features should interact across different views.
|
| 128 |
+
|
| 129 |
+
Args:
|
| 130 |
+
position: Position maps [B, N, 3, H, W] (normalized 0-1)
|
| 131 |
+
grid_resolution: Spatial reduction factor
|
| 132 |
+
|
| 133 |
+
Returns:
|
| 134 |
+
torch.Tensor: Attention mask [B, N*grid_res**2, N*grid_res**2]
|
| 135 |
+
"""
|
| 136 |
+
|
| 137 |
+
position = position.half()
|
| 138 |
+
B, N, _, H, W = position.shape
|
| 139 |
+
assert H % grid_resolution == 0 and W % grid_resolution == 0
|
| 140 |
+
|
| 141 |
+
valid_mask = (position != 1).all(dim=2, keepdim=True)
|
| 142 |
+
valid_mask = valid_mask.expand_as(position)
|
| 143 |
+
position[valid_mask == False] = 0
|
| 144 |
+
|
| 145 |
+
position = rearrange(
|
| 146 |
+
position,
|
| 147 |
+
"b n c (num_h grid_h) (num_w grid_w) -> b n num_h num_w c grid_h grid_w",
|
| 148 |
+
num_h=grid_resolution,
|
| 149 |
+
num_w=grid_resolution,
|
| 150 |
+
)
|
| 151 |
+
valid_mask = rearrange(
|
| 152 |
+
valid_mask,
|
| 153 |
+
"b n c (num_h grid_h) (num_w grid_w) -> b n num_h num_w c grid_h grid_w",
|
| 154 |
+
num_h=grid_resolution,
|
| 155 |
+
num_w=grid_resolution,
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
grid_position = position.sum(dim=(-2, -1))
|
| 159 |
+
count_masked = valid_mask.sum(dim=(-2, -1))
|
| 160 |
+
|
| 161 |
+
grid_position = grid_position / count_masked.clamp(min=1)
|
| 162 |
+
grid_position[count_masked < 5] = 0
|
| 163 |
+
|
| 164 |
+
grid_position = grid_position.permute(0, 1, 4, 2, 3)
|
| 165 |
+
grid_position = rearrange(grid_position, "b n c h w -> b n (h w) c")
|
| 166 |
+
|
| 167 |
+
grid_position_expanded_1 = grid_position.unsqueeze(2).unsqueeze(4) # 形状变为 B, N, 1, L, 1, 3
|
| 168 |
+
grid_position_expanded_2 = grid_position.unsqueeze(1).unsqueeze(3) # 形状变为 B, 1, N, 1, L, 3
|
| 169 |
+
|
| 170 |
+
# 计算欧氏距离
|
| 171 |
+
distances = torch.norm(grid_position_expanded_1 - grid_position_expanded_2, dim=-1) # 形状为 B, N, N, L, L
|
| 172 |
+
|
| 173 |
+
weights = distances
|
| 174 |
+
grid_distance = 1.73 / grid_resolution
|
| 175 |
+
weights = weights < grid_distance
|
| 176 |
+
|
| 177 |
+
return weights
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
def compute_multi_resolution_mask(position_maps, grid_resolutions=[32, 16, 8]):
|
| 181 |
+
|
| 182 |
+
"""Generates attention masks at multiple spatial resolutions.
|
| 183 |
+
|
| 184 |
+
Creates pyramid of position-based masks for hierarchical attention.
|
| 185 |
+
|
| 186 |
+
Args:
|
| 187 |
+
position_maps: Position maps [B, N, 3, H, W]
|
| 188 |
+
grid_resolutions: List of downsampling factors
|
| 189 |
+
|
| 190 |
+
Returns:
|
| 191 |
+
dict: Resolution-specific masks keyed by flattened dimension size
|
| 192 |
+
"""
|
| 193 |
+
|
| 194 |
+
position_attn_mask = {}
|
| 195 |
+
with torch.no_grad():
|
| 196 |
+
for grid_resolution in grid_resolutions:
|
| 197 |
+
position_mask = compute_voxel_grid_mask(position_maps, grid_resolution)
|
| 198 |
+
position_mask = rearrange(position_mask, "b ni nj li lj -> b (ni li) (nj lj)")
|
| 199 |
+
position_attn_mask[position_mask.shape[1]] = position_mask
|
| 200 |
+
return position_attn_mask
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
@torch.no_grad()
|
| 204 |
+
def compute_discrete_voxel_indice(position, grid_resolution=8, voxel_resolution=128):
|
| 205 |
+
|
| 206 |
+
"""Quantizes position maps to discrete voxel indices.
|
| 207 |
+
|
| 208 |
+
Creates sparse 3D coordinate representations for efficient hashing.
|
| 209 |
+
|
| 210 |
+
Args:
|
| 211 |
+
position: Position maps [B, N, 3, H, W]
|
| 212 |
+
grid_resolution: Spatial downsampling factor
|
| 213 |
+
voxel_resolution: Quantization resolution
|
| 214 |
+
|
| 215 |
+
Returns:
|
| 216 |
+
torch.Tensor: Voxel indices [B, N, grid_res, grid_res, 3]
|
| 217 |
+
"""
|
| 218 |
+
|
| 219 |
+
position = position.half()
|
| 220 |
+
B, N, _, H, W = position.shape
|
| 221 |
+
assert H % grid_resolution == 0 and W % grid_resolution == 0
|
| 222 |
+
|
| 223 |
+
valid_mask = (position != 1).all(dim=2, keepdim=True)
|
| 224 |
+
valid_mask = valid_mask.expand_as(position)
|
| 225 |
+
position[valid_mask == False] = 0
|
| 226 |
+
|
| 227 |
+
position = rearrange(
|
| 228 |
+
position,
|
| 229 |
+
"b n c (num_h grid_h) (num_w grid_w) -> b n num_h num_w c grid_h grid_w",
|
| 230 |
+
num_h=grid_resolution,
|
| 231 |
+
num_w=grid_resolution,
|
| 232 |
+
)
|
| 233 |
+
valid_mask = rearrange(
|
| 234 |
+
valid_mask,
|
| 235 |
+
"b n c (num_h grid_h) (num_w grid_w) -> b n num_h num_w c grid_h grid_w",
|
| 236 |
+
num_h=grid_resolution,
|
| 237 |
+
num_w=grid_resolution,
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
+
grid_position = position.sum(dim=(-2, -1))
|
| 241 |
+
count_masked = valid_mask.sum(dim=(-2, -1))
|
| 242 |
+
|
| 243 |
+
grid_position = grid_position / count_masked.clamp(min=1)
|
| 244 |
+
voxel_mask_thres = (H // grid_resolution) * (W // grid_resolution) // (4 * 4)
|
| 245 |
+
grid_position[count_masked < voxel_mask_thres] = 0
|
| 246 |
+
|
| 247 |
+
grid_position = grid_position.permute(0, 1, 4, 2, 3).clamp(0, 1) # B N C H W
|
| 248 |
+
voxel_indices = grid_position * (voxel_resolution - 1)
|
| 249 |
+
voxel_indices = torch.round(voxel_indices).long()
|
| 250 |
+
return voxel_indices
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
def calc_multires_voxel_idxs(position_maps, grid_resolutions=[64, 32, 16, 8], voxel_resolutions=[512, 256, 128, 64]):
|
| 254 |
+
|
| 255 |
+
"""Generates multi-resolution voxel indices for position encoding.
|
| 256 |
+
|
| 257 |
+
Creates pyramid of quantized position representations.
|
| 258 |
+
|
| 259 |
+
Args:
|
| 260 |
+
position_maps: Input position maps
|
| 261 |
+
grid_resolutions: Spatial resolution levels
|
| 262 |
+
voxel_resolutions: Quantization levels
|
| 263 |
+
|
| 264 |
+
Returns:
|
| 265 |
+
dict: Voxel indices keyed by flattened dimension size, with resolution metadata
|
| 266 |
+
"""
|
| 267 |
+
|
| 268 |
+
voxel_indices = {}
|
| 269 |
+
with torch.no_grad():
|
| 270 |
+
for grid_resolution, voxel_resolution in zip(grid_resolutions, voxel_resolutions):
|
| 271 |
+
voxel_indice = compute_discrete_voxel_indice(position_maps, grid_resolution, voxel_resolution)
|
| 272 |
+
voxel_indice = rearrange(voxel_indice, "b n c h w -> b (n h w) c")
|
| 273 |
+
voxel_indices[voxel_indice.shape[1]] = {"voxel_indices": voxel_indice, "voxel_resolution": voxel_resolution}
|
| 274 |
+
return voxel_indices
|
| 275 |
+
|
| 276 |
+
|
| 277 |
+
class Basic2p5DTransformerBlock(torch.nn.Module):
|
| 278 |
+
|
| 279 |
+
|
| 280 |
+
"""Enhanced transformer block for multiview 2.5D image generation.
|
| 281 |
+
|
| 282 |
+
Extends standard transformer blocks with:
|
| 283 |
+
- Material-specific attention (MDA)
|
| 284 |
+
- Multiview attention (MA)
|
| 285 |
+
- Reference attention (RA)
|
| 286 |
+
- DINO feature integration
|
| 287 |
+
|
| 288 |
+
Args:
|
| 289 |
+
transformer: Base transformer block
|
| 290 |
+
layer_name: Identifier for layer
|
| 291 |
+
use_ma: Enable multiview attention
|
| 292 |
+
use_ra: Enable reference attention
|
| 293 |
+
use_mda: Enable material-aware attention
|
| 294 |
+
use_dino: Enable DINO feature integration
|
| 295 |
+
pbr_setting: List of PBR materials
|
| 296 |
+
"""
|
| 297 |
+
|
| 298 |
+
def __init__(
|
| 299 |
+
self,
|
| 300 |
+
transformer: BasicTransformerBlock,
|
| 301 |
+
layer_name,
|
| 302 |
+
use_ma=True,
|
| 303 |
+
use_ra=True,
|
| 304 |
+
use_mda=True,
|
| 305 |
+
use_dino=True,
|
| 306 |
+
pbr_setting=None,
|
| 307 |
+
) -> None:
|
| 308 |
+
|
| 309 |
+
"""
|
| 310 |
+
Initialization:
|
| 311 |
+
1. Material-Dimension Attention (MDA):
|
| 312 |
+
- Processes each PBR material with separate projection weights
|
| 313 |
+
- Uses custom SelfAttnProcessor2_0 with material awareness
|
| 314 |
+
|
| 315 |
+
2. Multiview Attention (MA):
|
| 316 |
+
- Adds cross-view attention with PoseRoPE
|
| 317 |
+
- Initialized as zero-initialized residual pathway
|
| 318 |
+
|
| 319 |
+
3. Reference Attention (RA):
|
| 320 |
+
- Conditions on reference view features
|
| 321 |
+
- Uses RefAttnProcessor2_0 for material-specific conditioning
|
| 322 |
+
|
| 323 |
+
4. DINO Attention:
|
| 324 |
+
- Incorporates DINO-ViT features
|
| 325 |
+
- Initialized as zero-initialized residual pathway
|
| 326 |
+
"""
|
| 327 |
+
|
| 328 |
+
super().__init__()
|
| 329 |
+
self.transformer = transformer
|
| 330 |
+
self.layer_name = layer_name
|
| 331 |
+
self.use_ma = use_ma
|
| 332 |
+
self.use_ra = use_ra
|
| 333 |
+
self.use_mda = use_mda
|
| 334 |
+
self.use_dino = use_dino
|
| 335 |
+
self.pbr_setting = pbr_setting
|
| 336 |
+
|
| 337 |
+
if self.use_mda:
|
| 338 |
+
self.attn1.set_processor(
|
| 339 |
+
SelfAttnProcessor2_0(
|
| 340 |
+
query_dim=self.dim,
|
| 341 |
+
heads=self.num_attention_heads,
|
| 342 |
+
dim_head=self.attention_head_dim,
|
| 343 |
+
dropout=self.dropout,
|
| 344 |
+
bias=self.attention_bias,
|
| 345 |
+
cross_attention_dim=None,
|
| 346 |
+
upcast_attention=self.attn1.upcast_attention,
|
| 347 |
+
out_bias=True,
|
| 348 |
+
pbr_setting=self.pbr_setting,
|
| 349 |
+
)
|
| 350 |
+
)
|
| 351 |
+
|
| 352 |
+
# multiview attn
|
| 353 |
+
if self.use_ma:
|
| 354 |
+
self.attn_multiview = Attention(
|
| 355 |
+
query_dim=self.dim,
|
| 356 |
+
heads=self.num_attention_heads,
|
| 357 |
+
dim_head=self.attention_head_dim,
|
| 358 |
+
dropout=self.dropout,
|
| 359 |
+
bias=self.attention_bias,
|
| 360 |
+
cross_attention_dim=None,
|
| 361 |
+
upcast_attention=self.attn1.upcast_attention,
|
| 362 |
+
out_bias=True,
|
| 363 |
+
processor=PoseRoPEAttnProcessor2_0(),
|
| 364 |
+
)
|
| 365 |
+
|
| 366 |
+
# ref attn
|
| 367 |
+
if self.use_ra:
|
| 368 |
+
self.attn_refview = Attention(
|
| 369 |
+
query_dim=self.dim,
|
| 370 |
+
heads=self.num_attention_heads,
|
| 371 |
+
dim_head=self.attention_head_dim,
|
| 372 |
+
dropout=self.dropout,
|
| 373 |
+
bias=self.attention_bias,
|
| 374 |
+
cross_attention_dim=None,
|
| 375 |
+
upcast_attention=self.attn1.upcast_attention,
|
| 376 |
+
out_bias=True,
|
| 377 |
+
processor=RefAttnProcessor2_0(
|
| 378 |
+
query_dim=self.dim,
|
| 379 |
+
heads=self.num_attention_heads,
|
| 380 |
+
dim_head=self.attention_head_dim,
|
| 381 |
+
dropout=self.dropout,
|
| 382 |
+
bias=self.attention_bias,
|
| 383 |
+
cross_attention_dim=None,
|
| 384 |
+
upcast_attention=self.attn1.upcast_attention,
|
| 385 |
+
out_bias=True,
|
| 386 |
+
pbr_setting=self.pbr_setting,
|
| 387 |
+
),
|
| 388 |
+
)
|
| 389 |
+
|
| 390 |
+
# dino attn
|
| 391 |
+
if self.use_dino:
|
| 392 |
+
self.attn_dino = Attention(
|
| 393 |
+
query_dim=self.dim,
|
| 394 |
+
heads=self.num_attention_heads,
|
| 395 |
+
dim_head=self.attention_head_dim,
|
| 396 |
+
dropout=self.dropout,
|
| 397 |
+
bias=self.attention_bias,
|
| 398 |
+
cross_attention_dim=self.cross_attention_dim,
|
| 399 |
+
upcast_attention=self.attn2.upcast_attention,
|
| 400 |
+
out_bias=True,
|
| 401 |
+
)
|
| 402 |
+
|
| 403 |
+
self._initialize_attn_weights()
|
| 404 |
+
|
| 405 |
+
def _initialize_attn_weights(self):
|
| 406 |
+
|
| 407 |
+
"""Initializes specialized attention heads with base weights.
|
| 408 |
+
|
| 409 |
+
Uses weight sharing strategy:
|
| 410 |
+
- Copies base transformer weights to specialized heads
|
| 411 |
+
- Initializes newly-added parameters to zero
|
| 412 |
+
"""
|
| 413 |
+
|
| 414 |
+
if self.use_mda:
|
| 415 |
+
for token in self.pbr_setting:
|
| 416 |
+
if token == "albedo":
|
| 417 |
+
continue
|
| 418 |
+
getattr(self.attn1.processor, f"to_q_{token}").load_state_dict(self.attn1.to_q.state_dict())
|
| 419 |
+
getattr(self.attn1.processor, f"to_k_{token}").load_state_dict(self.attn1.to_k.state_dict())
|
| 420 |
+
getattr(self.attn1.processor, f"to_v_{token}").load_state_dict(self.attn1.to_v.state_dict())
|
| 421 |
+
getattr(self.attn1.processor, f"to_out_{token}").load_state_dict(self.attn1.to_out.state_dict())
|
| 422 |
+
|
| 423 |
+
if self.use_ma:
|
| 424 |
+
self.attn_multiview.load_state_dict(self.attn1.state_dict(), strict=False)
|
| 425 |
+
with torch.no_grad():
|
| 426 |
+
for layer in self.attn_multiview.to_out:
|
| 427 |
+
for param in layer.parameters():
|
| 428 |
+
param.zero_()
|
| 429 |
+
|
| 430 |
+
if self.use_ra:
|
| 431 |
+
self.attn_refview.load_state_dict(self.attn1.state_dict(), strict=False)
|
| 432 |
+
for token in self.pbr_setting:
|
| 433 |
+
if token == "albedo":
|
| 434 |
+
continue
|
| 435 |
+
getattr(self.attn_refview.processor, f"to_v_{token}").load_state_dict(
|
| 436 |
+
self.attn_refview.to_q.state_dict()
|
| 437 |
+
)
|
| 438 |
+
getattr(self.attn_refview.processor, f"to_out_{token}").load_state_dict(
|
| 439 |
+
self.attn_refview.to_out.state_dict()
|
| 440 |
+
)
|
| 441 |
+
with torch.no_grad():
|
| 442 |
+
for layer in self.attn_refview.to_out:
|
| 443 |
+
for param in layer.parameters():
|
| 444 |
+
param.zero_()
|
| 445 |
+
for token in self.pbr_setting:
|
| 446 |
+
if token == "albedo":
|
| 447 |
+
continue
|
| 448 |
+
for layer in getattr(self.attn_refview.processor, f"to_out_{token}"):
|
| 449 |
+
for param in layer.parameters():
|
| 450 |
+
param.zero_()
|
| 451 |
+
|
| 452 |
+
if self.use_dino:
|
| 453 |
+
self.attn_dino.load_state_dict(self.attn2.state_dict(), strict=False)
|
| 454 |
+
with torch.no_grad():
|
| 455 |
+
for layer in self.attn_dino.to_out:
|
| 456 |
+
for param in layer.parameters():
|
| 457 |
+
param.zero_()
|
| 458 |
+
|
| 459 |
+
if self.use_dino:
|
| 460 |
+
self.attn_dino.load_state_dict(self.attn2.state_dict(), strict=False)
|
| 461 |
+
with torch.no_grad():
|
| 462 |
+
for layer in self.attn_dino.to_out:
|
| 463 |
+
for param in layer.parameters():
|
| 464 |
+
param.zero_()
|
| 465 |
+
|
| 466 |
+
def __getattr__(self, name: str):
|
| 467 |
+
try:
|
| 468 |
+
return super().__getattr__(name)
|
| 469 |
+
except AttributeError:
|
| 470 |
+
return getattr(self.transformer, name)
|
| 471 |
+
|
| 472 |
+
def forward(
|
| 473 |
+
self,
|
| 474 |
+
hidden_states: torch.Tensor,
|
| 475 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 476 |
+
encoder_hidden_states: Optional[torch.Tensor] = None,
|
| 477 |
+
encoder_attention_mask: Optional[torch.Tensor] = None,
|
| 478 |
+
timestep: Optional[torch.LongTensor] = None,
|
| 479 |
+
cross_attention_kwargs: Dict[str, Any] = None,
|
| 480 |
+
class_labels: Optional[torch.LongTensor] = None,
|
| 481 |
+
added_cond_kwargs: Optional[Dict[str, torch.Tensor]] = None,
|
| 482 |
+
) -> torch.Tensor:
|
| 483 |
+
|
| 484 |
+
"""Forward pass with multi-mechanism attention.
|
| 485 |
+
|
| 486 |
+
Processing stages:
|
| 487 |
+
1. Material-aware self-attention (MDA)
|
| 488 |
+
2. Reference attention (RA)
|
| 489 |
+
3. Multiview attention (MA) with position-aware attention
|
| 490 |
+
4. Text conditioning (base attention)
|
| 491 |
+
5. DINO feature conditioning (optional)
|
| 492 |
+
6. Position-aware conditioning
|
| 493 |
+
7. Feed-forward network
|
| 494 |
+
|
| 495 |
+
Args:
|
| 496 |
+
hidden_states: Input features [B * N_materials * N_views, Seq_len, Feat_dim]
|
| 497 |
+
See base transformer for other parameters
|
| 498 |
+
|
| 499 |
+
Returns:
|
| 500 |
+
torch.Tensor: Output features
|
| 501 |
+
"""
|
| 502 |
+
# [Full multi-mechanism processing pipeline...]
|
| 503 |
+
# Key processing stages:
|
| 504 |
+
# 1. Material-aware self-attention (handles albedo/mr separation)
|
| 505 |
+
# 2. Reference attention (conditioned on reference features)
|
| 506 |
+
# 3. View-to-view attention with geometric constraints
|
| 507 |
+
# 4. Text-to-image cross-attention
|
| 508 |
+
# 5. DINO feature fusion (when enabled)
|
| 509 |
+
# 6. Positional conditioning (RoPE-style)
|
| 510 |
+
# 7. Feed-forward network with conditional normalization
|
| 511 |
+
|
| 512 |
+
# Notice that normalization is always applied before the real computation in the following blocks.
|
| 513 |
+
# 0. Self-Attention
|
| 514 |
+
batch_size = hidden_states.shape[0]
|
| 515 |
+
|
| 516 |
+
cross_attention_kwargs = cross_attention_kwargs.copy() if cross_attention_kwargs is not None else {}
|
| 517 |
+
num_in_batch = cross_attention_kwargs.pop("num_in_batch", 1)
|
| 518 |
+
mode = cross_attention_kwargs.pop("mode", None)
|
| 519 |
+
mva_scale = cross_attention_kwargs.pop("mva_scale", 1.0)
|
| 520 |
+
ref_scale = cross_attention_kwargs.pop("ref_scale", 1.0)
|
| 521 |
+
condition_embed_dict = cross_attention_kwargs.pop("condition_embed_dict", None)
|
| 522 |
+
dino_hidden_states = cross_attention_kwargs.pop("dino_hidden_states", None)
|
| 523 |
+
position_voxel_indices = cross_attention_kwargs.pop("position_voxel_indices", None)
|
| 524 |
+
N_pbr = len(self.pbr_setting) if self.pbr_setting is not None else 1
|
| 525 |
+
|
| 526 |
+
if self.norm_type == "ada_norm":
|
| 527 |
+
norm_hidden_states = self.norm1(hidden_states, timestep)
|
| 528 |
+
elif self.norm_type == "ada_norm_zero":
|
| 529 |
+
norm_hidden_states, gate_msa, shift_mlp, scale_mlp, gate_mlp = self.norm1(
|
| 530 |
+
hidden_states, timestep, class_labels, hidden_dtype=hidden_states.dtype
|
| 531 |
+
)
|
| 532 |
+
elif self.norm_type in ["layer_norm", "layer_norm_i2vgen"]:
|
| 533 |
+
norm_hidden_states = self.norm1(hidden_states)
|
| 534 |
+
elif self.norm_type == "ada_norm_continuous":
|
| 535 |
+
norm_hidden_states = self.norm1(hidden_states, added_cond_kwargs["pooled_text_emb"])
|
| 536 |
+
elif self.norm_type == "ada_norm_single":
|
| 537 |
+
shift_msa, scale_msa, gate_msa, shift_mlp, scale_mlp, gate_mlp = (
|
| 538 |
+
self.scale_shift_table[None] + timestep.reshape(batch_size, 6, -1)
|
| 539 |
+
).chunk(6, dim=1)
|
| 540 |
+
norm_hidden_states = self.norm1(hidden_states)
|
| 541 |
+
norm_hidden_states = norm_hidden_states * (1 + scale_msa) + shift_msa
|
| 542 |
+
else:
|
| 543 |
+
raise ValueError("Incorrect norm used")
|
| 544 |
+
|
| 545 |
+
if self.pos_embed is not None:
|
| 546 |
+
norm_hidden_states = self.pos_embed(norm_hidden_states)
|
| 547 |
+
|
| 548 |
+
# 1. Prepare GLIGEN inputs
|
| 549 |
+
cross_attention_kwargs = cross_attention_kwargs.copy() if cross_attention_kwargs is not None else {}
|
| 550 |
+
gligen_kwargs = cross_attention_kwargs.pop("gligen", None)
|
| 551 |
+
|
| 552 |
+
if self.use_mda:
|
| 553 |
+
mda_norm_hidden_states = rearrange(
|
| 554 |
+
norm_hidden_states, "(b n_pbr n) l c -> b n_pbr n l c", n=num_in_batch, n_pbr=N_pbr
|
| 555 |
+
)
|
| 556 |
+
attn_output = self.attn1(
|
| 557 |
+
mda_norm_hidden_states,
|
| 558 |
+
encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None,
|
| 559 |
+
attention_mask=attention_mask,
|
| 560 |
+
**cross_attention_kwargs,
|
| 561 |
+
)
|
| 562 |
+
attn_output = rearrange(attn_output, "b n_pbr n l c -> (b n_pbr n) l c")
|
| 563 |
+
else:
|
| 564 |
+
attn_output = self.attn1(
|
| 565 |
+
norm_hidden_states,
|
| 566 |
+
encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None,
|
| 567 |
+
attention_mask=attention_mask,
|
| 568 |
+
**cross_attention_kwargs,
|
| 569 |
+
)
|
| 570 |
+
|
| 571 |
+
if self.norm_type == "ada_norm_zero":
|
| 572 |
+
attn_output = gate_msa.unsqueeze(1) * attn_output
|
| 573 |
+
elif self.norm_type == "ada_norm_single":
|
| 574 |
+
attn_output = gate_msa * attn_output
|
| 575 |
+
|
| 576 |
+
hidden_states = attn_output + hidden_states
|
| 577 |
+
if hidden_states.ndim == 4:
|
| 578 |
+
hidden_states = hidden_states.squeeze(1)
|
| 579 |
+
|
| 580 |
+
# 1.2 Reference Attention
|
| 581 |
+
if "w" in mode:
|
| 582 |
+
condition_embed_dict[self.layer_name] = rearrange(
|
| 583 |
+
norm_hidden_states, "(b n) l c -> b (n l) c", n=num_in_batch
|
| 584 |
+
) # B, (N L), C
|
| 585 |
+
|
| 586 |
+
if "r" in mode and self.use_ra:
|
| 587 |
+
condition_embed = condition_embed_dict[self.layer_name]
|
| 588 |
+
|
| 589 |
+
#! Only using albedo features for reference attention
|
| 590 |
+
ref_norm_hidden_states = rearrange(
|
| 591 |
+
norm_hidden_states, "(b n_pbr n) l c -> b n_pbr (n l) c", n=num_in_batch, n_pbr=N_pbr
|
| 592 |
+
)[:, 0, ...]
|
| 593 |
+
|
| 594 |
+
attn_output = self.attn_refview(
|
| 595 |
+
ref_norm_hidden_states,
|
| 596 |
+
encoder_hidden_states=condition_embed,
|
| 597 |
+
attention_mask=None,
|
| 598 |
+
**cross_attention_kwargs,
|
| 599 |
+
) # b (n l) c
|
| 600 |
+
attn_output = rearrange(attn_output, "b n_pbr (n l) c -> (b n_pbr n) l c", n=num_in_batch, n_pbr=N_pbr)
|
| 601 |
+
|
| 602 |
+
ref_scale_timing = ref_scale
|
| 603 |
+
if isinstance(ref_scale, torch.Tensor):
|
| 604 |
+
ref_scale_timing = ref_scale.unsqueeze(1).repeat(1, num_in_batch * N_pbr).view(-1)
|
| 605 |
+
for _ in range(attn_output.ndim - 1):
|
| 606 |
+
ref_scale_timing = ref_scale_timing.unsqueeze(-1)
|
| 607 |
+
hidden_states = ref_scale_timing * attn_output + hidden_states
|
| 608 |
+
if hidden_states.ndim == 4:
|
| 609 |
+
hidden_states = hidden_states.squeeze(1)
|
| 610 |
+
|
| 611 |
+
# 1.3 Multiview Attention
|
| 612 |
+
if num_in_batch > 1 and self.use_ma:
|
| 613 |
+
multivew_hidden_states = rearrange(
|
| 614 |
+
norm_hidden_states, "(b n_pbr n) l c -> (b n_pbr) (n l) c", n_pbr=N_pbr, n=num_in_batch
|
| 615 |
+
)
|
| 616 |
+
position_indices = None
|
| 617 |
+
if position_voxel_indices is not None:
|
| 618 |
+
if multivew_hidden_states.shape[1] in position_voxel_indices:
|
| 619 |
+
position_indices = position_voxel_indices[multivew_hidden_states.shape[1]]
|
| 620 |
+
|
| 621 |
+
attn_output = self.attn_multiview(
|
| 622 |
+
multivew_hidden_states,
|
| 623 |
+
encoder_hidden_states=multivew_hidden_states,
|
| 624 |
+
position_indices=position_indices,
|
| 625 |
+
n_pbrs=N_pbr,
|
| 626 |
+
**cross_attention_kwargs,
|
| 627 |
+
)
|
| 628 |
+
|
| 629 |
+
attn_output = rearrange(attn_output, "(b n_pbr) (n l) c -> (b n_pbr n) l c", n_pbr=N_pbr, n=num_in_batch)
|
| 630 |
+
|
| 631 |
+
hidden_states = mva_scale * attn_output + hidden_states
|
| 632 |
+
if hidden_states.ndim == 4:
|
| 633 |
+
hidden_states = hidden_states.squeeze(1)
|
| 634 |
+
|
| 635 |
+
# 1.2 GLIGEN Control
|
| 636 |
+
if gligen_kwargs is not None:
|
| 637 |
+
hidden_states = self.fuser(hidden_states, gligen_kwargs["objs"])
|
| 638 |
+
|
| 639 |
+
# 3. Cross-Attention
|
| 640 |
+
if self.attn2 is not None:
|
| 641 |
+
if self.norm_type == "ada_norm":
|
| 642 |
+
norm_hidden_states = self.norm2(hidden_states, timestep)
|
| 643 |
+
elif self.norm_type in ["ada_norm_zero", "layer_norm", "layer_norm_i2vgen"]:
|
| 644 |
+
norm_hidden_states = self.norm2(hidden_states)
|
| 645 |
+
elif self.norm_type == "ada_norm_single":
|
| 646 |
+
# For PixArt norm2 isn't applied here:
|
| 647 |
+
# https://github.com/PixArt-alpha/PixArt-alpha/blob/0f55e922376d8b797edd44d25d0e7464b260dcab/diffusion/model/nets/PixArtMS.py#L70C1-L76C103
|
| 648 |
+
norm_hidden_states = hidden_states
|
| 649 |
+
elif self.norm_type == "ada_norm_continuous":
|
| 650 |
+
norm_hidden_states = self.norm2(hidden_states, added_cond_kwargs["pooled_text_emb"])
|
| 651 |
+
else:
|
| 652 |
+
raise ValueError("Incorrect norm")
|
| 653 |
+
|
| 654 |
+
if self.pos_embed is not None and self.norm_type != "ada_norm_single":
|
| 655 |
+
norm_hidden_states = self.pos_embed(norm_hidden_states)
|
| 656 |
+
|
| 657 |
+
attn_output = self.attn2(
|
| 658 |
+
norm_hidden_states,
|
| 659 |
+
encoder_hidden_states=encoder_hidden_states,
|
| 660 |
+
attention_mask=encoder_attention_mask,
|
| 661 |
+
**cross_attention_kwargs,
|
| 662 |
+
)
|
| 663 |
+
hidden_states = attn_output + hidden_states
|
| 664 |
+
|
| 665 |
+
# dino attn
|
| 666 |
+
if self.use_dino:
|
| 667 |
+
dino_hidden_states = dino_hidden_states.unsqueeze(1).repeat(1, N_pbr * num_in_batch, 1, 1)
|
| 668 |
+
dino_hidden_states = rearrange(dino_hidden_states, "b n l c -> (b n) l c")
|
| 669 |
+
attn_output = self.attn_dino(
|
| 670 |
+
norm_hidden_states,
|
| 671 |
+
encoder_hidden_states=dino_hidden_states,
|
| 672 |
+
attention_mask=None,
|
| 673 |
+
**cross_attention_kwargs,
|
| 674 |
+
)
|
| 675 |
+
|
| 676 |
+
hidden_states = attn_output + hidden_states
|
| 677 |
+
|
| 678 |
+
# 4. Feed-forward
|
| 679 |
+
# i2vgen doesn't have this norm 🤷♂️
|
| 680 |
+
if self.norm_type == "ada_norm_continuous":
|
| 681 |
+
norm_hidden_states = self.norm3(hidden_states, added_cond_kwargs["pooled_text_emb"])
|
| 682 |
+
elif not self.norm_type == "ada_norm_single":
|
| 683 |
+
norm_hidden_states = self.norm3(hidden_states)
|
| 684 |
+
|
| 685 |
+
if self.norm_type == "ada_norm_zero":
|
| 686 |
+
norm_hidden_states = norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None]
|
| 687 |
+
|
| 688 |
+
if self.norm_type == "ada_norm_single":
|
| 689 |
+
norm_hidden_states = self.norm2(hidden_states)
|
| 690 |
+
norm_hidden_states = norm_hidden_states * (1 + scale_mlp) + shift_mlp
|
| 691 |
+
|
| 692 |
+
if self._chunk_size is not None:
|
| 693 |
+
# "feed_forward_chunk_size" can be used to save memory
|
| 694 |
+
ff_output = _chunked_feed_forward(self.ff, norm_hidden_states, self._chunk_dim, self._chunk_size)
|
| 695 |
+
else:
|
| 696 |
+
ff_output = self.ff(norm_hidden_states)
|
| 697 |
+
|
| 698 |
+
if self.norm_type == "ada_norm_zero":
|
| 699 |
+
ff_output = gate_mlp.unsqueeze(1) * ff_output
|
| 700 |
+
elif self.norm_type == "ada_norm_single":
|
| 701 |
+
ff_output = gate_mlp * ff_output
|
| 702 |
+
|
| 703 |
+
hidden_states = ff_output + hidden_states
|
| 704 |
+
if hidden_states.ndim == 4:
|
| 705 |
+
hidden_states = hidden_states.squeeze(1)
|
| 706 |
+
|
| 707 |
+
return hidden_states
|
| 708 |
+
|
| 709 |
+
|
| 710 |
+
class ImageProjModel(torch.nn.Module):
|
| 711 |
+
|
| 712 |
+
"""Projects image embeddings into cross-attention space.
|
| 713 |
+
|
| 714 |
+
Transforms CLIP embeddings into additional context tokens for conditioning.
|
| 715 |
+
|
| 716 |
+
Args:
|
| 717 |
+
cross_attention_dim: Dimension of attention space
|
| 718 |
+
clip_embeddings_dim: Dimension of input CLIP embeddings
|
| 719 |
+
clip_extra_context_tokens: Number of context tokens to generate
|
| 720 |
+
"""
|
| 721 |
+
|
| 722 |
+
def __init__(self, cross_attention_dim=1024, clip_embeddings_dim=1024, clip_extra_context_tokens=4):
|
| 723 |
+
super().__init__()
|
| 724 |
+
|
| 725 |
+
self.generator = None
|
| 726 |
+
self.cross_attention_dim = cross_attention_dim
|
| 727 |
+
self.clip_extra_context_tokens = clip_extra_context_tokens
|
| 728 |
+
self.proj = torch.nn.Linear(clip_embeddings_dim, self.clip_extra_context_tokens * cross_attention_dim)
|
| 729 |
+
self.norm = torch.nn.LayerNorm(cross_attention_dim)
|
| 730 |
+
|
| 731 |
+
def forward(self, image_embeds):
|
| 732 |
+
|
| 733 |
+
"""Projects image embeddings to cross-attention context tokens.
|
| 734 |
+
|
| 735 |
+
Args:
|
| 736 |
+
image_embeds: Input embeddings [B, N, C] or [B, C]
|
| 737 |
+
|
| 738 |
+
Returns:
|
| 739 |
+
torch.Tensor: Context tokens [B, N*clip_extra_context_tokens, cross_attention_dim]
|
| 740 |
+
"""
|
| 741 |
+
|
| 742 |
+
embeds = image_embeds
|
| 743 |
+
num_token = 1
|
| 744 |
+
if embeds.dim() == 3:
|
| 745 |
+
num_token = embeds.shape[1]
|
| 746 |
+
embeds = rearrange(embeds, "b n c -> (b n) c")
|
| 747 |
+
|
| 748 |
+
clip_extra_context_tokens = self.proj(embeds).reshape(
|
| 749 |
+
-1, self.clip_extra_context_tokens, self.cross_attention_dim
|
| 750 |
+
)
|
| 751 |
+
clip_extra_context_tokens = self.norm(clip_extra_context_tokens)
|
| 752 |
+
|
| 753 |
+
clip_extra_context_tokens = rearrange(clip_extra_context_tokens, "(b nt) n c -> b (nt n) c", nt=num_token)
|
| 754 |
+
|
| 755 |
+
return clip_extra_context_tokens
|
| 756 |
+
|
| 757 |
+
|
| 758 |
+
class UNet2p5DConditionModel(torch.nn.Module):
|
| 759 |
+
|
| 760 |
+
"""2.5D UNet extension for multiview PBR generation.
|
| 761 |
+
|
| 762 |
+
Enhances standard 2D UNet with:
|
| 763 |
+
- Multiview attention mechanisms
|
| 764 |
+
- Material-aware processing
|
| 765 |
+
- Position-aware conditioning
|
| 766 |
+
- Dual-stream reference processing
|
| 767 |
+
|
| 768 |
+
Args:
|
| 769 |
+
unet: Base 2D UNet model
|
| 770 |
+
train_sched: Training scheduler (DDPM)
|
| 771 |
+
val_sched: Validation scheduler (EulerAncestral)
|
| 772 |
+
"""
|
| 773 |
+
|
| 774 |
+
def __init__(
|
| 775 |
+
self,
|
| 776 |
+
unet: UNet2DConditionModel,
|
| 777 |
+
train_sched: DDPMScheduler = None,
|
| 778 |
+
val_sched: EulerAncestralDiscreteScheduler = None,
|
| 779 |
+
) -> None:
|
| 780 |
+
super().__init__()
|
| 781 |
+
self.unet = unet
|
| 782 |
+
self.train_sched = train_sched
|
| 783 |
+
self.val_sched = val_sched
|
| 784 |
+
|
| 785 |
+
self.use_ma = True
|
| 786 |
+
self.use_ra = True
|
| 787 |
+
self.use_mda = True
|
| 788 |
+
self.use_dino = True
|
| 789 |
+
self.use_position_rope = True
|
| 790 |
+
self.use_learned_text_clip = True
|
| 791 |
+
self.use_dual_stream = True
|
| 792 |
+
self.pbr_setting = ["albedo", "mr"]
|
| 793 |
+
self.pbr_token_channels = 77
|
| 794 |
+
|
| 795 |
+
if self.use_dual_stream and self.use_ra:
|
| 796 |
+
self.unet_dual = copy.deepcopy(unet)
|
| 797 |
+
self.init_attention(self.unet_dual)
|
| 798 |
+
|
| 799 |
+
self.init_attention(
|
| 800 |
+
self.unet,
|
| 801 |
+
use_ma=self.use_ma,
|
| 802 |
+
use_ra=self.use_ra,
|
| 803 |
+
use_dino=self.use_dino,
|
| 804 |
+
use_mda=self.use_mda,
|
| 805 |
+
pbr_setting=self.pbr_setting,
|
| 806 |
+
)
|
| 807 |
+
self.init_condition(use_dino=self.use_dino)
|
| 808 |
+
|
| 809 |
+
@staticmethod
|
| 810 |
+
def from_pretrained(pretrained_model_name_or_path, **kwargs):
|
| 811 |
+
torch_dtype = kwargs.pop("torch_dtype", torch.float32)
|
| 812 |
+
config_path = os.path.join(pretrained_model_name_or_path, "config.json")
|
| 813 |
+
unet_ckpt_path = os.path.join(pretrained_model_name_or_path, "diffusion_pytorch_model.bin")
|
| 814 |
+
with open(config_path, "r", encoding="utf-8") as file:
|
| 815 |
+
config = json.load(file)
|
| 816 |
+
unet = UNet2DConditionModel(**config)
|
| 817 |
+
unet_2p5d = UNet2p5DConditionModel(unet)
|
| 818 |
+
unet_2p5d.unet.conv_in = torch.nn.Conv2d(
|
| 819 |
+
12,
|
| 820 |
+
unet.conv_in.out_channels,
|
| 821 |
+
kernel_size=unet.conv_in.kernel_size,
|
| 822 |
+
stride=unet.conv_in.stride,
|
| 823 |
+
padding=unet.conv_in.padding,
|
| 824 |
+
dilation=unet.conv_in.dilation,
|
| 825 |
+
groups=unet.conv_in.groups,
|
| 826 |
+
bias=unet.conv_in.bias is not None,
|
| 827 |
+
)
|
| 828 |
+
unet_ckpt = torch.load(unet_ckpt_path, map_location="cpu", weights_only=True)
|
| 829 |
+
unet_2p5d.load_state_dict(unet_ckpt, strict=True)
|
| 830 |
+
unet_2p5d = unet_2p5d.to(torch_dtype)
|
| 831 |
+
return unet_2p5d
|
| 832 |
+
|
| 833 |
+
def init_condition(self, use_dino):
|
| 834 |
+
|
| 835 |
+
"""Initializes conditioning mechanisms for multiview PBR generation.
|
| 836 |
+
|
| 837 |
+
Sets up:
|
| 838 |
+
1. Learned text embeddings: Material-specific tokens (albedo, mr) initialized to zeros
|
| 839 |
+
2. DINO projector: Model to process DINO-ViT features for cross-attention
|
| 840 |
+
|
| 841 |
+
Args:
|
| 842 |
+
use_dino: Flag to enable DINO feature integration
|
| 843 |
+
"""
|
| 844 |
+
|
| 845 |
+
if self.use_learned_text_clip:
|
| 846 |
+
for token in self.pbr_setting:
|
| 847 |
+
self.unet.register_parameter(
|
| 848 |
+
f"learned_text_clip_{token}", nn.Parameter(torch.zeros(self.pbr_token_channels, 1024))
|
| 849 |
+
)
|
| 850 |
+
self.unet.learned_text_clip_ref = nn.Parameter(torch.zeros(self.pbr_token_channels, 1024))
|
| 851 |
+
|
| 852 |
+
if use_dino:
|
| 853 |
+
self.unet.image_proj_model_dino = ImageProjModel(
|
| 854 |
+
cross_attention_dim=self.unet.config.cross_attention_dim,
|
| 855 |
+
clip_embeddings_dim=1536,
|
| 856 |
+
clip_extra_context_tokens=4,
|
| 857 |
+
)
|
| 858 |
+
|
| 859 |
+
def init_attention(self, unet, use_ma=False, use_ra=False, use_mda=False, use_dino=False, pbr_setting=None):
|
| 860 |
+
|
| 861 |
+
"""Recursively replaces standard transformers with enhanced 2.5D blocks.
|
| 862 |
+
|
| 863 |
+
Processes UNet architecture:
|
| 864 |
+
1. Downsampling blocks: Replaces transformers in attention layers
|
| 865 |
+
2. Middle block: Upgrades central transformers
|
| 866 |
+
3. Upsampling blocks: Modifies decoder transformers
|
| 867 |
+
|
| 868 |
+
Args:
|
| 869 |
+
unet: UNet model to enhance
|
| 870 |
+
use_ma: Enable multiview attention
|
| 871 |
+
use_ra: Enable reference attention
|
| 872 |
+
use_mda: Enable material-specific attention
|
| 873 |
+
use_dino: Enable DINO feature integration
|
| 874 |
+
pbr_setting: List of PBR materials
|
| 875 |
+
"""
|
| 876 |
+
|
| 877 |
+
for down_block_i, down_block in enumerate(unet.down_blocks):
|
| 878 |
+
if hasattr(down_block, "has_cross_attention") and down_block.has_cross_attention:
|
| 879 |
+
for attn_i, attn in enumerate(down_block.attentions):
|
| 880 |
+
for transformer_i, transformer in enumerate(attn.transformer_blocks):
|
| 881 |
+
if isinstance(transformer, BasicTransformerBlock):
|
| 882 |
+
attn.transformer_blocks[transformer_i] = Basic2p5DTransformerBlock(
|
| 883 |
+
transformer,
|
| 884 |
+
f"down_{down_block_i}_{attn_i}_{transformer_i}",
|
| 885 |
+
use_ma,
|
| 886 |
+
use_ra,
|
| 887 |
+
use_mda,
|
| 888 |
+
use_dino,
|
| 889 |
+
pbr_setting,
|
| 890 |
+
)
|
| 891 |
+
|
| 892 |
+
if hasattr(unet.mid_block, "has_cross_attention") and unet.mid_block.has_cross_attention:
|
| 893 |
+
for attn_i, attn in enumerate(unet.mid_block.attentions):
|
| 894 |
+
for transformer_i, transformer in enumerate(attn.transformer_blocks):
|
| 895 |
+
if isinstance(transformer, BasicTransformerBlock):
|
| 896 |
+
attn.transformer_blocks[transformer_i] = Basic2p5DTransformerBlock(
|
| 897 |
+
transformer, f"mid_{attn_i}_{transformer_i}", use_ma, use_ra, use_mda, use_dino, pbr_setting
|
| 898 |
+
)
|
| 899 |
+
|
| 900 |
+
for up_block_i, up_block in enumerate(unet.up_blocks):
|
| 901 |
+
if hasattr(up_block, "has_cross_attention") and up_block.has_cross_attention:
|
| 902 |
+
for attn_i, attn in enumerate(up_block.attentions):
|
| 903 |
+
for transformer_i, transformer in enumerate(attn.transformer_blocks):
|
| 904 |
+
if isinstance(transformer, BasicTransformerBlock):
|
| 905 |
+
attn.transformer_blocks[transformer_i] = Basic2p5DTransformerBlock(
|
| 906 |
+
transformer,
|
| 907 |
+
f"up_{up_block_i}_{attn_i}_{transformer_i}",
|
| 908 |
+
use_ma,
|
| 909 |
+
use_ra,
|
| 910 |
+
use_mda,
|
| 911 |
+
use_dino,
|
| 912 |
+
pbr_setting,
|
| 913 |
+
)
|
| 914 |
+
|
| 915 |
+
def __getattr__(self, name: str):
|
| 916 |
+
try:
|
| 917 |
+
return super().__getattr__(name)
|
| 918 |
+
except AttributeError:
|
| 919 |
+
return getattr(self.unet, name)
|
| 920 |
+
|
| 921 |
+
def forward(
|
| 922 |
+
self,
|
| 923 |
+
sample,
|
| 924 |
+
timestep,
|
| 925 |
+
encoder_hidden_states,
|
| 926 |
+
*args,
|
| 927 |
+
added_cond_kwargs=None,
|
| 928 |
+
cross_attention_kwargs=None,
|
| 929 |
+
down_intrablock_additional_residuals=None,
|
| 930 |
+
down_block_res_samples=None,
|
| 931 |
+
mid_block_res_sample=None,
|
| 932 |
+
**cached_condition,
|
| 933 |
+
):
|
| 934 |
+
|
| 935 |
+
"""Forward pass with multiview/material conditioning.
|
| 936 |
+
|
| 937 |
+
Key stages:
|
| 938 |
+
1. Input preparation (concat normal/position maps)
|
| 939 |
+
2. Reference feature extraction (dual-stream)
|
| 940 |
+
3. Position encoding (voxel indices)
|
| 941 |
+
4. DINO feature projection
|
| 942 |
+
5. Main UNet processing with attention conditioning
|
| 943 |
+
|
| 944 |
+
Args:
|
| 945 |
+
sample: Input latents [B, N_pbr, N_gen, C, H, W]
|
| 946 |
+
cached_condition: Dictionary containing:
|
| 947 |
+
- embeds_normal: Normal map embeddings
|
| 948 |
+
- embeds_position: Position map embeddings
|
| 949 |
+
- ref_latents: Reference image latents
|
| 950 |
+
- dino_hidden_states: DINO features
|
| 951 |
+
- position_maps: 3D position maps
|
| 952 |
+
- mva_scale: Multiview attention scale
|
| 953 |
+
- ref_scale: Reference attention scale
|
| 954 |
+
|
| 955 |
+
Returns:
|
| 956 |
+
torch.Tensor: Output features
|
| 957 |
+
"""
|
| 958 |
+
|
| 959 |
+
B, N_pbr, N_gen, _, H, W = sample.shape
|
| 960 |
+
assert H == W
|
| 961 |
+
|
| 962 |
+
if "cache" not in cached_condition:
|
| 963 |
+
cached_condition["cache"] = {}
|
| 964 |
+
|
| 965 |
+
sample = [sample]
|
| 966 |
+
if "embeds_normal" in cached_condition:
|
| 967 |
+
sample.append(cached_condition["embeds_normal"].unsqueeze(1).repeat(1, N_pbr, 1, 1, 1, 1))
|
| 968 |
+
if "embeds_position" in cached_condition:
|
| 969 |
+
sample.append(cached_condition["embeds_position"].unsqueeze(1).repeat(1, N_pbr, 1, 1, 1, 1))
|
| 970 |
+
sample = torch.cat(sample, dim=-3)
|
| 971 |
+
|
| 972 |
+
sample = rearrange(sample, "b n_pbr n c h w -> (b n_pbr n) c h w")
|
| 973 |
+
|
| 974 |
+
encoder_hidden_states_gen = encoder_hidden_states.unsqueeze(-3).repeat(1, 1, N_gen, 1, 1)
|
| 975 |
+
encoder_hidden_states_gen = rearrange(encoder_hidden_states_gen, "b n_pbr n l c -> (b n_pbr n) l c")
|
| 976 |
+
|
| 977 |
+
if added_cond_kwargs is not None:
|
| 978 |
+
text_embeds_gen = added_cond_kwargs["text_embeds"].unsqueeze(1).repeat(1, N_gen, 1)
|
| 979 |
+
text_embeds_gen = rearrange(text_embeds_gen, "b n c -> (b n) c")
|
| 980 |
+
time_ids_gen = added_cond_kwargs["time_ids"].unsqueeze(1).repeat(1, N_gen, 1)
|
| 981 |
+
time_ids_gen = rearrange(time_ids_gen, "b n c -> (b n) c")
|
| 982 |
+
added_cond_kwargs_gen = {"text_embeds": text_embeds_gen, "time_ids": time_ids_gen}
|
| 983 |
+
else:
|
| 984 |
+
added_cond_kwargs_gen = None
|
| 985 |
+
|
| 986 |
+
if self.use_position_rope:
|
| 987 |
+
if "position_voxel_indices" in cached_condition["cache"]:
|
| 988 |
+
position_voxel_indices = cached_condition["cache"]["position_voxel_indices"]
|
| 989 |
+
else:
|
| 990 |
+
if "position_maps" in cached_condition:
|
| 991 |
+
position_voxel_indices = calc_multires_voxel_idxs(
|
| 992 |
+
cached_condition["position_maps"],
|
| 993 |
+
grid_resolutions=[H, H // 2, H // 4, H // 8],
|
| 994 |
+
voxel_resolutions=[H * 8, H * 4, H * 2, H],
|
| 995 |
+
)
|
| 996 |
+
cached_condition["cache"]["position_voxel_indices"] = position_voxel_indices
|
| 997 |
+
else:
|
| 998 |
+
position_voxel_indices = None
|
| 999 |
+
|
| 1000 |
+
if self.use_dino:
|
| 1001 |
+
if "dino_hidden_states_proj" in cached_condition["cache"]:
|
| 1002 |
+
dino_hidden_states = cached_condition["cache"]["dino_hidden_states_proj"]
|
| 1003 |
+
else:
|
| 1004 |
+
assert "dino_hidden_states" in cached_condition
|
| 1005 |
+
dino_hidden_states = cached_condition["dino_hidden_states"]
|
| 1006 |
+
dino_hidden_states = self.image_proj_model_dino(dino_hidden_states)
|
| 1007 |
+
cached_condition["cache"]["dino_hidden_states_proj"] = dino_hidden_states
|
| 1008 |
+
else:
|
| 1009 |
+
dino_hidden_states = None
|
| 1010 |
+
|
| 1011 |
+
if self.use_ra:
|
| 1012 |
+
if "condition_embed_dict" in cached_condition["cache"]:
|
| 1013 |
+
condition_embed_dict = cached_condition["cache"]["condition_embed_dict"]
|
| 1014 |
+
else:
|
| 1015 |
+
condition_embed_dict = {}
|
| 1016 |
+
ref_latents = cached_condition["ref_latents"]
|
| 1017 |
+
N_ref = ref_latents.shape[1]
|
| 1018 |
+
|
| 1019 |
+
if not self.use_dual_stream:
|
| 1020 |
+
ref_latents = [ref_latents]
|
| 1021 |
+
if "embeds_normal" in cached_condition:
|
| 1022 |
+
ref_latents.append(torch.zeros_like(ref_latents[0]))
|
| 1023 |
+
if "embeds_position" in cached_condition:
|
| 1024 |
+
ref_latents.append(torch.zeros_like(ref_latents[0]))
|
| 1025 |
+
ref_latents = torch.cat(ref_latents, dim=2)
|
| 1026 |
+
|
| 1027 |
+
ref_latents = rearrange(ref_latents, "b n c h w -> (b n) c h w")
|
| 1028 |
+
|
| 1029 |
+
encoder_hidden_states_ref = self.unet.learned_text_clip_ref.repeat(B, N_ref, 1, 1)
|
| 1030 |
+
|
| 1031 |
+
encoder_hidden_states_ref = rearrange(encoder_hidden_states_ref, "b n l c -> (b n) l c")
|
| 1032 |
+
|
| 1033 |
+
if added_cond_kwargs is not None:
|
| 1034 |
+
text_embeds_ref = added_cond_kwargs["text_embeds"].unsqueeze(1).repeat(1, N_ref, 1)
|
| 1035 |
+
text_embeds_ref = rearrange(text_embeds_ref, "b n c -> (b n) c")
|
| 1036 |
+
time_ids_ref = added_cond_kwargs["time_ids"].unsqueeze(1).repeat(1, N_ref, 1)
|
| 1037 |
+
time_ids_ref = rearrange(time_ids_ref, "b n c -> (b n) c")
|
| 1038 |
+
added_cond_kwargs_ref = {
|
| 1039 |
+
"text_embeds": text_embeds_ref,
|
| 1040 |
+
"time_ids": time_ids_ref,
|
| 1041 |
+
}
|
| 1042 |
+
else:
|
| 1043 |
+
added_cond_kwargs_ref = None
|
| 1044 |
+
|
| 1045 |
+
noisy_ref_latents = ref_latents
|
| 1046 |
+
timestep_ref = 0
|
| 1047 |
+
if self.use_dual_stream:
|
| 1048 |
+
unet_ref = self.unet_dual
|
| 1049 |
+
else:
|
| 1050 |
+
unet_ref = self.unet
|
| 1051 |
+
unet_ref(
|
| 1052 |
+
noisy_ref_latents,
|
| 1053 |
+
timestep_ref,
|
| 1054 |
+
encoder_hidden_states=encoder_hidden_states_ref,
|
| 1055 |
+
class_labels=None,
|
| 1056 |
+
added_cond_kwargs=added_cond_kwargs_ref,
|
| 1057 |
+
# **kwargs
|
| 1058 |
+
return_dict=False,
|
| 1059 |
+
cross_attention_kwargs={
|
| 1060 |
+
"mode": "w",
|
| 1061 |
+
"num_in_batch": N_ref,
|
| 1062 |
+
"condition_embed_dict": condition_embed_dict,
|
| 1063 |
+
},
|
| 1064 |
+
)
|
| 1065 |
+
cached_condition["cache"]["condition_embed_dict"] = condition_embed_dict
|
| 1066 |
+
else:
|
| 1067 |
+
condition_embed_dict = None
|
| 1068 |
+
|
| 1069 |
+
mva_scale = cached_condition.get("mva_scale", 1.0)
|
| 1070 |
+
ref_scale = cached_condition.get("ref_scale", 1.0)
|
| 1071 |
+
|
| 1072 |
+
return self.unet(
|
| 1073 |
+
sample,
|
| 1074 |
+
timestep,
|
| 1075 |
+
encoder_hidden_states_gen,
|
| 1076 |
+
*args,
|
| 1077 |
+
class_labels=None,
|
| 1078 |
+
added_cond_kwargs=added_cond_kwargs_gen,
|
| 1079 |
+
down_intrablock_additional_residuals=(
|
| 1080 |
+
[sample.to(dtype=self.unet.dtype) for sample in down_intrablock_additional_residuals]
|
| 1081 |
+
if down_intrablock_additional_residuals is not None
|
| 1082 |
+
else None
|
| 1083 |
+
),
|
| 1084 |
+
down_block_additional_residuals=(
|
| 1085 |
+
[sample.to(dtype=self.unet.dtype) for sample in down_block_res_samples]
|
| 1086 |
+
if down_block_res_samples is not None
|
| 1087 |
+
else None
|
| 1088 |
+
),
|
| 1089 |
+
mid_block_additional_residual=(
|
| 1090 |
+
mid_block_res_sample.to(dtype=self.unet.dtype) if mid_block_res_sample is not None else None
|
| 1091 |
+
),
|
| 1092 |
+
return_dict=False,
|
| 1093 |
+
cross_attention_kwargs={
|
| 1094 |
+
"mode": "r",
|
| 1095 |
+
"num_in_batch": N_gen,
|
| 1096 |
+
"dino_hidden_states": dino_hidden_states,
|
| 1097 |
+
"condition_embed_dict": condition_embed_dict,
|
| 1098 |
+
"mva_scale": mva_scale,
|
| 1099 |
+
"ref_scale": ref_scale,
|
| 1100 |
+
"position_voxel_indices": position_voxel_indices,
|
| 1101 |
+
},
|
| 1102 |
+
)
|