Skip to content

Bug of some timestep indices cannot be found in index_for_timestep() of scheduling_flow_match_euler_discrete.py #10584

@Liang-ZX

Description

@Liang-ZX

Describe the bug

Description

In the index_for_timestep() method, there's a precision issue when comparing floating-point timesteps with integer schedule_timesteps. The comparison schedule_timesteps == timestep can fail to find matching indices due to floating-point truncation, causing some timestep indices to be missed.

Location: scheduling_flow_match_euler_discrete.py, line 303:

indices = (schedule_timesteps == timestep).nonzero()

Reproduction

import torch
import numpy as np
from diffusers.schedulers.scheduling_flow_match_euler_discrete import FlowMatchEulerDiscreteScheduler

num_train_timesteps = 1000
batch_size = 1

noise_scheduler = FlowMatchEulerDiscreteScheduler(
    num_train_timesteps=num_train_timesteps,
)

device = "cuda:0"
for i in range(100000):
    print(i)
    action_gt = torch.rand(64, 128).to(device)
    noise = torch.randn(
            action_gt.shape, dtype=action_gt.dtype, device=device
        )
    timesteps = torch.randint(
            1, num_train_timesteps,
            (batch_size,), device=device
        ).long()
    
    noisy_action = noise_scheduler.scale_noise(
            sample=action_gt,
            noise=noise,
            timestep=timesteps,
        )

Logs

  File "/anaconda3/envs/rdt/lib/python3.10/site-packages/diffusers/schedulers/scheduling_flow_match_euler_discrete.py", line 175, in scale_noise
    step_indices = [self.index_for_timestep(t, schedule_timesteps) for t in timestep]
  File "/anaconda3/envs/rdt/lib/python3.10/site-packages/diffusers/schedulers/scheduling_flow_match_euler_discrete.py", line 175, in <listcomp>
    step_indices = [self.index_for_timestep(t, schedule_timesteps) for t in timestep]
  File "/anaconda3/envs/rdt/lib/python3.10/site-packages/diffusers/schedulers/scheduling_flow_match_euler_discrete.py", line 292, in index_for_timestep
    return indices[pos].item()
IndexError: index 0 is out of bounds for dimension 0 with size 0

System Info

diffusers version: 0.32.1
Python version: 3.10
OS: Linux / Ubuntu 22

Who can help?

@yiyixuxu

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingstaleIssues that haven't received updates

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions