You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -528,3 +530,253 @@ from diffusers.modular_pipelines.stable_diffusion_xl.denoise import StableDiffus
528
530
StableDiffusionXLDenoiseStep()
529
531
```
530
532
533
+
## `AutoPipelineBlocks`
534
+
535
+
`AutoPipelineBlocks` allows you to pack different pipelines into one and automatically select which one to run at runtime based on the inputs. The main purpose is convenience and portability - for developers, you can package everything into one workflow, making it easier to share and use.
536
+
537
+
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.
538
+
539
+
Let's see an example. Here we'll create a dummy `AutoPipelineBlocks` that includes dummy text-to-image, image-to-image, and inpaint pipelines.
540
+
541
+
542
+
```py
543
+
from diffusers.modular_pipelines import AutoPipelineBlocks
544
+
545
+
# These are dummy blocks and we only focus on "inputs" for our purpose
546
+
inputs = [InputParam(name="prompt")]
547
+
# block_fn prints out which workflow is running so we can see the execution order at runtime
548
+
block_fn =lambdax, y: print("running the text-to-image workflow")
549
+
block_t2i_cls = make_block(inputs=inputs, block_fn=block_fn, description="I'm a text-to-image workflow!")
# Trigger inputs that determine which block to run
565
+
# - "mask" triggers inpaint workflow
566
+
# - "image" triggers img2img workflow (but only if mask is not provided)
567
+
# - if none of above, runs the text2img workflow (default)
568
+
block_trigger_inputs = ["mask", "image", None]
569
+
# Description is extremely important for AutoPipelineBlocks
570
+
@property
571
+
defdescription(self):
572
+
return (
573
+
"Pipeline generates images given different types of conditions!\n"
574
+
+"This is an auto pipeline block that works for text2img, img2img and inpainting tasks.\n"
575
+
+" - inpaint workflow is run when `mask` is provided.\n"
576
+
+" - img2img workflow is run when `image` is provided (but only when `mask` is not provided).\n"
577
+
+" - text2img workflow is run when neither `image` nor `mask` is provided.\n"
578
+
)
579
+
580
+
# Create the blocks
581
+
auto_blocks = AutoImageBlocks()
582
+
# convert to pipeline
583
+
auto_pipeline = auto_blocks.init_pipeline()
584
+
```
585
+
586
+
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.
Description: Pipeline generates images given different types of conditions!
599
+
This is an auto pipeline block that works for text2img, img2img and inpainting tasks.
600
+
- inpaint workflow is run when `mask`is provided.
601
+
- img2img workflow is run when `image`is provided (but only when `mask`isnot provided).
602
+
- text2img workflow is run when neither `image` nor `mask`is provided.
603
+
604
+
605
+
606
+
Sub-Blocks:
607
+
• inpaint [trigger: mask] (TestBlock)
608
+
Description: I'm a inpaint workflow!
609
+
610
+
• img2img [trigger: image] (TestBlock)
611
+
Description: I'm a image-to-image workflow!
612
+
613
+
• text2img [default] (TestBlock)
614
+
Description: I'm a text-to-image workflow!
615
+
616
+
)
617
+
```
618
+
619
+
Check out the documentation with `print(auto_pipeline.doc)`:
620
+
621
+
```py
622
+
>>>print(auto_pipeline.doc)
623
+
class AutoImageBlocks
624
+
625
+
Pipeline generates images given different types of conditions!
626
+
This is an auto pipeline block that works for text2img, img2img and inpainting tasks.
627
+
- inpaint workflow is run when `mask`is provided.
628
+
- img2img workflow is run when `image`is provided (but only when `mask`isnot provided).
629
+
- text2img workflow is run when neither `image` nor `mask`is provided.
630
+
631
+
Inputs:
632
+
633
+
prompt (`None`, *optional*):
634
+
635
+
image (`None`, *optional*):
636
+
637
+
mask (`None`, *optional*):
638
+
```
639
+
640
+
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?
641
+
642
+
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.
643
+
644
+
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:
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.
658
+
659
+
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.
660
+
661
+
```py
662
+
from diffusers.modular_pipelines import SequentialPipelineBlocks, InsertableDict
663
+
inputs = [InputParam(name="ip_adapter_image")]
664
+
block_fn = lambdax, y: print("running the ip-adapter workflow")
665
+
block_ipa_cls = make_block(inputs=inputs, block_fn=block_fn, description="I'm a IP-adapter workflow!")
666
+
667
+
class AutoIPAdapter(AutoPipelineBlocks):
668
+
block_classes = [block_ipa_cls]
669
+
block_names = ["ip-adapter"]
670
+
block_trigger_inputs = ["ip_adapter_image"]
671
+
@property
672
+
def description(self):
673
+
return"Run IP Adapter step if `ip_adapter_image` is provided. This step should be placed before the 'input' step.\n"
674
+
```
675
+
676
+
Now let's combine these 2 auto blocks together into a `SequentialPipelineBlocks`:
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.
Description: Run IP Adapter step if`ip_adapter_image`is provided. This step should be placed before the 'input' step.
707
+
708
+
709
+
[1] image-generation (AutoImageBlocks)
710
+
Description: Pipeline generates images given different types of conditions!
711
+
This is an auto pipeline block that works for text2img, img2img and inpainting tasks.
712
+
- inpaint workflow is run when `mask`is provided.
713
+
- img2img workflow is run when `image`is provided (but only when `mask`isnot provided).
714
+
- text2img workflow is run when neither `image` nor `mask`is provided.
715
+
716
+
717
+
)
718
+
719
+
```
720
+
721
+
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.
722
+
723
+
Let's try some examples:
724
+
725
+
`mask`: we expect it to skip the first ip-adapter since `ip_adapter_image`isnot provided, and then run the inpaint for the second block.
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 incomplex compositions.
0 commit comments