Skip to main content

laplacekit_diffusers

laplacekit_diffusers

Laplace on denoisers from Hugging Face diffusers.

Give laplace a denoiser, its DDPM-family scheduler and the clean training samples:

post = lk.diffusers.laplace(unet, scheduler, loader, n_data=60_000)
eps = lk.predict_outputs(post, {"sample": x_t, "timestep": torch.tensor(t)})
eps.mean, eps.variance  # the predicted noise and its variance per pixel

A denoiser takes a noisy sample \(x_t\) and a timestep \(t\). It predicts the noise, the velocity or the clean sample, whichever its scheduler’s prediction_type names. For each batch, laplace draws \(t\) and the noise from the batch’s generator, builds \(x_t\) with the scheduler’s add_noise, and fits a Gaussian likelihood with unit noise to the target. The model’s outputs must have the target’s shape, so a model that also predicts a variance is refused. Flow-matching schedulers are not supported yet.

Code reaches this package as lk.diffusers, which imports it on first use.

Functions

Name Description
health Check for lk.health() that diffusers is installed.
laplace Fit a Laplace approximation to a denoiser trained with a DDPM-family scheduler.

health

health()

Check for lk.health() that diffusers is installed.

Returns

Name Type Description
tuple[lk.HealthCheck, …] One check, ok with the version when diffusers is installed and error otherwise.

laplace

laplace(
    unet,
    scheduler,
    data,
    *,
    n_data=None,
    subset=lk.subsets.last_layer,
    hessian_approx=None,
    prior=lk.priors.Isotropic(),
    hyper_tuner=lk.MarginalLikelihood(),
    generator=None,
)

Fit a Laplace approximation to a denoiser trained with a DDPM-family scheduler.

DDPM-family schedulers include DDPMScheduler and DDIMScheduler. For each batch, the timestep \(t\) is uniform over the training timesteps, \(\epsilon \sim \mathcal{N}(0, I)\), and \(x_t\) is scheduler.add_noise(x0, eps, t). All of these are drawn from the batch’s generator. The target depends on scheduler.config.prediction_type. It is \(\epsilon\) for "epsilon", scheduler.get_velocity(x0, eps, t) for "v_prediction", and \(x_0\) for "sample".

The fit is lk.laplace with denoiser(unet), that preparation of the batches, and Gaussian(sigma=1.0, event_dims=len(S)), a Gaussian with fixed unit noise.

The scheduler, its prediction_type and its noise are checked before any data is read. The noise must be variance-preserving, \(x_t = \sqrt{\bar\alpha_t}\, x_0 + \sqrt{1 - \bar\alpha_t}\, \epsilon\), which is checked at three timesteps. One batch then goes through unet to check that the output has the target’s shape, so data is iterated once more than by lk.laplace.

Parameters

Name Type Description Default
unet nn.Module The denoiser, a UNet2DModel or any diffusers model with the same call. required
scheduler object The noise scheduler. It needs config and add_noise, and also get_velocity for "v_prediction". required
data Iterable[Tensor | Mapping[str, Tensor]] Batches of clean samples \(x_0\), [N, *S], or mappings that hold the clean "sample" and conditioning entries (class_labels, encoder_hidden_states, …), which reach the model unchanged. A mapping may not hold a "timestep", since laplace draws it. required
n_data int | None The number of examples, which is the size of the training set when data is a subset of it. None
subset Subset | Params | Selection As in lk.laplace. lk.subsets.last_layer
hessian_approx lk.core.HessianApprox[Any] | None As in lk.laplace. None
prior lk.core.Prior As in lk.laplace. lk.priors.Isotropic()
hyper_tuner lk.core.HyperTuner | None As in lk.laplace. The noise is fixed, so it tunes the prior precision only. lk.MarginalLikelihood()
generator torch.Generator | None As in lk.laplace. It seeds the timesteps and the noise too. None

Returns

Name Type Description
lk.LaplacePosterior[Any, Any] The posterior, as from lk.laplace.

Raises

Name Type Description
StructureError Before any data, when the scheduler lacks config or add_noise (as a flow-matching scheduler does), its prediction_type is not "epsilon", "v_prediction" or "sample", it has "v_prediction" without get_velocity, num_train_timesteps is not an int, or its noise is not variance-preserving (as in EulerDiscreteScheduler). On the first batch, when the output is not a tensor of the target’s shape (as with a model that predicts a variance). On any batch, when the batch is neither a tensor nor a mapping with a tensor "sample", it holds a "timestep", or unet does not return a BaseOutput with a sample. Otherwise as in lk.laplace.
CapabilityError As in lk.laplace.
ValueError The data has no batches, or as in lk.laplace.
TypeError data is a one-shot iterator, or as in lk.laplace.