Spaces:
Runtime error
Runtime error
File size: 12,391 Bytes
d4c1bb7 |
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 |
import torch.nn as nn
import torch
import math
from diffusers.models.transformers.transformer_2d import BasicTransformerBlock
from diffusers.models.embeddings import Timesteps, TimestepEmbedding
from timm.models.vision_transformer import Mlp
from .norm_layer import RMSNorm
# FFN
def FeedForward(dim, mult=4):
inner_dim = int(dim * mult)
return nn.Sequential(
nn.LayerNorm(dim),
nn.Linear(dim, inner_dim, bias=False),
nn.GELU(),
nn.Linear(inner_dim, dim, bias=False),
)
def reshape_tensor(x, heads):
bs, length, width = x.shape
#(bs, length, width) --> (bs, length, n_heads, dim_per_head)
x = x.view(bs, length, heads, -1)
# (bs, length, n_heads, dim_per_head) --> (bs, n_heads, length, dim_per_head)
x = x.transpose(1, 2)
# (bs, n_heads, length, dim_per_head) --> (bs*n_heads, length, dim_per_head)
x = x.reshape(bs, heads, length, -1)
return x
class PerceiverAttention(nn.Module):
def __init__(self, *, dim, dim_head=64, heads=8):
super().__init__()
self.scale = dim_head**-0.5
self.dim_head = dim_head
self.heads = heads
inner_dim = dim_head * heads
self.norm1 = nn.LayerNorm(dim)
self.norm2 = nn.LayerNorm(dim)
self.to_q = nn.Linear(dim, inner_dim, bias=False)
self.to_kv = nn.Linear(dim, inner_dim * 2, bias=False)
self.to_out = nn.Linear(inner_dim, dim, bias=False)
def forward(self, x, latents, shift=None, scale=None):
"""
Args:
x (torch.Tensor): image features
shape (b, n1, D)
latent (torch.Tensor): latent features
shape (b, n2, D)
"""
x = self.norm1(x)
latents = self.norm2(latents)
if shift is not None and scale is not None:
latents = latents * (1 + scale.unsqueeze(1)) + shift.unsqueeze(1)
b, l, _ = latents.shape
q = self.to_q(latents)
kv_input = torch.cat((x, latents), dim=-2)
k, v = self.to_kv(kv_input).chunk(2, dim=-1)
q = reshape_tensor(q, self.heads)
k = reshape_tensor(k, self.heads)
v = reshape_tensor(v, self.heads)
# attention
scale = 1 / math.sqrt(math.sqrt(self.dim_head))
weight = (q * scale) @ (k * scale).transpose(-2, -1) # More stable with f16 than dividing afterwards
weight = torch.softmax(weight.float(), dim=-1).type(weight.dtype)
out = weight @ v
out = out.permute(0, 2, 1, 3).reshape(b, l, -1)
return self.to_out(out)
class ReshapeExpandToken(nn.Module):
def __init__(self, expand_token, token_dim):
super().__init__()
self.expand_token = expand_token
self.token_dim = token_dim
def forward(self, x):
x = x.reshape(-1, self.expand_token, self.token_dim)
return x
class TimeResampler(nn.Module):
def __init__(
self,
dim=1024,
depth=8,
dim_head=64,
heads=16,
num_queries=8,
embedding_dim=768,
output_dim=1024,
ff_mult=4,
timestep_in_dim=320,
timestep_flip_sin_to_cos=True,
timestep_freq_shift=0,
expand_token=None,
extra_dim=None,
):
super().__init__()
self.latents = nn.Parameter(torch.randn(1, num_queries, dim) / dim**0.5)
self.expand_token = expand_token is not None
if expand_token:
self.expand_proj = torch.nn.Sequential(
torch.nn.Linear(embedding_dim, embedding_dim * 2),
torch.nn.GELU(),
torch.nn.Linear(embedding_dim * 2, embedding_dim * expand_token),
ReshapeExpandToken(expand_token, embedding_dim),
RMSNorm(embedding_dim, eps=1e-8),
)
self.proj_in = nn.Linear(embedding_dim, dim)
self.extra_feature = extra_dim is not None
if self.extra_feature:
self.proj_in_norm = RMSNorm(dim, eps=1e-8)
self.extra_proj_in = torch.nn.Sequential(
nn.Linear(extra_dim, dim),
RMSNorm(dim, eps=1e-8),
)
self.proj_out = nn.Linear(dim, output_dim)
self.norm_out = nn.LayerNorm(output_dim)
self.layers = nn.ModuleList([])
for _ in range(depth):
self.layers.append(
nn.ModuleList(
[
# msa
PerceiverAttention(dim=dim, dim_head=dim_head, heads=heads),
# ff
FeedForward(dim=dim, mult=ff_mult),
# adaLN
nn.Sequential(nn.SiLU(), nn.Linear(dim, 4 * dim, bias=True))
]
)
)
# time
self.time_proj = Timesteps(timestep_in_dim, timestep_flip_sin_to_cos, timestep_freq_shift)
self.time_embedding = TimestepEmbedding(timestep_in_dim, dim, act_fn="silu")
def forward(self, x, timestep, need_temb=False, extra_feature=None):
timestep_emb = self.embedding_time(x, timestep) # bs, dim
latents = self.latents.repeat(x.size(0), 1, 1)
if self.expand_token:
x = self.expand_proj(x)
x = self.proj_in(x)
if self.extra_feature:
extra_feature = self.extra_proj_in(extra_feature)
x = self.proj_in_norm(x)
x = torch.cat([x, extra_feature], dim=1)
x = x + timestep_emb[:, None]
for attn, ff, adaLN_modulation in self.layers:
shift_msa, scale_msa, shift_mlp, scale_mlp = adaLN_modulation(timestep_emb).chunk(4, dim=1)
latents = attn(x, latents, shift_msa, scale_msa) + latents
res = latents
for idx_ff in range(len(ff)):
layer_ff = ff[idx_ff]
latents = layer_ff(latents)
if idx_ff == 0 and isinstance(layer_ff, nn.LayerNorm): # adaLN
latents = latents * (1 + scale_mlp.unsqueeze(1)) + shift_mlp.unsqueeze(1)
latents = latents + res
# latents = ff(latents) + latents
latents = self.proj_out(latents)
latents = self.norm_out(latents)
if need_temb:
return latents, timestep_emb
else:
return latents
def embedding_time(self, sample, timestep):
# 1. time
timesteps = timestep
if not torch.is_tensor(timesteps):
# TODO: this requires sync between CPU and GPU. So try to pass timesteps as tensors if you can
# This would be a good case for the `match` statement (Python 3.10+)
is_mps = sample.device.type == "mps"
if isinstance(timestep, float):
dtype = torch.float32 if is_mps else torch.float64
else:
dtype = torch.int32 if is_mps else torch.int64
timesteps = torch.tensor([timesteps], dtype=dtype, device=sample.device)
elif len(timesteps.shape) == 0:
timesteps = timesteps[None].to(sample.device)
# broadcast to batch dimension in a way that's compatible with ONNX/Core ML
timesteps = timesteps.expand(sample.shape[0])
t_emb = self.time_proj(timesteps)
# timesteps does not contain any weights and will always return f32 tensors
# but time_embedding might actually be running in fp16. so we need to cast here.
# there might be better ways to encapsulate this.
t_emb = t_emb.to(dtype=sample.dtype)
emb = self.time_embedding(t_emb, None)
return emb
class CrossLayerCrossScaleProjector(nn.Module):
def __init__(
self,
inner_dim=2688,
num_attention_heads=42,
attention_head_dim=64,
cross_attention_dim=2688,
num_layers=4,
# resampler
dim=1280,
depth=4,
dim_head=64,
heads=20,
num_queries=1024,
embedding_dim=1152 + 1536,
output_dim=4096,
ff_mult=4,
timestep_in_dim=320,
timestep_flip_sin_to_cos=True,
timestep_freq_shift=0,
):
super().__init__()
self.cross_layer_blocks = nn.ModuleList(
[
BasicTransformerBlock(
inner_dim,
num_attention_heads,
attention_head_dim,
dropout=0,
cross_attention_dim=cross_attention_dim,
activation_fn="geglu",
num_embeds_ada_norm=None,
attention_bias=False,
only_cross_attention=False,
double_self_attention=False,
upcast_attention=False,
norm_type='layer_norm',
norm_elementwise_affine=True,
norm_eps=1e-6,
attention_type="default",
)
for _ in range(num_layers)
]
)
self.cross_scale_blocks = nn.ModuleList(
[
BasicTransformerBlock(
inner_dim,
num_attention_heads,
attention_head_dim,
dropout=0,
cross_attention_dim=cross_attention_dim,
activation_fn="geglu",
num_embeds_ada_norm=None,
attention_bias=False,
only_cross_attention=False,
double_self_attention=False,
upcast_attention=False,
norm_type='layer_norm',
norm_elementwise_affine=True,
norm_eps=1e-6,
attention_type="default",
)
for _ in range(num_layers)
]
)
self.proj = Mlp(
in_features=inner_dim,
hidden_features=int(inner_dim*2),
act_layer=lambda: nn.GELU(approximate="tanh"),
drop=0
)
self.proj_cross_layer = Mlp(
in_features=inner_dim,
hidden_features=int(inner_dim*2),
act_layer=lambda: nn.GELU(approximate="tanh"),
drop=0
)
self.proj_cross_scale = Mlp(
in_features=inner_dim,
hidden_features=int(inner_dim*2),
act_layer=lambda: nn.GELU(approximate="tanh"),
drop=0
)
self.resampler = TimeResampler(
dim=dim,
depth=depth,
dim_head=dim_head,
heads=heads,
num_queries=num_queries,
embedding_dim=embedding_dim,
output_dim=output_dim,
ff_mult=ff_mult,
timestep_in_dim=timestep_in_dim,
timestep_flip_sin_to_cos=timestep_flip_sin_to_cos,
timestep_freq_shift=timestep_freq_shift,
)
def forward(self, low_res_shallow, low_res_deep, high_res_deep, timesteps, cross_attention_kwargs=None, need_temb=True):
'''
low_res_shallow [bs, 729*l, c]
low_res_deep [bs, 729, c]
high_res_deep [bs, 729*4, c]
'''
cross_layer_hidden_states = low_res_deep
for block in self.cross_layer_blocks:
cross_layer_hidden_states = block(
cross_layer_hidden_states,
encoder_hidden_states=low_res_shallow,
cross_attention_kwargs=cross_attention_kwargs,
)
cross_layer_hidden_states = self.proj_cross_layer(cross_layer_hidden_states)
cross_scale_hidden_states = low_res_deep
for block in self.cross_scale_blocks:
cross_scale_hidden_states = block(
cross_scale_hidden_states,
encoder_hidden_states=high_res_deep,
cross_attention_kwargs=cross_attention_kwargs,
)
cross_scale_hidden_states = self.proj_cross_scale(cross_scale_hidden_states)
hidden_states = self.proj(low_res_deep) + cross_scale_hidden_states
hidden_states = torch.cat([hidden_states, cross_layer_hidden_states], dim=1)
hidden_states, timestep_emb = self.resampler(hidden_states, timesteps, need_temb=True)
return hidden_states, timestep_emb
|