Fix nan sigmas when num_inference_steps=1 with shift_terminal - #14572
Open
promptsmith1990 wants to merge 1 commit into
Open
Fix nan sigmas when num_inference_steps=1 with shift_terminal#14572promptsmith1990 wants to merge 1 commit into
promptsmith1990 wants to merge 1 commit into
Conversation
FlowMatchEulerDiscreteScheduler.stretch_shift_to_terminal computes one_minus_z[-1] / scale_factor, where scale_factor is itself derived from one_minus_z[-1]. With num_inference_steps=1 the single sigma in the schedule is both the first and last point, and before any terminal stretching it is exactly 1.0 (pure noise), so one_minus_z[-1] is 0 and the division degenerates to 0/0, producing nan sigmas and timesteps. Any pipeline configured with shift_terminal (LTX, LTX2, ...) that is run with a single denoising step hits this. The same unguarded `if self.config.shift_terminal:` call-site pattern was copy-pasted into two other schedulers that share the identical stretch_shift_to_terminal implementation, UniPCMultistepScheduler's use_flow_sigmas branch and FlowMatchLCMScheduler, and both reproduce the same nan crash under the same conditions. Fixes this by skipping the stretch when there is only one point in the schedule to stretch, in all three call sites. Multi-step schedules are unaffected and still terminate at shift_terminal as before. Fixes huggingface#14411 Test Plan: pytest tests/schedulers/test_scheduler_flow_match_euler_discrete.py -v pytest tests/schedulers/test_scheduler_unipc.py -k flow_sigmas_single_step -v New test_scheduler_flow_match_euler_discrete.py covers: multi-step still reaches shift_terminal (regression guard), single-step no longer produces nan sigmas/timesteps, and a single scheduler.step() call runs to completion without nan output. A matching regression test was added to test_scheduler_unipc.py for the use_flow_sigmas branch. Also ran utils/check_copies.py and ruff check/format on the changed files (clean) to make sure the stretch_shift_to_terminal "# Copied from" block itself was left untouched. This PR was prepared with AI assistance (Claude Code): the assistant found the bug while investigating issue huggingface#14411, traced the same defect into the two other schedulers, wrote the fix and tests, and ran the verification above. I reviewed the diff and the verification output before submitting. Self-review (against .ai/references/review-rules.md): no blocking issues found. One scope note left for the actual review: the new test_scheduler_flow_match_euler_discrete.py uses a plain unittest.TestCase rather than SchedulerCommonTest, since FlowMatchEulerDiscreteScheduler currently has no dedicated test file at all and adopting the full common-test harness (dummy sample generation, forward-pass parity checks, etc.) for the first time is a separate, larger effort than this bug fix.
promptsmith1990
force-pushed
the
fix/flow-shift-terminal-single-step
branch
from
August 23, 2026 18:43
629de55 to
83e0624
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes #14411.
FlowMatchEulerDiscreteScheduler.stretch_shift_to_terminalcomputesone_minus_z[-1] / scale_factor, wherescale_factoris itself derived fromone_minus_z[-1]. Withnum_inference_steps=1the single sigma in the schedule is both the first and last point, and before any terminal stretching it is exactly1.0(pure noise), soone_minus_z[-1]is0and the division degenerates to0/0, producingnansigmas and timesteps. Any pipeline configured withshift_terminal(LTX, LTX2, ...) run with a single denoising step hits this.While investigating, I found the same unguarded
if self.config.shift_terminal:call-site pattern copy-pasted into two other schedulers that share the identicalstretch_shift_to_terminalimplementation —UniPCMultistepScheduler'suse_flow_sigmasbranch, andFlowMatchLCMScheduler— and both reproduce the samenancrash under the same conditions, so this PR fixes all three call sites the same way: skip the stretch when there's only one point in the schedule to stretch. Multi-step schedules are unaffected and still terminate atshift_terminalas before (covered by a regression test).Testing
FlowMatchEulerDiscreteSchedulerhad no dedicated test file at all despite being used across ~20 pipeline test files, so I addedtests/schedulers/test_scheduler_flow_match_euler_discrete.pycovering: multi-step still reachesshift_terminal(regression guard against the fix disabling stretching generally), single-step no longer producesnansigmas/timesteps, and a singlescheduler.step()call runs to completion withoutnanoutput. A matching regression test was added totest_scheduler_unipc.pyfor theuse_flow_sigmasbranch.Also ran
utils/check_copies.pyandruff check/ruff formaton the changed files (clean) to confirm thestretch_shift_to_terminal# Copied fromblock itself was left untouched — only the calling guard changed at each site.Before submitting
self-reviewskill on the diff?AI disclosure
Self-review notes (against
.ai/references/review-rules.md)Who can review?
@yiyixuxu @dg845 — schedulers