Skip to content

TCDScheduler advances the generator on the final step of a strength-truncated custom schedule #15288

Open

Description

@twojian

Describe the bug

When TCDScheduler uses a non-empty custom timestep schedule truncated by
strength < 1, its actual final step advances the supplied random generator
when eta > 0.

For example, timesteps=[999, 499, 19] with strength=0.5 leaves only [19],
but num_inference_steps remains 3. The final-step check uses the original
step count and enters the noise-sampling branch.

In the tested case, the terminal output stays the same because the noise
coefficient is zero, but the generator state changes. Subsequent draws using
the same generator therefore differ.

I expected the actual final step to leave the generator state unchanged,
consistent with the scheduler's comment that noise is not used on the final
timestep.

A possible fix is to decide whether another step exists using the length of
self.timesteps. I have tested a candidate fix and can provide it for review.

Reproduction

import diffusers
import torch
from diffusers import TCDScheduler

print(f"torch={torch.__version__} diffusers={diffusers.__version__}")
print(f"source={diffusers.__file__}")

scheduler = TCDScheduler(prediction_type="sample")
scheduler.set_timesteps(timesteps=[999, 499, 19], strength=0.5, device="cpu")

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,
)
after = generator.get_state()

next_draw = torch.randn(4, generator=generator, device="cpu")
control_draw = torch.randn(4, generator=control, device="cpu")

print(f"actual timesteps={scheduler.timesteps.tolist()}")
print(f"num_inference_steps={scheduler.num_inference_steps}")
print(f"prev_sample={result.prev_sample.item()}")
print(f"pred_noised_sample={result.pred_noised_sample.item()}")
print(f"RNG state preserved: {torch.equal(before, after)}")
print(f"next draw: {next_draw.tolist()}")
print(f"control:   {control_draw.tolist()}")

assert torch.equal(before, after), "The terminal step advanced the generator"

Context

No response

Logs

Baseline: unmodified Diffusers source at commit
43e30028af5120d8b0868cb6975b9fad39cc4fab

torch=2.9.0+cu126
diffusers=0.44.0.dev0

actual timesteps=[19]
num_inference_steps=3
prev_sample=0.21879726648330688
pred_noised_sample=0.21879726648330688
RNG state preserved: False

next draw: [-0.293428897857666, -2.1787893772125244, 0.5684312582015991, -1.0845223665237427]
control:   [1.5409960746765137, -0.293428897857666, -2.1787893772125244, 0.5684312582015991]

AssertionError: The terminal step advanced the generator

System Info

Platform: Windows 11
Python: 3.12.8
PyTorch: 2.9.0+cu126
Diffusers: 0.44.0.dev0, source checkout at commit 43e3002
Device used for reproduction: CPU

Who can help?

No response

Activity

  1. twojian commented on Nov 14, 2026

    @twojian
    Author

    I tested a candidate fix that checks whether another step exists using the
    actual length of self.timesteps.

    On CPU, I compared it with the baseline across custom and standard schedules,
    three prediction types, both set_alpha_to_one values, three eta values,
    and both return formats. In the 48 truncated-schedule cases with eta > 0,
    the baseline advanced the generator on the terminal step. The candidate fix
    preserved its state. The terminal samples were identical, while subsequent
    random draws differed.

    The candidate patch applies cleanly to commit
    43e30028af5120d8b0868cb6975b9fad39cc4fab. I can prepare a PR with the
    fix and a regression test if the approach looks appropriate.

  2. 1OF1Research commented on Nov 14, 2026

    @1OF1Research

    Confirming on the current release — diffusers 0.43.0, torch 2.14.0+cpu, Linux x86_64 (your report is 0.44.0.dev0 on Windows):

    Your exact repro gives the same result here: actual timesteps [19], num_inference_steps still 3, RNG state preserved: False, and the next draw matches your log exactly ([-0.2934, -2.1788, 0.5684, -1.0845] against control [1.5410, -0.2934, ...]).

    One added measurement, a multi-step schedule, to show where the boundary sits:

    • Custom timesteps [999, 799, 599, 399, 199] at strength 1.0 (no truncation): 5 steps advance the generator by exactly 4 draws — the final step draws nothing, as documented.
    • The same schedule at strength 0.6 (truncated to [599, 399, 199], num_inference_steps still 5): 3 steps advance it by 3 draws — the actual final step draws noise it does not use.

    So the stale count is set in set_timesteps, which truncates the schedule but keeps the pre-truncation num_inference_steps (there is a # TODO: also reset self.num_inference_steps? at that spot), and step() then trusts that count for its final-step check. Deciding finality from len(self.timesteps), as you suggest, fixes both shapes above; only resetting the count after truncation would leave step() trusting a number that a custom schedule can still desync from the schedule itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions