Diffusers documentation
SequentialPipelineBlocks
SequentialPipelineBlocks
SequentialPipelineBlocks, diğer ModularPipelineBlocks öğelerini bir dizi halinde bir araya getiren çoklu blok türüdür. Veri, inputs ve intermediate_outputs kullanılarak bir bloktan diğerine doğrusal olarak akar. SequentialPipelineBlocks içindeki her blok genellikle hattın bir adımını temsil eder ve bunları birleştirerek hattı kademeli olarak oluşturursunuz.
Bu kılavuz, iki bloğu SequentialPipelineBlocks içine nasıl bağlayacağınızı gösterir.
İki ModularPipelineBlocks oluşturun. İlk blok olan InputBlock, bir batch_size değeri üretir; ikinci blok olan ImageEncoderBlock ise batch_size değerini inputs olarak kullanır.
from diffusers.modular_pipelines import ModularPipelineBlocks, InputParam, OutputParam
class InputBlock(ModularPipelineBlocks):
@property
def inputs(self):
return [
InputParam(name="prompt", type_hint=list, description="list of text prompts"),
InputParam(name="num_images_per_prompt", type_hint=int, description="number of images per prompt"),
]
@property
def intermediate_outputs(self):
return [
OutputParam(name="batch_size", description="calculated batch size"),
]
@property
def description(self):
return "A block that determines batch_size based on the number of prompts and num_images_per_prompt argument."
def __call__(self, components, state):
block_state = self.get_block_state(state)
batch_size = len(block_state.prompt)
block_state.batch_size = batch_size * block_state.num_images_per_prompt
self.set_block_state(state, block_state)
return components, stateimport torch
from diffusers.modular_pipelines import ModularPipelineBlocks, InputParam, OutputParam
class ImageEncoderBlock(ModularPipelineBlocks):
@property
def inputs(self):
return [
InputParam(name="image", type_hint="PIL.Image", description="raw input image to process"),
InputParam(name="batch_size", type_hint=int),
]
@property
def intermediate_outputs(self):
return [
OutputParam(name="image_latents", description="latents representing the image"),
]
@property
def description(self):
return "Encode raw image into its latent presentation"
def __call__(self, components, state):
block_state = self.get_block_state(state)
# Simulate processing the image
# This will change the state of the image from a PIL image to a tensor for all blocks
block_state.image = torch.randn(1, 3, 512, 512)
block_state.batch_size = block_state.batch_size * 2
block_state.image_latents = torch.randn(1, 4, 64, 64)
self.set_block_state(state, block_state)
return components, stateİki bloğu SequentialPipelineBlocks tanımlayarak bağlayın. Blok örneklerini block_classes içinde, bunlara karşılık gelen adları ise block_names içinde listeleyin. Bloklar, block_classes içinde göründükleri sırada çalıştırılır ve veri, bir bloktan diğerine PipelineState üzerinden akar.
class ImageProcessingStep(SequentialPipelineBlocks):
"""
# auto_docstring
"""
model_name = "my_model"
block_classes = [InputBlock(), ImageEncoderBlock()]
block_names = ["input", "image_encoder"]
@property
def description(self):
return (
"Process text prompts and images for the pipeline. It:\n"
" - Determines the batch size from the prompts.\n"
" - Encodes the image into latent space."
)Bir SequentialPipelineBlocks oluşturduğunuzda, inputs, intermediate_outputs ve expected_components gibi özellikler alt bloklardan otomatik olarak toplanır, bu nedenle bunları yeniden tanımlamanıza gerek yoktur.
Belirlemeniz gereken birkaç özellik var:
description: Birleştirilen blok için, birleştirilen adımın ne yaptığını açıklayan bir açıklama eklemenizi öneririz.model_name: Bu, alt bloklardan otomatik olarak türetilir ancak her zaman doğru olmayabilir, bu nedenle devre dışı bırakmanız gerekebilir.outputs: Varsayılan olarak bu,intermediate_outputsile aynıdır ancak belgede hangi değerlerin görüneceğini kontrol etmek için manuel olarak ayarlayabilirsiniz. Bu, tüm ara değerler yerine yalnızca nihai çıktıları göstermek için kullanışlıdır.
Bu özellikler, toplanmış inputs, intermediate_outputs ve expected_components ile birlikte, doc özelliğinin otomatik olarak oluşturulmasında kullanılır.
Alt blokları incelemek için ImageProcessingStep bloğunu yazdırın ve bloğun girdileri, çıktıları ve bileşenlerinin tam bir özetini almak için doc kullanın.
blocks = ImageProcessingStep()
print(blocks)
print(blocks.doc)