Diffusers documentation

LoopSequentialPipelineBlocks

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

and get access to the augmented documentation experience

to get started

LoopSequentialPipelineBlocks

LoopSequentialPipelineBlocks 是一种多块类型,用于在循环中组合其他 ModularPipelineBlocks。数据以循环方式流动,使用 inputs 和 intermediate_outputs,并且每个块都会迭代执行。这通常用于创建去噪循环,该循环默认是迭代的。

本指南将向您展示如何创建 LoopSequentialPipelineBlocks。

循环包装器

LoopSequentialPipelineBlocks,也称为循环包装器,因为它定义了循环结构、迭代变量和配置。在循环包装器中,您需要以下变量。

  • loop_inputs 是用户提供的值,等同于 inputs。
  • loop_intermediate_outputs 是由该 block 创建并添加到 PipelineState 中的新的中间变量。它等同于 intermediate_outputs。
  • __call__ 方法定义了循环结构和迭代逻辑。
import torch
from diffusers.modular_pipelines import LoopSequentialPipelineBlocks, ModularPipelineBlocks, InputParam, OutputParam

class LoopWrapper(LoopSequentialPipelineBlocks):
    model_name = "test"
    @property
    def description(self):
        return "I'm a loop!!"
    @property
    def loop_inputs(self):
        return [InputParam(name="num_steps")]
    @torch.no_grad()
    def __call__(self, components, state):
        block_state = self.get_block_state(state)
        # Loop structure - can be customized to your needs
        for i in range(block_state.num_steps):
            # loop_step executes all registered blocks in sequence
            components, block_state = self.loop_step(components, block_state, i=i)
        self.set_block_state(state, block_state)
        return components, state

循环包装器可以向循环块传递额外的参数,例如当前迭代索引。

循环块

循环块是一个 ModularPipelineBlocks,但 __call__ 方法的行为有所不同。

循环块共享同一个 BlockState,以便在循环的每次迭代中累积和更改值。

class LoopBlock(ModularPipelineBlocks):
    model_name = "test"
    @property
    def inputs(self):
        return [InputParam(name="x")]
    @property
    def intermediate_outputs(self):
        # outputs produced by this block
        return [OutputParam(name="x")]
    @property
    def description(self):
        return "I'm a block used inside the `LoopWrapper` class"
    def __call__(self, components, block_state, i: int):
        block_state.x += 1
        return components, block_state

LoopSequentialPipelineBlocks

使用 from_blocks_dict() 方法将循环块添加到循环包装器中,以创建 LoopSequentialPipelineBlocks。

loop = LoopWrapper.from_blocks_dict({"block1": LoopBlock})

使用 from_blocks_dict() 添加更多循环块,以便在每次迭代中运行。这样可以在不更改循环逻辑本身的情况下修改这些块。

loop = LoopWrapper.from_blocks_dict({"block1": LoopBlock(), "block2": LoopBlock})

若要为现有流水线的去噪循环添加一个循环块,例如在每个步骤中运行 callback_on_step_end 函数,请参阅回调指南中的 模块化流水线。

Update on GitHub