utils

Small tensor helpers shared across the fastgen subpackage.

Functions

classifier_free_guidance

Combine conditional and unconditional predictions via classifier-free guidance.

expand_like

Pad x with trailing singleton dims until it has the same ndim as target.

classifier_free_guidance(cond_pred, uncond_pred, guidance_scale)

Combine conditional and unconditional predictions via classifier-free guidance.

Uses the DMD2 convention cond + (scale - 1) * (cond - uncond), which is mathematically equivalent to the standard CFG formula uncond + scale * (cond - uncond).

Parameters:
  • cond_pred (Tensor)

  • uncond_pred (Tensor)

  • guidance_scale (float)

Return type:

Tensor

expand_like(x, target)

Pad x with trailing singleton dims until it has the same ndim as target.

Used to broadcast per-sample scalars like alpha_t / sigma_t across the spatial / temporal axes of a video latent.

Example:

x = torch.ones(5)  # shape (5,)
target = torch.ones(5, 4, 16, 16)
expand_like(x, target).shape  # (5, 1, 1, 1)
Parameters:
  • x (Tensor)

  • target (Tensor)

Return type:

Tensor