Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions src/diffusers/pipelines/ltx2/pipeline_ltx2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Author

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_device is where the text encoder runs, so only its inputs go there. The embeddings go to the pipeline device.

prompt_attention_mask = prompt_attention_mask.to(device)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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:
Expand All @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
37 changes: 24 additions & 13 deletions src/diffusers/pipelines/ltx2/pipeline_ltx2_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,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
Expand Down Expand Up @@ -394,15 +394,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)

# duplicate text embeddings for each generation per prompt, using mps friendly method
_, seq_len, _ = prompt_embeds.shape
Expand Down Expand Up @@ -1137,7 +1139,7 @@ def prepare_latents(
)

condition_frames, condition_strengths, condition_indices, condition_pixel_frames = self.preprocess_conditions(
conditions, height, width, num_frames, device=device
conditions, height, width, num_frames, device=get_module_execution_device(self.vae)
)
# Encode each condition through the VAE. We keep both the 5D latent (for coord computation) and the packed
# 3D latent (for first-frame replacement or keyframe append).
Expand Down Expand Up @@ -1692,8 +1694,14 @@ def __call__(
prompt_embeds = torch.cat([negative_prompt_embeds, prompt_embeds], dim=0)
prompt_attention_mask = torch.cat([negative_prompt_attention_mask, prompt_attention_mask], dim=0)

connector_prompt_embeds, connector_audio_prompt_embeds, connector_attention_mask = self.connectors(
prompt_embeds, prompt_attention_mask, padding_side=self.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=self.tokenizer_padding_side,
)
connector_prompt_embeds, connector_audio_prompt_embeds, connector_attention_mask = (
output.to(device) for output in connector_outputs
)

if auto_duration:
Expand All @@ -1703,9 +1711,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,
Expand Down Expand Up @@ -2108,13 +2117,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()
Expand Down
48 changes: 34 additions & 14 deletions src/diffusers/pipelines/ltx2/pipeline_ltx2_dfr.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from ...models.transformers import LTX2VideoTransformer3DModel
from ...schedulers import FlowMatchEulerDiscreteScheduler, LTXEulerAncestralRFScheduler
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 .dfr_layout import LTX2DFREpilogueTile, resolve_canvas, video_tile_plan
Expand Down Expand Up @@ -505,15 +505,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)

# duplicate text embeddings for each generation per prompt, using mps friendly method
_, seq_len, _ = prompt_embeds.shape
Expand Down Expand Up @@ -1206,7 +1208,7 @@ def encode_conditions(
The returned index is on `num_frames`' own pixel grid; carrying it onto a refined canvas is the caller's job.
"""
condition_frames, condition_strengths, condition_indices, condition_pixel_frames = self.preprocess_conditions(
conditions, height, width, num_frames, device=device
conditions, height, width, num_frames, device=get_module_execution_device(self.vae)
)
encoded = []
for pixels, strength, latent_index, num_pixel_frames in zip(
Expand Down Expand Up @@ -1505,7 +1507,11 @@ def rebuild_epilogue_keyframes(
)
plane = (1 - decode_noise_scale) * plane + decode_noise_scale * noise
timestep = torch.full((plane.shape[0],), decode_timestep, device=device, dtype=plane.dtype)
frames = self.vae.decode(plane.to(self.vae.dtype), timestep, return_dict=False)[0]
frames = self.vae.decode(
plane.to(device=get_module_execution_device(self.vae), dtype=self.vae.dtype),
timestep,
return_dict=False,
)[0]

# PIL is the reference's Lanczos, and it wants one 8-bit channels-last image at a time.
stretched_batch = []
Expand All @@ -1515,7 +1521,9 @@ def rebuild_epilogue_keyframes(
image = image.resize((image.width * 2, image.height * 2), resample=PIL.Image.Resampling.LANCZOS)
stretched = torch.from_numpy(np.asarray(image, dtype=np.float32) / 127.5 - 1.0)
stretched_batch.append(stretched.permute(2, 0, 1).unsqueeze(1))
stretched = torch.stack(stretched_batch).to(device=device, dtype=self.vae.dtype)
stretched = torch.stack(stretched_batch).to(
device=get_module_execution_device(self.vae), dtype=self.vae.dtype
)

encoded.append(retrieve_latents(self.vae.encode(stretched), sample_mode="argmax"))
return torch.cat(encoded, dim=2).to(device=device, dtype=dtype)
Expand Down Expand Up @@ -1763,18 +1771,25 @@ def __call__(
max_sequence_length=max_sequence_length,
device=device,
)
video_prompt_embeds, audio_prompt_embeds, connector_attention_mask = self.connectors(
prompt_embeds, prompt_attention_mask, padding_side=self.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=self.tokenizer_padding_side,
)
video_prompt_embeds, audio_prompt_embeds, connector_attention_mask = (
output.to(device) for output in connector_outputs
)

if num_frames is None:
if getattr(self, "duration_head", None) is None:
raise ValueError(
"`num_frames` must be supplied when the pipeline has no `duration_head` component to predict it."
)
duration_head_device = get_module_execution_device(self.duration_head)
num_frames = self.duration_head.predict_num_frames(
video_prompt_embeds[:1],
audio_prompt_embeds[:1],
video_prompt_embeds[:1].to(duration_head_device),
audio_prompt_embeds[:1].to(duration_head_device),
frame_rate=frame_rate,
temporal_compression_ratio=self.vae_temporal_compression_ratio,
min_seconds=min_seconds,
Expand Down Expand Up @@ -1943,10 +1958,15 @@ def __call__(
video_latents = self._denormalize_latents(
video_latents, self.vae.latents_mean, self.vae.latents_std, self.vae.config.scaling_factor
)
video = self.vae.decode(video_latents.to(self.vae.dtype), timestep, return_dict=False)[0]
video_latents = video_latents.to(device=get_module_execution_device(self.vae), dtype=self.vae.dtype)
video = self.vae.decode(video_latents, timestep, return_dict=False)[0]
video = self.video_processor.postprocess_video(video, output_type=output_type)

audio = self.vocoder(self.audio_vae.decode(public_audio.to(self.audio_vae.dtype), return_dict=False)[0])
public_audio = public_audio.to(
device=get_module_execution_device(self.audio_vae), dtype=self.audio_vae.dtype
)
generated_mel_spectrograms = self.audio_vae.decode(public_audio, return_dict=False)[0]
audio = self.vocoder(generated_mel_spectrograms.to(get_module_execution_device(self.vocoder)))
audio_samples = min(
audio.shape[-1], round(requested_frames / frame_rate * self.vocoder.config.output_sampling_rate)
)
Expand Down
Loading