Repository navigation
[LTX-2] Fix component device placement for split-device pipelines #15420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dacorvo
wants to merge
3
commits into
main
Choose a base branch
from
ltx2-split-device
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+243
−91
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,7 +36,7 @@ | |
| from ...models.transformers import LTX2VideoTransformer3DModel | ||
| from ...schedulers import FlowMatchEulerDiscreteScheduler | ||
| from ...utils import is_torch_xla_available, logging, replace_example_docstring | ||
| from ...utils.torch_utils import randn_tensor | ||
| from ...utils.torch_utils import get_module_execution_device, randn_tensor | ||
| from ...video_processor import VideoProcessor | ||
| from ..pipeline_utils import DiffusionPipeline | ||
| from .duration_head import LTX2DurationHead | ||
|
|
@@ -343,15 +343,17 @@ def _get_gemma_prompt_embeds( | |
| ) | ||
| text_input_ids = text_inputs.input_ids | ||
| prompt_attention_mask = text_inputs.attention_mask | ||
| text_input_ids = text_input_ids.to(device) | ||
| prompt_attention_mask = prompt_attention_mask.to(device) | ||
| model_device = get_module_execution_device(self.text_encoder) | ||
|
|
||
| text_encoder_outputs = self.text_encoder( | ||
| input_ids=text_input_ids, attention_mask=prompt_attention_mask, output_hidden_states=True | ||
| input_ids=text_input_ids.to(model_device), | ||
| attention_mask=prompt_attention_mask.to(model_device), | ||
| output_hidden_states=True, | ||
| ) | ||
| text_encoder_hidden_states = text_encoder_outputs.hidden_states | ||
| text_encoder_hidden_states = torch.stack(text_encoder_hidden_states, dim=-1) | ||
| prompt_embeds = text_encoder_hidden_states.flatten(2, 3).to(dtype=dtype) # Pack to 3D | ||
| prompt_embeds = text_encoder_hidden_states.flatten(2, 3).to(device=device, dtype=dtype) # Pack to 3D | ||
| prompt_attention_mask = prompt_attention_mask.to(device) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. |
||
|
|
||
| # duplicate text embeddings for each generation per prompt, using mps friendly method | ||
| _, seq_len, _ = prompt_embeds.shape | ||
|
|
@@ -1247,8 +1249,14 @@ def __call__( | |
| tokenizer_padding_side = "left" # Padding side for the default Gemma text encoder | ||
| if getattr(self, "tokenizer", None) is not None: | ||
| tokenizer_padding_side = getattr(self.tokenizer, "padding_side", "left") | ||
| connector_prompt_embeds, connector_audio_prompt_embeds, connector_attention_mask = self.connectors( | ||
| prompt_embeds, prompt_attention_mask, padding_side=tokenizer_padding_side | ||
| connectors_device = get_module_execution_device(self.connectors) | ||
| connector_outputs = self.connectors( | ||
| prompt_embeds.to(connectors_device), | ||
| prompt_attention_mask.to(connectors_device), | ||
| padding_side=tokenizer_padding_side, | ||
| ) | ||
| connector_prompt_embeds, connector_audio_prompt_embeds, connector_attention_mask = ( | ||
| output.to(device) for output in connector_outputs | ||
| ) | ||
|
|
||
| if auto_duration: | ||
|
|
@@ -1258,9 +1266,10 @@ def __call__( | |
| video_tokens = video_tokens.chunk(2, dim=0)[1] | ||
| audio_tokens = audio_tokens.chunk(2, dim=0)[1] | ||
| # Rows past the first are `num_videos_per_prompt` duplicates of the same prompt. | ||
| duration_head_device = get_module_execution_device(self.duration_head) | ||
| num_frames = self.duration_head.predict_num_frames( | ||
| video_tokens[:1], | ||
| audio_tokens[:1], | ||
| video_tokens[:1].to(duration_head_device), | ||
| audio_tokens[:1].to(duration_head_device), | ||
| frame_rate=frame_rate, | ||
| temporal_compression_ratio=self.vae_temporal_compression_ratio, | ||
| min_seconds=min_seconds, | ||
|
|
@@ -1654,13 +1663,15 @@ def __call__( | |
| latents, self.vae.latents_mean, self.vae.latents_std, self.vae.config.scaling_factor | ||
| ) | ||
|
|
||
| latents = latents.to(self.vae.dtype) | ||
| latents = latents.to(device=get_module_execution_device(self.vae), dtype=self.vae.dtype) | ||
| video = self.vae.decode(latents, timestep, return_dict=False)[0] | ||
| video = self.video_processor.postprocess_video(video, output_type=output_type) | ||
|
|
||
| audio_latents = audio_latents.to(self.audio_vae.dtype) | ||
| audio_latents = audio_latents.to( | ||
| device=get_module_execution_device(self.audio_vae), dtype=self.audio_vae.dtype | ||
| ) | ||
| generated_mel_spectrograms = self.audio_vae.decode(audio_latents, return_dict=False)[0] | ||
| audio = self.vocoder(generated_mel_spectrograms) | ||
| audio = self.vocoder(generated_mel_spectrograms.to(get_module_execution_device(self.vocoder))) | ||
|
|
||
| # Offload all models | ||
| self.maybe_free_model_hooks() | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it not be
model_device?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sayakpaul that's the pattern drawn by @JingyaHuang that I replicate here:
model_deviceis where the text encoder runs, so only its inputs go there. The embeddings go to the pipelinedevice.