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: ### Sampling Loop
  
  :param shape: is the shape of the generated images in the
      form `[batch_size, channels, height, width]`
  :param cond: is the conditional embeddings $c$
  :param temperature: is the noise temperature (random noise gets multiplied by this)
  :param x_last: is $x_T$. If not provided random noise will be used.
  :param uncond_scale: is the unconditional guidance scale $s$. This is used for
      $\epsilon_  heta(x_t, c) = s\epsilon_       ext{cond}(x_t, c) + (s - 1)\epsilon_    ext{cond}(x_t, c_u)$
  :param uncond_cond: is the conditional embedding for empty prompt $c_u$
  :param skip_steps: is the number of time steps to skip $t'$. We start sampling from $T - t'$.
      And `x_last` is then $x_{T - t'}$.
docstr_tok: ["#","#","#","Sampling","Loop",":","param","shape",":","is","the","shape","of","the","generated","images","in","the","form","`","[","batch_size",",","channels",",","height",",","width","]","`",":","param","cond",":","is","the","conditional","embeddings","$","c","$",":","param","temperature",":","is","the","noise","temperature","(","random","noise","gets","multiplied","by","this",")",":","param","x_last",":","is","$","x_T","$",".","If","not","provided","random","noise","will","be","used",".",":","param","uncond_scale",":","is","the","unconditional","guidance","scale","$","s","$",".","This","is","used","for","$","\\epsilon_","heta","(","x_t",",","c",")","=","s\\epsilon_","ext","{","cond","}","(","x_t",",","c",")","+","(","s","-","1",")","\\epsilon_","ext","{","cond","}","(","x_t",",","c_u",")","$",":","param","uncond_cond",":","is","the","conditional","embedding","for","empty","prompt","$","c_u","$",":","param","skip_steps",":","is","the","number","of","time","steps","to","skip","$","t","'","$",".","We","start","sampling","from","$","T","-","t","'","$",".","And","`","x_last","`","is","then","$","x_","{","T","-","t","'","}","$","."]
code: def sample(
      self,
      shape: List[int],
      cond: torch.Tensor,
      repeat_noise: bool = False,
      temperature: float = 1.0,
      x_last: Optional[torch.Tensor] = None,
      uncond_scale: float = 1.0,
      uncond_cond: Optional[torch.Tensor] = None,
      skip_steps: int = 0,
  ):
      """
      ### Sampling Loop
  
      :param shape: is the shape of the generated images in the
          form `[batch_size, channels, height, width]`
      :param cond: is the conditional embeddings $c$
      :param temperature: is the noise temperature (random noise gets multiplied by this)
      :param x_last: is $x_T$. If not provided random noise will be used.
      :param uncond_scale: is the unconditional guidance scale $s$. This is used for
          $\epsilon_\theta(x_t, c) = s\epsilon_\text{cond}(x_t, c) + (s - 1)\epsilon_\text{cond}(x_t, c_u)$
      :param uncond_cond: is the conditional embedding for empty prompt $c_u$
      :param skip_steps: is the number of time steps to skip $t'$. We start sampling from $T - t'$.
          And `x_last` is then $x_{T - t'}$.
      """
  
      # Get device and batch size
      device = self.model.device
      bs = shape[0]
  
      # Get $x_T$
      x = (
          x_last
          if x_last is not None
          else torch.randn(shape, device=device)
      )
  
      # Time steps to sample at $T - t', T - t' - 1, \dots, 1$
      time_steps = np.flip(self.time_steps)[skip_steps:]
  
      # Sampling loop
      for step in monit.iterate("Sample", time_steps):
          # Time step $t$
          ts = x.new_full((bs,), step, dtype=torch.long)
  
          # Sample $x_{t-1}$
          x, pred_x0, e_t = self.p_sample(
              x,
              cond,
              ts,
              step,
              repeat_noise=repeat_noise,
              temperature=temperature,
              uncond_scale=uncond_scale,
              uncond_cond=uncond_cond,
          )
  
      # Return $x_0$
      return x
  
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 2 for calling the DDPMSampler obj's underlying member method code with example usage: obj.sample(shape, cond, repeat_noise, temperature, x_last, uncond_scale, uncond_cond, skip_steps) and returns: x