Buckets:

mishig's picture
download
raw
56.7 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Bắt đầu nhanh&quot;,&quot;local&quot;:&quot;quickstart&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Chạy một pipeline&quot;,&quot;local&quot;:&quot;run-a-pipeline&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Hiểu cấu trúc&quot;,&quot;local&quot;:&quot;understand-the-structure&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Quy trình làm việc&quot;,&quot;local&quot;:&quot;workflows&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Các khối con&quot;,&quot;local&quot;:&quot;sub-blocks&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Tạo các quy trình làm việc mới&quot;,&quot;local&quot;:&quot;compose-new-workflows&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Các bước tiếp theo&quot;,&quot;local&quot;:&quot;next-steps&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/>
<link href="/docs/diffusers/main/en/_app/immutable/entry/start.0S0_iEoG.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/chunks/I9eooN77.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/chunks/Bupkjr9f.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/entry/app.CPdOxCbw.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/chunks/CyvZ3FS5.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/chunks/l0QnnOED.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/nodes/0.DteTvmQZ.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/chunks/CoSrdQax.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/nodes/328.g2gShkQF.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/chunks/DSPDl1gz.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/chunks/D5eijdnr.js" rel="modulepreload">
<!--1x94atl--><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Quickstart&quot;,&quot;local&quot;:&quot;quickstart&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Run a pipeline&quot;,&quot;local&quot;:&quot;run-a-pipeline&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Understand the structure&quot;,&quot;local&quot;:&quot;understand-the-structure&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Workflows&quot;,&quot;local&quot;:&quot;workflows&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Sub-blocks&quot;,&quot;local&quot;:&quot;sub-blocks&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Compose new workflows&quot;,&quot;local&quot;:&quot;compose-new-workflows&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Next steps&quot;,&quot;local&quot;:&quot;next-steps&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;: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="quickstart" 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="#quickstart"><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><!---->Bắt đầu nhanh<!----></span></h1><!--]--><!----> <p>Modular Diffusers là một framework để nhanh chóng xây dựng các pipeline linh hoạt và có thể tùy chỉnh. Các pipeline này có thể vượt ra ngoài những gì các <code>DiffusionPipeline</code> tiêu chuẩn có thể làm. Nền tảng cốt lõi của Modular Diffusers là các <a href="/docs/diffusers/v0.43.0/vi/api/modular_diffusers/pipeline_blocks#diffusers.ModularPipelineBlocks">ModularPipelineBlocks</a> có thể được kết hợp với các block khác để thích ứng với các quy trình làm việc mới. Các block này được chuyển đổi thành một <a href="/docs/diffusers/v0.43.0/vi/api/modular_diffusers/pipeline#diffusers.ModularPipeline">ModularPipeline</a>, một giao diện thân thiện với người dùng để chạy các tác vụ tạo sinh.</p> <p>Hướng dẫn này cho bạn cách chạy một pipeline dạng module, hiểu cấu trúc của nó và tùy chỉnh nó bằng cách sửa đổi các khối cấu thành. Nếu bạn đã quen thuộc với <a href="/docs/diffusers/v0.43.0/vi/api/pipelines/overview#diffusers.DiffusionPipeline">DiffusionPipeline</a>, hãy đọc <a href="./coming_from_diffusion_pipeline">Coming from DiffusionPipeline</a> trước để biết những điểm khác biệt.</p> <!--[1--><h2 class="relative group"><a id="run-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="#run-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><!---->Chạy một pipeline<!----></span></h2><!--]--><!----> <p><a href="/docs/diffusers/v0.43.0/vi/api/modular_diffusers/pipeline#diffusers.ModularPipeline">ModularPipeline</a> là giao diện chính để tải, chạy và quản lý các pipeline dạng module.</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> Đã sao chép</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, ComponentsManager
<span class="hljs-comment"># Use ComponentsManager to enable auto CPU offloading for memory efficiency</span>
manager = ComponentsManager()
manager.enable_auto_cpu_offload(device=<span class="hljs-string">&quot;cuda:0&quot;</span>)
pipe = ModularPipeline.from_pretrained(<span class="hljs-string">&quot;Qwen/Qwen-Image&quot;</span>, components_manager=manager)
pipe.load_components(dtype=torch.bfloat16)
image = pipe(
prompt=<span class="hljs-string">&quot;cat wizard with red hat, gandalf, lord of the rings, detailed, fantasy, cute, adorable, Pixar, Disney&quot;</span>,
).images[<span class="hljs-number">0</span>]
image<!----></pre></div><!----> <p><a href="/docs/diffusers/v0.43.0/vi/api/modular_diffusers/pipeline#diffusers.ModularPipeline.from_pretrained">from_pretrained()</a> sử dụng nạp chậm - nó đọc cấu hình để biết nơi nạp mỗi thành phần, nhưng không thực sự nạp trọng số mô hình cho đến khi bạn gọi <a href="/docs/diffusers/v0.43.0/vi/api/modular_diffusers/pipeline#diffusers.ModularPipeline.load_components">load_components()</a>. Điều này cho bạn quyền kiểm soát thời điểm và cách thức nạp các thành phần.</p> <blockquote class="tip"><p><code>ComponentsManager</code> với <code>enable_auto_cpu_offload</code> tự động di chuyển mô hình giữa CPU và GPU khi cần, giúp giảm mức sử dụng bộ nhớ cho các mô hình lớn như Qwen-Image. Tìm hiểu thêm trong hướng dẫn <a href="./components_manager">ComponentsManager</a>.</p> <p>Nếu bạn không cần offloading, hãy xóa đối số <code>components_manager</code> và di chuyển pipeline sang thiết bị của bạn một cách thủ công bằng <code>to("cuda")</code>.</p></blockquote> <p>Tìm hiểu thêm về việc tạo và nạp pipeline trong các hướng dẫn <a href="https://huggingface.co/docs/diffusers/modular_diffusers/modular_pipeline#creating-a-pipeline" rel="nofollow">Tạo pipeline</a> và <a href="https://huggingface.co/docs/diffusers/modular_diffusers/modular_pipeline#loading-components" rel="nofollow">Nạp các thành phần</a>.</p> <!--[1--><h2 class="relative group"><a id="understand-the-structure" 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="#understand-the-structure"><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><!---->Hiểu cấu trúc<!----></span></h2><!--]--><!----> <p>Một <a href="/docs/diffusers/v0.43.0/vi/api/modular_diffusers/pipeline#diffusers.ModularPipeline">ModularPipeline</a> có hai phần: một <strong>định nghĩa</strong> (các khối) và một <strong>trạng thái</strong> (các thành phần và cấu hình đã được nạp).</p> <p>In pipeline để xem trạng thái của nó — các thành phần cùng trạng thái tải và cấu hình của chúng.</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-built_in">print</span>(pipe)<!----></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> Đã sao chép</div><!----></button><!----></div> <pre class=" "><!----><span class="hljs-symbol">QwenImageModularPipeline</span> {
<span class="hljs-string">&quot;_blocks_class_name&quot;</span>: <span class="hljs-string">&quot;QwenImageAutoBlocks&quot;</span>,
<span class="hljs-string">&quot;_class_name&quot;</span>: <span class="hljs-string">&quot;QwenImageModularPipeline&quot;</span>,
<span class="hljs-string">&quot;_diffusers_version&quot;</span>: <span class="hljs-string">&quot;0.37.0.dev0&quot;</span>,
<span class="hljs-string">&quot;transformer&quot;</span>: [
<span class="hljs-string">&quot;diffusers&quot;</span>,
<span class="hljs-string">&quot;QwenImageTransformer2DModel&quot;</span>,
{
<span class="hljs-string">&quot;pretrained_model_name_or_path&quot;</span>: <span class="hljs-string">&quot;Qwen/Qwen-Image&quot;</span>,
<span class="hljs-string">&quot;revision&quot;</span>: null,
<span class="hljs-string">&quot;subfolder&quot;</span>: <span class="hljs-string">&quot;transformer&quot;</span>,
<span class="hljs-string">&quot;type_hint&quot;</span>: [
<span class="hljs-string">&quot;diffusers&quot;</span>,
<span class="hljs-string">&quot;QwenImageTransformer2DModel&quot;</span>
],
<span class="hljs-string">&quot;variant&quot;</span>: null
}
],
...
}<!----></pre></div><!----> <p>Truy cập định nghĩa thông qua <code>pipe.blocks</code> — đây là <a href="/docs/diffusers/v0.43.0/vi/api/modular_diffusers/pipeline_blocks#diffusers.ModularPipelineBlocks">ModularPipelineBlocks</a> xác định các luồng công việc, đầu vào, đầu ra và logic tính toán của pipeline.</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-built_in">print</span>(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> Đã sao chép</div><!----></button><!----></div> <pre class=" "><!---->QwenImageAutoBlocks(
Class: SequentialPipelineBlocks
Description: Auto Modular pipeline <span class="hljs-keyword">for</span> <span class="hljs-keyword">text</span>-to-<span class="hljs-keyword">image</span>, <span class="hljs-keyword">image</span>-to-<span class="hljs-keyword">image</span>, inpainting, and controlnet tasks using QwenImage.
Supported workflows:
- <span class="hljs-string">`text2image`</span>: <span class="hljs-keyword">requires</span> <span class="hljs-string">`prompt`</span>
- <span class="hljs-string">`image2image`</span>: <span class="hljs-keyword">requires</span> <span class="hljs-string">`prompt`</span>, <span class="hljs-string">`image`</span>
- <span class="hljs-string">`inpainting`</span>: <span class="hljs-keyword">requires</span> <span class="hljs-string">`prompt`</span>, <span class="hljs-string">`mask_image`</span>, <span class="hljs-string">`image`</span>
- <span class="hljs-string">`controlnet_text2image`</span>: <span class="hljs-keyword">requires</span> <span class="hljs-string">`prompt`</span>, <span class="hljs-string">`control_image`</span>
...
Components:
text_encoder (<span class="hljs-string">`Qwen2_5_VLForConditionalGeneration`</span>)
vae (<span class="hljs-string">`AutoencoderKLQwenImage`</span>)
transformer (<span class="hljs-string">`QwenImageTransformer2DModel`</span>)
...
Sub-Blocks:
[<span class="hljs-number">0</span>] text_encoder (QwenImageAutoTextEncoderStep)
[<span class="hljs-number">1</span>] vae_encoder (QwenImageAutoVaeEncoderStep)
[<span class="hljs-number">2</span>] controlnet_vae_encoder (QwenImageOptionalControlNetVaeEncoderStep)
[<span class="hljs-number">3</span>] denoise (QwenImageAutoCoreDenoiseStep)
[<span class="hljs-number">4</span>] decode (QwenImageAutoDecodeStep)
)<!----></pre></div><!----> <p>Đầu ra trả về:</p> <ul><li>Các quy trình làm việc được hỗ trợ (text2image, image2image, inpainting, v.v.)</li> <li>Các khối con cấu thành nên nó (text_encoder, vae_encoder, denoise, decode)</li></ul> <!--[2--><h3 class="relative group"><a id="workflows" 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="#workflows"><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><!---->Quy trình làm việc<!----></span></h3><!--]--><!----> <p>Pipeline này hỗ trợ nhiều luồng công việc và điều chỉnh hành vi dựa trên các đầu vào bạn cung cấp. Ví dụ, nếu bạn truyền <code>image</code> vào pipeline, nó sẽ chạy luồng công việc từ ảnh sang ảnh thay vì từ văn bản sang ảnh. Tìm hiểu thêm về cách hoạt động bên trong trong hướng dẫn <a href="https://huggingface.co/docs/diffusers/modular_diffusers/auto_pipeline_blocks" rel="nofollow">AutoPipelineBlocks</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> diffusers.utils <span class="hljs-keyword">import</span> load_image
input_image = load_image(<span class="hljs-string">&quot;https://github.com/Trgtuan10/Image_storage/blob/main/cute_cat.png?raw=true&quot;</span>)
image = pipe(
prompt=<span class="hljs-string">&quot;cat wizard with red hat, gandalf, lord of the rings, detailed, fantasy, cute, adorable, Pixar, Disney&quot;</span>,
image=input_image,
).images[<span class="hljs-number">0</span>]<!----></pre></div><!----> <p>Sử dụng <code>get_workflow()</code> để trích xuất các khối cho một workflow cụ thể. Truyền tên workflow (ví dụ: <code>"image2image"</code>, <code>"inpainting"</code>, <code>"controlnet_text2image"</code>) để chỉ lấy các khối liên quan đến workflow đó. Điều này hữu ích khi bạn muốn tùy chỉnh hoặc gỡ lỗi một workflow cụ thể. Bạn có thể kiểm tra <code>pipe.blocks.available_workflows</code> để xem tất cả các workflow khả dụng.</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!---->img2img_blocks = pipe.blocks.get_workflow(<span class="hljs-string">&quot;image2image&quot;</span>)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="sub-blocks" 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="#sub-blocks"><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><!---->Các khối con<!----></span></h3><!--]--><!----> <p>Các block có thể chứa các block khác. <code>pipe.blocks</code> cung cấp cho bạn định nghĩa block cấp cao nhất (ở đây là <code>QwenImageAutoBlocks</code>), trong khi <code>sub_blocks</code> cho phép bạn truy cập các block nhỏ hơn bên trong nó.</p> <p><code>QwenImageAutoBlocks</code> bao gồm: <code>text_encoder</code>, <code>vae_encoder</code>, <code>controlnet_vae_encoder</code>, <code>denoise</code> và <code>decode</code>.</p> <p>Các khối con này chạy lần lượt và dữ liệu chảy tuyến tính từ khối này sang khối tiếp theo — <code>intermediate_outputs</code> của mỗi khối sẽ trở thành <code>inputs</code> cho khối kế tiếp. Đây là cách <a href="./sequential_pipeline_blocks"><code>SequentialPipelineBlocks</code></a> hoạt động.</p> <p>Bạn có thể truy cập chúng thông qua thuộc tính <code>sub_blocks</code>. Thuộc tính <code>doc</code> hữu ích để xem tài liệu đầy đủ của bất kỳ khối nào, bao gồm các đầu vào, đầu ra và các thành phần của nó.</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!---->vae_encoder_block = pipe.blocks.sub_blocks[<span class="hljs-string">&quot;vae_encoder&quot;</span>]
<span class="hljs-built_in">print</span>(vae_encoder_block.doc)<!----></pre></div><!----> <p>Khối này có thể được chuyển đổi thành một pipeline để nó có thể chạy độc lập bằng <a href="/docs/diffusers/v0.43.0/vi/api/modular_diffusers/pipeline_blocks#diffusers.ModularPipelineBlocks.init_pipeline">init_pipeline()</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!---->vae_encoder_pipe = vae_encoder_block.init_pipeline()
<span class="hljs-comment"># Reuse the VAE we already loaded, we can reuse it with update_components() method</span>
vae_encoder_pipe.update_components(vae=pipe.vae)
<span class="hljs-comment"># Run just this block</span>
image_latents = vae_encoder_pipe(image=input_image).image_latents
<span class="hljs-built_in">print</span>(image_latents.shape)<!----></pre></div><!----> <p>Nó tái sử dụng VAE từ pipeline gốc của chúng ta thay vì tải lại, giúp duy trì hiệu quả sử dụng bộ nhớ. Tìm hiểu thêm trong hướng dẫn <a href="https://huggingface.co/docs/diffusers/modular_diffusers/modular_pipeline#loading-components" rel="nofollow">Tải các thành phần</a>.</p> <p>Vì các khối có thể kết hợp được, bạn có thể sửa đổi định nghĩa của pipeline bằng cách thêm, xóa hoặc thay thế các khối để tạo ra các quy trình làm việc mới. Trong phần tiếp theo, chúng ta sẽ thêm một khối phát hiện cạnh canny vào pipeline ControlNet, để bạn có thể truyền vào một hình ảnh thông thường thay vì một bản đồ cạnh canny đã được xử lý trước.</p> <!--[1--><h2 class="relative group"><a id="compose-new-workflows" 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="#compose-new-workflows"><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><!---->Tạo các quy trình làm việc mới<!----></span></h2><!--]--><!----> <p>Hãy thêm một khối phát hiện cạnh canny vào pipeline ControlNet. Trước tiên, hãy tải một khối canny có sẵn từ Hub (xem <a href="https://huggingface.co/docs/diffusers/modular_diffusers/custom_blocks" rel="nofollow">Building Custom Blocks</a> để tự tạo khối của riêng bạn).</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> diffusers.modular_pipelines <span class="hljs-keyword">import</span> ModularPipelineBlocks
<span class="hljs-comment"># Load a canny block from the Hub</span>
canny_block = ModularPipelineBlocks.from_pretrained(
<span class="hljs-string">&quot;diffusers-internal-dev/canny-filtering&quot;</span>,
trust_remote_code=<span class="hljs-literal">True</span>,
)
<span class="hljs-built_in">print</span>(canny_block.doc)<!----></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> Đã sao chép</div><!----></button><!----></div> <pre class=" "><!---->class CannyBlock
Inputs:
image (`Union[Image, ndarray]`):
Image <span class="hljs-keyword">to</span> compute canny <span class="hljs-built_in">filter</span> <span class="hljs-keyword">on</span>
low_threshold (`<span class="hljs-keyword">int</span>`, *optional*, defaults <span class="hljs-keyword">to</span> <span class="hljs-number">50</span>):
Low threshold <span class="hljs-keyword">for</span> the canny <span class="hljs-built_in">filter</span>.
high_threshold (`<span class="hljs-keyword">int</span>`, *optional*, defaults <span class="hljs-keyword">to</span> <span class="hljs-number">200</span>):
High threshold <span class="hljs-keyword">for</span> the canny <span class="hljs-built_in">filter</span>.
...
Outputs:
control_image (`PIL.Image`):
Canny <span class="hljs-keyword">map</span> <span class="hljs-keyword">for</span> <span class="hljs-built_in">input</span> image<!----></pre></div><!----> <p>Sử dụng <code>get_workflow</code> để trích xuất workflow ControlNet từ <code>QwenImageAutoBlocks</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-comment"># Get the controlnet workflow that we want to work with</span>
blocks = pipe.blocks.get_workflow(<span class="hljs-string">&quot;controlnet_text2image&quot;</span>)
<span class="hljs-built_in">print</span>(blocks.doc)<!----></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> Đã sao chép</div><!----></button><!----></div> <pre class=" "><!---->class <span class="hljs-symbol">SequentialPipelineBlocks</span>
<span class="hljs-symbol">Inputs</span>:
prompt (<span class="hljs-string">`str`</span>):
<span class="hljs-symbol">The</span> prompt or prompts to guide image generation.
control_image (<span class="hljs-string">`Image`</span>):
<span class="hljs-symbol">Control</span> image for <span class="hljs-symbol">ControlNet</span> conditioning.
...<!----></pre></div><!----> <p>Workflow được trích xuất là <a href="./sequential_pipeline_blocks"><code>SequentialPipelineBlocks</code></a> và hiện tại yêu cầu <code>control_image</code> làm đầu vào. Chèn khối canny vào đầu để pipeline nhận một hình ảnh thông thường thay thế.</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-comment"># Insert canny at the beginning</span>
blocks.sub_blocks.insert(<span class="hljs-string">&quot;canny&quot;</span>, canny_block, <span class="hljs-number">0</span>)
<span class="hljs-comment"># Check the updated structure: CannyBlock is now listed as first sub-block</span>
<span class="hljs-built_in">print</span>(blocks)
<span class="hljs-comment"># Check the updated doc</span>
<span class="hljs-built_in">print</span>(blocks.doc)<!----></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> Đã sao chép</div><!----></button><!----></div> <pre class=" "><!---->class SequentialPipelineBlocks
Inputs:
image (<span class="hljs-string">`Union[Image, ndarray]`</span>):
Image <span class="hljs-keyword">to</span> compute canny filter <span class="hljs-keyword">on</span>
low_threshold (<span class="hljs-string">`int`</span>, *optional*, defaults <span class="hljs-keyword">to</span> <span class="hljs-number">50</span>):
Low threshold for the canny filter.
high_threshold (<span class="hljs-string">`int`</span>, *optional*, defaults <span class="hljs-keyword">to</span> <span class="hljs-number">200</span>):
High threshold for the canny filter.
prompt (<span class="hljs-string">`str`</span>):
The prompt <span class="hljs-literal">or</span> prompts <span class="hljs-keyword">to</span> guide image generation.
<span class="hljs-params">...</span><!----></pre></div><!----> <p>Bây giờ pipeline nhận <code>image</code> làm đầu vào thay vì <code>control_image</code>. Vì các khối trong một chuỗi tự động chia sẻ dữ liệu, đầu ra của khối canny (<code>control_image</code>) sẽ chảy đến khối denoise cần nó, và đầu vào của khối canny (<code>image</code>) trở thành đầu vào của pipeline vì không có khối nào trước đó cung cấp nó.</p> <p>Tạo một pipeline từ các khối đã chỉnh sửa và tải một mô hình ControlNet. ControlNet không nằm trong kho lưu trữ mô hình gốc, vì vậy hãy tải nó riêng và thêm vào bằng <a href="/docs/diffusers/v0.43.0/vi/api/modular_diffusers/pipeline#diffusers.ModularPipeline.update_components">update_components()</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!---->pipeline = blocks.init_pipeline(<span class="hljs-string">&quot;Qwen/Qwen-Image&quot;</span>, components_manager=manager)
pipeline.load_components(dtype=torch.bfloat16)
<span class="hljs-comment"># Load the ControlNet model</span>
controlnet_spec = pipeline.get_component_spec(<span class="hljs-string">&quot;controlnet&quot;</span>)
controlnet_spec.pretrained_model_name_or_path = <span class="hljs-string">&quot;InstantX/Qwen-Image-ControlNet-Union&quot;</span>
controlnet = controlnet_spec.load(dtype=torch.bfloat16)
pipeline.update_components(controlnet=controlnet)<!----></pre></div><!----> <p>Bây giờ chạy pipeline - khối canny tiền xử lý ảnh cho ControlNet.</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> diffusers.utils <span class="hljs-keyword">import</span> load_image
prompt = <span class="hljs-string">&quot;cat wizard with red hat, gandalf, lord of the rings, detailed, fantasy, cute, adorable, Pixar, Disney&quot;</span>
image = load_image(<span class="hljs-string">&quot;https://github.com/Trgtuan10/Image_storage/blob/main/cute_cat.png?raw=true&quot;</span>)
output = pipeline(
prompt=prompt,
image=image,
).images[<span class="hljs-number">0</span>]
output<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="next-steps" 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="#next-steps"><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><!---->Các bước tiếp theo<!----></span></h2><!--]--><!----> <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">Học các kiến thức cơ bản</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">Xây dựng các khối tùy chỉnh</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">Chia sẻ các thành phần</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">Giao diện trực quan</div><!--]--></div> <div class="language-select"><div><p>Hiểu các khối xây dựng cốt lõi của Modular Diffusers:</p> <ul><li><a href="./pipeline_block">ModularPipelineBlocks</a>: Đơn vị cơ bản để định nghĩa một bước trong pipeline.</li> <li><a href="./sequential_pipeline_blocks">SequentialPipelineBlocks</a>: Xâu các khối để chạy theo trình tự.</li> <li><a href="./auto_pipeline_blocks">AutoPipelineBlocks</a>: Tạo các pipeline hỗ trợ nhiều luồng công việc.</li> <li><a href="./modular_diffusers_states">Trạng thái</a>: Cách dữ liệu được chia sẻ giữa các khối.</li></ul><!----></div><!----> <div hidden=""><p>Tìm hiểu cách tạo các khối của riêng bạn với logic tùy chỉnh trong hướng dẫn <a href="./custom_blocks">Building Custom Blocks</a>.</p><!----></div><!----> <div hidden=""><p>Sử dụng <a href="./components_manager"><code>ComponentsManager</code></a> để chia sẻ các mô hình giữa nhiều pipeline và quản lý bộ nhớ một cách hiệu quả.</p><!----></div><!----> <div hidden=""><p>Kết nối các pipeline dạng module với <a href="https://github.com/cubiq/Mellon" rel="nofollow">Mellon</a>, một giao diện trực quan dựa trên node để xây dựng workflow. Các khối tùy chỉnh được xây dựng bằng Modular Diffusers hoạt động ngay lập tức với Mellon - không cần viết mã UI. Đọc thêm trong hướng dẫn Mellon.</p><!----></div><!----><!----></div><!----> <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/quickstart.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_1qsdbwf = {
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.0S0_iEoG.js"),
import("/docs/diffusers/main/en/_app/immutable/entry/app.CPdOxCbw.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 328],
data: [null,null],
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
56.7 kB
·
Xet hash:
c57ffece1a77bfd48b2daf69503073adcc43d0e09779fe5b7037078850650d25

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.