TorchLean API

NN.Spec.Generative.Diffusion.PFODE

Probability-flow ODE (spec layer) #

This file defines a small continuous-time VP schedule (linear $\beta(t)$) and the corresponding probability-flow ODE drift field, using an $\varepsilon_\theta(x,t)$ model.

Why include this in the spec layer:

We keep the implementation scalar-polymorphic (Context α) so it can be:

References (informal pointers):

Continuous-time linear VP schedule on $t\in[0,1]$: $\beta(t)=\beta_0+t(\beta_1-\beta_0)$.

  • beta0 : α

    $\beta(0)$.

  • beta1 : α

    $\beta(1)$.

Instances For

    Linear interpolation $\beta(t)$ on $t\in[0,1]$.

    Instances For

      Closed-form $\bar\alpha(t)$ for the VP SDE with linear $\beta(t)$:

      $$ \bar\alpha(t) =\exp\!\left(-\int_0^t\beta(s)\,ds\right) =\exp\!\left[-\left(\beta_0t+\tfrac12(\beta_1-\beta_0)t^2\right)\right]. $$

      Instances For

        $\sigma(t)=\sqrt{1-\bar\alpha(t)}$ (clamped to stay total).

        Instances For
          def Generative.Diffusion.pfOdeRhs {α : Type} [Context α] {s : Spec.Shape} (sch : VPLinearSchedule α) (model : EpsModel α s) (x : Spec.Tensor α s) (t : α) :

          Probability-flow ODE drift for a VP schedule, expressed via an $\varepsilon_\theta(x,t)$ model.

          For VP SDE:

          $$ dx=-\tfrac12\beta(t)x\,dt+\sqrt{\beta(t)}\,dW. $$

          The probability-flow ODE is:

          $$ dx=\left[-\tfrac12\beta(t)x-\tfrac12\beta(t)\operatorname{score}(x,t)\right]dt. $$

          Using the $\varepsilon$-parameterization, an approximate score is $\operatorname{score}\approx-\hat\varepsilon/\sigma(t)$, so:

          $$ dx=\left[ -\tfrac12\beta(t)x +\tfrac12\frac{\beta(t)}{\sigma(t)}\hat\varepsilon(x,t) \right]dt. $$

          Instances For
            def Generative.Diffusion.eulerStep {α : Type} [Context α] {s : Spec.Shape} (f : Spec.Tensor α sαSpec.Tensor α s) (x : Spec.Tensor α s) (t dt : α) :

            One explicit Euler step for an ODE $x'=f(x,t)$:

            $x_{\mathrm{next}}=x+dt\,f(x,t)$.

            To integrate the probability-flow ODE backwards from $t=1$ to $t=0$, use a negative $dt$.

            Instances For
              def Generative.Diffusion.pfOdeSampleEuler {α : Type} [Context α] {s : Spec.Shape} (sch : VPLinearSchedule α) (model : EpsModel α s) (steps : ) (x1 : Spec.Tensor α s) :

              Deterministic probability-flow sampler using Euler integration on a uniform grid.

              Inputs:

              • steps: number of Euler steps (typically large, e.g. 1000),
              • x1: initial state at $t=1$ (typically standard normal noise).

              We integrate backwards in time on the grid: $t_i=1-i/\mathtt{steps}$, with $dt=-1/\mathtt{steps}$.

              Instances For
                def Generative.Diffusion.pfOdeSampleEuler.loop {α : Type} [Context α] {s : Spec.Shape} (sch : VPLinearSchedule α) (model : EpsModel α s) (n : ) (dt : α) :
                Spec.Tensor α sSpec.Tensor α s
                Instances For

                  Real-valued probability-flow Euler step as a DynamicalSystem.

                  This is the formal hook used by trajectory/fixed-point/contraction lemmas in NN.Spec.Dynamics.System: at a fixed time and step size, Euler integration is an autonomous discrete update on the current sample.

                  Instances For