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: Sample x_{t-1} from the model using Pseudo Linear Multistep.
  
  Same usage as p_sample().
docstr_tok: ["Sample","x_","{","t-1","}","from","the","model","using","Pseudo","Linear","Multistep",".","Same","usage","as","p_sample","(",")","."]
code: def plms_sample(
      self,
      model,
      x,
      t,
      clip_denoised=True,
      denoised_fn=None,
      cond_fn=None,
      model_kwargs=None,
      cond_fn_with_grad=False,
      order=2,
      old_out=None,
  ):
      """
      Sample x_{t-1} from the model using Pseudo Linear Multistep.
  
      Same usage as p_sample().
      """
      if not int(order) or not 1 <= order <= 4:
          raise ValueError("order is invalid (should be int from 1-4).")
  
      def get_model_output(x, t):
          with paddle.set_grad_enabled(
              cond_fn_with_grad and cond_fn is not None
          ):
              x = (
                  x.detach().requires_grad_()
                  if cond_fn_with_grad
                  else x
              )
              out_orig = self.p_mean_variance(
                  model,
                  x,
                  t,
                  clip_denoised=clip_denoised,
                  denoised_fn=denoised_fn,
                  model_kwargs=model_kwargs,
              )
              if cond_fn is not None:
                  if cond_fn_with_grad:
                      out = self.condition_score_with_grad(
                          cond_fn,
                          out_orig,
                          x,
                          t,
                          model_kwargs=model_kwargs,
                      )
                      x = x.detach()
                  else:
                      out = self.condition_score(
                          cond_fn,
                          out_orig,
                          x,
                          t,
                          model_kwargs=model_kwargs,
                      )
              else:
                  out = out_orig
  
          # Usually our model outputs epsilon, but we re-derive it
          # in case we used x_start or x_prev prediction.
          eps = self._predict_eps_from_xstart(x, t, out["pred_xstart"])
          return eps, out, out_orig
  
      alpha_bar = _extract_into_tensor(self.alphas_cumprod, t, x.shape)
      alpha_bar_prev = _extract_into_tensor(
          self.alphas_cumprod_prev, t, x.shape
      )
      eps, out, out_orig = get_model_output(x, t)
  
      if order > 1 and old_out is None:
          # Pseudo Improved Euler
          old_eps = [eps]
          mean_pred = (
              out["pred_xstart"] * paddle.sqrt(alpha_bar_prev)
              + paddle.sqrt(1 - alpha_bar_prev) * eps
          )
          eps_2, _, _ = get_model_output(mean_pred, t - 1)
          eps_prime = (eps + eps_2) / 2
          pred_prime = self._predict_xstart_from_eps(x, t, eps_prime)
          mean_pred = (
              pred_prime * paddle.sqrt(alpha_bar_prev)
              + paddle.sqrt(1 - alpha_bar_prev) * eps_prime
          )
      else:
          # Pseudo Linear Multistep (Adams-Bashforth)
          old_eps = old_out["old_eps"]
          old_eps.append(eps)
          cur_order = min(order, len(old_eps))
          if cur_order == 1:
              eps_prime = old_eps[-1]
          elif cur_order == 2:
              eps_prime = (3 * old_eps[-1] - old_eps[-2]) / 2
          elif cur_order == 3:
              eps_prime = (
                  23 * old_eps[-1] - 16 * old_eps[-2] + 5 * old_eps[-3]
              ) / 12
          elif cur_order == 4:
              eps_prime = (
                  55 * old_eps[-1]
                  - 59 * old_eps[-2]
                  + 37 * old_eps[-3]
                  - 9 * old_eps[-4]
              ) / 24
          else:
              raise RuntimeError("cur_order is invalid.")
          pred_prime = self._predict_xstart_from_eps(x, t, eps_prime)
          mean_pred = (
              pred_prime * paddle.sqrt(alpha_bar_prev)
              + paddle.sqrt(1 - alpha_bar_prev) * eps_prime
          )
  
      if len(old_eps) >= order:
          old_eps.pop(0)
  
      nonzero_mask = paddle.cast((t != 0), "float32").reshape(
          [-1, *([1] * (len(x.shape) - 1))]
      )
      sample = mean_pred * nonzero_mask + out["pred_xstart"] * (
          1 - nonzero_mask
      )
  
      return {
          "sample": sample,
          "pred_xstart": out_orig["pred_xstart"],
          "old_eps": old_eps,
      }
  
imports: import enum
  import math
  import numpy
  import paddle
  from .losses import discretized_gaussian_log_likelihood
  from .losses import normal_kl
  from .nn import mean_flat
label_desc: Use image node_id 23 for calling the GaussianDiffusion obj's underlying member method code with example usage: obj.plms_sample(model, x, t, clip_denoised, denoised_fn, cond_fn, model_kwargs, cond_fn_with_grad, order, old_out) and returns: dict, eps,  out,  out_orig