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",")","."]
code: def k_diffusion_sample_loop(
      self,
      k_sampler,
      pbar,
      model,
      shape,
      noise=None,  # all given
      clip_denoised=True,
      denoised_fn=None,
      cond_fn=None,
      device=None,  # ALL UNUSED
      model_kwargs=None,  # {'precomputed_aligned_embeddings': precomputed_embeddings},
      progress=False,  # unused as well
  ):
      assert isinstance(model_kwargs, dict)
      if device is None:
          device = next(model.parameters()).device
      s_in = noise.new_ones([noise.shape[0]])
  
      def model_split(*args, **kwargs):
          model_output = model(*args, **kwargs)
          model_epsilon, model_var = th.split(
              model_output, model_output.shape[1] // 2, dim=1
          )
          return model_epsilon, model_var
  
      #
      """
          print(self.betas)
          print(th.tensor(self.betas))
          noise_schedule = NoiseScheduleVP(schedule='discrete', betas=th.tensor(self.betas))
          """
      noise_schedule = NoiseScheduleVP(
          schedule="linear",
          continuous_beta_0=0.1 / 4,
          continuous_beta_1=20.0 / 4,
      )
  
      def model_fn_prewrap(x, t, *args, **kwargs):
          """
              x_in = torch.cat([x] * 2)
              t_in = torch.cat([t_continuous] * 2)
              c_in = torch.cat([unconditional_condition, condition])
              noise_uncond, noise = noise_pred_fn(x_in, t_in, cond=c_in).chunk(2)
          print(t)
          print(self.timestep_map)
          exit()
          """
          """
              model_output = model(x, self._scale_timesteps(t*4000), **model_kwargs)
              out = self.p_mean_variance(model, x, t*4000, model_kwargs=model_kwargs)
              return out['pred_xstart']
              """
          x, _ = x.chunk(2)
          t, _ = (t * 1000).chunk(2)
          res = torch.cat(
              [
                  model_split(
                      x, t, conditioning_free=True, **model_kwargs
                  )[0],
                  model_split(x, t, **model_kwargs)[0],
              ]
          )
          pbar.update(1)
          return res
  
      model_fn = model_wrapper(
          model_fn_prewrap,
          noise_schedule,
          model_type="noise",  # "noise" or "x_start" or "v" or "score"
          model_kwargs=model_kwargs,
          guidance_type="classifier-free",
          condition=th.Tensor(1),
          unconditional_condition=th.Tensor(1),
          guidance_scale=self.conditioning_free_k,
      )
      dpm_solver = DPM_Solver(
          model_fn, noise_schedule, algorithm_type="dpmsolver++"
      )
      x_sample = dpm_solver.sample(
          noise,
          steps=self.num_timesteps,
          order=2,
          skip_type="time_uniform",
          method="multistep",
      )
      #'''
      return x_sample
  
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 12 for calling the GaussianDiffusion obj's underlying member method code with example usage: obj.k_diffusion_sample_loop(k_sampler, pbar, model, shape, noise, clip_denoised, denoised_fn, cond_fn, device, model_kwargs, progress) and returns: x_sample, model_epsilon,  model_var, res