-
Notifications
You must be signed in to change notification settings - Fork 0
/
prior_networks.py
236 lines (189 loc) · 8.32 KB
/
prior_networks.py
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
import torch
import torch.nn as nn
import math
from timm.models.vision_transformer import Mlp
import einops
import warnings
from diffusers.models.embeddings import get_timestep_embedding
def _no_grad_trunc_normal_(tensor, mean, std, a, b):
# Cut & paste from PyTorch official master until it's in a few official releases - RW
# Method based on https://people.sc.fsu.edu/~jburkardt/presentations/truncated_normal.pdf
def norm_cdf(x):
# Computes standard normal cumulative distribution function
return (1. + math.erf(x / math.sqrt(2.))) / 2.
if (mean < a - 2 * std) or (mean > b + 2 * std):
warnings.warn("mean is more than 2 std from [a, b] in nn.init.trunc_normal_. "
"The distribution of values may be incorrect.",
stacklevel=2)
with torch.no_grad():
# Values are generated by using a truncated uniform distribution and
# then using the inverse CDF for the normal distribution.
# Get upper and lower cdf values
l = norm_cdf((a - mean) / std)
u = norm_cdf((b - mean) / std)
# Uniformly fill tensor with values from [l, u], then translate to
# [2l-1, 2u-1].
tensor.uniform_(2 * l - 1, 2 * u - 1)
# Use inverse cdf transform for normal distribution to get truncated
# standard normal
tensor.erfinv_()
# Transform to proper mean, std
tensor.mul_(std * math.sqrt(2.))
tensor.add_(mean)
# Clamp to ensure it's in the proper range
tensor.clamp_(min=a, max=b)
return tensor
def trunc_normal_(tensor, mean=0., std=1., a=-2., b=2.):
# type: (Tensor, float, float, float, float) -> Tensor
r"""Fills the input Tensor with values drawn from a truncated
normal distribution. The values are effectively drawn from the
normal distribution :math:`\mathcal{N}(\text{mean}, \text{std}^2)`
with values outside :math:`[a, b]` redrawn until they are within
the bounds. The method used for generating the random values works
best when :math:`a \leq \text{mean} \leq b`.
Args:
tensor: an n-dimensional `torch.Tensor`
mean: the mean of the normal distribution
std: the standard deviation of the normal distribution
a: the minimum cutoff value
b: the maximum cutoff value
Examples:
>>> w = torch.empty(3, 5)
>>> nn.init.trunc_normal_(w)
"""
return _no_grad_trunc_normal_(tensor, mean, std, a, b)
class ClipPatchEmbed(nn.Module):
""" Image to Patch Embedding
"""
def __init__(self, patch_size, embed_dim=768):
super().__init__()
self.patch_size = patch_size
self.proj = nn.Conv1d(1, embed_dim, kernel_size=patch_size, stride=patch_size)
def forward(self, x):
B, D = x.shape
assert D % self.patch_size == 0
# (B, D) -> (B, 1, D)
# -> (B, embed_dim, num_patches)
# -> (B, num_patches, embed_dim)
x = x.unsqueeze(1)
x = self.proj(x).transpose(1, 2)
return x
def unpatchify(x):
x = einops.rearrange(x, 'B L d -> B (L d)')
return x
class Attention(nn.Module):
def __init__(self, dim, num_heads=8, qkv_bias=False, qk_scale=None, attn_drop=0., proj_drop=0.):
super().__init__()
self.num_heads = num_heads
head_dim = dim // num_heads
self.scale = qk_scale or head_dim ** -0.5
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):
B, L, C = x.shape
qkv = self.qkv(x)
qkv = einops.rearrange(qkv, 'B L (K H D) -> K B H L D', K=3, H=self.num_heads).float()
q, k, v = qkv[0], qkv[1], qkv[2] # B H L D
x = torch.nn.functional.scaled_dot_product_attention(q, k, v)
x = einops.rearrange(x, 'B H L D -> B L (H D)')
x = self.proj(x)
x = self.proj_drop(x)
return x
class Block(nn.Module):
def __init__(self, dim, num_heads, mlp_ratio=4., qkv_bias=False, qk_scale=None,
act_layer=nn.GELU, norm_layer=nn.LayerNorm, skip=False):
super().__init__()
self.norm1 = norm_layer(dim)
self.attn = Attention(
dim, num_heads=num_heads, qkv_bias=qkv_bias, qk_scale=qk_scale)
self.norm2 = norm_layer(dim)
mlp_hidden_dim = int(dim * mlp_ratio)
self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer)
self.skip_linear = nn.Linear(2 * dim, dim) if skip else None
def forward(self, x, skip=None):
if self.skip_linear is not None:
x = self.skip_linear(torch.cat([x, skip], dim=-1))
x = x + self.attn(self.norm1(x))
x = x + self.mlp(self.norm2(x))
return x
class UViT_Clip(nn.Module):
def __init__(self, clip_dim=1024, patch_size=16, embed_dim=768, depth=12, num_heads=12, mlp_ratio=4.,
qkv_bias=False, qk_scale=None, norm_layer=nn.LayerNorm, mlp_time_embed=False,
cond_dim=42, num_cond_token=1, final=True, skip=True):
super().__init__()
self.num_features = self.embed_dim = embed_dim # num_features for consistency with other models
self.patch_embed = ClipPatchEmbed(patch_size=patch_size, embed_dim=embed_dim)
num_patches = clip_dim // patch_size
self.time_embed = nn.Sequential(
nn.Linear(embed_dim, 4 * embed_dim),
nn.SiLU(),
nn.Linear(4 * embed_dim, embed_dim),
) if mlp_time_embed else nn.Identity()
self.context_embed = nn.Linear(cond_dim, embed_dim)
self.extras = 1 + num_cond_token
self.pos_embed = nn.Parameter(torch.zeros(1, num_patches + self.extras, embed_dim))
self.in_blocks = nn.ModuleList([
Block(
dim=embed_dim, num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias, qk_scale=qk_scale,
norm_layer=norm_layer)
for _ in range(depth // 2)])
self.mid_block = Block(
dim=embed_dim, num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias, qk_scale=qk_scale,
norm_layer=norm_layer)
self.out_blocks = nn.ModuleList([
Block(
dim=embed_dim, num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias, qk_scale=qk_scale,
norm_layer=norm_layer, skip=skip)
for _ in range(depth // 2)])
self.norm = norm_layer(embed_dim)
self.patch_dim = patch_size
self.decoder_pred = nn.Linear(embed_dim, self.patch_dim, bias=True)
self.final_layer = nn.Linear(clip_dim, clip_dim) if final else nn.Identity()
trunc_normal_(self.pos_embed, std=.02)
self.apply(self._init_weights)
def _init_weights(self, m):
if isinstance(m, nn.Linear):
trunc_normal_(m.weight, std=.02)
if isinstance(m, nn.Linear) and m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.LayerNorm):
nn.init.constant_(m.bias, 0)
nn.init.constant_(m.weight, 1.0)
@torch.jit.ignore
def no_weight_decay(self):
return {'pos_embed'}
def forward(self, x, timesteps, context=None):
x = self.patch_embed(x)
B, L, D = x.shape
time_token = self.time_embed(get_timestep_embedding(timesteps, self.embed_dim))
time_token = time_token.unsqueeze(dim=1)
if context is not None:
context_token = self.context_embed(context)
context_token = context_token.unsqueeze(dim=1)
x = torch.cat((x, time_token, context_token), dim=1)
x = x + self.pos_embed # (B, L+2, D)
else:
x = torch.cat((time_token, x), dim=1)
x = x + self.pos_embed[:, :-1] # (B, L+1, D)
skips = []
for blk in self.in_blocks:
x = blk(x)
skips.append(x)
x = self.mid_block(x)
for blk in self.out_blocks:
x = blk(x, skips.pop())
x = self.norm(x)
x = self.decoder_pred(x)
x = x[:, :L, :]
x = unpatchify(x)
x = self.final_layer(x)
return x
if __name__ == "__main__":
net = UViT_Clip()
x = torch.randn(1, 1024)
t = torch.randint(1000, (1, ))
c = torch.randn(1, 42)
y = net(x, t, c)
print(y.shape)