diff --git a/src/diffusers/schedulers/scheduling_tcd.py b/src/diffusers/schedulers/scheduling_tcd.py index 951a34b1f1cb..fd85052c2d49 100644 --- a/src/diffusers/schedulers/scheduling_tcd.py +++ b/src/diffusers/schedulers/scheduling_tcd.py @@ -678,7 +678,7 @@ def step( # Eta (referred to as "gamma" in the paper) was introduced to control the stochasticity in every step. # When eta = 0, it represents deterministic sampling, whereas eta = 1 indicates full stochastic sampling. if eta > 0: - if self.step_index != self.num_inference_steps - 1: + if self.step_index != len(self.timesteps) - 1: noise = randn_tensor( model_output.shape, generator=generator, device=model_output.device, dtype=pred_noised_sample.dtype ) diff --git a/tests/schedulers/test_scheduler_tcd.py b/tests/schedulers/test_scheduler_tcd.py index e95c536c7f0a..8944473705d9 100644 --- a/tests/schedulers/test_scheduler_tcd.py +++ b/tests/schedulers/test_scheduler_tcd.py @@ -178,3 +178,55 @@ def test_custom_timesteps_too_large(self): msg="`timesteps` must start before `self.config.train_timesteps`: {scheduler.config.num_train_timesteps}}", ): scheduler.set_timesteps(timesteps=timesteps) + + def test_truncated_custom_timesteps_final_step_preserves_generator(self): + # Regression test for https://github.com/huggingface/diffusers/issues/15288: + # `strength < 1` truncates a custom timestep schedule, but `num_inference_steps` + # keeps the pre-truncation count, so the actual final step used to sample noise + # (and advance the generator) even though that noise is never used. + for prediction_type in ["epsilon", "sample", "v_prediction"]: + scheduler_class = self.scheduler_classes[0] + scheduler_config = self.get_scheduler_config(prediction_type=prediction_type) + scheduler = scheduler_class(**scheduler_config) + + # Single-step truncation: [999, 499, 19] with strength=0.5 leaves only [19]. + scheduler.set_timesteps(timesteps=[999, 499, 19], strength=0.5) + + self.assertEqual(scheduler.timesteps.tolist(), [19]) + + generator = torch.Generator(device="cpu").manual_seed(0) + control = torch.Generator(device="cpu").manual_seed(0) + sample = torch.ones((1, 1), device="cpu") + + before = generator.get_state().clone() + result = scheduler.step( + torch.zeros_like(sample), scheduler.timesteps[0], sample, eta=0.3, generator=generator + ) + + # The terminal step must not consume randomness. + self.assertTrue(torch.equal(before, generator.get_state())) + self.assertTrue(torch.equal(torch.randn(4, generator=generator), torch.randn(4, generator=control))) + # Noise is not used on the final step, so the output is unchanged. + self.assertEqual(result.prev_sample, result.pred_noised_sample) + + # Multi-step truncation: [999, 799, 599, 399, 199] with strength=0.6 + # leaves [599, 399, 199]. Only the non-final steps consume randomness. + scheduler.set_timesteps(timesteps=[999, 799, 599, 399, 199], strength=0.6) + + self.assertEqual(scheduler.timesteps.tolist(), [599, 399, 199]) + + generator = torch.Generator(device="cpu").manual_seed(0) + control = torch.Generator(device="cpu").manual_seed(0) + sample = torch.ones((1, 1), device="cpu") + + for i, t in enumerate(scheduler.timesteps): + before = generator.get_state().clone() + scheduler.step(torch.zeros_like(sample), t, sample, eta=0.3, generator=generator) + if i == len(scheduler.timesteps) - 1: + self.assertTrue(torch.equal(before, generator.get_state())) + else: + self.assertFalse(torch.equal(before, generator.get_state())) + # Each non-final step draws one noise tensor of the model output shape. + torch.randn(1, 1, generator=control) + + self.assertTrue(torch.equal(torch.randn(4, generator=generator), torch.randn(4, generator=control)))