-
Notifications
You must be signed in to change notification settings - Fork 6.4k
The Modular Diffusers #9672
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
+16,691
−42
Merged
The Modular Diffusers #9672
Changes from 183 commits
Commits
Show all changes
188 commits
Select commit
Hold shift + click to select a range
33f85fa
add
yiyixuxu 52a7f1c
add dataflow info for each block in builder _repr_
yiyixuxu e8d0980
add img2img support - output does not match with non-modular pipeline…
yiyixuxu ad3f9a2
update img2img, result match
yiyixuxu ddea157
add from_pipe + run_blocks
yiyixuxu af9572d
controlnet
yiyixuxu 2b6dcbf
fix controlnet
yiyixuxu 70272b1
combine controlnetstep into contronetdesnoisestep
yiyixuxu 46ec174
refactor guider, remove prepareguidance step to be combinedd into den…
yiyixuxu f1b3036
update pag guider - draft
yiyixuxu 540d303
refactor guider
yiyixuxu 6742f16
up
yiyixuxu 005195c
add
yiyixuxu 024a9f5
fix so that run_blocks can work with inputs in the state
yiyixuxu 37e8dc7
remove img2img blocksgit status consolidate text2img and img2img
yiyixuxu 8b811fe
refactor, from_pretrained, from_pipe, remove_blocks, replace_blocks
yiyixuxu c70a285
style
yiyixuxu ffc2992
add autostep (not complete)
yiyixuxu ace53e2
update/refactor
yiyixuxu a8df0f1
Modular APG (#10173)
hlky e50d614
only add model as expected_component when the model need to run for t…
yiyixuxu bc3d1c9
add model_cpu_offload_seq + _exlude_from_cpu_offload
yiyixuxu 2b3cd2d
update
yiyixuxu b305c77
add offload support!
yiyixuxu 0b90051
add vae encoder node
yiyixuxu 806e8e6
Merge branch 'main' into modular-diffusers
yiyixuxu 4fa85c7
add model_manager and global offloading method
yiyixuxu 72d9a81
components manager
yiyixuxu 10d4a77
style
yiyixuxu 27dde51
add output arg to run_blocks
yiyixuxu 8c02572
add memory_reserve_margin arg to auto offload
yiyixuxu a09ca7f
refactors: block __init__ no longer accept args. remove update_state…
yiyixuxu ed59f90
modular pipeline builder -> ModularPipeline
yiyixuxu 72c5bf0
add a from_block class method to modular pipeline
yiyixuxu 6c93626
remove run_blocks, just use __call__
yiyixuxu 1d63306
make it work with lora
yiyixuxu 2e0f5c8
start to add inpaint
yiyixuxu c12a05b
update to to not assume pipeline has hf_device_map
yiyixuxu 54f410d
add inpaint
yiyixuxu 6985906
controlnet input & remove the MultiPipelineBlocks class
yiyixuxu db94ca8
add controlnet inpaint + more refactor
yiyixuxu e973de6
fix contro;net inpaint preprocess
yiyixuxu 7a34832
[modular] Stable Diffusion XL ControlNet Union (#10509)
hlky 2220af6
refactor
yiyixuxu fb78f4f
Merge branch 'modular-diffusers' of github.com:huggingface/diffusers …
yiyixuxu 0966663
adjust print
yiyixuxu 7f897a9
fix
yiyixuxu a6804de
add controlnet union to auto & fix for pag
yiyixuxu 7007f72
InputParam, OutputParam, get_auto_doc
yiyixuxu a226920
get_block_state make it less verbose
yiyixuxu 77b5fa5
make it work with lora has both text_encoder & unet
yiyixuxu 6e2fe26
fix more for lora
yiyixuxu 68a5185
refactor more, ipadapter node, lora node
yiyixuxu d046cf7
block state + fix for num_images_per_prompt > 1 for denoise/controlne…
yiyixuxu 71df158
Update src/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_di…
yiyixuxu b3fb418
Merge branch 'modular-diffusers' of github.com:huggingface/diffusers …
yiyixuxu 00cae4e
docstring doc doc doc
yiyixuxu ccb35ac
Merge branch 'main' into modular-diffusers
yiyixuxu 00a3bc9
fix
yiyixuxu 4bed3e3
up up
yiyixuxu c7020df
add model_info
yiyixuxu 2c3e4ea
fix
yiyixuxu e5089d7
update
yiyixuxu 8ddb20b
up
yiyixuxu cff0fd6
more refactor
yiyixuxu 485f8d1
more refactor
yiyixuxu addaad0
more more more refactor
yiyixuxu 12650e1
up
yiyixuxu 96795af
Merge branch 'main' into modular-diffusers
yiyixuxu 6a509ba
Merge branch 'main' into modular-diffusers
yiyixuxu a8e853b
[modular diffusers] more refactor (#11235)
yiyixuxu 7ad01a6
rename modular_pipeline_block_mappings.py to modular_block_mapping
yiyixuxu 5a8c1b5
add block mappings to modular_diffusers.stable_diffusion_xl.__init__
yiyixuxu 8913d59
add to method to modular loader, copied from DiffusionPipeline, not t…
yiyixuxu 45392cc
update the description of StableDiffusionXLDenoiseLoopWrapper
yiyixuxu 9e58856
add __repr__ method for InsertableOrderedDict
yiyixuxu 04c16d0
update
yiyixuxu 083479c
ordereddict -> insertableOrderedDict; make sure loader to method works
yiyixuxu 4751d45
shorten loop subblock name
yiyixuxu d12531d
lora: only remove hooks that we add back
yiyixuxu 19545fd
update components manager __repr__
yiyixuxu 78d2454
fix
yiyixuxu 085ade0
add doc (developer guide)
yiyixuxu 42c06e9
update doc
yiyixuxu 1ae591e
update code format
yiyixuxu bb40443
up
yiyixuxu 7c78fb1
add a overview doc page
yiyixuxu 48e4ff5
update overview
yiyixuxu e49413d
update doc
yiyixuxu ffbaa89
move save_pretrained to the correct place
yiyixuxu cdaaa40
update ComponentSpec.from_component, only update config if it is crea…
yiyixuxu 1c9f0a8
ujpdate toctree
yiyixuxu 174628e
Merge branch 'main' into modular-diffusers
yiyixuxu c0327e4
update init
yiyixuxu 5917d70
remove lora related changes
yiyixuxu 8c038f0
Update src/diffusers/loaders/lora_base.py
yiyixuxu cb328d3
Apply suggestions from code review
yiyixuxu 7d2a633
style
yiyixuxu 74b908b
style
yiyixuxu 9530245
correct code format
yiyixuxu c437ae7
copies
yiyixuxu f3453f0
copy
yiyixuxu a82e211
style
yiyixuxu a33206d
fix
yiyixuxu 75e6238
revert changes in pipelines.stable_diffusion_xl folder, can seperate …
yiyixuxu 129d658
oops, fix
yiyixuxu da4242d
use diffusers ModelHook, raise a import error for accelerate inside e…
yiyixuxu ab6d634
style
yiyixuxu 7492e33
fix
yiyixuxu b92cda2
move quicktour to first page
yiyixuxu 61772f0
updatee a comment
yiyixuxu 9abac85
remove mapping file, move to preeset.py
yiyixuxu 84f4b27
modular_pipeline_presets.py -> modular_blocks_presets.py
yiyixuxu 449f299
move all the sequential pipelines & auto pipelines to the blocks_pres…
yiyixuxu 7608d2e
style
yiyixuxu f63d62e
intermediates_inputs -> intermediate_inputs; component_manager -> com…
yiyixuxu 655512e
components manager: change get -> search_models; add get_ids, get_com…
yiyixuxu 885a596
blocks -> sub_blocks; will not by default load all; add load_default…
yiyixuxu b543bcc
docstring blocks -> sub_blocks
yiyixuxu 75540f4
more blocks -> sub_blocks
yiyixuxu 93760b1
InsertableOrderedDict -> InsertableDict
yiyixuxu 9aaec5b
up
yiyixuxu 58dbe0c
finimsh the quickstart!
yiyixuxu 49ea4d1
style
yiyixuxu 92b6b43
add some visuals
yiyixuxu 8c680bc
up
yiyixuxu fedaa00
Merge branch 'main' into modular-diffusers
yiyixuxu fdd2bed
2024 -> 2025; fix a circular import
yiyixuxu 3a3441c
start the write your own pipeline block tutorial
yiyixuxu 9fae382
Apply suggestions from code review
yiyixuxu b43e703
Update docs/source/en/modular_diffusers/write_own_pipeline_block.md
yiyixuxu c75b88f
up
yiyixuxu 285f877
make InsertableDict importable from modular_pipelines
yiyixuxu f09b1cc
start the section on sequential pipelines
yiyixuxu c5849ba
more
yiyixuxu 363737e
add loop sequential blocks
yiyixuxu bbd9340
up
yiyixuxu 0138e17
remove the get_exeuction_blocks rec from AutoPipelineBlocks repr
yiyixuxu db4b54c
finish the autopipelines section!
yiyixuxu abf28d5
update
yiyixuxu 4b12a60
Merge branch 'main' into modular-diffusers
yiyixuxu f27fbce
more attemp to fix circular import
yiyixuxu 98ea5c9
Merge branch 'modular-diffusers' of github.com:huggingface/diffusers …
yiyixuxu b5db8aa
developer_guide -> end-to-end guide
yiyixuxu 4543d21
rename quick start- it is really not quick
yiyixuxu 1987c07
update docstree
yiyixuxu 2e20241
up up
yiyixuxu 13fe248
add modularpipelineblocks to be pushtohub mixin
yiyixuxu 8cb5b08
up upup
yiyixuxu 3e46c86
fix links in the doc
yiyixuxu 13c51bb
Modular PAG Guider (#11860)
a-r-r-o-w b750c69
Modular Guider ConfigMixin (#11862)
a-r-r-o-w 284f827
Modular custom config object serialization (#11868)
a-r-r-o-w 2c66fb3
Apply suggestions from code review
yiyixuxu 63e94cb
resolve conflicnt
yiyixuxu 4f8b6f5
style + copy
yiyixuxu 23de59e
add sub_blocks for pipelineBlock
yiyixuxu 7cea9a3
add a guider section on doc
yiyixuxu 0a4819a
add sub_folder to save_pretrained() for config mixin
yiyixuxu 229c4b3
add from_pretrained/save_pretrained for guider
yiyixuxu 179d6d9
add subfolder to push_to_hub
yiyixuxu 5af003a
update from_componeenet, update_component
yiyixuxu 0fcdd69
style
yiyixuxu ceeb3c1
fix
yiyixuxu 0fcce2a
Merge branch 'main' into modular-diffusers
yiyixuxu 6521f59
make sure modularpipeline from_pretrained works without modular_model…
yiyixuxu e0083b2
Merge branch 'modular-diffusers' of github.com:huggingface/diffusers …
yiyixuxu 863c7df
components manager: use shorter ID, display id instead of name
yiyixuxu a2da000
add a guide on components manager
yiyixuxu be5e10a
Copied-from implementation of PAG-guider (#11882)
a-r-r-o-w 04171c7
Merge branch 'modular-diffusers' of github.com:huggingface/diffusers …
yiyixuxu e6ffde2
Apply suggestions from code review
yiyixuxu 5f3ebef
update remove duplicated config for pag, and remove the description o…
yiyixuxu 59abd95
add link to components manager doc
yiyixuxu f95c320
addreess more review comments
yiyixuxu 79166dc
Merge branch 'main' into modular-diffusers
yiyixuxu cb9dca5
add experimental marks to all modular docs
yiyixuxu d27b654
add more docstrings + experimental marks
yiyixuxu 595581d
style
yiyixuxu 73c5fe8
Merge branch 'modular-diffusers' of github.com:huggingface/diffusers …
yiyixuxu de7cdf6
Merge modular diffusers with main (#11893)
a-r-r-o-w a935bea
big doc updategit status!
yiyixuxu 2b006a2
Merge branch 'modular-diffusers' of github.com:huggingface/diffusers …
yiyixuxu 9106f9c
Merge branch 'main' into modular-diffusers
a-r-r-o-w cf0f8e5
Merge branch 'main' into modular-diffusers
a-r-r-o-w 2104bef
update more modular pipeline doc
yiyixuxu 65ba892
update doc
yiyixuxu 01300a3
up
yiyixuxu 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
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
316 changes: 316 additions & 0 deletions
316
docs/source/en/modular_diffusers/auto_pipeline_blocks.md
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,316 @@ | ||
| <!--Copyright 2025 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. | ||
| --> | ||
|
|
||
| # AutoPipelineBlocks | ||
|
|
||
| <Tip warning={true}> | ||
|
|
||
| 🧪 **Experimental Feature**: Modular Diffusers is an experimental feature we are actively developing. The API may be subject to breaking changes. | ||
|
|
||
| </Tip> | ||
|
|
||
| `AutoPipelineBlocks` is a subclass of `ModularPipelineBlocks`. It is a multi-block that automatically selects which sub-blocks to run based on the inputs provided at runtime, creating conditional workflows that adapt to different scenarios. The main purpose is convenience and portability - for developers, you can package everything into one workflow, making it easier to share and use. | ||
|
|
||
| In this tutorial, we will show you how to create an `AutoPipelineBlocks` and learn more about how the conditional selection works. | ||
|
|
||
| <Tip> | ||
|
|
||
| Other types of multi-blocks include [SequentialPipelineBlocks](sequential_pipeline_blocks.md) (for linear workflows) and [LoopSequentialPipelineBlocks](loop_sequential_pipeline_blocks.md) (for iterative workflows). For information on creating individual blocks, see the [PipelineBlock guide](pipeline_block.md). | ||
|
|
||
| Additionally, like all `ModularPipelineBlocks`, `AutoPipelineBlocks` are definitions/specifications, not runnable pipelines. You need to convert them into a `ModularPipeline` to actually execute them. For information on creating and running pipelines, see the [Modular Pipeline guide](modular_pipeline.md). | ||
|
|
||
| </Tip> | ||
|
|
||
| For example, you might want to support text-to-image and image-to-image tasks. Instead of creating two separate pipelines, you can create an `AutoPipelineBlocks` that automatically chooses the workflow based on whether an `image` input is provided. | ||
|
|
||
| Let's see an example. We'll use the helper function from the [PipelineBlock guide](./pipeline_block.md) to create our blocks: | ||
|
|
||
| **Helper Function** | ||
|
|
||
| ```py | ||
| from diffusers.modular_pipelines import PipelineBlock, InputParam, OutputParam | ||
| import torch | ||
|
|
||
| def make_block(inputs=[], intermediate_inputs=[], intermediate_outputs=[], block_fn=None, description=None): | ||
| class TestBlock(PipelineBlock): | ||
| model_name = "test" | ||
|
|
||
| @property | ||
| def inputs(self): | ||
| return inputs | ||
|
|
||
| @property | ||
| def intermediate_inputs(self): | ||
| return intermediate_inputs | ||
|
|
||
| @property | ||
| def intermediate_outputs(self): | ||
| return intermediate_outputs | ||
|
|
||
| @property | ||
| def description(self): | ||
| return description if description is not None else "" | ||
|
|
||
| def __call__(self, components, state): | ||
| block_state = self.get_block_state(state) | ||
| if block_fn is not None: | ||
| block_state = block_fn(block_state, state) | ||
| self.set_block_state(state, block_state) | ||
| return components, state | ||
|
|
||
| return TestBlock | ||
| ``` | ||
|
|
||
| Now let's create a dummy `AutoPipelineBlocks` that includes dummy text-to-image, image-to-image, and inpaint pipelines. | ||
|
|
||
|
|
||
| ```py | ||
| from diffusers.modular_pipelines import AutoPipelineBlocks | ||
|
|
||
| # These are dummy blocks and we only focus on "inputs" for our purpose | ||
| inputs = [InputParam(name="prompt")] | ||
| # block_fn prints out which workflow is running so we can see the execution order at runtime | ||
| block_fn = lambda x, y: print("running the text-to-image workflow") | ||
| block_t2i_cls = make_block(inputs=inputs, block_fn=block_fn, description="I'm a text-to-image workflow!") | ||
|
|
||
| inputs = [InputParam(name="prompt"), InputParam(name="image")] | ||
| block_fn = lambda x, y: print("running the image-to-image workflow") | ||
| block_i2i_cls = make_block(inputs=inputs, block_fn=block_fn, description="I'm a image-to-image workflow!") | ||
|
|
||
| inputs = [InputParam(name="prompt"), InputParam(name="image"), InputParam(name="mask")] | ||
| block_fn = lambda x, y: print("running the inpaint workflow") | ||
| block_inpaint_cls = make_block(inputs=inputs, block_fn=block_fn, description="I'm a inpaint workflow!") | ||
|
|
||
| class AutoImageBlocks(AutoPipelineBlocks): | ||
| # List of sub-block classes to choose from | ||
| block_classes = [block_inpaint_cls, block_i2i_cls, block_t2i_cls] | ||
| # Names for each block in the same order | ||
| block_names = ["inpaint", "img2img", "text2img"] | ||
| # Trigger inputs that determine which block to run | ||
| # - "mask" triggers inpaint workflow | ||
| # - "image" triggers img2img workflow (but only if mask is not provided) | ||
| # - if none of above, runs the text2img workflow (default) | ||
| block_trigger_inputs = ["mask", "image", None] | ||
| # Description is extremely important for AutoPipelineBlocks | ||
| @property | ||
| def description(self): | ||
| return ( | ||
| "Pipeline generates images given different types of conditions!\n" | ||
| + "This is an auto pipeline block that works for text2img, img2img and inpainting tasks.\n" | ||
| + " - inpaint workflow is run when `mask` is provided.\n" | ||
| + " - img2img workflow is run when `image` is provided (but only when `mask` is not provided).\n" | ||
| + " - text2img workflow is run when neither `image` nor `mask` is provided.\n" | ||
| ) | ||
|
|
||
| # Create the blocks | ||
| auto_blocks = AutoImageBlocks() | ||
| # convert to pipeline | ||
| auto_pipeline = auto_blocks.init_pipeline() | ||
| ``` | ||
|
|
||
| Now we have created an `AutoPipelineBlocks` that contains 3 sub-blocks. Notice the warning message at the top - this automatically appears in every `ModularPipelineBlocks` that contains `AutoPipelineBlocks` to remind end users that dynamic block selection happens at runtime. | ||
|
|
||
| ```py | ||
| AutoImageBlocks( | ||
| Class: AutoPipelineBlocks | ||
|
|
||
| ==================================================================================================== | ||
| This pipeline contains blocks that are selected at runtime based on inputs. | ||
| Trigger Inputs: ['mask', 'image'] | ||
| ==================================================================================================== | ||
|
|
||
|
|
||
| Description: Pipeline generates images given different types of conditions! | ||
| This is an auto pipeline block that works for text2img, img2img and inpainting tasks. | ||
| - inpaint workflow is run when `mask` is provided. | ||
| - img2img workflow is run when `image` is provided (but only when `mask` is not provided). | ||
| - text2img workflow is run when neither `image` nor `mask` is provided. | ||
|
|
||
|
|
||
|
|
||
| Sub-Blocks: | ||
| • inpaint [trigger: mask] (TestBlock) | ||
| Description: I'm a inpaint workflow! | ||
|
|
||
| • img2img [trigger: image] (TestBlock) | ||
| Description: I'm a image-to-image workflow! | ||
|
|
||
| • text2img [default] (TestBlock) | ||
| Description: I'm a text-to-image workflow! | ||
|
|
||
| ) | ||
| ``` | ||
|
|
||
| Check out the documentation with `print(auto_pipeline.doc)`: | ||
|
|
||
| ```py | ||
| >>> print(auto_pipeline.doc) | ||
| class AutoImageBlocks | ||
|
|
||
| Pipeline generates images given different types of conditions! | ||
| This is an auto pipeline block that works for text2img, img2img and inpainting tasks. | ||
| - inpaint workflow is run when `mask` is provided. | ||
| - img2img workflow is run when `image` is provided (but only when `mask` is not provided). | ||
| - text2img workflow is run when neither `image` nor `mask` is provided. | ||
|
|
||
| Inputs: | ||
|
|
||
| prompt (`None`, *optional*): | ||
|
|
||
| image (`None`, *optional*): | ||
|
|
||
| mask (`None`, *optional*): | ||
| ``` | ||
|
|
||
| There is a fundamental trade-off of AutoPipelineBlocks: it trades clarity for convenience. While it is really easy for packaging multiple workflows, it can become confusing without proper documentation. e.g. if we just throw a pipeline at you and tell you that it contains 3 sub-blocks and takes 3 inputs `prompt`, `image` and `mask`, and ask you to run an image-to-image workflow: if you don't have any prior knowledge on how these pipelines work, you would be pretty clueless, right? | ||
|
|
||
| This pipeline we just made though, has a docstring that shows all available inputs and workflows and explains how to use each with different inputs. So it's really helpful for users. For example, it's clear that you need to pass `image` to run img2img. This is why the description field is absolutely critical for AutoPipelineBlocks. We highly recommend you to explain the conditional logic very well for each `AutoPipelineBlocks` you would make. We also recommend to always test individual pipelines first before packaging them into AutoPipelineBlocks. | ||
|
|
||
| Let's run this auto pipeline with different inputs to see if the conditional logic works as described. Remember that we have added `print` in each `PipelineBlock`'s `__call__` method to print out its workflow name, so it should be easy to tell which one is running: | ||
|
|
||
| ```py | ||
| >>> _ = auto_pipeline(image="image", mask="mask") | ||
| running the inpaint workflow | ||
| >>> _ = auto_pipeline(image="image") | ||
| running the image-to-image workflow | ||
| >>> _ = auto_pipeline(prompt="prompt") | ||
| running the text-to-image workflow | ||
| >>> _ = auto_pipeline(image="prompt", mask="mask") | ||
| running the inpaint workflow | ||
| ``` | ||
|
|
||
| However, even with documentation, it can become very confusing when AutoPipelineBlocks are combined with other blocks. The complexity grows quickly when you have nested AutoPipelineBlocks or use them as sub-blocks in larger pipelines. | ||
|
|
||
| Let's make another `AutoPipelineBlocks` - this one only contains one block, and it does not include `None` in its `block_trigger_inputs` (which corresponds to the default block to run when none of the trigger inputs are provided). This means this block will be skipped if the trigger input (`ip_adapter_image`) is not provided at runtime. | ||
|
|
||
| ```py | ||
| from diffusers.modular_pipelines import SequentialPipelineBlocks, InsertableDict | ||
| inputs = [InputParam(name="ip_adapter_image")] | ||
| block_fn = lambda x, y: print("running the ip-adapter workflow") | ||
| block_ipa_cls = make_block(inputs=inputs, block_fn=block_fn, description="I'm a IP-adapter workflow!") | ||
|
|
||
| class AutoIPAdapter(AutoPipelineBlocks): | ||
| block_classes = [block_ipa_cls] | ||
| block_names = ["ip-adapter"] | ||
| block_trigger_inputs = ["ip_adapter_image"] | ||
| @property | ||
| def description(self): | ||
| return "Run IP Adapter step if `ip_adapter_image` is provided." | ||
| ``` | ||
|
|
||
| Now let's combine these 2 auto blocks together into a `SequentialPipelineBlocks`: | ||
|
|
||
| ```py | ||
| auto_ipa_blocks = AutoIPAdapter() | ||
| blocks_dict = InsertableDict() | ||
| blocks_dict["ip-adapter"] = auto_ipa_blocks | ||
| blocks_dict["image-generation"] = auto_blocks | ||
| all_blocks = SequentialPipelineBlocks.from_blocks_dict(blocks_dict) | ||
| pipeline = all_blocks.init_pipeline() | ||
| ``` | ||
|
|
||
| Let's take a look: now things get more confusing. In this particular example, you could still try to explain the conditional logic in the `description` field here - there are only 4 possible execution paths so it's doable. However, since this is a `SequentialPipelineBlocks` that could contain many more blocks, the complexity can quickly get out of hand as the number of blocks increases. | ||
|
|
||
| ```py | ||
| >>> all_blocks | ||
| SequentialPipelineBlocks( | ||
| Class: ModularPipelineBlocks | ||
|
|
||
| ==================================================================================================== | ||
| This pipeline contains blocks that are selected at runtime based on inputs. | ||
| Trigger Inputs: ['image', 'mask', 'ip_adapter_image'] | ||
| Use `get_execution_blocks()` with input names to see selected blocks (e.g. `get_execution_blocks('image')`). | ||
| ==================================================================================================== | ||
|
|
||
|
|
||
| Description: | ||
|
|
||
|
|
||
| Sub-Blocks: | ||
| [0] ip-adapter (AutoIPAdapter) | ||
| Description: Run IP Adapter step if `ip_adapter_image` is provided. | ||
|
|
||
|
|
||
| [1] image-generation (AutoImageBlocks) | ||
| Description: Pipeline generates images given different types of conditions! | ||
| This is an auto pipeline block that works for text2img, img2img and inpainting tasks. | ||
| - inpaint workflow is run when `mask` is provided. | ||
| - img2img workflow is run when `image` is provided (but only when `mask` is not provided). | ||
| - text2img workflow is run when neither `image` nor `mask` is provided. | ||
|
|
||
|
|
||
| ) | ||
|
|
||
| ``` | ||
|
|
||
| This is when the `get_execution_blocks()` method comes in handy - it basically extracts a `SequentialPipelineBlocks` that only contains the blocks that are actually run based on your inputs. | ||
|
|
||
| Let's try some examples: | ||
|
|
||
| `mask`: we expect it to skip the first ip-adapter since `ip_adapter_image` is not provided, and then run the inpaint for the second block. | ||
|
|
||
| ```py | ||
| >>> all_blocks.get_execution_blocks('mask') | ||
| SequentialPipelineBlocks( | ||
| Class: ModularPipelineBlocks | ||
|
|
||
| Description: | ||
|
|
||
|
|
||
| Sub-Blocks: | ||
| [0] image-generation (TestBlock) | ||
| Description: I'm a inpaint workflow! | ||
|
|
||
| ) | ||
| ``` | ||
|
|
||
| Let's also actually run the pipeline to confirm: | ||
|
|
||
| ```py | ||
| >>> _ = pipeline(mask="mask") | ||
| skipping auto block: AutoIPAdapter | ||
| running the inpaint workflow | ||
| ``` | ||
|
|
||
| Try a few more: | ||
|
|
||
| ```py | ||
| print(f"inputs: ip_adapter_image:") | ||
| blocks_select = all_blocks.get_execution_blocks('ip_adapter_image') | ||
| print(f"expected_execution_blocks: {blocks_select}") | ||
| print(f"actual execution blocks:") | ||
| _ = pipeline(ip_adapter_image="ip_adapter_image", prompt="prompt") | ||
| # expect to see ip-adapter + text2img | ||
|
|
||
| print(f"inputs: image:") | ||
| blocks_select = all_blocks.get_execution_blocks('image') | ||
| print(f"expected_execution_blocks: {blocks_select}") | ||
| print(f"actual execution blocks:") | ||
| _ = pipeline(image="image", prompt="prompt") | ||
| # expect to see img2img | ||
|
|
||
| print(f"inputs: prompt:") | ||
| blocks_select = all_blocks.get_execution_blocks('prompt') | ||
| print(f"expected_execution_blocks: {blocks_select}") | ||
| print(f"actual execution blocks:") | ||
| _ = pipeline(prompt="prompt") | ||
| # expect to see text2img (prompt is not a trigger input so fallback to default) | ||
|
|
||
| print(f"inputs: mask + ip_adapter_image:") | ||
| blocks_select = all_blocks.get_execution_blocks('mask','ip_adapter_image') | ||
| print(f"expected_execution_blocks: {blocks_select}") | ||
| print(f"actual execution blocks:") | ||
| _ = pipeline(mask="mask", ip_adapter_image="ip_adapter_image") | ||
| # expect to see ip-adapter + inpaint | ||
| ``` | ||
|
|
||
| In summary, `AutoPipelineBlocks` is a good tool for packaging multiple workflows into a single, convenient interface and it can greatly simplify the user experience. However, always provide clear descriptions explaining the conditional logic, test individual pipelines first before combining them, and use `get_execution_blocks()` to understand runtime behavior in complex compositions. |
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.
Uh oh!
There was an error while loading. Please reload this page.