import{s as Ds,n as Fs,o as Vs}from"../chunks/scheduler.25b97de1.js";import{S as Js,i as Ns,g as h,s as i,r as C,m as o,H as c,A as Ws,h as g,f as n,c as m,j as Ms,u as j,x as T,n as r,B as d,k as ks,y as p,a as l,v as H,d as P,t as q,w as A}from"../chunks/index.d9030fc9.js";import{H as U,E as Xs}from"../chunks/EditOnGithub.91d95064.js";function Ys(zs){let u,G,S,K,w,O,v,Ls=`Most transformer models use full attention in the sense that the attention matrix is square. It can be a big
computational bottleneck when you have long texts. Longformer and reformer are models that try to be more efficient and
use a sparse version of the attention matrix to speed up training.`,Q,x,R,y,Es=`<a href="model_doc/reformer">Reformer</a> uses LSH attention. In the softmax(QK^t), only the biggest elements (in the softmax
dimension) of the matrix QK^t are going to give useful contributions. So for each query q in Q, we can consider only
the keys k in K that are close to q. A hash function is used to determine if q and k are close. The attention mask is
modified to mask the current token (except at the first position), because it will give a query and a key equal (so
very similar to each other). Since the hash can be a bit random, several hash functions are used in practice
(determined by a n_rounds parameter) and then are averaged together.`,B,b,D,_,Ts=`<a href="model_doc/longformer">Longformer</a> uses local attention: often, the local context (e.g., what are the two tokens to the
left and right?) is enough to take action for a given token. Also, by stacking attention layers that have a small
window, the last layer will have a receptive field of more than just the tokens in the window, allowing them to build a
representation of the whole sentence.`,F,k,Cs=`Some preselected input tokens are also given global attention: for those few tokens, the attention matrix can access
all tokens and this process is symmetric: all other tokens have access to those specific tokens (on top of the ones in
their local window). This is shown in Figure 2d of the paper, see below for a sample attention mask:`,V,f,js='<img scale="50 %" align="center" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/local_attention_mask.png"/>',J,$,Hs=`Using those attention matrices with less parameters then allows the model to have inputs having a bigger sequence
length.`,N,M,W,z,X,t,L,Ps="Reformer",$s,Y,qs='<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>l</mi></mrow><annotation encoding="application/x-tex">l</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span></span></span></span>',Z,ss,As='<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi></mrow><annotation encoding="application/x-tex">d</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">d</span></span></span></span>',as,ts,Ss='<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>l</mi></mrow><annotation encoding="application/x-tex">l</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span></span></span></span>',ns,es,Is='<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi></mrow><annotation encoding="application/x-tex">d</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">d</span></span></span></span>',ls,is,Us='<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>l</mi><mn>1</mn></msub><mo>×</mo><msub><mi>d</mi><mn>1</mn></msub></mrow><annotation encoding="application/x-tex">l_{1} \\times d_{1}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.01968em;">l</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.0197em;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 mtight">1</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.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.8444em;vertical-align:-0.15em;"></span><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.3011em;"><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 mtight">1</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>',ms,ps,Gs='<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>l</mi><mn>2</mn></msub><mo>×</mo><msub><mi>d</mi><mn>2</mn></msub></mrow><annotation encoding="application/x-tex">l_{2} \\times d_{2}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.01968em;">l</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.0197em;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 mtight">2</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.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.8444em;vertical-align:-0.15em;"></span><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.3011em;"><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 mtight">2</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>',os,rs,Ks='<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>l</mi><mn>1</mn></msub><mo>×</mo><msub><mi>l</mi><mn>2</mn></msub><mo>=</mo><mi>l</mi></mrow><annotation encoding="application/x-tex">l_{1} \\times l_{2} = l</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.01968em;">l</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.0197em;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 mtight">1</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.2222em;"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.8444em;vertical-align:-0.15em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.01968em;">l</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.0197em;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 mtight">2</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:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span></span></span></span>',hs,cs,Os='<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msub><mi>d</mi><mn>1</mn></msub><mo>+</mo><msub><mi>d</mi><mn>2</mn></msub><mo>=</mo><mi>d</mi></mrow><annotation encoding="application/x-tex">d_{1} + d_{2} = d</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8444em;vertical-align:-0.15em;"></span><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.3011em;"><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 mtight">1</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.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.8444em;vertical-align:-0.15em;"></span><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.3011em;"><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 mtight">2</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:0.6944em;"></span><span class="mord mathnormal">d</span></span></span></span>',gs,ds,Qs='<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>j</mi></mrow><annotation encoding="application/x-tex">j</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.854em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.05724em;">j</span></span></span></span>',us,fs,Rs='<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>j</mi><mi mathvariant="normal">%</mi><mi>l</mi><mn>1</mn></mrow><annotation encoding="application/x-tex">j \\% l1</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.9444em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.05724em;">j</span><span class="mord">%</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord">1</span></span></span></span>',ws,vs,Bs='<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>j</mi><mi mathvariant="normal">/</mi><mi mathvariant="normal">/</mi><mi>l</mi><mn>1</mn></mrow><annotation encoding="application/x-tex">j // l1</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 mathnormal" style="margin-right:0.05724em;">j</span><span class="mord">//</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord">1</span></span></span></span>',xs,ys,E,bs,I,_s;return w=new U({props:{title:"Attention mechanisms",local:"attention-mechanisms",headingTag:"h1"}}),x=new U({props:{title:"LSH attention",local:"lsh-attention",headingTag:"h2"}}),b=new U({props:{title:"Local attention",local:"local-attention",headingTag:"h2"}}),M=new U({props:{title:"Other tricks",local:"other-tricks",headingTag:"h2"}}),z=new U({props:{title:"Axial positional encodings",local:"axial-positional-encodings",headingTag:"h3"}}),E=new Xs({props:{source:"https://github.com/huggingface/transformers/blob/main/docs/source/en/attention.md"}}),{c(){u=h("meta"),G=i(),S=h("p"),K=i(),C(w.$$.fragment),O=i(),v=h("p"),v.textContent=Ls,Q=i(),C(x.$$.fragment),R=i(),y=h("p"),y.innerHTML=Es,B=i(),C(b.$$.fragment),D=i(),_=h("p"),_.innerHTML=Ts,F=i(),k=h("p"),k.textContent=Cs,V=i(),f=h("div"),f.innerHTML=js,J=i(),$=h("p"),$.textContent=Hs,N=i(),C(M.$$.fragment),W=i(),C(z.$$.fragment),X=i(),t=h("p"),L=h("a"),L.textContent=Ps,$s=o(` uses axial positional encodings: in traditional transformer models, the positional encoding
E is a matrix of size`),Y=new c(!1),Z=o(" by"),ss=new c(!1),as=o(","),ts=new c(!1),ns=o(" being the sequence length and"),es=new c(!1),ls=o(` the dimension of the
hidden state. If you have very long texts, this matrix can be huge and take way too much space on the GPU. To alleviate
that, axial positional encodings consist of factorizing that big matrix E in two smaller matrices E1 and E2, with
dimensions`),is=new c(!1),ms=o(" and"),ps=new c(!1),os=o(", such that"),rs=new c(!1),hs=o(" and"),cs=new c(!1),gs=o(` (with the product for the lengths, this ends up being way smaller). The embedding for time
step`),ds=new c(!1),us=o(" in E is obtained by concatenating the embeddings for timestep"),fs=new c(!1),ws=o(" in E1 and"),vs=new c(!1),xs=o(`
in E2.`),ys=i(),C(E.$$.fragment),bs=i(),I=h("p"),this.h()},l(s){const a=Ws("svelte-u9bgzb",document.head);u=g(a,"META",{name:!0,content:!0}),a.forEach(n),G=m(s),S=g(s,"P",{}),Ms(S).forEach(n),K=m(s),j(w.$$.fragment,s),O=m(s),v=g(s,"P",{"data-svelte-h":!0}),T(v)!=="svelte-1badtw"&&(v.textContent=Ls),Q=m(s),j(x.$$.fragment,s),R=m(s),y=g(s,"P",{"data-svelte-h":!0}),T(y)!=="svelte-1s477ej"&&(y.innerHTML=Es),B=m(s),j(b.$$.fragment,s),D=m(s),_=g(s,"P",{"data-svelte-h":!0}),T(_)!=="svelte-h79jys"&&(_.innerHTML=Ts),F=m(s),k=g(s,"P",{"data-svelte-h":!0}),T(k)!=="svelte-1x90fhu"&&(k.textContent=Cs),V=m(s),f=g(s,"DIV",{class:!0,"data-svelte-h":!0}),T(f)!=="svelte-d4kpls"&&(f.innerHTML=js),J=m(s),$=g(s,"P",{"data-svelte-h":!0}),T($)!=="svelte-11i8g0o"&&($.textContent=Hs),N=m(s),j(M.$$.fragment,s),W=m(s),j(z.$$.fragment,s),X=m(s),t=g(s,"P",{});var e=Ms(t);L=g(e,"A",{href:!0,"data-svelte-h":!0}),T(L)!=="svelte-zf8r8v"&&(L.textContent=Ps),$s=r(e,` uses axial positional encodings: in traditional transformer models, the positional encoding
E is a matrix of size`),Y=d(e,!1),Z=r(e," by"),ss=d(e,!1),as=r(e,","),ts=d(e,!1),ns=r(e," being the sequence length and"),es=d(e,!1),ls=r(e,` the dimension of the
hidden state. If you have very long texts, this matrix can be huge and take way too much space on the GPU. To alleviate
that, axial positional encodings consist of factorizing that big matrix E in two smaller matrices E1 and E2, with
dimensions`),is=d(e,!1),ms=r(e," and"),ps=d(e,!1),os=r(e,", such that"),rs=d(e,!1),hs=r(e," and"),cs=d(e,!1),gs=r(e,` (with the product for the lengths, this ends up being way smaller). The embedding for time
step`),ds=d(e,!1),us=r(e," in E is obtained by concatenating the embeddings for timestep"),fs=d(e,!1),ws=r(e," in E1 and"),vs=d(e,!1),xs=r(e,`
in E2.`),e.forEach(n),ys=m(s),j(E.$$.fragment,s),bs=m(s),I=g(s,"P",{}),Ms(I).forEach(n),this.h()},h(){ks(u,"name","hf:doc:metadata"),ks(u,"content",Zs),ks(f,"class","flex justify-center"),ks(L,"href","model_doc/reformer"),Y.a=Z,ss.a=as,ts.a=ns,es.a=ls,is.a=ms,ps.a=os,rs.a=hs,cs.a=gs,ds.a=us,fs.a=ws,vs.a=xs},m(s,a){p(document.head,u),l(s,G,a),l(s,S,a),l(s,K,a),H(w,s,a),l(s,O,a),l(s,v,a),l(s,Q,a),H(x,s,a),l(s,R,a),l(s,y,a),l(s,B,a),H(b,s,a),l(s,D,a),l(s,_,a),l(s,F,a),l(s,k,a),l(s,V,a),l(s,f,a),l(s,J,a),l(s,$,a),l(s,N,a),H(M,s,a),l(s,W,a),H(z,s,a),l(s,X,a),l(s,t,a),p(t,L),p(t,$s),Y.m(qs,t),p(t,Z),ss.m(As,t),p(t,as),ts.m(Ss,t),p(t,ns),es.m(Is,t),p(t,ls),is.m(Us,t),p(t,ms),ps.m(Gs,t),p(t,os),rs.m(Ks,t),p(t,hs),cs.m(Os,t),p(t,gs),ds.m(Qs,t),p(t,us),fs.m(Rs,t),p(t,ws),vs.m(Bs,t),p(t,xs),l(s,ys,a),H(E,s,a),l(s,bs,a),l(s,I,a),_s=!0},p:Fs,i(s){_s||(P(w.$$.fragment,s),P(x.$$.fragment,s),P(b.$$.fragment,s),P(M.$$.fragment,s),P(z.$$.fragment,s),P(E.$$.fragment,s),_s=!0)},o(s){q(w.$$.fragment,s),q(x.$$.fragment,s),q(b.$$.fragment,s),q(M.$$.fragment,s),q(z.$$.fragment,s),q(E.$$.fragment,s),_s=!1},d(s){s&&(n(G),n(S),n(K),n(O),n(v),n(Q),n(R),n(y),n(B),n(D),n(_),n(F),n(k),n(V),n(f),n(J),n($),n(N),n(W),n(X),n(t),n(ys),n(bs),n(I)),n(u),A(w,s),A(x,s),A(b,s),A(M,s),A(z,s),A(E,s)}}}const Zs='{"title":"Attention mechanisms","local":"attention-mechanisms","sections":[{"title":"LSH attention","local":"lsh-attention","sections":[],"depth":2},{"title":"Local attention","local":"local-attention","sections":[],"depth":2},{"title":"Other tricks","local":"other-tricks","sections":[{"title":"Axial positional encodings","local":"axial-positional-encodings","sections":[],"depth":3}],"depth":2}],"depth":1}';function sa(zs){return Vs(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class ea extends Js{constructor(u){super(),Ns(this,u,sa,Ys,Ds,{})}}export{ea as component};
