-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathunified_sampler.py
More file actions
152 lines (130 loc) · 5.78 KB
/
unified_sampler.py
File metadata and controls
152 lines (130 loc) · 5.78 KB
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
import torch
from torch import nn
from typing import Callable, Union
class Linear:
def alpha_in(self, t):
return t
def gamma_in(self, t):
return 1 - t
def alpha_to(self, t):
return 1
def gamma_to(self, t):
return -1
class UnifiedSampler(torch.nn.Module):
"""
UCGM-S: https://arxiv.org/abs/2505.07447
Credit to https://github.com/LINs-lab/UCGM/blob/main/methodes/unigen.py
"""
def __init__(self):
super().__init__()
transport = Linear()
self.alpha_in, self.gamma_in = transport.alpha_in, transport.gamma_in
self.alpha_to, self.gamma_to = transport.alpha_to, transport.gamma_to
if self.gamma_in(torch.tensor(0)).abs().item() < 0.005:
self.integ_st = 0 # Start point if integral from 0 to 1
self.alpha_in, self.gamma_in = self.gamma_in, self.alpha_in
self.alpha_to, self.gamma_to = self.gamma_to, self.alpha_to
elif self.alpha_in(torch.tensor(0)).abs().item() < 0.005:
self.integ_st = 1 # Start point if integral from 1 to 0
else:
raise ValueError("Invalid Alpha and Gamma functions")
def forward(
self,
model: Union[nn.Module, Callable],
x_t: torch.Tensor,
t: torch.Tensor,
tt: Union[torch.Tensor, None] = None,
**model_kwargs,
):
tt = tt.flatten() # if tt is not None else t.clone().flatten()
dent = self.alpha_in(t) * self.gamma_to(t) - self.gamma_in(t) * self.alpha_to(t)
q = torch.ones(x_t.size(0), device=x_t.device) * (t).flatten()
q = q if self.integ_st == 1 else 1 - q
F_t = (-1) ** (1 - self.integ_st) * model(x_t, t=q, tt=tt, **model_kwargs)
t = torch.abs(t)
z_hat = (x_t * self.gamma_to(t) - F_t * self.gamma_in(t)) / dent
x_hat = (F_t * self.alpha_in(t) - x_t * self.alpha_to(t)) / dent
return x_hat, z_hat, F_t, dent
def kumaraswamy_transform(self, t, a, b, c):
return (1 - (1 - t**a) ** b) ** c
@torch.no_grad()
def sampling_loop(
self,
inital_noise_z: torch.FloatTensor,
sampling_model: Union[nn.Module, Callable],
sampling_steps: int = 20,
stochast_ratio: float = 0.0,
extrapol_ratio: float = 0.0,
sampling_order: int = 1,
time_dist_ctrl: list = [1.0, 1.0, 1.0],
rfba_gap_steps: list = [0.0, 0.0],
**model_kwargs,
):
"""
Performs unified sampling to generate data samples from the learned distribution.
"""
input_dtype = inital_noise_z.dtype
assert sampling_order in [1, 2]
num_steps = (sampling_steps + 1) // 2 if sampling_order == 2 else sampling_steps
# Time step discretization.
num_steps = num_steps + 1 if (rfba_gap_steps[1] - 0.0) == 0.0 else num_steps
t_steps = torch.linspace(
rfba_gap_steps[0], 1.0 - rfba_gap_steps[1], num_steps, dtype=torch.float64
).to(inital_noise_z)
t_steps = t_steps[:-1] if (rfba_gap_steps[1] - 0.0) == 0.0 else t_steps
t_steps = self.kumaraswamy_transform(t_steps, *time_dist_ctrl)
t_steps = torch.cat([(1 - t_steps), torch.zeros_like(t_steps[:1])])
# Prepare the buffer for the first order prediction
x_hats, z_hats, buffer_freq = [], [], 1
# Main sampling loop
x_cur = inital_noise_z.to(torch.float64)
samples = [inital_noise_z.cpu()]
for i, (t_cur, t_next) in enumerate(zip(t_steps[:-1], t_steps[1:])):
# First order prediction
x_hat, z_hat, _, _ = self.forward(
sampling_model,
x_cur.to(input_dtype),
t_cur.to(input_dtype),
torch.zeros_like(t_cur),
**model_kwargs,
)
samples.append(x_hat.cpu())
x_hat, z_hat = x_hat.to(torch.float64), z_hat.to(torch.float64)
# Apply extrapolation for prediction (extrapolating z is not nessary?)
if buffer_freq > 0 and extrapol_ratio > 0:
z_hats.append(z_hat)
x_hats.append(x_hat)
if i > buffer_freq:
z_hat = z_hat + extrapol_ratio * (z_hat - z_hats[-buffer_freq - 1])
x_hat = x_hat + extrapol_ratio * (x_hat - x_hats[-buffer_freq - 1])
z_hats.pop(0), x_hats.pop(0)
if stochast_ratio == "SDE":
stochast_ratio = (
torch.sqrt((t_next - t_cur).abs())
* torch.sqrt(2 * self.alpha_in(t_cur))
/ self.alpha_in(t_next)
)
stochast_ratio = torch.clamp(stochast_ratio ** (1 / 0.50), min=0, max=1)
noi = torch.randn(x_cur.size()).to(x_cur)
else:
noi = torch.randn(x_cur.size()).to(x_cur) if stochast_ratio > 0 else 0.0
x_next = self.gamma_in(t_next) * x_hat + self.alpha_in(t_next) * (
z_hat * ((1 - stochast_ratio) ** 0.5) + noi * (stochast_ratio**0.5)
)
# Apply second order correction, Heun-like
if sampling_order == 2 and i < num_steps - 1:
x_pri, z_pri, _, _ = self.forward(
sampling_model,
x_next.to(input_dtype),
t_next.to(input_dtype),
**model_kwargs,
)
x_pri, z_pri = x_pri.to(torch.float64), z_pri.to(torch.float64)
x_next = x_cur * self.gamma_in(t_next) / self.gamma_in(t_cur) + (
self.alpha_in(t_next)
- self.gamma_in(t_next)
* self.alpha_in(t_cur)
/ self.gamma_in(t_cur)
) * (0.5 * z_hat + 0.5 * z_pri)
x_cur = x_next
return torch.stack(samples, dim=0).to(input_dtype)