Skip to content

Commit 2aa3ef4

Browse files
committed
components manager
1 parent c14e4ce commit 2aa3ef4

File tree

9 files changed

+143
-432
lines changed

9 files changed

+143
-432
lines changed

docs/source/en/_toctree.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
- local: modular_diffusers/modular_pipeline
130130
title: ModularPipeline
131131
- local: modular_diffusers/components_manager
132-
title: Components Manager
132+
title: ComponentsManager
133133
- local: modular_diffusers/guiders
134134
title: Guiders
135135

docs/source/en/api/modular_diffusers/pipeline_components.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@
1010

1111
## ComponentsManager
1212

13-
[[autodoc]] diffusers.modular_pipelines.components_manager.ComponentsManager
13+
[[autodoc]] diffusers.modular_pipelines.components_manager.ComponentsManager
14+
15+
## InsertableDict
16+
17+
[[autodoc]] diffusers.modular_pipelines.modular_pipeline_utils.InsertableDict

docs/source/en/modular_diffusers/auto_pipeline_blocks.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ specific language governing permissions and limitations under the License.
1616

1717
This guide shows how to create [`~modular_pipelines.AutoPipelineBlocks`].
1818

19-
Create three [``~modular_pipelines.PipelineBlocks`] for text-to-image, image-to-image, and inpainting. These represent the different workflows available in the pipeline.
19+
Create three [`~modular_pipelines.PipelineBlock`] for text-to-image, image-to-image, and inpainting. These represent the different workflows available in the pipeline.
2020

2121
<hfoptions id="auto">
2222
<hfoption id="text-to-image">
@@ -155,14 +155,14 @@ class AutoImageBlocks(AutoPipelineBlocks):
155155

156156
It is **very** important to include a `description` to avoid any confusion over how to run a block and what inputs are required. While [`~modular_pipelines.AutoPipelineBlocks`] are convenient, it's conditional logic may be difficult to figure out if it isn't properly explained.
157157

158-
Create an instance of `AutoImageBlocks` and use [`~modular_pipelines.ModularPipeline.init_pipeline`] to convert it to a pipeline.
158+
Create an instance of `AutoImageBlocks` and use [`~modular_pipelines.ModularPipelineBlocks.init_pipeline`] to convert it to a pipeline.
159159

160160
```py
161161
auto_blocks = AutoImageBlocks()
162162
auto_pipeline = auto_blocks.init_pipeline()
163163
```
164164

165-
For more complex compositions, nested [`~modular_pipelines.AutoPipelineBlocks`] blocks when they're used as sub-blocks in larger pipelines, use the [`~modular_pipelines.PipelineBlocks.get_execution_blocks`] method to extract the a block that is actually run based on your input.
165+
For more complex compositions, nested [`~modular_pipelines.AutoPipelineBlocks`] blocks when they're used as sub-blocks in larger pipelines, use the [`~modular_pipelines.SequentialPipelineBlocks.get_execution_blocks`] method to extract the a block that is actually run based on your input.
166166

167167
```py
168168
auto_blocks.get_execution_blocks("mask")

docs/source/en/modular_diffusers/components_manager.md

Lines changed: 92 additions & 416 deletions
Large diffs are not rendered by default.

docs/source/en/modular_diffusers/loop_sequential_pipeline_blocks.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ specific language governing permissions and limitations under the License.
1212

1313
# LoopSequentialPipelineBlocks
1414

15-
[`~modular_pipelines.LoopSequentialPipelineBlocks`] are a multi-block type that composes other [`~modular_pipelines.PipelineBlocks`] together in a loop. Data flows circularly, using `intermediate_inputs` and `intermediate_outputs`, and each block is run iteratively. This is typically used to create a denoising loop which is iterative by default.
15+
[`~modular_pipelines.LoopSequentialPipelineBlocks`] are a multi-block type that composes other [`~modular_pipelines.PipelineBlock`] together in a loop. Data flows circularly, using `intermediate_inputs` and `intermediate_outputs`, and each block is run iteratively. This is typically used to create a denoising loop which is iterative by default.
1616

1717
This guide shows you how to create [`~modular_pipelines.LoopSequentialPipelineBlocks`].
1818

@@ -80,13 +80,13 @@ class LoopBlock(PipelineBlock):
8080

8181
## LoopSequentialPipelineBlocks
8282

83-
Use the [~modular_pipelines.LoopSequentialPipelineBlocks.from_blocks_dict`] method to add the loop block to the loop wrapper to create [~modular_pipelines.LoopSequentialPipelineBlocks`].
83+
Use the [`~modular_pipelines.LoopSequentialPipelineBlocks.from_blocks_dict`] method to add the loop block to the loop wrapper to create [`~modular_pipelines.LoopSequentialPipelineBlocks`].
8484

8585
```py
8686
loop = LoopWrapper.from_blocks_dict({"block1": LoopBlock})
8787
```
8888

89-
Add more loop blocks to run within each iteration with [~modular_pipelines.LoopSequentialPipelineBlocks.from_blocks_dict`]. This allows you to modify the blocks without changing the loop logic itself.
89+
Add more loop blocks to run within each iteration with [`~modular_pipelines.LoopSequentialPipelineBlocks.from_blocks_dict`]. This allows you to modify the blocks without changing the loop logic itself.
9090

9191
```py
9292
loop = LoopWrapper.from_blocks_dict({"block1": LoopBlock(), "block2": LoopBlock})

docs/source/en/modular_diffusers/modular_diffusers_states.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def __call__(self, components, state):
7272

7373
## State interaction
7474

75-
[`~modular_pipelines.PipelineState`] and [`BlockState`] interaction is defined by a block's `inputs`, `intermediate_inputs`, and `intermediate_outputs`.
75+
[`~modular_pipelines.PipelineState`] and [`~modular_pipelines.BlockState`] interaction is defined by a block's `inputs`, `intermediate_inputs`, and `intermediate_outputs`.
7676

7777
- `inputs`, a block can modify an input - like `block_state.image` - but the change is local to the [`~modular_pipelines.BlockState`] and won't affect the original image in [`~modular_pipelines.PipelineState`].
7878
- `intermediate_inputs`, is often values created from a previous block. When a block modifies `intermediate_inputs` - like `batch_size` - this change is reflected in both the [`~modular_pipelines.BlockState`] and [`~modular_pipelines.PipelineState`]. Any subsequent blocks are also affected.

docs/source/en/modular_diffusers/modular_pipeline.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ specific language governing permissions and limitations under the License.
1212

1313
# ModularPipeline
1414

15-
[`ModularPipeline`] converts [`PipelineBlock`]'s into an executable pipeline that loads models and performs the computation steps defined in a block. It is the main interface for users to run a pipeline and it is very similar to the [`DiffusionPipeline`] API.
15+
[`ModularPipeline`] converts [`~modular_pipelines.PipelineBlock`]'s into an executable pipeline that loads models and performs the computation steps defined in a block. It is the main interface for users to run a pipeline and it is very similar to the [`DiffusionPipeline`] API.
1616

1717
The main difference is to include an expected `output` argument in the pipeline.
1818

@@ -92,6 +92,37 @@ image.save("moduar_inpaint_out.png")
9292

9393
This guide will show you how to create a [`ModularPipeline`] and manage the components in it.
9494

95+
## Adding blocks
96+
97+
Blocks are [`InsertableDict`] objects that can be inserted at specific positions, providing a flexible way to mix-and-match blocks.
98+
99+
Use [`~modular_pipelines.modular_pipeline_utils.InsertableDict.insert`] on either the block class or `sub_blocks` attribute to add a block.
100+
101+
```py
102+
# BLOCKS is dict of block classes, you need to add class to it
103+
BLOCKS.insert("block_name", BlockClass, index)
104+
# sub_blocks attribute contains instance, add a block instance to the attribute
105+
t2i_blocks.sub_blocks.insert("block_name", block_instance, index)
106+
```
107+
108+
Use [`~modular_pipelines.modular_pipeline_utils.InsertableDict.pop`] on either the block class or `sub_blocks` attribute to remove a block.
109+
110+
```py
111+
# remove a block class from preset
112+
BLOCKS.pop("text_encoder")
113+
# split out a block instance on its own
114+
text_encoder_block = t2i_blocks.sub_blocks.pop("text_encoder")
115+
```
116+
117+
Swap blocks by setting the existing block to the new block.
118+
119+
```py
120+
# Replace block class in preset
121+
BLOCKS["prepare_latents"] = CustomPrepareLatents
122+
# Replace in sub_blocks attribute using an block instance
123+
t2i_blocks.sub_blocks["prepare_latents"] = CustomPrepareLatents()
124+
```
125+
95126
## Creating a pipeline
96127

97128
There are two ways to create a [`ModularPipeline`]. Assemble and create a pipeline from [`PipelineBlocks`] or load an existing pipeline with [`~ModularPipeline.from_pretrained`].
@@ -104,7 +135,7 @@ You should also initialize a [`ComponentsManager`] to handle device placement an
104135
<hfoptions id="create">
105136
<hfoption id="PipelineBlocks">
106137

107-
Use the [`ModularPipelineBlocks.init_pipeline`] method to create a [`ModularPipeline`] from the component and configuration specifications. This method loads the *specifications* from a `modular_model_index.json` file, but it doesn't load the *models* yet.
138+
Use the [`~ModularPipelineBlocks.init_pipeline`] method to create a [`ModularPipeline`] from the component and configuration specifications. This method loads the *specifications* from a `modular_model_index.json` file, but it doesn't load the *models* yet.
108139

109140
```py
110141
from diffusers import ComponentsManager

docs/source/en/modular_diffusers/overview.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ The Modular Diffusers docs are organized as shown below.
3535

3636
- [States](./modular_diffusers_states) explains how data is shared and communicated between pipeline blocks and [`ModularPipeline`].
3737
- [PipelineBlock](./pipeline_block) is the most basic unit of a [`ModularPipeline`] and this guide shows you how to create one.
38-
- [SequentialPipelineBlocks](./sequential_pipeline_blocks) is a type of block that chains multiple blocks so they run one after another, passing data along the chain. This guide shows you how to create [`SequentialPipelineBlocks`] and how they connect and work together.
39-
- [LoopSequentialPipelineBlocks](./loop_sequential_pipeline_blocks) is a type of block that runs a series of blocks in a loop. This guide shows you how to create [`LoopSequentialPipelineBlocks`].
40-
- [AutoPipelineBlocks](./auto_pipeline_blocks) is a type of block that automatically chooses which blocks to run based on the input. This guide shows you how to create [`AutoPipelineBlocks`].
38+
- [SequentialPipelineBlocks](./sequential_pipeline_blocks) is a type of block that chains multiple blocks so they run one after another, passing data along the chain. This guide shows you how to create [`~modular_pipelines.SequentialPipelineBlocks`] and how they connect and work together.
39+
- [LoopSequentialPipelineBlocks](./loop_sequential_pipeline_blocks) is a type of block that runs a series of blocks in a loop. This guide shows you how to create [`~modular_pipelines.LoopSequentialPipelineBlocks`].
40+
- [AutoPipelineBlocks](./auto_pipeline_blocks) is a type of block that automatically chooses which blocks to run based on the input. This guide shows you how to create [`~modular_pipelines.AutoPipelineBlocks`].

docs/source/en/modular_diffusers/sequential_pipeline_blocks.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ specific language governing permissions and limitations under the License.
1212

1313
# SequentialPipelineBlocks
1414

15-
[`~modular_pipelines.SequentialPipelineBlocks`] are a multi-block type that composes other [`~modular_pipelines.PipelineBlocks`] together in a sequence. Data flows linearly from one block to the next using `intermediate_inputs` and `intermediate_outputs`. Each block in [`~modular_pipelines.SequentialPipelineBlocks`] usually represents a step in the pipeline, and by combining them, you gradually build a pipeline.
15+
[`~modular_pipelines.SequentialPipelineBlocks`] are a multi-block type that composes other [`~modular_pipelines.PipelineBlock`] together in a sequence. Data flows linearly from one block to the next using `intermediate_inputs` and `intermediate_outputs`. Each block in [`~modular_pipelines.SequentialPipelineBlocks`] usually represents a step in the pipeline, and by combining them, you gradually build a pipeline.
1616

1717
This guide shows you how to connect two blocks into a [`~modular_pipelines.SequentialPipelineBlocks`].
1818

19-
Create two [`~modular_pipelines.PipelineBlocks`]. The first block, `InputBlock`, outputs a `batch_size` value and the second block, `ImageEncoderBlock` uses `batch_size` as `intermediate_inputs`.
19+
Create two [`~modular_pipelines.PipelineBlock`]. The first block, `InputBlock`, outputs a `batch_size` value and the second block, `ImageEncoderBlock` uses `batch_size` as `intermediate_inputs`.
2020

2121
<hfoptions id="sequential">
2222
<hfoption id="InputBlock">

0 commit comments

Comments
 (0)