-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Z-Image-Turbo ControlNet #12792
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
hlky
wants to merge
27
commits into
huggingface:main
Choose a base branch
from
hlky:z-image-controlnet
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.
+1,503
−5
Open
Z-Image-Turbo ControlNet #12792
Changes from 16 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
2354fda
init
hlky 1e2009d
passed transformer
hlky 0c30839
ruff
hlky 52f996e
convert passed
hlky 4b446b3
__init__
hlky a1ff390
pipeline example
hlky 7ab347d
ruff
hlky 8cab0c9
pipeline load_image
hlky 8688fa6
t_scale
hlky 9051272
x_pad_token
hlky 0d8c3f1
controlnet_block_samples
hlky f789325
conditioning_scale
hlky 5f8ab7b
self.config
hlky bc72f9c
sample_mode, default controlnet_conditioning_scale
hlky 13b706a
ruff
hlky 728ba02
Merge branch 'main' into z-image-controlnet
hlky 09849a7
ZImageControlTransformer2DModel
hlky f63a5a8
ModuleDict
hlky f9540cb
patchify control_context
hlky 3e472ac
transformer weights
hlky 0e7c643
-enumerate in ZImageTransformer2DModel
hlky 413c7cb
Merge branch 'main' into z-image-controlnet
hlky a00f104
Option 3
hlky a961402
from_single_file
hlky 6e1c218
Remove convert script
hlky 8e7743a
Copied from
hlky c135170
Example
hlky 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,70 @@ | ||
| import argparse | ||
| from contextlib import nullcontext | ||
|
|
||
| import safetensors.torch | ||
| import torch | ||
| from accelerate import init_empty_weights | ||
| from huggingface_hub import hf_hub_download | ||
|
|
||
| from diffusers.models.controlnets.controlnet_z_image import ZImageControlNetModel | ||
| from diffusers.utils.import_utils import is_accelerate_available | ||
|
|
||
|
|
||
| """ | ||
| python scripts/convert_z_image_controlnet_to_diffusers.py \ | ||
| --original_controlnet_repo_id "alibaba-pai/Z-Image-Turbo-Fun-Controlnet-Union" \ | ||
| --filename "Z-Image-Turbo-Fun-Controlnet-Union.safetensors" | ||
| --output_path "z-image-controlnet-hf/" | ||
| """ | ||
|
|
||
|
|
||
| CTX = init_empty_weights if is_accelerate_available else nullcontext | ||
|
|
||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("--original_controlnet_repo_id", default=None, type=str) | ||
| parser.add_argument("--filename", default="Z-Image-Turbo-Fun-Controlnet-Union.safetensors", type=str) | ||
| parser.add_argument("--checkpoint_path", default=None, type=str) | ||
| parser.add_argument("--output_path", type=str) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
|
|
||
| def load_original_checkpoint(args): | ||
| if args.original_controlnet_repo_id is not None: | ||
| ckpt_path = hf_hub_download(repo_id=args.original_controlnet_repo_id, filename=args.filename) | ||
| elif args.checkpoint_path is not None: | ||
| ckpt_path = args.checkpoint_path | ||
| else: | ||
| raise ValueError(" please provide either `original_controlnet_repo_id` or a local `checkpoint_path`") | ||
|
|
||
| original_state_dict = safetensors.torch.load_file(ckpt_path) | ||
| return original_state_dict | ||
|
|
||
|
|
||
| def convert_z_image_controlnet_checkpoint_to_diffusers(original_state_dict): | ||
| converted_state_dict = {} | ||
|
|
||
| converted_state_dict.update(original_state_dict) | ||
|
|
||
| return converted_state_dict | ||
|
|
||
|
|
||
| def main(args): | ||
| original_ckpt = load_original_checkpoint(args) | ||
|
|
||
| control_in_dim = 16 | ||
| control_layers_places = [0, 5, 10, 15, 20, 25] | ||
|
|
||
| converted_controlnet_state_dict = convert_z_image_controlnet_checkpoint_to_diffusers(original_ckpt) | ||
|
|
||
| controlnet = ZImageControlNetModel( | ||
| control_layers_places=control_layers_places, | ||
| control_in_dim=control_in_dim, | ||
| ).to(torch.bfloat16) | ||
| controlnet.load_state_dict(converted_controlnet_state_dict) | ||
| print("Saving Z-Image ControlNet in Diffusers format") | ||
| controlnet.save_pretrained(args.output_path) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main(args) | ||
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.
there is no conversion going on? maybe we could just load from the original repo using from_single_file so we don't need to host this seperately