Skip to content
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

Animatediff Proposal #5413

Merged
merged 56 commits into from
Nov 2, 2023
Merged

Animatediff Proposal #5413

merged 56 commits into from
Nov 2, 2023

Conversation

DN6
Copy link
Collaborator

@DN6 DN6 commented Oct 16, 2023

What does this PR do?

Adds the AnimateDiff model to diffusers.

This PR:

  1. Introduces a dedicated UNetMotionModel and motion blocks that allow using existing 2D UNet models with the Motion Modules introduced in AnimateDiff.

The approach taken by AnimateDiff is slightly different to a 3D UNet. No temporal convs are applied to the inputs and the motion module weights are saved separately from the 2D UNet weights. This is slightly similar to a ControlNet/Adapter approach so IMO it would be better to create a dedicated model and blocks to support this type of functionality, rather than adapt the existing 3D UNets.

  1. Adds a MotionAdapter module that acts as a dedicated container for saving and loading motion module weights from the hub.

Proposed API

# Loading motion module into Pipeline
from diffusers import MotionAdapter, AnimateDiffPipeline

motion_adapter = MotionAdapter.from_pretrained("<path to saved motion modules>")
pipe = AnimateDiffPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", motion_adapter=motion_adapter)

# Calling pipe.unet should return an UNetMotionModel object

# Create brand new UNetMotionModel with all random weights
unet = UNetMotionModel()

# Load from an existing 2D UNet and MotionAdapter
unet2D = UNet2DConditionModel.from_pretrained("...")
motion_adapter = MotionAdapter.from_pretrained("...")

unet_motion = UNetMotionModel.from_unet2d(unet2D, motion_adapter: Optional = None)

# Or load motion module after init
unet_motion.load_motion_modules(motion_adapter)

# Save only motion modules
unet_motion.save_motion_module(<path to save model>, push_to_hub=True)

# Save all weights to a single model repo (Including UNet weights) 
unet_motion.save_pretrained()

# Load fused models (Where the motion weights are saved along with the UNet weights in a single repo)
unet_motion = UNetMotionModel.from_pretrained("<path to model>") 

TODO:

  1. Test forward pass of UNetMotionModel to verify outputs match the original model
  2. Test pipeline outputs for the same prompts defined in the original model repo
  3. Support loading Motion LoRAs
  4. Add documentation
  5. Add tests
  6. Add support for training/fine tuning AnimateDiff models.

Fixes # (issue)

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 DN6 mentioned this pull request Oct 16, 2023
8 tasks
@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Oct 20, 2023

The documentation is not available anymore as the PR was closed or merged.

Copy link
Contributor

@patrickvonplaten patrickvonplaten left a comment

Choose a reason for hiding this comment

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

I've added some final comments. It would be great if you could go over them and then I think we can merge tomorrow!

src/diffusers/models/attention.py Show resolved Hide resolved
src/diffusers/models/embeddings.py Show resolved Hide resolved
src/diffusers/models/transformer_temporal.py Outdated Show resolved Hide resolved

hidden_states = self.proj_in(hidden_states)
encoder_hidden_states = encoder_hidden_states if self.use_cross_attention else None
Copy link
Contributor

Choose a reason for hiding this comment

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

I still don't understand why we need this. If cross_attention_dim is None then why do we have to manually set encodre_hidden_states to None. This looks more like a hacky bug correction. Why do we pass encoder_hidden_states in the first place if we don't have cross attention?

src/diffusers/models/unet_3d_condition.py Outdated Show resolved Hide resolved
src/diffusers/models/unet_motion_model.py Show resolved Hide resolved
@patrickvonplaten patrickvonplaten merged commit 2a8cf8e into main Nov 2, 2023
13 checks passed
@patrickvonplaten
Copy link
Contributor

Great job!

@patrickvonplaten patrickvonplaten deleted the animatediff-model branch November 2, 2023 14:04
@DN6 DN6 restored the animatediff-model branch November 7, 2023 13:29
kashif pushed a commit to kashif/diffusers that referenced this pull request Nov 11, 2023
* draft design

* clean up

* clean up

* clean up

* clean up

* clean up

* clean  up

* clean up

* clean up

* clean up

* update pipeline

* clean up

* clean up

* clean up

* add tests

* change motion block

* clean up

* clean up

* clean up

* update

* update

* update

* update

* update

* update

* update

* update

* clean up

* update

* update

* update model test

* update

* update

* update

* update

* make style

* update

* fix embeddings

* update

* merge upstream

* max fix copies

* fix bug

* fix mistake

* add docs

* update

* clean up

* update

* clean up

* clean up

* fix docstrings

* fix docstrings

* update

* update

* clean  up

* update
yoonseokjin pushed a commit to yoonseokjin/diffusers that referenced this pull request Dec 25, 2023
* draft design

* clean up

* clean up

* clean up

* clean up

* clean up

* clean  up

* clean up

* clean up

* clean up

* update pipeline

* clean up

* clean up

* clean up

* add tests

* change motion block

* clean up

* clean up

* clean up

* update

* update

* update

* update

* update

* update

* update

* update

* clean up

* update

* update

* update model test

* update

* update

* update

* update

* make style

* update

* fix embeddings

* update

* merge upstream

* max fix copies

* fix bug

* fix mistake

* add docs

* update

* clean up

* update

* clean up

* clean up

* fix docstrings

* fix docstrings

* update

* update

* clean  up

* update
AmericanPresidentJimmyCarter pushed a commit to AmericanPresidentJimmyCarter/diffusers that referenced this pull request Apr 26, 2024
* draft design

* clean up

* clean up

* clean up

* clean up

* clean up

* clean  up

* clean up

* clean up

* clean up

* update pipeline

* clean up

* clean up

* clean up

* add tests

* change motion block

* clean up

* clean up

* clean up

* update

* update

* update

* update

* update

* update

* update

* update

* clean up

* update

* update

* update model test

* update

* update

* update

* update

* make style

* update

* fix embeddings

* update

* merge upstream

* max fix copies

* fix bug

* fix mistake

* add docs

* update

* clean up

* update

* clean up

* clean up

* fix docstrings

* fix docstrings

* update

* update

* clean  up

* update
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.

4 participants