From 4443864994674da19cde86a061c9da2566797d07 Mon Sep 17 00:00:00 2001 From: Mert Kaya <47145541+mertkayacs@users.noreply.github.com> Date: Wed, 2 Dec 2026 17:49:21 +0300 Subject: [PATCH] fix(ltx): keep the final automatic denoising step --- .../ltx/pipeline_ltx_i2v_long_multi_prompt.py | 26 +++++++--- .../ltx/test_ltx_i2v_long_multi_prompt.py | 52 +++++++++++++++++++ 2 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 tests/pipelines/ltx/test_ltx_i2v_long_multi_prompt.py diff --git a/src/diffusers/pipelines/ltx/pipeline_ltx_i2v_long_multi_prompt.py b/src/diffusers/pipelines/ltx/pipeline_ltx_i2v_long_multi_prompt.py index b75cf0aede47..b6fd2a15146c 100644 --- a/src/diffusers/pipelines/ltx/pipeline_ltx_i2v_long_multi_prompt.py +++ b/src/diffusers/pipelines/ltx/pipeline_ltx_i2v_long_multi_prompt.py @@ -734,6 +734,22 @@ def prepare_latents( return latents, negative_index_latents, latent_num_frames, latent_height, latent_width + def _prepare_timesteps( + self, num_inference_steps: int, sigmas: list[float] | torch.Tensor | None, device: torch.device + ) -> torch.Tensor: + if sigmas is not None: + sigmas = torch.tensor(sigmas, dtype=torch.float32) if not isinstance(sigmas, torch.Tensor) else sigmas + self.scheduler.set_timesteps(sigmas=sigmas, device=device) + else: + self.scheduler.set_timesteps(num_inference_steps=num_inference_steps, device=device) + + timesteps = self.scheduler.timesteps + # Auto-generated schedules already keep the terminal sigma out of timesteps. + if sigmas is not None and len(sigmas) > 0 and sigmas[-1] == 0: + timesteps = timesteps[:-1] + self._num_timesteps = len(timesteps) + return timesteps + # TODO: refactor this out @torch.no_grad() def vae_decode_tiled( @@ -1176,13 +1192,7 @@ def __call__( prompt_attention_mask = torch.cat([negative_prompt_attention_mask, prompt_attention_mask], dim=0) # 6.2 Window-level timesteps reset: fresh sampling for each temporal window - if sigmas is not None: - s = torch.tensor(sigmas, dtype=torch.float32) if not isinstance(sigmas, torch.Tensor) else sigmas - self.scheduler.set_timesteps(sigmas=s, device=device) - self._num_timesteps = len(sigmas) - else: - self.scheduler.set_timesteps(num_inference_steps=num_inference_steps, device=device) - self._num_timesteps = num_inference_steps + timesteps = self._prepare_timesteps(num_inference_steps, sigmas, device) # 6.3 Extract window latents [B,C,T,H,W] window_latents = latents[:, :, w_start:w_end] @@ -1266,7 +1276,7 @@ def __call__( # 6.6 Denoising loop per full window (no spatial tiling) sigmas_current = self.scheduler.sigmas.to(device=latents_packed.device) if sigmas_current.shape[0] >= 2: - for i, t in enumerate(self.progress_bar(self.scheduler.timesteps[:-1])): + for i, t in enumerate(self.progress_bar(timesteps)): if self.interrupt: break # Skip semantics: if sigma exceeds threshold, skip this step (do not call scheduler.step) diff --git a/tests/pipelines/ltx/test_ltx_i2v_long_multi_prompt.py b/tests/pipelines/ltx/test_ltx_i2v_long_multi_prompt.py new file mode 100644 index 000000000000..01808bcfd955 --- /dev/null +++ b/tests/pipelines/ltx/test_ltx_i2v_long_multi_prompt.py @@ -0,0 +1,52 @@ +# Copyright 2026 The HuggingFace Team. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pytest +import torch + +from diffusers import FlowMatchEulerDiscreteScheduler, LTXEulerAncestralRFScheduler, LTXI2VLongMultiPromptPipeline + + +@pytest.mark.parametrize("scheduler_cls", [FlowMatchEulerDiscreteScheduler, LTXEulerAncestralRFScheduler]) +@pytest.mark.parametrize( + "sigmas", [None, [1.0, 0.5, 0.0], torch.tensor([1.0, 0.5, 0.0])], ids=["auto", "list", "tensor"] +) +def test_denoising_schedule_reaches_zero(scheduler_cls, sigmas): + scheduler = scheduler_cls() + pipe = LTXI2VLongMultiPromptPipeline( + scheduler=scheduler, vae=None, text_encoder=None, tokenizer=None, transformer=None + ) + device = torch.device("cpu") + num_inference_steps = 2 if sigmas is None else 3 + + timesteps = pipe._prepare_timesteps(num_inference_steps, sigmas, device) + + sample = torch.ones(1, device=device) + generator = torch.Generator(device=device).manual_seed(0) + for timestep in timesteps: + sample = scheduler.step(torch.ones_like(sample), timestep, sample, generator=generator, return_dict=False)[0] + + assert scheduler.sigmas[scheduler.step_index].item() == 0.0 + assert len(timesteps) == pipe.num_timesteps == 2 + + +def test_flow_match_explicit_sigmas_without_terminal_zero(): + scheduler = FlowMatchEulerDiscreteScheduler() + pipe = LTXI2VLongMultiPromptPipeline( + scheduler=scheduler, vae=None, text_encoder=None, tokenizer=None, transformer=None + ) + + timesteps = pipe._prepare_timesteps(3, [1.0, 0.5], torch.device("cpu")) + + torch.testing.assert_close(timesteps, scheduler.timesteps)