-
Notifications
You must be signed in to change notification settings - Fork 6.6k
[WIP ] Add AnimateDiff SparseCntrl #6289
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
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2a6a996
update
DN6 ed5360d
update
DN6 79dae12
Merge branch 'main' into animatediff-v3
DN6 7d2498d
update
DN6 7565fee
update
DN6 918a70b
update
DN6 e789459
update
DN6 06e3dc0
update
DN6 b46e167
update
DN6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/diffusers/pipelines/animatediff/convert_motion_loras_to_diffusers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import argparse | ||
|
|
||
| import torch | ||
| from safetensors.torch import save_file | ||
|
|
||
|
|
||
| def convert_motion_module(original_state_dict): | ||
| converted_state_dict = {} | ||
| for k, v in original_state_dict.items(): | ||
| if "pos_encoder" in k: | ||
| continue | ||
|
|
||
| else: | ||
| converted_state_dict[ | ||
| k.replace(".norms.0", ".norm1") | ||
| .replace(".norms.1", ".norm2") | ||
| .replace(".ff_norm", ".norm3") | ||
| .replace(".attention_blocks.0", ".attn1") | ||
| .replace(".attention_blocks.1", ".attn2") | ||
| .replace(".temporal_transformer", "") | ||
| ] = v | ||
|
|
||
| return converted_state_dict | ||
|
|
||
|
|
||
| def get_args(): | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("--ckpt_path", type=str, required=True) | ||
| parser.add_argument("--output_path", type=str, required=True) | ||
|
|
||
| return parser.parse_args() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| args = get_args() | ||
|
|
||
| state_dict = torch.load(args.ckpt_path, map_location="cpu") | ||
|
|
||
| if "state_dict" in state_dict.keys(): | ||
| state_dict = state_dict["state_dict"] | ||
|
|
||
| conv_state_dict = convert_motion_module(state_dict) | ||
|
|
||
| # convert to new format | ||
| output_dict = {} | ||
| for module_name, params in conv_state_dict.items(): | ||
| if type(params) is not torch.Tensor: | ||
| continue | ||
| output_dict.update({f"unet.{module_name}": params}) | ||
|
|
||
| save_file(output_dict, f"{args.output_path}/diffusion_pytorch_model.safetensors") |
51 changes: 51 additions & 0 deletions
51
src/diffusers/pipelines/animatediff/convert_motion_module_ckpt_to_diffusers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import argparse | ||
|
|
||
| import torch | ||
|
|
||
| from diffusers import MotionAdapter | ||
|
|
||
|
|
||
| def convert_motion_module(original_state_dict): | ||
| converted_state_dict = {} | ||
| for k, v in original_state_dict.items(): | ||
| if "pos_encoder" in k: | ||
| continue | ||
|
|
||
| else: | ||
| converted_state_dict[ | ||
| k.replace(".norms.0", ".norm1") | ||
| .replace(".norms.1", ".norm2") | ||
| .replace(".ff_norm", ".norm3") | ||
| .replace(".attention_blocks.0", ".attn1") | ||
| .replace(".attention_blocks.1", ".attn2") | ||
| .replace(".temporal_transformer", "") | ||
| ] = v | ||
|
|
||
| return converted_state_dict | ||
|
|
||
|
|
||
| def get_args(): | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("--ckpt_path", type=str, required=True) | ||
| parser.add_argument("--output_path", type=str, required=True) | ||
| parser.add_argument("--use_motion_mid_block", action="store_true") | ||
| parser.add_argument("--motion_max_seq_length", type=int, default=32) | ||
|
|
||
| return parser.parse_args() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| args = get_args() | ||
|
|
||
| state_dict = torch.load(args.ckpt_path, map_location="cpu") | ||
| if "state_dict" in state_dict.keys(): | ||
| state_dict = state_dict["state_dict"] | ||
|
|
||
| conv_state_dict = convert_motion_module(state_dict) | ||
| adapter = MotionAdapter( | ||
| use_motion_mid_block=args.use_motion_mid_block, motion_max_seq_length=args.motion_max_seq_length | ||
| ) | ||
| # skip loading position embeddings | ||
| adapter.load_state_dict(conv_state_dict, strict=False) | ||
| adapter.save_pretrained(args.output_path) | ||
| adapter.save_pretrained(args.output_path, variant="fp16") | ||
49 changes: 49 additions & 0 deletions
49
src/diffusers/pipelines/animatediff/convert_sparse_cntrl_to_diffusers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import argparse | ||
|
|
||
| import torch | ||
|
|
||
| from diffusers.models import SparseControlNetModel | ||
|
|
||
|
|
||
| def convert_sparse_cntrl_module(original_state_dict): | ||
| converted_state_dict = {} | ||
| for k, v in original_state_dict.items(): | ||
| if "pos_encoder" in k: | ||
| continue | ||
|
|
||
| else: | ||
| converted_state_dict[ | ||
| k.replace(".norms.0", ".norm1") | ||
| .replace(".norms.1", ".norm2") | ||
| .replace(".ff_norm", ".norm3") | ||
| .replace(".attention_blocks.0", ".attn1") | ||
| .replace(".attention_blocks.1", ".attn2") | ||
| .replace(".temporal_transformer", "") | ||
| ] = v | ||
|
|
||
| return converted_state_dict | ||
|
|
||
|
|
||
| def get_args(): | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("--ckpt_path", type=str, required=True) | ||
| parser.add_argument("--output_path", type=str, required=True) | ||
| parser.add_argument("--motion_max_seq_length", type=int, default=32) | ||
|
|
||
| return parser.parse_args() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| args = get_args() | ||
|
|
||
| state_dict = torch.load(args.ckpt_path, map_location="cpu") | ||
| if "state_dict" in state_dict.keys(): | ||
| state_dict = state_dict["state_dict"] | ||
|
|
||
| conv_state_dict = convert_sparse_cntrl_module(state_dict) | ||
| controlnet = SparseControlNetModel() | ||
|
|
||
| # skip loading position embeddings | ||
| controlnet.load_state_dict(conv_state_dict, strict=False) | ||
| controlnet.save_pretrained(args.output_path) | ||
| controlnet.save_pretrained(args.output_path, variant="fp16") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For fp16 variant, we should also set the dtype to
torch.float16. I don't see that being used. This issue persists here too: https://huggingface.co/guoyww/animatediff-motion-adapter-v1-5-2/tree/main.Both the fp16 and non-fp16 variants have the same size which shouldn't be the case.
Am I missing out on anything?