Skip to content

FreeInit #6315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Jan 17, 2024
Merged

FreeInit #6315

merged 22 commits into from
Jan 17, 2024

Conversation

a-r-r-o-w
Copy link
Member

@a-r-r-o-w a-r-r-o-w commented Dec 25, 2023

What does this PR do?

Fixes #6165.

Colab

Before submitting

Who can review?

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.

@DN6 @sayakpaul @patrickvonplaten

@a-r-r-o-w
Copy link
Member Author

This PR attempts to add support for FreeInit. I realize that what I currently have isn't really ideal since it mixes up FreeInit-related code to what should ideally only be AnimateDiff, and limits the maintainability as more features are added in the future. However, I think that adding a new FreeInit-specific pipeline would be unnecessary and lead to a lot of redundant code. Let me know what you think would be the best way to add support for it.

@sayakpaul sayakpaul requested a review from DN6 December 25, 2023 04:59
@a-r-r-o-w a-r-r-o-w changed the title freeinit FreeInit Dec 25, 2023
@a-r-r-o-w
Copy link
Member Author

Sharing some results from current code based on this notebook.

Results
AnimateDiff AnimateDiff + FreeInit (method Gaussian) AnimateDiff + FreeInit (method Butterworth)
Realistic Vision
ToonYou

Copy link
Member Author

@a-r-r-o-w a-r-r-o-w left a comment

Choose a reason for hiding this comment

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

FreeInit changes could be added to AnimateDiff+Controlnet and other future pipelines as well.

@HuggingFaceDocBuilderDev

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.

@a-r-r-o-w a-r-r-o-w marked this pull request as ready for review December 28, 2023 00:36
@a-r-r-o-w
Copy link
Member Author

a-r-r-o-w commented Dec 28, 2023

Here's a Drive link to some more results. Also, updated Colab notebook.

Minimal code
import torch
from diffusers import MotionAdapter, AnimateDiffPipeline, DDIMScheduler
from diffusers.utils import export_to_gif
from IPython.display import display, Image

adapter = MotionAdapter.from_pretrained("guoyww/animatediff-motion-adapter-v1-5-2")

# model_id = "zhyemmmm/ToonYou"
model_id = "SG161222/Realistic_Vision_V5.1_noVAE"
pipe = AnimateDiffPipeline.from_pretrained(model_id, motion_adapter=adapter, torch_dtype=torch.float16).to("cuda")
pipe.scheduler = DDIMScheduler.from_pretrained(
    model_id,
    subfolder="scheduler",
    beta_schedule="linear",
    clip_sample=False,
    timestep_spacing="linspace",
    steps_offset=1
)
pipe.enable_vae_slicing()

pipe.enable_free_init(method="gaussian", use_fast_sampling=True)
output = pipe(
    prompt=(
        "masterpiece, bestquality, highlydetailed, ultradetailed, sunset, "
        "orange sky, warm lighting, fishing boats, ocean waves seagulls, "
        "rippling water, wharf, silhouette, serene atmosphere, dusk, evening glow, "
        "golden hour, coastal landscape, seaside scenery"
    ),
    negative_prompt="bad quality, worse quality",
    num_frames=16,
    guidance_scale=7.5,
    num_inference_steps=20,
    generator=torch.Generator("cpu").manual_seed(666),
)
pipe.disable_free_init()

frames = output.frames[0]
export_to_gif(frames, "animation.gif")
display(Image("animation.gif", embed=True))

@DN6 I think this is ready for another review :) I've moved the denoising loop and latent decoding part to separate functions in order to allow freeinit to work more easily and also be able to return intermediate results. Also, at the moment, we have two pipelines that are related to AnimateDiff which could use FreeInit - one being this, and the other being the community AnimateDiff+Controlnet pipeline. Once we're satisfied with the changes here, let's add it to the other pipeline and any future pipelines as well? This can be used in TextToVideoSDPipeline and SVD as well I think.

Maybe some related tests can also be added?

@a-r-r-o-w a-r-r-o-w mentioned this pull request Jan 3, 2024
6 tasks
Copy link
Collaborator

@DN6 DN6 left a comment

Choose a reason for hiding this comment

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

Some small changes requested. But overall it's looking good 👍🏽 Can we add a fast test to check that the outputs are different with freeinit enabled.

a-r-r-o-w and others added 4 commits January 4, 2024 20:01
Co-Authored-By: Dhruv Nair <dhruv.nair@gmail.com>
Co-Authored-By: Dhruv Nair <dhruv.nair@gmail.com>
@a-r-r-o-w a-r-r-o-w requested a review from DN6 January 4, 2024 14:51
@a-r-r-o-w
Copy link
Member Author

@DN6 Requesting a review. I think this should be good to merge, no? I'll open follow up PRs for other animatediff pipelines following this.

@a-r-r-o-w
Copy link
Member Author

@sayakpaul too, in case some other changes from design pov are required, thanks

Copy link
Collaborator

@DN6 DN6 left a comment

Choose a reason for hiding this comment

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

Excellent work @a-r-r-o-w ! I think we just need an update to the AnimateDiff docs (on what FreeInit is and an example snippet) and we can merge this.

Copy link
Collaborator

@DN6 DN6 left a comment

Choose a reason for hiding this comment

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

Nice work! 👍🏽

@DN6 DN6 merged commit 9112028 into huggingface:main Jan 17, 2024
@a-r-r-o-w a-r-r-o-w deleted the freeinit branch January 25, 2024 09:45
AmericanPresidentJimmyCarter pushed a commit to AmericanPresidentJimmyCarter/diffusers that referenced this pull request Apr 26, 2024
* freeinit

* update freeinit implementation based on review

Co-Authored-By: Dhruv Nair <dhruv.nair@gmail.com>

* fix

* another fix

* refactor

* fix timesteps missing bug

* apply suggestions from review

Co-Authored-By: Dhruv Nair <dhruv.nair@gmail.com>

* add test for freeinit

* apply suggestions from review

Co-Authored-By: Dhruv Nair <dhruv.nair@gmail.com>

* refactor

* fix test

* fix tensor not on same device

* update

* remove return_intermediate_results

* fix broken freeinit test

* update animatediff docs

---------

Co-authored-by: Dhruv Nair <dhruv.nair@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FreeInit
4 participants