Diffusers documentation

ModularPipelineBlocks

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

ModularPipelineBlocks

ModularPipelineBlocks هو الكتلة الأساسية لبناء ModularPipeline. يحدد المكونات والمدخلات/المخرجات والحسابات التي يجب أن تنفذها الكتلة لخطوة محددة في خط الأنابيب. تتصل ModularPipelineBlocks بالكتل الأخرى باستخدام state لتسهيل البناء المعياري لسير العمل.

لا يمكن تنفيذ ModularPipelineBlocks بمفرده. فهو مخطط لما ينبغي أن يقوم به كل خطوة في خط الأنابيب. لتشغيل خط الأنابيب وتنفيذه فعليًا، يجب تحويل ModularPipelineBlocks إلى ModularPipeline.

ستوضح لك هذه الدليل كيفية إنشاء ModularPipelineBlocks.

المدخلات والمخرجات

راجع دليل الحالات إذا لم تكن على دراية بكيفية عمل الحالة في Modular Diffusers.

يتطلب ModularPipelineBlocks inputs وintermediate_outputs.

  • inputs هي القيم التي يقرأها الكُتلة من PipelineState لتنفيذ عمليات الحساب الخاصة بها. يمكن أن تكون هذه القيم قيمًا يوفّرها المستخدم (مثل موجه أو صورة) أو قيمًا ينتجها كُتلة سابقة (مثل image_latents المُرمّزة).

    استخدم InputParam لتعريف inputs.

class ImageEncodeStep(ModularPipelineBlocks):
    ...

    @property
    def inputs(self):
        return [
            InputParam(name="image", type_hint="PIL.Image", required=True, description="raw input image to process"),
        ]
    ...
  • intermediate_outputs هي قيم جديدة تنشئها وحدة وتُضاف إلى PipelineState. وتكون intermediate_outputs متاحة كـinputs للوحدات اللاحقة، أو متاحة كإخراج نهائي من تشغيل خط الأنابيب.

    استخدم OutputParam لتعريف intermediate_outputs.

class ImageEncodeStep(ModularPipelineBlocks):
    ...

    @property
    def intermediate_outputs(self):
        return [
            OutputParam(name="image_latents", description="latents representing the image"),
        ]

    ...

تشارك المدخلات والمخرجات الوسيطة البيانات لربط الكتل. وهي متاحة في أي نقطة، مما يتيح لك تتبع تقدم سير العمل.

المكوّنات والإعدادات

يتم تحديد المكونات وإعدادات مستوى خط الأنابيب التي يحتاجها الكتل في ComponentSpec و ConfigSpec.

  • يتضمن ComponentSpec المكونات المتوقعة التي يستخدمها الكتل. تحتاج إلى name للمكوّن، ومن الأفضل أن يكون لديك type_hint يحدد بالضبط ما هو المكوّن.
  • يحتوي ConfigSpec على إعدادات على مستوى خط الأنابيب تتحكم في السلوك عبر جميع الكتل.
class ImageEncodeStep(ModularPipelineBlocks):
    ...

    @property
    def expected_components(self):
        return [
            ComponentSpec(name="vae", type_hint=AutoencoderKL),
        ]

    @property
    def expected_configs(self):
        return [
            ConfigSpec("force_zeros_for_empty_prompt", True),
        ]

    ...

عند تحويل الكتل إلى خط أنابيب، تصبح المكونات متاحة للكتلة كحجة أولى في __call__.

منطق الحساب

يُعرَّف الحساب الذي يؤديه الكتل في طريقة __call__ ويتبع هيكلية محددة.

  1. استرجع BlockState للحصول على عرض محلي لـ inputs.
  2. نفّذ منطق الحساب على inputs.
  3. حدّث PipelineState لدفع التغييرات من BlockState المحلي إلى PipelineState العام.
  4. أعدّ المكونات والحالة التي تصبح متاحةً للكتلة التالية.
class ImageEncodeStep(ModularPipelineBlocks):

    def __call__(self, components, state):
        # Get a local view of the state variables this block needs
        block_state = self.get_block_state(state)

        # Your computation logic here
        # block_state contains all your inputs
        # Access them like: block_state.image, block_state.processed_image

        # Update the pipeline state with your updated block_states
        self.set_block_state(state, block_state)
        return components, state

جمع كل شيء معًا

إليك الكتلة الكاملة مع جميع الأجزاء مترابطة.

from diffusers import ComponentSpec, AutoencoderKL
from diffusers.modular_pipelines import InputParam, ModularPipelineBlocks, OutputParam


class ImageEncodeStep(ModularPipelineBlocks):

    @property
    def description(self):
        return "Encode an image into latent space."

    @property
    def expected_components(self):
        return [
            ComponentSpec(name="vae", type_hint=AutoencoderKL),
        ]

    @property
    def inputs(self):
        return [
            InputParam(name="image", type_hint="PIL.Image", required=True, description="raw input image to process"),
        ]

    @property
    def intermediate_outputs(self):
        return [
            OutputParam(name="image_latents", type_hint="torch.Tensor", description="latents representing the image"),
        ]

    def __call__(self, components, state):
        block_state = self.get_block_state(state)
        block_state.image_latents = components.vae.encode(block_state.image)
        self.set_block_state(state, block_state)
        return components, state

يحتوي كل كتلة على خاصية doc يتم توليدها تلقائيًا من الخصائص التي عرّفتها أعلاه. وهي توفر ملخصًا لوصف الكتلة ومكوّناتها ومدخلاتها ومخرجاتها.

block = ImageEncoderStep()
print(block.doc)
class ImageEncodeStep

  Encode an image into latent space.

  Components:
      vae (`AutoencoderKL`)

  Inputs:
      image (`PIL.Image`):
          raw input image to process

  Outputs:
      image_latents (`torch.Tensor`):
          latents representing the image
Update on GitHub