|
| 1 | +""" |
| 2 | +Copyright 2025 Google LLC |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +""" |
| 16 | + |
| 17 | +from typing import Tuple, Union, List |
| 18 | +import jax |
| 19 | +import jax.numpy as jnp |
| 20 | +from flax import nnx |
| 21 | +from maxdiffusion import common_types |
| 22 | + |
| 23 | +from .feature_extractor_ltx2 import LTX2GemmaFeatureExtractor |
| 24 | +from .embeddings_connector_ltx2 import Embeddings1DConnector |
| 25 | + |
| 26 | +Array = common_types.Array |
| 27 | +DType = common_types.DType |
| 28 | + |
| 29 | + |
| 30 | +class LTX2VideoGemmaTextEncoder(nnx.Module): |
| 31 | + """ |
| 32 | + Encoder for Video-only tasks. |
| 33 | + Pipeline: Gemma Hidden States -> Feature Extractor -> Video Connector -> Output |
| 34 | + """ |
| 35 | + |
| 36 | + def __init__( |
| 37 | + self, |
| 38 | + # Feature Extractor Config |
| 39 | + gemma_dim: int = 3840, # Gemma-3-12b |
| 40 | + gemma_layers: int = 49, # Gemma-3 has 48 layers + 1 embedding layer output = 49 hidden states |
| 41 | + projection_dim: int = 3840, # LTX-2 conditioning dim |
| 42 | + # Connector Config |
| 43 | + connector_heads: int = 32, |
| 44 | + connector_head_dim: int = 128, |
| 45 | + connector_layers: int = 2, |
| 46 | + num_thinking_tokens: int = 128, |
| 47 | + dtype: DType = jnp.float32, |
| 48 | + attention_kernel: str = "flash", |
| 49 | + mesh: jax.sharding.Mesh = None, |
| 50 | + rngs: nnx.Rngs = None, |
| 51 | + ): |
| 52 | + input_dim = gemma_dim * gemma_layers |
| 53 | + |
| 54 | + self.feature_extractor = LTX2GemmaFeatureExtractor( |
| 55 | + input_dim=input_dim, |
| 56 | + output_dim=projection_dim, |
| 57 | + dtype=dtype, |
| 58 | + rngs=rngs, |
| 59 | + ) |
| 60 | + |
| 61 | + self.embeddings_connector = Embeddings1DConnector( |
| 62 | + input_dim=projection_dim, |
| 63 | + heads=connector_heads, |
| 64 | + head_dim=connector_head_dim, |
| 65 | + layers=connector_layers, |
| 66 | + num_learnable_registers=num_thinking_tokens, |
| 67 | + rope_type="interleaved", |
| 68 | + attention_kernel=attention_kernel, |
| 69 | + mesh=mesh, |
| 70 | + rngs=rngs, |
| 71 | + ) |
| 72 | + |
| 73 | + def __call__( |
| 74 | + self, |
| 75 | + hidden_states: Union[Tuple[Array, ...], List[Array]], |
| 76 | + attention_mask: Array, |
| 77 | + ) -> Array: |
| 78 | + """ |
| 79 | + Args: |
| 80 | + hidden_states: From Gemma output.hidden_states (Tuple of [B, T, D]) |
| 81 | + attention_mask: [B, T] |
| 82 | + """ |
| 83 | + # 1. Feature Extraction (Stack -> Norm -> Project) |
| 84 | + features = self.feature_extractor(hidden_states, attention_mask) |
| 85 | + |
| 86 | + # 2. Connection (Refine + Thinking Tokens) |
| 87 | + video_embeds = self.embeddings_connector(features, attention_mask) |
| 88 | + |
| 89 | + return video_embeds |
| 90 | + |
| 91 | + |
| 92 | +class LTX2AudioVideoGemmaTextEncoder(nnx.Module): |
| 93 | + """ |
| 94 | + Encoder for Audio-Video tasks. |
| 95 | + Pipeline: Gemma Hidden States -> Feature Extractor -> [Video Connector, Audio Connector] |
| 96 | + """ |
| 97 | + |
| 98 | + def __init__( |
| 99 | + self, |
| 100 | + # Feature Extractor Config (Shared) |
| 101 | + gemma_dim: int = 3840, # Gemma-3-12b |
| 102 | + gemma_layers: int = 49, # Gemma-3 has 48 layers + 1 embedding layer output = 49 hidden states |
| 103 | + projection_dim: int = 3840, |
| 104 | + # Connector Config |
| 105 | + connector_heads: int = 30, |
| 106 | + connector_head_dim: int = 128, |
| 107 | + connector_layers: int = 2, |
| 108 | + num_thinking_tokens: int = 128, |
| 109 | + dtype: DType = jnp.float32, |
| 110 | + attention_kernel: str = "flash", |
| 111 | + mesh: jax.sharding.Mesh = None, |
| 112 | + rngs: nnx.Rngs = None, |
| 113 | + ): |
| 114 | + input_dim = gemma_dim * gemma_layers |
| 115 | + |
| 116 | + self.feature_extractor = LTX2GemmaFeatureExtractor( |
| 117 | + input_dim=input_dim, |
| 118 | + output_dim=projection_dim, |
| 119 | + dtype=dtype, |
| 120 | + rngs=rngs, |
| 121 | + ) |
| 122 | + |
| 123 | + # Two independent connectors |
| 124 | + self.video_embeddings_connector = Embeddings1DConnector( |
| 125 | + input_dim=projection_dim, |
| 126 | + heads=connector_heads, |
| 127 | + head_dim=connector_head_dim, |
| 128 | + layers=connector_layers, |
| 129 | + num_learnable_registers=num_thinking_tokens, |
| 130 | + rope_type="interleaved", |
| 131 | + attention_kernel=attention_kernel, |
| 132 | + mesh=mesh, |
| 133 | + rngs=rngs, |
| 134 | + ) |
| 135 | + |
| 136 | + self.audio_embeddings_connector = Embeddings1DConnector( |
| 137 | + input_dim=projection_dim, |
| 138 | + heads=connector_heads, |
| 139 | + head_dim=connector_head_dim, |
| 140 | + layers=connector_layers, |
| 141 | + num_learnable_registers=num_thinking_tokens, |
| 142 | + rope_type="interleaved", |
| 143 | + attention_kernel=attention_kernel, |
| 144 | + mesh=mesh, |
| 145 | + rngs=rngs, |
| 146 | + ) |
| 147 | + |
| 148 | + def __call__( |
| 149 | + self, |
| 150 | + hidden_states: Union[Tuple[Array, ...], List[Array]], |
| 151 | + attention_mask: Array, |
| 152 | + ) -> Tuple[Array, Array]: |
| 153 | + """ |
| 154 | + Returns: |
| 155 | + (video_embeds, audio_embeds) |
| 156 | + """ |
| 157 | + # 1. Shared Feature Extraction |
| 158 | + features = self.feature_extractor(hidden_states, attention_mask) |
| 159 | + |
| 160 | + # 2. Parallel Connection |
| 161 | + video_embeds = self.video_embeddings_connector(features, attention_mask) |
| 162 | + audio_embeds = self.audio_embeddings_connector(features, attention_mask) |
| 163 | + |
| 164 | + return video_embeds, audio_embeds |
0 commit comments