UniPCMultistepScheduler returns NaN on the last step with final_sigmas_type='zero' (solver_type='bh1' or lower_order_final=False)
Describe the bug
UniPCMultistepScheduler returns a fully-NaN sample on the last step with the default final_sigmas_type="zero" in two configurations:
solver_type="bh1"(the docstring recommendsbh1"for unconditional sampling when steps < 10") - any step count, anysolver_order.lower_order_final=False(anysolver_type) - the docstring describes this as valid for >= 15 steps.
The final sigma is 0, so lambda_t = +inf, h = inf. In multistep_uni_p_bh_update:
bh1:B_h = hh = -inf, and for a first-order steppred_res = 0, so the code evaluatesalpha_t * (-inf) * 0 = NaN.lower_order_final=False: the last predictor step runs atsolver_order, withrk = (lambda_si - lambda_s0) / h = 0, soD1s = (mi - m0) / rk = inf.
DPMSolverMultistepScheduler already forces the last step to first order when final_sigmas_type == "zero"; UniPC has no equivalent guard. The existing UniPC tests all use final_sigmas_type="sigma_min", which hides it (the library default is "zero").
Reproduction
import torch
from diffusers import UniPCMultistepScheduler, DPMSolverMultistepScheduler
def run(s, steps=20):
s.set_timesteps(steps)
x = torch.randn(1, 4, 8, 8, generator=torch.Generator().manual_seed(0))
for i, t in enumerate(s.timesteps):
x = s.step(torch.zeros_like(x), t, x).prev_sample # dummy model output
if not torch.isfinite(x).all():
return f"non-finite at step {i + 1}/{len(s.timesteps)}"
return "finite"
print("bh1 default :", run(UniPCMultistepScheduler(solver_type="bh1")))
print("unipc lof=False :", run(UniPCMultistepScheduler(lower_order_final=False)))
print("dpm lof=False :", run(DPMSolverMultistepScheduler(lower_order_final=False)))
Output on current main (f9316b0d, CPU, torch 2.15):
bh1 default : non-finite at step 20/20
unipc lof=False : non-finite at step 20/20
dpm lof=False : finite
Same for 3, 5, 9, 20 steps with bh1 (epsilon, v_prediction, sample).
Context
Found by a differential sweep of step() over solver/scheduler variants (an AI agent - Claude Code - wrote and ran the sweep; I did not hit this in a production pipeline, only in the scheduler loop with a dummy model). Impact: with the documented bh1 recommendation the final denoised latent is NaN (black image) unless the user also sets final_sigmas_type="sigma_min".
Possible fix (not yet a PR - waiting for maintainer ack per the AI contribution rules)
Two small changes in scheduling_unipc_multistep.py:
- in
step, forcethis_order = 1on the last step whenfinal_sigmas_type == "zero"; - in
multistep_uni_p_bh_update, only apply theB_h * pred_resterm whenD1s is not None(mathematically identical, avoidsinf * 0).
Plus a regression test (bh1/bh2 x lower_order_final True/False with final_sigmas_type="zero"). I have this prepared locally; after it the repro above is finite and the UniPC loop matches DPMSolverMultistepScheduler bit-for-bit for lower_order_final=False; the scheduler test file passes (64 passed) and ruff is clean.
Out of scope here: predict_x0=False with final_sigmas_type="zero" also yields NaN (0 * expm1(inf)), but that combination looks inherently unsupported.
System Info
diffusers 0.42.0.dev0 (main f9316b0d), Python 3.x, torch 2.15.0+cpu, Windows 10, CPU only.
Independent confirmation on the shipped release (diffusers 0.43.0, torch 2.14.0+cpu, CPU), running the report above verbatim plus extra step counts and controls:
| configuration | result |
|---|---|
bh1, defaults, 20 steps |
non-finite at step 20/20 |
bh1, defaults, 3 / 5 / 9 steps |
non-finite at the final step, every count |
lower_order_final=False, defaults, 20 steps |
non-finite at step 20/20 |
bh2, defaults, 20 steps |
finite |
bh1 with final_sigmas_type="sigma_min" |
finite |
DPMSolverMultistepScheduler, lower_order_final=False |
finite |
The failure needs exactly the two ingredients named in the report — a final sigma of 0 under the default final_sigmas_type="zero", reached through the bh1 path or a non-lowered final order — and the controls isolate it: the same scheduler is fine with sigma_min, bh2 defaults are fine, and the DPM sibling, which already forces first order on the final step, is fine.
The proposed direction (force first order on the last step when final_sigmas_type == "zero", and apply the B_h * pred_res term only when D1s is not None) matches that DPM precedent, and the table gives it a direct regression target: every row finite, with the currently finite rows unchanged.