import{s as ct,o as ft,n as ut}from"../chunks/scheduler.5c93273d.js";import{S as bt,i as Mt,g as i,s,r as C,A as dt,h as m,f as l,c as p,j as mt,u as U,x as r,k as ot,y as ht,a,v as X,d as W,t as k,w as B}from"../chunks/index.e43dd92b.js";import{T as wt}from"../chunks/Tip.1cbfe904.js";import{C as rt}from"../chunks/CodeBlock.6896320e.js";import{H as K,E as Jt}from"../chunks/getInferenceSnippets.d410b42d.js";function _t(N){let n,f="并非所有视频模型都有三种类型的注意力（交叉、时间和空间）！";return{c(){n=i("p"),n.textContent=f},l(o){n=m(o,"P",{"data-svelte-h":!0}),r(n)!=="svelte-hnfw5g"&&(n.textContent=f)},m(o,G){a(o,n,G)},p:ut,d(o){o&&l(n)}}}function yt(N){let n,f,o,G,u,R,b,O="缓存通过存储和重用不同层的中间输出（如注意力层和前馈层）来加速推理，而不是在每个推理步骤执行整个计算。它显著提高了生成速度，但以更多内存为代价，并且不需要额外的训练。",x,M,tt="本指南向您展示如何在 Diffusers 中使用支持的缓存方法。",v,d,F,h,et='<a href="https://huggingface.co/papers/2408.12588" rel="nofollow">金字塔注意力广播 (PAB)</a> 基于这样一种观察：在生成过程的连续时间步之间，注意力输出差异不大。注意力差异在交叉注意力层中最小，并且通常在一个较长的时间步范围内被缓存。其次是时间注意力和空间注意力层。',I,c,Q,w,lt="PAB 可以与其他技术（如序列并行性和无分类器引导并行性（数据并行性））结合，实现近乎实时的视频生成。",E,J,at="设置并传递一个 <code>PyramidAttentionBroadcastConfig</code> 到管道的变换器以启用它。<code>spatial_attention_block_skip_range</code> 控制跳过空间注意力块中注意力计算的频率，<code>spatial_attention_timestep_skip_range</code> 是要跳过的时间步范围。注意选择一个合适的范围，因为较小的间隔可能导致推理速度变慢，而较大的间隔可能导致生成质量降低。",V,_,P,y,S,T,nt='<a href="https://huggingface.co/papers/2410.19355" rel="nofollow">FasterCache</a> 缓存并重用注意力特征，类似于 <a href="#pyramid-attention-broadcast">PAB</a>，因为每个连续时间步的输出差异很小。',A,j,st=`此方法在使用无分类器引导进行采样时（在大多数基础模型中常见），也可能选择跳过无条件分支预测，并且
如果连续时间步之间的预测潜在输出存在显著冗余，则从条件分支预测中估计它。`,H,$,pt="设置并将 <code>FasterCacheConfig</code> 传递给管道的 transformer 以启用它。",z,Z,D,g,L,Y,q;return u=new K({props:{title:"缓存",local:"缓存",headingTag:"h1"}}),d=new K({props:{title:"金字塔注意力广播",local:"金字塔注意力广播",headingTag:"h2"}}),c=new wt({props:{warning:!1,$$slots:{default:[_t]},$$scope:{ctx:N}}}),_=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}}),y=new K({props:{title:"FasterCache",local:"fastercache",headingTag:"h2"}}),Z=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}}),g=new Jt({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/zh/optimization/cache.md"}}),{c(){n=i("meta"),f=s(),o=i("p"),G=s(),C(u.$$.fragment),R=s(),b=i("p"),b.textContent=O,x=s(),M=i("p"),M.textContent=tt,v=s(),C(d.$$.fragment),F=s(),h=i("p"),h.innerHTML=et,I=s(),C(c.$$.fragment),Q=s(),w=i("p"),w.textContent=lt,E=s(),J=i("p"),J.innerHTML=at,V=s(),C(_.$$.fragment),P=s(),C(y.$$.fragment),S=s(),T=i("p"),T.innerHTML=nt,A=s(),j=i("p"),j.textContent=st,H=s(),$=i("p"),$.innerHTML=pt,z=s(),C(Z.$$.fragment),D=s(),C(g.$$.fragment),L=s(),Y=i("p"),this.h()},l(t){const e=dt("svelte-u9bgzb",document.head);n=m(e,"META",{name:!0,content:!0}),e.forEach(l),f=p(t),o=m(t,"P",{}),mt(o).forEach(l),G=p(t),U(u.$$.fragment,t),R=p(t),b=m(t,"P",{"data-svelte-h":!0}),r(b)!=="svelte-1f1nqg4"&&(b.textContent=O),x=p(t),M=m(t,"P",{"data-svelte-h":!0}),r(M)!=="svelte-1onzyg1"&&(M.textContent=tt),v=p(t),U(d.$$.fragment,t),F=p(t),h=m(t,"P",{"data-svelte-h":!0}),r(h)!=="svelte-3i3btm"&&(h.innerHTML=et),I=p(t),U(c.$$.fragment,t),Q=p(t),w=m(t,"P",{"data-svelte-h":!0}),r(w)!=="svelte-1js4ud9"&&(w.textContent=lt),E=p(t),J=m(t,"P",{"data-svelte-h":!0}),r(J)!=="svelte-jg5pkc"&&(J.innerHTML=at),V=p(t),U(_.$$.fragment,t),P=p(t),U(y.$$.fragment,t),S=p(t),T=m(t,"P",{"data-svelte-h":!0}),r(T)!=="svelte-1op45ku"&&(T.innerHTML=nt),A=p(t),j=m(t,"P",{"data-svelte-h":!0}),r(j)!=="svelte-1dt33dj"&&(j.textContent=st),H=p(t),$=m(t,"P",{"data-svelte-h":!0}),r($)!=="svelte-1emi3m1"&&($.innerHTML=pt),z=p(t),U(Z.$$.fragment,t),D=p(t),U(g.$$.fragment,t),L=p(t),Y=m(t,"P",{}),mt(Y).forEach(l),this.h()},h(){ot(n,"name","hf:doc:metadata"),ot(n,"content",Tt)},m(t,e){ht(document.head,n),a(t,f,e),a(t,o,e),a(t,G,e),X(u,t,e),a(t,R,e),a(t,b,e),a(t,x,e),a(t,M,e),a(t,v,e),X(d,t,e),a(t,F,e),a(t,h,e),a(t,I,e),X(c,t,e),a(t,Q,e),a(t,w,e),a(t,E,e),a(t,J,e),a(t,V,e),X(_,t,e),a(t,P,e),X(y,t,e),a(t,S,e),a(t,T,e),a(t,A,e),a(t,j,e),a(t,H,e),a(t,$,e),a(t,z,e),X(Z,t,e),a(t,D,e),X(g,t,e),a(t,L,e),a(t,Y,e),q=!0},p(t,[e]){const it={};e&2&&(it.$$scope={dirty:e,ctx:t}),c.$set(it)},i(t){q||(W(u.$$.fragment,t),W(d.$$.fragment,t),W(c.$$.fragment,t),W(_.$$.fragment,t),W(y.$$.fragment,t),W(Z.$$.fragment,t),W(g.$$.fragment,t),q=!0)},o(t){k(u.$$.fragment,t),k(d.$$.fragment,t),k(c.$$.fragment,t),k(_.$$.fragment,t),k(y.$$.fragment,t),k(Z.$$.fragment,t),k(g.$$.fragment,t),q=!1},d(t){t&&(l(f),l(o),l(G),l(R),l(b),l(x),l(M),l(v),l(F),l(h),l(I),l(Q),l(w),l(E),l(J),l(V),l(P),l(S),l(T),l(A),l(j),l(H),l($),l(z),l(D),l(L),l(Y)),l(n),B(u,t),B(d,t),B(c,t),B(_,t),B(y,t),B(Z,t),B(g,t)}}}const Tt='{"title":"缓存","local":"缓存","sections":[{"title":"金字塔注意力广播","local":"金字塔注意力广播","sections":[],"depth":2},{"title":"FasterCache","local":"fastercache","sections":[],"depth":2}],"depth":1}';function jt(N){return ft(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Xt extends bt{constructor(n){super(),Mt(this,n,jt,yt,ct,{})}}export{Xt as component};
