Skip to navigation
ReferenceFull Library ReferenceNemo AutomodelNemo AutomodelComponentsFlow Matchingnemo_automodel.components.flow_matching.time_shift_utils

nemo_automodel.components.flow_matching.time_shift_utils

View as MarkdownOpen in Claude

Module Contents

Functions

NameDescription
compute_density_for_timestep_samplingSample timesteps from different distributions for better training coverage.
get_flow_match_loss_weightCompute loss weights for flow matching based on sigma values.
time_shiftConvert timesteps to sigmas with sequence-length-aware shifting.

API

nemo_automodel.components.flow_matching.time_shift_utils.compute_density_for_timestep_sampling(
weighting_scheme: typing.Literal['logit_normal', 'mode', 'uniform'] | str,
batch_size: int,
logit_mean: float = 0.0,
logit_std: float = 1.0,
mode_scale: float = 1.29
) -> torch.Tensor

Sample timesteps from different distributions for better training coverage.

Parameters:

weighting_scheme
Literal['logit_normal', 'mode', 'uniform'] | str

“logit_normal” or “mode” for the corresponding transform. “uniform” and any other value use uniform sampling.

batch_size
int

number of samples to generate

logit_mean
floatDefaults to 0.0

mean for logit-normal distribution

logit_std
floatDefaults to 1.0

std for logit-normal distribution

mode_scale
floatDefaults to 1.29

scale for mode-based sampling

Returns: torch.Tensor

Tensor of shape [batch_size] on the CPU, using PyTorch’s default floating-point dtype. Logit-normal samples

nemo_automodel.components.flow_matching.time_shift_utils.get_flow_match_loss_weight(
sigma: torch.Tensor,
shift: float = 3.0
) -> torch.Tensor

Compute loss weights for flow matching based on sigma values.

Higher sigma (more noise) typically gets higher weight.

Parameters:

sigma
torch.Tensor

Floating-point tensor of arbitrary shape containing sigma values in the range [0, 1].

shift
floatDefaults to 3.0

weight scaling factor

Returns: torch.Tensor

New tensor of loss weights with the same shape, dtype, and device as sigma. The result does not alias

nemo_automodel.components.flow_matching.time_shift_utils.time_shift(
t: torch.Tensor,
image_seq_len: int,
shift_type: typing.Literal['linear', 'sqrt', 'constant'] | str = 'constant',
base_shift: float = 0.5,
max_shift: float = 1.15,
constant: float = 3.0
) -> torch.Tensor

Convert timesteps to sigmas with sequence-length-aware shifting.

Parameters:

t
torch.Tensor

Tensor of arbitrary shape containing timesteps in the range [0, 1].

image_seq_len
int

number of tokens (frames * height * width / patch_size^2)

shift_type
Literal['linear', 'sqrt', 'constant'] | strDefaults to 'constant'

“linear”, “sqrt”, or “constant”. Any other value returns t unchanged.

base_shift
floatDefaults to 0.5

base shift for linear mode

max_shift
floatDefaults to 1.15

max shift for linear mode

constant
floatDefaults to 3.0

shift value for constant mode (default 3.0 matches Pika)

Returns: torch.Tensor

Tensor with the same shape as t containing sigma values. Shifted floating-point results preserve the