Skip to content

[WIP] test prepare_latents for ltx0.95 - #10976

Merged
yiyixuxu merged 18 commits into
integrations/ltx-0.9.5from
ltx-95-latents-yiyi
Mar 14, 2025
Merged

[WIP] test prepare_latents for ltx0.95#10976
yiyixuxu merged 18 commits into
integrations/ltx-0.9.5from
ltx-95-latents-yiyi

Conversation

@yiyixuxu

@yiyixuxuyiyixuxu commented Mar 6, 2025

Copy link
Copy Markdown
Collaborator

for #10968

importtorchfromdiffusers.pipelines.ltx.pipeline_ltx_conditionimportLTXConditionPipeline, LTXVideoConditionfromdiffusers.utilsimportexport_to_video, load_videodevice="cuda:2"dtype=torch.bfloat16# command to convert the checkpoint"""python scripts/convert_ltx_to_diffusers.py --transformer_ckpt_path "./ltx-video-2b-v0.9.5rc1.safetensors" --vae_ckpt_path "./ltx-video-2b-v0.9.5rc1.safetensors" --output_path "/raid/yiyi/LTX-Video-95" --version 0.9.5 --save_pipeline"""repo="/raid/yiyi/LTX-Video-95"# Initialize the pipelinepipe=LTXConditionPipeline.from_pretrained(repo, torch_dtype=dtype)
pipe.to(device)
video=load_video(
"/raid/yiyi/LTX-Video/outputs/2025-03-11/video_output_0_a-woman-with-long-brown-hair-and_42_512x768x40_0.mp4"
)
condition=LTXVideoCondition(
video=video,
frame_index=8,
)
# Define promptsprompt="A woman with long brown hair and light skin smiles at another woman with long blonde hair. The woman with brown hair wears a black jacket and has a small, barely noticeable mole on her right cheek. The camera angle is a close-up, focused on the woman with brown hair's face. The lighting is warm and natural, likely from the setting sun, casting a soft glow on the scene. The scene appears to be real-life footage"negative_prompt='worst quality, inconsistent motion, blurry, jittery, distorted'# Generate the videovideo=pipe(
conditions=[condition],
prompt=prompt,
negative_prompt=negative_prompt,
width=768,
height=512,
num_frames=161,
num_inference_steps=40,
).frames[0]
# Export the videoexport_to_video(video, "output.mp4", fps=24)

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Comment threadsrc/diffusers/pipelines/ltx/pipeline_ltx_condition.py
Comment threadsrc/diffusers/models/autoencoders/autoencoder_kl_ltx.py
Comment on lines +655 to +665
condition_latents, rope_interpolation_scale = self._pack_latents(
condition_latents, self.transformer_spatial_patch_size, self.transformer_temporal_patch_size, device
)

rope_interpolation_scale = (
rope_interpolation_scale *
torch.tensor([self.vae_temporal_compression_ratio, self.vae_spatial_compression_ratio, self.vae_spatial_compression_ratio], device=rope_interpolation_scale.device)[None, :, None]
)
rope_interpolation_scale[:, 0] = (rope_interpolation_scale[:, 0] + 1 - self.vae_temporal_compression_ratio).clamp(min=0)
rope_interpolation_scale[:, 0] += condition.frame_index

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is compatible with what we do in LTXRotaryPosEmbed layer... We prepare the meshgrid there and only pass the interpolation scales from the pipeline. It seems like here we are preparing the meshgrid beforehand, which will be incorrect. I think we would have to do one of the following:

  • Make sure to only pass multiplicative interpolation scale without first multiplying with the latent_coords (the screenshot below shows how I handled it in the other PR)
  • If we're passing latent_coords, we will have to handle it differently in the transformer for LTX v0.9.0/v0.9.1 vs v0.9.5

image

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@a-r-r-o-w

I think we have different ways to handle rope in our current code base, in general, I think it's more convenient/natural to prepare position ids (e.g. the image_ids, text_ids in flux or the grid here for ltx) at same time when we patchify the latents (e.g. pack_latent for ltx or flux). flux and ltx do this in pipeline and other models like lumina handle both together inside transformer with a patch embed

I think it is ok to have this flexibility for rope since it's something that slows us down for each integration. maybe a general rule is to try to follow closer to the original code base and fit it into one of the patterns that's easier for us to maintain.

generator,
device,
torch.float32,
prompt_embeds.dtype,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use float32 here and then typecast before sending into transformer, no? That way there won't be a downcast/upcast for CFG

@yiyixuxu
yiyixuxu requested review from a-r-r-o-w and hlkyMarch 12, 2025 08:18

@hlkyhlky left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @yiyixuxu

@a-r-r-o-wa-r-r-o-w left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for taking this up @yiyixuxu! LGTM but I had one doubt

Comment threadsrc/diffusers/pipelines/ltx/pipeline_ltx_condition.py Outdated
Comment threadsrc/diffusers/pipelines/ltx/pipeline_ltx_condition.py Outdated
Comment threadsrc/diffusers/pipelines/ltx/pipeline_ltx_condition.py Outdated
Comment on lines -627 to -633
rope_interpolation_scale = [
# TODO!!! This is incorrect: the frame index needs to added AFTER multiplying the interpolation
# scale with the grid.
(self.vae_temporal_compression_ratio + condition.frame_index) / frame_rate,
self.vae_spatial_compression_ratio,
self.vae_spatial_compression_ratio,
]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yiyixuxu Pardon my stupidity, but I can't seem to find if we're handling this + condition.frame_index part. Is this missing by any chance, or was I mistaken in trying to handle this here?

In the original code, this is what I was meaning to handle: https://github.com/Lightricks/LTX-Video/blob/496dc5058f4408dcb777282f3fb6377fb2da08e6/ltx_video/pipelines/pipeline_ltx_video.py#L1285

* torch.tensor([scale_factor_t, scale_factor, scale_factor], device=video_ids.device)[None, :, None]
)
scaled_latent_coords[:, 0] = (scaled_latent_coords[:, 0] + 1 - scale_factor_t).clamp(min=0)
scaled_latent_coords[:, 0] += frame_index

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@a-r-r-o-w it's here!

@yiyixuxu
yiyixuxu merged commit e98fea2 into integrations/ltx-0.9.5Mar 14, 2025
@yiyixuxu
yiyixuxu deleted the ltx-95-latents-yiyi branch March 14, 2025 09:24
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@yiyixuxu@HuggingFaceDocBuilderDev@a-r-r-o-w@hlky