import{s as ct,n as ft,o as bt}from"../chunks/scheduler.5c93273d.js";import{S as ut,i as Mt,g as p,s,r as Z,A as dt,h as i,f as l,c as n,j as ot,u as C,x as o,k as q,y as ht,a,v as g,d as X,t as k,w as W}from"../chunks/index.e43dd92b.js";import{C as rt}from"../chunks/CodeBlock.6896320e.js";import{H as K,E as wt}from"../chunks/getInferenceSnippets.7a494bf3.js";function Jt(O){let m,G,$,Y,c,N,f,tt="缓存通过存储和重用不同层的中间输出（如注意力层和前馈层）来加速推理，而不是在每个推理步骤执行整个计算。它显著提高了生成速度，但以更多内存为代价，并且不需要额外的训练。",R,b,et="本指南向您展示如何在 Diffusers 中使用支持的缓存方法。",v,u,x,M,lt='<a href="https://huggingface.co/papers/2408.12588" rel="nofollow">金字塔注意力广播 (PAB)</a> 基于这样一种观察：在生成过程的连续时间步之间，注意力输出差异不大。注意力差异在交叉注意力层中最小，并且通常在一个较长的时间步范围内被缓存。其次是时间注意力和空间注意力层。',F,r,at="<p>并非所有视频模型都有三种类型的注意力（交叉、时间和空间）！</p>",I,d,st="PAB 可以与其他技术（如序列并行性和无分类器引导并行性（数据并行性））结合，实现近乎实时的视频生成。",Q,h,nt="设置并传递一个 <code>PyramidAttentionBroadcastConfig</code> 到管道的变换器以启用它。<code>spatial_attention_block_skip_range</code> 控制跳过空间注意力块中注意力计算的频率，<code>spatial_attention_timestep_skip_range</code> 是要跳过的时间步范围。注意选择一个合适的范围，因为较小的间隔可能导致推理速度变慢，而较大的间隔可能导致生成质量降低。",E,w,V,J,H,y,pt='<a href="https://huggingface.co/papers/2410.19355" rel="nofollow">FasterCache</a> 缓存并重用注意力特征，类似于 <a href="#pyramid-attention-broadcast">PAB</a>，因为每个连续时间步的输出差异很小。',P,_,it=`此方法在使用无分类器引导进行采样时（在大多数基础模型中常见），也可能选择跳过无条件分支预测，并且
如果连续时间步之间的预测潜在输出存在显著冗余，则从条件分支预测中估计它。`,S,T,mt="设置并将 <code>FasterCacheConfig</code> 传递给管道的 transformer 以启用它。",A,j,z,U,L,B,D;return c=new K({props:{title:"缓存",local:"缓存",headingTag:"h1"}}),u=new K({props:{title:"金字塔注意力广播",local:"金字塔注意力广播",headingTag:"h2"}}),w=new rt({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwQ29nVmlkZW9YUGlwZWxpbmUlMkMlMjBQeXJhbWlkQXR0ZW50aW9uQnJvYWRjYXN0Q29uZmlnJTBBJTBBcGlwZWxpbmUlMjAlM0QlMjBDb2dWaWRlb1hQaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTIyVEhVRE0lMkZDb2dWaWRlb1gtNWIlMjIlMkMlMjB0b3JjaF9kdHlwZSUzRHRvcmNoLmJmbG9hdDE2KSUwQXBpcGVsaW5lLnRvKCUyMmN1ZGElMjIpJTBBJTBBY29uZmlnJTIwJTNEJTIwUHlyYW1pZEF0dGVudGlvbkJyb2FkY2FzdENvbmZpZyglMEElMjAlMjAlMjAlMjBzcGF0aWFsX2F0dGVudGlvbl9ibG9ja19za2lwX3JhbmdlJTNEMiUyQyUwQSUyMCUyMCUyMCUyMHNwYXRpYWxfYXR0ZW50aW9uX3RpbWVzdGVwX3NraXBfcmFuZ2UlM0QoMTAwJTJDJTIwODAwKSUyQyUwQSUyMCUyMCUyMCUyMGN1cnJlbnRfdGltZXN0ZXBfY2FsbGJhY2slM0RsYW1iZGElM0ElMjBwaXBlLmN1cnJlbnRfdGltZXN0ZXAlMkMlMEEpJTBBcGlwZWxpbmUudHJhbnNmb3JtZXIuZW5hYmxlX2NhY2hlKGNvbmZpZyk=",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> CogVideoXPipeline, PyramidAttentionBroadcastConfig

pipeline = CogVideoXPipeline.from_pretrained(<span class="hljs-string">&quot;THUDM/CogVideoX-5b&quot;</span>, torch_dtype=torch.bfloat16)
pipeline.to(<span class="hljs-string">&quot;cuda&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,
)
pipeline.transformer.enable_cache(config)`,wrap:!1}}),J=new K({props:{title:"FasterCache",local:"fastercache",headingTag:"h2"}}),j=new rt({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwQ29nVmlkZW9YUGlwZWxpbmUlMkMlMjBGYXN0ZXJDYWNoZUNvbmZpZyUwQSUwQXBpcGUlMjBsaW5lJTNEJTIwQ29nVmlkZW9YUGlwZWxpbmUuZnJvbV9wcmV0cmFpbmVkKCUyMlRIVURNJTJGQ29nVmlkZW9YLTViJTIyJTJDJTIwdG9yY2hfZHR5cGUlM0R0b3JjaC5iZmxvYXQxNiklMEFwaXBlbGluZS50byglMjJjdWRhJTIyKSUwQSUwQWNvbmZpZyUyMCUzRCUyMEZhc3RlckNhY2hlQ29uZmlnKCUwQSUyMCUyMCUyMCUyMHNwYXRpYWxfYXR0ZW50aW9uX2Jsb2NrX3NraXBfcmFuZ2UlM0QyJTJDJTBBJTIwJTIwJTIwJTIwc3BhdGlhbF9hdHRlbnRpb25fdGltZXN0ZXBfc2tpcF9yYW5nZSUzRCgtMSUyQyUyMDY4MSklMkMlMEElMjAlMjAlMjAlMjBjdXJyZW50X3RpbWVzdGVwX2NhbGxiYWNrJTNEbGFtYmRhJTNBJTIwcGlwZS5jdXJyZW50X3RpbWVzdGVwJTJDJTBBJTIwJTIwJTIwJTIwYXR0ZW50aW9uX3dlaWdodF9jYWxsYmFjayUzRGxhbWJkYSUyMF8lM0ElMjAwLjMlMkMlMEElMjAlMjAlMjAlMjB1bmNvbmRpdGlvbmFsX2JhdGNoX3NraXBfcmFuZ2UlM0Q1JTJDJTBBJTIwJTIwJTIwJTIwdW5jb25kaXRpb25hbF9iYXRjaF90aW1lc3RlcF9za2lwX3JhbmdlJTNEKC0xJTJDJTIwNzgxKSUyQyUwQSUyMCUyMCUyMCUyMHRlbnNvcl9mb3JtYXQlM0QlMjJCRkNIVyUyMiUyQyUwQSklMEFwaXBlbGluZS50cmFuc2Zvcm1lci5lbmFibGVfY2FjaGUoY29uZmlnKQ==",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> CogVideoXPipeline, FasterCacheConfig

pipe line= CogVideoXPipeline.from_pretrained(<span class="hljs-string">&quot;THUDM/CogVideoX-5b&quot;</span>, torch_dtype=torch.bfloat16)
pipeline.to(<span class="hljs-string">&quot;cuda&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>,
)
pipeline.transformer.enable_cache(config)`,wrap:!1}}),U=new wt({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/zh/optimization/cache.md"}}),{c(){m=p("meta"),G=s(),$=p("p"),Y=s(),Z(c.$$.fragment),N=s(),f=p("p"),f.textContent=tt,R=s(),b=p("p"),b.textContent=et,v=s(),Z(u.$$.fragment),x=s(),M=p("p"),M.innerHTML=lt,F=s(),r=p("blockquote"),r.innerHTML=at,I=s(),d=p("p"),d.textContent=st,Q=s(),h=p("p"),h.innerHTML=nt,E=s(),Z(w.$$.fragment),V=s(),Z(J.$$.fragment),H=s(),y=p("p"),y.innerHTML=pt,P=s(),_=p("p"),_.textContent=it,S=s(),T=p("p"),T.innerHTML=mt,A=s(),Z(j.$$.fragment),z=s(),Z(U.$$.fragment),L=s(),B=p("p"),this.h()},l(t){const e=dt("svelte-u9bgzb",document.head);m=i(e,"META",{name:!0,content:!0}),e.forEach(l),G=n(t),$=i(t,"P",{}),ot($).forEach(l),Y=n(t),C(c.$$.fragment,t),N=n(t),f=i(t,"P",{"data-svelte-h":!0}),o(f)!=="svelte-1f1nqg4"&&(f.textContent=tt),R=n(t),b=i(t,"P",{"data-svelte-h":!0}),o(b)!=="svelte-1onzyg1"&&(b.textContent=et),v=n(t),C(u.$$.fragment,t),x=n(t),M=i(t,"P",{"data-svelte-h":!0}),o(M)!=="svelte-3i3btm"&&(M.innerHTML=lt),F=n(t),r=i(t,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),o(r)!=="svelte-r7ebaz"&&(r.innerHTML=at),I=n(t),d=i(t,"P",{"data-svelte-h":!0}),o(d)!=="svelte-1js4ud9"&&(d.textContent=st),Q=n(t),h=i(t,"P",{"data-svelte-h":!0}),o(h)!=="svelte-jg5pkc"&&(h.innerHTML=nt),E=n(t),C(w.$$.fragment,t),V=n(t),C(J.$$.fragment,t),H=n(t),y=i(t,"P",{"data-svelte-h":!0}),o(y)!=="svelte-1op45ku"&&(y.innerHTML=pt),P=n(t),_=i(t,"P",{"data-svelte-h":!0}),o(_)!=="svelte-1dt33dj"&&(_.textContent=it),S=n(t),T=i(t,"P",{"data-svelte-h":!0}),o(T)!=="svelte-1emi3m1"&&(T.innerHTML=mt),A=n(t),C(j.$$.fragment,t),z=n(t),C(U.$$.fragment,t),L=n(t),B=i(t,"P",{}),ot(B).forEach(l),this.h()},h(){q(m,"name","hf:doc:metadata"),q(m,"content",yt),q(r,"class","tip")},m(t,e){ht(document.head,m),a(t,G,e),a(t,$,e),a(t,Y,e),g(c,t,e),a(t,N,e),a(t,f,e),a(t,R,e),a(t,b,e),a(t,v,e),g(u,t,e),a(t,x,e),a(t,M,e),a(t,F,e),a(t,r,e),a(t,I,e),a(t,d,e),a(t,Q,e),a(t,h,e),a(t,E,e),g(w,t,e),a(t,V,e),g(J,t,e),a(t,H,e),a(t,y,e),a(t,P,e),a(t,_,e),a(t,S,e),a(t,T,e),a(t,A,e),g(j,t,e),a(t,z,e),g(U,t,e),a(t,L,e),a(t,B,e),D=!0},p:ft,i(t){D||(X(c.$$.fragment,t),X(u.$$.fragment,t),X(w.$$.fragment,t),X(J.$$.fragment,t),X(j.$$.fragment,t),X(U.$$.fragment,t),D=!0)},o(t){k(c.$$.fragment,t),k(u.$$.fragment,t),k(w.$$.fragment,t),k(J.$$.fragment,t),k(j.$$.fragment,t),k(U.$$.fragment,t),D=!1},d(t){t&&(l(G),l($),l(Y),l(N),l(f),l(R),l(b),l(v),l(x),l(M),l(F),l(r),l(I),l(d),l(Q),l(h),l(E),l(V),l(H),l(y),l(P),l(_),l(S),l(T),l(A),l(z),l(L),l(B)),l(m),W(c,t),W(u,t),W(w,t),W(J,t),W(j,t),W(U,t)}}}const yt='{"title":"缓存","local":"缓存","sections":[{"title":"金字塔注意力广播","local":"金字塔注意力广播","sections":[],"depth":2},{"title":"FasterCache","local":"fastercache","sections":[],"depth":2}],"depth":1}';function _t(O){return bt(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ct extends ut{constructor(m){super(),Mt(this,m,_t,Jt,ct,{})}}export{Ct as component};
