Skip to content

[Bug fix] "previous_timestep()" in DDPM scheduling compatible with "trailing" and "linspace" options - #9384

Merged
yiyixuxu merged 6 commits into
huggingface:mainfrom
AnandK27:AnandK27-lr-prev-timestep-patch
Dec 3, 2024
Merged

[Bug fix] "previous_timestep()" in DDPM scheduling compatible with "trailing" and "linspace" options#9384
yiyixuxu merged 6 commits into
huggingface:mainfrom
AnandK27:AnandK27-lr-prev-timestep-patch

Conversation

@AnandK27

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes the bug of previous_timestep() not giving the right timestep for numbers not a factor of 1000 during "trailing" and "linspace" options. The previous_timestep has to be accruate for the options only during inference and since during inference the timesteps array is available, I just grab the value from there as for custom_timesteps. During training it is going to be 1 less than the current timestep.

(This is simplest solution I can do, if you need calculating the previous timestep for each case without the timesteps array let me know!)

Test Script

importtorchfromdiffusersimportDDPMScheduler# Initialize the DDPM schedulerscheduler=DDPMScheduler.from_pretrained("google/ddpm-celebahq-256")
#test paramsscheduler.config.timestep_spacing='trailing'# or 'linspace'scheduler.set_timesteps(num_inference_steps=7) # any prime number# Set up a dummy pipelinegenerator=torch.manual_seed(0)
# Initialize some dummy latents (normally this would come from your model)latents=torch.randn((1, 3, 64, 64))
# Inference loopfori, tinenumerate(scheduler.timesteps):
# In a real scenario, you'd run your model here to get model_outputmodel_output=torch.randn_like(latents)
# Get the previous timestepprev_timestep=scheduler.previous_timestep(t)
# Print current and previous timestepsprint(f"Step {i}:")
print(f" Current timestep: {t}")
print(f" Previous timestep: {prev_timestep}")
# Scheduler steplatents=scheduler.step(model_output, t, latents, generator=generator).prev_sampleprint("Test complete.")

Output

Step 0:
Current timestep: 999
Previous timestep: 856
Step 1:
Current timestep: 856
Previous timestep: 713
Step 2:
Current timestep: 713
Previous timestep: 570
Step 3:
Current timestep: 570
Previous timestep: 428
Step 4:
Current timestep: 428
Previous timestep: 285
Step 5:
Current timestep: 285
Previous timestep: 142
Step 6:
Current timestep: 142
Previous timestep: -1
Test complete.

Fixes#9261

Before submitting

Who can review?

@yiyixuxu@RochMollero@bghira
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@RochMollero

RochMollero commented Sep 7, 2024 via email

Copy link
Copy Markdown

@AnandK27

Copy link
Copy Markdown
ContributorAuthor

Yeah the change has to be propagated to remaining schedulers. There is also a lot of duplication in schedulers and will need an overhaul to modularize it. Probably the moderators can open issues for these.

@yiyixuxu
yiyixuxu self-requested a review September 9, 2024 16:24
@yiyixuxu

Copy link
Copy Markdown
Collaborator

thanks so much for your PR! will take a look!
all our newer schedulers has a step counter so would not have this issue

@AnandK27

AnandK27 commented Sep 9, 2024

Copy link
Copy Markdown
ContributorAuthor

I will close the PR then

@yiyixuxu

Copy link
Copy Markdown
Collaborator

hey! sorry I just noticed this
it appears my comment #9384 (comment) has been misleading, I only meant that we do not need to apply the change for remaining schedulers (not all of them anyway), this is still very much an issue with DDPM

feel free to re-open this

@AnandK27AnandK27 reopened this Oct 16, 2024
@github-actions

Copy link
Copy Markdown
Contributor

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.

@github-actionsgithub-actionsBot added the stale Issues that haven't received updates label Nov 9, 2024
@yiyixuxuyiyixuxu removed the stale Issues that haven't received updates label Dec 3, 2024
@yiyixuxu
yiyixuxu requested a review from hlkyDecember 3, 2024 03:52
@yiyixuxu

Copy link
Copy Markdown
Collaborator

can you take a look? @hlky

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

hlky
hlky approved these changes Dec 3, 2024

@hlkyhlky left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @AnandK27! 🤗

@yiyixuxu
yiyixuxu merged commit 5effcd3 into huggingface:mainDec 3, 2024
lawrence-cj pushed a commit to lawrence-cj/diffusers that referenced this pull request Dec 4, 2024
…railing" and "linspace" options (huggingface#9384)
* Update scheduling_ddpm.py
* fix copies
---------
Co-authored-by: YiYi Xu <yixu310@gmail.com>
Co-authored-by: hlky <hlky@hlky.ac>
sayakpaul pushed a commit that referenced this pull request Dec 23, 2024
…railing" and "linspace" options (#9384)
* Update scheduling_ddpm.py
* fix copies
---------
Co-authored-by: YiYi Xu <yixu310@gmail.com>
Co-authored-by: hlky <hlky@hlky.ac>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"previous_timestep()" in DDPM scheduling not compatible with "trailing" option. DDIM bugged too

5 participants

@AnandK27@RochMollero@yiyixuxu@HuggingFaceDocBuilderDev@hlky