Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"ModularPipeline","local":"modularpipeline","sections":[{"title":"パイプラインの作成","local":"creating-a-pipeline","sections":[{"title":"init_pipeline","local":"initpipeline","sections":[],"depth":3},{"title":"from_pretrained","local":"frompretrained","sections":[],"depth":3}],"depth":2},{"title":"コンポーネントの読み込み","local":"loading-components","sections":[],"depth":2},{"title":"コンポーネントの更新","local":"updating-components","sections":[{"title":"AutoModelから","local":"from-automodel","sections":[],"depth":3},{"title":"ComponentSpec から","local":"from-componentspec","sections":[],"depth":3}],"depth":2},{"title":"パイプラインをステージに分割する","local":"splitting-a-pipeline-into-stages","sections":[],"depth":2},{"title":"モジュール化されたリポジトリ","local":"modular-repository","sections":[{"title":"自己完結型のコピーを保存","local":"save-a-self-contained-copy","sections":[],"depth":3},{"title":"他のリポジトリのコンポーネントを参照する","local":"referencing-components-in-other-repositories","sections":[{"title":"既存のモジュラーインデックスを更新する","local":"update-an-existing-modular-index","sections":[],"depth":4}],"depth":3},{"title":"カスタムコード","local":"custom-code","sections":[],"depth":3}],"depth":2}],"depth":1}"/> | |
| <link href="/docs/diffusers/main/en/_app/immutable/entry/start.BlHKsxeM.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/DG-RFPVk.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/DL5wG0aa.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/entry/app.DY7e59sO.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/3xP16cmI.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/DnVIJfC5.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/DsnmJJEf.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/DJVdSu4x.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/nodes/0.DQHfDd8W.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/U_CqR1m1.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/nodes/3.flNpzA7Q.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/HOzRnvhq.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/X_3e9n0u.js" rel="modulepreload"> | |
| <!--10zod39--><meta name="hf:doc:metadata" content="{"title":"ModularPipeline","local":"modularpipeline","sections":[{"title":"Creating a pipeline","local":"creating-a-pipeline","sections":[{"title":"init_pipeline","local":"initpipeline","sections":[],"depth":3},{"title":"from_pretrained","local":"frompretrained","sections":[],"depth":3}],"depth":2},{"title":"Loading components","local":"loading-components","sections":[],"depth":2},{"title":"Updating components","local":"updating-components","sections":[{"title":"From AutoModel","local":"from-automodel","sections":[],"depth":3},{"title":"From ComponentSpec","local":"from-componentspec","sections":[],"depth":3}],"depth":2},{"title":"Splitting a pipeline into stages","local":"splitting-a-pipeline-into-stages","sections":[],"depth":2},{"title":"Modular repository","local":"modular-repository","sections":[{"title":"Save a self-contained copy","local":"save-a-self-contained-copy","sections":[],"depth":3},{"title":"Referencing components in other repositories","local":"referencing-components-in-other-repositories","sections":[{"title":"Update an existing modular index","local":"update-an-existing-modular-index","sections":[],"depth":4}],"depth":3},{"title":"Custom code","local":"custom-code","sections":[],"depth":3}],"depth":2}],"depth":1}"/><!----> | |
| <link href="/docs/diffusers/main/en/_app/immutable/assets/0.tn0RQdqM.css" rel="modulepreload"> <!--[--><!--[0--><!--[--><!--[0--><!--[--><p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: inline-end; margin-inline-start: 10px; display: inline-flex; position: relative; z-index: 10;"><div dir="ltr" class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----></span> <span dir="auto">Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg><!----></button></div> <!--[-1--><!--]--></div><!----> <!--[0--><h1 class="relative group"><a id="modularpipeline" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#modularpipeline"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->ModularPipeline<!----></span></h1><!--]--><!----> <p><a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline">ModularPipeline</a> は、<a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline_blocks#diffusers.ModularPipelineBlocks">ModularPipelineBlocks</a> を実行可能なパイプラインに変換し、モデルの読み込みとブロックで定義された計算ステップの実行を行います。パイプラインの実行における主要なインターフェースであり、API は <a href="/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline">DiffusionPipeline</a> と非常に似ていますが、いくつか重要な違いがあります。</p> <ul><li><p><strong>Loading is lazy.</strong> With <a href="/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline">DiffusionPipeline</a>, <a href="/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained">from_pretrained()</a> creates the pipeline and loads all models at the same time. With <a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline">ModularPipeline</a>, creating and loading are two separate steps: <a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline.from_pretrained">from_pretrained()</a> reads the configuration and knows where to load each component from, but doesn’t actually load the model weights. You load the models later with <a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline.load_components">load_components()</a>, which is where you pass loading arguments like <code>dtype</code> and <code>quantization_config</code>.</p></li> <li><p><strong>パイプラインを作成する2つの方法</strong>。既存の diffusers モデルリポジトリに対して <a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline.from_pretrained">from_pretrained()</a> を使用すると、デフォルトのパイプラインブロックに自動的にマッピングされ、追加の設定なしで <a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline">ModularPipeline</a> に変換されます。現在サポートされているモデルを確認するには、<a href="https://github.com/huggingface/diffusers/tree/main/src/diffusers/modular_pipelines" rel="nofollow">modular_pipelines_directory</a> を参照してください。また、<a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline_blocks#diffusers.ModularPipelineBlocks">ModularPipelineBlocks</a> から独自のパイプラインを組み立て、<a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline_blocks#diffusers.ModularPipelineBlocks.init_pipeline">init_pipeline()</a> メソッドで変換することもできます(詳細は <a href="#creating-a-pipeline">パイプラインの作成</a> を参照)。</p></li> <li><p><strong>パイプラインの実行方法は同じです。</strong> 読み込みが完了すると、これまで通り同じ引数でパイプラインを呼び出せます。パイプラインのブロックが<a href="./auto_pipeline_blocks"><code>AutoPipelineBlocks</code></a>を使用して入力の種類に基づいてワークフローを自動的に選択する場合、単一の<a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline">ModularPipeline</a>で複数のワークフロー(テキストから画像へ、画像から画像へ、インペインティングなど)をサポートできます。</p></li></ul> <p>以下は、SDXLによるテキストから画像への変換、画像から画像への変換、インペインティングの完全な例です。</p> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">テキストから画像</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">image-to-image</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">inpainting</div><!--]--></div> <div class="language-select"><div><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> ModularPipeline | |
| pipeline = ModularPipeline.from_pretrained(<span class="hljs-string">"stabilityai/stable-diffusion-xl-base-1.0"</span>) | |
| pipeline.load_components(dtype=torch.float16) | |
| pipeline.to(<span class="hljs-string">"cuda"</span>) <span class="hljs-comment"># or "mps", "xpu", "cpu"</span> | |
| image = pipeline(prompt=<span class="hljs-string">"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"</span>).images[<span class="hljs-number">0</span>] | |
| image.save(<span class="hljs-string">"modular_t2i_out.png"</span>)<!----></pre></div><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> ModularPipeline | |
| <span class="hljs-keyword">from</span> diffusers.utils <span class="hljs-keyword">import</span> load_image | |
| pipeline = ModularPipeline.from_pretrained(<span class="hljs-string">"stabilityai/stable-diffusion-xl-base-1.0"</span>) | |
| pipeline.load_components(dtype=torch.float16) | |
| pipeline.to(<span class="hljs-string">"cuda"</span>) <span class="hljs-comment"># or "mps", "xpu", "cpu"</span> | |
| url = <span class="hljs-string">"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/sdxl-text2img.png"</span> | |
| init_image = load_image(url) | |
| prompt = <span class="hljs-string">"a dog catching a frisbee in the jungle"</span> | |
| image = pipeline(prompt=prompt, image=init_image, strength=<span class="hljs-number">0.8</span>).images[<span class="hljs-number">0</span>] | |
| image.save(<span class="hljs-string">"modular_i2i_out.png"</span>)<!----></pre></div><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> ModularPipeline | |
| <span class="hljs-keyword">from</span> diffusers.utils <span class="hljs-keyword">import</span> load_image | |
| pipeline = ModularPipeline.from_pretrained(<span class="hljs-string">"stabilityai/stable-diffusion-xl-base-1.0"</span>) | |
| pipeline.load_components(dtype=torch.float16) | |
| pipeline.to(<span class="hljs-string">"cuda"</span>) <span class="hljs-comment"># or "mps", "xpu", "cpu"</span> | |
| img_url = <span class="hljs-string">"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/sdxl-text2img.png"</span> | |
| mask_url = <span class="hljs-string">"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/sdxl-inpaint-mask.png"</span> | |
| init_image = load_image(img_url) | |
| mask_image = load_image(mask_url) | |
| prompt = <span class="hljs-string">"A deep sea diver floating"</span> | |
| image = pipeline(prompt=prompt, image=init_image, mask_image=mask_image, strength=<span class="hljs-number">0.85</span>).images[<span class="hljs-number">0</span>] | |
| image.save(<span class="hljs-string">"modular_inpaint_out.png"</span>)<!----></pre></div><!----></div><!----><!----></div><!----> <p>このガイドでは、<a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline">ModularPipeline</a>の作成方法、そのコンポーネントの管理方法、およびパイプラインの実行方法について説明します。</p> <!--[1--><h2 class="relative group"><a id="creating-a-pipeline" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#creating-a-pipeline"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->パイプラインの作成<!----></span></h2><!--]--><!----> <p>There are two ways to create a <a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline">ModularPipeline</a>. Assemble and create a pipeline from <a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline_blocks#diffusers.ModularPipelineBlocks">ModularPipelineBlocks</a> with <a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline_blocks#diffusers.ModularPipelineBlocks.init_pipeline">init_pipeline()</a>, or load an existing pipeline with <a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline.from_pretrained">from_pretrained()</a>.</p> <p><a href="./components_manager"><code>ComponentsManager</code></a> を初期化して、デバイスの配置とメモリ管理を処理することもできます。自動オフロードが不要な場合は、この手順を省略し、<code>pipeline.to("cuda")</code> を使用してパイプラインを手動でデバイスに移動できます。</p> <blockquote class="tip"><p>異なるワークフロー間でコンポーネントの管理をどのように支援できるかについては、<a href="./components_manager">ComponentsManager</a>のドキュメントを参照してください。</p></blockquote> <!--[2--><h3 class="relative group"><a id="initpipeline" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#initpipeline"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->init_pipeline<!----></span></h3><!--]--><!----> <p><a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline_blocks#diffusers.ModularPipelineBlocks.init_pipeline">init_pipeline()</a> は、任意の <a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline_blocks#diffusers.ModularPipelineBlocks">ModularPipelineBlocks</a> を <a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline">ModularPipeline</a> に変換します。</p> <p>仕組みを確認するために、最小限のブロックを定義してみましょう。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> CLIPTextModel | |
| <span class="hljs-keyword">from</span> diffusers.modular_pipelines <span class="hljs-keyword">import</span> ( | |
| ComponentSpec, | |
| ModularPipelineBlocks, | |
| PipelineState, | |
| ) | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyBlock</span>(<span class="hljs-title class_ inherited__">ModularPipelineBlocks</span>): | |
| <span class="hljs-meta"> @property</span> | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">expected_components</span>(<span class="hljs-params">self</span>): | |
| <span class="hljs-keyword">return</span> [ | |
| ComponentSpec( | |
| name=<span class="hljs-string">"text_encoder"</span>, | |
| type_hint=CLIPTextModel, | |
| pretrained_model_name_or_path=<span class="hljs-string">"openai/clip-vit-large-patch14"</span>, | |
| ), | |
| ] | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">__call__</span>(<span class="hljs-params">self, components, state: PipelineState</span>) -> PipelineState: | |
| <span class="hljs-keyword">return</span> components, state<!----></pre></div><!----> <p><a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline_blocks#diffusers.ModularPipelineBlocks.init_pipeline">init_pipeline()</a>を呼び出して、パイプラインに変換します。パイプラインの<code>blocks</code>属性は、パイプラインが作成されたブロックであり、期待される入力、出力、および計算ロジックを決定します。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-py "><!---->block = MyBlock() | |
| pipe = block.init_pipeline() | |
| pipe.blocks<!----></pre></div><!----> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class=" "><!----><span class="hljs-title class_">MyBlock</span> <span class="hljs-punctuation">{</span> | |
| <span class="hljs-string">"_class_name"</span>: <span class="hljs-string">"MyBlock"</span>, | |
| <span class="hljs-string">"_diffusers_version"</span>: <span class="hljs-string">"0.37.0.dev0"</span> | |
| <span class="hljs-punctuation">}</span><!----></pre></div><!----> <blockquote class="warning"><p>ブロックは変更可能であり、パイプラインを作成する前に、ブロックの追加、削除、交換を自由に実行できます。ただし、パイプラインが作成された後で<code>pipeline.blocks</code>を変更しても、<code>pipeline.blocks</code>はコピーを返すため、パイプラインには影響しません。異なるブロック構造を使用したい場合は、ブロックを変更した後に新しいパイプラインを作成してください。</p></blockquote> <p>リポジトリを指定せずに<a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline_blocks#diffusers.ModularPipelineBlocks.init_pipeline">init_pipeline()</a>を呼び出すと、ブロックの<a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline_components#diffusers.ComponentSpec">ComponentSpec</a>で定義された<code>pretrained_model_name_or_path</code>を使用して、各コンポーネントの読み込み元が決定されます。パイプラインを出力すると、コンポーネントの読み込み設定が表示されます。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-py "><!---->pipe | |
| ModularPipeline { | |
| <span class="hljs-string">"_blocks_class_name"</span>: <span class="hljs-string">"MyBlock"</span>, | |
| <span class="hljs-string">"_class_name"</span>: <span class="hljs-string">"ModularPipeline"</span>, | |
| <span class="hljs-string">"_diffusers_version"</span>: <span class="hljs-string">"0.37.0.dev0"</span>, | |
| <span class="hljs-string">"text_encoder"</span>: [ | |
| null, | |
| null, | |
| { | |
| <span class="hljs-string">"pretrained_model_name_or_path"</span>: <span class="hljs-string">"openai/clip-vit-large-patch14"</span>, | |
| <span class="hljs-string">"revision"</span>: null, | |
| <span class="hljs-string">"subfolder"</span>: <span class="hljs-string">""</span>, | |
| <span class="hljs-string">"type_hint"</span>: [ | |
| <span class="hljs-string">"transformers"</span>, | |
| <span class="hljs-string">"CLIPTextModel"</span> | |
| ], | |
| <span class="hljs-string">"variant"</span>: null | |
| } | |
| ] | |
| }<!----></pre></div><!----> <p><a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline_blocks#diffusers.ModularPipelineBlocks.init_pipeline">init_pipeline()</a> にリポジトリを渡すと、そのリポジトリ内のパイプライン設定(<code>model_index.json</code> または <code>modular_model_index.json</code>)とブロックのコンポーネントを照合して、読み込みパスを上書きします。読み込み仕様がどのように記録・保存されるかは、<a href="#modular-repository">Modular repository</a> を参照してください。</p> <p>以下の例では、<code>pretrained_model_name_or_path</code> が <code>"stabilityai/stable-diffusion-xl-base-1.0"</code> に更新されます。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-py "><!---->pipe = block.init_pipeline(<span class="hljs-string">"stabilityai/stable-diffusion-xl-base-1.0"</span>) | |
| pipe | |
| ModularPipeline { | |
| <span class="hljs-string">"_blocks_class_name"</span>: <span class="hljs-string">"MyBlock"</span>, | |
| <span class="hljs-string">"_class_name"</span>: <span class="hljs-string">"ModularPipeline"</span>, | |
| <span class="hljs-string">"_diffusers_version"</span>: <span class="hljs-string">"0.37.0.dev0"</span>, | |
| <span class="hljs-string">"text_encoder"</span>: [ | |
| null, | |
| null, | |
| { | |
| <span class="hljs-string">"pretrained_model_name_or_path"</span>: <span class="hljs-string">"stabilityai/stable-diffusion-xl-base-1.0"</span>, | |
| <span class="hljs-string">"revision"</span>: null, | |
| <span class="hljs-string">"subfolder"</span>: <span class="hljs-string">"text_encoder"</span>, | |
| <span class="hljs-string">"type_hint"</span>: [ | |
| <span class="hljs-string">"transformers"</span>, | |
| <span class="hljs-string">"CLIPTextModel"</span> | |
| ], | |
| <span class="hljs-string">"variant"</span>: null | |
| } | |
| ] | |
| }<!----></pre></div><!----> <p>ブロック内のコンポーネントがリポジトリに存在しない場合、<code>null</code> のままとなり、<a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline.load_components">load_components()</a> 実行時にスキップされます。</p> <!--[2--><h3 class="relative group"><a id="frompretrained" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#frompretrained"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->from_pretrained<!----></span></h3><!--]--><!----> <p><a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline.from_pretrained">from_pretrained()</a> は、ブロックを自分で定義せずに <a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline">ModularPipeline</a> を作成するための便利な方法です。</p> <p>3種類のリポジトリで動作します。</p> <p><strong>通常の diffusers リポジトリ。</strong>サポートされているモデルリポジトリを渡すと、自動的にデフォルトのパイプラインブロックにマッピングされます。現在サポートされているモデルには、SDXL、Wan、Qwen、Z-Image、Flux、Flux2 が含まれます。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> ModularPipeline, ComponentsManager | |
| components = ComponentsManager() | |
| pipeline = ModularPipeline.from_pretrained( | |
| <span class="hljs-string">"stabilityai/stable-diffusion-xl-base-1.0"</span>, components_manager=components | |
| )<!----></pre></div><!----> <p><strong>モジュール型リポジトリ。</strong>これらのリポジトリには、各コンポーネントのクラスと読み込み元を指定する<code>modular_model_index.json</code>が含まれており、コンポーネントはこれと並べて保存されます。作成方法については、<a href="#modular-repository">モジュール型リポジトリ</a>を参照してください。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> ModularPipeline, ComponentsManager | |
| components = ComponentsManager() | |
| pipeline = ModularPipeline.from_pretrained( | |
| <span class="hljs-string">"MiniMaxAI/MiniMax-H3"</span>, components_manager=components | |
| )<!----></pre></div><!----> <p><strong>カスタムコードを含むモジュラーリポジトリ。</strong> 一部のリポジトリには、読み込み設定に加えてカスタムパイプラインブロックが含まれています。これらをロードするには、<code>trust_remote_code=True</code> を追加してください。独自のブロックを作成する方法については、<a href="./custom_blocks">カスタムブロック</a> を参照してください。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> ModularPipeline, ComponentsManager | |
| components = ComponentsManager() | |
| pipeline = ModularPipeline.from_pretrained( | |
| <span class="hljs-string">"diffusers/Florence2-image-Annotator"</span>, trust_remote_code=<span class="hljs-literal">True</span>, components_manager=components | |
| )<!----></pre></div><!----> <p>パイプラインブロックがワークフローを定義している場合(<code>pipeline.blocks.available_workflows</code> を確認してください)、<code>workflow=</code> を渡すことで、そのワークフローのブロックのみを保持できます。これは <a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline_blocks#diffusers.ModularPipelineBlocks.get_workflow">get_workflow()</a> と同じプルーニング処理です。これにより、パイプラインは当該ワークフローが使用するコンポーネントのみを宣言し、その docstring には該当するワークフローの入力が正確に記述されます。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-py "><!---->pipeline = ModularPipeline.from_pretrained(<span class="hljs-string">"Qwen/Qwen-Image"</span>, workflow=<span class="hljs-string">"inpainting"</span>)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="loading-components" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#loading-components"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->コンポーネントの読み込み<!----></span></h2><!--]--><!----> <p><a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline">ModularPipeline</a> は、コンポーネントを自動的にインスタンス化しません。設定とコンポーネントの仕様を読み込むだけです。<a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline.load_components">load_components()</a> を使用してコンポーネントを読み込むことができます。</p> <p>これにより、有効な読み込み仕様を持つすべてのコンポーネントが読み込まれます。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> torch | |
| pipeline.load_components(dtype=torch.float16)<!----></pre></div><!----> <p>名前を指定して特定のコンポーネントを読み込むこともできます。以下の例では、<code>text_encoder</code>のみを読み込みます。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-py "><!---->pipeline.load_components(names=[<span class="hljs-string">"text_encoder"</span>], dtype=torch.float16)<!----></pre></div><!----> <p>ブロックがワークフローを定義するパイプラインにおいて、<code>workflow=</code> はそのワークフローが使用するコンポーネントのみを読み込みます。パイプラインはすべてのブロックを保持するため、これは1つのパイプラインを複数のワークフローにわたって実行するための便利な方法です。各呼び出しでは、新しいワークフローがまだ欠いているものだけが追加されます。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-py "><!---->pipeline.load_components(workflow=<span class="hljs-string">"inpainting"</span>, dtype=torch.float16)<!----></pre></div><!----> <p>読み込み後、パイプラインを出力すると、どのコンポーネントが読み込まれたかが表示されます。最初の2つのフィールドは、<code>null</code>からコンポーネントのライブラリとクラスに変更されます。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-py "><!---->pipeline<!----></pre></div><!----> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class=" "><!----><span class="hljs-comment"># text_encoder is loaded - shows library and class</span> | |
| <span class="hljs-string">"text_encoder"</span>: [ | |
| <span class="hljs-string">"transformers"</span>, | |
| <span class="hljs-string">"CLIPTextModel"</span>, | |
| { ... } | |
| ] | |
| <span class="hljs-comment"># unet is not loaded yet - still null</span> | |
| <span class="hljs-string">"unet"</span>: [ | |
| <span class="hljs-literal">null</span>, | |
| <span class="hljs-literal">null</span>, | |
| { ... } | |
| ]<!----></pre></div><!----> <p><code>dtype</code>、<code>variant</code>、<code>revision</code>、<code>quantization_config</code> などのキーワード引数は、各コンポーネントに対して <code>from_pretrained()</code> に渡されます。すべてのコンポーネントに適用する単一の値を渡すことも、コンポーネントごとに値を設定するための dict を渡すこともできます。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-comment"># apply bfloat16 to all components</span> | |
| pipeline.load_components(dtype=torch.bfloat16) | |
| <span class="hljs-comment"># different dtypes per component</span> | |
| pipeline.load_components(dtype={<span class="hljs-string">"transformer"</span>: torch.bfloat16, <span class="hljs-string">"default"</span>: torch.float32})<!----></pre></div><!----> <p><a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline.load_components">load_components()</a> は、まだ読み込まれておらず、有効な読み込み仕様を持つコンポーネントのみを読み込みます。つまり、パイプラインにコンポーネントがすでに設定されている場合、<a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline.load_components">load_components()</a> を再度呼び出しても、そのコンポーネントは再読み込みされません。</p> <!--[1--><h2 class="relative group"><a id="updating-components" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#updating-components"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->コンポーネントの更新<!----></span></h2><!--]--><!----> <p><a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline.update_components">update_components()</a> は、パイプライン内のコンポーネントを新しいものに置き換えます。コンポーネントが更新されると、パイプライン設定内の読み込み仕様も更新され、<a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline.load_components">load_components()</a> は以降の呼び出しでそれをスキップします。</p> <!--[2--><h3 class="relative group"><a id="from-automodel" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#from-automodel"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->AutoModelから<!----></span></h3><!--]--><!----> <p><code>AutoModel.from_pretrained()</code> で読み込まれたモデルオブジェクトを渡すことができます。この方法で読み込まれたモデルには、読み込み情報が自動的にタグ付けされます。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoModel | |
| unet = AutoModel.from_pretrained( | |
| <span class="hljs-string">"RunDiffusion/Juggernaut-XL-v9"</span>, subfolder=<span class="hljs-string">"unet"</span>, variant=<span class="hljs-string">"fp16"</span>, dtype=torch.float16 | |
| ) | |
| pipeline.update_components(unet=unet)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="from-componentspec" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#from-componentspec"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->ComponentSpec から<!----></span></h3><!--]--><!----> <p><a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline.get_component_spec">get_component_spec()</a> を使用して、現在のコンポーネント仕様のコピーを取得し、変更を加えたうえで、新しいコンポーネントを読み込みます。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-py "><!---->unet_spec = pipeline.get_component_spec(<span class="hljs-string">"unet"</span>) | |
| <span class="hljs-comment"># modify to load from a different repository</span> | |
| unet_spec.pretrained_model_name_or_path = <span class="hljs-string">"RunDiffusion/Juggernaut-XL-v9"</span> | |
| <span class="hljs-comment"># load and update</span> | |
| unet = unet_spec.load(dtype=torch.float16) | |
| pipeline.update_components(unet=unet)<!----></pre></div><!----> <p><a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline_components#diffusers.ComponentSpec">ComponentSpec</a> をゼロから作成することもできます。</p> <p>すべてのコンポーネントが事前学習済み重みから読み込まれるわけではありません。一部のコンポーネントは<code>pipeline.config_component_names</code>に列挙されている設定から作成されます。これらのコンポーネントには、<a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline_components#diffusers.ComponentSpec.load">load()</a>ではなく<a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline_components#diffusers.ComponentSpec.create">create()</a>を使用してください。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-py "><!---->guider_spec = pipeline.get_component_spec(<span class="hljs-string">"guider"</span>) | |
| guider_spec.config = {<span class="hljs-string">"guidance_scale"</span>: <span class="hljs-number">5.0</span>} | |
| guider = guider_spec.create() | |
| pipeline.update_components(guider=guider)<!----></pre></div><!----> <p>または、オブジェクトを直接渡すこともできます。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> diffusers.guiders <span class="hljs-keyword">import</span> ClassifierFreeGuidance | |
| guider = ClassifierFreeGuidance(guidance_scale=<span class="hljs-number">5.0</span>) | |
| pipeline.update_components(guider=guider)<!----></pre></div><!----> <p>利用可能なGuiderとその設定方法の詳細については、<a href="../using-diffusers/guiders">Guider</a>のガイドを参照してください。</p> <!--[1--><h2 class="relative group"><a id="splitting-a-pipeline-into-stages" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#splitting-a-pipeline-into-stages"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->パイプラインをステージに分割する<!----></span></h2><!--]--><!----> <p>ブロックは合成可能であるため、パイプラインを分解し、各ステージごとに独立したパイプラインとして再構築できます。以下の例では、テキストエンコーダーブロックをパイプラインの残りの部分から分離する方法を示しています。これにより、プロンプトを独立してエンコードし、その埋め込みベクトルをメインのパイプラインに渡すことができます。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> ModularPipeline, ComponentsManager | |
| <span class="hljs-keyword">import</span> torch | |
| device = <span class="hljs-string">"cuda"</span> <span class="hljs-comment"># or "mps", "xpu", "cpu"</span> | |
| dtype = torch.bfloat16 | |
| repo_id = <span class="hljs-string">"black-forest-labs/FLUX.2-klein-4B"</span> | |
| <span class="hljs-comment"># get the blocks and separate out the text encoder</span> | |
| blocks = ModularPipeline.from_pretrained(repo_id).blocks | |
| text_block = blocks.sub_blocks.pop(<span class="hljs-string">"text_encoder"</span>) | |
| <span class="hljs-comment"># use ComponentsManager to handle offloading across multiple pipelines</span> | |
| manager = ComponentsManager() | |
| manager.enable_auto_cpu_offload(device=device) | |
| <span class="hljs-comment"># create separate pipelines for each stage</span> | |
| text_encoder_pipeline = text_block.init_pipeline(repo_id, components_manager=manager) | |
| pipeline = blocks.init_pipeline(repo_id, components_manager=manager) | |
| <span class="hljs-comment"># encode text</span> | |
| text_encoder_pipeline.load_components(dtype=dtype) | |
| text_embeddings = text_encoder_pipeline(prompt=<span class="hljs-string">"a cat"</span>).get_by_kwargs(<span class="hljs-string">"denoiser_input_fields"</span>) | |
| <span class="hljs-comment"># denoise and decode</span> | |
| pipeline.load_components(dtype=dtype) | |
| output = pipeline( | |
| **text_embeddings, | |
| num_inference_steps=<span class="hljs-number">4</span>, | |
| ).images[<span class="hljs-number">0</span>]<!----></pre></div><!----> <p><a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline_components#diffusers.ComponentsManager">ComponentsManager</a>は、複数のパイプラインにまたがるメモリを管理します。<a href="/docs/diffusers/main/ja/api/pipelines/overview#diffusers.DiffusionPipeline">DiffusionPipeline</a>のオフローディング戦略が固定の順序に従うのに対し、<a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline_components#diffusers.ComponentsManager">ComponentsManager</a>は、モデルのフォワードパスが実行されるたびに、現在のメモリ状況に基づいて動的にオフローディングの判断を行います。そのため、作成するパイプラインの数がいくつであれ、どの順序で実行しても動作します。詳細については、<a href="./components_manager">ComponentsManager</a>のガイドを参照してください。</p> <p>パイプラインのステージがコンポーネントを共有する場合(例: エンコードとデコードの両方に同じVAEを使用する場合)、<a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline.update_components">update_components()</a> を使用して、すでに読み込まれたコンポーネントを別のパイプラインに渡すことで、再度読み込む必要をなくすことができます。</p> <!--[1--><h2 class="relative group"><a id="modular-repository" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#modular-repository"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->モジュール化されたリポジトリ<!----></span></h2><!--]--><!----> <p>パイプラインブロックが<em>事前学習済みコンポーネント</em>を使用する場合、リポジトリが必要です。リポジトリは読み込み仕様とメタデータを提供します。</p> <p><a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline">ModularPipeline</a>は、通常のdiffusersリポジトリとそのまま連携できます。ただし、より柔軟性を高めるために<em>モジュラーリポジトリ</em>を作成することも可能です。モジュラーリポジトリには、以下の3つの要素を含む<code>modular_model_index.json</code>ファイルが含まれます。</p> <ul><li><code>library</code> と <code>class</code> は、コンポーネントがどのライブラリから読み込まれたか、およびそのクラスを示します。<code>null</code> の場合、コンポーネントはまだ読み込まれていません。</li> <li><code>loading_specs_dict</code>には、コンポーネントを読み込むために必要な情報(読み込む対象のリポジトリやサブフォルダなど)が含まれています。</li></ul> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> ModularPipeline | |
| <span class="hljs-comment"># load from a regular repo</span> | |
| pipeline = ModularPipeline.from_pretrained(<span class="hljs-string">"stabilityai/stable-diffusion-xl-base-1.0"</span>) | |
| pipeline.load_components() | |
| <span class="hljs-comment"># push as a modular repository</span> | |
| pipeline.save_pretrained(<span class="hljs-string">"local/path"</span>, repo_id=<span class="hljs-string">"my-username/sdxl-modular"</span>, push_to_hub=<span class="hljs-literal">True</span>)<!----></pre></div><!----> <p>デフォルトでは、<a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline.save_pretrained">save_pretrained()</a>は、Diffusersがシリアライズできる現在ロードされている各コンポーネントを書き出します。ロードされていない、またはシリアライズできないコンポーネントは書き出されず、既存のロード仕様を維持します。</p> <!--[2--><h3 class="relative group"><a id="save-a-self-contained-copy" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#save-a-self-contained-copy"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->自己完結型のコピーを保存<!----></span></h3><!--]--><!----> <p>すべてのコンポーネントを読み込み、保存します。すべての仕様は結果を指しているため、オフラインを含むすべてのコンポーネントが1つの場所から完全に再読み込みされます。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-py "><!---->pipe = ModularPipeline.from_pretrained(<span class="hljs-string">"MiniMaxAI/MiniMax-H3"</span>) | |
| pipe.load_components() | |
| pipe.save_pretrained(<span class="hljs-string">"path/to/local-copy"</span>)<!----></pre></div><!----> <p><code>modular_model_index.json</code> に記録される保存先は、<a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline.save_pretrained">save_pretrained()</a> が書き込む場所です。ローカル保存の場合は <code>save_directory</code>、<code>push_to_hub=True</code> を渡す場合は <code>repo_id</code> になります。</p> <p>ファイルを他の方法で移動しても、インデックスは書き換えられません。<code>hf download --local-dir</code>でダウンロードしたコピーは引き続き動作します。ローカルディレクトリからパイプラインを読み込む場合、そのディレクトリにファイルが存在するコンポーネントは、記録されたリポジトリではなく、そこから読み込まれます。<code>hf upload</code>でアップロードしたコピーは、旧場所を指し続けます。そのため、その場合はインデックスを手動で更新してください。</p> <p>自己完結型のコピーは、キャッシュに既に存在するファイルに対して追加のディスク容量を消費しません。同一のファイルはリポジトリ間で1つのキャッシュコピーを共有するため、元のリポジトリから変更なしで保存されたコンポーネントは、すでにダウンロード済みのファイルを再利用します。ファイルが共有される条件については、<a href="../installation#share-files-across-repositories">リポジトリ間でファイルを共有する</a>を参照してください。</p> <!--[2--><h3 class="relative group"><a id="referencing-components-in-other-repositories" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#referencing-components-in-other-repositories"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->他のリポジトリのコンポーネントを参照する<!----></span></h3><!--]--><!----> <p><code>modular_model_index.json</code> におけるコンポーネントの読み込み仕様は、別のリポジトリを指すことができます。例えば、<a href="https://huggingface.co/diffusers/flux2-bnb-4bit-modular" rel="nofollow">diffusers/flux2-bnb-4bit-modular</a> は、量子化された transformer を <code>diffusers/FLUX.2-dev-bnb-4bit</code> から、残りのコンポーネントを <code>black-forest-labs/FLUX.2-dev</code> から読み込みます。</p> <p>自己完結型のリポジトリを強く推奨します。すべてのコンポーネントを1つのリポジトリにまとめることで、他のリポジトリに依存せずに、パイプラインをダウンロード、共有、オフラインで利用しやすくなります。コンポーネントを別の場所にホストすることを意図している場合のみ、リポジトリ間の参照を使用してください。</p> <p><code>overwrite_modular_index=False</code> を渡すことで、<code>modular_model_index.json</code> 内の読み込み仕様をそのまま維持できます。保存済みコンポーネントの読み込み仕様が空の場合、保持すべき内容がないため、宛先が自動的に補完されます。</p> <p><code>huggingface_hub>=1.32.0</code> 以降、Xet ベースの同一ファイルはシンボリックリンクを介してリポジトリ間で 1 つのキャッシュ済みペイロードを共有でき、重複したダウンロードやディスク使用量を回避できます。この機能にはリポジトリ間の参照は不要です。要件と制限については、<a href="../installation#share-files-across-repositories">リポジトリ間でファイルを共有する</a> を参照してください。</p> <!--[3--><h4 class="relative group"><a id="update-an-existing-modular-index" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#update-an-existing-modular-index"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->既存のモジュラーインデックスを更新する<!----></span></h4><!--]--><!----> <p>元のリポジトリへの既存の参照は引き続き機能します。参照が重複したダウンロードを回避するためにのみ使用されていた場合は、コンポーネントファイルがモジュラーリポジトリで利用可能になった時点で、そこから読み込むよう参照を更新することを推奨します。</p> <p>まず、モジュラーリポジトリに、コンポーネントの構成と、すべての必須ウェイトファイル(ウェイトインデックスおよびシャード化されたチェックポイントのすべてのシャードを含む)が含まれていることを確認します。一致する Xet ハッシュにより、共有キャッシュを再利用できるウェイトファイルが決定されます。欠落しているコンポーネント、または意図的に別のチェックポイントから読み込まれるコンポーネントへの参照は保持してください。</p> <p>ローカルのコピーにある<code>modular_model_index.json</code>の、関連する読み込み仕様のみを編集します。例えば、モジュラーリポジトリ内の<code>vae/</code>に保存されているVAEを、同じ重みバリアントで使用する場合は、以下のようにします。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> json | |
| <span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path | |
| index_path = Path(<span class="hljs-string">"path/to/local-copy/modular_model_index.json"</span>) | |
| index = json.loads(index_path.read_text()) | |
| spec = index[<span class="hljs-string">"vae"</span>][<span class="hljs-number">2</span>] | |
| spec[<span class="hljs-string">"pretrained_model_name_or_path"</span>] = <span class="hljs-string">"my-username/my-modular-repo"</span> | |
| spec.pop(<span class="hljs-string">"repo"</span>, <span class="hljs-literal">None</span>) | |
| spec[<span class="hljs-string">"subfolder"</span>] = <span class="hljs-string">"vae"</span> | |
| spec[<span class="hljs-string">"revision"</span>] = <span class="hljs-literal">None</span> | |
| index_path.write_text(json.dumps(index, indent=<span class="hljs-number">2</span>) + <span class="hljs-string">"\n"</span>)<!----></pre></div><!----> <p>これにより、レガシーの<code>repo</code>フィールドも削除されます。元のリポジトリのrevisionは、宛先のrevisionを特定しません。宛先のデフォルトブランチには<code>None</code>を使用するか、コンポーネントを含む宛先のrevisionをピン留めしてください。ファイル名が異なる場合は、<code>subfolder</code>と<code>variant</code>を調整してください。</p> <p>更新されたインデックスを公開する前に、<code>ModularPipeline.from_pretrained("path/to/local-copy")</code> で読み込み、<code>load_components(names="vae")</code> を呼び出して、コンポーネントが期待されるパラメータで読み込まれることを確認してください。編集したインデックスのみをアップロードしてください。重みを再保存するとハッシュが変更され、キャッシュの再利用ができなくなる可能性があります。</p> <!--[2--><h3 class="relative group"><a id="custom-code" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#custom-code"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->カスタムコード<!----></span></h3><!--]--><!----> <p>モジュール化されたリポジトリには、Python コードとしてカスタムのパイプラインブロックを含めることもできます。これにより、Diffusers にネイティブで存在しない特殊なブロックを共有できます。例えば、<a href="https://huggingface.co/diffusers/Florence2-image-Annotator" rel="nofollow">diffusers/Florence2-image-Annotator</a> には、読み込み設定とともにカスタムブロックが含まれています:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class=" "><!----><span class="hljs-type">Florence2</span>-image-<span class="hljs-type">Annotator</span>/ | |
| ├── <span class="hljs-keyword">block</span>.py <span class="hljs-comment"># Custom pipeline blocks implementation</span> | |
| ├── config.json <span class="hljs-comment"># Pipeline configuration and auto_map</span> | |
| ├── mellon_config.json <span class="hljs-comment"># UI configuration for Mellon</span> | |
| └── modular_model_index.json <span class="hljs-comment"># Component loading specifications</span><!----></pre></div><!----> <p><code>config.json</code>ファイルには、<a href="/docs/diffusers/main/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline">ModularPipeline</a>がカスタムブロックの場所を特定するための<code>auto_map</code>キーが含まれています。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> コピーしました</div><!----></button><!----></div> <pre class="language-json "><!----><span class="hljs-punctuation">{</span> | |
| <span class="hljs-attr">"_class_name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"Florence2AnnotatorBlocks"</span><span class="hljs-punctuation">,</span> | |
| <span class="hljs-attr">"auto_map"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> | |
| <span class="hljs-attr">"ModularPipelineBlocks"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"block.Florence2AnnotatorBlocks"</span> | |
| <span class="hljs-punctuation">}</span> | |
| <span class="hljs-punctuation">}</span><!----></pre></div><!----> <p><code>trust_remote_code=True</code> を使用して、<a href="#frompretrained">from_pretrained</a> に示すようにカスタムコードリポジトリを読み込みます。独自のブロックを作成して共有する方法については、<a href="./custom_blocks">Custom blocks</a> を参照してください。</p> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/diffusers/blob/main/docs/source/en/modular_diffusers/modular_pipeline.md" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg><!----> <span><span class="underline">Update</span> on GitHub</span></a><!----> <p></p><!--]--><!----><!--]--><!--]--><!--]--> <!--[-1--><!--]--><!--]--> | |
| <script> | |
| { | |
| __sveltekit_19i0et7 = { | |
| base: "/docs/diffusers/main/en", | |
| assets: "/docs/diffusers/main/en" | |
| }; | |
| const element = document.currentScript.parentElement; | |
| Promise.all([ | |
| import("/docs/diffusers/main/en/_app/immutable/entry/start.BlHKsxeM.js"), | |
| import("/docs/diffusers/main/en/_app/immutable/entry/app.DY7e59sO.js") | |
| ]).then(([kit, app]) => { | |
| kit.start(app, element, { | |
| node_ids: [0, 3], | |
| data: [null,null], | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 102 kB
- Xet hash:
- 838e071f4c3bda8eb6593e829939b55e2ccf87adcc6940e734f3d0529ea3d239
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.