| 1 | from dataclasses import dataclass |
| 2 | |
| 3 | import torch |
| 4 | |
| 5 | |
| 6 | @dataclass(frozen=True) |
| 7 | class Modality: |
| 8 | """ |
| 9 | Input data for a single modality (video or audio) in the transformer. |
| 10 | Bundles the latent tokens, timestep embeddings, positional information, |
| 11 | and text conditioning context for processing by the diffusion transformer. |
| 12 | Attributes: |
| 13 | latent: Patchified latent tokens, shape ``(B, T, D)`` where *B* is |
| 14 | the batch size, *T* is the total number of tokens (noisy + |
| 15 | conditioning), and *D* is the input dimension. |
| 16 | timesteps: Per-token timestep embeddings, shape ``(B, T)``. |
| 17 | positions: Positional coordinates, shape ``(B, 3, T)`` for video |
| 18 | (time, height, width) or ``(B, 1, T)`` for audio. |
| 19 | context: Text conditioning embeddings from the prompt encoder. |
| 20 | enabled: Whether this modality is active in the current forward pass. |
| 21 | context_mask: Optional mask for the text context tokens. |
| 22 | attention_mask: Optional 2-D self-attention mask, shape ``(B, T, T)``. |
| 23 | Values in ``[0, 1]`` where ``1`` = full attention and ``0`` = no |
| 24 | attention. ``None`` means unrestricted (full) attention between |
| 25 | all tokens. Built incrementally by conditioning items; see |
| 26 | :class:`~ltx_core.conditioning.types.attention_strength_wrapper.ConditioningItemAttentionStrengthWrapper`. |
| 27 | late_attention_mask: Optional alternate self-attention mask used in |
| 28 | later transformer layers. |
| 29 | cross_kv_mask: Optional cross-attention key/value mask. Can be |
| 30 | ``(B, T)`` or pairwise ``(B, Q, K)``. |
| 31 | cross_query_mask: Optional cross-attention query/output mask, |
| 32 | shape ``(B, T)``. |
| 33 | late_cross_kv_mask: Optional alternate cross KV mask for later layers. |
| 34 | late_cross_query_mask: Optional alternate cross query/output mask for |
| 35 | later layers. |
| 36 | v2a_grad_scale: Backward-only scale applied to the video-to-audio |
| 37 | cross-attention update. |
| 38 | """ |
| 39 | |
| 40 | latent: ( |
| 41 | torch.Tensor |
| 42 | ) # Shape: (B, T, D) where B is the batch size, T is the number of tokens, and D is input dimension |
| 43 | sigma: torch.Tensor # Shape: (B,). Current sigma value, used for cross-attention timestep calculation. |
| 44 | timesteps: torch.Tensor # Shape: (B, T) where T is the number of timesteps |
| 45 | positions: ( |
| 46 | torch.Tensor |
| 47 | ) # Shape: (B, 3, T) for video, where 3 is the number of dimensions and T is the number of tokens |
| 48 | context: torch.Tensor |
| 49 | enabled: bool = True |
| 50 | context_mask: torch.Tensor | None = None |
| 51 | attention_mask: torch.Tensor | None = None |
| 52 | late_attention_mask: torch.Tensor | None = None |
| 53 | cross_kv_mask: torch.Tensor | None = None |
| 54 | cross_query_mask: torch.Tensor | None = None |
| 55 | late_cross_kv_mask: torch.Tensor | None = None |
| 56 | late_cross_query_mask: torch.Tensor | None = None |
| 57 | v2a_grad_scale: float = 1.0 |
| 58 |