Skip to content

Fix: TCDScheduler advances the generator on the final step of a strength-truncated custom schedule - #15301

Open
SIDDARTHAREDDY8 wants to merge 2 commits into
huggingface:mainfrom
SIDDARTHAREDDY8:oss-bhai-2026-11-15
Open

SIDDARTHAREDDY8 wants to merge 2 commits into
huggingface:mainfrom
SIDDARTHAREDDY8:oss-bhai-2026-11-15

Conversation

@SIDDARTHAREDDY8

Copy link
Copy Markdown

Fixes #15288.

Fix for #15288: in TCDScheduler.step, the final-step check now uses len(self.timesteps) - 1 (the actual, possibly strength-truncated schedule) instead of self.num_inference_steps - 1 (the pre-truncation count). One-line change in src/diffusers/schedulers/scheduling_tcd.py. Added regression test test_truncated_custom_timesteps_final_step_preserves_generator in tests/schedulers/test_scheduler_tcd.py covering truncated custom schedules with eta=0.3 across all three prediction types (epsilon, sample, v_prediction): single-step truncation [999,499,19] strength 0.5 (asserts timesteps [19], RNG state unchanged, next draw matches a fresh control generator, prev_sample == pred_noised_sample) and multi-step truncation [999,799,599,399,199] strength 0.6 (asserts only non-final steps consume randomness and subsequent draws match the control). Collision checks clean twice (before build and immediately before push): 0 PRs mention 15288 in body search and the issue timeline has no cross-referenced PRs; reporter twojian only offered a candidate fix in a comment, no PR exists. Branch pushed to SIDDARTHAREDDY8/diffusers oss-bhai-2026-11-15 at commit cbd4676; both files verified byte-identical on the branch via the contents API and the compare against upstream main shows exactly these 2 files, 1 commit ahead. NOTE FOR REVIEWER: push_branch.py could not be used as-is — the fork was stale (main HEAD a3e0b8e, Oct 20) so creating a branch at upstream HEAD 43e3002 returned 404, and merge-upstream sync is blocked because the PAT lacks workflow scope for .github/workflows/benchmark.yml. Pushed instead via a manual Git Data API flow parented on the fork's own main HEAD a3e0b8e. Safe because both changed files are byte-identical between fork HEAD and upstream HEAD (blob SHAs verified), so the PR diff is exactly the fix + test; the PR will show 148 commits behind, which is cosmetic. No PR opened (reviewer gates that).

Tests pass: Issue repro script confirmed the bug on unmodified main (43e3002): RNG state preserved False, next draw [-0.2934, -2.1788, ...] vs control [1.5410, -0.2934, ...], matching the issue log exactly. After the fix the repro passes: RNG state preserved True, next draw matches the control generator exactly, terminal prev_sample unchanged (0.218797...). Repo test suite tests/schedulers/test_scheduler_tcd.py: 35 passed, including the new regression test test_truncated_custom_timesteps_final_step_preserves_generator; the new test FAILS on unmodified code (verified via git stash) and passes with the fix. ruff check and ruff format clean on both changed files. Tests run in a venv with system-site-packages torch 2.14.0+cpu against a shallow clone at upstream HEAD 43e3002.

…ted custom schedule

Signed-off-by: SIDDARTHA REDDY <75976672+SIDDARTHAREDDY8@users.noreply.github.com>

@1AbdullahButt 1AbdullahButt left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I ran the change on CPU against main with a small script that records, for each step() with eta=0.3, whether the generator state moved:

schedule main this PR
num_inference_steps=4 [T, T, T, F] [T, T, T, F]
num_inference_steps=4, strength=0.5 [T, T, T, F] [T, T, T, F]
timesteps=[999, 499, 19], strength=0.5 → [19] [T] [F]

So the fix does what #15288 asks, and the usual num_inference_steps + strength path is unchanged, since there num_inference_steps already matches len(self.timesteps).

One thing worth looking at here or in a follow-up: LCMScheduler.step has the same check (if self.step_index != self.num_inference_steps - 1:), and the same truncated custom schedule advances its generator on both main and this branch ([T] for [19]). TCD was derived from LCM, so applying the same one-line change there, with a matching test, would keep the two in line.

Small nit: the regression test asserts scheduler.num_inference_steps == 3, which pins the pre-truncation count. That is the current behaviour, but if someone later makes num_inference_steps follow the truncated schedule, this test would fail for a reason unrelated to the generator. Asserting only on timesteps and the RNG state would avoid that.

(I made this review with help from Claude Code. I ran the comparison script above; I did not run the full scheduler test suite.)

The regression test for huggingface#15288 already asserts on the truncated
timesteps and the generator state; pinning num_inference_steps to its
pre-truncation value adds nothing. Suggested in review on huggingface#15301.

Signed-off-by: SIDDARTHA REDDY <75976672+SIDDARTHAREDDY8@users.noreply.github.com>
Signed-off-by: Siddartha Reddy Chinthala <siddarthareddychinthala@gmail.com>
@SIDDARTHAREDDY8

Copy link
Copy Markdown
Author

Thanks for validating the fix on CPU. Applied both suggestions: the test no longer pins num_inference_steps (commit 2b042c7), and the LCM follow-up is open as #15364.

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants