import{s as me,n as de,o as fe}from"../chunks/scheduler.8c3d61f6.js";import{S as he,i as ue,g as i,s,r as j,A as be,h as o,f as a,c as l,j as pe,u as U,x as p,k as D,y as we,a as n,v as Z,d as k,t as X,w as W}from"../chunks/index.da70eac4.js";import{C as ce}from"../chunks/CodeBlock.a9c4becf.js";import{H as K,E as ye}from"../chunks/getInferenceSnippets.cb466318.js";function Me(O){let r,$,v,G,m,N,d,ee="Caching accelerates inference by storing and reusing intermediate outputs of different layers, such as attention and feedforward layers, instead of performing the entire computation at each inference step. It significantly improves generation speed at the expense of more memory and doesn’t require additional training.",Y,f,te="This guide shows you how to use the caching methods supported in Diffusers.",R,h,x,u,ae='<a href="https://huggingface.co/papers/2408.12588" rel="nofollow">Pyramid Attention Broadcast (PAB)</a> is based on the observation that attention outputs aren’t that different between successive timesteps of the generation process. The attention differences are smallest in the cross attention layers and are generally cached over a longer timestep range. This is followed by temporal attention and spatial attention layers.',F,c,ne="<p>Not all video models have three types of attention (cross, temporal, and spatial)!</p>",I,b,se="PAB can be combined with other techniques like sequence parallelism and classifier-free guidance parallelism (data parallelism) for near real-time video generation.",Q,w,le='Set up and pass a <a href="/docs/diffusers/main/en/api/cache#diffusers.PyramidAttentionBroadcastConfig">PyramidAttentionBroadcastConfig</a> to a pipeline’s transformer to enable it. The <code>spatial_attention_block_skip_range</code> controls how often to skip attention calculations in the spatial attention blocks and the <code>spatial_attention_timestep_skip_range</code> is the range of timesteps to skip. Take care to choose an appropriate range because a smaller interval can lead to slower inference speeds and a larger interval can result in lower generation quality.',E,y,V,M,P,J,ie='<a href="https://huggingface.co/papers/2410.19355" rel="nofollow">FasterCache</a> caches and reuses attention features similar to <a href="#pyramid-attention-broadcast">PAB</a> since output differences are small for each successive timestep.',A,g,oe="This method may also choose to skip the unconditional branch prediction, when using classifier-free guidance for sampling (common in most base models), and estimate it from the conditional branch prediction if there is significant redundancy in the predicted latent outputs between successive timesteps.",S,T,re='Set up and pass a <a href="/docs/diffusers/main/en/api/cache#diffusers.FasterCacheConfig">FasterCacheConfig</a> to a pipeline’s transformer to enable it.',H,_,q,C,z,B,L;return m=new K({props:{title:"Caching",local:"caching",headingTag:"h1"}}),h=new K({props:{title:"Pyramid Attention Broadcast",local:"pyramid-attention-broadcast",headingTag:"h2"}}),y=new ce({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}}),M=new K({props:{title:"FasterCache",local:"fastercache",headingTag:"h2"}}),_=new ce({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}}),C=new ye({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/en/optimization/cache.md"}}),{c(){r=i("meta"),$=s(),v=i("p"),G=s(),j(m.$$.fragment),N=s(),d=i("p"),d.textContent=ee,Y=s(),f=i("p"),f.textContent=te,R=s(),j(h.$$.fragment),x=s(),u=i("p"),u.innerHTML=ae,F=s(),c=i("blockquote"),c.innerHTML=ne,I=s(),b=i("p"),b.textContent=se,Q=s(),w=i("p"),w.innerHTML=le,E=s(),j(y.$$.fragment),V=s(),j(M.$$.fragment),P=s(),J=i("p"),J.innerHTML=ie,A=s(),g=i("p"),g.textContent=oe,S=s(),T=i("p"),T.innerHTML=re,H=s(),j(_.$$.fragment),q=s(),j(C.$$.fragment),z=s(),B=i("p"),this.h()},l(e){const t=be("svelte-u9bgzb",document.head);r=o(t,"META",{name:!0,content:!0}),t.forEach(a),$=l(e),v=o(e,"P",{}),pe(v).forEach(a),G=l(e),U(m.$$.fragment,e),N=l(e),d=o(e,"P",{"data-svelte-h":!0}),p(d)!=="svelte-y872v2"&&(d.textContent=ee),Y=l(e),f=o(e,"P",{"data-svelte-h":!0}),p(f)!=="svelte-67viyl"&&(f.textContent=te),R=l(e),U(h.$$.fragment,e),x=l(e),u=o(e,"P",{"data-svelte-h":!0}),p(u)!=="svelte-175y28v"&&(u.innerHTML=ae),F=l(e),c=o(e,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),p(c)!=="svelte-jobxuf"&&(c.innerHTML=ne),I=l(e),b=o(e,"P",{"data-svelte-h":!0}),p(b)!=="svelte-rlr2qi"&&(b.textContent=se),Q=l(e),w=o(e,"P",{"data-svelte-h":!0}),p(w)!=="svelte-1c6hyt4"&&(w.innerHTML=le),E=l(e),U(y.$$.fragment,e),V=l(e),U(M.$$.fragment,e),P=l(e),J=o(e,"P",{"data-svelte-h":!0}),p(J)!=="svelte-1hv75yq"&&(J.innerHTML=ie),A=l(e),g=o(e,"P",{"data-svelte-h":!0}),p(g)!=="svelte-1ekw8kg"&&(g.textContent=oe),S=l(e),T=o(e,"P",{"data-svelte-h":!0}),p(T)!=="svelte-11scqgp"&&(T.innerHTML=re),H=l(e),U(_.$$.fragment,e),q=l(e),U(C.$$.fragment,e),z=l(e),B=o(e,"P",{}),pe(B).forEach(a),this.h()},h(){D(r,"name","hf:doc:metadata"),D(r,"content",Je),D(c,"class","tip")},m(e,t){we(document.head,r),n(e,$,t),n(e,v,t),n(e,G,t),Z(m,e,t),n(e,N,t),n(e,d,t),n(e,Y,t),n(e,f,t),n(e,R,t),Z(h,e,t),n(e,x,t),n(e,u,t),n(e,F,t),n(e,c,t),n(e,I,t),n(e,b,t),n(e,Q,t),n(e,w,t),n(e,E,t),Z(y,e,t),n(e,V,t),Z(M,e,t),n(e,P,t),n(e,J,t),n(e,A,t),n(e,g,t),n(e,S,t),n(e,T,t),n(e,H,t),Z(_,e,t),n(e,q,t),Z(C,e,t),n(e,z,t),n(e,B,t),L=!0},p:de,i(e){L||(k(m.$$.fragment,e),k(h.$$.fragment,e),k(y.$$.fragment,e),k(M.$$.fragment,e),k(_.$$.fragment,e),k(C.$$.fragment,e),L=!0)},o(e){X(m.$$.fragment,e),X(h.$$.fragment,e),X(y.$$.fragment,e),X(M.$$.fragment,e),X(_.$$.fragment,e),X(C.$$.fragment,e),L=!1},d(e){e&&(a($),a(v),a(G),a(N),a(d),a(Y),a(f),a(R),a(x),a(u),a(F),a(c),a(I),a(b),a(Q),a(w),a(E),a(V),a(P),a(J),a(A),a(g),a(S),a(T),a(H),a(q),a(z),a(B)),a(r),W(m,e),W(h,e),W(y,e),W(M,e),W(_,e),W(C,e)}}}const Je='{"title":"Caching","local":"caching","sections":[{"title":"Pyramid Attention Broadcast","local":"pyramid-attention-broadcast","sections":[],"depth":2},{"title":"FasterCache","local":"fastercache","sections":[],"depth":2}],"depth":1}';function ge(O){return fe(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ue extends he{constructor(r){super(),ue(this,r,ge,Me,me,{})}}export{Ue as component};
