-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
Merged
The Modular Diffusers #9672
Changes from 130 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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
291 changes: 291 additions & 0 deletions
291
docs/source/en/modular_diffusers/write_own_pipeline_block.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,291 @@ | ||
<!--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. | ||
--> | ||
|
||
# `ModularPipelineBlocks` | ||
|
||
In Modular Diffusers, you build your workflow using `ModularPipelineBlocks`. We support 4 different types of blocks: `PipelineBlock`, `SequentialPipelineBlocks`, `LoopSequentialPipelineBlocks`, and `AutoPipelineBlocks`. Among them, `PipelineBlock` is the most fundamental building block of the whole system - it's like a brick in a Lego system. These blocks are designed to easily connect with each other, allowing for modular construction of creative and potentially very complex workflows. | ||
|
||
In this tutorial, we will focus on how to write a basic `PipelineBlock` and how it interacts with other components in the system. We will also cover how to connect them together using the multi-blocks: `SequentialPipelineBlocks`, `LoopSequentialPipelineBlocks`, and `AutoPipelineBlocks`. | ||
|
||
|
||
### Understanding the Foundation: `PipelineState` | ||
|
||
Before we dive into creating `PipelineBlock`s, we need to have a basic understanding of `PipelineState` - the core data structure that all blocks operate on. This concept is fundamental to understanding how blocks interact with each other and the pipeline system. | ||
|
||
|
||
In the modular diffusers system, `PipelineState` acts as the global state container that `PipelineBlock`s operate on - each block gets a local view (`BlockState`) of the relevant variables it needs from `PipelineState`, performs its operations, and then updates `PipelineState` with any changes. | ||
|
||
While `PipelineState` maintains the complete runtime state of the pipeline, `PipelineBlock`s define what parts of that state they can read from and write to through their `input`s, `intermediates_inputs`, and `intermediates_outputs` properties. | ||
|
||
A `PipelineState` consists of two distinct states: | ||
- The **immutable state** (i.e. the `inputs` dict) contains a copy of values provided by users. Once a value is added to the immutable state, it cannot be changed. Blocks can read from the immutable state but cannot write to it. | ||
- The **mutable state** (i.e. the `intermediates` dict) contains variables that are passed between blocks and can be modified by them. | ||
|
||
Here's an example of what a `PipelineState` looks like: | ||
|
||
``` | ||
PipelineState( | ||
inputs={ | ||
prompt: 'a cat' | ||
guidance_scale: 7.0 | ||
num_inference_steps: 25 | ||
}, | ||
intermediates={ | ||
prompt_embeds: Tensor(dtype=torch.float32, shape=torch.Size([1, 1, 1, 1])) | ||
negative_prompt_embeds: None | ||
}, | ||
``` | ||
|
||
### Creating a `PipelineBlock` | ||
|
||
To write a `PipelineBlock` class, you need to define a few properties that determine how your block interacts with the pipeline state. Understanding these properties is crucial - they define what data your block can access and what it can produce. | ||
|
||
The three main properties you need to define are: | ||
- `inputs`: Immutable values from the user that cannot be modified | ||
- `intermediate_inputs`: Mutable values from previous blocks that can be read and modified | ||
- `intermediate_outputs`: New values your block creates for subsequent blocks | ||
|
||
Let's explore each one and understand how they work with the pipeline state. | ||
|
||
**Inputs: Immutable User Values** | ||
|
||
Inputs are variables your block needs from the immutable pipeline state - these are user-provided values that cannot be modified by any block. You define them using `InputParam`: | ||
|
||
```py | ||
user_inputs = [ | ||
InputParam(name="image", type_hint="PIL.Image", description="raw input image to process") | ||
] | ||
``` | ||
|
||
When you list something as an input, you're saying "I need this value directly from the end user, and I will talk to them directly, telling them what I need in the 'description' field. They will provide it and it will come to me unchanged." | ||
|
||
This is especially useful for raw values that serve as the "source of truth" in your workflow. For example, with a raw image, many workflows require preprocessing steps like resizing that a previous block might have performed. But in many cases, you also want the raw PIL image. In some inpainting workflows, you need the original image to overlay with the generated result for better control and consistency. | ||
|
||
**Intermediate Inputs: Mutable Values from Previous Blocks** | ||
|
||
Intermediate inputs are variables your block needs from the mutable pipeline state - these are values that can be read and modified. They're typically created by previous blocks, but could also be directly provided by the user if not the case: | ||
|
||
```py | ||
user_intermediate_inputs = [ | ||
InputParam(name="processed_image", type_hint="torch.Tensor", description="image that has been preprocessed and normalized"), | ||
] | ||
``` | ||
|
||
When you list something as an intermediate input, you're saying "I need this value, but I want to work with a different block that has already created it. I already know for sure that I can get it from this other block, but it's okay if other developers can use something different." | ||
|
||
**Intermediate Outputs: New Values for Subsequent Blocks** | ||
|
||
Intermediate outputs are new variables your block creates and adds to the mutable pipeline state so they can be used by subsequent blocks: | ||
|
||
```py | ||
user_intermediate_outputs = [ | ||
OutputParam(name="image_latents", description="latents representing the image") | ||
] | ||
``` | ||
|
||
Intermediate inputs and intermediate outputs work together like Lego studs and anti-studs - they're the connection points that make blocks modular. When one block produces an intermediate output, it becomes available as an intermediate input for subsequent blocks. This is where the "modular" nature of the system really shines - blocks can be connected and reconnected in different ways as long as their inputs and outputs match. We will see more how they connect when we talk about multi-blocks. | ||
|
||
**The `__call__` Method Structure** | ||
|
||
Your `PipelineBlock`'s `__call__` method should follow this structure: | ||
|
||
```py | ||
def __call__(self, components, state): | ||
# Get a local view of the state variables this block needs | ||
block_state = self.get_block_state(state) | ||
|
||
# Your computation logic here | ||
# block_state contains all your inputs and intermediate_inputs | ||
# You can access them like: block_state.image, block_state.processed_image | ||
|
||
# Update the pipeline state with your updated block_states | ||
self.add_block_state(state, block_state) | ||
return components, state | ||
``` | ||
|
||
The `block_state` object contains all the variables you defined in `inputs` and `intermediate_inputs`, making them easily accessible for your computation. | ||
|
||
**Components and Configs** | ||
|
||
You can define the components and pipeline-level configs your block needs using `ComponentSpec` and `ConfigSpec`: | ||
|
||
```py | ||
from diffusers import ComponentSpec, ConfigSpec | ||
|
||
# Define components your block needs | ||
expected_components = [ | ||
ComponentSpec(name="unet", type_hint=UNet2DConditionModel), | ||
ComponentSpec(name="scheduler", type_hint=EulerDiscreteScheduler) | ||
] | ||
|
||
# Define pipeline-level configs | ||
expected_config = [ | ||
ConfigSpec("force_zeros_for_empty_prompt", True) | ||
] | ||
``` | ||
|
||
**Components**: You must provide a `name` and ideally a `type_hint`. The actual loading details (repo, subfolder, variant) are typically specified when creating the pipeline, as we covered in the [quicktour](quicktour.md#loading-components-into-a-modularpipeline). | ||
|
||
**Configs**: Simple pipeline-level settings that control behavior across all blocks. | ||
|
||
When you convert your blocks into a pipeline using `blocks.init_pipeline()`, the pipeline collects all component requirements from the blocks and fetches the loading specs from the modular repository. The components are then made available to your block in the `components` argument of the `__call__` method. | ||
|
||
That's all you need to define in order to create a `PipelineBlock`. There is no hidden complexity. In fact we are going to create a helper function that take exactly these variables as input and return a pipeline block. We will use this helper function through out the tutorial to create test blocks | ||
|
||
Note that for `__call__` method, the only part you should implement differently is the part between `get_block_state` and `add_block_state`, which can be abstracted into a simple function that takes `block_state` and returns the updated state. This is why our helper function accepts a `block_fn` parameter that does exactly that. | ||
|
||
**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.add_block_state(state, block_state) | ||
return components, state | ||
|
||
return TestBlock() | ||
``` | ||
|
||
|
||
Let's create a simple block to see how these definitions interact with the pipeline state: | ||
|
||
```py | ||
user_inputs = [ | ||
InputParam(name="image", type_hint="PIL.Image", description="raw input image to process") | ||
] | ||
|
||
user_intermediate_inputs = [InputParam(name="batch_size", type_hint=int)] | ||
|
||
user_intermediate_outputs = [ | ||
OutputParam(name="image_latents", description="latents representing the image") | ||
] | ||
|
||
def user_block_fn(block_state, pipeline_state): | ||
print(f"pipeline_state (before update): {pipeline_state}") | ||
print(f"block_state (before update): {block_state}") | ||
|
||
# Simulate processing the image | ||
block_state.image = torch.randn(1, 3, 512, 512) | ||
block_state.batch_size = block_state.batch_size * 2 | ||
block_state.processed_image = [torch.randn(1, 3, 512, 512)] * block_state.batch_size | ||
block_state.image_latents = torch.randn(1, 4, 64, 64) | ||
|
||
print(f"block_state (after update): {block_state}") | ||
return block_state | ||
|
||
# Create a block with our definitions | ||
block = make_block( | ||
inputs=user_inputs, | ||
intermediate_inputs=user_intermediate_inputs, | ||
intermediate_outputs=user_intermediate_outputs, | ||
block_fn=user_block_fn | ||
) | ||
pipe = block.init_pipeline() | ||
``` | ||
|
||
Let's check the pipeline's docstring to see what inputs it expects: | ||
|
||
```py | ||
>>> print(pipe.doc) | ||
class TestBlock | ||
|
||
Inputs: | ||
|
||
image (`PIL.Image`, *optional*): | ||
raw input image to process | ||
|
||
batch_size (`int`, *optional*): | ||
|
||
Outputs: | ||
|
||
image_latents (`None`): | ||
latents representing the image | ||
``` | ||
|
||
Notice that `batch_size` appears as an input even though we defined it as an intermediate input. This happens because no previous block provided it, so the pipeline makes it available as a user input. However, unlike regular inputs, this value goes directly into the mutable intermediate state. | ||
|
||
Now let's run the pipeline: | ||
|
||
```py | ||
from diffusers.utils import load_image | ||
|
||
image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/image_of_squirrel_painting.png") | ||
state = pipe(image=image, batch_size=2) | ||
print(f"pipeline_state (after update): {state}") | ||
``` | ||
|
||
```out | ||
pipeline_state (before update): PipelineState( | ||
inputs={ | ||
image: <PIL.Image.Image image mode=RGB size=512x512 at 0x7F226024EB90> | ||
}, | ||
intermediates={ | ||
batch_size: 2 | ||
}, | ||
) | ||
block_state (before update): BlockState( | ||
image: <PIL.Image.Image image mode=RGB size=512x512 at 0x7F2260260220> | ||
batch_size: 2 | ||
) | ||
|
||
block_state (after update): BlockState( | ||
image: Tensor(dtype=torch.float32, shape=torch.Size([1, 3, 512, 512])) | ||
batch_size: 4 | ||
processed_image: List[4] of Tensors with shapes [torch.Size([1, 3, 512, 512]), torch.Size([1, 3, 512, 512]), torch.Size([1, 3, 512, 512]), torch.Size([1, 3, 512, 512])] | ||
image_latents: Tensor(dtype=torch.float32, shape=torch.Size([1, 4, 64, 64])) | ||
) | ||
pipeline_state (after update): PipelineState( | ||
inputs={ | ||
image: <PIL.Image.Image image mode=RGB size=512x512 at 0x7F226024EB90> | ||
}, | ||
intermediates={ | ||
batch_size: 4 | ||
image_latents: Tensor(dtype=torch.float32, shape=torch.Size([1, 4, 64, 64])) | ||
}, | ||
) | ||
``` | ||
|
||
**Key Observations:** | ||
|
||
1. **Before the update**: `image` goes to the immutable inputs dict, while `batch_size` goes to the mutable intermediates dict, and both are available in `block_state`. | ||
|
||
2. **After the update**: | ||
- **`image` modification**: Changed in `block_state` but not in `pipeline_state` - this change is local to the block only | ||
- **`batch_size` modification**: Updated in both `block_state` and `pipeline_state` - this change affects subsequent blocks (we didn't need to declare it as an intermediate output since it was already in the intermediates dict) | ||
- **`image_latents` creation**: Added to `pipeline_state` because it was declared as an intermediate output | ||
- **`processed_image` creation**: Not added to `pipeline_state` because it wasn't declared as an intermediate output | ||
|
||
I hope by now you have a basic idea about how `PipelineBlock` manages state through inputs, intermediate inputs, and intermediate outputs. The real power comes when we connect multiple blocks together - their intermediate outputs become intermediate inputs for subsequent blocks, creating modular workflows. Let's explore how to build these connections using multi-blocks like `SequentialPipelineBlocks`. |
Oops, something went wrong.
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.