TCDScheduler advances the generator on the final step of a strength-truncated custom schedule
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 43e30028af5120d8b0868cb6975b9fad39cc4fab Device used for reproduction: CPU
Who can help?
No response
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.
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_stepsstill 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.
I found an additional RNG-consumption boundary while reviewing #15301. The existing PR correctly fixes the original strength-truncated terminal step, but a non-terminal step can also sample noise with an exactly zero coefficient.
For example:
import torch
from diffusers import TCDScheduler
scheduler = TCDScheduler(prediction_type="sample", original_inference_steps=1000)
scheduler.set_timesteps(timesteps=[999, 0], strength=1.0, device="cpu")
g = torch.Generator(device="cpu").manual_seed(0)
sample = torch.ones((1, 1))
before = g.get_state().clone()
result = scheduler.step(torch.zeros_like(sample), scheduler.timesteps[0], sample, eta=0.3, generator=g)
print("generator unchanged:", torch.equal(before, g.get_state()))
print("output equals predicted noised sample:", torch.equal(result.prev_sample, result.pred_noised_sample))
On the first (non-terminal) step, prev_timestep=0 and timestep_s=floor((1-eta)*0)=0, so alpha_prod_t_prev/alpha_prod_s=1, and the noise coefficient is exactly zero. The final-step guard in #15301 nevertheless draws random noise. A second related boundary occurs for very small positive eta values when the computed integer timestep_s is unchanged from prev_timestep.
I checked these coefficient and RNG branches using an isolated PyTorch CPU calculation. I have not yet run the complete Diffusers regression suite for the proposed extension.
One potential unified guard is prev_step_index < len(self.timesteps) and timestep_s < prev_timestep, subject to regression tests and a device-sync check. This keeps the original truncated-final-step fix and avoids drawing provably unused noise for the integer-timestep cases above.
Since #15301 is already open, I'd prefer to coordinate on one comprehensive PR rather than submit overlapping fixes. Would the maintainers and current PR author prefer extending #15301 or accepting a consolidated alternative with the expanded regression coverage?