Buckets:

mishig's picture
download
raw
48.7 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Mise en cache&quot;,&quot;local&quot;:&quot;caching&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Choisir une méthode de cache&quot;,&quot;local&quot;:&quot;choose-a-cache-method&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Diffusion de l'attention pyramidale&quot;,&quot;local&quot;:&quot;pyramid-attention-broadcast&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;FasterCache&quot;,&quot;local&quot;:&quot;fastercache&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;SeaCache&quot;,&quot;local&quot;:&quot;seacache&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;FirstBlockCache&quot;,&quot;local&quot;:&quot;firstblockcache&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;TaylorSeer Cache&quot;,&quot;local&quot;:&quot;taylorseer-cache&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;MagCache&quot;,&quot;local&quot;:&quot;magcache&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Cache KV de texte&quot;,&quot;local&quot;:&quot;text-kv-cache&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Ajouter la mise en cache à un nouveau modèle&quot;,&quot;local&quot;:&quot;add-caching-to-a-new-model&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;: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="{&quot;title&quot;:&quot;Caching&quot;,&quot;local&quot;:&quot;caching&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Choose a cache method&quot;,&quot;local&quot;:&quot;choose-a-cache-method&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Pyramid Attention Broadcast&quot;,&quot;local&quot;:&quot;pyramid-attention-broadcast&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;FasterCache&quot;,&quot;local&quot;:&quot;fastercache&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;SeaCache&quot;,&quot;local&quot;:&quot;seacache&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;FirstBlockCache&quot;,&quot;local&quot;:&quot;firstblockcache&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;TaylorSeer Cache&quot;,&quot;local&quot;:&quot;taylorseer-cache&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;MagCache&quot;,&quot;local&quot;:&quot;magcache&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Text KV Cache&quot;,&quot;local&quot;:&quot;text-kv-cache&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Add caching to a new model&quot;,&quot;local&quot;:&quot;add-caching-to-a-new-model&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="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><!---->Mise en cache<!----></span></h1><!--]--><!----> <p>Le cache réutilise les sorties des couches intermédiaires entre les étapes de débruitage pour accélérer l’inférence. Il utilise plus de mémoire et ne nécessite pas d’entraînement. Activez une méthode avec une configuration sur un transformer qui prend en charge le cache.</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><!---->Choisir une méthode de cache<!----></span></h2><!--]--><!----> <p>Choisissez une méthode en fonction de la quantité de configuration que vous allez définir et de la précision requise. Chaque méthode équilibre la vitesse, la mémoire et la fidélité des sorties par rapport à une exécution non mise en cache de manière différente. Comparez les sorties avec et sans cache sur vos propres prompts avant de vous fier à une méthode.</p> <table><thead><tr><th>Méthode</th><th>À utiliser lorsque</th><th>Notes</th></tr></thead><tbody><tr><td><a href="#text-kv-cache">Cache KV de texte</a></td><td>Image NucleusMoE, réutilisation des projections K/V du texte entre les étapes</td><td>Actuellement, les blocs d’images NucleusMoE sont interceptés</td></tr><tr><td><a href="#seacache">SeaCache</a></td><td>Génération de vidéos Cosmos 3</td><td>Les paramètres ne sont souvent pas transférables d'un modèle à l'autre</td></tr><tr><td><a href="#firstblockcache">FirstBlockCache</a></td><td>Voulez-vous un seul réglage principal de vitesse/qualité sur un transformateur enregistré</td><td>Un <code>threshold</code> contrôle la fréquence à laquelle les étapes sont ignorées</td></tr><tr><td><a href="#magcache">MagCache</a></td><td>Avez-vous des rapports d'amplitude pour votre point de contrôle et votre planificateur, ou allez-vous d'abord effectuer une calibration</td><td>Les ratios sont spécifiques au point de contrôle et au planificateur</td></tr><tr><td><a href="#taylorseer-cache">TaylorSeer</a></td><td>Souhaitez-vous prédire des activations ultérieures à partir d'étapes antérieures</td><td>Un <code>max_order</code> plus élevé utilise plus de mémoire</td></tr><tr><td><a href="#pyramid-attention-broadcast">PAB</a></td><td>Vidéo, prêt à ajuster la réutilisation de l'attention (intervalles de blocs et de pas de temps à sauter par type d'attention)</td><td>Les plages à ignorer nécessitent un réglage spécifique à chaque modèle</td></tr><tr><td><a href="#fastercache">FasterCache</a></td><td>Comme PAB, avec en plus le saut optionnel de la branche CFG</td><td>Expérimental</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><!---->Diffusion de l'attention pyramidale<!----></span></h2><!--]--><!----> <p><a href="https://huggingface.co/papers/2408.12588" rel="nofollow">Pyramid Attention Broadcast (PAB)</a> approxime l'attention sur les étapes de débruitage en réutilisant les sorties d'attention pour certains blocs et certaines étapes temporelles, plutôt que de tout recalculer à chaque étape. La configuration distingue les types d'attention (spatiale, temporelle, croisée) lorsque le modèle les possède. Tous les modèles vidéo n'exposent pas les trois types ; définissez uniquement les plages correspondant aux blocs dont vous disposez.</p> <p>Chaque type utilise un <code>*_attention_block_skip_range</code> (fréquence de recalcul par rapport à la réutilisation au sein de la fenêtre) et un <code>*_attention_timestep_skip_range</code> (étapes de débruitage pouvant être ignorées). Vous devez passer <code>current_timestep_callback</code> afin que l’hook puisse lire l’étape actuelle du pipeline. Des sauts plus larges ou plus agressifs signifient généralement plus de vitesse et un risque de qualité accru.</p> <p>Passez un <a href="/docs/diffusers/main/fr/api/cache#diffusers.PyramidAttentionBroadcastConfig">PyramidAttentionBroadcastConfig</a> pour l’activer.</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> Copié</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">&quot;THUDM/CogVideoX-5b&quot;</span>, dtype=torch.bfloat16)
pipe.to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</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> met en cache et réutilise les caractéristiques d’attention de manière similaire à <a href="#pyramid-attention-broadcast">PAB</a>. Il peut également ignorer la branche inconditionnelle sous guidage sans classifieur et l’estimer à partir de la branche conditionnelle lorsque les latents successifs sont suffisamment redondants.</p> <p>Passez un <a href="/docs/diffusers/main/fr/api/cache#diffusers.FasterCacheConfig">FasterCacheConfig</a> pour l’activer. Comme pour PAB, définissez <code>*_attention_block_skip_range</code> et <code>*_attention_timestep_skip_range</code> pour les types d’attention dont vous disposez, ainsi que les options de saut de branche CFG si vous les souhaitez.</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> Copié</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">&quot;THUDM/CogVideoX-5b&quot;</span>, dtype=torch.bfloat16)
pipe.to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</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">&quot;BFCHW&quot;</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> compare les indicateurs Spectral Evolution Aware (SEA) entre les étapes successives de débruitage. Lorsque la variation accumulée reste sous un seuil, il passe outre la pile de blocs transformer et prédit la sortie à partir des résidus mis en cache. La méthode est approximative et conçue pour la génération de vidéos.</p> <p>Les adaptateurs intégrés à SeaCache incluent :</p> <ul><li>Cosmos 3 est l'intégration principale, optimisée et testée.</li> <li>Wan T2V utilise le chemin générique de blocs répétés comme démonstration de la manière de fournir les latents visuels bruts à SeaCache. Les mêmes paramètres de cache ne sont peut-être pas transférables à Wan ou à d'autres variantes de Wan.</li></ul> <p>Activez SeaCache sur le transformer. La boucle de débruitage de Cosmos 3 associe l’étape du planificateur, la valeur sigma et le nombre d’étapes à chaque <code>cache_context</code>, de sorte qu’aucun paramètre supplémentaire n’est nécessaire.</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> Copié</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">&quot;nvidia/Cosmos3-Nano&quot;</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 peut modifier les sorties. Appelez <code>pipe.transformer.disable_cache()</code> lorsque vous avez besoin que chaque étape exécute le transformeur complet. L’appel d’activation identique fonctionne avec <a href="/docs/diffusers/main/fr/api/pipelines/cosmos3#diffusers.Cosmos3OmniPipeline">Cosmos3OmniPipeline</a>, <a href="/docs/diffusers/main/fr/api/pipelines/cosmos3#diffusers.Cosmos3OmniModularPipeline">Cosmos3OmniModularPipeline</a> et <a href="/docs/diffusers/main/fr/api/pipelines/cosmos3#diffusers.Cosmos3DistilledModularPipeline">Cosmos3DistilledModularPipeline</a>.</p> <p>Pour utiliser SeaCache avec un autre transformeur vidéo, suivez <a href="#add-caching-to-a-new-model">Ajouter la mise en cache à un nouveau modèle</a>. SeaCache nécessite également <code>step_index</code>, <code>sigma</code> et <code>num_inference_steps</code> dans chaque <code>cache_context</code>, ainsi qu'un <code>raw_vision_callback</code> lorsqu'aucun adaptateur intégré n'existe. Ajustez les paramètres en fonction du modèle et du planificateur.</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/fr/api/cache#diffusers.FirstBlockCacheConfig">FirstBlockCacheConfig</a> mesure dans quelle mesure les premières couches du débruiteur évoluent d'un pas de temps à l'autre. Si la variation est faible, le modèle passe outre les couches ultérieures coûteuses et réutilise la sortie précédente.</p> <p>Activez-le via <code>enable_cache</code> afin que <code>disable_cache</code> et <code>is_cache_enabled</code> restent synchronisés. La valeur par défaut de <code>threshold</code> est <code>0.05</code>. Une valeur plus élevée, comme <code>0.2</code>, saute plus souvent pour un gain de vitesse supplémentaire, mais la qualité de la génération peut en souffrir.</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> Copié</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">&quot;Qwen/Qwen-Image&quot;</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><!---->TaylorSeer Cache<!----></span></h2><!--]--><!----> <p><a href="https://huggingface.co/papers/2503.06923" rel="nofollow">TaylorSeer Cache</a> accélère l'inférence des modèles de diffusion grâce à des développements en série de Taylor sur les étapes de débruitage. Il prédit les activations des étapes ultérieures à partir des étapes antérieures et réutilise ces prédictions pendant plusieurs étapes, ce qui réduit le travail complet effectué par le transformer.</p> <ul><li><code>cache_interval</code> : Nombre d'étapes pour réutiliser les sorties mises en cache avant d'effectuer un passage avant complet</li> <li><code>disable_cache_before_step</code> : Étapes initiales qui utilisent des calculs complets pour collecter les données nécessaires aux approximations</li> <li><code>max_order</code> : Des ordres de Taylor plus élevés peuvent être plus précis mais utilisent plus de mémoire. Conservez cette valeur à <code>1</code> sauf si vous avez une raison de la modifier.</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> Copié</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">&quot;black-forest-labs/FLUX.1-dev&quot;</span>,
dtype=torch.bfloat16,
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</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> saute des blocs de transformeur en fonction de l'amplitude de la mise à jour résiduelle. Les amplitudes de mise à jour décroissent de manière prévisible au fil du débruitage, et MagCache suit un budget d'erreur à partir de rapports d'amplitude précalculés (<code>mag_ratios</code>) pour déterminer quand la réutilisation est sûre. Ces rapports sont spécifiques au point de contrôle et au planificateur. Les rapports issus d'un grand nombre d'étapes peuvent être interpolés vers un nombre d'étapes inférieur.</p> <blockquote class="tip"><p>Les <code>FLUX_MAG_RATIOS</code> inclus ont été mesurés sur FLUX.1. D'autres modèles, y compris Flux2 et Flux2 Klein, nécessitent leur propre exécution de calibration.</p></blockquote> <p>MagCache suit deux étapes :</p> <ol><li>Calibration : exécutez une inférence avec <code>calibrate=True</code>. Le hook mesure les amplitudes des résidus et affiche les ratios calculés.</li> <li>Inférence : désactivez le cache de calibration, puis transmettez ces ratios à <code>MagCacheConfig</code> pour accélérer le processus.</li></ol> <p>Le guidage sans classifieur peut affecter l’étalonnage. Les pipelines qui utilisent le CFG réel avec des contextes séquentiels, comme Flux lorsque <code>true_cfg_scale > 1</code>, entrent séparément dans <code>cache_context("cond")</code> et <code>cache_context("uncond")</code>. L’étalonnage peut afficher un tableau par contexte, mais vous devriez utiliser le tableau conditionnel dans la plupart des cas. Les pipelines qui appliquent le CFG par lot en concaténant les entrées conditionnelles et non conditionnelles (par exemple, CogVideoX) produisent un tableau unique et conjoint que vous pouvez utiliser directement.</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> Copié</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">&quot;black-forest-labs/FLUX.1-schnell&quot;</span>,
dtype=torch.bfloat16
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</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">&quot;A cat playing chess&quot;</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">&quot;A cat playing chess&quot;</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><!---->Cache KV de texte<!----></span></h2><!--]--><!----> <p><a href="/docs/diffusers/main/fr/api/cache#diffusers.TextKVCacheConfig">TextKVCacheConfig</a> calcule les projections de clés et de valeurs du texte une seule fois et les réutilise à chaque étape de débruitage, au lieu de les recalculer. <a href="/docs/diffusers/main/fr/api/cache#diffusers.apply_text_kv_cache">apply_text_kv_cache()</a> ne cible actuellement que <code>NucleusMoEImageTransformerBlock</code>. Activez-le avec <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> Copié</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">&quot;NucleusAI/NucleusMoE-Image&quot;</span>, dtype=torch.bfloat16
)
pipe.to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span>
pipe.transformer.enable_cache(TextKVCacheConfig())
image = pipe(<span class="hljs-string">&quot;A cat holding a sign that says hello world&quot;</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><!---->Ajouter la mise en cache à un nouveau modèle<!----></span></h2><!--]--><!----> <p>Les méthodes de cache s’attachent à un modèle via des hooks, et ces hooks dépendent de la classe du modèle, de ses blocs transformer et de la boucle de débruitage du pipeline. Configurez les trois éléments avant d’appeler <code>enable_cache</code> sur un nouveau modèle.</p> <ol><li><p>Hériter de <a href="/docs/diffusers/main/fr/api/cache#diffusers.CacheMixin">CacheMixin</a> dans la classe transformer. Cela ajoute <code>enable_cache</code>, <code>disable_cache</code> et <code>cache_context</code>.</p></li> <li><p>Enregistrez la disposition de la sortie du bloc de transformer dans <code>_register_transformer_blocks_metadata</code> dans <code>hooks/_helpers.py</code>. Les méthodes qui ignorent ou réutilisent des blocs entiers, telles que FirstBlockCache, MagCache et SeaCache, l’utilisent pour localiser les états cachés dans les sorties du bloc.</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> Copié</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>Enveloppez chaque appel de transformer dans la boucle de débruitage du pipeline avec <code>cache_context</code>. Utilisez des noms distincts, tels que <code>"cond"</code> et <code>"uncond"</code>, lorsque le guidage sans classifieur exécute les branches conditionnelle et non conditionnelle en tant qu’appels séparés, afin que chaque branche conserve son propre état de cache.</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> Copié</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">&quot;cond&quot;</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 et FasterCache ont également besoin de <code>current_timestep_callback</code> afin que les hooks puissent lire le timestep actuel. Vérifiez la qualité de la sortie sur le nouveau modèle, car les réglages de cache optimisés pour un modèle ne se transmettent souvent pas à un autre.</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:
48.7 kB
·
Xet hash:
35c8990b71fb668bc4d2927f7ab24142bb5b6f1c8b6ec07478f3cf4528b8ea99

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