-
Notifications
You must be signed in to change notification settings - Fork 6.3k
[@cene555][Kandinsky 3.0] Add Kandinsky 3.0 #5913
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
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
3e2ff83
finalize
patrickvonplaten 69702a6
finalize
patrickvonplaten 233970e
finalize
patrickvonplaten ed509c8
add slow test
patrickvonplaten b75ca86
add slow test
patrickvonplaten 2c2dac8
add slow test
patrickvonplaten 368e70e
Fix more
patrickvonplaten 446a9d4
add slow test
patrickvonplaten b89a387
fix more
patrickvonplaten 97f621e
fix more
patrickvonplaten fe0a4ed
fix more
patrickvonplaten 92711d5
fix more
patrickvonplaten 22dfa36
fix more
patrickvonplaten 5781f73
fix more
patrickvonplaten 20c78cf
fix more
patrickvonplaten efe9a7e
fix more
patrickvonplaten fb37208
fix more
patrickvonplaten 37684a9
Better
patrickvonplaten f16f1c3
Fix more
patrickvonplaten 15c6e85
Fix more
patrickvonplaten 33febd4
add slow test
patrickvonplaten d810bb8
Add auto pipelines
patrickvonplaten fa33cce
add slow test
patrickvonplaten 3fef9ea
Add all
patrickvonplaten 18a542c
add slow test
patrickvonplaten f561034
add slow test
patrickvonplaten c3417ac
add slow test
patrickvonplaten b8c0c13
add slow test
patrickvonplaten 52ec729
add slow test
patrickvonplaten 6a6fd2a
Apply suggestions from code review
patrickvonplaten b1ca9ac
add slow test
patrickvonplaten 0e276a4
add slow test
patrickvonplaten 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
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,24 @@ | ||
<!--Copyright 2023 The HuggingFace Team. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
--> | ||
|
||
# Kandinsky 3 | ||
|
||
TODO | ||
|
||
## Kandinsky3Pipeline | ||
|
||
[[autodoc]] Kandinsky3Pipeline | ||
- all | ||
- __call__ | ||
|
||
## Kandinsky3Img2ImgPipeline | ||
|
||
[[autodoc]] Kandinsky3Img2ImgPipeline | ||
- all | ||
- __call__ |
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,98 @@ | ||
#!/usr/bin/env python3 | ||
import argparse | ||
import fnmatch | ||
|
||
from safetensors.torch import load_file | ||
|
||
from diffusers import Kandinsky3UNet | ||
|
||
|
||
MAPPING = { | ||
"to_time_embed.1": "time_embedding.linear_1", | ||
"to_time_embed.3": "time_embedding.linear_2", | ||
"in_layer": "conv_in", | ||
"out_layer.0": "conv_norm_out", | ||
"out_layer.2": "conv_out", | ||
"down_samples": "down_blocks", | ||
"up_samples": "up_blocks", | ||
"projection_lin": "encoder_hid_proj.projection_linear", | ||
"projection_ln": "encoder_hid_proj.projection_norm", | ||
"feature_pooling": "add_time_condition", | ||
"to_query": "to_q", | ||
"to_key": "to_k", | ||
"to_value": "to_v", | ||
"output_layer": "to_out.0", | ||
"self_attention_block": "attentions.0", | ||
} | ||
|
||
DYNAMIC_MAP = { | ||
"resnet_attn_blocks.*.0": "resnets_in.*", | ||
"resnet_attn_blocks.*.1": ("attentions.*", 1), | ||
"resnet_attn_blocks.*.2": "resnets_out.*", | ||
} | ||
# MAPPING = {} | ||
|
||
|
||
def convert_state_dict(unet_state_dict): | ||
""" | ||
Convert the state dict of a U-Net model to match the key format expected by Kandinsky3UNet model. | ||
Args: | ||
unet_model (torch.nn.Module): The original U-Net model. | ||
unet_kandi3_model (torch.nn.Module): The Kandinsky3UNet model to match keys with. | ||
|
||
Returns: | ||
OrderedDict: The converted state dictionary. | ||
""" | ||
# Example of renaming logic (this will vary based on your model's architecture) | ||
converted_state_dict = {} | ||
for key in unet_state_dict: | ||
new_key = key | ||
for pattern, new_pattern in MAPPING.items(): | ||
new_key = new_key.replace(pattern, new_pattern) | ||
|
||
for dyn_pattern, dyn_new_pattern in DYNAMIC_MAP.items(): | ||
has_matched = False | ||
if fnmatch.fnmatch(new_key, f"*.{dyn_pattern}.*") and not has_matched: | ||
star = int(new_key.split(dyn_pattern.split(".")[0])[-1].split(".")[1]) | ||
|
||
if isinstance(dyn_new_pattern, tuple): | ||
new_star = star + dyn_new_pattern[-1] | ||
dyn_new_pattern = dyn_new_pattern[0] | ||
else: | ||
new_star = star | ||
|
||
pattern = dyn_pattern.replace("*", str(star)) | ||
new_pattern = dyn_new_pattern.replace("*", str(new_star)) | ||
|
||
new_key = new_key.replace(pattern, new_pattern) | ||
has_matched = True | ||
|
||
converted_state_dict[new_key] = unet_state_dict[key] | ||
|
||
return converted_state_dict | ||
|
||
|
||
def main(model_path, output_path): | ||
# Load your original U-Net model | ||
unet_state_dict = load_file(model_path) | ||
|
||
# Initialize your Kandinsky3UNet model | ||
config = {} | ||
|
||
# Convert the state dict | ||
converted_state_dict = convert_state_dict(unet_state_dict) | ||
|
||
unet = Kandinsky3UNet(config) | ||
unet.load_state_dict(converted_state_dict) | ||
|
||
unet.save_pretrained(output_path) | ||
print(f"Converted model saved to {output_path}") | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description="Convert U-Net PyTorch model to Kandinsky3UNet format") | ||
parser.add_argument("--model_path", type=str, required=True, help="Path to the original U-Net PyTorch model") | ||
parser.add_argument("--output_path", type=str, required=True, help="Path to save the converted model") | ||
|
||
args = parser.parse_args() | ||
main(args.model_path, args.output_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
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.
This one's new!