Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"Penyimpanan sementara","local":"caching","sections":[{"title":"Pilih metode cache","local":"choose-a-cache-method","sections":[],"depth":2},{"title":"Pyramid Attention Broadcast","local":"pyramid-attention-broadcast","sections":[],"depth":2},{"title":"FasterCache","local":"fastercache","sections":[],"depth":2},{"title":"SeaCache","local":"seacache","sections":[],"depth":2},{"title":"FirstBlockCache","local":"firstblockcache","sections":[],"depth":2},{"title":"Cache TaylorSeer","local":"taylorseer-cache","sections":[],"depth":2},{"title":"MagCache","local":"magcache","sections":[],"depth":2},{"title":"Teks KV Cache","local":"text-kv-cache","sections":[],"depth":2},{"title":"Menambahkan caching ke model baru","local":"add-caching-to-a-new-model","sections":[],"depth":2}],"depth":1}"/> | |
| <link href="/docs/diffusers/main/en/_app/immutable/entry/start.B6roO9Zr.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/CjYFLaCl.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/Cc4T7sti.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/entry/app.CxxEVG8A.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/C8F28shN.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/BTnLXsn-.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.CmHT0Edy.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/C7Ky3Ped.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/nodes/8.DhsC3tNN.js" rel="modulepreload"> | |
| <link href="/docs/diffusers/main/en/_app/immutable/chunks/D2YP2_0L.js" rel="modulepreload"> | |
| <!--fs68f1--><meta name="hf:doc:metadata" content="{"title":"Caching","local":"caching","sections":[{"title":"Choose a cache method","local":"choose-a-cache-method","sections":[],"depth":2},{"title":"Pyramid Attention Broadcast","local":"pyramid-attention-broadcast","sections":[],"depth":2},{"title":"FasterCache","local":"fastercache","sections":[],"depth":2},{"title":"SeaCache","local":"seacache","sections":[],"depth":2},{"title":"FirstBlockCache","local":"firstblockcache","sections":[],"depth":2},{"title":"TaylorSeer Cache","local":"taylorseer-cache","sections":[],"depth":2},{"title":"MagCache","local":"magcache","sections":[],"depth":2},{"title":"Text KV Cache","local":"text-kv-cache","sections":[],"depth":2},{"title":"Add caching to a new model","local":"add-caching-to-a-new-model","sections":[],"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="caching" 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="#caching"><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><!---->Penyimpanan sementara<!----></span></h1><!--]--><!----> <p>Caching menggunakan kembali output lapisan perantara di antara langkah-langkah denoising untuk mempercepat inferensi. Metode ini menggunakan lebih banyak memori dan tidak memerlukan pelatihan. Aktifkan metode dengan konfigurasi pada transformer yang mendukung caching.</p> <!--[1--><h2 class="relative group"><a id="choose-a-cache-method" 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="#choose-a-cache-method"><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><!---->Pilih metode cache<!----></span></h2><!--]--><!----> <p>Pilih metode berdasarkan seberapa banyak konfigurasi yang akan Anda atur dan tingkat kecocokan yang Anda butuhkan. Setiap metode menyeimbangkan kecepatan, memori, dan seberapa dekat keluaran dengan eksekusi tanpa cache secara berbeda. Bandingkan keluaran dengan dan tanpa cache pada prompt Anda sendiri sebelum mengandalkan suatu metode.</p> <table><thead><tr><th>Metode</th><th>Gunakan saat</th><th>Catatan</th></tr></thead><tbody><tr><td><a href="#text-kv-cache">KV Cache Teks</a></td><td>Gambar NucleusMoE, penggunaan ulang proyeksi K/V teks di antara langkah</td><td>Saat ini hook blok gambar NucleusMoE</td></tr><tr><td><a href="#seacache">SeaCache</a></td><td>Generasi video Cosmos 3</td><td>Pengaturan sering kali tidak dapat dipindahkan antar model</td></tr><tr><td><a href="#firstblockcache">FirstBlockCache</a></td><td>Mau satu pengaturan utama kecepatan/kualitas pada transformer yang terdaftar</td><td>Satu <code>threshold</code> mengontrol seberapa sering langkah dilewati</td></tr><tr><td><a href="#magcache">MagCache</a></td><td>Apakah Anda memiliki rasio magnitudo untuk checkpoint dan scheduler Anda, atau akan melakukan kalibrasi terlebih dahulu</td><td>Rasio bersifat spesifik untuk checkpoint dan scheduler</td></tr><tr><td><a href="#taylorseer-cache">TaylorSeer</a></td><td>Ingin memprediksi aktivasi di langkah kemudian dari langkah sebelumnya</td><td><code>max_order</code> yang lebih tinggi menggunakan lebih banyak memori</td></tr><tr><td><a href="#pyramid-attention-broadcast">PAB</a></td><td>Video, bersedia menyesuaikan penggunaan ulang perhatian (rentang blok dan lewati langkah waktu per jenis perhatian)</td><td>Rentang lewati perlu penyetelan per model</td></tr><tr><td><a href="#fastercache">FasterCache</a></td><td>Seperti PAB, ditambah opsi untuk melewati cabang CFG</td><td>Eksperimental</td></tr></tbody></table> <!--[1--><h2 class="relative group"><a id="pyramid-attention-broadcast" 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="#pyramid-attention-broadcast"><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><!---->Pyramid Attention Broadcast<!----></span></h2><!--]--><!----> <p><a href="https://huggingface.co/papers/2408.12588" rel="nofollow">Pyramid Attention Broadcast (PAB)</a> memperkirakan perhatian di antara langkah-langkah denoising dengan menggunakan kembali output perhatian untuk beberapa blok dan timestep, alih-alih menghitung ulang setiap langkah. Config memisahkan jenis-jenis perhatian (spasial, temporal, silang) ketika model memilikinya. Tidak semua model video mengekspos ketiga jenis tersebut, dan hanya atur rentang yang sesuai dengan blok yang Anda miliki.</p> <p>Setiap jenis menggunakan <code>*_attention_block_skip_range</code> (seberapa sering merekomputasi vs. menggunakan ulang di dalam jendela) dan <code>*_attention_timestep_skip_range</code> (timestep denoising mana yang boleh dilewati). Anda harus meneruskan <code>current_timestep_callback</code> agar hook dapat membaca timestep saat ini dari pipeline. Skip yang lebih lebar atau lebih agresif biasanya berarti lebih banyak kecepatan dan lebih banyak risiko kualitas.</p> <p>Lewatkan <a href="/docs/diffusers/main/id/api/cache#diffusers.PyramidAttentionBroadcastConfig">PyramidAttentionBroadcastConfig</a> untuk mengaktifkannya.</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> Disalin</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> CogVideoXPipeline, PyramidAttentionBroadcastConfig | |
| pipe = CogVideoXPipeline.from_pretrained(<span class="hljs-string">"THUDM/CogVideoX-5b"</span>, dtype=torch.bfloat16) | |
| pipe.to(<span class="hljs-string">"cuda"</span>) <span class="hljs-comment"># or "mps", "xpu", "cpu"</span> | |
| config = PyramidAttentionBroadcastConfig( | |
| spatial_attention_block_skip_range=<span class="hljs-number">2</span>, | |
| spatial_attention_timestep_skip_range=(<span class="hljs-number">100</span>, <span class="hljs-number">800</span>), | |
| current_timestep_callback=<span class="hljs-keyword">lambda</span>: pipe.current_timestep, | |
| ) | |
| pipe.transformer.enable_cache(config)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="fastercache" 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="#fastercache"><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><!---->FasterCache<!----></span></h2><!--]--><!----> <p><a href="https://huggingface.co/papers/2410.19355" rel="nofollow">FasterCache</a> meng-cache dan menggunakan ulang fitur perhatian yang serupa dengan <a href="#pyramid-attention-broadcast">PAB</a>. Ia juga dapat melewati cabang non-kondisional di bawah classifier-free guidance dan memperkirakan cabang tersebut dari cabang kondisional ketika latens yang berurutan cukup redundan.</p> <p>Lewatkan <a href="/docs/diffusers/main/id/api/cache#diffusers.FasterCacheConfig">FasterCacheConfig</a> untuk mengaktifkannya. Seperti PAB, atur <code>*_attention_block_skip_range</code> dan <code>*_attention_timestep_skip_range</code> untuk jenis perhatian yang Anda miliki, serta opsi skip cabang CFG jika Anda menginginkannya.</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> Disalin</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> CogVideoXPipeline, FasterCacheConfig | |
| pipe = CogVideoXPipeline.from_pretrained(<span class="hljs-string">"THUDM/CogVideoX-5b"</span>, dtype=torch.bfloat16) | |
| pipe.to(<span class="hljs-string">"cuda"</span>) <span class="hljs-comment"># or "mps", "xpu", "cpu"</span> | |
| config = FasterCacheConfig( | |
| spatial_attention_block_skip_range=<span class="hljs-number">2</span>, | |
| spatial_attention_timestep_skip_range=(-<span class="hljs-number">1</span>, <span class="hljs-number">681</span>), | |
| current_timestep_callback=<span class="hljs-keyword">lambda</span>: pipe.current_timestep, | |
| attention_weight_callback=<span class="hljs-keyword">lambda</span> _: <span class="hljs-number">0.3</span>, | |
| unconditional_batch_skip_range=<span class="hljs-number">5</span>, | |
| unconditional_batch_timestep_skip_range=(-<span class="hljs-number">1</span>, <span class="hljs-number">781</span>), | |
| tensor_format=<span class="hljs-string">"BFCHW"</span>, | |
| ) | |
| pipe.transformer.enable_cache(config)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="seacache" 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="#seacache"><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><!---->SeaCache<!----></span></h2><!--]--><!----> <p><a href="https://huggingface.co/papers/2602.18993" rel="nofollow">SeaCache</a> membandingkan indikator Spectral Evolution Aware (SEA) antara langkah denoising yang berurutan. Ketika perubahan kumulatif tetap di bawah ambang batas, metode ini melewati tumpukan blok transformer dan memprediksi keluaran dari residual yang di-cache. Metode ini bersifat aproksimasi dan dirancang untuk generasi video.</p> <p>Adaptor bawaan untuk SeaCache meliputi:</p> <ul><li>Cosmos 3 adalah integrasi utama yang dioptimalkan dan diuji.</li> <li>Wan T2V menggunakan jalur blok berulang generik sebagai demo untuk cara menyediakan latens visual mentah ke SeaCache. Parameter cache yang sama mungkin tidak dapat diterapkan ke Wan atau varian Wan lainnya.</li></ul> <p>Aktifkan SeaCache pada transformer. Loop denoising Cosmos 3 menempelkan langkah scheduler, sigma, dan jumlah langkah ke setiap <code>cache_context</code>, sehingga tidak diperlukan parameter tambahan.</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> Disalin</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> Cosmos3OmniPipeline, SeaCacheConfig | |
| pipe = Cosmos3OmniPipeline.from_pretrained(<span class="hljs-string">"nvidia/Cosmos3-Nano"</span>) | |
| pipe.transformer.enable_cache(SeaCacheConfig(threshold=<span class="hljs-number">0.2</span>, max_consecutive_cached=<span class="hljs-number">2</span>))<!----></pre></div><!----> <p>SeaCache dapat mengubah output. Panggil <code>pipe.transformer.disable_cache()</code> ketika Anda membutuhkan setiap langkah untuk menjalankan transformer penuh. Panggilan enable yang sama berfungsi dengan <a href="/docs/diffusers/main/id/api/pipelines/cosmos3#diffusers.Cosmos3OmniPipeline">Cosmos3OmniPipeline</a>, <a href="/docs/diffusers/main/id/api/pipelines/cosmos3#diffusers.Cosmos3OmniModularPipeline">Cosmos3OmniModularPipeline</a>, dan <a href="/docs/diffusers/main/id/api/pipelines/cosmos3#diffusers.Cosmos3DistilledModularPipeline">Cosmos3DistilledModularPipeline</a>.</p> <p>Untuk menggunakan SeaCache dengan video transformer lain, ikuti <a href="#add-caching-to-a-new-model">Menambahkan caching ke model baru</a>. SeaCache juga membutuhkan <code>step_index</code>, <code>sigma</code>, dan <code>num_inference_steps</code> di setiap <code>cache_context</code>, serta <code>raw_vision_callback</code> jika tidak ada adapter bawaan. Sesuaikan parameter untuk setiap model dan scheduler.</p> <!--[1--><h2 class="relative group"><a id="firstblockcache" 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="#firstblockcache"><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><!---->FirstBlockCache<!----></span></h2><!--]--><!----> <p><a href="/docs/diffusers/main/id/api/cache#diffusers.FirstBlockCacheConfig">FirstBlockCacheConfig</a> memeriksa seberapa besar perubahan pada lapisan awal denoiser dari satu timestep ke timestep berikutnya. Jika perubahannya kecil, model akan melewati lapisan-lapisan akhir yang mahal dan menggunakan kembali output sebelumnya.</p> <p>Aktifkan melalui <code>enable_cache</code> agar <code>disable_cache</code> dan <code>is_cache_enabled</code> tetap sinkron. Nilai <code>threshold</code> bawaan adalah <code>0.05</code>. Nilai yang lebih tinggi seperti <code>0.2</code> akan lebih sering dilewati untuk kecepatan ekstra, tetapi kualitas generasi mungkin menurun.</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> Disalin</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline, FirstBlockCacheConfig | |
| pipe = DiffusionPipeline.from_pretrained( | |
| <span class="hljs-string">"Qwen/Qwen-Image"</span>, dtype=torch.bfloat16 | |
| ) | |
| pipe.transformer.enable_cache(FirstBlockCacheConfig(threshold=<span class="hljs-number">0.2</span>))<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="taylorseer-cache" 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="#taylorseer-cache"><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><!---->Cache TaylorSeer<!----></span></h2><!--]--><!----> <p><a href="https://huggingface.co/papers/2503.06923" rel="nofollow">TaylorSeer Cache</a> mempercepat inferensi difusi dengan ekspansi deret Taylor di antara langkah-langkah denoising. Metode ini memprediksi aktivasi langkah berikutnya dari langkah sebelumnya dan menggunakan ulang prediksi tersebut selama beberapa langkah sehingga transformer melakukan lebih sedikit kerja penuh.</p> <ul><li><code>cache_interval</code>: Jumlah langkah untuk menggunakan kembali output yang di-cache sebelum melakukan forward pass penuh</li> <li><code>disable_cache_before_step</code>: Langkah-langkah awal yang menggunakan komputasi penuh untuk mengumpulkan data untuk aproksimasi</li> <li><code>max_order</code>: Orde Taylor yang lebih tinggi dapat lebih akurat tetapi menggunakan lebih banyak memori. Biarkan ini tetap <code>1</code> kecuali Anda memiliki alasan untuk mengubahnya.</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> Disalin</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> FluxPipeline, TaylorSeerCacheConfig | |
| pipe = FluxPipeline.from_pretrained( | |
| <span class="hljs-string">"black-forest-labs/FLUX.1-dev"</span>, | |
| dtype=torch.bfloat16, | |
| ).to(<span class="hljs-string">"cuda"</span>) <span class="hljs-comment"># or "mps", "xpu", "cpu"</span> | |
| config = TaylorSeerCacheConfig( | |
| cache_interval=<span class="hljs-number">5</span>, | |
| max_order=<span class="hljs-number">1</span>, | |
| disable_cache_before_step=<span class="hljs-number">10</span>, | |
| taylor_factors_dtype=torch.bfloat16, | |
| ) | |
| pipe.transformer.enable_cache(config)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="magcache" 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="#magcache"><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><!---->MagCache<!----></span></h2><!--]--><!----> <p><a href="https://github.com/Zehong-Ma/MagCache" rel="nofollow">MagCache</a> melewati blok transformer berdasarkan magnitudo pembaruan residual. Magnitudo pembaruan meluruh secara prediktif selama denoising, dan MagCache melacak anggaran error dari rasio magnitudo yang dihitung sebelumnya (<code>mag_ratios</code>) untuk menentukan kapan reuse aman. Rasio-rasio tersebut spesifik untuk checkpoint dan scheduler. Rasio dari jumlah langkah yang tinggi dapat diinterpolasi ke jumlah langkah yang lebih sedikit.</p> <blockquote class="tip"><p><code>FLUX_MAG_RATIOS</code> yang disertakan diukur pada FLUX.1. Model lain, termasuk Flux2 dan Flux2 Klein, memerlukan kalibrasi tersendiri.</p></blockquote> <p>MagCache mengikuti dua langkah:</p> <ol><li>Kalibrasi: Jalankan inferensi sekali dengan <code>calibrate=True</code>. Hook mengukur magnitudo residual dan mencetak rasio yang dihitung.</li> <li>Inferensi: Nonaktifkan cache kalibrasi, lalu teruskan rasio tersebut ke <code>MagCacheConfig</code> untuk akselerasi.</li></ol> <p>Classifier-free guidance dapat memengaruhi kalibrasi. Pipeline yang menggunakan true CFG dengan konteks berurutan, seperti Flux ketika <code>true_cfg_scale > 1</code>, masuk ke <code>cache_context("cond")</code> dan <code>cache_context("uncond")</code> secara terpisah. Kalibrasi mungkin mencetak satu array per konteks, tetapi Anda sebaiknya menggunakan array kondisional dalam kebanyakan kasus. Pipeline yang melakukan batching CFG dengan menggabungkan input kondisional dan non-kondisional (misalnya, CogVideoX) menghasilkan satu array gabungan yang dapat Anda gunakan langsung.</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> Disalin</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> FluxPipeline, MagCacheConfig | |
| <span class="hljs-keyword">from</span> diffusers.hooks.mag_cache <span class="hljs-keyword">import</span> FLUX_MAG_RATIOS | |
| pipe = FluxPipeline.from_pretrained( | |
| <span class="hljs-string">"black-forest-labs/FLUX.1-schnell"</span>, | |
| dtype=torch.bfloat16 | |
| ).to(<span class="hljs-string">"cuda"</span>) <span class="hljs-comment"># or "mps", "xpu", "cpu"</span> | |
| <span class="hljs-comment"># 1. Calibration Step</span> | |
| <span class="hljs-comment"># Run full inference to measure model behavior.</span> | |
| calib_config = MagCacheConfig(calibrate=<span class="hljs-literal">True</span>, num_inference_steps=<span class="hljs-number">4</span>) | |
| pipe.transformer.enable_cache(calib_config) | |
| <span class="hljs-comment"># Run a prompt to trigger calibration</span> | |
| pipe(<span class="hljs-string">"A cat playing chess"</span>, num_inference_steps=<span class="hljs-number">4</span>) | |
| <span class="hljs-comment"># Prints: [MagCache] Calibration Complete. Copy these values to MagCacheConfig(mag_ratios=...):</span> | |
| <span class="hljs-comment"># 2. Inference Step</span> | |
| <span class="hljs-comment"># Disable calibration hooks before enabling MagCache for inference.</span> | |
| pipe.transformer.disable_cache() | |
| <span class="hljs-comment"># Apply ratios from calibration, or use the Flux defaults:</span> | |
| <span class="hljs-comment"># mag_ratios=FLUX_MAG_RATIOS</span> | |
| mag_config = MagCacheConfig( | |
| mag_ratios=[<span class="hljs-number">1.0</span>, <span class="hljs-number">1.37</span>, <span class="hljs-number">0.97</span>, <span class="hljs-number">0.87</span>], | |
| num_inference_steps=<span class="hljs-number">4</span> | |
| ) | |
| pipe.transformer.enable_cache(mag_config) | |
| image = pipe(<span class="hljs-string">"A cat playing chess"</span>, num_inference_steps=<span class="hljs-number">4</span>).images[<span class="hljs-number">0</span>]<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="text-kv-cache" 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="#text-kv-cache"><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><!---->Teks KV Cache<!----></span></h2><!--]--><!----> <p><a href="/docs/diffusers/main/id/api/cache#diffusers.TextKVCacheConfig">TextKVCacheConfig</a> menghitung proyeksi kunci dan nilai teks sekali dan memakainya kembali di setiap langkah denoising, alih-alih menghitungnya ulang. <a href="/docs/diffusers/main/id/api/cache#diffusers.apply_text_kv_cache">apply_text_kv_cache()</a> saat ini hanya mengaitkan <code>NucleusMoEImageTransformerBlock</code>. Aktifkan dengan <code>enable_cache</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> Disalin</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">import</span> torch | |
| <span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> NucleusMoEImagePipeline, TextKVCacheConfig | |
| pipe = NucleusMoEImagePipeline.from_pretrained( | |
| <span class="hljs-string">"NucleusAI/NucleusMoE-Image"</span>, dtype=torch.bfloat16 | |
| ) | |
| pipe.to(<span class="hljs-string">"cuda"</span>) <span class="hljs-comment"># or "mps", "xpu", "cpu"</span> | |
| pipe.transformer.enable_cache(TextKVCacheConfig()) | |
| image = pipe(<span class="hljs-string">"A cat holding a sign that says hello world"</span>, num_inference_steps=<span class="hljs-number">50</span>).images[<span class="hljs-number">0</span>]<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="add-caching-to-a-new-model" 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="#add-caching-to-a-new-model"><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><!---->Menambahkan caching ke model baru<!----></span></h2><!--]--><!----> <p>Metode cache menempel pada model melalui hook, dan hook tersebut bergantung pada kelas model, blok transformer-nya, dan loop denoising pipeline. Siapkan ketiga komponen tersebut sebelum memanggil <code>enable_cache</code> pada model baru.</p> <ol><li><p>Warisi dari <a href="/docs/diffusers/main/id/api/cache#diffusers.CacheMixin">CacheMixin</a> di kelas transformer. Ini menambahkan <code>enable_cache</code>, <code>disable_cache</code>, dan <code>cache_context</code>.</p></li> <li><p>Daftarkan tata letak keluaran blok transformer di <code>_register_transformer_blocks_metadata</code> dalam <code>hooks/_helpers.py</code>. Metode yang melewati atau menggunakan kembali seluruh blok, seperti FirstBlockCache, MagCache, dan SeaCache, menggunakannya untuk menemukan hidden states dalam keluaran blok tersebut.</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!---->TransformerBlockRegistry.register( | |
| model_class=MyTransformerBlock, | |
| metadata=TransformerBlockMetadata( | |
| return_hidden_states_index=<span class="hljs-number">0</span>, | |
| return_encoder_hidden_states_index=<span class="hljs-literal">None</span>, | |
| ), | |
| )<!----></pre></div><!----></li> <li><p>Bungkus setiap panggilan transformer dalam loop denoising pipeline dengan <code>cache_context</code>. Gunakan nama yang terpisah, seperti <code>"cond"</code> dan <code>"uncond"</code>, ketika classifier-free guidance menjalankan cabang kondisional dan non-kondisional sebagai panggilan terpisah, sehingga setiap cabang mempertahankan status cache-nya sendiri.</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> Disalin</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">with</span> <span class="hljs-variable language_">self</span>.transformer.cache_context(<span class="hljs-string">"cond"</span>): | |
| noise_pred = <span class="hljs-variable language_">self</span>.transformer(hidden_states=latents, timestep=timestep, ...)[<span class="hljs-number">0</span>]<!----></pre></div><!----></li></ol> <p>PAB dan FasterCache juga membutuhkan <code>current_timestep_callback</code> agar hook dapat membaca timestep saat ini. Periksa kualitas output pada model baru, karena pengaturan cache yang dioptimalkan untuk satu model sering kali tidak dapat diterapkan ke model lain.</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/optimization/cache.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_kft2r9 = { | |
| 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.B6roO9Zr.js"), | |
| import("/docs/diffusers/main/en/_app/immutable/entry/app.CxxEVG8A.js") | |
| ]).then(([kit, app]) => { | |
| kit.start(app, element, { | |
| node_ids: [0, 8], | |
| data: [null,null], | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 47.6 kB
- Xet hash:
- ddfe78f2b3f0a4c0aedfecb1a6ed08af254d34a84e738871b052be77922dd1b5
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.