I wanted to use a DDPMScheduler with a cosine scheduling and obtained images filled with nan when sampling images.
I quickly inspected the code and found that it was caused by a division by 0 in the step function of the class DDPMScheduler right here :
pred_original_sample_coeff= (alpha_prod_t_prev** (0.5) *self.betas[timestep]) /beta_prod_tcurrent_sample_coeff=self.alphas[timestep] ** (0.5) *beta_prod_t_prev/beta_prod_t
beta_prod_t being equal to 0 at step 0 when using cosine scheduler because it comes from :
alpha_prod_t=self.alphas_cumprod[timestep]
alpha_prod_t_prev=self.alphas_cumprod[timestep-1] iftimestep>0elseself.onebeta_prod_t=1-alpha_prod_t
alphas_cumprod calculated like so in this case :
x=torch.linspace(0, num_train_timesteps, num_train_timesteps+1)
alphas_cumprod=torch.cos(((x/num_train_timesteps) +s) / (1+s) *torch.pi*0.5) **2alphas_cumprod/=alphas_cumprod[0].item()
Thus, alpha_cumprod[0] = 1 and beta_prod_t = 1 - 1 = 0
I saw no issue reporting this, maybe I am using it wrong. 🤷♂️
I tried using DDPMScheduler(num_train_timesteps=1000, schedule="cosine") in the 2d_ddpm_compare_schedulers.ipynb and got nan filled images as result.
I wanted to use a
DDPMSchedulerwith a cosine scheduling and obtained images filled with nan when sampling images.I quickly inspected the code and found that it was caused by a division by 0 in the
stepfunction of the classDDPMSchedulerright here :beta_prod_tbeing equal to 0 at step 0 when using cosine scheduler because it comes from :alphas_cumprodcalculated like so in this case :Thus, alpha_cumprod[0] = 1 and beta_prod_t = 1 - 1 = 0
I saw no issue reporting this, maybe I am using it wrong. 🤷♂️
I tried using
DDPMScheduler(num_train_timesteps=1000, schedule="cosine")in the2d_ddpm_compare_schedulers.ipynband got nan filled images as result.