PNDMScheduler mislabels timesteps when img2img starts part-way through the schedule
Les mainteneurs répondent en général sous 1 jour
@shravansumanthanan y travaille déjà.
Depuis le 29/11/2026.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 62/100
- Type d'issue
- Bug
- Clarté
- Clairement spécifiée
- Activité
- Active
- Domaine
- machine-learning
Piste de recherche
Start with PNDMScheduler in the diffusers scheduler implementation, focusing on timesteps, set_begin_index, and step_plms. Trace how img2img, inpaint, and depth2img pipelines slice scheduler.timesteps and how the scheduler call counter selects correction steps. Reproduce with test_stable_diffusion_img2img_default_case and the supplied img2img script; the fix should make partially started schedules use timestep positions consistently and eliminate the reported latent error.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Describe the bug
With skip_prk_steps=True (the SD 1.x and SD 2 default), PNDMScheduler.timesteps repeats the second timestep: t0, t1, t1, t2, .... step_plms uses its call counter to treat the second call as a correction of the first step, and the third call as a new step from t1. The img2img, inpaint and depth2img pipelines pass scheduler.timesteps[t_start:], which drops the repeat, but the counter logic stays the same. From the third call on, the sample is one inference step noisier than the timestep passed with it, and the sampler drops to first order.
The worst case is a short run that starts at the repeated entry. With 2 steps and strength 0.8 the timesteps are [1, 1], so the second call runs a 501-to-1 correction on a latent that is already at t=1. The output latent is then off by about its own size. The fast test test_stable_diffusion_img2img_default_case uses this setting. At 25 to 100 steps the error is smaller, but still several times larger than it should be.
Reproduction
The reference solves the same ODE from the same starting latent, with deterministic DDIM steps of one training timestep. A stride-2 solve differs from it by about 0.003 RMS, which is small next to the errors below.
import torch
from diffusers import StableDiffusionImg2ImgPipeline
from diffusers.utils import load_image
torch.backends.cuda.matmul.allow_tf32 = False
torch.backends.cudnn.allow_tf32 = False
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
"stable-diffusion-v1-5/stable-diffusion-v1-5", variant="fp16", torch_dtype=torch.float32, safety_checker=None
).to("cuda")
init = load_image(
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png"
).resize((512, 512))
prompt, gs = "a fantasy landscape, trending on artstation", 7.5
sched = pipe.scheduler # PNDMScheduler, skip_prk_steps=True
pe, ne = pipe.encode_prompt(prompt, "cuda", 1, True)
emb = torch.cat([ne, pe])
@torch.no_grad()
def reference(x, t0):
# Same ODE from the same starting latent: DDIM with steps of one training timestep, down to alphas_cumprod[0],
# where PNDM's last step lands (set_alpha_to_one=False).
ac = sched.alphas_cumprod.to("cuda", torch.float64)
x = x.double()
for t in range(t0, 0, -1):
out = pipe.unet(torch.cat([x, x]).float(), t, encoder_hidden_states=emb).sample.double()
u, c = out.chunk(2)
eps = u + gs * (c - u)
a, a_prev = ac[t], ac[t - 1]
x0 = (x - (1 - a).sqrt() * eps) / a.sqrt()
x = a_prev.sqrt() * x0 + (1 - a_prev).sqrt() * eps
return x.float()
for steps, strength in [(50, 0.5), (2, 0.8)]:
seen = []
step = sched.step
sched.step = lambda out, t, sample, **kw: (seen.append((int(t), sample.clone())), step(out, t, sample, **kw))[1]
latents = pipe(prompt, image=init, strength=strength, num_inference_steps=steps, guidance_scale=gs,
generator=torch.Generator("cuda").manual_seed(0), output_type="latent").images
del sched.step
ref = reference(seen[0][1], seen[0][0])
print(f"steps={steps} strength={strength} timesteps={[t for t, _ in seen][:4]}... "
f"RMS error vs reference: {(latents - ref).pow(2).mean().sqrt():.3f} (latent RMS {ref.pow(2).mean().sqrt():.2f})")
Output on 0.43.0:
steps=50 strength=0.5 timesteps=[501, 481, 461, 441]... RMS error vs reference: 0.132 (latent RMS 1.04)
steps=2 strength=0.8 timesteps=[1, 1]... RMS error vs reference: 1.103 (latent RMS 1.02)
With a local fix (give PNDMScheduler a set_begin_index, which these pipelines already call when it exists, and pick the correction step by position in self.timesteps instead of by call count) the same script prints 0.034 and 0.000. For 2 steps the reference is one DDIM step from t=1, which is also what the fixed scheduler does, so the 0.000 is expected there.
Context
More settings from the same setup. Latent RMS error against the reference, and mean absolute pixel error (0 to 255) of the decoded images:
| steps | strength | latent, 0.43.0 | latent, local fix | pixel, 0.43.0 | pixel, local fix |
|---|---|---|---|---|---|
| 50 | 0.8 | 0.116 | 0.026 | 4.36 | 0.91 |
| 50 | 0.5 | 0.132 | 0.034 | 4.41 | 1.07 |
| 25 | 0.5 | 0.185 | 0.090 | 6.28 | 2.71 |
| 10 | 0.5 | 0.375 | 0.182 | 16.9 | 7.00 |
| 5 | 0.8 | 0.419 | 0.316 | 15.8 | 13.5 |
| 4 | 0.8 | 0.477 | 0.244 | 15.7 | 9.05 |
| 3 | 0.6 | 0.235 | 0.235 | 9.81 | 7.91 |
| 2 | 0.8 | 1.103 | 0.000 | 24.9 | 0.00 |
Supporting check with Gaussian data, where the noise prediction and the exact ODE result are known in closed form (SD 1.5 scheduler config, strength 0.5). Going from 20 to 320 steps, each doubling cuts the error by 1.89, 1.78, 1.85 and 1.95 on 0.43.0, which is first order. With the local fix the ratios are 3.41 and 7.42, then the error stops at about 1.3e-4.
Logs
System Info
diffusers 0.43.0, torch 2.11.0+cu128, transformers 5.24.1, Python 3.14.7, Windows 11, RTX 5090, fp32 with TF32 off.
Who can help?
@yiyixuxu
- Langage dominant
- Python
- Étoiles
- 35k
- Forks
- 7.5k
- Merge moyen
- 2 j 22 h
- PR mergées (30 j)
- 58
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de huggingface/diffusers
-
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
huggingface/diffusers#15436 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
InpaintProcessor crashes when padding_mask_crop is set and a dimension is omittedPeut-être pris Une pull request liée à cette issue est ouverte ou déjà fusionnée. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
huggingface/diffusers#15433 ·
Les mainteneurs répondent en général sous 1 jour
-
LTXEulerAncestralRFScheduler skips signal scaling with s_noise=0Peut-être pris @hparik11 l’a pris il y a 3 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 74/100
huggingface/diffusers#15423 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
Docs: 13 docstrings document parameters their functions don't havePeut-être pris @M-Colley l’a pris il y a 3 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 84/100
huggingface/diffusers#15363 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
`ZImageImg2ImgPipeline` / `ZImageInpaintPipeline`: explicit height/width different from the input image raises RuntimeErrorPeut-être pris @chessing234 l’a pris il y a 11 jours. Ouvertebug
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
huggingface/diffusers#15337 · 3 commentaires ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de huggingface/diffusers
Issues similaires
-
[19.0] pos_sale: settling an order with a 0 down payment line crashes (reading 'reward_id') and leaves a wrong amount duePeut-être pris Une pull request liée à cette issue est ouverte ou déjà fusionnée. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 79/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 79/100
developmentseed/titiler-covjson#164 ·
Les mainteneurs répondent en général sous 1 jour
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
awslabs/visual-asset-management-system#2164 ·
Les mainteneurs répondent en général sous 1 jour
-
needs-triage
Difficulté 2/5 1-3 heures Accessibilité débutants 86/100
MakazhanAlpamys/Soup#3589 ·
Les mainteneurs répondent en général sous 1 jour
-
channels status reports the wrong Enabled value for Feishu instances and default-enabled WebSocketOuverte
Difficulté 2/5 1-3 heures Accessibilité débutants 86/100
Les mainteneurs répondent en général sous 1 jour