| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| import math |
|
|
| import numpy as np |
| import torch as th |
| import enum |
|
|
| from .diffusion_utils import discretized_gaussian_log_likelihood, normal_kl |
|
|
|
|
| def mean_flat(tensor): |
| """ |
| Take the mean over all non-batch dimensions. |
| """ |
| return tensor.mean(dim=list(range(1, len(tensor.shape)))) |
|
|
|
|
| class ModelMeanType(enum.Enum): |
| """ |
| Which type of output the model predicts. |
| """ |
|
|
| PREVIOUS_X = enum.auto() |
| START_X = enum.auto() |
| EPSILON = enum.auto() |
|
|
|
|
| class ModelVarType(enum.Enum): |
| """ |
| What is used as the model's output variance. |
| The LEARNED_RANGE option has been added to allow the model to predict |
| values between FIXED_SMALL and FIXED_LARGE, making its job easier. |
| """ |
|
|
| LEARNED = enum.auto() |
| FIXED_SMALL = enum.auto() |
| FIXED_LARGE = enum.auto() |
| LEARNED_RANGE = enum.auto() |
|
|
|
|
| class LossType(enum.Enum): |
| MSE = enum.auto() |
| RESCALED_MSE = ( |
| enum.auto() |
| ) |
| KL = enum.auto() |
| RESCALED_KL = enum.auto() |
|
|
| def is_vb(self): |
| return self == LossType.KL or self == LossType.RESCALED_KL |
|
|
|
|
| def _warmup_beta(beta_start, beta_end, num_diffusion_timesteps, warmup_frac): |
| betas = beta_end * np.ones(num_diffusion_timesteps, dtype=np.float64) |
| warmup_time = int(num_diffusion_timesteps * warmup_frac) |
| betas[:warmup_time] = np.linspace(beta_start, beta_end, warmup_time, dtype=np.float64) |
| return betas |
|
|
|
|
| def get_beta_schedule(beta_schedule, *, beta_start, beta_end, num_diffusion_timesteps): |
| """ |
| This is the deprecated API for creating beta schedules. |
| See get_named_beta_schedule() for the new library of schedules. |
| """ |
| if beta_schedule == "quad": |
| betas = ( |
| np.linspace( |
| beta_start ** 0.5, |
| beta_end ** 0.5, |
| num_diffusion_timesteps, |
| dtype=np.float64, |
| ) |
| ** 2 |
| ) |
| elif beta_schedule == "linear": |
| betas = np.linspace(beta_start, beta_end, num_diffusion_timesteps, dtype=np.float64) |
| elif beta_schedule == "warmup10": |
| betas = _warmup_beta(beta_start, beta_end, num_diffusion_timesteps, 0.1) |
| elif beta_schedule == "warmup50": |
| betas = _warmup_beta(beta_start, beta_end, num_diffusion_timesteps, 0.5) |
| elif beta_schedule == "const": |
| betas = beta_end * np.ones(num_diffusion_timesteps, dtype=np.float64) |
| elif beta_schedule == "jsd": |
| betas = 1.0 / np.linspace( |
| num_diffusion_timesteps, 1, num_diffusion_timesteps, dtype=np.float64 |
| ) |
| else: |
| raise NotImplementedError(beta_schedule) |
| assert betas.shape == (num_diffusion_timesteps,) |
| return betas |
|
|
|
|
| def get_named_beta_schedule(schedule_name, num_diffusion_timesteps): |
| """ |
| Get a pre-defined beta schedule for the given name. |
| The beta schedule library consists of beta schedules which remain similar |
| in the limit of num_diffusion_timesteps. |
| Beta schedules may be added, but should not be removed or changed once |
| they are committed to maintain backwards compatibility. |
| """ |
| if schedule_name == "linear": |
| |
| |
| scale = 1000 / num_diffusion_timesteps |
| return get_beta_schedule( |
| "linear", |
| beta_start=scale * 0.0001, |
| beta_end=scale * 0.02, |
| num_diffusion_timesteps=num_diffusion_timesteps, |
| ) |
| elif schedule_name == "squaredcos_cap_v2": |
| return betas_for_alpha_bar( |
| num_diffusion_timesteps, |
| lambda t: math.cos((t + 0.008) / 1.008 * math.pi / 2) ** 2, |
| ) |
| else: |
| raise NotImplementedError(f"unknown beta schedule: {schedule_name}") |
|
|
|
|
| def betas_for_alpha_bar(num_diffusion_timesteps, alpha_bar, max_beta=0.999): |
| """ |
| Create a beta schedule that discretizes the given alpha_t_bar function, |
| which defines the cumulative product of (1-beta) over time from t = [0,1]. |
| :param num_diffusion_timesteps: the number of betas to produce. |
| :param alpha_bar: a lambda that takes an argument t from 0 to 1 and |
| produces the cumulative product of (1-beta) up to that |
| part of the diffusion process. |
| :param max_beta: the maximum beta to use; use values lower than 1 to |
| prevent singularities. |
| """ |
| betas = [] |
| for i in range(num_diffusion_timesteps): |
| t1 = i / num_diffusion_timesteps |
| t2 = (i + 1) / num_diffusion_timesteps |
| betas.append(min(1 - alpha_bar(t2) / alpha_bar(t1), max_beta)) |
| return np.array(betas) |
|
|
|
|
| class GaussianDiffusion: |
| """ |
| Utilities for training and sampling diffusion models. |
| Original ported from this codebase: |
| https://github.com/hojonathanho/diffusion/blob/1e0dceb3b3495bbe19116a5e1b3596cd0706c543/diffusion_tf/diffusion_utils_2.py#L42 |
| :param betas: a 1-D numpy array of betas for each diffusion timestep, |
| starting at T and going to 1. |
| """ |
|
|
| def __init__( |
| self, |
| *, |
| betas, |
| model_mean_type, |
| model_var_type, |
| loss_type |
| ): |
|
|
| self.model_mean_type = model_mean_type |
| self.model_var_type = model_var_type |
| self.loss_type = loss_type |
|
|
| |
| betas = np.array(betas, dtype=np.float64) |
| self.betas = betas |
| assert len(betas.shape) == 1, "betas must be 1-D" |
| assert (betas > 0).all() and (betas <= 1).all() |
|
|
| self.num_timesteps = int(betas.shape[0]) |
|
|
| alphas = 1.0 - betas |
| self.alphas_cumprod = np.cumprod(alphas, axis=0) |
| self.alphas_cumprod_prev = np.append(1.0, self.alphas_cumprod[:-1]) |
| self.alphas_cumprod_next = np.append(self.alphas_cumprod[1:], 0.0) |
| assert self.alphas_cumprod_prev.shape == (self.num_timesteps,) |
|
|
| |
| self.sqrt_alphas_cumprod = np.sqrt(self.alphas_cumprod) |
| self.sqrt_one_minus_alphas_cumprod = np.sqrt(1.0 - self.alphas_cumprod) |
| self.log_one_minus_alphas_cumprod = np.log(1.0 - self.alphas_cumprod) |
| self.sqrt_recip_alphas_cumprod = np.sqrt(1.0 / self.alphas_cumprod) |
| self.sqrt_recipm1_alphas_cumprod = np.sqrt(1.0 / self.alphas_cumprod - 1) |
|
|
| |
| self.posterior_variance = ( |
| betas * (1.0 - self.alphas_cumprod_prev) / (1.0 - self.alphas_cumprod) |
| ) |
| |
| self.posterior_log_variance_clipped = np.log( |
| np.append(self.posterior_variance[1], self.posterior_variance[1:]) |
| ) if len(self.posterior_variance) > 1 else np.array([]) |
|
|
| self.posterior_mean_coef1 = ( |
| betas * np.sqrt(self.alphas_cumprod_prev) / (1.0 - self.alphas_cumprod) |
| ) |
| self.posterior_mean_coef2 = ( |
| (1.0 - self.alphas_cumprod_prev) * np.sqrt(alphas) / (1.0 - self.alphas_cumprod) |
| ) |
|
|
| def q_mean_variance(self, x_start, t): |
| """ |
| Get the distribution q(x_t | x_0). |
| :param x_start: the [N x C x ...] tensor of noiseless inputs. |
| :param t: the number of diffusion steps (minus 1). Here, 0 means one step. |
| :return: A tuple (mean, variance, log_variance), all of x_start's shape. |
| """ |
| mean = _extract_into_tensor(self.sqrt_alphas_cumprod, t, x_start.shape) * x_start |
| variance = _extract_into_tensor(1.0 - self.alphas_cumprod, t, x_start.shape) |
| log_variance = _extract_into_tensor(self.log_one_minus_alphas_cumprod, t, x_start.shape) |
| return mean, variance, log_variance |
|
|
| def q_sample(self, x_start, t, noise=None): |
| """ |
| Diffuse the data for a given number of diffusion steps. |
| In other words, sample from q(x_t | x_0). |
| :param x_start: the initial data batch. |
| :param t: the number of diffusion steps (minus 1). Here, 0 means one step. |
| :param noise: if specified, the split-out normal noise. |
| :return: A noisy version of x_start. |
| """ |
| if noise is None: |
| noise = th.randn_like(x_start) |
| assert noise.shape == x_start.shape |
| return ( |
| _extract_into_tensor(self.sqrt_alphas_cumprod, t, x_start.shape) * x_start |
| + _extract_into_tensor(self.sqrt_one_minus_alphas_cumprod, t, x_start.shape) * noise |
| ) |
|
|
| def q_posterior_mean_variance(self, x_start, x_t, t): |
| """ |
| Compute the mean and variance of the diffusion posterior: |
| q(x_{t-1} | x_t, x_0) |
| """ |
| assert x_start.shape == x_t.shape |
| posterior_mean = ( |
| _extract_into_tensor(self.posterior_mean_coef1, t, x_t.shape) * x_start |
| + _extract_into_tensor(self.posterior_mean_coef2, t, x_t.shape) * x_t |
| ) |
| posterior_variance = _extract_into_tensor(self.posterior_variance, t, x_t.shape) |
| posterior_log_variance_clipped = _extract_into_tensor( |
| self.posterior_log_variance_clipped, t, x_t.shape |
| ) |
| assert ( |
| posterior_mean.shape[0] |
| == posterior_variance.shape[0] |
| == posterior_log_variance_clipped.shape[0] |
| == x_start.shape[0] |
| ) |
| return posterior_mean, posterior_variance, posterior_log_variance_clipped |
|
|
| def q_posterior_mean_variance_dual(self, x_start, x_t, t): |
| """ |
| Compute the posterior mean and variance for each modality: |
| q(x_{t-1} | x_t, x_0) |
| Inputs: |
| x_start: tuple (x_v_start, x_a_start) |
| x_t: tuple (x_v_t, x_a_t) |
| t: Tensor of shape [B] |
| Outputs: |
| posterior_mean: (mean_v, mean_a) |
| posterior_variance: (var_v, var_a) |
| posterior_log_variance_clipped: (logvar_v, logvar_a) |
| """ |
| x_v_start, x_a_start = x_start |
| x_v_t, x_a_t = x_t |
|
|
| def single_modality_q(x_start_i, x_t_i): |
| assert x_start_i.shape == x_t_i.shape |
| posterior_mean = ( |
| _extract_into_tensor(self.posterior_mean_coef1, t, x_t_i.shape) * x_start_i |
| + _extract_into_tensor(self.posterior_mean_coef2, t, x_t_i.shape) * x_t_i |
| ) |
| posterior_variance = _extract_into_tensor(self.posterior_variance, t, x_t_i.shape) |
| posterior_log_variance_clipped = _extract_into_tensor( |
| self.posterior_log_variance_clipped, t, x_t_i.shape |
| ) |
| return posterior_mean, posterior_variance, posterior_log_variance_clipped |
|
|
| mean_v, var_v, logvar_v = single_modality_q(x_v_start, x_v_t) |
| mean_a, var_a, logvar_a = single_modality_q(x_a_start, x_a_t) |
|
|
| return (mean_v, mean_a), (var_v, var_a), (logvar_v, logvar_a) |
|
|
| def p_mean_variance(self, model, x, t, clip_denoised=True, denoised_fn=None, model_kwargs=None): |
| """ |
| Dual-modality version. |
| x: (x_v_t, x_a_t) |
| model: takes (x_v_t, x_a_t, t, **model_kwargs) |
| returns: out_v, out_a: dicts with 'mean', 'variance', 'log_variance', 'pred_xstart' |
| """ |
| if model_kwargs is None: |
| model_kwargs = {} |
|
|
| x_v, x_a = x |
| B, C_v = x_v.shape[:2] |
| B, C_a = x_a.shape[:2] |
| assert t.shape == (B,) |
|
|
| |
| model_output_v, model_output_a = model(x_v, x_a, t, **model_kwargs) |
|
|
| |
| def process_modality(x_t, model_output, C): |
| if isinstance(model_output, tuple): |
| model_output, _ = model_output |
|
|
| if self.model_var_type in [ModelVarType.LEARNED, ModelVarType.LEARNED_RANGE]: |
| assert model_output.shape == (B, C * 2, *x_t.shape[2:]) |
| model_output, model_var_values = th.split(model_output, C, dim=1) |
| min_log = _extract_into_tensor(self.posterior_log_variance_clipped, t, x_t.shape) |
| max_log = _extract_into_tensor(np.log(self.betas), t, x_t.shape) |
| frac = (model_var_values + 1) / 2 |
| model_log_variance = frac * max_log + (1 - frac) * min_log |
| model_variance = th.exp(model_log_variance) |
| else: |
| model_variance_, model_log_variance_ = { |
| ModelVarType.FIXED_LARGE: ( |
| np.append(self.posterior_variance[1], self.betas[1:]), |
| np.log(np.append(self.posterior_variance[1], self.betas[1:])), |
| ), |
| ModelVarType.FIXED_SMALL: ( |
| self.posterior_variance, |
| self.posterior_log_variance_clipped, |
| ), |
| }[self.model_var_type] |
| model_variance = _extract_into_tensor(model_variance_, t, x_t.shape) |
| model_log_variance = _extract_into_tensor(model_log_variance_, t, x_t.shape) |
|
|
| def process_xstart(x): |
| if denoised_fn is not None: |
| x = denoised_fn(x) |
| if clip_denoised: |
| x = x.clamp(-1, 1) |
| return x |
|
|
| if self.model_mean_type == ModelMeanType.START_X: |
| pred_xstart = process_xstart(model_output) |
| else: |
| pred_xstart = process_xstart( |
| self._predict_xstart_from_eps(x_t=x_t, t=t, eps=model_output) |
| ) |
|
|
| model_mean, _, _ = self.q_posterior_mean_variance( |
| x_start=pred_xstart, x_t=x_t, t=t |
| ) |
|
|
| return { |
| "mean": model_mean, |
| "variance": model_variance, |
| "log_variance": model_log_variance, |
| "pred_xstart": pred_xstart, |
| } |
|
|
| out_v = process_modality(x_v, model_output_v, C_v) |
| out_a = process_modality(x_a, model_output_a, C_a) |
|
|
| return out_v, out_a |
|
|
|
|
| def _predict_xstart_from_eps(self, x_t, t, eps): |
| assert x_t.shape == eps.shape |
| return ( |
| _extract_into_tensor(self.sqrt_recip_alphas_cumprod, t, x_t.shape) * x_t |
| - _extract_into_tensor(self.sqrt_recipm1_alphas_cumprod, t, x_t.shape) * eps |
| ) |
|
|
| def _predict_eps_from_xstart(self, x_t, t, pred_xstart): |
| return ( |
| _extract_into_tensor(self.sqrt_recip_alphas_cumprod, t, x_t.shape) * x_t - pred_xstart |
| ) / _extract_into_tensor(self.sqrt_recipm1_alphas_cumprod, t, x_t.shape) |
|
|
| def condition_mean( |
| self, |
| cond_fn, |
| p_mean_var_v, |
| p_mean_var_a, |
| x_v, x_a, |
| t, |
| model_kwargs=None, |
| ): |
| """ |
| Compute conditional mean separately for each modality: |
| new_mean = mean + variance * ∇ log p(y|x_t) |
| """ |
| if model_kwargs is None: |
| model_kwargs = {} |
|
|
| |
| grad_v, grad_a = cond_fn(x_v, x_a, t, **model_kwargs) |
|
|
| new_mean_v = p_mean_var_v["mean"].float() + p_mean_var_v["variance"] * grad_v.float() |
| new_mean_a = p_mean_var_a["mean"].float() + p_mean_var_a["variance"] * grad_a.float() |
|
|
| return new_mean_v, new_mean_a |
|
|
| def p_sample( |
| self, |
| model, |
| x_v, |
| x_a, |
| t, |
| clip_denoised=True, |
| denoised_fn=None, |
| cond_fn=None, |
| model_kwargs=None, |
| ): |
| """ |
| Sample x_{t-1} from the model at the given timestep. |
| :param model: the model to sample from. |
| :param x: the current tensor at x_{t-1}. |
| :param t: the value of t, starting at 0 for the first diffusion step. |
| :param clip_denoised: if True, clip the x_start prediction to [-1, 1]. |
| :param denoised_fn: if not None, a function which applies to the |
| x_start prediction before it is used to sample. |
| :param cond_fn: if not None, this is a gradient function that acts |
| similarly to the model. |
| :param model_kwargs: if not None, a dict of extra keyword arguments to |
| pass to the model. This can be used for conditioning. |
| :return: a dict containing the following keys: |
| - 'sample': a random sample from the model. |
| - 'pred_xstart': a prediction of x_0. |
| """ |
| |
| |
| |
| |
| |
| |
| |
| |
| out_v, out_a = self.p_mean_variance( |
| model=model, |
| x=(x_v, x_a), |
| t=t, |
| clip_denoised=clip_denoised, |
| denoised_fn=denoised_fn, |
| model_kwargs=model_kwargs, |
| ) |
| noise_v = th.randn_like(x_v) |
| noise_a = th.randn_like(x_a) |
|
|
| nonzero_mask_v = ( |
| (t != 0).float().view(-1, *([1] * (len(x_v.shape) - 1))) |
| ) |
| nonzero_mask_a = ( |
| (t != 0).float().view(-1, *([1] * (len(x_a.shape) - 1))) |
| ) |
|
|
| if cond_fn is not None: |
|
|
| out_v["mean"], out_a["mean"] = condition_mean(cond_fn, out_v, out_a, x_v, x_a, t, model_kwargs=model_kwargs) |
| sample_v = out_v["mean"] + nonzero_mask_v * th.exp(0.5 * out_v["log_variance"]) * noise_v |
| sample_a = out_a["mean"] + nonzero_mask_a * th.exp(0.5 * out_a["log_variance"]) * noise_a |
| return {"sample_v": sample_v, "sample_a": sample_a, "pred_xstart_v": out_v["pred_xstart"], "pred_xstart_a": out_a["pred_xstart"]} |
|
|
| def p_sample_loop( |
| self, |
| model, |
| shape_v, |
| shape_a, |
| noise_v=None, |
| noise_a=None, |
| clip_denoised=True, |
| denoised_fn=None, |
| cond_fn=None, |
| model_kwargs=None, |
| device=None, |
| progress=False, |
| ): |
| """ |
| Generate samples from the model. |
| :param model: the model module. |
| :param shape: the shape of the samples, (N, C, H, W). |
| :param noise: if specified, the noise from the encoder to sample. |
| Should be of the same shape as `shape`. |
| :param clip_denoised: if True, clip x_start predictions to [-1, 1]. |
| :param denoised_fn: if not None, a function which applies to the |
| x_start prediction before it is used to sample. |
| :param cond_fn: if not None, this is a gradient function that acts |
| similarly to the model. |
| :param model_kwargs: if not None, a dict of extra keyword arguments to |
| pass to the model. This can be used for conditioning. |
| :param device: if specified, the device to create the samples on. |
| If not specified, use a model parameter's device. |
| :param progress: if True, show a tqdm progress bar. |
| :return: a non-differentiable batch of samples. |
| """ |
| final = None |
| for sample in self.p_sample_loop_progressive( |
| model, |
| shape_v, |
| shape_a, |
| noise_v=noise_v, |
| noise_a=noise_a, |
| clip_denoised=clip_denoised, |
| denoised_fn=denoised_fn, |
| cond_fn=cond_fn, |
| model_kwargs=model_kwargs, |
| device=device, |
| progress=progress, |
| ): |
| final = sample |
| return final["sample_v"], final["sample_a"] |
|
|
| def p_sample_loop_progressive( |
| self, |
| model, |
| shape_v, |
| shape_a, |
| noise_v=None, |
| noise_a=None, |
| clip_denoised=True, |
| denoised_fn=None, |
| cond_fn=None, |
| model_kwargs=None, |
| device=None, |
| progress=False, |
| ): |
| """ |
| Generate samples from the model and yield intermediate samples from |
| each timestep of diffusion. |
| Arguments are the same as p_sample_loop(). |
| Returns a generator over dicts, where each dict is the return value of |
| p_sample(). |
| """ |
| if device is None: |
| device = next(model.parameters()).device |
| assert isinstance(shape_v, (tuple, list)) |
| assert isinstance(shape_a, (tuple, list)) |
|
|
| if noise_v is not None: |
| img = noise_v |
| else: |
| img = th.randn(*shape_v, device=device) |
| if noise_a is not None: |
| audio = noise_a |
| else: |
| audio = th.randn(*shape_a, device=device) |
|
|
| indices = list(range(self.num_timesteps))[::-1] |
|
|
| if progress: |
| |
| from tqdm.auto import tqdm |
|
|
| indices = tqdm(indices) |
|
|
| for i in indices: |
| t = th.tensor([i] * shape_v[0], device=device) |
| with th.no_grad(): |
| |
| out = self.p_sample( |
| model, |
| img, |
| audio, |
| t, |
| clip_denoised=clip_denoised, |
| denoised_fn=denoised_fn, |
| cond_fn=cond_fn, |
| model_kwargs=model_kwargs, |
| ) |
| yield out |
| img = out["sample_v"] |
| audio = out["sample_a"] |
|
|
| def ddim_sample( |
| self, |
| model, |
| x, |
| t, |
| clip_denoised=True, |
| denoised_fn=None, |
| cond_fn=None, |
| model_kwargs=None, |
| eta=0.0, |
| ): |
| """ |
| Sample x_{t-1} from the model using DDIM. |
| Same usage as p_sample(). |
| """ |
| out = self.p_mean_variance( |
| model, |
| x, |
| t, |
| clip_denoised=clip_denoised, |
| denoised_fn=denoised_fn, |
| model_kwargs=model_kwargs, |
| ) |
| if cond_fn is not None: |
| out = self.condition_score(cond_fn, out, x, t, model_kwargs=model_kwargs) |
|
|
| |
| |
| eps = self._predict_eps_from_xstart(x, t, out["pred_xstart"]) |
|
|
| alpha_bar = _extract_into_tensor(self.alphas_cumprod, t, x.shape) |
| alpha_bar_prev = _extract_into_tensor(self.alphas_cumprod_prev, t, x.shape) |
| sigma = ( |
| eta |
| * th.sqrt((1 - alpha_bar_prev) / (1 - alpha_bar)) |
| * th.sqrt(1 - alpha_bar / alpha_bar_prev) |
| ) |
| |
| noise = th.randn_like(x) |
| mean_pred = ( |
| out["pred_xstart"] * th.sqrt(alpha_bar_prev) |
| + th.sqrt(1 - alpha_bar_prev - sigma ** 2) * eps |
| ) |
| nonzero_mask = ( |
| (t != 0).float().view(-1, *([1] * (len(x.shape) - 1))) |
| ) |
| sample = mean_pred + nonzero_mask * sigma * noise |
| return {"sample": sample, "pred_xstart": out["pred_xstart"]} |
|
|
| def ddim_reverse_sample( |
| self, |
| model, |
| x, |
| t, |
| clip_denoised=True, |
| denoised_fn=None, |
| cond_fn=None, |
| model_kwargs=None, |
| eta=0.0, |
| ): |
| """ |
| Sample x_{t+1} from the model using DDIM reverse ODE. |
| """ |
| assert eta == 0.0, "Reverse ODE only for deterministic path" |
| out = self.p_mean_variance( |
| model, |
| x, |
| t, |
| clip_denoised=clip_denoised, |
| denoised_fn=denoised_fn, |
| model_kwargs=model_kwargs, |
| ) |
| if cond_fn is not None: |
| out = self.condition_score(cond_fn, out, x, t, model_kwargs=model_kwargs) |
| |
| |
| eps = ( |
| _extract_into_tensor(self.sqrt_recip_alphas_cumprod, t, x.shape) * x |
| - out["pred_xstart"] |
| ) / _extract_into_tensor(self.sqrt_recipm1_alphas_cumprod, t, x.shape) |
| alpha_bar_next = _extract_into_tensor(self.alphas_cumprod_next, t, x.shape) |
|
|
| |
| mean_pred = out["pred_xstart"] * th.sqrt(alpha_bar_next) + th.sqrt(1 - alpha_bar_next) * eps |
|
|
| return {"sample": mean_pred, "pred_xstart": out["pred_xstart"]} |
|
|
| def ddim_sample_loop( |
| self, |
| model, |
| shape, |
| noise=None, |
| clip_denoised=True, |
| denoised_fn=None, |
| cond_fn=None, |
| model_kwargs=None, |
| device=None, |
| progress=False, |
| eta=0.0, |
| ): |
| """ |
| Generate samples from the model using DDIM. |
| Same usage as p_sample_loop(). |
| """ |
| final = None |
| for sample in self.ddim_sample_loop_progressive( |
| model, |
| shape, |
| noise=noise, |
| clip_denoised=clip_denoised, |
| denoised_fn=denoised_fn, |
| cond_fn=cond_fn, |
| model_kwargs=model_kwargs, |
| device=device, |
| progress=progress, |
| eta=eta, |
| ): |
| final = sample |
| return final["sample"] |
|
|
| def ddim_sample_loop_progressive( |
| self, |
| model, |
| shape, |
| noise=None, |
| clip_denoised=True, |
| denoised_fn=None, |
| cond_fn=None, |
| model_kwargs=None, |
| device=None, |
| progress=False, |
| eta=0.0, |
| ): |
| """ |
| Use DDIM to sample from the model and yield intermediate samples from |
| each timestep of DDIM. |
| Same usage as p_sample_loop_progressive(). |
| """ |
| if device is None: |
| device = next(model.parameters()).device |
| assert isinstance(shape, (tuple, list)) |
| if noise is not None: |
| img = noise |
| else: |
| img = th.randn(*shape, device=device) |
| indices = list(range(self.num_timesteps))[::-1] |
|
|
| if progress: |
| |
| from tqdm.auto import tqdm |
|
|
| indices = tqdm(indices) |
|
|
| for i in indices: |
| t = th.tensor([i] * shape[0], device=device) |
| with th.no_grad(): |
| out = self.ddim_sample( |
| model, |
| img, |
| t, |
| clip_denoised=clip_denoised, |
| denoised_fn=denoised_fn, |
| cond_fn=cond_fn, |
| model_kwargs=model_kwargs, |
| eta=eta, |
| ) |
| yield out |
| img = out["sample"] |
|
|
| def _vb_terms_bpd( |
| self, model, x_v_start, x_a_start, x_v_t, x_a_t, t, clip_denoised=True, model_kwargs=None |
| ): |
| """ |
| Dual-modality VB loss. |
| """ |
|
|
| |
| (true_mean_v, true_mean_a), _, (logvar_v, logvar_a) = self.q_posterior_mean_variance_dual( |
| x_start=(x_v_start, x_a_start), |
| x_t=(x_v_t, x_a_t), |
| t=t, |
| ) |
|
|
| |
| out_v, out_a = self.p_mean_variance( |
| model=model, |
| x=(x_v_t, x_a_t), |
| t=t, |
| clip_denoised=clip_denoised, |
| model_kwargs=model_kwargs, |
| ) |
|
|
| |
| kl_v = normal_kl(true_mean_v, logvar_v, out_v["mean"], out_v["log_variance"]) |
| kl_a = normal_kl(true_mean_a, logvar_a, out_a["mean"], out_a["log_variance"]) |
| kl_v = mean_flat(kl_v) / np.log(2.0) |
| kl_a = mean_flat(kl_a) / np.log(2.0) |
|
|
| |
| decoder_nll_v = -discretized_gaussian_log_likelihood( |
| x_v_start, means=out_v["mean"], log_scales=0.5 * out_v["log_variance"] |
| ) |
| decoder_nll_v = mean_flat(decoder_nll_v) / np.log(2.0) |
|
|
| decoder_nll_a = -discretized_gaussian_log_likelihood( |
| x_a_start, means=out_a["mean"], log_scales=0.5 * out_a["log_variance"] |
| ) |
| decoder_nll_a = mean_flat(decoder_nll_a) / np.log(2.0) |
|
|
| |
| output_v = th.where((t == 0), decoder_nll_v, kl_v) |
| output_a = th.where((t == 0), decoder_nll_a, kl_a) |
|
|
| return { |
| "output_v": output_v, |
| "output_a": output_a, |
| "pred_xstart": (out_v["pred_xstart"], out_a["pred_xstart"]), |
| } |
|
|
| def training_losses(self, model, x_v_start, x_a_start, t, model_kwargs=None, noise_v=None, noise_a=None): |
| """ |
| Compute training losses for a single timestep. |
| :param model: the model to evaluate loss on. |
| :param x_start: the [N x C x ...] tensor of inputs. |
| :param t: a batch of timestep indices. |
| :param model_kwargs: if not None, a dict of extra keyword arguments to |
| pass to the model. This can be used for conditioning. |
| :param noise: if specified, the specific Gaussian noise to try to remove. |
| :return: a dict with the key "loss" containing a tensor of shape [N]. |
| Some mean or variance settings may also have other keys. |
| """ |
| if model_kwargs is None: |
| model_kwargs = {} |
| if noise_v is None: |
| noise_v = th.randn_like(x_v_start) |
| x_v_t = self.q_sample(x_v_start, t, noise=noise_v) |
| if noise_a is None: |
| noise_a = th.randn_like(x_a_start) |
| x_a_t = self.q_sample(x_a_start, t, noise=noise_a) |
|
|
| terms = {} |
|
|
| if self.loss_type == LossType.KL or self.loss_type == LossType.RESCALED_KL: |
| vb_terms = self._vb_terms_bpd( |
| model=model, |
| x_v_start=x_v_start, |
| x_a_start=x_a_start, |
| x_v_t=x_v_t, |
| x_a_t=x_a_t, |
| t=t, |
| clip_denoised=False, |
| model_kwargs=model_kwargs, |
| ) |
| terms["vb_v"] = vb_terms["output_v"] |
| terms["vb_a"] = vb_terms["output_a"] |
| terms["loss"] = vb_terms["output_v"] + vb_terms["output_a"] |
| if self.loss_type == LossType.RESCALED_KL: |
| terms["loss"] *= self.num_timesteps |
| |
| elif self.loss_type == LossType.MSE or self.loss_type == LossType.RESCALED_MSE: |
| model_output_v, model_output_a = model(x_v_t, x_a_t, t, **model_kwargs) |
| if self.model_var_type in [ModelVarType.LEARNED, ModelVarType.LEARNED_RANGE]: |
| B, C_v = x_v_t.shape[:2] |
| B, C_a = x_a_t.shape[:2] |
|
|
| model_output_v, model_var_v = th.split(model_output_v, C_v, dim=1) |
| model_output_a, model_var_a = th.split(model_output_a, C_a, dim=1) |
|
|
| frozen_out_v = th.cat([model_output_v.detach(), model_var_v], dim=1) |
| frozen_out_a = th.cat([model_output_a.detach(), model_var_a], dim=1) |
|
|
| frozen_model = lambda *args, **kwargs: (frozen_out_v, frozen_out_a) |
|
|
| vb_output = self._vb_terms_bpd( |
| model=frozen_model, |
| x_v_start=x_v_start, |
| x_a_start=x_a_start, |
| x_v_t=x_v_t, |
| x_a_t=x_a_t, |
| t=t, |
| clip_denoised=False, |
| ) |
|
|
| terms["vb_v"] = vb_output["output_v"] |
| terms["vb_a"] = vb_output["output_a"] |
|
|
| |
| def process_mse(modality, x_start, x_t, model_output, noise): |
| target = { |
| ModelMeanType.PREVIOUS_X: self.q_posterior_mean_variance_dual( |
| x_start=(x_v_start, x_a_start), |
| x_t=(x_v_t, x_a_t), |
| t=t, |
| )[0][0 if modality == "v" else 1], |
| ModelMeanType.START_X: x_start, |
| ModelMeanType.EPSILON: noise, |
| }[self.model_mean_type] |
|
|
| assert model_output.shape == target.shape == x_start.shape |
| terms[f"mse_{modality}"] = mean_flat((target - model_output) ** 2) |
|
|
| process_mse("v", x_v_start, x_v_t, model_output_v, noise_v) |
| process_mse("a", x_a_start, x_a_t, model_output_a, noise_a) |
|
|
| if "vb_v" in terms and "vb_a" in terms: |
| terms["vb"] = terms["vb_v"] + terms["vb_a"] |
| if self.loss_type == LossType.RESCALED_MSE: |
| terms["vb"] *= self.num_timesteps / 1000.0 |
|
|
| terms["loss"] = terms["mse_v"] + terms["mse_a"] |
| if "vb" in terms: |
| terms["loss"] += terms["vb"] |
|
|
|
|
| return terms |
|
|
| def _prior_bpd(self, x_start): |
| """ |
| Get the prior KL term for the variational lower-bound, measured in |
| bits-per-dim. |
| This term can't be optimized, as it only depends on the encoder. |
| :param x_start: the [N x C x ...] tensor of inputs. |
| :return: a batch of [N] KL values (in bits), one per batch element. |
| """ |
| batch_size = x_start.shape[0] |
| t = th.tensor([self.num_timesteps - 1] * batch_size, device=x_start.device) |
| qt_mean, _, qt_log_variance = self.q_mean_variance(x_start, t) |
| kl_prior = normal_kl( |
| mean1=qt_mean, logvar1=qt_log_variance, mean2=0.0, logvar2=0.0 |
| ) |
| return mean_flat(kl_prior) / np.log(2.0) |
|
|
| def calc_bpd_loop(self, model, x_start, clip_denoised=True, model_kwargs=None): |
| """ |
| Compute the entire variational lower-bound, measured in bits-per-dim, |
| as well as other related quantities. |
| :param model: the model to evaluate loss on. |
| :param x_start: the [N x C x ...] tensor of inputs. |
| :param clip_denoised: if True, clip denoised samples. |
| :param model_kwargs: if not None, a dict of extra keyword arguments to |
| pass to the model. This can be used for conditioning. |
| :return: a dict containing the following keys: |
| - total_bpd: the total variational lower-bound, per batch element. |
| - prior_bpd: the prior term in the lower-bound. |
| - vb: an [N x T] tensor of terms in the lower-bound. |
| - xstart_mse: an [N x T] tensor of x_0 MSEs for each timestep. |
| - mse: an [N x T] tensor of epsilon MSEs for each timestep. |
| """ |
| device = x_start.device |
| batch_size = x_start.shape[0] |
|
|
| vb = [] |
| xstart_mse = [] |
| mse = [] |
| for t in list(range(self.num_timesteps))[::-1]: |
| t_batch = th.tensor([t] * batch_size, device=device) |
| noise = th.randn_like(x_start) |
| x_t = self.q_sample(x_start=x_start, t=t_batch, noise=noise) |
| |
| with th.no_grad(): |
| out = self._vb_terms_bpd( |
| model, |
| x_start=x_start, |
| x_t=x_t, |
| t=t_batch, |
| clip_denoised=clip_denoised, |
| model_kwargs=model_kwargs, |
| ) |
| vb.append(out["output"]) |
| xstart_mse.append(mean_flat((out["pred_xstart"] - x_start) ** 2)) |
| eps = self._predict_eps_from_xstart(x_t, t_batch, out["pred_xstart"]) |
| mse.append(mean_flat((eps - noise) ** 2)) |
|
|
| vb = th.stack(vb, dim=1) |
| xstart_mse = th.stack(xstart_mse, dim=1) |
| mse = th.stack(mse, dim=1) |
|
|
| prior_bpd = self._prior_bpd(x_start) |
| total_bpd = vb.sum(dim=1) + prior_bpd |
| return { |
| "total_bpd": total_bpd, |
| "prior_bpd": prior_bpd, |
| "vb": vb, |
| "xstart_mse": xstart_mse, |
| "mse": mse, |
| } |
|
|
|
|
| def _extract_into_tensor(arr, timesteps, broadcast_shape): |
| """ |
| Extract values from a 1-D numpy array for a batch of indices. |
| :param arr: the 1-D numpy array. |
| :param timesteps: a tensor of indices into the array to extract. |
| :param broadcast_shape: a larger shape of K dimensions with the batch |
| dimension equal to the length of timesteps. |
| :return: a tensor of shape [batch_size, 1, ...] where the shape has K dims. |
| """ |
| res = th.from_numpy(arr).to(device=timesteps.device)[timesteps].float() |
| while len(res.shape) < len(broadcast_shape): |
| res = res[..., None] |
| return res + th.zeros(broadcast_shape, device=timesteps.device) |
|
|