class_docstr: ## Latent diffusion model
  
  This contains following components:
  
  * [AutoEncoder](model/autoencoder.html)
  * [U-Net](model/unet.html) with [attention](model/unet_attention.html)
  * [CLIP embeddings generator](model/clip_embedder.html)
class_docstr_tok: ["#","#","Latent","diffusion","model","This","contains","following","components",":","*","[","AutoEncoder","]","(","model\/autoencoder.html",")","*","[","U-Net","]","(","model\/unet.html",")","with","[","attention","]","(","model\/unet_attention.html",")","*","[","CLIP","embeddings","generator","]","(","model\/clip_embedder.html",")"]
docstr: :param unet_model: is the [U-Net](model/unet.html) that predicts noise
   $\epsilon_     ext{cond}(x_t, c)$, in latent space
  :param autoencoder: is the [AutoEncoder](model/autoencoder.html)
  :param clip_embedder: is the [CLIP embeddings generator](model/clip_embedder.html)
  :param latent_scaling_factor: is the scaling factor for the latent space. The encodings of
   the autoencoder are scaled by this before feeding into the U-Net.
  :param n_steps: is the number of diffusion steps $T$.
  :param linear_start: is the start of the $eta$ schedule.
  :param linear_end: is the end of the $eta$ schedule.
docstr_tok: [":","param","unet_model",":","is","the","[","U-Net","]","(","model\/unet.html",")","that","predicts","noise","$","\\epsilon_","ext","{","cond","}","(","x_t",",","c",")","$",",","in","latent","space",":","param","autoencoder",":","is","the","[","AutoEncoder","]","(","model\/autoencoder.html",")",":","param","clip_embedder",":","is","the","[","CLIP","embeddings","generator","]","(","model\/clip_embedder.html",")",":","param","latent_scaling_factor",":","is","the","scaling","factor","for","the","latent","space",".","The","encodings","of","the","autoencoder","are","scaled","by","this","before","feeding","into","the","U-Net",".",":","param","n_steps",":","is","the","number","of","diffusion","steps","$","T","$",".",":","param","linear_start",":","is","the","start","of","the","$","\beta","$","schedule",".",":","param","linear_end",":","is","the","end","of","the","$","\beta","$","schedule","."]
code: def __init__(
      self,
      unet_model: UNetModel,
      autoencoder: Autoencoder,
      clip_embedder: CLIPTextEmbedder,
      latent_scaling_factor: float,
      n_steps: int,
      linear_start: float,
      linear_end: float,
  ):
      """
      :param unet_model: is the [U-Net](model/unet.html) that predicts noise
       $\epsilon_\text{cond}(x_t, c)$, in latent space
      :param autoencoder: is the [AutoEncoder](model/autoencoder.html)
      :param clip_embedder: is the [CLIP embeddings generator](model/clip_embedder.html)
      :param latent_scaling_factor: is the scaling factor for the latent space. The encodings of
       the autoencoder are scaled by this before feeding into the U-Net.
      :param n_steps: is the number of diffusion steps $T$.
      :param linear_start: is the start of the $\beta$ schedule.
      :param linear_end: is the end of the $\beta$ schedule.
      """
      super().__init__()
      # Wrap the [U-Net](model/unet.html) to keep the same model structure as
      # [CompVis/stable-diffusion](https://github.com/CompVis/stable-diffusion).
      self.model = DiffusionWrapper(unet_model)
      # Auto-encoder and scaling factor
      self.first_stage_model = autoencoder
      self.latent_scaling_factor = latent_scaling_factor
      # [CLIP embeddings generator](model/clip_embedder.html)
      self.cond_stage_model = clip_embedder
  
      # Number of steps $T$
      self.n_steps = n_steps
  
      # $\beta$ schedule
      beta = (
          torch.linspace(
              linear_start**0.5,
              linear_end**0.5,
              n_steps,
              dtype=torch.float64,
          )
          ** 2
      )
      self.beta = nn.Parameter(
          beta.to(torch.float32), requires_grad=False
      )
      # $\alpha_t = 1 - \beta_t$
      alpha = 1.0 - beta
      # $\bar\alpha_t = \prod_{s=1}^t \alpha_s$
      alpha_bar = torch.cumprod(alpha, dim=0)
      self.alpha_bar = nn.Parameter(
          alpha_bar.to(torch.float32), requires_grad=False
      )
  
file_path: annotated_deep_learning_paper_implementations/labml_nn/diffusion/stable_diffusion/latent_diffusion.py
imports: from typing import List
  import torch
  import torch.nn
  from labml_nn.diffusion.stable_diffusion.model.autoencoder import Autoencoder
  from labml_nn.diffusion.stable_diffusion.model.clip_embedder import CLIPTextEmbedder
  from labml_nn.diffusion.stable_diffusion.model.unet import UNetModel
label_desc: Use image node_id 1 to create a new LatentDiffusion object from inherited base classes: nn with example: obj = LatentDiffusion(unet_model, autoencoder, clip_embedder, latent_scaling_factor, n_steps, linear_start, linear_end)