DriftingForImageGeneration
ImageGenerationModelDriftingForImageGeneration(config: DriftingConfig)The drifting objective and its one-step sampler.
Parameters
configDriftingConfigAttributes
driftingDriftingModelfeature_ encoder_DriftingFeatureEncoderload_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 .
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
1Instance methods
5draw_training_batch(positive_queue: _DriftingSampleQueue, unconditional_queue: _DriftingSampleQueue, labels: Tensor)Draw what one training step reads from the two queues.
Parameters
positive_ queue_DriftingSampleQueuemake_sample_queues made.unconditional_ queue_DriftingSampleQueuemake_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.
forward(images: Tensor, labels: Tensor, negatives: Tensor | None = None, guidance_scale: float | Tensor | None = None)One drifting training step.
Parameters
imagesTensor(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.(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.(N_c,). Drawn from the configured power law when None
and negatives are given; 1 when neither is.Returns
DriftingOutputThe loss, the generated batch, the scales, the per-feature terms and the raw drift energies.
Raises
ValueErrorguidance_scale included.TypeErrorguidance_scale is neither a number nor a tensor.RuntimeErrorNotes
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
— the
unconditional samples; the terms are summed (Eq. 14).
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(n_samples,) class indices. Drawn uniformly when None —
the model is class-conditional and has no null class.1.0 for
L/2, B/16, L/16; 1.1 for B/2.temperaturefloat= 1.0temp).(n_samples, C, H, W) noise; drawn when absent (and then
scaled by temperature).(n_samples, noise_coords) style indices; drawn when absent.devicestr or None= NoneReturns
GenerationOutputsamples 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)load_feature_encoder
→_DriftingFeatureEncoderload_feature_encoder(pretrained: bool = False)Build the frozen feature encoder, with random weights.
Parameters
pretrainedbool= FalseTrue raises
NotImplementedError before anything is allocated.Returns
_DriftingFeatureEncoderThe 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
NotImplementedErrorpretrained 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.
The two empty sample queues a training loop feeds (A.8's memory banks).
Returns
_DriftingSampleQueuenum_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