Spaces:
Running
on
Zero
Running
on
Zero
File size: 10,872 Bytes
7f51798 |
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 |
import torch
import torch.nn as nn
import numpy as np
import math
from einops import rearrange
from pdb import set_trace as st
# from .dit_models import DiT, DiTBlock, DiT_models, get_2d_sincos_pos_embed, modulate, FinalLayer
from .dit_models_xformers import DiT, DiTBlock, DiT_models, get_2d_sincos_pos_embed, modulate, FinalLayer
# from .dit_models import DiT, DiTBlock, DiT_models, get_2d_sincos_pos_embed, modulate, FinalLayer
def modulate2(x, shift, scale):
return x * (1 + scale) + shift
class DiTBlock2(DiTBlock):
"""
A DiT block with adaptive layer norm zero (adaLN-Zero) conditioning.
"""
def __init__(self, hidden_size, num_heads, mlp_ratio=4, **block_kwargs):
super().__init__(hidden_size, num_heads, mlp_ratio, **block_kwargs)
def forward(self, x, c):
shift_msa, scale_msa, gate_msa, shift_mlp, scale_mlp, gate_mlp = self.adaLN_modulation(
c).chunk(6, dim=-1)
# st()
x = x + gate_msa * self.attn(
modulate2(self.norm1(x), shift_msa, scale_msa))
x = x + gate_mlp * self.mlp(
modulate2(self.norm2(x), shift_mlp, scale_mlp))
return x
class FinalLayer2(FinalLayer):
"""
The final layer of DiT, basically the decoder_pred in MAE with adaLN.
"""
def __init__(self, hidden_size, patch_size, out_channels):
super().__init__(hidden_size, patch_size, out_channels)
def forward(self, x, c):
shift, scale = self.adaLN_modulation(c).chunk(2, dim=-1)
x = modulate2(self.norm_final(x), shift, scale)
x = self.linear(x)
return x
class DiT2(DiT):
# a conditional ViT
def __init__(self,
input_size=32,
patch_size=2,
in_channels=4,
hidden_size=1152,
depth=28,
num_heads=16,
mlp_ratio=4,
class_dropout_prob=0.1,
num_classes=1000,
learn_sigma=True,
mixing_logit_init=-3,
mixed_prediction=True,
context_dim=False,
roll_out=False,
plane_n=3,
return_all_layers=False,
in_plane_attention=True,
vit_blk=...):
super().__init__(input_size,
patch_size,
in_channels,
hidden_size,
depth,
num_heads,
mlp_ratio,
class_dropout_prob,
num_classes,
learn_sigma,
mixing_logit_init,
mixed_prediction,
context_dim,
roll_out,
vit_blk=DiTBlock2,
final_layer_blk=FinalLayer2)
# st()
# no t and x embedder
del self.x_embedder
del self.t_embedder
del self.final_layer
torch.cuda.empty_cache()
self.clip_text_proj = None
self.plane_n = plane_n
self.return_all_layers = return_all_layers
self.in_plane_attention = in_plane_attention
def forward(self, c, *args, **kwargs):
# return super().forward(x, timesteps, context, y, get_attr, **kwargs)
"""
Forward pass of DiT.
c: (N, C, H, W) tensor of spatial inputs (images or latent representations of images)
"""
x = self.pos_embed.repeat(
c.shape[0], 1, 1).to(c.dtype) # (N, T, D), where T = H * W / patch_size ** 2
if self.return_all_layers:
all_layers = []
# if context is not None:
# c = context # B 3HW C
for blk_idx, block in enumerate(self.blocks):
if self.roll_out:
if self.in_plane_attention: # plane-wise output
if blk_idx % 2 == 0: # with-in plane self attention
x = rearrange(x, 'b (n l) c -> (b n) l c ', n=self.plane_n)
x = block(x,
rearrange(c,
'b (n l) c -> (b n) l c ',
n=self.plane_n)) # (N, T, D)
# st()
if self.return_all_layers:
all_layers.append(
rearrange(x,
'(b n) l c -> b (n l) c',
n=self.plane_n))
# all_layers.append(x)
else: # global attention
x = rearrange(x, '(b n) l c -> b (n l) c ', n=self.plane_n)
x = block(x, c) # (N, T, D)
# st()
if self.return_all_layers:
# all merged into B dim
all_layers.append(x)
# all_layers.append(
# rearrange(x,
# 'b (n l) c -> (b n) l c',
# n=self.plane_n))
else:
# ! already b (n l) c
# if blk_idx == 0: # rearrange once
# x = rearrange(x, '(b n) l c -> b (n l) c ', n=self.plane_n)
x = block(x, c) # (N, T, D)
if self.return_all_layers:
# all merged into B dim
all_layers.append(x)
else:
x = block(x, c) # (N, T, D)
# x = self.final_layer(x, c) # (N, T, patch_size ** 2 * out_channels)
# if self.roll_out: # move n from L to B axis
# x = rearrange(x, 'b (n l) c ->(b n) l c', n=3)
# x = self.unpatchify(x) # (N, out_channels, H, W)
# if self.roll_out: # move n from L to B axis
# x = rearrange(x, '(b n) c h w -> b (n c) h w', n=3)
# st()
if self.return_all_layers:
return all_layers
else:
# return x.to(torch.float32)
return x
# class DiT2_DPT(DiT2):
# def __init__(self, input_size=32, patch_size=2, in_channels=4, hidden_size=1152, depth=28, num_heads=16, mlp_ratio=4, class_dropout_prob=0.1, num_classes=1000, learn_sigma=True, mixing_logit_init=-3, mixed_prediction=True, context_dim=False, roll_out=False, plane_n=3, vit_blk=...):
# super().__init__(input_size, patch_size, in_channels, hidden_size, depth, num_heads, mlp_ratio, class_dropout_prob, num_classes, learn_sigma, mixing_logit_init, mixed_prediction, context_dim, roll_out, plane_n, vit_blk)
# self.return_all_layers = True
#################################################################################
# DiT2 Configs #
#################################################################################
def DiT2_XL_2(**kwargs):
return DiT2(depth=28,
hidden_size=1152,
patch_size=2,
num_heads=16,
**kwargs)
def DiT2_XL_2_half(**kwargs):
return DiT2(depth=28 // 2,
hidden_size=1152,
patch_size=2,
num_heads=16,
**kwargs)
def DiT2_XL_4(**kwargs):
return DiT2(depth=28,
hidden_size=1152,
patch_size=4,
num_heads=16,
**kwargs)
def DiT2_XL_8(**kwargs):
return DiT2(depth=28,
hidden_size=1152,
patch_size=8,
num_heads=16,
**kwargs)
def DiT2_L_2(**kwargs):
return DiT2(depth=24,
hidden_size=1024,
patch_size=2,
num_heads=16,
**kwargs)
def DiT2_L_2_stage1(**kwargs):
return DiT2(depth=24-6,
hidden_size=1024,
patch_size=2,
num_heads=16,
**kwargs)
def DiT2_L_2_stage1(**kwargs):
return DiT2(depth=24-6,
hidden_size=1024,
patch_size=2,
num_heads=16,
**kwargs)
def DiT2_L_2_half(**kwargs):
return DiT2(depth=24//2,
hidden_size=1024,
patch_size=2,
num_heads=16,
**kwargs)
def DiT2_L_2_half_ninelayer(**kwargs):
return DiT2(depth=9,
hidden_size=1024,
patch_size=2,
num_heads=16,
**kwargs)
def DiT2_L_4(**kwargs):
return DiT2(depth=24,
hidden_size=1024,
patch_size=4,
num_heads=16,
**kwargs)
def DiT2_L_8(**kwargs):
return DiT2(depth=24,
hidden_size=1024,
patch_size=8,
num_heads=16,
**kwargs)
def DiT2_B_2(**kwargs):
return DiT2(depth=12,
hidden_size=768,
patch_size=2,
num_heads=12,
**kwargs)
def DiT2_B_2_stage1(**kwargs):
return DiT2(depth=12, # ! just 12, stage-2 3 layers afterwards.
hidden_size=768,
patch_size=2,
num_heads=12,
**kwargs)
def DiT2_B_4(**kwargs):
return DiT2(depth=12,
hidden_size=768,
patch_size=4,
num_heads=12,
**kwargs)
def DiT2_B_8(**kwargs):
return DiT2(depth=12,
hidden_size=768,
patch_size=8,
num_heads=12,
**kwargs)
def DiT2_B_16(**kwargs): # ours cfg
return DiT2(depth=12,
hidden_size=768,
patch_size=16,
num_heads=12,
**kwargs)
def DiT2_S_2(**kwargs):
return DiT2(depth=12, hidden_size=384, patch_size=2, num_heads=6, **kwargs)
def DiT2_S_4(**kwargs):
return DiT2(depth=12, hidden_size=384, patch_size=4, num_heads=6, **kwargs)
def DiT2_S_8(**kwargs):
return DiT2(depth=12, hidden_size=384, patch_size=8, num_heads=6, **kwargs)
DiT2_models = {
'DiT2-XL/2': DiT2_XL_2,
'DiT2-XL/2/half': DiT2_XL_2_half,
'DiT2-XL/4': DiT2_XL_4,
'DiT2-XL/8': DiT2_XL_8,
'DiT2-L/2': DiT2_L_2,
'DiT2-L/2/S1': DiT2_L_2_stage1,
'DiT2-L/2/S1-v2': DiT2_L_2_stage1,
'DiT2-B/2/S1': DiT2_B_2_stage1,
'DiT2-L/4': DiT2_L_4,
'DiT2-L/2-half': DiT2_L_2_half,
'DiT2-L/2-ninelayer': DiT2_L_2_half_ninelayer,
'DiT2-L/8': DiT2_L_8,
'DiT2-B/2': DiT2_B_2,
'DiT2-B/4': DiT2_B_4,
'DiT2-B/8': DiT2_B_8,
'DiT2-B/16': DiT2_B_16,
'DiT2-S/2': DiT2_S_2,
'DiT2-S/4': DiT2_S_4,
'DiT2-S/8': DiT2_S_8,
}
|