| 84 | |
| 85 | |
| 86 | class LTXVGemmaTextEncoderModel(torch.nn.Module): |
| 87 | def __init__( |
| 88 | self, |
| 89 | model: Gemma3ForConditionalGeneration, |
| 90 | feature_extractor, # FeatureExtractorV1/V2 |
| 91 | embeddings_processor, # VideoEmbeddingsProcessor or AVEmbeddingsProcessor |
| 92 | processor: Gemma3Processor | None = None, |
| 93 | dtype=torch.bfloat16, |
| 94 | device="cpu", |
| 95 | ): |
| 96 | super().__init__() |
| 97 | self.model = model |
| 98 | self.processor = processor |
| 99 | self.feature_extractor = feature_extractor.to(dtype=dtype) |
| 100 | self.embeddings_processor = embeddings_processor.to(dtype=dtype) |
| 101 | self.dtypes = set([dtype]) |
| 102 | # Cache an estimate of memory required to load/keep the model on device |
| 103 | # weights size + small overhead |
| 104 | self._model_memory_required = ( |
| 105 | comfy.model_management.module_size(self.model) + 256 * 1024 * 1024 |
| 106 | ) |
| 107 | |
| 108 | def set_clip_options(self, options): |
| 109 | pass |
| 110 | |
| 111 | def reset_clip_options(self): |
| 112 | pass |
| 113 | |
| 114 | def forward(self, input_ids, attention_mask, padding_side="right"): |
| 115 | # Block 1: Run Gemma model |
| 116 | outputs = self.model( |
| 117 | input_ids=input_ids, |
| 118 | attention_mask=attention_mask, |
| 119 | output_hidden_states=True, |
| 120 | ) |
| 121 | all_layer_hiddens = torch.stack(outputs.hidden_states, dim=-1) # [B, T, D, L] |
| 122 | |
| 123 | # Block 2: Feature extraction |
| 124 | features = self.feature_extractor( |
| 125 | all_layer_hiddens, attention_mask, padding_side |
| 126 | ) |
| 127 | return features # dict with "video" and optionally "audio" |
| 128 | |
| 129 | def encode_token_weights(self, token_weight_pairs): |
| 130 | token_pairs = token_weight_pairs["gemma"] |
| 131 | input_ids = torch.tensor( |
| 132 | [[t[0] for t in token_pairs]], device=self.model.device |
| 133 | ) |
| 134 | attention_mask = torch.tensor( |
| 135 | [[w[1] for w in token_pairs]], device=self.model.device |
| 136 | ) |
| 137 | |
| 138 | self.to(self.model.device) |
| 139 | |
| 140 | features = self(input_ids, attention_mask, padding_side="left") |
| 141 | |
| 142 | # Convert binary mask -> additive mask for processor |
| 143 | encoded_input_dtype = next(iter(features.values())).dtype |
nothing calls this directly
no outgoing calls
no test coverage detected