-
Notifications
You must be signed in to change notification settings - Fork 0
/
basics.py
253 lines (209 loc) · 8.21 KB
/
basics.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import torch
from torch import nn
import torch.nn.functional as F
import numpy as np
import math
from einops import rearrange, repeat
class PreNorm(nn.Module):
def __init__(self, dim, fn):
super().__init__()
self.norm = nn.LayerNorm(dim)
self.fn = fn
def forward(self, x, **kwargs):
return self.fn(self.norm(x), **kwargs)
class PostNorm(nn.Module):
def __init__(self, dim, fn):
super().__init__()
self.norm = nn.LayerNorm(dim)
self.fn = fn
def forward(self, x, **kwargs):
return self.norm(self.fn(x, **kwargs))
class GeAct(nn.Module):
"""Gated activation function"""
def __init__(self, act_fn):
super().__init__()
self.fn = act_fn
def forward(self, x):
c = x.shape[-1] # channel last arrangement
return self.fn(x[..., :int(c//2)]) * x[..., int(c//2):]
class MLP(nn.Module):
def __init__(self, dims, act_fn, dropout=0.):
super().__init__()
layers = []
for i in range(len(dims) - 1):
if isinstance(act_fn, GeAct) and i < len(dims) - 2:
layers.append(nn.Linear(dims[i], dims[i+1] * 2))
else:
layers.append(nn.Linear(dims[i], dims[i+1]))
if i < len(dims) - 2:
layers.append(act_fn)
layers.append(nn.Dropout(dropout))
self.net = nn.Sequential(*layers)
def forward(self, x):
return self.net(x)
def masked_instance_norm(x, mask, eps = 1e-6):
"""
x of shape: [batch_size (N), num_objects (L), features(C)]
mask of shape: [batch_size (N), num_objects (L), 1]
"""
mask = mask.float() # (N,L,1)
mean = (torch.sum(x * mask, 1) / torch.sum(mask, 1)) # (N,C)
mean = mean.detach()
var_term = ((x - mean.unsqueeze(1).expand_as(x)) * mask)**2 # (N,L,C)
var = (torch.sum(var_term, 1) / torch.sum(mask, 1)) #(N,C)
var = var.detach()
mean_reshaped = mean.unsqueeze(1).expand_as(x) # (N, L, C)
var_reshaped = var.unsqueeze(1).expand_as(x) # (N, L, C)
ins_norm = (x - mean_reshaped) / torch.sqrt(var_reshaped + eps) # (N, L, C)
return ins_norm
class GroupNorm(nn.Module):
# group norm with channel at the last dimension
def __init__(self, num_groups, num_channels,
domain_wise=False,
eps=1e-8, affine=True):
super().__init__()
self.num_groups = num_groups
self.num_channels = num_channels
self.domain_wise = domain_wise
self.eps = eps
self.affine = affine
if affine:
self.weight = nn.Parameter(torch.ones(num_channels), requires_grad=True)
self.bias = nn.Parameter(torch.zeros(num_channels), requires_grad=True)
def forward(self, x):
# b h w c
b, g_c = x.shape[0], x.shape[-1]
c = g_c // self.num_groups
if self.domain_wise:
x = rearrange(x, 'b ... (g c) -> b g (... c)', g=self.num_groups)
else:
x = rearrange(x, 'b ... (g c) -> b ... g c', g=self.num_groups)
mean = x.mean(dim=-1, keepdim=True)
var = x.var(dim=-1, keepdim=True)
x = (x - mean) / (var + self.eps).sqrt()
if self.domain_wise:
# b g (... c) -> b ... (g c)
x = x.view(b, self.num_groups, -1, c)
x = rearrange(x, 'b g ... c -> b ... (g c)')
else:
x = rearrange(x, 'b ... g c -> b ... (g c)',
g=self.num_groups)
if self.affine:
x = x * self.weight + self.bias
return x
class InstanceNorm(nn.Module):
# instance norm with channel at the last dimension
def __init__(self, num_channels, eps=1e-6, affine=False):
super().__init__()
self.num_channels = num_channels
self.eps = eps
self.affine = affine
if affine:
self.weight = nn.Parameter(torch.ones(num_channels), requires_grad=True)
self.bias = nn.Parameter(torch.zeros(num_channels), requires_grad=True)
def forward(self, x):
# b h w c
shape = x.shape
# collapse all spatial dimension
x = x.reshape(shape[0], -1, shape[-1])
mean = x.mean(dim=-2, keepdim=True)
var = x.var(dim=-2, keepdim=True)
x = (x - mean) / (var + self.eps).sqrt()
if self.affine:
x = x * self.weight + self.bias
# restore the spatial dimension
x = x.reshape(shape)
return x
def get_time_embedding(t, dim):
"""
This matches the implementation in Denoising Diffusion Probabilistic Models:
From Fairseq.
Build sinusoidal embeddings.
This matches the implementation in tensor2tensor, but differs slightly
from the description in Section 3.5 of "Attention Is All You Need".
"""
assert len(t.shape) == 1
half_dim = dim // 2
emb = math.log(10000) / (half_dim - 1)
emb = torch.exp(torch.arange(half_dim, dtype=torch.float32) * -emb)
emb = emb.to(device=t.device)
emb = t.float()[:, None] * emb[None, :]
emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=1)
if dim % 2 == 1: # zero pad
emb = torch.nn.functional.pad(emb, (0, 1, 0, 0))
return emb
# below code are taken from the amazing Hyena repo:
# https://github.com/HazyResearch/safari/blob/9ecfaf0e49630b5913fce19adec231b41c2e0e39/src/models/sequence/hyena.py#L64
class Sin(nn.Module):
def __init__(self, dim, w=10, train_freq=True):
super().__init__()
self.freq = nn.Parameter(w * torch.ones(1, dim)) if train_freq else w * torch.ones(1, dim)
def forward(self, x):
return torch.sin(self.freq * x)
class PositionalEmbedding(nn.Module):
def __init__(self,
emb_dim: int,
seq_len: int,
lr_pos_emb: float = 1e-5,
**kwargs):
"""Complex exponential positional embeddings for Hyena filters."""
super().__init__()
self.seq_len = seq_len
# The time embedding fed to the filteres is normalized so that t_f = 1
t = torch.linspace(0, 1, self.seq_len)[None, :, None] # 1, L, 1
assert emb_dim > 1
bands = (emb_dim - 1) // 2
# To compute the right embeddings we use the "proper" linspace
t_rescaled = torch.linspace(0, seq_len - 1, seq_len)[None, :, None]
w = 2 * math.pi * t_rescaled / seq_len # 1, L, 1
f = torch.linspace(1e-4, bands - 1, bands)[None, None] # 1, 1, emb_dim
z = torch.exp(-1j * f * w)
z = torch.cat([t, z.real, z.imag], dim=-1)
self.register_parameter("z", nn.Parameter(z))
optim = {"lr": lr_pos_emb}
setattr(getattr(self, "z"), "_optim", optim)
self.register_buffer("t", t)
def forward(self, L):
return self.z[:, :L], self.t[:, :L]
# reference convolution with residual connection
def fftconv_ref(u, k, D, dropout_mask, gelu=False, k_rev=None):
seqlen = u.shape[-1]
fft_size = 2 * seqlen
k_f = torch.fft.rfft(k, n=fft_size) / fft_size
if k_rev is not None:
k_rev_f = torch.fft.rfft(k_rev, n=fft_size) / fft_size
k_f = k_f + k_rev_f.conj()
u_f = torch.fft.rfft(u.to(dtype=k.dtype), n=fft_size)
if len(u.shape) > 3:
k_f = k_f.unsqueeze(1)
y = torch.fft.irfft(u_f * k_f, n=fft_size, norm='forward')[..., :seqlen]
out = y + u * D.unsqueeze(-1) # bias term
if gelu:
out = F.gelu(out)
if dropout_mask is not None:
return (out * rearrange(dropout_mask, 'b H -> b H 1')).to(dtype=u.dtype)
else:
return out.to(dtype=u.dtype)
class ExponentialModulation(nn.Module):
def __init__(
self,
d_model,
fast_decay_pct=0.3,
slow_decay_pct=1.5,
target=1e-2,
modulate: bool = True,
shift: float = 0.0,
**kwargs
):
super().__init__()
self.modulate = modulate
self.shift = shift
max_decay = math.log(target) / fast_decay_pct
min_decay = math.log(target) / slow_decay_pct
deltas = torch.linspace(min_decay, max_decay, d_model)[None, None]
self.register_buffer("deltas", deltas)
def forward(self, t, x):
if self.modulate:
decay = torch.exp(-t * self.deltas.abs())
x = x * (decay + self.shift)
return x