-
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
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.