Skip to content
class

FlowMatchingForImageGeneration

extendsImageGenerationModel
FlowMatchingForImageGeneration(config: FlowMatchingConfig)

Flow Matching with the CFM training loss and .generate().

forward(x) returns a DiffusionModelOutput whose loss is the Conditional Flow Matching objective and whose sample is the field's prediction at the sampled path points. generate(n) integrates the field from a standard-normal draw to t = 1.

Parameters

Architecture, path and solver settings.

Attributes

flow_matchingFlowMatchingModel
Underlying velocity field, exposing the path and likelihood API.

Notes

Reference: Lipman, Chen, Ben-Hamu, Nickel, and Le, "Flow Matching for Generative Modeling", ICLR, 2023 (arXiv:2210.02747). Reported for the optimal-transport path: 6.35 FID / 2.99 bits per dimension on CIFAR-10, and 5.02 / 3.53, 14.45 / 3.31, 20.9 / 2.90 on ImageNet at 32, 64 and 128.

Examples

>>> import lucid
>>> from lucid.models.generative.flow_matching import (
...     FlowMatchingConfig, FlowMatchingForImageGeneration,
... )
>>> cfg = FlowMatchingConfig(sample_size=8, base_channels=16,
...                          channel_mult=(1, 2), num_res_blocks=1,
...                          attention_resolutions=(), resnet_groups=8)
>>> model = FlowMatchingForImageGeneration(cfg).eval()
>>> model(lucid.randn((2, 3, 8, 8))).loss.shape
()
>>> model.generate(n_samples=2, steps=4).samples.shape
(2, 3, 8, 8)

Used by 2

Constructors

1
dunder

__init__

→None
__init__(config: FlowMatchingConfig)

Properties

1
prop

nfe

→int
nfe: int

int: Field evaluations spent by the most recent solve.

Instance methods

2
fn

generate

→GenerationOutput
generate(n_samples: int = 1, steps: int | None = None, device: str | None = None, noise: Tensor | None = None)

Sample by integrating the field from t = 0 to t = 1.

Parameters

n_samplesint= 1
Number of samples to draw.
stepsint= None
Fixed evaluation budget instead of an adaptive solve — the regime in which straighter paths pay off.
devicestr= None
Where to allocate the prior draw. Defaults to the device the model's parameters are on.
noiseTensor= None
Starting point in place of a fresh draw.

Returns

GenerationOutput

samples of shape (n_samples, C, H, W).

Examples

>>> import lucid
>>> from lucid.models.generative.flow_matching import (
...     FlowMatchingConfig, FlowMatchingForImageGeneration,
... )
>>> cfg = FlowMatchingConfig(sample_size=8, base_channels=16,
...                          channel_mult=(1, 2), num_res_blocks=1,
...                          attention_resolutions=(), resnet_groups=8)
>>> model = FlowMatchingForImageGeneration(cfg).eval()
>>> model.generate(n_samples=3, steps=2).samples.shape
(3, 3, 8, 8)
>>> model.nfe  # a fixed budget: two steps, two field evaluations
2
>>> noise = lucid.randn((1, 3, 8, 8))
>>> model.generate(noise=noise, steps=2).samples.shape  # batch follows noise
(1, 3, 8, 8)