Skip to content
class

TVMForImageGeneration

extendsImageGenerationModel
TVMForImageGeneration(config: TVMConfig)

TVM's objective and its one- and few-step sampler.

Parameters

configTVMConfig
The variant to build.

Attributes

The network being trained.

Notes

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: θ∗\theta^* at TVMConfig.target_ema_decay, which the terminal target and the unconditional half of the guided velocity are read off, and θ∗∗\theta^{**} 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 θ∗\theta^*, so that is θ∗←θ,θ∗∗←θ\theta^* \leftarrow \theta,\theta^{**} \leftarrow \theta 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

1
dunder

__init__

→None
__init__(config: TVMConfig)

Initialise the model. See the class docstring for parameters.

Properties

2
prop

ema_network

→TVMModel
ema_network: TVMModel

The evaluation EMA θ∗∗\theta^{**} — what Algorithm 1 outputs.

Returns

TVMModel

The averaged network.

Raises

ValueError
If TVMConfig.eval_ema_decay is None, so no evaluation average is kept.
prop

target_network

→TVMModel
target_network: TVMModel

The EMA network θ∗\theta^* 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 γ=0\gamma = 0.

Returns

TVMModel

The target network.

Instance methods

5
fn

forward

→TVMOutput
forward(images: Tensor, labels: Tensor | None = None)

One training step of the TVM objective (Eq. 15).

Parameters

imagesTensor
(B, C, H, W) — the data, here the VAE latent of it.
labelsTensor or None= None
(B,) class indices. Each is replaced by the null label with probability TVMConfig.class_dropout, and that sample trains at w=1w = 1. None trains the unconditional field throughout.

Returns

TVMOutput

The loss and its two terms.

Notes

Per sample, with β=1/w\beta = 1/w,

β2∥ddsfθ(xt,t,s)−uθ∗(sg[xt+fθ(xt,t,s)],s)∥21t≠s+β2∥uθ(xs′,s′)−[w v+(1−w) uθ∗(xs′,s′,∅,1)]∥2.\beta^2\Bigl\lVert \tfrac{d}{ds} f_\theta(x_t, t, s) - u_{\theta^*}\bigl(\mathrm{sg}[x_t + f_\theta(x_t, t, s)], s\bigr)\Bigr\rVert^2 \mathbb{1}_{t \ne s} + \beta^2\bigl\lVert u_\theta(x_{s'}, s') - \bigl[w\,v + (1 - w)\,u_{\theta^*}(x_{s'}, s', \varnothing, 1)\bigr]\bigr\rVert^2 .

Squared norms are averaged over elements, as the released training code computes them; the paper's sum differs by the constant CHWCHW.

fn

from_vae_latent

→Tensor
from_vae_latent(latent: Tensor)

Whiten a Stable Diffusion VAE latent the way the model was trained.

Parameters

latentTensor
(B, C, H, W) — a sample from the VAE posterior, ℓ=μϕ(x)+σϕ(x) ϵ\ell = \mu_\phi(x) + \sigma_\phi(x)\,\epsilon, which is what the released pipeline encodes images into (not the posterior mean).

Returns

Tensor

z=(ℓ−μ)/σz = (\ell - \mu) / \sigma 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)
fn

generate

→GenerationOutput
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
How many to draw.
labelsTensor or None= None
(n_samples,) class indices. None samples the unconditional field.
stepsint= 1
Network evaluations. [0,1][0, 1] is split evenly and each piece is one jump xs=xt+(s−t) wFθ(xt,t,s)x_s = x_t + (s - t)\,w F_\theta(x_t, t, s); the same network serves every step count, from one up to the ODE limit, without retraining.
guidance_scalefloat or None= None
ww. None uses the weight the model was trained at for labelled draws and one for unlabelled ones.
noiseTensor or None= None
Starting point x1x_1. Drawn when absent.
devicestr or None= None
Where to draw. Defaults to the model's own device.
use_emabool= False
Sample from the evaluation EMA θ∗∗\theta^{**} rather than the live weights — what the paper evaluates after training. A loaded checkpoint already is that average, so the default samples from the live network.

Returns

GenerationOutput

samples of shape (n_samples, out_channels, H, W) — a latent, for the Stable Diffusion VAE to decode.

Raises

ValueError
If steps 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)
fn

to_vae_latent

→Tensor
to_vae_latent(samples: Tensor)

Undo the per-channel whitening the model's latents carry.

Parameters

samplesTensor
(B, C, H, W) — generate's output.

Returns

Tensor

ℓ=z σ+μ\ell = z\,\sigma + \mu 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)
fn

update_ema

→None
update_ema()

Advance both of Algorithm 1's averages by one step.

θ∗←γ∗θ∗+(1−γ∗)θ\theta^* \leftarrow \gamma^*\theta^* + (1 - \gamma^*)\theta and θ∗∗←γ∗∗θ∗∗+(1−γ∗∗)θ\theta^{**} \leftarrow \gamma^{**}\theta^{**} + (1 - \gamma^{**})\theta, 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.