import{s as me,o as fe,n as de}from"../chunks/scheduler.8c3d61f6.js";import{S as he,i as ue,g as o,s as l,r as j,A as be,h as r,f as a,c as i,j as re,u as U,x as c,k as pe,y as we,a as n,v as k,d as X,t as W,w as v}from"../chunks/index.da70eac4.js";import{T as ye}from"../chunks/Tip.1d9b8c37.js";import{C as ce}from"../chunks/CodeBlock.a9c4becf.js";import{H as K,E as Me}from"../chunks/getInferenceSnippets.33773fa8.js";function ge(N){let s,f="Not all video models have three types of attention (cross, temporal, and spatial)!";return{c(){s=o("p"),s.textContent=f},l(p){s=r(p,"P",{"data-svelte-h":!0}),c(s)!=="svelte-1katfuo"&&(s.textContent=f)},m(p,B){n(p,s,B)},p:de,d(p){p&&a(s)}}}function Je(N){let s,f,p,B,d,Y,h,O="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.",x,u,ee="This guide shows you how to use the caching methods supported in Diffusers.",R,b,F,w,te='<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.',I,m,Q,y,ae="PAB can be combined with other techniques like sequence parallelism and classifier-free guidance parallelism (data parallelism) for near real-time video generation.",E,M,ne='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.',V,g,P,J,A,T,se='<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.',S,_,le="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.",H,C,ie='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.',q,$,z,Z,D,G,L;return d=new K({props:{title:"Caching",local:"caching",headingTag:"h1"}}),b=new K({props:{title:"Pyramid Attention Broadcast",local:"pyramid-attention-broadcast",headingTag:"h2"}}),m=new ye({props:{warning:!1,$$slots:{default:[ge]},$$scope:{ctx:N}}}),g=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}}),J=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}}),Z=new Me({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/en/optimization/cache.md"}}),{c(){s=o("meta"),f=l(),p=o("p"),B=l(),j(d.$$.fragment),Y=l(),h=o("p"),h.textContent=O,x=l(),u=o("p"),u.textContent=ee,R=l(),j(b.$$.fragment),F=l(),w=o("p"),w.innerHTML=te,I=l(),j(m.$$.fragment),Q=l(),y=o("p"),y.textContent=ae,E=l(),M=o("p"),M.innerHTML=ne,V=l(),j(g.$$.fragment),P=l(),j(J.$$.fragment),A=l(),T=o("p"),T.innerHTML=se,S=l(),_=o("p"),_.textContent=le,H=l(),C=o("p"),C.innerHTML=ie,q=l(),j($.$$.fragment),z=l(),j(Z.$$.fragment),D=l(),G=o("p"),this.h()},l(e){const t=be("svelte-u9bgzb",document.head);s=r(t,"META",{name:!0,content:!0}),t.forEach(a),f=i(e),p=r(e,"P",{}),re(p).forEach(a),B=i(e),U(d.$$.fragment,e),Y=i(e),h=r(e,"P",{"data-svelte-h":!0}),c(h)!=="svelte-y872v2"&&(h.textContent=O),x=i(e),u=r(e,"P",{"data-svelte-h":!0}),c(u)!=="svelte-67viyl"&&(u.textContent=ee),R=i(e),U(b.$$.fragment,e),F=i(e),w=r(e,"P",{"data-svelte-h":!0}),c(w)!=="svelte-175y28v"&&(w.innerHTML=te),I=i(e),U(m.$$.fragment,e),Q=i(e),y=r(e,"P",{"data-svelte-h":!0}),c(y)!=="svelte-rlr2qi"&&(y.textContent=ae),E=i(e),M=r(e,"P",{"data-svelte-h":!0}),c(M)!=="svelte-1c6hyt4"&&(M.innerHTML=ne),V=i(e),U(g.$$.fragment,e),P=i(e),U(J.$$.fragment,e),A=i(e),T=r(e,"P",{"data-svelte-h":!0}),c(T)!=="svelte-1hv75yq"&&(T.innerHTML=se),S=i(e),_=r(e,"P",{"data-svelte-h":!0}),c(_)!=="svelte-1ekw8kg"&&(_.textContent=le),H=i(e),C=r(e,"P",{"data-svelte-h":!0}),c(C)!=="svelte-11scqgp"&&(C.innerHTML=ie),q=i(e),U($.$$.fragment,e),z=i(e),U(Z.$$.fragment,e),D=i(e),G=r(e,"P",{}),re(G).forEach(a),this.h()},h(){pe(s,"name","hf:doc:metadata"),pe(s,"content",Te)},m(e,t){we(document.head,s),n(e,f,t),n(e,p,t),n(e,B,t),k(d,e,t),n(e,Y,t),n(e,h,t),n(e,x,t),n(e,u,t),n(e,R,t),k(b,e,t),n(e,F,t),n(e,w,t),n(e,I,t),k(m,e,t),n(e,Q,t),n(e,y,t),n(e,E,t),n(e,M,t),n(e,V,t),k(g,e,t),n(e,P,t),k(J,e,t),n(e,A,t),n(e,T,t),n(e,S,t),n(e,_,t),n(e,H,t),n(e,C,t),n(e,q,t),k($,e,t),n(e,z,t),k(Z,e,t),n(e,D,t),n(e,G,t),L=!0},p(e,[t]){const oe={};t&2&&(oe.$$scope={dirty:t,ctx:e}),m.$set(oe)},i(e){L||(X(d.$$.fragment,e),X(b.$$.fragment,e),X(m.$$.fragment,e),X(g.$$.fragment,e),X(J.$$.fragment,e),X($.$$.fragment,e),X(Z.$$.fragment,e),L=!0)},o(e){W(d.$$.fragment,e),W(b.$$.fragment,e),W(m.$$.fragment,e),W(g.$$.fragment,e),W(J.$$.fragment,e),W($.$$.fragment,e),W(Z.$$.fragment,e),L=!1},d(e){e&&(a(f),a(p),a(B),a(Y),a(h),a(x),a(u),a(R),a(F),a(w),a(I),a(Q),a(y),a(E),a(M),a(V),a(P),a(A),a(T),a(S),a(_),a(H),a(C),a(q),a(z),a(D),a(G)),a(s),v(d,e),v(b,e),v(m,e),v(g,e),v(J,e),v($,e),v(Z,e)}}}const Te='{"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 _e(N){return fe(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class ke extends he{constructor(s){super(),ue(this,s,_e,Je,me,{})}}export{ke as component};
