Buckets:

HuggingFaceDocBuilder's picture
download
raw
48.8 kB
import{s as Ya,n as Fa,o as Sa}from"../chunks/scheduler.31fdf58d.js";import{S as qa,i as Ka,e as p,s as n,c as M,q as d,H as ps,h as Pa,a as m,d as t,b as l,f as rs,g as w,j as i,r as g,u as ms,k as oa,l as c,m as e,n as f,t as b,o as x,p as k}from"../chunks/index.2f76fdf0.js";import{C as Da}from"../chunks/CopyLLMTxtMenu.978df916.js";import{C as ha}from"../chunks/CodeBlock.e52df5d6.js";import{H as sa,E as Oa}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.13ad0e45.js";function st(da){let y,os,is,hs,T,ds,j,gs,J,ga="Imagine you’re having a conversation with someone, and instead of remembering what they previously said, they have to start from scratch every time you respond. This would be slow and inefficient, right?",us,C,ua="You can extend this analogy to transformer models. Autoregressive model generation can be slow because it makes a prediction one token at a time. Each new prediction is dependent on all the previous context.",ys,z,ya="To predict the 1000th token, the model requires information from the previous 999 tokens. The information is represented as matrix multiplications across the token representations.",vs,U,va="To predict the 1001th token, you need the same information from the previous 999 tokens in addition to any information from the 1000th token. This is a lot of matrix multiplications a model has to compute over and over for each token!",Ms,V,Ma="A key-value (KV) cache eliminates this inefficiency by storing kv pairs derived from the attention layers of previously processed tokens. The stored kv pairs are retrieved from the cache and reused for subsequent tokens, avoiding the need to recompute.",ws,_,wa="<p>Caching should only be used for <strong>inference</strong>. It may cause unexpected errors if it’s enabled during training.</p>",fs,Z,fa="To better understand how and why caching works, let’s take a closer look at the structure of the attention matrices.",bs,B,xs,r,aa,O,ba="scaled dot-product attention",ta,ss,xa="b",ea,as,ka="h",na,ts,_a="T",la,es,Ta="d_head",pa,ks,Ia=`<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>Attention</mtext><mo stretchy="false">(</mo><mi>Q</mi><mo separator="true">,</mo><mi>K</mi><mo separator="true">,</mo><mi>V</mi><mo stretchy="false">)</mo><mo>=</mo><mtext>softmax</mtext><mrow><mo fence="true">(</mo><mfrac><mrow><mi>Q</mi><msup><mi>K</mi><mi mathvariant="normal">⊤</mi></msup></mrow><msqrt><msub><mi>d</mi><mtext>head</mtext></msub></msqrt></mfrac><mo>×</mo><mtext>mask</mtext><mo fence="true">)</mo></mrow><mi>V</mi></mrow><annotation encoding="application/x-tex">
\\text{Attention}(Q, K, V) = \\text{softmax}\\left( \\frac{Q K^\\top}{\\sqrt{d_{\\text{head}}}} \\times \\text{mask} \\right) V
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord text"><span class="mord">Attention</span></span><span class="mopen">(</span><span class="mord mathnormal">Q</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal" style="margin-right:0.07153em;">K</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal" style="margin-right:0.22222em;">V</span><span class="mclose">)</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:2.4761em;vertical-align:-0.95em;"></span><span class="mord text"><span class="mord">softmax</span></span><span class="mspace" style="margin-right:0.1667em;"></span><span class="minner"><span class="mopen delimcenter" style="top:0em;"><span class="delimsizing size3">(</span></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.5261em;"><span style="top:-2.2528em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord sqrt"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.8572em;"><span class="svg-align" style="top:-3em;"><span class="pstrut" style="height:3em;"></span><span class="mord" style="padding-left:0.833em;"><span class="mord"><span class="mord mathnormal">d</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">head</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span><span style="top:-2.8172em;"><span class="pstrut" style="height:3em;"></span><span class="hide-tail" style="min-width:0.853em;height:1.08em;"><svg xmlns="http://www.w3.org/2000/svg" width="400em" height="1.08em" viewBox="0 0 400000 1080" preserveAspectRatio="xMinYMin slice"><path d="M95,702
c-2.7,0,-7.17,-2.7,-13.5,-8c-5.8,-5.3,-9.5,-10,-9.5,-14
c0,-2,0.3,-3.3,1,-4c1.3,-2.7,23.83,-20.7,67.5,-54
c44.2,-33.3,65.8,-50.3,66.5,-51c1.3,-1.3,3,-2,5,-2c4.7,0,8.7,3.3,12,10
s173,378,173,378c0.7,0,35.3,-71,104,-213c68.7,-142,137.5,-285,206.5,-429
c69,-144,104.5,-217.7,106.5,-221
l0 -0
c5.3,-9.3,12,-14,20,-14
H400000v40H845.2724
s-225.272,467,-225.272,467s-235,486,-235,486c-2.7,4.7,-9,7,-19,7
c-6,0,-10,-1,-12,-3s-194,-422,-194,-422s-65,47,-65,47z
M834 80h400000v40h-400000z"/></svg></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.1828em;"><span></span></span></span></span></span></span></span><span style="top:-3.23em;"><span class="pstrut" style="height:3em;"></span><span class="frac-line" style="border-bottom-width:0.04em;"></span></span><span style="top:-3.677em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord mathnormal">Q</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.07153em;">K</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.8491em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">⊤</span></span></span></span></span></span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.93em;"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mord text"><span class="mord">mask</span></span><span class="mclose delimcenter" style="top:0em;"><span class="delimsizing size3">)</span></span></span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal" style="margin-right:0.22222em;">V</span></span></span></span></span>`,_s,W,ja="The query (<code>Q</code>), key (<code>K</code>), and value (<code>V</code>) matrices are projections from the input embeddings of shape <code>(b, h, T, d_head)</code>.",Ts,u,ma,js,Aa='<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>K</mi><mtext>past</mtext></msub></mrow><annotation encoding="application/x-tex"> K_{\\text{past}} </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.9694em;vertical-align:-0.2861em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.07153em;">K</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.2806em;"><span style="top:-2.55em;margin-left:-0.0715em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">past</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span></span></span></span>',Js,Cs,Na='<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>V</mi><mtext>past</mtext></msub></mrow><annotation encoding="application/x-tex"> V_{\\text{past}} </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.9694em;vertical-align:-0.2861em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.22222em;">V</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.2806em;"><span style="top:-2.55em;margin-left:-0.2222em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">past</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span></span></span></span>',zs,Us,Qa=`<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mtext>Attention</mtext><mo stretchy="false">(</mo><msub><mi>q</mi><mi>t</mi></msub><mo separator="true">,</mo><mo stretchy="false">[</mo><munder><munder><mrow><msub><mi>k</mi><mn>1</mn></msub><mo separator="true">,</mo><msub><mi>k</mi><mn>2</mn></msub><mo separator="true">,</mo><mo>…</mo><mo separator="true">,</mo><msub><mi>k</mi><mrow><mi>t</mi><mo>−</mo><mn>1</mn></mrow></msub></mrow><mo stretchy="true">⏟</mo></munder><mtext>cached</mtext></munder><mo separator="true">,</mo><msub><mi>k</mi><mi>t</mi></msub><mo stretchy="false">]</mo><mo separator="true">,</mo><mo stretchy="false">[</mo><munder><munder><mrow><msub><mi>v</mi><mn>1</mn></msub><mo separator="true">,</mo><msub><mi>v</mi><mn>2</mn></msub><mo separator="true">,</mo><mo>…</mo><mo separator="true">,</mo><msub><mi>v</mi><mrow><mi>t</mi><mo>−</mo><mn>1</mn></mrow></msub></mrow><mo stretchy="true">⏟</mo></munder><mtext>cached</mtext></munder><mo separator="true">,</mo><msub><mi>v</mi><mi>t</mi></msub><mo stretchy="false">]</mo><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">
\\text{Attention}(q_t, [\\underbrace{k_1, k_2, \\dots, k_{t-1}}_{\\text{cached}}, k_{t}], [\\underbrace{v_1, v_2, \\dots, v_{t-1}}_{\\text{cached}}, v_{t}])
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:2.2924em;vertical-align:-1.5424em;"></span><span class="mord text"><span class="mord">Attention</span></span><span class="mopen">(</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.03588em;">q</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.2806em;"><span style="top:-2.55em;margin-left:-0.0359em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">t</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mopen">[</span><span class="mord munder"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.6944em;"><span style="top:-1.4576em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">cached</span></span></span></span></span><span style="top:-3em;"><span class="pstrut" style="height:3em;"></span><span class="mord munder"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.6944em;"><span class="svg-align" style="top:-2.1437em;"><span class="pstrut" style="height:3em;"></span><span class="stretchy" style="height:0.548em;min-width:1.6em;"><span class="brace-left" style="height:0.548em;"><svg xmlns="http://www.w3.org/2000/svg" width="400em" height="0.548em" viewBox="0 0 400000 548" preserveAspectRatio="xMinYMin slice"><path d="M0 6l6-6h17c12.688 0 19.313.3 20 1 4 4 7.313 8.3 10 13
35.313 51.3 80.813 93.8 136.5 127.5 55.688 33.7 117.188 55.8 184.5 66.5.688
0 2 .3 4 1 18.688 2.7 76 4.3 172 5h399450v120H429l-6-1c-124.688-8-235-61.7
-331-161C60.687 138.7 32.312 99.3 7 54L0 41V6z"/></svg></span><span class="brace-center" style="height:0.548em;"><svg xmlns="http://www.w3.org/2000/svg" width="400em" height="0.548em" viewBox="0 0 400000 548" preserveAspectRatio="xMidYMin slice"><path d="M199572 214
c100.7 8.3 195.3 44 280 108 55.3 42 101.7 93 139 153l9 14c2.7-4 5.7-8.7 9-14
53.3-86.7 123.7-153 211-199 66.7-36 137.3-56.3 212-62h199568v120H200432c-178.3
11.7-311.7 78.3-403 201-6 8-9.7 12-11 12-.7.7-6.7 1-18 1s-17.3-.3-18-1c-1.3 0
-5-4-11-12-44.7-59.3-101.3-106.3-170-141s-145.3-54.3-229-60H0V214z"/></svg></span><span class="brace-right" style="height:0.548em;"><svg xmlns="http://www.w3.org/2000/svg" width="400em" height="0.548em" viewBox="0 0 400000 548" preserveAspectRatio="xMaxYMin slice"><path d="M399994 0l6 6v35l-6 11c-56 104-135.3 181.3-238 232-57.3
28.7-117 45-179 50H-300V214h399897c43.3-7 81-15 113-26 100.7-33 179.7-91 237
-174 2.7-5 6-9 10-13 .7-1 7.3-1 20-1h17z"/></svg></span></span></span><span style="top:-3em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord"><span class="mord mathnormal" style="margin-right:0.03148em;">k</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3011em;"><span style="top:-2.55em;margin-left:-0.0315em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">1</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.03148em;">k</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3011em;"><span style="top:-2.55em;margin-left:-0.0315em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="minner">…</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.03148em;">k</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3011em;"><span style="top:-2.55em;margin-left:-0.0315em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">t</span><span class="mbin mtight">−</span><span class="mord mtight">1</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2083em;"><span></span></span></span></span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.8563em;"><span></span></span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:1.5424em;"><span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.03148em;">k</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.2806em;"><span style="top:-2.55em;margin-left:-0.0315em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">t</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">]</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mopen">[</span><span class="mord munder"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.4306em;"><span style="top:-1.4576em;"><span class="pstrut" style="height:3em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">cached</span></span></span></span></span><span style="top:-3em;"><span class="pstrut" style="height:3em;"></span><span class="mord munder"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.4306em;"><span class="svg-align" style="top:-2.1437em;"><span class="pstrut" style="height:3em;"></span><span class="stretchy" style="height:0.548em;min-width:1.6em;"><span class="brace-left" style="height:0.548em;"><svg xmlns="http://www.w3.org/2000/svg" width="400em" height="0.548em" viewBox="0 0 400000 548" preserveAspectRatio="xMinYMin slice"><path d="M0 6l6-6h17c12.688 0 19.313.3 20 1 4 4 7.313 8.3 10 13
35.313 51.3 80.813 93.8 136.5 127.5 55.688 33.7 117.188 55.8 184.5 66.5.688
0 2 .3 4 1 18.688 2.7 76 4.3 172 5h399450v120H429l-6-1c-124.688-8-235-61.7
-331-161C60.687 138.7 32.312 99.3 7 54L0 41V6z"/></svg></span><span class="brace-center" style="height:0.548em;"><svg xmlns="http://www.w3.org/2000/svg" width="400em" height="0.548em" viewBox="0 0 400000 548" preserveAspectRatio="xMidYMin slice"><path d="M199572 214
c100.7 8.3 195.3 44 280 108 55.3 42 101.7 93 139 153l9 14c2.7-4 5.7-8.7 9-14
53.3-86.7 123.7-153 211-199 66.7-36 137.3-56.3 212-62h199568v120H200432c-178.3
11.7-311.7 78.3-403 201-6 8-9.7 12-11 12-.7.7-6.7 1-18 1s-17.3-.3-18-1c-1.3 0
-5-4-11-12-44.7-59.3-101.3-106.3-170-141s-145.3-54.3-229-60H0V214z"/></svg></span><span class="brace-right" style="height:0.548em;"><svg xmlns="http://www.w3.org/2000/svg" width="400em" height="0.548em" viewBox="0 0 400000 548" preserveAspectRatio="xMaxYMin slice"><path d="M399994 0l6 6v35l-6 11c-56 104-135.3 181.3-238 232-57.3
28.7-117 45-179 50H-300V214h399897c43.3-7 81-15 113-26 100.7-33 179.7-91 237
-174 2.7-5 6-9 10-13 .7-1 7.3-1 20-1h17z"/></svg></span></span></span><span style="top:-3em;"><span class="pstrut" style="height:3em;"></span><span class="mord"><span class="mord"><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3011em;"><span style="top:-2.55em;margin-left:-0.0359em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">1</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3011em;"><span style="top:-2.55em;margin-left:-0.0359em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="minner">…</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3011em;"><span style="top:-2.55em;margin-left:-0.0359em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">t</span><span class="mbin mtight">−</span><span class="mord mtight">1</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2083em;"><span></span></span></span></span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.8563em;"><span></span></span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:1.5424em;"><span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.2806em;"><span style="top:-2.55em;margin-left:-0.0359em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">t</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">])</span></span></span></span></span>`,Vs,o,ia,Zs,La='<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>x</mi><mi>t</mi></msub></mrow><annotation encoding="application/x-tex"> x_t </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.5806em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal">x</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.2806em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">t</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span>',Bs,ns,Ja="stored",ca,ls,Ca="appended",ra,Ws,Ea=`<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msub><mi>K</mi><mtext>cache</mtext></msub><mo>←</mo><mtext>concat</mtext><mo stretchy="false">(</mo><msub><mi>K</mi><mtext>past</mtext></msub><mo separator="true">,</mo><msub><mi>k</mi><mi>t</mi></msub><mo stretchy="false">)</mo><mo separator="true">,</mo><mspace width="1em"/><msub><mi>V</mi><mtext>cache</mtext></msub><mo>←</mo><mtext>concat</mtext><mo stretchy="false">(</mo><msub><mi>V</mi><mtext>past</mtext></msub><mo separator="true">,</mo><msub><mi>v</mi><mi>t</mi></msub><mo stretchy="false">)</mo></mrow><annotation encoding="application/x-tex">
K_{\\text{cache}} \\leftarrow \\text{concat}(K_{\\text{past}}, k_t), \\quad V_{\\text{cache}} \\leftarrow \\text{concat}(V_{\\text{past}}, v_t)
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8333em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.07153em;">K</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.0715em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">cache</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">←</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.0361em;vertical-align:-0.2861em;"></span><span class="mord text"><span class="mord">concat</span></span><span class="mopen">(</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.07153em;">K</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.2806em;"><span style="top:-2.55em;margin-left:-0.0715em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">past</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.03148em;">k</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.2806em;"><span style="top:-2.55em;margin-left:-0.0315em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">t</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">)</span><span class="mpunct">,</span><span class="mspace" style="margin-right:1em;"></span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.22222em;">V</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.2222em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">cache</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">←</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.0361em;vertical-align:-0.2861em;"></span><span class="mord text"><span class="mord">concat</span></span><span class="mopen">(</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.22222em;">V</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.2806em;"><span style="top:-2.55em;margin-left:-0.2222em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord text mtight"><span class="mord mtight">past</span></span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.2806em;"><span style="top:-2.55em;margin-left:-0.0359em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">t</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mclose">)</span></span></span></span></span>`,Xs,X,za="Attention is calculated independently in each layer of the model, and caching is done on a per-layer basis.",Hs,H,Ua="Refer to the table below to compare how caching improves efficiency.",Rs,R,Va="<thead><tr><th>without caching</th> <th>with caching</th></tr></thead> <tbody><tr><td>for each step, recompute all previous <code>K</code> and <code>V</code></td> <td>for each step, only compute current <code>K</code> and <code>V</code></td></tr> <tr><td>attention cost per step is <strong>quadratic</strong> with sequence length</td> <td>attention cost per step is <strong>linear</strong> with sequence length (memory grows linearly, but compute/token remains low)</td></tr></tbody>",$s,$,Gs,G,Za="A basic KV cache interface takes a key and value tensor for the current token and returns the updated <code>K</code> and <code>V</code> tensors. This is internally managed by a model’s <code>forward</code> method.",Is,I,As,A,Ba='When you use Transformers’ <a href="/docs/transformers/pr_43924/en/internal/generation_utils#transformers.Cache">Cache</a> class, the self-attention module performs several critical steps to integrate past and present information.',Ns,N,Wa='<li><p>The attention module concatenates current kv pairs with past kv pairs stored in the cache. This creates attentions weights with the shape <code>(new_tokens_length, past_kv_length + new_tokens_length)</code>. The current and past kv pairs are essentially combined to compute the attention scores, ensuring a model is aware of previous context and the current input.</p></li> <li><p>When the <code>forward</code> method is called iteratively, it’s crucial that the attention mask shape matches the combined length of the past and current kv pairs. The attention mask should have the shape <code>(batch_size, past_kv_length + new_tokens_length)</code>. This is typically handled internally in <a href="/docs/transformers/pr_43924/en/main_classes/text_generation#transformers.GenerationMixin.generate">generate()</a>, but if you want to implement your own generation loop with <a href="/docs/transformers/pr_43924/en/internal/generation_utils#transformers.Cache">Cache</a>, keep this in mind! The attention mask should hold the past and current token values.</p></li>',Qs,Q,Ls,L,Xa="Caches are structured as a list of layers, where each layer contains a key and value cache. The key and value caches are tensors with the shape <code>[batch_size, num_heads, seq_len, head_dim]</code>.",Es,E,Ha="Layers can be of different types (e.g. <code>DynamicLayer</code>, <code>StaticLayer</code>, <code>StaticSlidingWindowLayer</code>), which mostly changes how sequence length is handled and how the cache is updated.",Ys,Y,Ra="The simplest is a <code>DynamicLayer</code> that grows as more tokens are processed. The sequence length dimension (<code>seq_len</code>) increases with each new token:",Fs,F,Ss,S,$a="Other layer types like <code>StaticLayer</code> and <code>StaticSlidingWindowLayer</code> have a fixed sequence length that is set when the cache is created. This makes them compatible with <code>torch.compile</code>. In the case of <code>StaticSlidingWindowLayer</code>, existing tokens are shifted out of the cache when a new token is added.",qs,q,Ga='The example below demonstrates how to create a generation loop with <a href="/docs/transformers/pr_43924/en/internal/generation_utils#transformers.DynamicCache">DynamicCache</a>. As discussed, the attention mask is a concatenation of past and current token values.',Ks,K,Ps,P,Ds,cs,Os;return T=new Da({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),j=new sa({props:{title:"Caching",local:"caching",headingTag:"h1"}}),B=new sa({props:{title:"Attention matrices",local:"attention-matrices",headingTag:"h2"}}),$=new sa({props:{title:"Cache class",local:"cache-class",headingTag:"h2"}}),I=new ha({props:{code:"bmV3X0slMkMlMjBuZXdfViUyMCUzRCUyMGNhY2hlLnVwZGF0ZShrX3QlMkMlMjB2X3QlMkMlMjBsYXllcl9pZHgpJTBBYXR0bl9vdXRwdXQlMjAlM0QlMjBhdHRuX2xheWVyX2lkeF9mbihxX3QlMkMlMjBuZXdfSyUyQyUyMG5ld19WKQ==",highlighted:`new_K, new_V = cache.update(k_t, v_t, layer_idx)
attn_output = attn_layer_idx_fn(q_t, new_K, new_V)`,lang:"py",wrap:!1}}),Q=new sa({props:{title:"Cache storage implementation",local:"cache-storage-implementation",headingTag:"h2"}}),F=new ha({props:{code:"Y2FjaGUubGF5ZXJzJTVCaWR4JTVELmtleXMlMjAlM0QlMjB0b3JjaC5jYXQoJTVCY2FjaGUubGF5ZXJzJTVCaWR4JTVELmtleXMlMkMlMjBrZXlfc3RhdGVzJTVEJTJDJTIwZGltJTNELTIpJTBBY2FjaGUubGF5ZXJzJTVCaWR4JTVELnZhbHVlcyUyMCUzRCUyMHRvcmNoLmNhdCglNUJjYWNoZS5sYXllcnMlNUJpZHglNUQudmFsdWVzJTJDJTIwdmFsdWVfc3RhdGVzJTVEJTJDJTIwZGltJTNELTIp",highlighted:`cache.layers[idx].keys = torch.cat([cache.layers[idx].keys, key_states], dim=-<span class="hljs-number">2</span>)
cache.layers[idx].values = torch.cat([cache.layers[idx].values, value_states], dim=-<span class="hljs-number">2</span>)`,lang:"py",wrap:!1}}),K=new ha({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwdHJhbnNmb3JtZXJzJTIwaW1wb3J0JTIwQXV0b1Rva2VuaXplciUyQyUyMEF1dG9Nb2RlbEZvckNhdXNhbExNJTJDJTIwRHluYW1pY0NhY2hlJTBBZnJvbSUyMGFjY2VsZXJhdGUlMjBpbXBvcnQlMjBBY2NlbGVyYXRvciUwQSUwQWRldmljZSUyMCUzRCUyMEFjY2VsZXJhdG9yKCkuZGV2aWNlJTBBJTBBbW9kZWxfaWQlMjAlM0QlMjAlMjJtZXRhLWxsYW1hJTJGTGxhbWEtMi03Yi1jaGF0LWhmJTIyJTBBbW9kZWwlMjAlM0QlMjBBdXRvTW9kZWxGb3JDYXVzYWxMTS5mcm9tX3ByZXRyYWluZWQobW9kZWxfaWQlMkMlMjBkdHlwZSUzRHRvcmNoLmJmbG9hdDE2JTJDJTIwZGV2aWNlX21hcCUzRGRldmljZSklMEF0b2tlbml6ZXIlMjAlM0QlMjBBdXRvVG9rZW5pemVyLmZyb21fcHJldHJhaW5lZChtb2RlbF9pZCklMEElMEFwYXN0X2tleV92YWx1ZXMlMjAlM0QlMjBEeW5hbWljQ2FjaGUoY29uZmlnJTNEbW9kZWwuY29uZmlnKSUwQW1lc3NhZ2VzJTIwJTNEJTIwJTVCJTdCJTIycm9sZSUyMiUzQSUyMCUyMnVzZXIlMjIlMkMlMjAlMjJjb250ZW50JTIyJTNBJTIwJTIySGVsbG8lMkMlMjB3aGF0J3MlMjB5b3VyJTIwbmFtZS4lMjIlN0QlNUQlMEFpbnB1dHMlMjAlM0QlMjB0b2tlbml6ZXIuYXBwbHlfY2hhdF90ZW1wbGF0ZShtZXNzYWdlcyUyQyUyMGFkZF9nZW5lcmF0aW9uX3Byb21wdCUzRFRydWUlMkMlMjByZXR1cm5fdGVuc29ycyUzRCUyMnB0JTIyJTJDJTIwcmV0dXJuX2RpY3QlM0RUcnVlKS50byhtb2RlbC5kZXZpY2UpJTBBJTBBZ2VuZXJhdGVkX2lkcyUyMCUzRCUyMGlucHV0cy5pbnB1dF9pZHMlMEFtYXhfbmV3X3Rva2VucyUyMCUzRCUyMDEwJTBBJTBBZm9yJTIwXyUyMGluJTIwcmFuZ2UobWF4X25ld190b2tlbnMpJTNBJTBBJTIwJTIwJTIwJTIwb3V0cHV0cyUyMCUzRCUyMG1vZGVsKCoqaW5wdXRzJTJDJTIwcGFzdF9rZXlfdmFsdWVzJTNEcGFzdF9rZXlfdmFsdWVzJTJDJTIwdXNlX2NhY2hlJTNEVHJ1ZSklMEElMjAlMjAlMjAlMjAlMjMlMjBHcmVlZGlseSUyMHNhbXBsZSUyMG9uZSUyMG5leHQlMjB0b2tlbiUwQSUyMCUyMCUyMCUyMG5leHRfdG9rZW5faWRzJTIwJTNEJTIwb3V0cHV0cy5sb2dpdHMlNUIlM0ElMkMlMjAtMSUzQSU1RC5hcmdtYXgoLTEpJTBBJTIwJTIwJTIwJTIwZ2VuZXJhdGVkX2lkcyUyMCUzRCUyMHRvcmNoLmNhdCglNUJnZW5lcmF0ZWRfaWRzJTJDJTIwbmV4dF90b2tlbl9pZHMlNUQlMkMlMjBkaW0lM0QtMSklMEElMjAlMjAlMjAlMjAlMjMlMjBQcmVwYXJlJTIwaW5wdXRzJTIwZm9yJTIwdGhlJTIwbmV4dCUyMGdlbmVyYXRpb24lMjBzdGVwJTIwYnklMjBsZWF2aW5nJTIwdW5wcm9jZXNzZWQlMjB0b2tlbnMlMkMlMjBpbiUyMG91ciUyMGNhc2UlMjB3ZSUyMGhhdmUlMjBvbmx5JTIwb25lJTIwbmV3JTIwdG9rZW4lMEElMjAlMjAlMjAlMjAlMjMlMjBhbmQlMjBleHBhbmRpbmclMjBhdHRuJTIwbWFzayUyMGZvciUyMHRoZSUyMG5ldyUyMHRva2VuJTJDJTIwYXMlMjBleHBsYWluZWQlMjBhYm92ZSUwQSUyMCUyMCUyMCUyMGF0dGVudGlvbl9tYXNrJTIwJTNEJTIwaW5wdXRzJTVCJTIyYXR0ZW50aW9uX21hc2slMjIlNUQlMEElMjAlMjAlMjAlMjBhdHRlbnRpb25fbWFzayUyMCUzRCUyMHRvcmNoLmNhdCglNUJhdHRlbnRpb25fbWFzayUyQyUyMGF0dGVudGlvbl9tYXNrLm5ld19vbmVzKChhdHRlbnRpb25fbWFzay5zaGFwZSU1QjAlNUQlMkMlMjAxKSklNUQlMkMlMjBkaW0lM0QtMSklMEElMjAlMjAlMjAlMjBpbnB1dHMlMjAlM0QlMjAlN0IlMjJpbnB1dF9pZHMlMjIlM0ElMjBuZXh0X3Rva2VuX2lkcyUyQyUyMCUyMmF0dGVudGlvbl9tYXNrJTIyJTNBJTIwYXR0ZW50aW9uX21hc2slN0QlMEElMEFwcmludCh0b2tlbml6ZXIuYmF0Y2hfZGVjb2RlKGdlbmVyYXRlZF9pZHMlMkMlMjBza2lwX3NwZWNpYWxfdG9rZW5zJTNEVHJ1ZSklNUIwJTVEKSUwQSUyMiU1QklOU1QlNUQlMjBIZWxsbyUyQyUyMHdoYXQncyUyMHlvdXIlMjBuYW1lLiUyMCU1QiUyRklOU1QlNUQlMjAlMjBIZWxsbyElMjBNeSUyMG5hbWUlMjBpcyUyMExMYU1BJTJDJTIy",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, AutoModelForCausalLM, DynamicCache
<span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> Accelerator
device = Accelerator().device
model_id = <span class="hljs-string">&quot;meta-llama/Llama-2-7b-chat-hf&quot;</span>
model = AutoModelForCausalLM.from_pretrained(model_id, dtype=torch.bfloat16, device_map=device)
tokenizer = AutoTokenizer.from_pretrained(model_id)
past_key_values = DynamicCache(config=model.config)
messages = [{<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;user&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: <span class="hljs-string">&quot;Hello, what&#x27;s your name.&quot;</span>}]
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=<span class="hljs-literal">True</span>, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>, return_dict=<span class="hljs-literal">True</span>).to(model.device)
generated_ids = inputs.input_ids
max_new_tokens = <span class="hljs-number">10</span>
<span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(max_new_tokens):
outputs = model(**inputs, past_key_values=past_key_values, use_cache=<span class="hljs-literal">True</span>)
<span class="hljs-comment"># Greedily sample one next token</span>
next_token_ids = outputs.logits[:, -<span class="hljs-number">1</span>:].argmax(-<span class="hljs-number">1</span>)
generated_ids = torch.cat([generated_ids, next_token_ids], dim=-<span class="hljs-number">1</span>)
<span class="hljs-comment"># Prepare inputs for the next generation step by leaving unprocessed tokens, in our case we have only one new token</span>
<span class="hljs-comment"># and expanding attn mask for the new token, as explained above</span>
attention_mask = inputs[<span class="hljs-string">&quot;attention_mask&quot;</span>]
attention_mask = torch.cat([attention_mask, attention_mask.new_ones((attention_mask.shape[<span class="hljs-number">0</span>], <span class="hljs-number">1</span>))], dim=-<span class="hljs-number">1</span>)
inputs = {<span class="hljs-string">&quot;input_ids&quot;</span>: next_token_ids, <span class="hljs-string">&quot;attention_mask&quot;</span>: attention_mask}
<span class="hljs-built_in">print</span>(tokenizer.batch_decode(generated_ids, skip_special_tokens=<span class="hljs-literal">True</span>)[<span class="hljs-number">0</span>])
<span class="hljs-string">&quot;[INST] Hello, what&#x27;s your name. [/INST] Hello! My name is LLaMA,&quot;</span>`,lang:"py",wrap:!1}}),P=new Oa({props:{source:"https://github.com/huggingface/transformers/blob/main/docs/source/en/cache_explanation.md"}}),{c(){y=p("meta"),os=n(),is=p("p"),hs=n(),M(T.$$.fragment),ds=n(),M(j.$$.fragment),gs=n(),J=p("p"),J.textContent=ga,us=n(),C=p("p"),C.textContent=ua,ys=n(),z=p("p"),z.textContent=ya,vs=n(),U=p("p"),U.textContent=va,Ms=n(),V=p("p"),V.textContent=Ma,ws=n(),_=p("blockquote"),_.innerHTML=wa,fs=n(),Z=p("p"),Z.textContent=fa,bs=n(),M(B.$$.fragment),xs=n(),r=p("p"),aa=d("The "),O=p("strong"),O.textContent=ba,ta=d(" is calculated as shown below for a batch of size "),ss=p("code"),ss.textContent=xa,ea=d(", number of attention heads "),as=p("code"),as.textContent=ka,na=d(", sequence length so far "),ts=p("code"),ts.textContent=_a,la=d(", and dimension per attention head "),es=p("code"),es.textContent=Ta,pa=d(`.
`),ks=new ps(!1),_s=n(),W=p("p"),W.innerHTML=ja,Ts=n(),u=p("p"),ma=d("For causal attention, the mask prevents the model from attending to future tokens. Once a token is processed, its representation never changes with respect to future tokens, which means "),js=new ps(!1),Js=d(" and "),Cs=new ps(!1),zs=d(` can be cached and reused to compute the last token’s representation.
`),Us=new ps(!1),Vs=n(),o=p("p"),ia=d("At inference time, you only need the last token’s query to compute the representation "),Zs=new ps(!1),Bs=d(" that predicts the next token $ t+1 $. At each step, the new key and value vectors are "),ns=p("strong"),ns.textContent=Ja,ca=d(" in the cache and "),ls=p("strong"),ls.textContent=Ca,ra=d(` to the past keys and values.
`),Ws=new ps(!1),Xs=n(),X=p("p"),X.textContent=za,Hs=n(),H=p("p"),H.textContent=Ua,Rs=n(),R=p("table"),R.innerHTML=Va,$s=n(),M($.$$.fragment),Gs=n(),G=p("p"),G.innerHTML=Za,Is=n(),M(I.$$.fragment),As=n(),A=p("p"),A.innerHTML=Ba,Ns=n(),N=p("ol"),N.innerHTML=Wa,Qs=n(),M(Q.$$.fragment),Ls=n(),L=p("p"),L.innerHTML=Xa,Es=n(),E=p("p"),E.innerHTML=Ha,Ys=n(),Y=p("p"),Y.innerHTML=Ra,Fs=n(),M(F.$$.fragment),Ss=n(),S=p("p"),S.innerHTML=$a,qs=n(),q=p("p"),q.innerHTML=Ga,Ks=n(),M(K.$$.fragment),Ps=n(),M(P.$$.fragment),Ds=n(),cs=p("p"),this.h()},l(s){const a=Pa("svelte-u9bgzb",document.head);y=m(a,"META",{name:!0,content:!0}),a.forEach(t),os=l(s),is=m(s,"P",{}),rs(is).forEach(t),hs=l(s),w(T.$$.fragment,s),ds=l(s),w(j.$$.fragment,s),gs=l(s),J=m(s,"P",{"data-svelte-h":!0}),i(J)!=="svelte-1vgfrh0"&&(J.textContent=ga),us=l(s),C=m(s,"P",{"data-svelte-h":!0}),i(C)!=="svelte-110unin"&&(C.textContent=ua),ys=l(s),z=m(s,"P",{"data-svelte-h":!0}),i(z)!=="svelte-1sc0ng3"&&(z.textContent=ya),vs=l(s),U=m(s,"P",{"data-svelte-h":!0}),i(U)!=="svelte-stwnf4"&&(U.textContent=va),Ms=l(s),V=m(s,"P",{"data-svelte-h":!0}),i(V)!=="svelte-k1lwaa"&&(V.textContent=Ma),ws=l(s),_=m(s,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),i(_)!=="svelte-1f3w2hm"&&(_.innerHTML=wa),fs=l(s),Z=m(s,"P",{"data-svelte-h":!0}),i(Z)!=="svelte-aj1f1y"&&(Z.textContent=fa),bs=l(s),w(B.$$.fragment,s),xs=l(s),r=m(s,"P",{});var h=rs(r);aa=g(h,"The "),O=m(h,"STRONG",{"data-svelte-h":!0}),i(O)!=="svelte-1et3y0d"&&(O.textContent=ba),ta=g(h," is calculated as shown below for a batch of size "),ss=m(h,"CODE",{"data-svelte-h":!0}),i(ss)!=="svelte-1y90nls"&&(ss.textContent=xa),ea=g(h,", number of attention heads "),as=m(h,"CODE",{"data-svelte-h":!0}),i(as)!=="svelte-1blmdqe"&&(as.textContent=ka),na=g(h,", sequence length so far "),ts=m(h,"CODE",{"data-svelte-h":!0}),i(ts)!=="svelte-18tc35m"&&(ts.textContent=_a),la=g(h,", and dimension per attention head "),es=m(h,"CODE",{"data-svelte-h":!0}),i(es)!=="svelte-krx0ij"&&(es.textContent=Ta),pa=g(h,`.
`),ks=ms(h,!1),h.forEach(t),_s=l(s),W=m(s,"P",{"data-svelte-h":!0}),i(W)!=="svelte-11ldl4s"&&(W.innerHTML=ja),Ts=l(s),u=m(s,"P",{});var D=rs(u);ma=g(D,"For causal attention, the mask prevents the model from attending to future tokens. Once a token is processed, its representation never changes with respect to future tokens, which means "),js=ms(D,!1),Js=g(D," and "),Cs=ms(D,!1),zs=g(D,` can be cached and reused to compute the last token’s representation.
`),Us=ms(D,!1),D.forEach(t),Vs=l(s),o=m(s,"P",{});var v=rs(o);ia=g(v,"At inference time, you only need the last token’s query to compute the representation "),Zs=ms(v,!1),Bs=g(v," that predicts the next token $ t+1 $. At each step, the new key and value vectors are "),ns=m(v,"STRONG",{"data-svelte-h":!0}),i(ns)!=="svelte-ho7h3"&&(ns.textContent=Ja),ca=g(v," in the cache and "),ls=m(v,"STRONG",{"data-svelte-h":!0}),i(ls)!=="svelte-ygjkh1"&&(ls.textContent=Ca),ra=g(v,` to the past keys and values.
`),Ws=ms(v,!1),v.forEach(t),Xs=l(s),X=m(s,"P",{"data-svelte-h":!0}),i(X)!=="svelte-fo5dj4"&&(X.textContent=za),Hs=l(s),H=m(s,"P",{"data-svelte-h":!0}),i(H)!=="svelte-ebwicu"&&(H.textContent=Ua),Rs=l(s),R=m(s,"TABLE",{"data-svelte-h":!0}),i(R)!=="svelte-1s32lyj"&&(R.innerHTML=Va),$s=l(s),w($.$$.fragment,s),Gs=l(s),G=m(s,"P",{"data-svelte-h":!0}),i(G)!=="svelte-ranj33"&&(G.innerHTML=Za),Is=l(s),w(I.$$.fragment,s),As=l(s),A=m(s,"P",{"data-svelte-h":!0}),i(A)!=="svelte-d1qaan"&&(A.innerHTML=Ba),Ns=l(s),N=m(s,"OL",{"data-svelte-h":!0}),i(N)!=="svelte-3wkb9m"&&(N.innerHTML=Wa),Qs=l(s),w(Q.$$.fragment,s),Ls=l(s),L=m(s,"P",{"data-svelte-h":!0}),i(L)!=="svelte-1su092y"&&(L.innerHTML=Xa),Es=l(s),E=m(s,"P",{"data-svelte-h":!0}),i(E)!=="svelte-159gg5y"&&(E.innerHTML=Ha),Ys=l(s),Y=m(s,"P",{"data-svelte-h":!0}),i(Y)!=="svelte-1nihuku"&&(Y.innerHTML=Ra),Fs=l(s),w(F.$$.fragment,s),Ss=l(s),S=m(s,"P",{"data-svelte-h":!0}),i(S)!=="svelte-1sgb5p0"&&(S.innerHTML=$a),qs=l(s),q=m(s,"P",{"data-svelte-h":!0}),i(q)!=="svelte-1pbbc0g"&&(q.innerHTML=Ga),Ks=l(s),w(K.$$.fragment,s),Ps=l(s),w(P.$$.fragment,s),Ds=l(s),cs=m(s,"P",{}),rs(cs).forEach(t),this.h()},h(){oa(y,"name","hf:doc:metadata"),oa(y,"content",at),oa(_,"class","warning"),ks.a=null,js.a=Js,Cs.a=zs,Us.a=null,Zs.a=Bs,Ws.a=null},m(s,a){c(document.head,y),e(s,os,a),e(s,is,a),e(s,hs,a),f(T,s,a),e(s,ds,a),f(j,s,a),e(s,gs,a),e(s,J,a),e(s,us,a),e(s,C,a),e(s,ys,a),e(s,z,a),e(s,vs,a),e(s,U,a),e(s,Ms,a),e(s,V,a),e(s,ws,a),e(s,_,a),e(s,fs,a),e(s,Z,a),e(s,bs,a),f(B,s,a),e(s,xs,a),e(s,r,a),c(r,aa),c(r,O),c(r,ta),c(r,ss),c(r,ea),c(r,as),c(r,na),c(r,ts),c(r,la),c(r,es),c(r,pa),ks.m(Ia,r),e(s,_s,a),e(s,W,a),e(s,Ts,a),e(s,u,a),c(u,ma),js.m(Aa,u),c(u,Js),Cs.m(Na,u),c(u,zs),Us.m(Qa,u),e(s,Vs,a),e(s,o,a),c(o,ia),Zs.m(La,o),c(o,Bs),c(o,ns),c(o,ca),c(o,ls),c(o,ra),Ws.m(Ea,o),e(s,Xs,a),e(s,X,a),e(s,Hs,a),e(s,H,a),e(s,Rs,a),e(s,R,a),e(s,$s,a),f($,s,a),e(s,Gs,a),e(s,G,a),e(s,Is,a),f(I,s,a),e(s,As,a),e(s,A,a),e(s,Ns,a),e(s,N,a),e(s,Qs,a),f(Q,s,a),e(s,Ls,a),e(s,L,a),e(s,Es,a),e(s,E,a),e(s,Ys,a),e(s,Y,a),e(s,Fs,a),f(F,s,a),e(s,Ss,a),e(s,S,a),e(s,qs,a),e(s,q,a),e(s,Ks,a),f(K,s,a),e(s,Ps,a),f(P,s,a),e(s,Ds,a),e(s,cs,a),Os=!0},p:Fa,i(s){Os||(b(T.$$.fragment,s),b(j.$$.fragment,s),b(B.$$.fragment,s),b($.$$.fragment,s),b(I.$$.fragment,s),b(Q.$$.fragment,s),b(F.$$.fragment,s),b(K.$$.fragment,s),b(P.$$.fragment,s),Os=!0)},o(s){x(T.$$.fragment,s),x(j.$$.fragment,s),x(B.$$.fragment,s),x($.$$.fragment,s),x(I.$$.fragment,s),x(Q.$$.fragment,s),x(F.$$.fragment,s),x(K.$$.fragment,s),x(P.$$.fragment,s),Os=!1},d(s){s&&(t(os),t(is),t(hs),t(ds),t(gs),t(J),t(us),t(C),t(ys),t(z),t(vs),t(U),t(Ms),t(V),t(ws),t(_),t(fs),t(Z),t(bs),t(xs),t(r),t(_s),t(W),t(Ts),t(u),t(Vs),t(o),t(Xs),t(X),t(Hs),t(H),t(Rs),t(R),t($s),t(Gs),t(G),t(Is),t(As),t(A),t(Ns),t(N),t(Qs),t(Ls),t(L),t(Es),t(E),t(Ys),t(Y),t(Fs),t(Ss),t(S),t(qs),t(q),t(Ks),t(Ps),t(Ds),t(cs)),t(y),k(T,s),k(j,s),k(B,s),k($,s),k(I,s),k(Q,s),k(F,s),k(K,s),k(P,s)}}}const at='{"title":"Caching","local":"caching","sections":[{"title":"Attention matrices","local":"attention-matrices","sections":[],"depth":2},{"title":"Cache class","local":"cache-class","sections":[],"depth":2},{"title":"Cache storage implementation","local":"cache-storage-implementation","sections":[],"depth":2}],"depth":1}';function tt(da){return Sa(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class it extends qa{constructor(y){super(),Ka(this,y,tt,st,Ya,{})}}export{it as component};

Xet Storage Details

Size:
48.8 kB
·
Xet hash:
bd5035bb465dd93e7d4821d44557e605bd4250e51f1411bccc0e6428c147e530

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