nemo_automodel.components.flow_matching.adapters
nemo_automodel.components.flow_matching.adapters
Model adapters for FlowMatching Pipeline.
This module provides model-specific adapters that decouple the flow matching logic from model-specific implementation details.
Available Adapters:
- ModelAdapter: Abstract base class for all adapters
- HunyuanAdapter: For HunyuanVideo 1.5 style models
- SimpleAdapter: For simple transformer models (e.g., Wan)
- FluxAdapter: For FLUX.1 text-to-image models
- QwenImageAdapter: For Qwen-Image text-to-image models
- QwenImage21Adapter: For Qwen-Image-2.1 text-to-image models
- LTX2Adapter: For LTX-2 dual-stream video+audio models
Usage: from automodel.flow_matching.adapters import HunyuanAdapter, SimpleAdapter, FluxAdapter
Or import the base class to create custom adapters
from automodel.flow_matching.adapters import ModelAdapter
Submodules
nemo_automodel.components.flow_matching.adapters.basenemo_automodel.components.flow_matching.adapters.fluxnemo_automodel.components.flow_matching.adapters.flux2nemo_automodel.components.flow_matching.adapters.hunyuannemo_automodel.components.flow_matching.adapters.ltx2nemo_automodel.components.flow_matching.adapters.qwen_imagenemo_automodel.components.flow_matching.adapters.qwen_image_21nemo_automodel.components.flow_matching.adapters.simple
Package Contents
Classes
API
Context object passed to model adapters containing all necessary data.
This provides a clean interface for adapters to access the data they need without coupling to the batch dictionary structure.
Original batch dictionary (for model-specific data)
Probability of dropping text embeddings (setting to 0) during training for classifier-free guidance (CFG). Defaults to 0.0 for backward compatibility.
“video” or “image”
Device for tensor operations
Data type for tensor operations
[B, C, F, H, W] for video or [B, C, H, W] for image - Original clean latents (also accessible via deprecated ‘video_latents’ property for backward compatibility)
[B, C, F, H, W] or [B, C, H, W] - Noisy latents after interpolation
[B] - Sigma values
“t2v” or “i2v”
[B] - Sampled timesteps
Backward compatibility alias for ‘latents’ field.
Bases: ModelAdapter
Model adapter for FLUX.2-dev image generation models.
The preprocessor stores latents already patchified (2x2 spatial patch → channel) and BN-normalized using vae.bn running statistics. The FlowMatchingPipeline adds noise in that space, so this adapter only needs to flatten the spatial dims (pack) before the transformer call and reshape back (unpack) after.
Batch format expected from multiresolution dataloader:
- image_latents: patchified + BN-normalized latents [B, 128, H_p, W_p]
- text_embeddings: Mistral3 stacked embeddings [B, seq_len, 15360]
FLUX.2 transformer forward interface:
- hidden_states: packed latents [B, H_p*W_p, 128]
- encoder_hidden_states: text embeddings [B, seq_len, 15360]
- timestep: normalized [0, 1]
- img_ids: 4D spatial coords [B, H_p*W_p, 4]
- txt_ids: 4D text coords [B, seq_len, 4]
- guidance: guidance scale [B]
Flatten spatial dims: [B, C, H, W] -> [B, H*W, C].
Build 4D image positional IDs with coords (T=0, h, w, L=0).
Returns [B, H_p*W_p, 4] long tensor.
Build 4D text positional IDs with coords (T=0, H=0, W=0, position).
Returns [B, seq_len, 4] long tensor.
Restore spatial dims: [B, seq, C] -> [B, C, H, W].
Execute FLUX.2 transformer forward pass.
Returns prediction in [B, 128, H_p, W_p] — same space as the stored patchified latents, so MSE loss against (noise - latents) is correct.
Prepare inputs for FLUX.2 transformer from FlowMatchingContext.
Expects 4D patchified+BN-normalized latents [B, 128, H_p, W_p].
Bases: ModelAdapter
Model adapter for FLUX.1 image generation models.
Supports batch format from multiresolution dataloader:
- image_latents: [B, C, H, W] for images
- text_embeddings: T5 embeddings [B, seq_len, 4096]
- pooled_prompt_embeds: CLIP pooled [B, 768]
FLUX model forward interface:
- hidden_states: Packed latents
- encoder_hidden_states: T5 text embeddings
- pooled_projections: CLIP pooled embeddings
- timestep: Normalized timesteps [0, 1]
- img_ids / txt_ids: Positional embeddings
Pack latents from [B, C, H, W] to Flux format [B, (H//2)(W//2), C4].
Flux uses a 2x2 patch embedding, so latents are reshaped accordingly.
Prepare positional IDs for image latents.
Returns tensor of shape [B, (H//2)*(W//2), 3] containing (batch_idx, y, x).
Unpack latents from Flux format back to [B, C, H, W].
Parameters:
Packed latents of shape [B, num_patches, channels]
Original image height in pixels
Original image width in pixels
VAE compression factor (default: 8)
Execute forward pass for Flux model.
Returns unpacked prediction in [B, C, H, W] format.
Prepare inputs for Flux model from FlowMatchingContext.
Expects 4D image latents: [B, C, H, W]
Bases: ModelAdapter
Model adapter for HunyuanVideo 1.5 style models.
These models use:
- Condition latents concatenated with noisy latents
- Dual text encoders with attention masks
- Image embeddings for i2v
Expected batch keys:
- text_embeddings: Primary text encoder output [B, seq_len, dim]
- text_mask: Attention mask for primary encoder [B, seq_len] (optional)
- text_embeddings_2: Secondary text encoder output [B, seq_len, dim] (optional)
- text_mask_2: Attention mask for secondary encoder [B, seq_len] (optional)
- image_embeds: Image embeddings for i2v [B, seq_len, dim] (optional)
Example: adapter = HunyuanAdapter() pipeline = FlowMatchingPipelineV2(model_adapter=adapter)
Execute forward pass for HunyuanVideo model.
Parameters:
HunyuanVideo model
Dictionary from prepare_inputs()
Returns: torch.Tensor
Model prediction tensor
Generate conditional latents based on task type.
Parameters:
Input latents [B, C, F, H, W]
Task type (“t2v” or “i2v”)
Returns: torch.Tensor
Conditional latents [B, C+1, F, H, W]
Prepare inputs for HunyuanVideo model.
Parameters:
FlowMatchingContext with batch data
Returns: Dict[str, Any]
Dictionary containing:
Bases: ModelAdapter
Model adapter for LTX-2 dual-stream (video + audio) transformers.
The adapter is stateless: all per-step tensors live in the inputs dict
created fresh by each prepare_inputs() call, so gradient accumulation
over micro-batches is safe.
Parameters:
Multiplier on the audio flow-matching MSE loss added to the video loss (LTX-2 reference training uses 1.0).
Video frame rate the model was trained at (LTX-2 uses 24).
Drop private stash keys and kwargs the model’s forward doesn’t accept.
Parameters:
The transformer (possibly wrapped; unwrapped via .module
for signature inspection only - the wrapped module is called).
Full inputs dict from prepare_inputs().
Returns: Dict[str, Any]
Kwargs safe to splat into model(...).
Compute the audio flow-matching loss from stashed tensors.
Parameters:
Dictionary from prepare_inputs() after forward() has run;
must contain _audio_pred [B, 8, L, 16] and _audio_target
(float32 [B, 8, L, 16]).
Returns: Dict[str, torch.Tensor] | None
{“audio_loss”: scalar} - unweighted MSE in float32 scaled by
Execute the dual-stream forward pass.
Calls the LTX-2 transformer with the kwargs from prepare_inputs()
(filtered to the model’s forward signature so minor diffusers-version
differences in optional kwargs don’t break the call). The raw
transformer outputs ARE the flow predictions (no conversion).
Parameters:
The LTX-2 transformer (possibly FSDP-wrapped).
Dictionary from prepare_inputs(). Mutated: the unpacked
audio prediction is stashed under _audio_pred for
auxiliary_losses().
Returns: torch.Tensor
Video flow prediction [B, 128, F, H, W].
Prepare dual-stream transformer inputs; noise the audio latents.
The video latents are already noised by the pipeline
(context.noisy_latents). Audio latents are noised here with the
same per-sample sigma and an independent Gaussian draw, in float32
(matching the pipeline’s video noising), then cast to the model dtype.
Parameters:
FlowMatchingContext with batch data. context.timesteps
is the rescaled timestep (sigma * num_train_timesteps) [B];
context.sigma is the raw sigma in [0, 1] [B].
Returns: Dict[str, Any]
Dictionary containing the transformer kwargs:
Raises:
KeyError: If the batch lacks LTX-2 audio/text cache keys.
Abstract base class for model-specific forward pass logic.
Implement this class to add support for new model architectures without modifying the FlowMatchingPipeline.
The adapter pattern decouples the flow matching logic from model-specific details like input preparation and forward pass conventions.
Example: class MyCustomAdapter(ModelAdapter): def prepare_inputs(self, context: FlowMatchingContext) -> Dict[str, Any]: return { “x”: context.noisy_latents, “t”: context.timesteps, “cond”: context.batch[“my_conditioning”], }
def forward(self, model: nn.Module, inputs: Dict[str, Any]) -> torch.Tensor: return model(**inputs)
pipeline = FlowMatchingPipelineV2(model_adapter=MyCustomAdapter())
Return additional named scalar losses computed from adapter-stashed state.
Called by FlowMatchingPipeline.step() after the primary loss is
computed. Override for models that train extra prediction streams
(e.g. a second modality) alongside the primary latents: stash the
needed tensors in the inputs dict during prepare_inputs() /
forward() and compute the losses here.
Parameters:
The dict returned by prepare_inputs(), after
forward() has run (adapters may stash prediction tensors
in it during forward()).
Returns: Dict[str, torch.Tensor] | None
Mapping of metric name to scalar loss tensor (each connected to
Compute the unreduced flow-matching loss.
Parameters:
Tensor of shape [batch, …], with arbitrary trailing latent dimensions.
Tensor of shape [batch, …] matching model_pred, containing the flow velocity target.
Returns: torch.Tensor
Float32 tensor of shape [batch, …] matching model_pred, containing the per-element loss.
Execute the model forward pass.
Parameters:
The model to call
Dictionary of inputs from prepare_inputs()
Returns: torch.Tensor
Model prediction tensor
Post-process model prediction if needed.
Override this for models that return extra outputs or need transformation.
Parameters:
Raw model output
Returns: torch.Tensor
Processed prediction tensor
Prepare model-specific inputs from the context.
Parameters:
FlowMatchingContext containing all necessary data
Returns: Dict[str, Any]
Dictionary of inputs to pass to the model’s forward method
Bases: ModelAdapter
Model adapter for Qwen-Image-2.1 text-to-image models.
Supports batch format from multiresolution dataloader:
- image_latents: [B, 64, H, W]
- text_embeddings: Qwen3-VL embeddings [B, seq_len, 4096], right-padded
- text_attention_mask: optional [B, seq_len] bool marking valid text tokens
Qwen-Image-2.1 transformer forward interface:
- hidden_states: Flattened latents [B, H*W, 64]
- encoder_hidden_states: Text embeddings [B, text_len, 4096]
- timestep: Normalized timesteps [0, 1]
- img_shapes: [[(1, H, W)]] per sample
- img_mask: [B, text_len + H*W/4] bool, True at target-image slots
The transformer lays out RoPE from row 0 of img_mask for the whole batch, and every text position
(padding included) advances the target image’s frame index. Each sample is therefore run as its own
unpadded transformer call, so it sees exactly the positions of single-prompt inference.
Text positions first, then one slot per 2x2 group of target latent tokens.
Flatten latents from [B, C, H, W] to [B, H*W, C] (no patching in 2.1).
Restore [B, H*W, C] token predictions to [B, C, H, W].
Execute forward pass for Qwen-Image-2.1 model.
Runs one transformer call per sample, trimmed to that sample’s prompt length, and returns the target-image prediction in [B, C, H, W] format.
The call count is always the local batch size, never the number of distinct prompt lengths: under FSDP every call issues collectives, so all ranks must make the same number of calls, and the diffusion sampler gives every rank the same local batch size.
Prepare inputs for Qwen-Image-2.1 model from FlowMatchingContext.
Expects 4D image latents: [B, C, H, W] with even H and W.
Bases: ModelAdapter
Model adapter for Qwen-Image text-to-image models.
Supports batch format from multiresolution dataloader:
- image_latents: [B, C, H, W]
- text_embeddings: Qwen2 embeddings [B, seq_len, hidden_dim]
Qwen-Image transformer forward interface:
- hidden_states: Packed latents [B, num_patches, C*4]
- encoder_hidden_states: Qwen2 text embeddings [B, seq_len, hidden_dim]
- encoder_hidden_states_mask: Attention mask (None for flash attention)
- timestep: Normalized timesteps [0, 1]
- img_shapes: List of image shape tuples [[(1, h//2, w//2)]] per sample
- guidance: Optional guidance scale embedding [B]
Pack latents from [B, C, H, W] to [B, (H//2)(W//2), C4].
Uses 2x2 patch grouping to match the transformer’s patch embedding.
Unpack latents from [B, num_patches, channels] back to [B, C, H, W].
Parameters:
Packed latents of shape [B, num_patches, channels]
Original image height in pixels
Original image width in pixels
VAE compression factor (default: 8)
Execute forward pass for Qwen-Image model.
Returns unpacked prediction in [B, C, H, W] format.
Prepare inputs for Qwen-Image model from FlowMatchingContext.
Expects 4D image latents: [B, C, H, W]
Bases: ModelAdapter
Model adapter for simple transformer models (e.g., Wan).
These models use a simple interface with:
- hidden_states: noisy latents
- timestep: timestep values
- encoder_hidden_states: text embeddings
Expected batch keys:
- text_embeddings: Text encoder output [B, seq_len, dim]
Example: adapter = SimpleAdapter() pipeline = FlowMatchingPipelineV2(model_adapter=adapter)
Execute forward pass for simple transformer model.
Parameters:
Transformer model
Dictionary from prepare_inputs()
Returns: torch.Tensor
Model prediction tensor
Prepare inputs for simple transformer model.
Parameters:
FlowMatchingContext with batch data
Returns: Dict[str, Any]
Dictionary containing: