-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b1ee17
commit c564b04
Showing
3 changed files
with
259 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
from dataclasses import dataclass | ||
from typing import Tuple, Union | ||
|
||
import torch | ||
import torch.nn.functional as F | ||
|
||
from ..configuration_utils import ConfigMixin, register_to_config | ||
from ..utils import BaseOutput | ||
from .scheduling_utils import SchedulerMixin | ||
|
||
|
||
def log_add_exp(a, b): | ||
maximum = torch.max(a, b) | ||
return maximum + torch.log(torch.exp(a - maximum) + torch.exp(b - maximum)) | ||
|
||
|
||
def extract(a, t, x_shape): | ||
b, *_ = t.shape | ||
out = a.gather(-1, t) | ||
return out.reshape(b, *((1,) * (len(x_shape) - 1))) | ||
|
||
|
||
def index_to_log_onehot(x, num_classes): | ||
assert x.max().item() < num_classes, f"Error: {x.max().item()} >= {num_classes}" | ||
x_onehot = F.one_hot(x, num_classes) | ||
permute_order = (0, -1) + tuple(range(1, len(x.size()))) | ||
x_onehot = x_onehot.permute(permute_order) | ||
log_x = torch.log(x_onehot.float().clamp(min=1e-30)) | ||
return log_x | ||
|
||
|
||
def log_onehot_to_index(log_x): | ||
return log_x.argmax(1) | ||
|
||
|
||
@dataclass | ||
class VQDiffusionSchedulerOutput(BaseOutput): | ||
... | ||
|
||
|
||
class VQDiffusionScheduler(SchedulerMixin, ConfigMixin): | ||
@register_to_config | ||
def __init__(self): | ||
... | ||
|
||
def set_timestamps(self): | ||
... | ||
|
||
def step(self, out, t, log_x) -> Union[VQDiffusionSchedulerOutput, Tuple]: | ||
log_x_recon = F.log_softmax(out.double(), dim=1).float() | ||
batch_size = TODO | ||
|
||
zero_vector = torch.zeros(batch_size, 1, self.content_seq_len) - 70 | ||
log_x_recon = torch.cat((log_x_recon, zero_vector), dim=1) | ||
log_x_recon = torch.clamp(log_x_recon, -70, 0) | ||
|
||
log_model_pred = self.q_posterior(log_x_start=log_x_recon, log_x_t=log_x, t=t) | ||
|
||
out = self.log_sample_categorical(log_model_pred) | ||
|
||
return out | ||
|
||
def q_posterior(self, log_x_start, log_x_t, t): # p_theta(xt_1|xt) = sum(q(xt-1|xt,x0')*p(x0')) | ||
# notice that log_x_t is onehot | ||
assert t.min().item() >= 0 and t.max().item() < self.num_timesteps | ||
batch_size = log_x_start.size()[0] | ||
onehot_x_t = log_onehot_to_index(log_x_t) | ||
mask = (onehot_x_t == self.num_classes - 1).unsqueeze(1) | ||
log_one_vector = torch.zeros(batch_size, 1, 1).type_as(log_x_t) | ||
log_zero_vector = torch.log(log_one_vector + 1.0e-30).expand(-1, -1, self.content_seq_len) | ||
|
||
log_qt = self.q_pred(log_x_t, t) # q(xt|x0) | ||
log_qt = log_qt[:, :-1, :] | ||
log_cumprod_ct = extract(self.log_cumprod_ct, t, log_x_start.shape) # ct~ | ||
ct_cumprod_vector = log_cumprod_ct.expand(-1, self.num_classes - 1, -1) | ||
log_qt = (~mask) * log_qt + mask * ct_cumprod_vector | ||
|
||
log_qt_one_timestep = self.q_pred_one_timestep(log_x_t, t) # q(xt|xt_1) | ||
log_qt_one_timestep = torch.cat((log_qt_one_timestep[:, :-1, :], log_zero_vector), dim=1) | ||
log_ct = extract(self.log_ct, t, log_x_start.shape) # ct | ||
ct_vector = log_ct.expand(-1, self.num_classes - 1, -1) | ||
ct_vector = torch.cat((ct_vector, log_one_vector), dim=1) | ||
log_qt_one_timestep = (~mask) * log_qt_one_timestep + mask * ct_vector | ||
|
||
q = log_x_start[:, :-1, :] - log_qt | ||
q = torch.cat((q, log_zero_vector), dim=1) | ||
q_log_sum_exp = torch.logsumexp(q, dim=1, keepdim=True) | ||
q = q - q_log_sum_exp | ||
log_EV_xtmin_given_xt_given_xstart = self.q_pred(q, t - 1) + log_qt_one_timestep + q_log_sum_exp | ||
return torch.clamp(log_EV_xtmin_given_xt_given_xstart, -70, 0) | ||
|
||
def q_pred(self, log_x_start, t): # q(xt|x0) | ||
# log_x_start can be onehot or not | ||
t = (t + (self.num_timesteps + 1)) % (self.num_timesteps + 1) | ||
log_cumprod_at = extract(self.log_cumprod_at, t, log_x_start.shape) # at~ | ||
log_cumprod_bt = extract(self.log_cumprod_bt, t, log_x_start.shape) # bt~ | ||
log_cumprod_ct = extract(self.log_cumprod_ct, t, log_x_start.shape) # ct~ | ||
log_1_min_cumprod_ct = extract(self.log_1_min_cumprod_ct, t, log_x_start.shape) # 1-ct~ | ||
|
||
log_probs = torch.cat( | ||
[ | ||
log_add_exp(log_x_start[:, :-1, :] + log_cumprod_at, log_cumprod_bt), | ||
log_add_exp(log_x_start[:, -1:, :] + log_1_min_cumprod_ct, log_cumprod_ct), | ||
], | ||
dim=1, | ||
) | ||
|
||
return log_probs | ||
|
||
def q_pred_one_timestep(self, log_x_t, t): # q(xt|xt_1) | ||
log_at = extract(self.log_at, t, log_x_t.shape) # at | ||
log_bt = extract(self.log_bt, t, log_x_t.shape) # bt | ||
log_ct = extract(self.log_ct, t, log_x_t.shape) # ct | ||
log_1_min_ct = extract(self.log_1_min_ct, t, log_x_t.shape) # 1-ct | ||
|
||
log_probs = torch.cat( | ||
[log_add_exp(log_x_t[:, :-1, :] + log_at, log_bt), log_add_exp(log_x_t[:, -1:, :] + log_1_min_ct, log_ct)], | ||
dim=1, | ||
) | ||
|
||
return log_probs | ||
|
||
# use gumbel to sample onehot vector from log probability | ||
def log_sample_categorical(self, logits): | ||
uniform = torch.rand_like(logits) | ||
gumbel_noise = -torch.log(-torch.log(uniform + 1e-30) + 1e-30) | ||
sample = (gumbel_noise + logits).argmax(dim=1) | ||
log_sample = index_to_log_onehot(sample, self.num_classes) | ||
return log_sample |