Diffusers documentation

状态

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

and get access to the augmented documentation experience

to get started

状态

块依赖于PipelineStateBlockState数据结构进行通信和数据共享。

状态 描述
PipelineState 维护管道执行所需的整体数据,并允许块读取和更新其数据。
BlockState 允许每个块使用来自inputs的必要数据执行其计算

本指南解释了状态如何工作以及它们如何连接块。

PipelineState

PipelineState是所有块的全局状态容器。它维护管道的完整运行时状态,并为块提供了一种结构化的方式来读取和写入共享数据。

PipelineState中有两个字典用于结构化数据。

  • values字典是一个可变状态,包含用户提供的输入值的副本和由块生成的中间输出值。如果一个块修改了一个input,它将在调用set_block_state后反映在values字典中。
PipelineState(
  values={
    'prompt': 'a cat'
    'guidance_scale': 7.0
    'num_inference_steps': 25
    'prompt_embeds': Tensor(dtype=torch.float32, shape=torch.Size([1, 1, 1, 1]))
    'negative_prompt_embeds': None
  },
)

BlockState

BlockStatePipelineState中相关变量的局部视图,单个块需要这些变量来执行其计算。

直接作为属性访问这些变量,如block_state.image

BlockState(
    image: <PIL.Image.Image image mode=RGB size=512x512 at 0x7F3ECC494640>
)

当一个块的__call__方法被执行时,它用self.get_block_state(state)检索BlockState,执行其操作,并用self.set_block_state(state, block_state)更新PipelineState

def __call__(self, components, state):
    # 检索BlockState
    block_state = self.get_block_state(state)

    # 对输入进行计算的逻辑

    # 更新PipelineState
    self.set_block_state(state, block_state)
    return components, state

状态交互

PipelineStateBlockState的交互由块的inputsintermediate_outputs定义。

  • inputs, 一个块可以修改输入 - 比如 block_state.image - 并且这个改变可以通过调用 set_block_state 全局传播到 PipelineState
  • intermediate_outputs,是一个块创建的新变量。它被添加到 PipelineStatevalues 字典中,并且可以作为后续块的可用变量,或者由用户作为管道的最终输出访问。
< > Update on GitHub