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
Copy file name to clipboardExpand all lines: docs/source/en/modular_diffusers/write_own_pipeline_block.md
+172-6Lines changed: 172 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -262,7 +262,7 @@ I hope by now you have a basic idea about how `PipelineBlock` manages state thro
262
262
263
263
## Create a `SequentialPipelineBlocks`
264
264
265
-
I think by this point, you're already familiar with `SequentialPipelineBlocks` and how to create them with the `from_blocks_dict` API. It's one of the most common ways to use Modular Diffusers, and we've covered it pretty well in the [quicktour](https://moon-ci-docs.huggingface.co/docs/diffusers/pr_9672/en/modular_diffusers/quicktour#modularpipelineblocks).
265
+
I assume that you're already familiar with `SequentialPipelineBlocks` and how to create them with the `from_blocks_dict` API. It's one of the most common ways to use Modular Diffusers, and we've covered it pretty well in the [Getting Started Guide](https://moon-ci-docs.huggingface.co/docs/diffusers/pr_9672/en/modular_diffusers/quicktour#modularpipelineblocks).
266
266
267
267
But how do blocks actually connect and work together? Understanding this is crucial for building effective modular workflows. Let's explore this through an example.
268
268
@@ -331,11 +331,177 @@ At runtime, you have data flow like this:
331
331
332
332
**How SequentialPipelineBlocks Works:**
333
333
334
-
1.**Execution Order**: Blocks are executed in the order they're registered in the `blocks_dict`
335
-
2.**Data Flow**: Outputs from one block become available as intermediate inputs to all subsequent blocks
336
-
3.**Smart Input Resolution**: The pipeline automatically figures out which values need to be provided by the user and which will be generated by previous blocks
337
-
4.**Consistent Interface**: Each block maintains its own behavior and operates through its defined interface, while collectively these interfaces determine what the entire pipeline accepts and produces
334
+
1. Blocks are executed in the order they're registered in the `blocks_dict`
335
+
2. Outputs from one block become available as intermediate inputs to all subsequent blocks
336
+
3. The pipeline automatically figures out which values need to be provided by the user and which will be generated by previous blocks
337
+
4. Each block maintains its own behavior and operates through its defined interface, while collectively these interfaces determine what the entire pipeline accepts and produces
338
338
339
339
What happens within each block follows the same pattern we described earlier: each block gets its own `block_state` with the relevant inputs and intermediate inputs, performs its computation, and updates the pipeline state with its intermediate outputs.
340
340
341
-
## `LoopSequentialPipelineBlocks`
341
+
## `LoopSequentialPipelineBlocks`
342
+
343
+
To create a loop in Modular Diffusers, you could use a single `PipelineBlock` like this:
344
+
345
+
```python
346
+
classDenoiseLoop(PipelineBlock):
347
+
def__call__(self, components, state):
348
+
block_state =self.get_block_state(state)
349
+
for t inrange(block_state.num_inference_steps):
350
+
# ... loop logic here
351
+
pass
352
+
self.add_block_state(state, block_state)
353
+
return components, state
354
+
```
355
+
356
+
Or you could create a `LoopSequentialPipelineBlocks`. The key difference is that with `LoopSequentialPipelineBlocks`, the loop itself is modular: you can add or remove blocks within the loop or reuse the same loop structure with different block combinations.
357
+
358
+
It involves two parts: a **loop wrapper** and **loop blocks**
359
+
360
+
* The **loop wrapper** (`LoopSequentialPipelineBlocks`) defines the loop structure, e.g. it defines the iteration variables, and loop configurations such as progress bar.
361
+
362
+
* The **loop blocks** are basically standard pipeline blocks you add to the loop wrapper.
363
+
- they run sequentially for each iteration of the loop
364
+
- they receive the current iteration index as an additional parameter
365
+
- they share the same block_state throughout the entire loop
366
+
367
+
Unlike regular `SequentialPipelineBlocks` where each block gets its own state, loop blocks share a single state that persists and evolves across iterations.
368
+
369
+
We will build a simple loop block to demonstrate these concepts. Creating a loop block involves three steps:
370
+
1. defining the loop wrapper class
371
+
2. creating the loop blocks
372
+
3. adding the loop blocks to the loop wrapper class to create the loop wrapper instance
373
+
374
+
**Step 1: Define the Loop Wrapper**
375
+
376
+
To create a `LoopSequentialPipelineBlocks` class, you need to define:
377
+
378
+
*`loop_inputs`: User input variables (equivalent to `PipelineBlock.inputs`)
379
+
*`loop_intermediate_inputs`: Intermediate variables needed from the mutable pipeline state (equivalent to `PipelineBlock.intermediates_inputs`)
380
+
*`loop_intermediate_outputs`: New intermediate variables this block will add to the mutable pipeline state (equivalent to `PipelineBlock.intermediates_outputs`)
381
+
*`__call__` method: Defines the loop structure and iteration logic
382
+
383
+
Here is an example of a loop wrapper:
384
+
385
+
```py
386
+
import torch
387
+
from diffusers.modular_pipelines import LoopSequentialPipelineBlocks, PipelineBlock, InputParam, OutputParam
388
+
389
+
classLoopWrapper(LoopSequentialPipelineBlocks):
390
+
model_name ="test"
391
+
@property
392
+
defdescription(self):
393
+
return"I'm a loop!!"
394
+
@property
395
+
defloop_inputs(self):
396
+
return [InputParam(name="num_steps")]
397
+
@torch.no_grad()
398
+
def__call__(self, components, state):
399
+
block_state =self.get_block_state(state)
400
+
# Loop structure - can be customized to your needs
401
+
for i inrange(block_state.num_steps):
402
+
# loop_step executes all registered blocks in sequence
assert x ==20# Each iteration runs 2 blocks, so 10 iterations * 2 = 20
495
+
```
496
+
497
+
**Key Differences from SequentialPipelineBlocks:**
498
+
499
+
The main difference is that loop blocks share the same `block_state` across all iterations, allowing values to accumulate and evolve throughout the loop. Loop blocks could receive additional arguments (like the current iteration index) depending on the loop wrapper's implementation, since the wrapper defines how loop blocks are called. You can easily add, remove, or reorder blocks within the loop without changing the loop logic itself.
500
+
501
+
The officially supported denoising loops in Modular Diffusers are implemented using `LoopSequentialPipelineBlocks`. You can explore the actual implementation to see how these concepts work in practice:
502
+
503
+
```py
504
+
from diffusers.modular_pipelines.stable_diffusion_xl.denoise import StableDiffusionXLDenoiseStep
0 commit comments