---
title: cosmos
---

<div class="python-reference">
<a id="fastvideo.models.dits.cosmos-classes"></a>
<a id="fastvideo.models.dits.cosmos-functions"></a>
<a id="fastvideo.models.dits.cosmos"></a>

<h2 id="fastvideo.models.dits.cosmos.CosmosVideoConfig"><code>fastvideo.models.dits.cosmos.CosmosVideoConfig</code></h2>
<pre class="doc-signature"><code>CosmosVideoConfig(arch_config: DiTArchConfig = CosmosArchConfig(), prefix: str = &#x27;Cosmos&#x27;, quant_config: QuantizationConfig | None = None, *, _resolved_attention_backend: AttentionBackendEnum | None = None)</code></pre>


<h2 id="fastvideo.models.dits.cosmos.RMSNorm"><code>fastvideo.models.dits.cosmos.RMSNorm</code></h2>
<pre class="doc-signature"><code>RMSNorm(hidden_size: int, eps: float = 1e-06, dtype: torch.dtype = torch.float32, var_hidden_size: int | None = None, has_weight: bool = True)</code></pre>
<p>Root mean square normalization.</p>
<p>Computes x -&gt; w * x / sqrt(E[x^2] + eps) where w is the learned weight.
Refer to https://arxiv.org/abs/1910.07467</p>

<h3 id="fastvideo.models.dits.cosmos.RMSNorm.forward_native"><code>fastvideo.models.dits.cosmos.RMSNorm.forward_native</code></h3>
<pre class="doc-signature"><code>forward_native(x: torch.Tensor, residual: torch.Tensor | None = None) -&gt; torch.Tensor | tuple&#91;torch.Tensor, torch.Tensor&#93;</code></pre>
<p>PyTorch-native implementation equivalent to forward().</p>
<h3 id="fastvideo.models.dits.cosmos.RMSNorm.extra_repr"><code>fastvideo.models.dits.cosmos.RMSNorm.extra_repr</code></h3>
<pre class="doc-signature"><code>extra_repr() -&gt; str</code></pre>

<h2 id="fastvideo.models.dits.cosmos.MLP"><code>fastvideo.models.dits.cosmos.MLP</code></h2>
<pre class="doc-signature"><code>MLP(input_dim: int, mlp_hidden_dim: int, output_dim: int | None = None, bias: bool = True, act_type: str = &#x27;gelu_pytorch_tanh&#x27;, dtype: torch.dtype | None = None, prefix: str = &#x27;&#x27;, quant_config: QuantizationConfig | None = None)</code></pre>
<p>MLP for DiT blocks, NO gated linear units</p>

<h3 id="fastvideo.models.dits.cosmos.MLP.forward"><code>fastvideo.models.dits.cosmos.MLP.forward</code></h3>
<pre class="doc-signature"><code>forward(x: torch.Tensor) -&gt; torch.Tensor</code></pre>

<h2 id="fastvideo.models.dits.cosmos.apply_rotary_emb"><code>fastvideo.models.dits.cosmos.apply_rotary_emb</code></h2>
<pre class="doc-signature"><code>apply_rotary_emb(x: torch.Tensor, freqs_cis: torch.Tensor | tuple&#91;torch.Tensor, torch.Tensor&#93;, use_real: bool = True, use_real_unbind_dim: int = -1, sequence_dim: int = 2) -&gt; torch.Tensor</code></pre>
<p>Apply rotary embeddings to input tensors using the given frequency tensor. This function applies rotary embeddings
to the given query or key 'x' tensors using the provided frequency tensor 'freqs_cis'. The input tensors are
reshaped as complex numbers, and the frequency tensor is reshaped for broadcasting compatibility. The resulting
tensors contain rotary embeddings and are returned as real tensors.
Args:
    x (<code>torch.Tensor</code>):
        Query or key tensor to apply rotary embeddings. [B, H, S, D] if sequence_dim=2 else [B, S, H, D].
    freqs_cis (<code>Tuple[torch.Tensor]</code>): Precomputed frequency tensor for complex exponentials. ([S, D], [S, D],)
    sequence_dim: 1 = sequence at dim 1 (cos [1,S,1,D], x [B,S,H,D]); 2 = sequence at dim 2 (cos [1,1,S,D], x [B,H,S,D]).
Returns:
    Tuple[torch.Tensor, torch.Tensor]: Tuple of modified query tensor and key tensor with rotary embeddings.</p>
<h2 id="fastvideo.models.dits.cosmos.Timesteps"><code>fastvideo.models.dits.cosmos.Timesteps</code></h2>
<pre class="doc-signature"><code>Timesteps(num_channels: int, flip_sin_to_cos: bool, downscale_freq_shift: float, scale: int = 1)</code></pre>


<h3 id="fastvideo.models.dits.cosmos.Timesteps.forward"><code>fastvideo.models.dits.cosmos.Timesteps.forward</code></h3>
<pre class="doc-signature"><code>forward(timesteps: torch.Tensor) -&gt; torch.Tensor</code></pre>

<h2 id="fastvideo.models.dits.cosmos.BaseDiT"><code>fastvideo.models.dits.cosmos.BaseDiT</code></h2>
<pre class="doc-signature"><code>BaseDiT(config: DiTConfig, hf_config: dict&#91;str, Any&#93;, **kwargs)</code></pre>


<h3 id="fastvideo.models.dits.cosmos.BaseDiT.forward"><code>fastvideo.models.dits.cosmos.BaseDiT.forward</code></h3>
<pre class="doc-signature"><code>forward(hidden_states: torch.Tensor, encoder_hidden_states: torch.Tensor | list&#91;torch.Tensor&#93;, timestep: torch.LongTensor, encoder_hidden_states_image: torch.Tensor | list&#91;torch.Tensor&#93; | None = None, guidance = None, **kwargs) -&gt; torch.Tensor</code></pre>

<h3 id="fastvideo.models.dits.cosmos.BaseDiT.preprocess_loaded_weight"><code>fastvideo.models.dits.cosmos.BaseDiT.preprocess_loaded_weight</code></h3>
<pre class="doc-signature"><code>preprocess_loaded_weight(name: str, tensor: torch.Tensor) -&gt; torch.Tensor</code></pre>
<p>Adapt a checkpoint tensor before sharding; default models are unchanged.</p>
<h3 id="fastvideo.models.dits.cosmos.BaseDiT.inference_config_overrides"><code>fastvideo.models.dits.cosmos.BaseDiT.inference_config_overrides</code></h3>
<pre class="doc-signature"><code>inference_config_overrides() -&gt; dict&#91;str, Any&#93;</code></pre>
<p>Extra <code>config.json</code> keys a deployable checkpoint of this model needs.</p>
<h3 id="fastvideo.models.dits.cosmos.BaseDiT.inference_export_expected_shape"><code>fastvideo.models.dits.cosmos.BaseDiT.inference_export_expected_shape</code></h3>
<pre class="doc-signature"><code>inference_export_expected_shape(output_key: str, base_shape: tuple&#91;int, ...&#93;) -&gt; tuple&#91;int, ...&#93;</code></pre>
<p>The exported shape of <em>output_key</em> given the base checkpoint's shape.</p>
<h2 id="fastvideo.models.dits.cosmos.CosmosPatchEmbed"><code>fastvideo.models.dits.cosmos.CosmosPatchEmbed</code></h2>
<pre class="doc-signature"><code>CosmosPatchEmbed(in_channels: int, out_channels: int, patch_size: tuple&#91;int, int, int&#93;, bias: bool = True)</code></pre>


<details><summary>Source</summary><pre><code>class CosmosPatchEmbed(nn.Module):

    def __init__(self,
                 in_channels: int,
                 out_channels: int,
                 patch_size: tuple&#91;int, int, int&#93;,
                 bias: bool = True) -&gt; None:
        super().__init__()
        self.patch_size = patch_size

        self.proj = nn.Linear(in_channels * patch_size&#91;0&#93; * patch_size&#91;1&#93; * patch_size&#91;2&#93;, out_channels, bias=bias)

    def forward(self, hidden_states: torch.Tensor) -&gt; torch.Tensor:
        batch_size, num_channels, num_frames, height, width = hidden_states.shape
        p_t, p_h, p_w = self.patch_size
        hidden_states = hidden_states.reshape(batch_size, num_channels, num_frames // p_t, p_t, height // p_h, p_h,
                                              width // p_w, p_w)
        hidden_states = hidden_states.permute(0, 2, 4, 6, 1, 3, 5, 7).flatten(4, 7)
        hidden_states = self.proj(hidden_states)
        return hidden_states</code></pre></details>
<h3 id="fastvideo.models.dits.cosmos.CosmosPatchEmbed.forward"><code>fastvideo.models.dits.cosmos.CosmosPatchEmbed.forward</code></h3>
<pre class="doc-signature"><code>forward(hidden_states: torch.Tensor) -&gt; torch.Tensor</code></pre>

<details><summary>Source</summary><pre><code>def forward(self, hidden_states: torch.Tensor) -&gt; torch.Tensor:
    batch_size, num_channels, num_frames, height, width = hidden_states.shape
    p_t, p_h, p_w = self.patch_size
    hidden_states = hidden_states.reshape(batch_size, num_channels, num_frames // p_t, p_t, height // p_h, p_h,
                                          width // p_w, p_w)
    hidden_states = hidden_states.permute(0, 2, 4, 6, 1, 3, 5, 7).flatten(4, 7)
    hidden_states = self.proj(hidden_states)
    return hidden_states</code></pre></details>
<h2 id="fastvideo.models.dits.cosmos.CosmosTimestepEmbedding"><code>fastvideo.models.dits.cosmos.CosmosTimestepEmbedding</code></h2>
<pre class="doc-signature"><code>CosmosTimestepEmbedding(in_features: int, out_features: int)</code></pre>


<details><summary>Source</summary><pre><code>class CosmosTimestepEmbedding(nn.Module):

    def __init__(self, in_features: int, out_features: int) -&gt; None:
        super().__init__()
        self.linear_1 = nn.Linear(in_features, out_features, bias=False)
        self.activation = nn.SiLU()
        self.linear_2 = nn.Linear(out_features, 3 * out_features, bias=False)

    def forward(self, timesteps: torch.Tensor) -&gt; torch.Tensor:
        emb = self.linear_1(timesteps)
        emb = self.activation(emb)
        emb = self.linear_2(emb)
        return emb</code></pre></details>
<h3 id="fastvideo.models.dits.cosmos.CosmosTimestepEmbedding.forward"><code>fastvideo.models.dits.cosmos.CosmosTimestepEmbedding.forward</code></h3>
<pre class="doc-signature"><code>forward(timesteps: torch.Tensor) -&gt; torch.Tensor</code></pre>

<details><summary>Source</summary><pre><code>def forward(self, timesteps: torch.Tensor) -&gt; torch.Tensor:
    emb = self.linear_1(timesteps)
    emb = self.activation(emb)
    emb = self.linear_2(emb)
    return emb</code></pre></details>
<h2 id="fastvideo.models.dits.cosmos.CosmosEmbedding"><code>fastvideo.models.dits.cosmos.CosmosEmbedding</code></h2>
<pre class="doc-signature"><code>CosmosEmbedding(embedding_dim: int, condition_dim: int)</code></pre>


<details><summary>Source</summary><pre><code>class CosmosEmbedding(nn.Module):

    def __init__(self, embedding_dim: int, condition_dim: int) -&gt; None:
        super().__init__()

        self.time_proj = Timesteps(embedding_dim, flip_sin_to_cos=True, downscale_freq_shift=0.0)
        self.t_embedder = CosmosTimestepEmbedding(embedding_dim, condition_dim)
        self.norm = RMSNorm(embedding_dim, eps=1e-6)

    def forward(self, hidden_states: torch.Tensor, timestep: torch.LongTensor) -&gt; torch.Tensor:
        timesteps_proj = self.time_proj(timestep).type_as(hidden_states)
        temb = self.t_embedder(timesteps_proj)
        embedded_timestep = self.norm(timesteps_proj)
        return temb, embedded_timestep</code></pre></details>
<h3 id="fastvideo.models.dits.cosmos.CosmosEmbedding.forward"><code>fastvideo.models.dits.cosmos.CosmosEmbedding.forward</code></h3>
<pre class="doc-signature"><code>forward(hidden_states: torch.Tensor, timestep: torch.LongTensor) -&gt; torch.Tensor</code></pre>

<details><summary>Source</summary><pre><code>def forward(self, hidden_states: torch.Tensor, timestep: torch.LongTensor) -&gt; torch.Tensor:
    timesteps_proj = self.time_proj(timestep).type_as(hidden_states)
    temb = self.t_embedder(timesteps_proj)
    embedded_timestep = self.norm(timesteps_proj)
    return temb, embedded_timestep</code></pre></details>
<h2 id="fastvideo.models.dits.cosmos.CosmosAdaLayerNorm"><code>fastvideo.models.dits.cosmos.CosmosAdaLayerNorm</code></h2>
<pre class="doc-signature"><code>CosmosAdaLayerNorm(in_features: int, hidden_features: int)</code></pre>


<details><summary>Source</summary><pre><code>class CosmosAdaLayerNorm(nn.Module):

    def __init__(self, in_features: int, hidden_features: int) -&gt; None:
        super().__init__()
        self.embedding_dim = in_features

        self.activation = nn.SiLU()
        self.norm = nn.LayerNorm(in_features, elementwise_affine=False, eps=1e-6)
        self.linear_1 = nn.Linear(in_features, hidden_features, bias=False)
        self.linear_2 = nn.Linear(hidden_features, 2 * in_features, bias=False)

    def forward(self,
                hidden_states: torch.Tensor,
                embedded_timestep: torch.Tensor,
                temb: torch.Tensor | None = None) -&gt; torch.Tensor:
        embedded_timestep = self.activation(embedded_timestep)
        embedded_timestep = self.linear_1(embedded_timestep)
        embedded_timestep = self.linear_2(embedded_timestep)

        if temb is not None:
            embedded_timestep = embedded_timestep + temb&#91;..., :2 * self.embedding_dim&#93;

        shift, scale = embedded_timestep.chunk(2, dim=-1)
        with torch.autocast(device_type=hidden_states.device.type, enabled=False):
            hidden_states = self.norm(hidden_states)

        if embedded_timestep.ndim == 2:
            shift, scale = (x.unsqueeze(1) for x in (shift, scale))

        hidden_states = hidden_states * (1 + scale) + shift
        return hidden_states</code></pre></details>
<h3 id="fastvideo.models.dits.cosmos.CosmosAdaLayerNorm.forward"><code>fastvideo.models.dits.cosmos.CosmosAdaLayerNorm.forward</code></h3>
<pre class="doc-signature"><code>forward(hidden_states: torch.Tensor, embedded_timestep: torch.Tensor, temb: torch.Tensor | None = None) -&gt; torch.Tensor</code></pre>

<details><summary>Source</summary><pre><code>def forward(self,
            hidden_states: torch.Tensor,
            embedded_timestep: torch.Tensor,
            temb: torch.Tensor | None = None) -&gt; torch.Tensor:
    embedded_timestep = self.activation(embedded_timestep)
    embedded_timestep = self.linear_1(embedded_timestep)
    embedded_timestep = self.linear_2(embedded_timestep)

    if temb is not None:
        embedded_timestep = embedded_timestep + temb&#91;..., :2 * self.embedding_dim&#93;

    shift, scale = embedded_timestep.chunk(2, dim=-1)
    with torch.autocast(device_type=hidden_states.device.type, enabled=False):
        hidden_states = self.norm(hidden_states)

    if embedded_timestep.ndim == 2:
        shift, scale = (x.unsqueeze(1) for x in (shift, scale))

    hidden_states = hidden_states * (1 + scale) + shift
    return hidden_states</code></pre></details>
<h2 id="fastvideo.models.dits.cosmos.CosmosAdaLayerNormZero"><code>fastvideo.models.dits.cosmos.CosmosAdaLayerNormZero</code></h2>
<pre class="doc-signature"><code>CosmosAdaLayerNormZero(in_features: int, hidden_features: int | None = None)</code></pre>


<details><summary>Source</summary><pre><code>class CosmosAdaLayerNormZero(nn.Module):

    def __init__(self, in_features: int, hidden_features: int | None = None) -&gt; None:
        super().__init__()

        self.norm = nn.LayerNorm(in_features, elementwise_affine=False, eps=1e-6)
        self.activation = nn.SiLU()

        if hidden_features is None:
            self.linear_1 = nn.Identity()
        else:
            self.linear_1 = nn.Linear(in_features, hidden_features, bias=False)

        self.linear_2 = nn.Linear(hidden_features, 3 * in_features, bias=False)

    def forward(
        self,
        hidden_states: torch.Tensor,
        embedded_timestep: torch.Tensor,
        temb: torch.Tensor | None = None,
    ) -&gt; torch.Tensor:
        embedded_timestep = self.activation(embedded_timestep)
        embedded_timestep = self.linear_1(embedded_timestep)
        embedded_timestep = self.linear_2(embedded_timestep)

        if temb is not None:
            embedded_timestep = embedded_timestep + temb

        shift, scale, gate = embedded_timestep.chunk(3, dim=-1)

        with torch.autocast(device_type=hidden_states.device.type, enabled=False):
            hidden_states = self.norm(hidden_states)

        if embedded_timestep.ndim == 2:
            shift, scale, gate = (x.unsqueeze(1) for x in (shift, scale, gate))

        hidden_states = hidden_states * (1 + scale) + shift
        return hidden_states, gate</code></pre></details>
<h3 id="fastvideo.models.dits.cosmos.CosmosAdaLayerNormZero.forward"><code>fastvideo.models.dits.cosmos.CosmosAdaLayerNormZero.forward</code></h3>
<pre class="doc-signature"><code>forward(hidden_states: torch.Tensor, embedded_timestep: torch.Tensor, temb: torch.Tensor | None = None) -&gt; torch.Tensor</code></pre>

<details><summary>Source</summary><pre><code>def forward(
    self,
    hidden_states: torch.Tensor,
    embedded_timestep: torch.Tensor,
    temb: torch.Tensor | None = None,
) -&gt; torch.Tensor:
    embedded_timestep = self.activation(embedded_timestep)
    embedded_timestep = self.linear_1(embedded_timestep)
    embedded_timestep = self.linear_2(embedded_timestep)

    if temb is not None:
        embedded_timestep = embedded_timestep + temb

    shift, scale, gate = embedded_timestep.chunk(3, dim=-1)

    with torch.autocast(device_type=hidden_states.device.type, enabled=False):
        hidden_states = self.norm(hidden_states)

    if embedded_timestep.ndim == 2:
        shift, scale, gate = (x.unsqueeze(1) for x in (shift, scale, gate))

    hidden_states = hidden_states * (1 + scale) + shift
    return hidden_states, gate</code></pre></details>
<h2 id="fastvideo.models.dits.cosmos.CosmosSelfAttention"><code>fastvideo.models.dits.cosmos.CosmosSelfAttention</code></h2>
<pre class="doc-signature"><code>CosmosSelfAttention(dim: int, num_heads: int, qk_norm = True, eps = 1e-06, prefix: str = &#x27;&#x27;)</code></pre>


<details><summary>Source</summary><pre><code>class CosmosSelfAttention(nn.Module):

    def __init__(self, dim: int, num_heads: int, qk_norm=True, eps=1e-6, prefix: str = &quot;&quot;) -&gt; None:
        assert dim % num_heads == 0
        super().__init__()
        self.dim = dim
        self.num_heads = num_heads
        self.head_dim = dim // num_heads
        self.qk_norm = qk_norm
        self.eps = eps

        # layers - use standard PyTorch layers when using torch backend
        self.to_q = nn.Linear(dim, dim, bias=False)
        self.to_k = nn.Linear(dim, dim, bias=False)
        self.to_v = nn.Linear(dim, dim, bias=False)
        self.to_out = nn.Linear(dim, dim, bias=False)
        self.dropout = nn.Dropout(0.0)

        self.norm_q = RMSNorm(self.head_dim, eps=eps) if qk_norm else nn.Identity()
        self.norm_k = RMSNorm(self.head_dim, eps=eps) if qk_norm else nn.Identity()

    def forward(self,
                hidden_states: torch.Tensor,
                encoder_hidden_states: torch.Tensor | None = None,
                attention_mask: torch.Tensor | None = None,
                image_rotary_emb: torch.Tensor | None = None) -&gt; torch.Tensor:

        if encoder_hidden_states is None:
            encoder_hidden_states = hidden_states

        # Get QKV
        query = self.to_q(hidden_states)
        key = self.to_k(encoder_hidden_states)
        value = self.to_v(encoder_hidden_states)

        # Reshape for multi-head attention
        query = query.unflatten(2, (self.num_heads, -1)).transpose(1, 2)
        key = key.unflatten(2, (self.num_heads, -1)).transpose(1, 2)
        value = value.unflatten(2, (self.num_heads, -1)).transpose(1, 2)

        # Apply normalization
        if self.norm_q is not None:
            query = self.norm_q(query)
        if self.norm_k is not None:
            key = self.norm_k(key)

        # Apply RoPE if provided
        if image_rotary_emb is not None:
            query = apply_rotary_emb(query, image_rotary_emb, use_real=True, use_real_unbind_dim=-2)
            key = apply_rotary_emb(key, image_rotary_emb, use_real=True, use_real_unbind_dim=-2)

        # Prepare for GQA (Grouped Query Attention)
        if torch.onnx.is_in_onnx_export():
            query_idx = torch.tensor(query.size(3), device=query.device)
            key_idx = torch.tensor(key.size(3), device=key.device)
            value_idx = torch.tensor(value.size(3), device=value.device)
        else:
            query_idx = query.size(3)
            key_idx = key.size(3)
            value_idx = value.size(3)
        key = key.repeat_interleave(query_idx // key_idx, dim=3)
        value = value.repeat_interleave(query_idx // value_idx, dim=3)

        # Attention computation
        attn_output = torch.nn.functional.scaled_dot_product_attention(query,
                                                                       key,
                                                                       value,
                                                                       attn_mask=attention_mask,
                                                                       dropout_p=0.0,
                                                                       is_causal=False)
        attn_output = attn_output.transpose(1, 2).flatten(2, 3).type_as(query)

        # Output projection
        attn_output = self.to_out(attn_output)
        attn_output = self.dropout(attn_output)

        return attn_output</code></pre></details>
<h3 id="fastvideo.models.dits.cosmos.CosmosSelfAttention.forward"><code>fastvideo.models.dits.cosmos.CosmosSelfAttention.forward</code></h3>
<pre class="doc-signature"><code>forward(hidden_states: torch.Tensor, encoder_hidden_states: torch.Tensor | None = None, attention_mask: torch.Tensor | None = None, image_rotary_emb: torch.Tensor | None = None) -&gt; torch.Tensor</code></pre>

<details><summary>Source</summary><pre><code>def forward(self,
            hidden_states: torch.Tensor,
            encoder_hidden_states: torch.Tensor | None = None,
            attention_mask: torch.Tensor | None = None,
            image_rotary_emb: torch.Tensor | None = None) -&gt; torch.Tensor:

    if encoder_hidden_states is None:
        encoder_hidden_states = hidden_states

    # Get QKV
    query = self.to_q(hidden_states)
    key = self.to_k(encoder_hidden_states)
    value = self.to_v(encoder_hidden_states)

    # Reshape for multi-head attention
    query = query.unflatten(2, (self.num_heads, -1)).transpose(1, 2)
    key = key.unflatten(2, (self.num_heads, -1)).transpose(1, 2)
    value = value.unflatten(2, (self.num_heads, -1)).transpose(1, 2)

    # Apply normalization
    if self.norm_q is not None:
        query = self.norm_q(query)
    if self.norm_k is not None:
        key = self.norm_k(key)

    # Apply RoPE if provided
    if image_rotary_emb is not None:
        query = apply_rotary_emb(query, image_rotary_emb, use_real=True, use_real_unbind_dim=-2)
        key = apply_rotary_emb(key, image_rotary_emb, use_real=True, use_real_unbind_dim=-2)

    # Prepare for GQA (Grouped Query Attention)
    if torch.onnx.is_in_onnx_export():
        query_idx = torch.tensor(query.size(3), device=query.device)
        key_idx = torch.tensor(key.size(3), device=key.device)
        value_idx = torch.tensor(value.size(3), device=value.device)
    else:
        query_idx = query.size(3)
        key_idx = key.size(3)
        value_idx = value.size(3)
    key = key.repeat_interleave(query_idx // key_idx, dim=3)
    value = value.repeat_interleave(query_idx // value_idx, dim=3)

    # Attention computation
    attn_output = torch.nn.functional.scaled_dot_product_attention(query,
                                                                   key,
                                                                   value,
                                                                   attn_mask=attention_mask,
                                                                   dropout_p=0.0,
                                                                   is_causal=False)
    attn_output = attn_output.transpose(1, 2).flatten(2, 3).type_as(query)

    # Output projection
    attn_output = self.to_out(attn_output)
    attn_output = self.dropout(attn_output)

    return attn_output</code></pre></details>
<h2 id="fastvideo.models.dits.cosmos.CosmosCrossAttention"><code>fastvideo.models.dits.cosmos.CosmosCrossAttention</code></h2>
<pre class="doc-signature"><code>CosmosCrossAttention(dim: int, cross_attention_dim: int, num_heads: int, qk_norm = True, eps = 1e-06, prefix: str = &#x27;&#x27;)</code></pre>


<details><summary>Source</summary><pre><code>class CosmosCrossAttention(nn.Module):

    def __init__(self,
                 dim: int,
                 cross_attention_dim: int,
                 num_heads: int,
                 qk_norm=True,
                 eps=1e-6,
                 prefix: str = &quot;&quot;) -&gt; None:
        assert dim % num_heads == 0
        super().__init__()
        self.dim = dim
        self.cross_attention_dim = cross_attention_dim
        self.num_heads = num_heads
        self.head_dim = dim // num_heads
        self.qk_norm = qk_norm
        self.eps = eps

        self.to_q = nn.Linear(dim, dim, bias=False)
        self.to_k = nn.Linear(cross_attention_dim, dim, bias=False)
        self.to_v = nn.Linear(cross_attention_dim, dim, bias=False)
        self.to_out = nn.Linear(dim, dim, bias=False)
        self.dropout = nn.Dropout(0.0)

        self.norm_q = RMSNorm(self.head_dim, eps=eps) if qk_norm else nn.Identity()
        self.norm_k = RMSNorm(self.head_dim, eps=eps) if qk_norm else nn.Identity()

    def forward(self,
                hidden_states: torch.Tensor,
                encoder_hidden_states: torch.Tensor,
                attention_mask: torch.Tensor | None = None) -&gt; torch.Tensor:

        # Get QKV
        query = self.to_q(hidden_states)
        key = self.to_k(encoder_hidden_states)
        value = self.to_v(encoder_hidden_states)

        # Reshape for multi-head attention
        query = query.unflatten(2, (self.num_heads, -1)).transpose(1, 2)
        key = key.unflatten(2, (self.num_heads, -1)).transpose(1, 2)
        value = value.unflatten(2, (self.num_heads, -1)).transpose(1, 2)

        # Apply normalization
        if self.norm_q is not None:
            query = self.norm_q(query)
        if self.norm_k is not None:
            key = self.norm_k(key)

        # Prepare for GQA (Grouped Query Attention)
        if torch.onnx.is_in_onnx_export():
            query_idx = torch.tensor(query.size(3), device=query.device)
            key_idx = torch.tensor(key.size(3), device=key.device)
            value_idx = torch.tensor(value.size(3), device=value.device)
        else:
            query_idx = query.size(3)
            key_idx = key.size(3)
            value_idx = value.size(3)
        key = key.repeat_interleave(query_idx // key_idx, dim=3)
        value = value.repeat_interleave(query_idx // value_idx, dim=3)

        # Attention computation
        attn_output = torch.nn.functional.scaled_dot_product_attention(query,
                                                                       key,
                                                                       value,
                                                                       attn_mask=attention_mask,
                                                                       dropout_p=0.0,
                                                                       is_causal=False)
        attn_output = attn_output.transpose(1, 2).flatten(2, 3).type_as(query)

        # Output projection
        attn_output = self.to_out(attn_output)
        attn_output = self.dropout(attn_output)

        return attn_output</code></pre></details>
<h3 id="fastvideo.models.dits.cosmos.CosmosCrossAttention.forward"><code>fastvideo.models.dits.cosmos.CosmosCrossAttention.forward</code></h3>
<pre class="doc-signature"><code>forward(hidden_states: torch.Tensor, encoder_hidden_states: torch.Tensor, attention_mask: torch.Tensor | None = None) -&gt; torch.Tensor</code></pre>

<details><summary>Source</summary><pre><code>def forward(self,
            hidden_states: torch.Tensor,
            encoder_hidden_states: torch.Tensor,
            attention_mask: torch.Tensor | None = None) -&gt; torch.Tensor:

    # Get QKV
    query = self.to_q(hidden_states)
    key = self.to_k(encoder_hidden_states)
    value = self.to_v(encoder_hidden_states)

    # Reshape for multi-head attention
    query = query.unflatten(2, (self.num_heads, -1)).transpose(1, 2)
    key = key.unflatten(2, (self.num_heads, -1)).transpose(1, 2)
    value = value.unflatten(2, (self.num_heads, -1)).transpose(1, 2)

    # Apply normalization
    if self.norm_q is not None:
        query = self.norm_q(query)
    if self.norm_k is not None:
        key = self.norm_k(key)

    # Prepare for GQA (Grouped Query Attention)
    if torch.onnx.is_in_onnx_export():
        query_idx = torch.tensor(query.size(3), device=query.device)
        key_idx = torch.tensor(key.size(3), device=key.device)
        value_idx = torch.tensor(value.size(3), device=value.device)
    else:
        query_idx = query.size(3)
        key_idx = key.size(3)
        value_idx = value.size(3)
    key = key.repeat_interleave(query_idx // key_idx, dim=3)
    value = value.repeat_interleave(query_idx // value_idx, dim=3)

    # Attention computation
    attn_output = torch.nn.functional.scaled_dot_product_attention(query,
                                                                   key,
                                                                   value,
                                                                   attn_mask=attention_mask,
                                                                   dropout_p=0.0,
                                                                   is_causal=False)
    attn_output = attn_output.transpose(1, 2).flatten(2, 3).type_as(query)

    # Output projection
    attn_output = self.to_out(attn_output)
    attn_output = self.dropout(attn_output)

    return attn_output</code></pre></details>
<h2 id="fastvideo.models.dits.cosmos.CosmosTransformerBlock"><code>fastvideo.models.dits.cosmos.CosmosTransformerBlock</code></h2>
<pre class="doc-signature"><code>CosmosTransformerBlock(num_attention_heads: int, attention_head_dim: int, cross_attention_dim: int, mlp_ratio: float = 4.0, adaln_lora_dim: int = 256, qk_norm: str = &#x27;rms_norm&#x27;, out_bias: bool = False, prefix: str = &#x27;&#x27;)</code></pre>


<details><summary>Source</summary><pre><code>class CosmosTransformerBlock(nn.Module):

    def __init__(
        self,
        num_attention_heads: int,
        attention_head_dim: int,
        cross_attention_dim: int,
        mlp_ratio: float = 4.0,
        adaln_lora_dim: int = 256,
        qk_norm: str = &quot;rms_norm&quot;,
        out_bias: bool = False,
        prefix: str = &quot;&quot;,
    ) -&gt; None:
        super().__init__()

        hidden_size = num_attention_heads * attention_head_dim

        self.norm1 = CosmosAdaLayerNormZero(in_features=hidden_size, hidden_features=adaln_lora_dim)
        self.attn1 = CosmosSelfAttention(dim=hidden_size,
                                         num_heads=num_attention_heads,
                                         qk_norm=(qk_norm == &quot;rms_norm&quot;),
                                         eps=1e-5,
                                         prefix=f&quot;&#123;prefix&#125;.attn1&quot;)

        self.norm2 = CosmosAdaLayerNormZero(in_features=hidden_size, hidden_features=adaln_lora_dim)
        self.attn2 = CosmosCrossAttention(dim=hidden_size,
                                          cross_attention_dim=cross_attention_dim,
                                          num_heads=num_attention_heads,
                                          qk_norm=(qk_norm == &quot;rms_norm&quot;),
                                          eps=1e-5,
                                          prefix=f&quot;&#123;prefix&#125;.attn2&quot;)

        self.norm3 = CosmosAdaLayerNormZero(in_features=hidden_size, hidden_features=adaln_lora_dim)
        self.ff = MLP(hidden_size, int(hidden_size * mlp_ratio), act_type=&quot;gelu&quot;, bias=False)

    def forward(
        self,
        hidden_states: torch.Tensor,
        encoder_hidden_states: torch.Tensor,
        embedded_timestep: torch.Tensor,
        temb: torch.Tensor | None = None,
        image_rotary_emb: torch.Tensor | None = None,
        extra_pos_emb: torch.Tensor | None = None,
        attention_mask: torch.Tensor | None = None,
    ) -&gt; torch.Tensor:
        if extra_pos_emb is not None:
            hidden_states = hidden_states + extra_pos_emb

        norm_hidden_states, gate = self.norm1(hidden_states, embedded_timestep, temb)

        attn_output = self.attn1(norm_hidden_states, image_rotary_emb=image_rotary_emb)
        hidden_states = hidden_states + gate * attn_output

        norm_hidden_states, gate = self.norm2(hidden_states, embedded_timestep, temb)
        attn_output = self.attn2(norm_hidden_states,
                                 encoder_hidden_states=encoder_hidden_states,
                                 attention_mask=attention_mask)

        hidden_states = hidden_states + gate * attn_output

        norm_hidden_states, gate = self.norm3(hidden_states, embedded_timestep, temb)
        ff_output = self.ff(norm_hidden_states)
        hidden_states = hidden_states + gate * ff_output

        return hidden_states</code></pre></details>
<h3 id="fastvideo.models.dits.cosmos.CosmosTransformerBlock.forward"><code>fastvideo.models.dits.cosmos.CosmosTransformerBlock.forward</code></h3>
<pre class="doc-signature"><code>forward(hidden_states: torch.Tensor, encoder_hidden_states: torch.Tensor, embedded_timestep: torch.Tensor, temb: torch.Tensor | None = None, image_rotary_emb: torch.Tensor | None = None, extra_pos_emb: torch.Tensor | None = None, attention_mask: torch.Tensor | None = None) -&gt; torch.Tensor</code></pre>

<details><summary>Source</summary><pre><code>def forward(
    self,
    hidden_states: torch.Tensor,
    encoder_hidden_states: torch.Tensor,
    embedded_timestep: torch.Tensor,
    temb: torch.Tensor | None = None,
    image_rotary_emb: torch.Tensor | None = None,
    extra_pos_emb: torch.Tensor | None = None,
    attention_mask: torch.Tensor | None = None,
) -&gt; torch.Tensor:
    if extra_pos_emb is not None:
        hidden_states = hidden_states + extra_pos_emb

    norm_hidden_states, gate = self.norm1(hidden_states, embedded_timestep, temb)

    attn_output = self.attn1(norm_hidden_states, image_rotary_emb=image_rotary_emb)
    hidden_states = hidden_states + gate * attn_output

    norm_hidden_states, gate = self.norm2(hidden_states, embedded_timestep, temb)
    attn_output = self.attn2(norm_hidden_states,
                             encoder_hidden_states=encoder_hidden_states,
                             attention_mask=attention_mask)

    hidden_states = hidden_states + gate * attn_output

    norm_hidden_states, gate = self.norm3(hidden_states, embedded_timestep, temb)
    ff_output = self.ff(norm_hidden_states)
    hidden_states = hidden_states + gate * ff_output

    return hidden_states</code></pre></details>
<h2 id="fastvideo.models.dits.cosmos.CosmosRotaryPosEmbed"><code>fastvideo.models.dits.cosmos.CosmosRotaryPosEmbed</code></h2>
<pre class="doc-signature"><code>CosmosRotaryPosEmbed(hidden_size: int, max_size: tuple&#91;int, int, int&#93; = (128, 240, 240), patch_size: tuple&#91;int, int, int&#93; = (1, 2, 2), base_fps: int = 24, rope_scale: tuple&#91;float, float, float&#93; = (2.0, 1.0, 1.0))</code></pre>


<details><summary>Source</summary><pre><code>class CosmosRotaryPosEmbed(nn.Module):

    def __init__(
            self,
            hidden_size: int,
            max_size: tuple&#91;int, int, int&#93; = (128, 240, 240),
            patch_size: tuple&#91;int, int, int&#93; = (1, 2, 2),
            base_fps: int = 24,
            rope_scale: tuple&#91;float, float, float&#93; = (2.0, 1.0, 1.0),
    ) -&gt; None:
        super().__init__()

        self.max_size = &#91;size // patch for size, patch in zip(max_size, patch_size, strict=False)&#93;
        self.patch_size = patch_size
        self.base_fps = base_fps

        self.dim_h = hidden_size // 6 * 2
        self.dim_w = hidden_size // 6 * 2
        self.dim_t = hidden_size - self.dim_h - self.dim_w

        self.h_ntk_factor = rope_scale&#91;1&#93;**(self.dim_h / (self.dim_h - 2))
        self.w_ntk_factor = rope_scale&#91;2&#93;**(self.dim_w / (self.dim_w - 2))
        self.t_ntk_factor = rope_scale&#91;0&#93;**(self.dim_t / (self.dim_t - 2))

    def forward(self, hidden_states: torch.Tensor, fps: int | None = None) -&gt; tuple&#91;torch.Tensor, torch.Tensor&#93;:
        fps = 16
        batch_size, num_channels, num_frames, height, width = hidden_states.shape
        pe_size = &#91;num_frames // self.patch_size&#91;0&#93;, height // self.patch_size&#91;1&#93;, width // self.patch_size&#91;2&#93;&#93;
        device = hidden_states.device

        h_theta = 10000.0 * self.h_ntk_factor
        w_theta = 10000.0 * self.w_ntk_factor
        t_theta = 10000.0 * self.t_ntk_factor

        seq = torch.arange(max(self.max_size), device=device, dtype=torch.float32)
        dim_h_range = (torch.arange(0, self.dim_h, 2, device=device, dtype=torch.float32)&#91;:(self.dim_h // 2)&#93; /
                       self.dim_h)
        dim_w_range = (torch.arange(0, self.dim_w, 2, device=device, dtype=torch.float32)&#91;:(self.dim_w // 2)&#93; /
                       self.dim_w)
        dim_t_range = (torch.arange(0, self.dim_t, 2, device=device, dtype=torch.float32)&#91;:(self.dim_t // 2)&#93; /
                       self.dim_t)

        h_spatial_freqs = 1.0 / (h_theta**dim_h_range)
        w_spatial_freqs = 1.0 / (w_theta**dim_w_range)
        temporal_freqs = 1.0 / (t_theta**dim_t_range)

        emb_h = torch.outer(seq&#91;:pe_size&#91;1&#93;&#93;, h_spatial_freqs)&#91;None, :, None, :&#93;.repeat(pe_size&#91;0&#93;, 1, pe_size&#91;2&#93;, 1)
        emb_w = torch.outer(seq&#91;:pe_size&#91;2&#93;&#93;, w_spatial_freqs)&#91;None, None, :, :&#93;.repeat(pe_size&#91;0&#93;, pe_size&#91;1&#93;, 1, 1)

        if fps is None:
            emb_t = torch.outer(seq&#91;:pe_size&#91;0&#93;&#93;, temporal_freqs)
        else:
            temporal_scale = seq&#91;:pe_size&#91;0&#93;&#93; / fps * self.base_fps
            emb_t = torch.outer(temporal_scale, temporal_freqs)

        emb_t = emb_t&#91;:, None, None, :&#93;.repeat(1, pe_size&#91;1&#93;, pe_size&#91;2&#93;, 1)
        freqs = torch.cat(&#91;emb_t, emb_h, emb_w&#93; * 2, dim=-1).flatten(0, 2).float()
        cos = torch.cos(freqs)
        sin = torch.sin(freqs)
        return cos, sin</code></pre></details>
<h3 id="fastvideo.models.dits.cosmos.CosmosRotaryPosEmbed.forward"><code>fastvideo.models.dits.cosmos.CosmosRotaryPosEmbed.forward</code></h3>
<pre class="doc-signature"><code>forward(hidden_states: torch.Tensor, fps: int | None = None) -&gt; tuple&#91;torch.Tensor, torch.Tensor&#93;</code></pre>

<details><summary>Source</summary><pre><code>def forward(self, hidden_states: torch.Tensor, fps: int | None = None) -&gt; tuple&#91;torch.Tensor, torch.Tensor&#93;:
    fps = 16
    batch_size, num_channels, num_frames, height, width = hidden_states.shape
    pe_size = &#91;num_frames // self.patch_size&#91;0&#93;, height // self.patch_size&#91;1&#93;, width // self.patch_size&#91;2&#93;&#93;
    device = hidden_states.device

    h_theta = 10000.0 * self.h_ntk_factor
    w_theta = 10000.0 * self.w_ntk_factor
    t_theta = 10000.0 * self.t_ntk_factor

    seq = torch.arange(max(self.max_size), device=device, dtype=torch.float32)
    dim_h_range = (torch.arange(0, self.dim_h, 2, device=device, dtype=torch.float32)&#91;:(self.dim_h // 2)&#93; /
                   self.dim_h)
    dim_w_range = (torch.arange(0, self.dim_w, 2, device=device, dtype=torch.float32)&#91;:(self.dim_w // 2)&#93; /
                   self.dim_w)
    dim_t_range = (torch.arange(0, self.dim_t, 2, device=device, dtype=torch.float32)&#91;:(self.dim_t // 2)&#93; /
                   self.dim_t)

    h_spatial_freqs = 1.0 / (h_theta**dim_h_range)
    w_spatial_freqs = 1.0 / (w_theta**dim_w_range)
    temporal_freqs = 1.0 / (t_theta**dim_t_range)

    emb_h = torch.outer(seq&#91;:pe_size&#91;1&#93;&#93;, h_spatial_freqs)&#91;None, :, None, :&#93;.repeat(pe_size&#91;0&#93;, 1, pe_size&#91;2&#93;, 1)
    emb_w = torch.outer(seq&#91;:pe_size&#91;2&#93;&#93;, w_spatial_freqs)&#91;None, None, :, :&#93;.repeat(pe_size&#91;0&#93;, pe_size&#91;1&#93;, 1, 1)

    if fps is None:
        emb_t = torch.outer(seq&#91;:pe_size&#91;0&#93;&#93;, temporal_freqs)
    else:
        temporal_scale = seq&#91;:pe_size&#91;0&#93;&#93; / fps * self.base_fps
        emb_t = torch.outer(temporal_scale, temporal_freqs)

    emb_t = emb_t&#91;:, None, None, :&#93;.repeat(1, pe_size&#91;1&#93;, pe_size&#91;2&#93;, 1)
    freqs = torch.cat(&#91;emb_t, emb_h, emb_w&#93; * 2, dim=-1).flatten(0, 2).float()
    cos = torch.cos(freqs)
    sin = torch.sin(freqs)
    return cos, sin</code></pre></details>
<h2 id="fastvideo.models.dits.cosmos.CosmosLearnablePositionalEmbed"><code>fastvideo.models.dits.cosmos.CosmosLearnablePositionalEmbed</code></h2>
<pre class="doc-signature"><code>CosmosLearnablePositionalEmbed(hidden_size: int, max_size: tuple&#91;int, int, int&#93;, patch_size: tuple&#91;int, int, int&#93;, eps: float = 1e-06)</code></pre>


<details><summary>Source</summary><pre><code>class CosmosLearnablePositionalEmbed(nn.Module):

    def __init__(
        self,
        hidden_size: int,
        max_size: tuple&#91;int, int, int&#93;,
        patch_size: tuple&#91;int, int, int&#93;,
        eps: float = 1e-6,
    ) -&gt; None:
        super().__init__()

        self.max_size = &#91;size // patch for size, patch in zip(max_size, patch_size, strict=False)&#93;
        self.patch_size = patch_size
        self.eps = eps

        self.pos_emb_t = nn.Parameter(torch.zeros(self.max_size&#91;0&#93;, hidden_size))
        self.pos_emb_h = nn.Parameter(torch.zeros(self.max_size&#91;1&#93;, hidden_size))
        self.pos_emb_w = nn.Parameter(torch.zeros(self.max_size&#91;2&#93;, hidden_size))

    def forward(self, hidden_states: torch.Tensor) -&gt; torch.Tensor:
        batch_size, num_channels, num_frames, height, width = hidden_states.shape
        pe_size = &#91;num_frames // self.patch_size&#91;0&#93;, height // self.patch_size&#91;1&#93;, width // self.patch_size&#91;2&#93;&#93;

        emb_t = self.pos_emb_t&#91;:pe_size&#91;0&#93;&#93;&#91;None, :, None, None, :&#93;.repeat(batch_size, 1, pe_size&#91;1&#93;, pe_size&#91;2&#93;, 1)
        emb_h = self.pos_emb_h&#91;:pe_size&#91;1&#93;&#93;&#91;None, None, :, None, :&#93;.repeat(batch_size, pe_size&#91;0&#93;, 1, pe_size&#91;2&#93;, 1)
        emb_w = self.pos_emb_w&#91;:pe_size&#91;2&#93;&#93;&#91;None, None, None, :, :&#93;.repeat(batch_size, pe_size&#91;0&#93;, pe_size&#91;1&#93;, 1, 1)
        emb = emb_t + emb_h + emb_w
        emb = emb.flatten(1, 3)

        norm = torch.linalg.vector_norm(emb, dim=-1, keepdim=True, dtype=torch.float32)
        norm = torch.add(self.eps, norm, alpha=np.sqrt(norm.numel() / emb.numel()))
        return (emb / norm).type_as(hidden_states)</code></pre></details>
<h3 id="fastvideo.models.dits.cosmos.CosmosLearnablePositionalEmbed.forward"><code>fastvideo.models.dits.cosmos.CosmosLearnablePositionalEmbed.forward</code></h3>
<pre class="doc-signature"><code>forward(hidden_states: torch.Tensor) -&gt; torch.Tensor</code></pre>

<details><summary>Source</summary><pre><code>def forward(self, hidden_states: torch.Tensor) -&gt; torch.Tensor:
    batch_size, num_channels, num_frames, height, width = hidden_states.shape
    pe_size = &#91;num_frames // self.patch_size&#91;0&#93;, height // self.patch_size&#91;1&#93;, width // self.patch_size&#91;2&#93;&#93;

    emb_t = self.pos_emb_t&#91;:pe_size&#91;0&#93;&#93;&#91;None, :, None, None, :&#93;.repeat(batch_size, 1, pe_size&#91;1&#93;, pe_size&#91;2&#93;, 1)
    emb_h = self.pos_emb_h&#91;:pe_size&#91;1&#93;&#93;&#91;None, None, :, None, :&#93;.repeat(batch_size, pe_size&#91;0&#93;, 1, pe_size&#91;2&#93;, 1)
    emb_w = self.pos_emb_w&#91;:pe_size&#91;2&#93;&#93;&#91;None, None, None, :, :&#93;.repeat(batch_size, pe_size&#91;0&#93;, pe_size&#91;1&#93;, 1, 1)
    emb = emb_t + emb_h + emb_w
    emb = emb.flatten(1, 3)

    norm = torch.linalg.vector_norm(emb, dim=-1, keepdim=True, dtype=torch.float32)
    norm = torch.add(self.eps, norm, alpha=np.sqrt(norm.numel() / emb.numel()))
    return (emb / norm).type_as(hidden_states)</code></pre></details>
<h2 id="fastvideo.models.dits.cosmos.CosmosTransformer3DModel"><code>fastvideo.models.dits.cosmos.CosmosTransformer3DModel</code></h2>
<pre class="doc-signature"><code>CosmosTransformer3DModel(config: CosmosVideoConfig, hf_config: dict&#91;str, Any&#93;)</code></pre>


<details><summary>Source</summary><pre><code>class CosmosTransformer3DModel(BaseDiT):
    _fsdp_shard_conditions = CosmosVideoConfig()._fsdp_shard_conditions
    _compile_conditions = CosmosVideoConfig()._compile_conditions
    # _supported_attention_backends = CosmosVideoConfig()._supported_attention_backends
    param_names_mapping = CosmosVideoConfig().param_names_mapping
    lora_param_names_mapping = CosmosVideoConfig().lora_param_names_mapping

    def __init__(self, config: CosmosVideoConfig, hf_config: dict&#91;str, Any&#93;) -&gt; None:
        super().__init__(config=config, hf_config=hf_config)

        inner_dim = config.num_attention_heads * config.attention_head_dim
        self.hidden_size = config.hidden_size
        self.num_attention_heads = config.num_attention_heads
        self.in_channels = config.in_channels
        self.out_channels = config.out_channels
        self.num_channels_latents = config.num_channels_latents
        self.patch_size = config.patch_size
        self.max_size = config.max_size
        self.rope_scale = config.rope_scale
        self.concat_padding_mask = config.concat_padding_mask
        self.extra_pos_embed_type = config.extra_pos_embed_type

        # 1. Patch Embedding
        # config.in_channels already includes the condition_mask channel
        # (HF config: in_channels=17 = 16 latent + 1 condition_mask).
        # Only add +1 for the padding_mask when concat_padding_mask=True.
        patch_embed_in_channels = config.in_channels + (1 if config.concat_padding_mask else 0)
        self.patch_embed = CosmosPatchEmbed(patch_embed_in_channels, inner_dim, config.patch_size, bias=False)

        # 2. Positional Embedding
        self.rope = CosmosRotaryPosEmbed(hidden_size=config.attention_head_dim,
                                         max_size=config.max_size,
                                         patch_size=config.patch_size,
                                         rope_scale=config.rope_scale)

        self.learnable_pos_embed = None
        if config.extra_pos_embed_type == &quot;learnable&quot;:
            self.learnable_pos_embed = CosmosLearnablePositionalEmbed(
                hidden_size=inner_dim,
                max_size=config.max_size,
                patch_size=config.patch_size,
            )

        # 3. Time Embedding
        self.time_embed = CosmosEmbedding(inner_dim, inner_dim)

        # 4. Transformer Blocks
        self.transformer_blocks = nn.ModuleList(&#91;
            CosmosTransformerBlock(
                num_attention_heads=config.num_attention_heads,
                attention_head_dim=config.attention_head_dim,
                cross_attention_dim=config.text_embed_dim,
                mlp_ratio=config.mlp_ratio,
                adaln_lora_dim=config.adaln_lora_dim,
                qk_norm=config.qk_norm,
                out_bias=False,
                prefix=f&quot;&#123;config.prefix&#125;.transformer_blocks.&#123;i&#125;&quot;,
            ) for i in range(config.num_layers)
        &#93;)

        # 5. Output norm &amp; projection
        self.norm_out = CosmosAdaLayerNorm(inner_dim, config.adaln_lora_dim)
        self.proj_out = nn.Linear(inner_dim, config.out_channels * math.prod(config.patch_size), bias=False)

        self.gradient_checkpointing = False
        self.__post_init__()

    def forward(self,
                hidden_states: torch.Tensor,
                timestep: torch.Tensor,
                encoder_hidden_states: torch.Tensor | list&#91;torch.Tensor&#93;,
                attention_mask: torch.Tensor | None = None,
                fps: int | None = None,
                condition_mask: torch.Tensor | None = None,
                padding_mask: torch.Tensor | None = None,
                **kwargs) -&gt; torch.Tensor:
        if not isinstance(encoder_hidden_states, torch.Tensor):
            encoder_hidden_states = encoder_hidden_states&#91;0&#93;

        batch_size, num_channels, num_frames, height, width = hidden_states.shape

        # Defensive dtype alignment: the Cosmos checkpoint is bf16 but
        # FSDP-wrapped training copies may report fp32 via
        # `next(parameters()).dtype`, which disables autocast in the
        # shared denoising stage and feeds fp32 tensors into bf16
        # weights. Cast every external input to the patch_embed weight
        # dtype so the model forward is robust regardless of caller.
        _target_dtype = self.patch_embed.proj.weight.dtype
        if hidden_states.dtype != _target_dtype:
            hidden_states = hidden_states.to(_target_dtype)
        if condition_mask is not None and condition_mask.dtype != _target_dtype:
            condition_mask = condition_mask.to(_target_dtype)
        if padding_mask is not None and padding_mask.dtype != _target_dtype:
            padding_mask = padding_mask.to(_target_dtype)
        if isinstance(encoder_hidden_states, torch.Tensor):
            if encoder_hidden_states.dtype != _target_dtype:
                encoder_hidden_states = encoder_hidden_states.to(_target_dtype)
        else:
            encoder_hidden_states = &#91;
                t.to(_target_dtype) if t.dtype != _target_dtype else t for t in encoder_hidden_states
            &#93;

        # 1. Concatenate padding mask if needed &amp; prepare attention mask
        if condition_mask is not None:
            hidden_states = torch.cat(&#91;hidden_states, condition_mask&#93;, dim=1)

        if self.concat_padding_mask:
            from torchvision import transforms
            padding_mask = transforms.functional.resize(padding_mask,
                                                        list(hidden_states.shape&#91;-2:&#93;),
                                                        interpolation=transforms.InterpolationMode.NEAREST)
            # torchvision.resize may upcast bf16/fp16 → fp32; restore
            # hidden_states&#x27; dtype so the subsequent cat doesn&#x27;t promote
            # everything and break patch_embed (bf16 weights).
            padding_mask = padding_mask.to(hidden_states.dtype)
            hidden_states = torch.cat(
                &#91;hidden_states, padding_mask.unsqueeze(2).repeat(batch_size, 1, num_frames, 1, 1)&#93;, dim=1)

        if attention_mask is not None:
            attention_mask = attention_mask.unsqueeze(1).unsqueeze(1)  # &#91;B, 1, 1, S&#93;

        # 2. Generate positional embeddings
        image_rotary_emb = self.rope(hidden_states, fps=fps)
        extra_pos_emb = self.learnable_pos_embed(hidden_states) if self.extra_pos_embed_type == &quot;learnable&quot; else None

        # 3. Patchify input
        p_t, p_h, p_w = self.patch_size
        post_patch_num_frames = num_frames // p_t
        post_patch_height = height // p_h
        post_patch_width = width // p_w
        hidden_states = self.patch_embed(hidden_states)
        hidden_states = hidden_states.flatten(1, 3)  # &#91;B, T, H, W, C&#93; -&gt; &#91;B, THW, C&#93; codespell:ignore

        # 4. Timestep embeddings
        if timestep.ndim == 1:
            temb, embedded_timestep = self.time_embed(hidden_states, timestep)
        elif timestep.ndim == 5:
            assert timestep.shape == (batch_size, 1, num_frames, 1,
                                      1), (f&quot;Expected timestep to have shape &#91;B, 1, T, 1, 1&#93;, but got &#123;timestep.shape&#125;&quot;)
            timestep = timestep.flatten()
            temb, embedded_timestep = self.time_embed(hidden_states, timestep)
            # We can do this because num_frames == post_patch_num_frames, as p_t is 1
            temb, embedded_timestep = (
                x.view(batch_size, post_patch_num_frames, 1, 1, -1).expand(-1, -1, post_patch_height, post_patch_width,
                                                                           -1).flatten(1, 3)
                for x in (temb, embedded_timestep)
            )  # &#91;BT, C&#93; -&gt; &#91;B, T, 1, 1, C&#93; -&gt; &#91;B, T, H, W, C&#93; -&gt; &#91;B, THW, C&#93; codespell:ignore
        else:
            raise ValueError(f&quot;Unsupported timestep shape: &#123;timestep.shape&#125;&quot;)

        # 6. Transformer blocks
        if torch.is_grad_enabled() and self.gradient_checkpointing:
            for i, block in enumerate(self.transformer_blocks):
                hidden_states = self._gradient_checkpointing_func(
                    block,
                    hidden_states,
                    encoder_hidden_states,
                    embedded_timestep,
                    temb,
                    image_rotary_emb,
                    extra_pos_emb,
                    attention_mask,
                )
        else:
            for i, block in enumerate(self.transformer_blocks):
                hidden_states = block(
                    hidden_states=hidden_states,
                    encoder_hidden_states=encoder_hidden_states,
                    embedded_timestep=embedded_timestep,
                    temb=temb,
                    image_rotary_emb=image_rotary_emb,
                    extra_pos_emb=extra_pos_emb,
                    attention_mask=attention_mask,
                )

        # 7. Output norm &amp; projection &amp; unpatchify
        hidden_states = self.norm_out(hidden_states, embedded_timestep, temb)
        hidden_states = self.proj_out(hidden_states)
        hidden_states = hidden_states.unflatten(2, (p_h, p_w, p_t, -1))
        hidden_states = hidden_states.unflatten(1, (post_patch_num_frames, post_patch_height, post_patch_width))
        # NOTE: The permutation order here is not the inverse operation of what happens when patching as usually expected.
        # It might be a source of confusion to the reader, but this is correct
        hidden_states = hidden_states.permute(0, 7, 1, 6, 2, 4, 3, 5)
        hidden_states = hidden_states.flatten(6, 7).flatten(4, 5).flatten(2, 3)

        # Return as tuple for compatibility with callers that
        # do `transformer(..., return_dict=False)&#91;0&#93;` (diffusers
        # convention used by CosmosDenoisingStage).
        if not kwargs.get(&quot;return_dict&quot;, True):
            return (hidden_states, )
        return hidden_states</code></pre></details>
<h3 id="fastvideo.models.dits.cosmos.CosmosTransformer3DModel.forward"><code>fastvideo.models.dits.cosmos.CosmosTransformer3DModel.forward</code></h3>
<pre class="doc-signature"><code>forward(hidden_states: torch.Tensor, timestep: torch.Tensor, encoder_hidden_states: torch.Tensor | list&#91;torch.Tensor&#93;, attention_mask: torch.Tensor | None = None, fps: int | None = None, condition_mask: torch.Tensor | None = None, padding_mask: torch.Tensor | None = None, **kwargs) -&gt; torch.Tensor</code></pre>

<details><summary>Source</summary><pre><code>def forward(self,
            hidden_states: torch.Tensor,
            timestep: torch.Tensor,
            encoder_hidden_states: torch.Tensor | list&#91;torch.Tensor&#93;,
            attention_mask: torch.Tensor | None = None,
            fps: int | None = None,
            condition_mask: torch.Tensor | None = None,
            padding_mask: torch.Tensor | None = None,
            **kwargs) -&gt; torch.Tensor:
    if not isinstance(encoder_hidden_states, torch.Tensor):
        encoder_hidden_states = encoder_hidden_states&#91;0&#93;

    batch_size, num_channels, num_frames, height, width = hidden_states.shape

    # Defensive dtype alignment: the Cosmos checkpoint is bf16 but
    # FSDP-wrapped training copies may report fp32 via
    # `next(parameters()).dtype`, which disables autocast in the
    # shared denoising stage and feeds fp32 tensors into bf16
    # weights. Cast every external input to the patch_embed weight
    # dtype so the model forward is robust regardless of caller.
    _target_dtype = self.patch_embed.proj.weight.dtype
    if hidden_states.dtype != _target_dtype:
        hidden_states = hidden_states.to(_target_dtype)
    if condition_mask is not None and condition_mask.dtype != _target_dtype:
        condition_mask = condition_mask.to(_target_dtype)
    if padding_mask is not None and padding_mask.dtype != _target_dtype:
        padding_mask = padding_mask.to(_target_dtype)
    if isinstance(encoder_hidden_states, torch.Tensor):
        if encoder_hidden_states.dtype != _target_dtype:
            encoder_hidden_states = encoder_hidden_states.to(_target_dtype)
    else:
        encoder_hidden_states = &#91;
            t.to(_target_dtype) if t.dtype != _target_dtype else t for t in encoder_hidden_states
        &#93;

    # 1. Concatenate padding mask if needed &amp; prepare attention mask
    if condition_mask is not None:
        hidden_states = torch.cat(&#91;hidden_states, condition_mask&#93;, dim=1)

    if self.concat_padding_mask:
        from torchvision import transforms
        padding_mask = transforms.functional.resize(padding_mask,
                                                    list(hidden_states.shape&#91;-2:&#93;),
                                                    interpolation=transforms.InterpolationMode.NEAREST)
        # torchvision.resize may upcast bf16/fp16 → fp32; restore
        # hidden_states&#x27; dtype so the subsequent cat doesn&#x27;t promote
        # everything and break patch_embed (bf16 weights).
        padding_mask = padding_mask.to(hidden_states.dtype)
        hidden_states = torch.cat(
            &#91;hidden_states, padding_mask.unsqueeze(2).repeat(batch_size, 1, num_frames, 1, 1)&#93;, dim=1)

    if attention_mask is not None:
        attention_mask = attention_mask.unsqueeze(1).unsqueeze(1)  # &#91;B, 1, 1, S&#93;

    # 2. Generate positional embeddings
    image_rotary_emb = self.rope(hidden_states, fps=fps)
    extra_pos_emb = self.learnable_pos_embed(hidden_states) if self.extra_pos_embed_type == &quot;learnable&quot; else None

    # 3. Patchify input
    p_t, p_h, p_w = self.patch_size
    post_patch_num_frames = num_frames // p_t
    post_patch_height = height // p_h
    post_patch_width = width // p_w
    hidden_states = self.patch_embed(hidden_states)
    hidden_states = hidden_states.flatten(1, 3)  # &#91;B, T, H, W, C&#93; -&gt; &#91;B, THW, C&#93; codespell:ignore

    # 4. Timestep embeddings
    if timestep.ndim == 1:
        temb, embedded_timestep = self.time_embed(hidden_states, timestep)
    elif timestep.ndim == 5:
        assert timestep.shape == (batch_size, 1, num_frames, 1,
                                  1), (f&quot;Expected timestep to have shape &#91;B, 1, T, 1, 1&#93;, but got &#123;timestep.shape&#125;&quot;)
        timestep = timestep.flatten()
        temb, embedded_timestep = self.time_embed(hidden_states, timestep)
        # We can do this because num_frames == post_patch_num_frames, as p_t is 1
        temb, embedded_timestep = (
            x.view(batch_size, post_patch_num_frames, 1, 1, -1).expand(-1, -1, post_patch_height, post_patch_width,
                                                                       -1).flatten(1, 3)
            for x in (temb, embedded_timestep)
        )  # &#91;BT, C&#93; -&gt; &#91;B, T, 1, 1, C&#93; -&gt; &#91;B, T, H, W, C&#93; -&gt; &#91;B, THW, C&#93; codespell:ignore
    else:
        raise ValueError(f&quot;Unsupported timestep shape: &#123;timestep.shape&#125;&quot;)

    # 6. Transformer blocks
    if torch.is_grad_enabled() and self.gradient_checkpointing:
        for i, block in enumerate(self.transformer_blocks):
            hidden_states = self._gradient_checkpointing_func(
                block,
                hidden_states,
                encoder_hidden_states,
                embedded_timestep,
                temb,
                image_rotary_emb,
                extra_pos_emb,
                attention_mask,
            )
    else:
        for i, block in enumerate(self.transformer_blocks):
            hidden_states = block(
                hidden_states=hidden_states,
                encoder_hidden_states=encoder_hidden_states,
                embedded_timestep=embedded_timestep,
                temb=temb,
                image_rotary_emb=image_rotary_emb,
                extra_pos_emb=extra_pos_emb,
                attention_mask=attention_mask,
            )

    # 7. Output norm &amp; projection &amp; unpatchify
    hidden_states = self.norm_out(hidden_states, embedded_timestep, temb)
    hidden_states = self.proj_out(hidden_states)
    hidden_states = hidden_states.unflatten(2, (p_h, p_w, p_t, -1))
    hidden_states = hidden_states.unflatten(1, (post_patch_num_frames, post_patch_height, post_patch_width))
    # NOTE: The permutation order here is not the inverse operation of what happens when patching as usually expected.
    # It might be a source of confusion to the reader, but this is correct
    hidden_states = hidden_states.permute(0, 7, 1, 6, 2, 4, 3, 5)
    hidden_states = hidden_states.flatten(6, 7).flatten(4, 5).flatten(2, 3)

    # Return as tuple for compatibility with callers that
    # do `transformer(..., return_dict=False)&#91;0&#93;` (diffusers
    # convention used by CosmosDenoisingStage).
    if not kwargs.get(&quot;return_dict&quot;, True):
        return (hidden_states, )
    return hidden_states</code></pre></details>
</div>
