Karras et al. (2022) sample by integrating the probability-flow ODE from down to with Heun's second-order method on a carefully spaced grid. Implement the grid, the step, the sampler with its function-evaluation count, and a comparison with Euler at equal cost.
karras_grid(sigma_min, sigma_max, N, rho=7.0)
N + 1.heun_step(f, x, t, t_next)
(x_next, nfe). Compute and the Euler point . If t_next == 0 return (the final step is Euler-only). Otherwise compute and return .sample_ode(f, x_T, t_grid, method="heun")
t_grid[0] to t_grid[-1], with "euler" taking one evaluation per interval and "heun" using heun_step. Return {"x": final array, "nfe": total number of calls to f} (so Euler on N intervals costs N, Heun costs 2N - 1 when the grid ends at 0). Raise ValueError for other methods.compare_at_budget(f, x_T, x_exact, sigma_min, sigma_max, nfe, rho=7.0)
nfe, run Euler on karras_grid(..., N=nfe) and Heun on karras_grid(..., N=(nfe + 1) // 2) so both use exactly nfe evaluations.{"euler": {"x", "nfe", "error"}, "heun": {"x", "nfe", "error"}} where error is the float RMSE over all entries.The tests use Gaussian data , whose probability-flow drift is and whose exact solution from to is .
np.append(t, 0.0) adds the final zero to the grid.f(x, t) = t, a Heun step from 1 to 0.5 lands at while Euler gives .