状态

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

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

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

PipelineState

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

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

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定义。

< > Update on GitHub