From 12079cadee40bf008c27679f1e9f894fb226b841 Mon Sep 17 00:00:00 2001 From: Phil Wang Date: Sun, 30 Oct 2022 09:53:28 -0700 Subject: [PATCH] add the v-parameterization from Salimans et al for the discrete case, allow for distillation --- .../denoising_diffusion_pytorch.py | 23 ++++++++++++++++++- setup.py | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/denoising_diffusion_pytorch/denoising_diffusion_pytorch.py b/denoising_diffusion_pytorch/denoising_diffusion_pytorch.py index 50835b7..fc6af0e 100644 --- a/denoising_diffusion_pytorch/denoising_diffusion_pytorch.py +++ b/denoising_diffusion_pytorch/denoising_diffusion_pytorch.py @@ -440,7 +440,7 @@ class GaussianDiffusion(nn.Module): self.objective = objective - assert objective in {'pred_noise', 'pred_x0'}, 'objective must be either pred_noise (predict noise) or pred_x0 (predict image start)' + assert objective in {'pred_noise', 'pred_x0', 'pred_v'}, 'objective must be either pred_noise (predict noise) or pred_x0 (predict image start) or pred_v (predict v [v-parameterization as defined in appendix D of progressive distillation paper, used in imagen-video successfully])' if beta_schedule == 'linear': betas = linear_beta_schedule(timesteps) @@ -511,6 +511,18 @@ class GaussianDiffusion(nn.Module): extract(self.sqrt_recipm1_alphas_cumprod, t, x_t.shape) ) + def predict_v(self, x_start, t, noise): + return ( + extract(self.sqrt_alphas_cumprod, t, x_start.shape) * noise - + extract(self.sqrt_one_minus_alphas_cumprod, t, x_start.shape) * x_start + ) + + def predict_start_from_v(self, x_t, t, v): + return ( + extract(self.sqrt_alphas_cumprod, t, x_t.shape) * x_t - + extract(self.sqrt_one_minus_alphas_cumprod, t, x_t.shape) * v + ) + def q_posterior(self, x_start, x_t, t): posterior_mean = ( extract(self.posterior_mean_coef1, t, x_t.shape) * x_start + @@ -534,6 +546,12 @@ class GaussianDiffusion(nn.Module): x_start = maybe_clip(x_start) pred_noise = self.predict_noise_from_start(x, t, x_start) + elif self.objective == 'pred_v': + v = model_output + x_start = self.predict_start_from_v(x, t, v) + x_start = maybe_clip(x_start) + pred_noise = self.predict_noise_from_start(x, t, x_start) + return ModelPrediction(pred_noise, x_start) def p_mean_variance(self, x, t, x_self_cond = None, clip_denoised = True): @@ -671,6 +689,9 @@ class GaussianDiffusion(nn.Module): target = noise elif self.objective == 'pred_x0': target = x_start + elif self.objective == 'pred_v': + v = self.predict_v(x_start, t, noise) + target = v else: raise ValueError(f'unknown objective {self.objective}') diff --git a/setup.py b/setup.py index 41bf50d..503bafd 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ from setuptools import setup, find_packages setup( name = 'denoising-diffusion-pytorch', packages = find_packages(), - version = '0.29.1', + version = '0.30.0', license='MIT', description = 'Denoising Diffusion Probabilistic Models - Pytorch', author = 'Phil Wang',