MCPcopy Create free account
hub / github.com/MotrixLab/FineMoGen / p_sample

Method p_sample

mogen/models/utils/gaussian_diffusion.py:608–670  ·  view source on GitHub ↗

Sample x_{t-1} from the model at the given timestep. :param model: the model to sample from. :param x: the current tensor at x_{t-1}. :param t: the value of t, starting at 0 for the first diffusion step. :param clip_denoised: if True, clip the x_start predic

(
        self,
        model,
        x,
        t,
        clip_denoised=True,
        denoised_fn=None,
        cond_fn=None,
        pre_seq=None,
        transl_req=None,
        model_kwargs=None,
    )

Source from the content-addressed store, hash-verified

606 return out
607
608 def p_sample(
609 self,
610 model,
611 x,
612 t,
613 clip_denoised=True,
614 denoised_fn=None,
615 cond_fn=None,
616 pre_seq=None,
617 transl_req=None,
618 model_kwargs=None,
619 ):
620 """
621 Sample x_{t-1} from the model at the given timestep.
622
623 :param model: the model to sample from.
624 :param x: the current tensor at x_{t-1}.
625 :param t: the value of t, starting at 0 for the first diffusion step.
626 :param clip_denoised: if True, clip the x_start prediction to [-1, 1].
627 :param denoised_fn: if not None, a function which applies to the
628 x_start prediction before it is used to sample.
629 :param cond_fn: if not None, this is a gradient function that acts
630 similarly to the model.
631 :param model_kwargs: if not None, a dict of extra keyword arguments to
632 pass to the model. This can be used for conditioning.
633 :return: a dict containing the following keys:
634 - 'sample': a random sample from the model.
635 - 'pred_xstart': a prediction of x_0.
636 """
637 # concat seq
638 if pre_seq is not None:
639 T = pre_seq.shape[1]
640 noise = th.randn_like(pre_seq)
641 x_t = self.q_sample(pre_seq, t, noise=noise)
642 x[:, :T, :] = x_t
643
644 if transl_req is not None:
645 for item in transl_req:
646 noise = th.randn(2).type_as(x)
647 transl = th.Tensor(item[1:]).type_as(x)
648 x_t = self.q_sample(transl, t, noise=noise)
649 x[:, :2, item[0]] = x_t
650
651 out = self.p_mean_variance(
652 model,
653 x,
654 t,
655 clip_denoised=clip_denoised,
656 denoised_fn=denoised_fn,
657 model_kwargs=model_kwargs,
658 )
659 noise = th.randn_like(x)
660 nonzero_mask = ((t != 0).float().view(-1, *([1] * (len(x.shape) - 1)))
661 ) # no noise when t == 0
662 if cond_fn is not None:
663 out["mean"] = self.condition_mean(cond_fn,
664 out,
665 x,

Callers 1

Calls 3

q_sampleMethod · 0.95
p_mean_varianceMethod · 0.95
condition_meanMethod · 0.95

Tested by

no test coverage detected