-
Notifications
You must be signed in to change notification settings - Fork 6.5k
[Fix Issue #1197 since 2022] Support pre-trained openai/guided-diffusion (ADM) with minimal code change #6730
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
Open
tongdaxu
wants to merge
3
commits into
huggingface:main
Choose a base branch
from
tongdaxu:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import argparse | ||
| import os | ||
|
|
||
| import torch | ||
| from convert_consistency_to_diffusers import con_pt_to_diffuser | ||
|
|
||
| from diffusers import ( | ||
| UNet2DModel, | ||
| ) | ||
|
|
||
|
|
||
| SMALL_256_UNET_CONFIG = { | ||
| "sample_size": 256, | ||
| "in_channels": 3, | ||
| "out_channels": 6, | ||
| "layers_per_block": 1, | ||
| "num_class_embeds": None, | ||
| "block_out_channels": [128, 128, 128 * 2, 128 * 2, 128 * 4, 128 * 4], | ||
| "attention_head_dim": 64, | ||
| "down_block_types": [ | ||
| "ResnetDownsampleBlock2D", | ||
| "ResnetDownsampleBlock2D", | ||
| "ResnetDownsampleBlock2D", | ||
| "ResnetDownsampleBlock2D", | ||
| "AttnDownBlock2D", | ||
| "ResnetDownsampleBlock2D", | ||
| ], | ||
| "up_block_types": [ | ||
| "ResnetUpsampleBlock2D", | ||
| "AttnUpBlock2D", | ||
| "ResnetUpsampleBlock2D", | ||
| "ResnetUpsampleBlock2D", | ||
| "ResnetUpsampleBlock2D", | ||
| "ResnetUpsampleBlock2D", | ||
| ], | ||
| "resnet_time_scale_shift": "scale_shift", | ||
| "upsample_type": "resnet", | ||
| "downsample_type": "resnet", | ||
| "norm_eps": 1e-06, | ||
| "norm_num_groups": 32, | ||
| } | ||
|
|
||
|
|
||
| LARGE_256_UNET_CONFIG = { | ||
| "sample_size": 256, | ||
| "in_channels": 3, | ||
| "out_channels": 6, | ||
| "layers_per_block": 2, | ||
| "num_class_embeds": None, | ||
| "block_out_channels": [256, 256, 256 * 2, 256 * 2, 256 * 4, 256 * 4], | ||
| "attention_head_dim": 64, | ||
| "down_block_types": [ | ||
| "ResnetDownsampleBlock2D", | ||
| "ResnetDownsampleBlock2D", | ||
| "ResnetDownsampleBlock2D", | ||
| "AttnDownBlock2D", | ||
| "AttnDownBlock2D", | ||
| "AttnDownBlock2D", | ||
| ], | ||
| "up_block_types": [ | ||
| "AttnUpBlock2D", | ||
| "AttnUpBlock2D", | ||
| "AttnUpBlock2D", | ||
| "ResnetUpsampleBlock2D", | ||
| "ResnetUpsampleBlock2D", | ||
| "ResnetUpsampleBlock2D", | ||
| ], | ||
| "resnet_time_scale_shift": "scale_shift", | ||
| "upsample_type": "resnet", | ||
| "downsample_type": "resnet", | ||
| "norm_eps": 1e-06, | ||
| "norm_num_groups": 32, | ||
| } | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser() | ||
|
|
||
| parser.add_argument("--unet_path", default=None, type=str, required=True, help="Path to the unet.pt to convert.") | ||
| parser.add_argument( | ||
| "--dump_path", default=None, type=str, required=True, help="Path to output the converted UNet model." | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| ckpt_name = os.path.basename(args.unet_path) | ||
| print(f"Checkpoint: {ckpt_name}") | ||
|
|
||
| # Get U-Net config | ||
| if "ffhq" in ckpt_name: | ||
| unet_config = SMALL_256_UNET_CONFIG | ||
| else: | ||
| unet_config = LARGE_256_UNET_CONFIG | ||
|
|
||
| unet_config["num_class_embeds"] = None | ||
|
|
||
| converted_unet_ckpt = con_pt_to_diffuser(args.unet_path, unet_config) | ||
|
|
||
| image_unet = UNet2DModel(**unet_config) | ||
| image_unet.load_state_dict(converted_unet_ckpt) | ||
|
|
||
| torch.save(converted_unet_ckpt, args.dump_path) | ||
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
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
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
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
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.
Could you show me the codepath that becomes effective when
resnet_time_scale_shift == "scale_shift"?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.
The convert_adm_to_diffusers.py is just calling functions of con_pt_to_diffuser function in convert_consistency_to_diffusers.py.
First, the resnet_time_scale_shift == "scale_shift" is not a new option set by this PR.
Setting resnet_time_scale_shift == "scale_shift" will pass the argument though UNet2DModel. Through init of UNet2DModel, it will be pass into get_down_block, UNetMidBlock2D and get_up_block, and will be further passed into each building blocks such as ResnetDownsampleBlock2D, ResnetUpsampleBlock2D, AttnDownBlock2D, AttnUpBlock2D. The eventual effect of resnet_time_scale_shift == "scale_shift" will set the class ResnetBlock2D's time_embedding_norm == "scale_shift". And this option effects the resnet's time embedding's shape https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/resnet.py#L283, and the behaviour of time embedding https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/resnet.py#L378.
It has no special effect on UNetMidBlock2D, as I circumvent this problem in https://github.com/tongdaxu/diffusers/blob/main/src/diffusers/models/unets/unet_2d.py#L200.
The resnet_time_scale_shift == "scale_shift" is necessary in model conversion script as the resnet's time embedding's input shape is doubled with resnet_time_scale_shift == "scale_shift".
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.
That makes sense, thanks!