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 wrapper로도 알려져 있습니다. loop wrapper 안에는 다음 변수가 필요합니다.

  • loop_inputs는 사용자가 제공한 값이며 inputs와 동일합니다.
  • loop_intermediate_outputs는 블록에 의해 새로 생성되어 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

loop wrapper는 현재 반복 인덱스 같은 추가 인수를 루프 블록에 전달할 수 있습니다.

루프 블록

루프 블록은 ModularPipelineBlocks이지만 __call__ 메서드의 동작 방식이 다릅니다.

  • 반복 변수를 루프 래퍼에서 수신합니다.
  • It works directly with the BlockState instead of the PipelineState.
  • BlockState를 가져오거나 업데이트할 필요가 없습니다.

Loop 블록은 루프의 각 반복에서 값이 누적되고 변경될 수 있도록 동일한 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