-
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
base: main
Are you sure you want to change the base?
Z-Image-Turbo ControlNet #12792
Conversation
|
|
||
| def forward( | ||
| self, | ||
| transformer: ZImageTransformer2DModel, |
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.
let's not pass transformer as an input
given that there are some shared layers, we can consider these two alternative design:
- option1: pre-computed shared stuff inside the pipeline, you can add a method to the
ZImageTransformer2DModelto be used by controlnet if it makes things easier (but no need to change the transformer code) e.g. inside pipeline
... = self.transformer.prepare_inputs(...)
controlnet_block_samples = self.controlnet(control_image=control_image, ...) - Option2: we can try to inject controlnet into transformer inside
__init__. similar to https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/animatediff/pipeline_animatediff.py#L140. Basically create a model that combine controlnet + transformer
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.
Which option would you prefer?
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.
let's try option 2
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.
Done
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.
Hi @hlky , I managed to get your PR working locally, but I modified it to work differently. I unified the weights of the base model's transformer with the transformer from the controlnet model. Initially, I did this because I wanted to generate a unified gguf, since when you load the gguf from_single_file you wouldn't be able to load two transformers at the same time. So I unified them and tested with a single gguf and also with a single pretrained model, and it worked. If this is an easier strategy for users, it will only be necessary to upload a different model to Spaces, such as z-image-turbo-control-hf for e.g.
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.
Hi @elismasilva, I see, currently if we used gguf for the transformer and controlnet this would not be applied to the combined model when it's created in the pipeline __init__, I think we can detect the quantization_config and pass that along to the combined model which should fix that, we can also add from_single_file support, a conversion script, etc. if users prefer to have a single weight file, but I did find that injecting the controlnet in __init__ as suggested by @yiyixuxu has increased cpu ram usage from creating the combined transformer+controlnet while the non-combined versions are also loaded, so I wonder if we should go for option 1 instead, we can still add from_single_file support to the controlnet for gguf in that case. Let's wait for some more input from YiYi on how best to proceed.
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.
I think the init technique in this case is only valid if the person is going to use the pipeline as a whole. Consider that the ControlNet transformer is not an independent model like those in SDXL, for example; it's an additional weighting of the transformer. So I believe it should be the transformer model's responsibility to append these weights, in case someone tries to load the transformer model in isolation, as is possible, and also considering modular diffusers.
|
How do we support control modes other than Canny? Other control modes still produce poor results—here’s what I got when I tried the HED example from the demo with prompt “A man holding a bottle” and input image. |
Hi, I ran a test with your image and got the following result:Result 1: Result 2:
To achieve a realistic effect, you will need to apply the Hires.Fix technique to the image after it has been generated: |







What does this PR do?
In the original code this is not a typical ControlNet, it is integrated into the
transformerand relies on operations performed in thetransformer's forward. In this PR we implement it as a typical ControlNet by duplicating the necessary operations from thetransformer's forward into the ControlNet's forward and passtransformertoZImageControlNetModel'sforwardto access the necessarytransformermodules, as a result this is perhaps a little slower than the original implementation, but it keeps things clean and in style.ZImageTransformer2DModelhas minimal changes,controlnet_block_samplesis introduced, this is aDict[int, torch.Tensor]returned fromZImageControlNetModelwhere theintis theZImageTransformer2DModellayersindex, this is another difference from typical ControlNet where every block has the ControlNet output applied.ZImageControlNetPipelinehas minimal changes, compared toZImagePipelineit addsprepare_imagefunction, addscontrol_imageandcontrolnet_conditioning_scaleparameters, prepares and encodescontrol_imageand callscontrolnetto obtaincontrolnet_block_sampleswhich are passed totransformer.control_guidance_start/control_guidance_endis not yet implemented.Test code
Output
Fixes #12769
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.