class_docstr: ## DDPM Sampler
  
  This extends the [`DiffusionSampler` base class](index.html).
  
  DDPM samples images by repeatedly removing noise by sampling step by step from
  $p_ heta(x_{t-1} | x_t)$,
  
  egin{align}
  
  p_  heta(x_{t-1} | x_t) &= \mathcal{N}ig(x_{t-1}; \mu_     heta(x_t, t),   ildeeta_t \mathbf{I} ig) \
  
  \mu_t(x_t, t) &= rac{\sqrt{arlpha_{t-1}}eta_t}{1 - arlpha_t}x_0
                       + rac{\sqrt{lpha_t}(1 - arlpha_{t-1})}{1-arlpha_t}x_t \
  
      ildeeta_t &= rac{1 - arlpha_{t-1}}{1 - arlpha_t} eta_t \
  
  x_0 &= rac{1}{\sqrt{arlpha_t}} x_t -  \Big(\sqrt{rac{1}{arlpha_t} - 1}\Big)\epsilon_  heta \
  
  \end{align}
class_docstr_tok: ["#","#","DDPM","Sampler","This","extends","the","[","`","DiffusionSampler","`","base","class","]","(","index.html",")",".","DDPM","samples","images","by","repeatedly","removing","noise","by","sampling","step","by","step","from","$","p_","heta","(","x_","{","t-1","}","|","x_t",")","$",",","\begin","{","align","}","p_","heta","(","x_","{","t-1","}","|","x_t",")","&","=","\\mathcal","{","N","}","\big","(","x_","{","t-1","}",";","\\mu_","heta","(","x_t",",","t",")",",","ilde\beta_t","\\mathbf","{","I","}","\big",")","\\","\\mu_t","(","x_t",",","t",")","&","=","rac","{","\\sqrt","{","\bar\u0007lpha_","{","t-1","}","}","\beta_t","}","{","1","-","\bar\u0007lpha_t","}","x_0","+","rac","{","\\sqrt","{","\u0007lpha_t","}","(","1","-","\bar\u0007lpha_","{","t-1","}",")","}","{","1-\bar\u0007lpha_t","}","x_t","\\","ilde\beta_t","&","=","rac","{","1","-","\bar\u0007lpha_","{","t-1","}","}","{","1","-","\bar\u0007lpha_t","}","\beta_t","\\","x_0","&","=","rac","{","1","}","{","\\sqrt","{","\bar\u0007lpha_t","}","}","x_t","-","\\Big","(","\\sqrt","{","rac","{","1","}","{","\bar\u0007lpha_t","}","-","1","}","\\Big",")","\\epsilon_","heta","\\","\\end","{","align","}"]
docstr: :param model: is the model to predict noise $\epsilon_  ext{cond}(x_t, c)$
docstr_tok: [":","param","model",":","is","the","model","to","predict","noise","$","\\epsilon_","ext","{","cond","}","(","x_t",",","c",")","$"]
code: def __init__(self, model: LatentDiffusion):
      """
      :param model: is the model to predict noise $\epsilon_\text{cond}(x_t, c)$
      """
      super().__init__(model)
  
      # Sampling steps $1, 2, \dots, T$
      self.time_steps = np.asarray(list(range(self.n_steps)))
  
      with torch.no_grad():
          # $\bar\alpha_t$
          alpha_bar = self.model.alpha_bar
          # $\beta_t$ schedule
          beta = self.model.beta
          #  $\bar\alpha_{t-1}$
          alpha_bar_prev = torch.cat(
              [alpha_bar.new_tensor([1.0]), alpha_bar[:-1]]
          )
  
          # $\sqrt{\bar\alpha}$
          self.sqrt_alpha_bar = alpha_bar**0.5
          # $\sqrt{1 - \bar\alpha}$
          self.sqrt_1m_alpha_bar = (1.0 - alpha_bar) ** 0.5
          # $\frac{1}{\sqrt{\bar\alpha_t}}$
          self.sqrt_recip_alpha_bar = alpha_bar**-0.5
          # $\sqrt{\frac{1}{\bar\alpha_t} - 1}$
          self.sqrt_recip_m1_alpha_bar = (1 / alpha_bar - 1) ** 0.5
  
          # $\frac{1 - \bar\alpha_{t-1}}{1 - \bar\alpha_t} \beta_t$
          variance = beta * (1.0 - alpha_bar_prev) / (1.0 - alpha_bar)
          # Clamped log of $\tilde\beta_t$
          self.log_var = torch.log(torch.clamp(variance, min=1e-20))
          # $\frac{\sqrt{\bar\alpha_{t-1}}\beta_t}{1 - \bar\alpha_t}$
          self.mean_x0_coef = (
              beta * (alpha_bar_prev**0.5) / (1.0 - alpha_bar)
          )
          # $\frac{\sqrt{\alpha_t}(1 - \bar\alpha_{t-1})}{1-\bar\alpha_t}$
          self.mean_xt_coef = (
              (1.0 - alpha_bar_prev)
              * ((1 - beta) ** 0.5)
              / (1.0 - alpha_bar)
          )
  
file_path: annotated_deep_learning_paper_implementations/labml_nn/diffusion/stable_diffusion/sampler/ddpm.py
imports: from typing import Optional, List
  import numpy
  import torch
  from labml import monit
  from labml_nn.diffusion.stable_diffusion.latent_diffusion import LatentDiffusion
  from labml_nn.diffusion.stable_diffusion.sampler import DiffusionSampler
label_desc: Use image node_id 1 to create a new DDPMSampler object from inherited base classes: DiffusionSampler with example: obj = DDPMSampler(model)