TVMForImageGeneration
ImageGenerationModelTVMForImageGeneration(config: TVMConfig)TVM's objective and its one- and few-step sampler.
Parameters
configTVMConfigAttributes
tvmTVMModelNotes
Reference: Zhou, Parger, Haque, and Song, "Terminal Velocity Matching", ICLR 2026 (arXiv:2511.19797). Training is Algorithm 1 with the objective of Eq. 15; sampling is Figure 3.
Call update_ema once per optimiser step. Algorithm 1
keeps two exponential moving averages of the weights and updates both
after every step: at
TVMConfig.target_ema_decay, which the terminal target and the
unconditional half of the guided velocity are read off, and
at TVMConfig.eval_ema_decay, the model
that is evaluated and that the released checkpoints are. Only the
caller knows when an optimiser step has happened.
Both copies are created from the live weights the first time either
is needed — the first training step reads , so that
is at initialisation, as Algorithm 1 has it — and both are kept
outside the module tree: not parameters, not in state_dict,
not counted. They are training state, like an optimiser's moments;
target_network and ema_network expose them for
checkpointing. Loading weights into the network — through
load_state_dict here or on tvm — restarts both from the
loaded weights, so to resume a run load the network first and the
averages' own checkpoints after it.
Examples
>>> import lucid
>>> from lucid.models.generative.tvm import (
... TVMConfig, TVMForImageGeneration)
>>> config = TVMConfig(sample_size=8, patch_size=2, hidden_size=32,
... depth=2, num_heads=4, num_classes=10)
>>> model = TVMForImageGeneration(config).eval()
>>> model.generate(2, labels=lucid.tensor([1, 2])).samples.shape
(2, 4, 8, 8)Used by 2
Constructors
1Properties
2The evaluation EMA — what Algorithm 1 outputs.
Returns
TVMModelThe averaged network.
Raises
ValueErrorTVMConfig.eval_ema_decay is None, so no
evaluation average is kept.The EMA network the targets are read from.
With TVMConfig.target_ema_decay at zero this is the live
network itself, used under a stop-gradient — Table 4b's
.
Returns
TVMModelThe target network.
Instance methods
5One training step of the TVM objective (Eq. 15).
Parameters
Returns
TVMOutputThe loss and its two terms.
Notes
Per sample, with ,
Squared norms are averaged over elements, as the released training code computes them; the paper's sum differs by the constant .
Whiten a Stable Diffusion VAE latent the way the model was trained.
Parameters
latentTensor(B, C, H, W) — a sample from the VAE posterior,
, which is
what the released pipeline encodes images into (not the
posterior mean).Returns
Tensor per channel — the images
forward trains on. to_vae_latent inverts it.
Examples
>>> import lucid
>>> from lucid.models import tvm_imagenet256_gen
>>> model = tvm_imagenet256_gen(sample_size=8, hidden_size=32, depth=2, num_heads=4)
>>> model.from_vae_latent(lucid.zeros((1, 4, 8, 8))).shape
(1, 4, 8, 8)generate(n_samples: int = 1, labels: Tensor | None = None, steps: int = 1, guidance_scale: float | None = None, noise: Tensor | None = None, device: str | None = None, use_ema: bool = False)Jump from noise to data in steps evaluations.
Parameters
n_ samplesint= 1(n_samples,) class indices. None samples the
unconditional field.stepsint= 1guidance_ scalefloat or None= NoneNone uses the weight the model was trained at
for labelled draws and one for unlabelled ones.devicestr or None= Noneuse_ emabool= FalseReturns
GenerationOutputsamples of shape (n_samples, out_channels, H, W) — a
latent, for the Stable Diffusion VAE to decode.
Raises
ValueErrorsteps is not positive.Examples
>>> from lucid.models import tvm_imagenet256_gen
>>> model = tvm_imagenet256_gen(sample_size=8, hidden_size=32, depth=2, num_heads=4)
>>> model.eval().generate(2, steps=4).samples.shape
(2, 4, 8, 8)Undo the per-channel whitening the model's latents carry.
Parameters
samplesTensor(B, C, H, W) — generate's output.Returns
Tensor with the statistics in
TVMConfig.latent_mean / TVMConfig.latent_std —
the raw latent the Stable Diffusion VAE's decoder expects.
Notes
The released models were trained on stabilityai/sd-vae-ft-ema
posterior samples whitened per channel, not scaled by 0.18215, so
decoding a sample without this step yields saturated noise.
lucid.models.generative.stable_diffusion.AutoencoderKL
is that decoder.
Examples
>>> import lucid
>>> from lucid.models import tvm_imagenet256_gen
>>> model = tvm_imagenet256_gen(sample_size=8, hidden_size=32, depth=2, num_heads=4)
>>> model.to_vae_latent(lucid.zeros((1, 4, 8, 8))).shape
(1, 4, 8, 8)Advance both of Algorithm 1's averages by one step.
and , over parameters only — the released training loop averages nothing else, and this network has no persistent buffers. Call it after each optimiser step.
The averages are evaluated once they are written. They are
outside parameters, so neither the optimiser's flush nor a
loop's reaches them on Metal.