Diffusers documentation

Pipeline blocks

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.37.0).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Pipeline blocks

ModularPipelineBlocks

class diffusers.ModularPipelineBlocks

< >

( )

Base class for all Pipeline Blocks: ConditionalPipelineBlocks, AutoPipelineBlocks, SequentialPipelineBlocks, LoopSequentialPipelineBlocks

ModularPipelineBlocks provides method to load and save the definition of pipeline blocks.

> This is an experimental feature and is likely to change in the future.

get_block_state

< >

( state: PipelineState )

Get all inputs and intermediates in one dictionary

get_execution_blocks

< >

( **kwargs )

Parameters

  • **kwargs — Input names and values. Only trigger inputs affect block selection.

Get the block(s) that would execute given the inputs. Must be implemented by subclasses that support conditional block selection.

get_workflow

< >

( workflow_name: str )

Parameters

  • workflow_name — Name of the workflow to retrieve.

Get the execution blocks for a specific workflow. Must be implemented by subclasses that define _workflow_map.

init_pipeline

< >

( pretrained_model_name_or_path: str | os.PathLike | None = None components_manager: diffusers.modular_pipelines.components_manager.ComponentsManager | None = None collection: str | None = None )

create a ModularPipeline, optionally accept pretrained_model_name_or_path to load from hub.

SequentialPipelineBlocks

class diffusers.SequentialPipelineBlocks

< >

( )

Parameters

  • block_classes — list of block classes to be used
  • block_names — list of prefixes for each block

A Pipeline Blocks that combines multiple pipeline block classes into one. When called, it will call each block in sequence.

This class inherits from ModularPipelineBlocks. Check the superclass documentation for the generic methods the library implements for all the pipeline blocks (such as loading or saving etc.)

> This is an experimental feature and is likely to change in the future.

from_blocks_dict

< >

( blocks_dict: dict description: str | None = None )

Parameters

  • blocks_dict — Dictionary mapping block names to block classes or instances

Creates a SequentialPipelineBlocks instance from a dictionary of blocks.

get_execution_blocks

< >

( **kwargs )

Parameters

  • **kwargs — Input names and values. Only trigger inputs affect block selection.

Get the blocks that would execute given the specified inputs.

As the traversal walks through sequential blocks, intermediate outputs from resolved blocks are added to the active inputs. This means conditional blocks that depend on intermediates (e.g., “run img2img if image_latents is present”) will resolve correctly, as long as the condition is based on presence/absence (None or not None), not on the actual value.

LoopSequentialPipelineBlocks

class diffusers.LoopSequentialPipelineBlocks

< >

( )

Parameters

  • block_classes — list of block classes to be used
  • block_names — list of prefixes for each block

A Pipeline blocks that combines multiple pipeline block classes into a For Loop. When called, it will call each block in sequence.

This class inherits from ModularPipelineBlocks. Check the superclass documentation for the generic methods the library implements for all the pipeline blocks (such as loading or saving etc.)

> This is an experimental feature and is likely to change in the future.

from_blocks_dict

< >

( blocks_dict: dict )

Parameters

  • blocks_dict — Dictionary mapping block names to block instances

Creates a LoopSequentialPipelineBlocks instance from a dictionary of blocks.

AutoPipelineBlocks

class diffusers.AutoPipelineBlocks

< >

( )

Parameters

  • block_classes — List of block classes to be used. Must have the same length as block_names and block_trigger_inputs.
  • block_names — List of names for each block. Must have the same length as block_classes and block_trigger_inputs.
  • block_trigger_inputs — List of input names where each element specifies the trigger input for the corresponding block. Use None to mark the default block.

A Pipeline Blocks that automatically selects a block to run based on the presence of trigger inputs.

This is a specialized version of ConditionalPipelineBlocks where:

  • Each block has one corresponding trigger input (1:1 mapping)
  • Block selection is automatic: the first block whose trigger input is present gets selected
  • block_trigger_inputs must have the same length as block_names and block_classes
  • Use None in block_trigger_inputs to specify the default block, i.e the block that will run if no trigger inputs are present

Example:

    class MyAutoBlock(AutoPipelineBlocks):
        block_classes = [InpaintEncoderBlock, ImageEncoderBlock, TextEncoderBlock]
        block_names = ["inpaint", "img2img", "text2img"]
        block_trigger_inputs = ["mask_image", "image", None]  # text2img is the default

With this definition:

  • As long as mask_image is provided, “inpaint” block runs (regardless of image being provided or not)
  • If mask_image is not provided but image is provided, “img2img” block runs
  • Otherwise, “text2img” block runs (default, trigger is None)

select_block

< >

( **kwargs )

Select block based on which trigger input is present (not None).

ConditionalPipelineBlocks

class diffusers.ConditionalPipelineBlocks

< >

( )

Parameters

  • block_classes — List of block classes to be used. Must have the same length as block_names.
  • block_names — List of names for each block. Must have the same length as block_classes.
  • block_trigger_inputs — List of input names that select_block() uses to determine which block to run. For ConditionalPipelineBlocks, this does not need to correspond to block_names and block_classes. For AutoPipelineBlocks, this must have the same length as block_names and block_classes, where each element specifies the trigger input for the corresponding block.
  • default_block_name — Name of the default block to run when no trigger inputs match. If None, this block can be skipped entirely when no trigger inputs are provided.

A Pipeline Blocks that conditionally selects a block to run based on the inputs. Subclasses must implement the select_block method to define the logic for selecting the block. Currently, we only support selection logic based on the presence or absence of inputs (i.e., whether they are None or not)

This class inherits from ModularPipelineBlocks. Check the superclass documentation for the generic methods the library implements for all the pipeline blocks (such as loading or saving etc.)

> This is an experimental feature and is likely to change in the future.

get_execution_blocks

< >

( **kwargs )

  • ModularPipelineBlocks

Parameters

  • **kwargs — Input names and values. Only trigger inputs affect block selection.

Returns

  • ModularPipelineBlocks

A leaf block or resolved SequentialPipelineBlocks

  • None: If this block would be skipped (no trigger matched and no default)

Get the block(s) that would execute given the inputs.

Recursively resolves nested ConditionalPipelineBlocks until reaching either:

  • A leaf block (no sub_blocks or LoopSequentialPipelineBlocks) → returns single ModularPipelineBlocks
  • A SequentialPipelineBlocks → delegates to its get_execution_blocks() which returns a SequentialPipelineBlocks containing the resolved execution blocks

select_block

< >

( **kwargs ) str | None

Parameters

  • **kwargs — Trigger input names and their values from the state.

Returns

str | None

The name of the block to run, or None to use default/skip.

Select the block to run based on the trigger inputs. Subclasses must implement this method to define the logic for selecting the block.

Note: When trigger inputs include intermediate outputs from earlier blocks, the selection logic should only depend on the presence or absence of the input (i.e., whether it is None or not), not on its actual value. This is because get_execution_blocks() resolves conditions statically by propagating intermediate output names without their runtime values.

Update on GitHub