Skip to content

Support batches in noise scheduler #25

Closed

Description

@jaketae

Firstly, kudos to the HF team for building this awesome library!

I would like to propose some modifications to the DDPM noise scheduler (and potentially other schedulers) to support batching. Based on training_ddpm.py, it appears that the DDPM noise scheduler does not batch forward diffusion steps:

for idx in range(bsz):
noise = torch.randn(clean_images.shape[1:]).to(clean_images.device)
noise_samples[idx] = noise
noisy_images[idx] = noise_scheduler.forward_step(clean_images[idx], noise, timesteps[idx])

Supporting batches should be possible in theory by crudely modifying DDPMScheduler.get_alpha_prod to something like

def get_alpha_prod(self, time_steps):
        mask = (time_steps < 0).float().numpy()
        return torch.from_numpy(self.one * mask + self.alphas_cumprod[time_steps] * (1 - mask))

The referenced line in the training example script would now be reduced to

#  noises = torch.randn(clean_images.shape).to(clean_images.device)
# timesteps = torch.randint(0, noise_scheduler.timesteps, (16, 1, 1, 1)).long()
noise_images = noise_scheduler.forward_step(clean_images, noises, timesteps)

Is there a particular reason why we should avoid batching the forward process? Curious to hear your thoughts!

Activity

  1. patrickvonplaten commented on Jun 22, 2022

    @patrickvonplaten
    Contributor

    Very much agree! @anton-l is working on this right now I think :-)

  2. self-assigned this
    on Jun 22, 2022
  3. anton-l commented on Jun 22, 2022

    @anton-l
    Member

    Yep, much cleaner now :) 848c86c

  4. added a commit that references this issue on Dec 5, 2022
  5. added a commit that references this issue on Mar 1, 2023
  6. added a commit that references this issue on Aug 1, 2025
  7. added a commit that references this issue on Oct 30, 2025
  8. added a commit that references this issue on Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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