Skip to content

EulerDiscreteScheduler returns incorrect v-prediction targets for discrete timesteps #15461

Backlog

Description

@mertkayacs

Describe the bug

I found this while reading EulerDiscreteScheduler.get_velocity. It looks up each timestep's position in the descending schedule and uses that position to index alphas_cumprod. At timestep 999 the default schedule reads alphas_cumprod[0]. I plan to fix the lookup to use the discrete training timestep value.

Reproduction

import torch
from diffusers import DDIMScheduler, EulerDiscreteScheduler

config = {
    "beta_start": 0.00085,
    "beta_end": 0.012,
    "beta_schedule": "scaled_linear",
    "prediction_type": "v_prediction",
}
sample = torch.tensor([[1.0, -2.0], [0.5, 3.0]])
noise = torch.tensor([[0.25, 0.5], [-0.75, 1.0]])
timesteps = torch.tensor([999.0, 500.0])
actual = EulerDiscreteScheduler(**config).get_velocity(sample, noise, timesteps)
expected = DDIMScheduler(**config).get_velocity(sample, noise, timesteps.long())
print((actual - expected).abs().max().item())

I get 1.4713691473007202. Both schedulers have the same betas, so the velocity targets should agree.

Context

The SDXL T2I-Adapter training script uses this result as its loss target when the scheduler is configured for v_prediction. I reproduced the mismatch at the scheduler call on CPU with synthetic inputs.

System Info

Diffusers 0.44.0.dev0 at 08d5de3, Python 3.14.5, PyTorch 2.15.0+cpu, Linux x86_64. Single process on CPU.

Activity

  1. varien199x commented on Dec 3, 2026

    @varien199x

    Hey Mert, how's it going.

    I checked EulerDiscreteScheduler in scheduling_euler_discrete.py against DDIMScheduler.get_velocity. In __init__, EulerDiscreteScheduler initializes self.timesteps in descending order (num_train_timesteps - 1 down to 0), so index_for_timestep(999.0) returns index 0 and flips the lookup against self.alphas_cumprod (which runs 0 to num_train_timesteps - 1). Passing timesteps directly as train_t2i_adapter_sdxl.py does hits that inverted lookup.

    When you switch the lookup in get_velocity, there are two non-integer timestep paths in EulerDiscreteScheduler worth checking:

    • With use_karras_sigmas=True, use_exponential_sigmas=True, or use_beta_sigmas=True, _sigma_to_t produces fractional timesteps (and set_timesteps may have fewer than num_train_timesteps entries). Calling alphas_cumprod[timesteps.long()] directly would truncate fractional timesteps, whereas interpolating alphas_cumprod or deriving alpha_cumprod from sigmas[step_indices] after set_timesteps stays exact.
    • With timestep_type == "continuous" and prediction_type == "v_prediction", self.timesteps holds 0.25 * sigma.log(), which goes negative when sigma < 1. Casting those values with .long() would index from the end of alphas_cumprod or go out of bounds instead of recovering sigma = exp(4 * t) and alpha_cumprod = 1 / (sigma**2 + 1).

    Guarding timestep_type == "discrete" (or routing continuous and interpolated timesteps separately) will keep discrete training scripts like SDXL T2I-Adapter working without silently mis-indexing continuous schedules.

  2. mertkayacs commented on Dec 3, 2026

    @mertkayacs
    ContributorAuthor

    Thanks, good points. My fix only uses the timestep value when it is an integer inside the training range. Fractional timesteps from the Karras, exponential and beta sigma schedules keep the current schedule-position lookup, and the continuous v-prediction case is skipped entirely, so nothing gets truncated or indexed from the end. The tests cover a fractional timestep (2.5) and a continuous log-sigma of 0.0. I'll open the PR once a maintainer confirms the approach.

  3. mertkayacs commented on Dec 4, 2026

    @mertkayacs
    ContributorAuthor

    I opened #15491 for this. It only changes the integer timesteps inside the training range, as discussed above.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions