class_docstr: Utilities for training and sampling diffusion models.
  
  Ported directly from here, and then adapted over time to further experimentation.
  https://github.com/hojonathanho/diffusion/blob/1e0dceb3b3495bbe19116a5e1b3596cd0706c543/diffusion_tf/diffusion_utils_2.py#L42
  
  :param betas: a 1-D numpy array of betas for each diffusion timestep,
                starting at T and going to 1.
  :param model_mean_type: a ModelMeanType determining what the model outputs.
  :param model_var_type: a ModelVarType determining how variance is output.
  :param loss_type: a LossType determining the loss function to use.
  :param rescale_timesteps: if True, pass floating point timesteps into the
                            model so that they are always scaled like in the
                            original paper (0 to 1000).
class_docstr_tok: ["Utilities","for","training","and","sampling","diffusion","models",".","Ported","directly","from","here",",","and","then","adapted","over","time","to","further","experimentation",".","https",":","\/\/github.com\/hojonathanho\/diffusion\/blob\/1e0dceb3b3495bbe19116a5e1b3596cd0706c543\/diffusion_tf\/diffusion_utils_2.py","#","L42",":","param","betas",":","a","1-D","numpy","array","of","betas","for","each","diffusion","timestep",",","starting","at","T","and","going","to","1.",":","param","model_mean_type",":","a","ModelMeanType","determining","what","the","model","outputs",".",":","param","model_var_type",":","a","ModelVarType","determining","how","variance","is","output",".",":","param","loss_type",":","a","LossType","determining","the","loss","function","to","use",".",":","param","rescale_timesteps",":","if","True",",","pass","floating","point","timesteps","into","the","model","so","that","they","are","always","scaled","like","in","the","original","paper","(","0","to","1000",")","."]
docstr: Compute the entire variational lower-bound, measured in bits-per-dim,
  as well as other related quantities.
  
  :param model: the model to evaluate loss on.
  :param x_start: the [N x C x ...] tensor of inputs.
  :param clip_denoised: if True, clip denoised samples.
  :param model_kwargs: if not None, a dict of extra keyword arguments to
      pass to the model. This can be used for conditioning.
  
  :return: a dict containing the following keys:
           - total_bpd: the total variational lower-bound, per batch element.
           - prior_bpd: the prior term in the lower-bound.
           - vb: an [N x T] tensor of terms in the lower-bound.
           - xstart_mse: an [N x T] tensor of x_0 MSEs for each timestep.
           - mse: an [N x T] tensor of epsilon MSEs for each timestep.
docstr_tok: ["Compute","the","entire","variational","lower-bound",",","measured","in","bits-per-dim",",","as","well","as","other","related","quantities",".",":","param","model",":","the","model","to","evaluate","loss","on",".",":","param","x_start",":","the","[","N","x","C","x","...","]","tensor","of","inputs",".",":","param","clip_denoised",":","if","True",",","clip","denoised","samples",".",":","param","model_kwargs",":","if","not","None",",","a","dict","of","extra","keyword","arguments","to","pass","to","the","model",".","This","can","be","used","for","conditioning",".",":","return",":","a","dict","containing","the","following","keys",":","-","total_bpd",":","the","total","variational","lower-bound",",","per","batch","element",".","-","prior_bpd",":","the","prior","term","in","the","lower-bound",".","-","vb",":","an","[","N","x","T","]","tensor","of","terms","in","the","lower-bound",".","-","xstart_mse",":","an","[","N","x","T","]","tensor","of","x_0","MSEs","for","each","timestep",".","-","mse",":","an","[","N","x","T","]","tensor","of","epsilon","MSEs","for","each","timestep","."]
code: def calc_bpd_loop(
      self, model, x_start, clip_denoised=True, model_kwargs=None
  ):
      """
      Compute the entire variational lower-bound, measured in bits-per-dim,
      as well as other related quantities.
  
      :param model: the model to evaluate loss on.
      :param x_start: the [N x C x ...] tensor of inputs.
      :param clip_denoised: if True, clip denoised samples.
      :param model_kwargs: if not None, a dict of extra keyword arguments to
          pass to the model. This can be used for conditioning.
  
      :return: a dict containing the following keys:
               - total_bpd: the total variational lower-bound, per batch element.
               - prior_bpd: the prior term in the lower-bound.
               - vb: an [N x T] tensor of terms in the lower-bound.
               - xstart_mse: an [N x T] tensor of x_0 MSEs for each timestep.
               - mse: an [N x T] tensor of epsilon MSEs for each timestep.
      """
      device = x_start.device
      batch_size = x_start.shape[0]
  
      vb = []
      xstart_mse = []
      mse = []
      for t in list(range(self.num_timesteps))[::-1]:
          t_batch = th.tensor([t] * batch_size, device=device)
          noise = th.randn_like(x_start)
          x_t = self.q_sample(x_start=x_start, t=t_batch, noise=noise)
          # Calculate VLB term at the current timestep
          with th.no_grad():
              out = self._vb_terms_bpd(
                  model,
                  x_start=x_start,
                  x_t=x_t,
                  t=t_batch,
                  clip_denoised=clip_denoised,
                  model_kwargs=model_kwargs,
              )
          vb.append(out["output"])
          xstart_mse.append(
              mean_flat((out["pred_xstart"] - x_start) ** 2)
          )
          eps = self._predict_eps_from_xstart(
              x_t, t_batch, out["pred_xstart"]
          )
          mse.append(mean_flat((eps - noise) ** 2))
  
      vb = th.stack(vb, dim=1)
      xstart_mse = th.stack(xstart_mse, dim=1)
      mse = th.stack(mse, dim=1)
  
      prior_bpd = self._prior_bpd(x_start)
      total_bpd = vb.sum(dim=1) + prior_bpd
      return {
          "total_bpd": total_bpd,
          "prior_bpd": prior_bpd,
          "vb": vb,
          "xstart_mse": xstart_mse,
          "mse": mse,
      }
  
imports: import enum
  import math
  import numpy
  import torch
  import torch
  from tqdm import tqdm
  from TTS.tts.layers.tortoise.dpm_solver import DPM_Solver, NoiseScheduleVP, model_wrapper
label_desc: Use image node_id 25 for calling the GaussianDiffusion obj's underlying member method code with example usage: obj.calc_bpd_loop(model, x_start, clip_denoised, model_kwargs) and returns: dict