-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmodel.py
More file actions
685 lines (567 loc) · 27.4 KB
/
Copy pathmodel.py
File metadata and controls
685 lines (567 loc) · 27.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# --------------------------------------------------------
# References:
# SiT: https://github.com/willisma/SiT
# Lightning-DiT: https://github.com/hustvl/LightningDiT
# --------------------------------------------------------
import os
import torch
import torch.nn as nn
import math
import torch.nn.functional as F
from util.model_util import get_2d_sincos_pos_embed, RMSNorm, rotate_half
from util.paths import repo_path
try:
from flash_attn_interface import flash_attn_func
FA3_AVAILABLE = True
except ImportError:
FA3_AVAILABLE = False
def modulate(x, shift, scale):
return x * (1 + scale.unsqueeze(1)) + shift.unsqueeze(1)
class DynamicRoPE:
"""Applies precomputed 2-D rotary position embeddings to q/k."""
def __init__(self, cos, sin):
self.cos = cos
self.sin = sin
def __call__(self, t):
return t * self.cos + rotate_half(t) * self.sin
class BottleneckPatchEmbed(nn.Module):
""" Image to Patch Embedding
"""
def __init__(self, img_size=224, patch_size=16, in_chans=3, pca_dim=768, embed_dim=768, bias=True):
super().__init__()
img_size = (img_size, img_size)
patch_size = (patch_size, patch_size)
num_patches = (img_size[1] // patch_size[1]) * (img_size[0] // patch_size[0])
self.img_size = img_size
self.patch_size = patch_size
self.num_patches = num_patches
self.proj1 = nn.Conv2d(in_chans, pca_dim, kernel_size=patch_size, stride=patch_size, bias=False)
self.proj2 = nn.Conv2d(pca_dim, embed_dim, kernel_size=1, stride=1, bias=bias)
def forward(self, x):
B, C, H, W = x.shape
# Allow any size divisible by patch_size (support rectangular inputs)
assert H % self.patch_size[0] == 0 and W % self.patch_size[1] == 0, \
f"Input size ({H}x{W}) must be divisible by patch size ({self.patch_size[0]}x{self.patch_size[1]})"
x = self.proj2(self.proj1(x)).flatten(2).transpose(1, 2)
return x
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.
: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.
"""
# https://github.com/openai/glide-text2im/blob/main/glide_text2im/nn.py
half = dim // 2
freqs = torch.exp(
-math.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
def scaled_dot_product_attention_flash3(query, key, value, dropout_p=0.0) -> torch.Tensor:
"""
Computes scaled dot product attention using the flash_attn library (FA3).
Args:
query: Shape (Batch, NumHeads, SeqLen_Q, HeadDim)
key: Shape (Batch, NumHeads, SeqLen_K, HeadDim)
value: Shape (Batch, NumHeads, SeqLen_K, HeadDim)
Returns:
output: Shape (Batch, NumHeads, SeqLen_Q, HeadDim)
"""
assert FA3_AVAILABLE, "flash_attn_func is not available. Please install flash attention library."
# 1. Permute dimensions from (B, H, S, D) to (B, S, H, D)
# Flash Attention requires the 'Heads' dimension to be 3rd, not 2nd.
# Mannually cast to bfloat16 for better performance and compatibility.
q = query.transpose(1, 2).to(torch.bfloat16)
k = key.transpose(1, 2).to(torch.bfloat16)
v = value.transpose(1, 2).to(torch.bfloat16)
# 2. Run Flash Attention
# Note: softmax_scale defaults to 1/sqrt(d) if not provided.
assert dropout_p == 0.0, "Flash Attention 3 currently does not support dropout."
# https://github.com/Dao-AILab/flash-attention/issues/1377
output = flash_attn_func(
q, k, v,
# dropout_p=dropout_p,
)[0]
# 3. Permute back to (B, H, S, D)
return output.transpose(1, 2)
class Attention(nn.Module):
def __init__(self, dim, num_heads=8, qkv_bias=True, qk_norm=True, attn_drop=0., proj_drop=0.,
attention_type='torch'):
super().__init__()
self.num_heads = num_heads
head_dim = dim // num_heads
self.attention_type = attention_type
self.q_norm = RMSNorm(head_dim) if qk_norm else nn.Identity()
self.k_norm = RMSNorm(head_dim) if qk_norm else nn.Identity()
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
self.attn_drop = nn.Dropout(attn_drop)
self.proj = nn.Linear(dim, dim)
self.proj_drop = nn.Dropout(proj_drop)
def forward(self, x, rope):
B, N, C = x.shape
qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4)
q, k, v = qkv[0], qkv[1], qkv[2] # make torchscript happy (cannot use tensor as tuple)
q = self.q_norm(q)
k = self.k_norm(k)
q = rope(q)
k = rope(k)
if self.attention_type == 'torch':
x = F.scaled_dot_product_attention(q, k, v, attn_mask=None, dropout_p=self.attn_drop.p if self.training else 0.,)
elif self.attention_type == 'flash3':
x = scaled_dot_product_attention_flash3(q, k, v, dropout_p=self.attn_drop.p if self.training else 0.)
else:
raise NotImplementedError(f'Attention type {self.attention_type} not implemented')
x = x.transpose(1, 2).reshape(B, N, C)
x = self.proj(x)
x = self.proj_drop(x)
return x
class SwiGLUFFN(nn.Module):
def __init__(
self,
dim: int,
hidden_dim: int,
drop=0.0,
bias=True
) -> None:
super().__init__()
hidden_dim = int(hidden_dim * 2 / 3)
self.w12 = nn.Linear(dim, 2 * hidden_dim, bias=bias)
self.w3 = nn.Linear(hidden_dim, dim, bias=bias)
self.ffn_dropout = nn.Dropout(drop)
def forward(self, x):
x12 = self.w12(x)
x1, x2 = x12.chunk(2, dim=-1)
hidden = F.silu(x1) * x2
return self.w3(self.ffn_dropout(hidden))
class FinalLayer(nn.Module):
"""
The final layer of PointDiT.
"""
def __init__(self, hidden_size, patch_size, out_channels):
super().__init__()
self.norm_final = RMSNorm(hidden_size)
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, c):
shift, scale = self.adaLN_modulation(c).chunk(2, dim=1)
x = modulate(self.norm_final(x), shift, scale)
return self.linear(x)
class PointDiTBlock(nn.Module):
def __init__(self, hidden_size, num_heads, mlp_ratio=4.0, attn_drop=0.0, proj_drop=0.0,
attention_type='torch'):
super().__init__()
self.norm1 = RMSNorm(hidden_size, eps=1e-6)
self.attn = Attention(hidden_size, num_heads=num_heads, qkv_bias=True, qk_norm=True,
attn_drop=attn_drop, proj_drop=proj_drop,
attention_type=attention_type)
self.norm2 = RMSNorm(hidden_size, eps=1e-6)
mlp_hidden_dim = int(hidden_size * mlp_ratio)
self.mlp = SwiGLUFFN(hidden_size, mlp_hidden_dim, drop=proj_drop)
self.adaLN_modulation = nn.Sequential(
nn.SiLU(),
nn.Linear(hidden_size, 6 * hidden_size, bias=True)
)
def forward(self, x, c, feat_rope):
shift_msa, scale_msa, gate_msa, shift_mlp, scale_mlp, gate_mlp = self.adaLN_modulation(c).chunk(6, dim=-1)
x = x + gate_msa.unsqueeze(1) * self.attn(modulate(self.norm1(x), shift_msa, scale_msa), rope=feat_rope)
x = x + gate_mlp.unsqueeze(1) * self.mlp(modulate(self.norm2(x), shift_mlp, scale_mlp))
return x
class PointDiT(nn.Module):
"""
Pixel-space diffusion transformer that denoises a point map, conditioned on
frozen DINOv3 features of the input image.
"""
def __init__(
self,
input_size=256,
patch_size=16,
in_channels=3,
hidden_size=1024,
depth=24,
num_heads=16,
mlp_ratio=4.0,
attn_drop=0.0,
proj_drop=0.0,
bottleneck_dim=128,
attention_type='torch',
feature_embedding_type='dinov3_vitb16',
dinov3_use_intermediate_layers=True,
dinov3_num_intermediate_layers=4,
finetune_feature_embedding=False,
):
super().__init__()
self.in_channels = in_channels
self.out_channels = in_channels
self.patch_size = patch_size
self.num_heads = num_heads
self.hidden_size = hidden_size
self.input_size = input_size
self.attention_type = attention_type
self.feature_embedding_type = feature_embedding_type
self.dinov3_use_intermediate_layers = dinov3_use_intermediate_layers
self.dinov3_num_intermediate_layers = dinov3_num_intermediate_layers
self.finetune_feature_embedding = finetune_feature_embedding
# time and class embed
self.t_embedder = TimestepEmbedder(hidden_size)
# frozen pre-trained image encoder producing the conditioning tokens
if self.feature_embedding_type.startswith('dinov3'):
vit_type = self.feature_embedding_type.split('_')[-1]
assert vit_type in ['vits16', 'vits16plus', 'vitb16', 'vitl16', 'vith16plus', 'vit7b16'], f'ViT type {vit_type} not supported for DINOv3 feature embedding'
sha_dict = {
'vits16': '08c60483',
'vits16plus': '4057cbaa',
'vitb16': '73cec8be',
'vitl16': '8aa4cbdd',
'vith16plus': '7c1da9a5',
'vit7b16': 'a955f4ea',
}
sha = sha_dict[vit_type]
# Configurable so the model can be built on any machine. Defaults assume the
# DINOv3 checkout in third_party/ and the gated weights in pretrained/dinov3/,
# both resolved inside the repository rather than against the cwd.
dinov3_repo = os.environ.get(
'DINOV3_REPO', repo_path('third_party/dinov3'))
dinov3_weights_dir = os.environ.get(
'DINOV3_WEIGHTS_DIR', repo_path('pretrained/dinov3'))
local_weights_path = os.path.join(
dinov3_weights_dir, f'dinov3_{vit_type}_pretrain_lvd1689m-{sha}.pth')
if not os.path.isdir(dinov3_repo):
raise FileNotFoundError(
f'DINOv3 repository not found at "{dinov3_repo}". Clone it with\n'
f' git clone https://github.com/facebookresearch/dinov3.git third_party/dinov3\n'
f'or set DINOV3_REPO to an existing checkout.')
# NOTE: need to load the weights in the main function due to the random init of the __init__
if os.path.isfile(local_weights_path):
self.y_embedder = torch.hub.load(
dinov3_repo, f'dinov3_{vit_type}', source='local', weights=local_weights_path)
else:
self.y_embedder = torch.hub.load(
dinov3_repo, f'dinov3_{vit_type}', source='local', pretrained=False)
# freeze DINOv3 parameters
if not self.finetune_feature_embedding:
for param in self.y_embedder.parameters():
param.requires_grad = False
self.y_embedder.num_patches = self.y_embedder.patch_embed.num_patches
# compute equally-spaced layer indices for intermediate feature extraction
if self.dinov3_use_intermediate_layers:
dinov3_depth = len(self.y_embedder.blocks)
n = self.dinov3_num_intermediate_layers
# n equally-spaced layers
self.dinov3_intermediate_layer_indices = [
int((i + 1) * dinov3_depth / n) - 1 for i in range(n)
]
self.y_embedder.num_features = self.y_embedder.num_features * n
else:
raise NotImplementedError(f'Feature embedding type {self.feature_embedding_type} not implemented')
# linear embed
self.x_embedder = BottleneckPatchEmbed(input_size, patch_size, in_channels, bottleneck_dim, hidden_size, bias=True)
# use fixed sin-cos embedding
num_patches = self.x_embedder.num_patches
self.pos_embed = nn.Parameter(torch.zeros(1, num_patches, hidden_size), requires_grad=False)
# position embedding for the conditioning image tokens (frozen sin-cos, added in
# forward); only needed when the encoder's feature dim differs from the model dim.
y_feat_dim = self.y_embedder.num_features
if y_feat_dim != hidden_size:
self.pos_embed_y = nn.Parameter(torch.zeros(1, num_patches, y_feat_dim), requires_grad=False)
else:
self.pos_embed_y = None
# Image and point tokens are concatenated along the channel dim, then projected
# back to the model dim (this is PointDiT's conditioning mechanism).
self.concat_proj = nn.Linear(y_feat_dim + hidden_size, hidden_size, bias=True)
# transformer
self.blocks = nn.ModuleList([
PointDiTBlock(hidden_size, num_heads, mlp_ratio=mlp_ratio,
attn_drop=attn_drop if (depth // 4 * 3 > i >= depth // 4) else 0.0,
proj_drop=proj_drop if (depth // 4 * 3 > i >= depth // 4) else 0.0,
attention_type=attention_type,
)
for i in range(depth)
])
# output head: linear D -> patch_size^2 * 3, unpatchified back to H x W x 3
self.final_layer = FinalLayer(hidden_size, patch_size, self.out_channels)
self.initialize_weights()
def initialize_weights(self):
# 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 (and freeze) pos_embed by sin-cos embedding:
pos_embed = get_2d_sincos_pos_embed(self.pos_embed.shape[-1], int(self.x_embedder.num_patches ** 0.5))
self.pos_embed.data.copy_(torch.from_numpy(pos_embed).float().unsqueeze(0))
if hasattr(self, 'pos_embed_y') and self.pos_embed_y is not None:
# Initialize (and freeze) pos_embed_y by sin-cos embedding:
# TODO: assuming same number of patches as pointmap
pos_embed_y = get_2d_sincos_pos_embed(self.pos_embed_y.shape[-1], int(self.x_embedder.num_patches ** 0.5))
self.pos_embed_y.data.copy_(torch.from_numpy(pos_embed_y).float().unsqueeze(0))
# Initialize patch_embed like nn.Linear (instead of nn.Conv2d):
w1 = self.x_embedder.proj1.weight.data
nn.init.xavier_uniform_(w1.view([w1.shape[0], -1]))
w2 = self.x_embedder.proj2.weight.data
nn.init.xavier_uniform_(w2.view([w2.shape[0], -1]))
nn.init.constant_(self.x_embedder.proj2.bias, 0)
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:
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.final_layer.adaLN_modulation[-1].weight, 0)
nn.init.constant_(self.final_layer.adaLN_modulation[-1].bias, 0)
nn.init.constant_(self.final_layer.linear.weight, 0)
nn.init.constant_(self.final_layer.linear.bias, 0)
def interpolate_pos_encoding(self, h, w):
"""
Interpolate position embeddings to handle variable input sizes.
h, w: number of patches in height and width
Returns: pos_embed [1, h*w, C]
"""
N = h * w
if N == self.pos_embed.shape[1]:
# Check if it's the same square size
orig_size = int(self.pos_embed.shape[1] ** 0.5)
if h == orig_size and w == orig_size:
return self.pos_embed
# Check cache
cache_key = (h, w)
if not hasattr(self, '_pos_embed_cache'):
self._pos_embed_cache = {}
if cache_key in self._pos_embed_cache:
return self._pos_embed_cache[cache_key]
# Get original grid size (square)
orig_size = int(self.pos_embed.shape[1] ** 0.5)
# Reshape to 2D grid
pos_embed = self.pos_embed.reshape(1, orig_size, orig_size, -1).permute(0, 3, 1, 2)
# Interpolate to new size
pos_embed = F.interpolate(pos_embed, size=(h, w), mode='bicubic', align_corners=False)
# Reshape back
pos_embed = pos_embed.permute(0, 2, 3, 1).reshape(1, h * w, -1)
if len(self._pos_embed_cache) > 64:
self._pos_embed_cache.clear()
self._pos_embed_cache[cache_key] = pos_embed
return pos_embed
def interpolate_pos_encoding_y(self, h, w):
"""
Interpolate y position embeddings (for image features) to handle variable input sizes.
h, w: number of patches in height and width
Returns: pos_embed_y [1, h*w, C]
"""
if self.pos_embed_y is None:
return self.interpolate_pos_encoding(h, w)
N = h * w
if N == self.pos_embed_y.shape[1]:
orig_size = int(self.pos_embed_y.shape[1] ** 0.5)
if h == orig_size and w == orig_size:
return self.pos_embed_y
# Check cache
cache_key = (h, w)
if not hasattr(self, '_pos_embed_y_cache'):
self._pos_embed_y_cache = {}
if cache_key in self._pos_embed_y_cache:
return self._pos_embed_y_cache[cache_key]
orig_size = int(self.pos_embed_y.shape[1] ** 0.5)
pos_embed = self.pos_embed_y.reshape(1, orig_size, orig_size, -1).permute(0, 3, 1, 2)
pos_embed = F.interpolate(pos_embed, size=(h, w), mode='bicubic', align_corners=False)
pos_embed = pos_embed.permute(0, 2, 3, 1).reshape(1, h * w, -1)
if len(self._pos_embed_y_cache) > 64:
self._pos_embed_y_cache.clear()
self._pos_embed_y_cache[cache_key] = pos_embed
return pos_embed
def get_rope_for_size(self, h, w, device):
"""
Generate rotary position embeddings for given grid size (cached).
h, w: number of patches in height and width
Returns: (cos, sin) each of shape [h*w, D]
"""
cache_key = (h, w, device)
if not hasattr(self, '_rope_cache'):
self._rope_cache = {}
if cache_key in self._rope_cache:
return self._rope_cache[cache_key]
half_head_dim = self.hidden_size // self.num_heads // 2
theta = 10000
dim = half_head_dim
freqs = 1. / (theta ** (torch.arange(0, dim, 2, device=device)[:(dim // 2)].float() / dim))
# Create position indices for h and w
t_h = torch.arange(h, device=device).float()
t_w = torch.arange(w, device=device).float()
# Compute frequencies for each dimension
freqs_h = torch.einsum('i,j->ij', t_h, freqs) # [h, D/4]
freqs_w = torch.einsum('i,j->ij', t_w, freqs) # [w, D/4]
# Repeat for complex representation (interleave)
freqs_h = freqs_h.repeat_interleave(2, dim=-1) # [h, D/2]
freqs_w = freqs_w.repeat_interleave(2, dim=-1) # [w, D/2]
# Create 2D grid: [h, w, D]
freqs_grid = torch.cat([
freqs_h[:, None, :].expand(-1, w, -1),
freqs_w[None, :, :].expand(h, -1, -1)
], dim=-1)
# Flatten to [h*w, D]
freqs_flat = freqs_grid.reshape(h * w, -1)
result = (freqs_flat.cos(), freqs_flat.sin())
if len(self._rope_cache) > 64:
self._rope_cache.clear()
self._rope_cache[cache_key] = result
return result
def unpatchify(self, x, p, h=None, w=None):
"""
x: (N, T, patch_size**2 * C)
h, w: number of patches (if None, assumes square)
imgs: (N, C, H*p, W*p)
"""
c = self.out_channels
if h is None or w is None:
h = w = int(x.shape[1] ** 0.5)
assert h * w == x.shape[1], f"h*w ({h}*{w}={h*w}) != num_patches ({x.shape[1]})"
x = x.reshape(shape=(x.shape[0], h, w, p, p, c))
x = torch.einsum('nhwpqc->nchpwq', x)
imgs = x.reshape(shape=(x.shape[0], c, h * p, w * p))
return imgs
def forward(self, x, t, y, cached_y_emb=None):
"""
x: (N, C, H, W)
t: (N,)
y: (N,) or (N, C, H, W)
cached_y_emb: Optional pre-computed y embedding [N, L, C] for sampling efficiency
"""
B, C, H, W = x.shape
h_patches = H // self.patch_size
w_patches = W // self.patch_size
# Interpolate position embeddings for current size
pos_embed = self.interpolate_pos_encoding(h_patches, w_patches)
# Generate RoPE for current size
rope_cos, rope_sin = self.get_rope_for_size(h_patches, w_patches, x.device)
# class and time embeddings
t_emb = self.t_embedder(t)
# Use cached y_emb if provided (for sampling efficiency), otherwise compute fresh
if cached_y_emb is not None:
y_emb = cached_y_emb
else:
if self.feature_embedding_type.startswith('dinov3'):
# DINOv3 image normalization
y = (y + 1) / 2. # to [0, 1]
mean = torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1).to(y.device)
std = torch.tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1).to(y.device)
y = (y - mean) / std
# downsample input when necessary
if self.patch_size != 16:
downsample_factor = self.patch_size // 16
y = F.interpolate(y, scale_factor=1.0 / downsample_factor, mode='bilinear', align_corners=False)
# DINOv3 feature embedding
# patch size 16
if self.dinov3_use_intermediate_layers:
# Extract features from n equally-spaced layers and concatenate
intermediate_features = self.y_embedder.get_intermediate_layers(
y, n=self.dinov3_intermediate_layer_indices, norm=True
)
# intermediate_features is a tuple of n tensors, each [B, num_patches, embed_dim]
y_emb = torch.cat(intermediate_features, dim=-1) # [B, num_patches, n * embed_dim]
else:
# Use only final layer features
y_emb = self.y_embedder.forward_features(y)['x_norm_patchtokens']
else:
raise NotImplementedError(f'Feature embedding type {self.feature_embedding_type} not implemented')
c = t_emb
# add pos embedding (interpolated for current size) - skip if using cached (already included)
if cached_y_emb is None:
pos_embed_y = self.interpolate_pos_encoding_y(h_patches, w_patches)
y_emb = y_emb + pos_embed_y
# forward PointDiT
x = self.x_embedder(x)
x = x + pos_embed
# Create dynamic RoPE for current size
feat_rope = DynamicRoPE(rope_cos, rope_sin)
# concat image and point tokens along the channel dim, then project to the model dim
x = torch.cat([y_emb, x], dim=-1) # [N, L, y_feat_dim + C]
x = self.concat_proj(x) # [N, L, C]
for block in self.blocks:
x = block(x, c, feat_rope)
x = self.final_layer(x, c)
return self.unpatchify(x, self.patch_size, h_patches, w_patches)
def extract_y_embedding(self, y, h_patches, w_patches):
"""
Extract DINOv3 feature embeddings from conditioning image.
Used for caching during sampling to avoid redundant feature extraction.
Args:
y: Conditioning image tensor [B, 3, H, W] in range [-1, 1]
h_patches: Number of patches in height (for position embedding)
w_patches: Number of patches in width (for position embedding)
Returns:
y_emb: Feature embeddings [B, num_patches, feature_dim]
"""
if self.feature_embedding_type.startswith('dinov3'):
# DINOv3 image normalization
y = (y + 1) / 2. # to [0, 1]
mean = torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1).to(y.device)
std = torch.tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1).to(y.device)
y = (y - mean) / std
if self.patch_size != 16:
downsample_factor = self.patch_size // 16
y = F.interpolate(y, scale_factor=1.0 / downsample_factor, mode='bilinear', align_corners=False)
if self.dinov3_use_intermediate_layers:
intermediate_features = self.y_embedder.get_intermediate_layers(
y, n=self.dinov3_intermediate_layer_indices, norm=True
)
y_emb = torch.cat(intermediate_features, dim=-1)
else:
y_emb = self.y_embedder.forward_features(y)['x_norm_patchtokens']
else:
raise NotImplementedError(f'Feature embedding type {self.feature_embedding_type} not implemented')
# For img2point task, add position embedding
pos_embed_y = self.interpolate_pos_encoding_y(h_patches, w_patches)
y_emb = y_emb + pos_embed_y
return y_emb
def PointDiT_B_16(**kwargs):
return PointDiT(depth=12, hidden_size=768, num_heads=12, bottleneck_dim=128, patch_size=16, **kwargs)
def PointDiT_L_16(**kwargs):
return PointDiT(depth=24, hidden_size=1024, num_heads=16, bottleneck_dim=128, patch_size=16, **kwargs)
def PointDiT_H_16(**kwargs):
return PointDiT(depth=32, hidden_size=1280, num_heads=16, bottleneck_dim=256, patch_size=16, **kwargs)
# The /16 suffix is the patch size; main.py parses it back out of --model.
PointDiT_models = {
'PointDiT-B/16': PointDiT_B_16,
'PointDiT-L/16': PointDiT_L_16,
'PointDiT-H/16': PointDiT_H_16,
}