-
Notifications
You must be signed in to change notification settings - Fork 2
/
sde.py
279 lines (216 loc) · 8.22 KB
/
sde.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
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
import torch
import torch.nn as nn
from absl import logging
import numpy as np
import math
from tqdm import tqdm
def get_sde(name, **kwargs):
if name == 'vpsde':
return VPSDE(**kwargs)
elif name == 'vpsde_cosine':
return VPSDECosine(**kwargs)
else:
raise NotImplementedError
def stp(s, ts: torch.Tensor): # scalar tensor product
if isinstance(s, np.ndarray):
s = torch.from_numpy(s).type_as(ts)
extra_dims = (1,) * (ts.dim() - 1)
return s.view(-1, *extra_dims) * ts
def mos(a, start_dim=1): # mean of square
return a.pow(2).flatten(start_dim=start_dim).mean(dim=-1)
def duplicate(tensor, *size):
return tensor.unsqueeze(dim=0).expand(*size, *tensor.shape)
class SDE(object):
r"""
dx = f(x, t)dt + g(t) dw with 0 <= t <= 1
f(x, t) is the drift
g(t) is the diffusion
"""
def drift(self, x, t):
raise NotImplementedError
def diffusion(self, t):
raise NotImplementedError
def cum_beta(self, t): # the variance of xt|x0
raise NotImplementedError
def cum_alpha(self, t):
raise NotImplementedError
def snr(self, t): # signal noise ratio
raise NotImplementedError
def nsr(self, t): # noise signal ratio
raise NotImplementedError
def marginal_prob(self, x0, t): # the mean and std of q(xt|x0)
alpha = self.cum_alpha(t)
beta = self.cum_beta(t)
mean = stp(alpha ** 0.5, x0) # E[xt|x0]
std = beta ** 0.5 # Cov[xt|x0] ** 0.5
return mean, std
def sample(self, x0, t_init=0): # sample from q(xn|x0), where n is uniform
t = torch.rand(x0.shape[0], device=x0.device) * (1. - t_init) + t_init
mean, std = self.marginal_prob(x0, t)
eps = torch.randn_like(x0)
xt = mean + stp(std, eps)
return t, eps, xt
class VPSDE(SDE):
def __init__(self, beta_min=0.1, beta_max=20):
# 0 <= t <= 1
self.beta_0 = beta_min
self.beta_1 = beta_max
def drift(self, x, t):
return -0.5 * stp(self.squared_diffusion(t), x)
def diffusion(self, t):
return self.squared_diffusion(t) ** 0.5
def squared_diffusion(self, t): # beta(t)
return self.beta_0 + t * (self.beta_1 - self.beta_0)
def squared_diffusion_integral(self, s, t): # \int_s^t beta(tau) d tau
return self.beta_0 * (t - s) + (self.beta_1 - self.beta_0) * (t ** 2 - s ** 2) * 0.5
def skip_beta(self, s, t): # beta_{t|s}, Cov[xt|xs]=beta_{t|s} I
return 1. - self.skip_alpha(s, t)
def skip_alpha(self, s, t): # alpha_{t|s}, E[xt|xs]=alpha_{t|s}**0.5 xs
x = -self.squared_diffusion_integral(s, t)
return x.exp()
def cum_beta(self, t):
return self.skip_beta(0, t)
def cum_alpha(self, t):
return self.skip_alpha(0, t)
def nsr(self, t):
return self.squared_diffusion_integral(0, t).expm1()
def snr(self, t):
return 1. / self.nsr(t)
def __str__(self):
return f'vpsde beta_0={self.beta_0} beta_1={self.beta_1}'
def __repr__(self):
return f'vpsde beta_0={self.beta_0} beta_1={self.beta_1}'
class VPSDECosine(SDE):
r"""
dx = f(x, t)dt + g(t) dw with 0 <= t <= 1
f(x, t) is the drift
g(t) is the diffusion
"""
def __init__(self, s=0.008):
self.s = s
self.F = lambda t: torch.cos((t + s) / (1 + s) * math.pi / 2) ** 2
self.F0 = math.cos(s / (1 + s) * math.pi / 2) ** 2
def drift(self, x, t):
ft = - torch.tan((t + self.s) / (1 + self.s) * math.pi / 2) / (1 + self.s) * math.pi / 2
return stp(ft, x)
def diffusion(self, t):
return (torch.tan((t + self.s) / (1 + self.s) * math.pi / 2) / (1 + self.s) * math.pi) ** 0.5
def cum_beta(self, t): # the variance of xt|x0
return 1 - self.cum_alpha(t)
def cum_alpha(self, t):
return self.F(t) / self.F0
def snr(self, t): # signal noise ratio
Ft = self.F(t)
return Ft / (self.F0 - Ft)
def nsr(self, t): # noise signal ratio
Ft = self.F(t)
return self.F0 / Ft - 1
def __str__(self):
return 'vpsde_cosine'
def __repr__(self):
return 'vpsde_cosine'
class ScoreModel(object):
r"""
The forward process is q(x_[0,T])
"""
def __init__(self, nnet: nn.Module, pred: str, sde: SDE, T=1):
assert T == 1
self.nnet = nnet
self.pred = pred
self.sde = sde
self.T = T
print(f'ScoreModel with pred={pred}, sde={sde}, T={T}')
def predict(self, xt, t, **kwargs):
if not isinstance(t, torch.Tensor):
t = torch.tensor(t)
t = t.to(xt.device)
if t.dim() == 0:
t = duplicate(t, xt.size(0))
return self.nnet(xt, t * 999, **kwargs) # follow SDE
def noise_pred(self, xt, t, **kwargs):
pred = self.predict(xt, t, **kwargs)
if self.pred == 'noise_pred':
noise_pred = pred
elif self.pred == 'x0_pred':
noise_pred = - stp(self.sde.snr(t).sqrt(), pred) + stp(self.sde.cum_beta(t).rsqrt(), xt)
else:
raise NotImplementedError
return noise_pred
def x0_pred(self, xt, t, **kwargs):
pred = self.predict(xt, t, **kwargs)
if self.pred == 'noise_pred':
x0_pred = stp(self.sde.cum_alpha(t).rsqrt(), xt) - stp(self.sde.nsr(t).sqrt(), pred)
elif self.pred == 'x0_pred':
x0_pred = pred
else:
raise NotImplementedError
return x0_pred
def score(self, xt, t, **kwargs):
cum_beta = self.sde.cum_beta(t)
noise_pred = self.noise_pred(xt, t, **kwargs)
return stp(-cum_beta.rsqrt(), noise_pred)
class ReverseSDE(object):
r"""
dx = [f(x, t) - g(t)^2 s(x, t)] dt + g(t) dw
"""
def __init__(self, score_model):
self.sde = score_model.sde # the forward sde
self.score_model = score_model
def drift(self, x, t, **kwargs):
drift = self.sde.drift(x, t) # f(x, t)
diffusion = self.sde.diffusion(t) # g(t)
score = self.score_model.score(x, t, **kwargs)
return drift - stp(diffusion ** 2, score)
def diffusion(self, t):
return self.sde.diffusion(t)
class ODE(object):
r"""
dx = [f(x, t) - g(t)^2 s(x, t)] dt
"""
def __init__(self, score_model):
self.sde = score_model.sde # the forward sde
self.score_model = score_model
def drift(self, x, t, **kwargs):
drift = self.sde.drift(x, t) # f(x, t)
diffusion = self.sde.diffusion(t) # g(t)
score = self.score_model.score(x, t, **kwargs)
return drift - 0.5 * stp(diffusion ** 2, score)
def diffusion(self, t):
return 0
def dct2str(dct):
return str({k: f'{v:.6g}' for k, v in dct.items()})
@ torch.no_grad()
def euler_maruyama(rsde, x_init, sample_steps, eps=1e-3, T=1, trace=None, verbose=False, **kwargs):
r"""
The Euler Maruyama sampler for reverse SDE / ODE
See `Score-Based Generative Modeling through Stochastic Differential Equations`
"""
assert isinstance(rsde, ReverseSDE) or isinstance(rsde, ODE)
print(f"euler_maruyama with sample_steps={sample_steps}")
timesteps = np.append(0., np.linspace(eps, T, sample_steps))
timesteps = torch.tensor(timesteps).to(x_init)
x = x_init
if trace is not None:
trace.append(x)
for s, t in tqdm(list(zip(timesteps, timesteps[1:]))[::-1], disable=not verbose, desc='euler_maruyama'):
drift = rsde.drift(x, t, **kwargs)
diffusion = rsde.diffusion(t)
dt = s - t
mean = x + drift * dt
sigma = diffusion * (-dt).sqrt()
x = mean + stp(sigma, torch.randn_like(x)) if s != 0 else mean
if trace is not None:
trace.append(x)
statistics = dict(s=s, t=t, sigma=sigma.item())
logging.debug(dct2str(statistics))
return x
def LSimple(score_model: ScoreModel, x0, pred='noise_pred', **kwargs):
t, noise, xt = score_model.sde.sample(x0)
if pred == 'noise_pred':
noise_pred = score_model.noise_pred(xt, t, **kwargs)
return mos(noise - noise_pred)
elif pred == 'x0_pred':
x0_pred = score_model.x0_pred(xt, t, **kwargs)
return mos(x0 - x0_pred)
else:
raise NotImplementedError(pred)