Buckets:
| # ModularPipelineBlocks | |
| [ModularPipelineBlocks](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline_blocks#diffusers.ModularPipelineBlocks) は [ModularPipeline](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline) を構築するための基本的なブロックです。パイプライン内の特定のステップにおいて、ブロックが実行すべきコンポーネント、入力/出力、および計算を定義します。[ModularPipelineBlocks](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline_blocks#diffusers.ModularPipelineBlocks) は [state](./modular_diffusers_states) を使用して他のブロックと接続し、ワークフローのモジュール化された構築を可能にします。 | |
| 単体では[ModularPipelineBlocks](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline_blocks#diffusers.ModularPipelineBlocks)を実行することはできません。これは、パイプライン内のステップが何をすべきかを示す設計図です。パイプラインを実際に実行するには、[ModularPipelineBlocks](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline_blocks#diffusers.ModularPipelineBlocks)を[ModularPipeline](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline)に変換する必要があります。 | |
| このガイドでは、[ModularPipelineBlocks](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline_blocks#diffusers.ModularPipelineBlocks)の作成方法について説明します。 | |
| ## 入力と出力 | |
| > [!TIP] | |
| > Modular Diffusers の state の仕組みに詳しくない場合は、[States](./modular_diffusers_states) ガイドを参照してください。 | |
| [ModularPipelineBlocks](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline_blocks#diffusers.ModularPipelineBlocks) には `inputs` と `intermediate_outputs` が必要です。 | |
| - `inputs` は、ブロックが計算を実行するために [PipelineState](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline_states#diffusers.modular_pipelines.PipelineState) から読み取る値です。これには、ユーザーが提供する値(プロンプトや画像など)や、以前のブロックが生成した値(エンコードされた `image_latents` など)が含まれます。 | |
| `InputParam` を使用して`inputs`を定義します。 | |
| ```py | |
| 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](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline_states#diffusers.modular_pipelines.PipelineState) に追加される新しい値です。`intermediate_outputs` は、後続のブロックの `inputs` として利用可能であり、パイプライン実行の最終出力としても利用可能です。 | |
| `OutputParam` を使用して、`intermediate_outputs` を定義します。 | |
| ```py | |
| class ImageEncodeStep(ModularPipelineBlocks): | |
| ... | |
| @property | |
| def intermediate_outputs(self): | |
| return [ | |
| OutputParam(name="image_latents", description="latents representing the image"), | |
| ] | |
| ... | |
| ``` | |
| 中間のインプットとアウトプットはデータを共有してブロックを接続します。これらはいつでもアクセス可能であり、ワークフローの進捗を追跡できます。 | |
| ## コンポーネントと設定 | |
| ブロックに必要なコンポーネントとパイプラインレベルの設定は、[ComponentSpec](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline_components#diffusers.ComponentSpec)と[ConfigSpec](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline_components#diffusers.ConfigSpec)で指定されます。 | |
| - [ComponentSpec](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline_components#diffusers.ComponentSpec)には、ブロックで使用される想定コンポーネントが含まれます。コンポーネントの`name`と、コンポーネントが具体的に何であるかを指定する`type_hint`が必要です。 | |
| - [ConfigSpec](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline_components#diffusers.ConfigSpec)には、すべてのブロックにわたる動作を制御するパイプラインレベルの設定が含まれています。 | |
| ```py | |
| 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. `inputs`のローカルビューを取得するために、[BlockState](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline_states#diffusers.modular_pipelines.BlockState)を取得します。 | |
| 2. `inputs`に対して計算ロジックを実装します。 | |
| 3. ローカルの[BlockState](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline_states#diffusers.modular_pipelines.BlockState)からの変更を、グローバルの[PipelineState](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline_states#diffusers.modular_pipelines.PipelineState)へ反映するために、[PipelineState](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline_states#diffusers.modular_pipelines.PipelineState)を更新します。 | |
| 4. 次のブロックで利用可能になるコンポーネントと状態を返します。 | |
| ```py | |
| 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 | |
| ``` | |
| ## すべてを組み合わせる | |
| 以下は、すべてのパーツを接続した完全なブロックです。 | |
| ```py | |
| 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`プロパティがあります。このプロパティは、ブロックの説明、コンポーネント、入力、出力の概要を提供します。 | |
| ```py | |
| 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 | |
| ``` | |
Xet Storage Details
- Size:
- 8.29 kB
- Xet hash:
- d15cb1898a6f74dbdf4f4657c468950f1714ad3fe1b8aa721431bb3b63b60efb
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.