Skip to content
class

DriftingForImageGeneration

extendsImageGenerationModel
DriftingForImageGeneration(config: DriftingConfig)

The drifting objective and its one-step sampler.

Parameters

The variant to build.

Attributes

The generator — the only trainable network.
feature_encoder_DriftingFeatureEncoder
The frozen ϕ\phi. Empty until load_feature_encoder builds it, unless the config drifts in the raw space (use_mae=False, use_convnext=False). It moves and casts with the model but is not a child of it, so it is in no state_dict, parameters() or train() walk.

Notes

Reference: Deng, Li, Li, Du, and He, "Generative Modeling via Drifting", arXiv:2602.04770, 2026. Training is Alg. 1 with the feature-space loss of Eq. (14) and the guidance of A.7; sampling is one evaluation of fθf_\theta.

A checkpoint holds the generator alone: about 8.5 GB of encoder at real size has no place in it, and from_pretrained loads strictly into a model whose encoder is an empty shell.

Train under lucid.amp.autocast with dtype=lucid.bfloat16 rather than casting the model, which loses optimizer updates (see DriftingConfig).

Examples

>>> import lucid
>>> from lucid.models.generative.drifting import (
...     DriftingConfig, DriftingForImageGeneration)
>>> config = DriftingConfig(sample_size=8, hidden_size=32, cond_dim=32, depth=1,
...                         num_heads=4, num_classes=10, noise_classes=4,
...                         noise_coords=2, n_cls_tokens=2, gen_per_label=4,
...                         use_mae=False)
>>> model = DriftingForImageGeneration(config)
>>> model(lucid.randn(2, 3, 4, 8, 8), lucid.tensor([0, 1])).loss.ndim
0
>>> model.generate(2, labels=lucid.tensor([3, 7])).samples.shape
(2, 4, 8, 8)

Used by 2

Constructors

1
dunder

__init__

→None
__init__(config: DriftingConfig)

Initialise the model. See the class docstring for parameters.

Instance methods

5
fn

draw_training_batch

→Tensor
draw_training_batch(positive_queue: _DriftingSampleQueue, unconditional_queue: _DriftingSampleQueue, labels: Tensor)

Draw what one training step reads from the two queues.

Parameters

positive_queue_DriftingSampleQueue
The per-class queue make_sample_queues made.
unconditional_queue_DriftingSampleQueue
The single-class queue make_sample_queues made. With neg_per_sample=0 it is not read, so it need not have been fed.
labelsTensor
(N_c,) the classes to train on.

Returns

Tensor

(N_c, pos_per_sample, ...) samples of each label.

fn

forward

→DriftingOutput
forward(images: Tensor, labels: Tensor, negatives: Tensor | None = None, guidance_scale: float | Tensor | None = None)

One drifting training step.

Parameters

imagesTensor
Positives: (N_c, N_pos, C, H, W) real samples of each label, or (N_c, C, H, W) for one each. At least one is required.
labelsTensor
(N_c,) class labels.
negativesTensor or None= None
(N_c, N_unc, C, H, W) unconditional real samples (random classes) — the extra negatives guidance trains with. None, or N_unc = 0, trains without guidance.
guidance_scalefloat or Tensor or None= None
Guidance scales: a float or 0-d tensor for every label, or (N_c,). Drawn from the configured power law when None and negatives are given; 1 when neither is.

Returns

DriftingOutput

The loss, the generated batch, the scales, the per-feature terms and the raw drift energies.

Raises

ValueError
If the batch is empty, or its tensors do not fit together — guidance_scale included.
TypeError
If guidance_scale is neither a number nor a tensor.
RuntimeError
If the config asks for a learned feature space and no encoder has been built.

Notes

gen_per_label samples are generated per label. Each feature set gets its own drifting loss, computed independently per label and location (rows (label, location)), against that label's positives, its own generated batch, and — weighted by w=(α−1)(Nneg−1)/Nuncw = (\alpha - 1)(N_\mathrm{neg} - 1) / N_\mathrm{unc} — the unconditional samples; the terms are summed (Eq. 14).

fn

generate

→GenerationOutput
generate(n_samples: int = 1, labels: Tensor | None = None, guidance_scale: float | Tensor = 1.0, temperature: float = 1.0, noise: Tensor | None = None, style_indices: Tensor | None = None, device: str | None = None)

Sample in one network evaluation.

Parameters

n_samplesint= 1
How many to draw.
labelsTensor or None= None
(n_samples,) class indices. Drawn uniformly when None — the model is class-conditional and has no null class.
guidance_scalefloat or Tensor= 1.0
Guidance scale α\alpha; any value in the trained range works without retraining. The paper's best: 1.0 for L/2, B/16, L/16; 1.1 for B/2.
temperaturefloat= 1.0
Multiplier on the Gaussian noise (the release's temp).
noiseTensor or None= None
(n_samples, C, H, W) noise; drawn when absent (and then scaled by temperature).
style_indicesTensor or None= None
(n_samples, noise_coords) style indices; drawn when absent.
devicestr or None= None
Where to draw. Defaults to the model's device.

Returns

GenerationOutput

samples of shape (n_samples, out_channels, H, W) — an SD-VAE latent for the latent models, [-1, 1] RGB for the pixel models.

Examples

A released configuration, shrunk by overrides so it builds quickly:
>>> from lucid.models import create_model
>>> model = create_model("drifting_base_2_gen", sample_size=8, hidden_size=32,
...                      cond_dim=32, depth=1, num_heads=4).eval()
>>> model.generate(3, guidance_scale=1.5).samples.shape
(3, 4, 8, 8)
fn

load_feature_encoder

→_DriftingFeatureEncoder
load_feature_encoder(pretrained: bool = False)

Build the frozen feature encoder, with random weights.

Parameters

pretrainedbool= False
No trained encoder is published, so True raises NotImplementedError before anything is allocated.

Returns

_DriftingFeatureEncoder

The encoder, also stored as self.feature_encoder, with every parameter frozen, on the generator's device and in its dtype. Called on (B, C, H, W) samples it returns the feature sets the loss is summed over, a dict of (B, T, D) tensors keyed by set name, "global" (the raw sample) first. forward reads it from self.feature_encoder, so the return value is only needed to look at the features.

Raises

NotImplementedError
If pretrained is set.

Notes

The paper's objective is the drifting loss in the feature space of an encoder trained as an MAE (A.3), and of ConvNeXt-V2 for pixels (A.4). A random encoder is a different objective: training runs end to end against it, which exercises the pipeline, but do not expect the paper's results from it. use_mae=False, use_convnext=False drifts in the raw space and needs no encoder.

fn

make_sample_queues

→_DriftingSampleQueue
make_sample_queues()

The two empty sample queues a training loop feeds (A.8's memory banks).

Returns

_DriftingSampleQueue

num_classes ring buffers of positive_bank_size real samples each, one per class.

Notes

Each step, push the real batch into both with add(samples, labels): (N, C, H, W) samples and their (N,) integer labels, all zeros for unconditional_queue. A full buffer overwrites its oldest entry. draw_training_batch then reads pos_per_sample positives per label and neg_per_sample unconditional negatives from them, which are the images and negatives of forward.

Examples

>>> import lucid
>>> import lucid.models as models
>>> model = models.create_model("drifting_base_2_gen", pos_per_sample=3,
...                             neg_per_sample=2, use_mae=False, sample_size=8,
...                             hidden_size=32, cond_dim=32, depth=1, num_heads=4,
...                             num_classes=10, gen_per_label=4)
>>> positive_queue, unconditional_queue = model.make_sample_queues()
>>> images, labels = lucid.randn(6, 4, 8, 8), lucid.tensor([0, 1, 0, 1, 0, 1])
>>> positive_queue.add(images, labels)
>>> unconditional_queue.add(images, lucid.zeros_like(labels))
>>> step = lucid.tensor([0, 1])
>>> positives, negatives = model.draw_training_batch(
...     positive_queue, unconditional_queue, step)
>>> positives.shape, negatives.shape
((2, 3, 4, 8, 8), (2, 2, 4, 8, 8))
>>> model(positives, step, negatives).loss.ndim
0