Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formulation of reverse diffusion process in DDPM ( 1 - alpha_prod_t = beta_prod_t assumption) #9474

Open
jadevaibhav opened this issue Sep 19, 2024 · 5 comments
Labels
bug Something isn't working stale Issues that haven't received updates

Comments

@jadevaibhav
Copy link

Describe the bug

I looked into sampling code of DDPM, and I believe there's a mistake:

I believe the code makes assumption that 1 - alpha_prod_t = beta_prod_t, which simply isn't true.

Original sampling algorithm:
original paper
image

x_0 prediction from sample and predicted noise :

Eqn 15 from paper, as referenced in code snippet as well:
Screenshot 2024-09-19 at 6 26 48 PM

code implementation in diffusers:

# 2. compute predicted original sample from predicted noise also called
# "predicted x_0" of formula (15) from https://arxiv.org/pdf/2006.11239.pdf
if self.config.prediction_type == "epsilon":
pred_original_sample = (sample - beta_prod_t ** (0.5) * model_output) / alpha_prod_t ** (0.5)

Here, it clearly seems like the 1 - alpha_prod_t = beta_prod_t is being used.

investigated from post by @AlejandroBaron in #9431_

Reproduction

I haven't tested out code, but this seems fundamental formulation issue.

Logs

No response

System Info

0.30.3

Who can help?

@sayakpaul @DN6 @yiy

@jadevaibhav jadevaibhav added the bug Something isn't working label Sep 19, 2024
@jadevaibhav
Copy link
Author

It looks like its a unfortunate naming, where beta_prod_t is defined as 1 - alpha_prod_t:

# 1. compute alphas, betas
alpha_prod_t = self.alphas_cumprod[t]
alpha_prod_t_prev = self.alphas_cumprod[prev_t] if prev_t >= 0 else self.one
beta_prod_t = 1 - alpha_prod_t
beta_prod_t_prev = 1 - alpha_prod_t_prev
current_alpha_t = alpha_prod_t / alpha_prod_t_prev
current_beta_t = 1 - current_alpha_t

I think it should be renamed for clarity.

@asomoza
Copy link
Member

asomoza commented Sep 20, 2024

cc: @yiyixuxu

@sayakpaul
Copy link
Member

Perhaps a comment could be added to clarify the confusion. That seems like the easiest solution to me.

@jadevaibhav
Copy link
Author

Thanks! Also, could you please clarify why is this formulation is used for sampling (step function)? The algo in paper sample from the Gaussian(pred_prev_mean, variance). Why do we first convert to the predicted original sample (pred_x_0) and then use following for mean calculation:

# 4. Compute coefficients for pred_original_sample x_0 and current sample x_t
# See formula (7) from https://arxiv.org/pdf/2006.11239.pdf
pred_original_sample_coeff = (alpha_prod_t_prev ** (0.5) * current_beta_t) / beta_prod_t
current_sample_coeff = current_alpha_t ** (0.5) * beta_prod_t_prev / beta_prod_t
# 5. Compute predicted previous sample µ_t
# See formula (7) from https://arxiv.org/pdf/2006.11239.pdf
pred_prev_sample = pred_original_sample_coeff * pred_original_sample + current_sample_coeff * sample

Copy link

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.

@github-actions github-actions bot added the stale Issues that haven't received updates label Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working stale Issues that haven't received updates
Projects
None yet
Development

No branches or pull requests

3 participants