import{S as xr,i as Wr,s as Vr,e as s,k as u,w as d,t as n,M as Ar,c as l,d as t,m as c,a as o,x as f,h as r,b as h,f as As,N as Lr,G as a,g as p,y as m,q as g,o as y,B as v,v as Rr}from"../chunks/vendor-hf-doc-builder.js";import{T as Xr}from"../chunks/Tip-hf-doc-builder.js";import{I}from"../chunks/IconCopyLink-hf-doc-builder.js";import{C as je}from"../chunks/CodeBlock-hf-doc-builder.js";import{D as zr}from"../chunks/DocNotebookDropdown-hf-doc-builder.js";function Cr(ya){let b,D,w,k,V,U,$,A,Je;return{c(){b=s("p"),D=n("If you\u2019re interested in basic LLM usage, our high-level "),w=s("a"),k=s("code"),V=n("Pipeline"),U=n(" interface is a great starting point. However, LLMs often require advanced features like quantization and fine control of the token selection step, which is best done through "),$=s("a"),A=n("generate()"),Je=n(". Autoregressive generation with LLMs is also resource-intensive and should be executed on a GPU for adequate throughput."),this.h()},l(G){b=l(G,"P",{});var M=o(b);D=r(M,"If you\u2019re interested in basic LLM usage, our high-level "),w=l(M,"A",{href:!0});var Ee=o(w);k=l(Ee,"CODE",{});var R=o(k);V=r(R,"Pipeline"),R.forEach(t),Ee.forEach(t),U=r(M," interface is a great starting point. However, LLMs often require advanced features like quantization and fine control of the token selection step, which is best done through "),$=l(M,"A",{href:!0});var Ke=o($);A=r(Ke,"generate()"),Ke.forEach(t),Je=r(M,". Autoregressive generation with LLMs is also resource-intensive and should be executed on a GPU for adequate throughput."),M.forEach(t),this.h()},h(){h(w,"href","pipeline_tutorial"),h($,"href","/docs/transformers/v4.32.0/en/main_classes/text_generation#transformers.GenerationMixin.generate")},m(G,M){p(G,b,M),a(b,D),a(b,w),a(w,k),a(k,V),a(b,U),a(b,$),a($,A),a(b,Je)},d(G){G&&t(b)}}}function Br(ya){let b,D,w,k,V,U,$,A,Je,G,M,Ee,R,Ke,va,O,Rs,et,Xs,zs,ba,tt,Cs,wa,L,Lt,Bs,Ns,xt,Fs,qs,Wt,Ss,Ma,at,Qs,_a,Te,ka,X,K,Vt,Ue,Hs,At,Ys,ja,ee,Ps,st,Ds,Os,Ja,z,J,an,Ks,Rt,el,Ea,lt,tl,Ta,C,E,sn,al,Xt,sl,Ua,te,ll,zt,ol,nl,Za,ae,rl,ot,il,pl,Ia,nt,hl,$a,se,Ga,rt,ul,La,Ze,xa,le,cl,Ct,dl,fl,Wa,oe,it,Bt,ml,gl,yl,ne,Nt,vl,bl,pt,wl,Ml,Va,ht,_l,Aa,re,kl,ut,jl,Jl,Ra,Ie,Xa,x,El,Ft,Tl,Ul,ct,Zl,Il,za,ie,$l,dt,Gl,Ll,Ca,$e,Ba,ft,xl,Na,B,pe,qt,Ge,Wl,St,Vl,Fa,he,Al,mt,Rl,Xl,qa,Le,Sa,N,ue,Qt,xe,zl,Ht,Cl,Qa,_,Bl,gt,Nl,Fl,Yt,ql,Sl,Pt,Ql,Hl,Dt,Yl,Pl,We,Dl,Ol,Ha,Ve,Ya,F,ce,Ot,Ae,Kl,Kt,eo,Pa,j,to,yt,ao,so,ea,lo,oo,ta,no,ro,Re,io,po,Da,Xe,Oa,q,de,aa,ze,ho,sa,uo,Ka,fe,co,Ce,fo,mo,es,Be,ts,S,me,la,Ne,go,oa,yo,as,vt,vo,ss,Q,ge,na,Fe,bo,ra,wo,ls,ye,bt,wt,Mo,_o,ko,Z,jo,Mt,Jo,Eo,_t,To,Uo,kt,Zo,Io,os,H,ve,ia,qe,$o,pa,Go,ns,be,jt,Se,Lo,xo,Wo,Jt,Qe,Vo,Ao,rs,Y,we,ha,He,Ro,ua,Xo,is,Et,Tt,Ut,zo,Co,ps,P,Me,ca,Ye,Bo,da,No,hs,_e,Zt,Pe,fa,Fo,qo,So,It,De,ma,Qo,Ho,us;return U=new I({}),M=new zr({props:{classNames:"absolute z-10 right-0 top-0",options:[{label:"Mixed",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/llm_tutorial.ipynb"},{label:"PyTorch",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/pytorch/llm_tutorial.ipynb"},{label:"TensorFlow",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/main/transformers_doc/en/tensorflow/llm_tutorial.ipynb"},{label:"Mixed",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/llm_tutorial.ipynb"},{label:"PyTorch",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/pytorch/llm_tutorial.ipynb"},{label:"TensorFlow",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/main/transformers_doc/en/tensorflow/llm_tutorial.ipynb"}]}}),Te=new je({props:{code:"cGlwJTIwaW5zdGFsbCUyMHRyYW5zZm9ybWVycyUyMGJpdHNhbmRieXRlcyUzRSUzRDAuMzkuMCUyMC1x",highlighted:"pip install transformers bitsandbytes&gt;=0.39.0 -q"}}),Ue=new I({}),se=new Xr({props:{$$slots:{default:[Cr]},$$scope:{ctx:ya}}}),Ze=new je({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Nb2RlbEZvckNhdXNhbExNJTBBJTBBbW9kZWwlMjAlM0QlMjBBdXRvTW9kZWxGb3JDYXVzYWxMTS5mcm9tX3ByZXRyYWluZWQoJTBBJTIwJTIwJTIwJTIwJTIyb3BlbmxtLXJlc2VhcmNoJTJGb3Blbl9sbGFtYV83YiUyMiUyQyUyMGRldmljZV9tYXAlM0QlMjJhdXRvJTIyJTJDJTIwbG9hZF9pbl80Yml0JTNEVHJ1ZSUwQSk=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM

<span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForCausalLM.from_pretrained(
<span class="hljs-meta">... </span>    <span class="hljs-string">&quot;openlm-research/open_llama_7b&quot;</span>, device_map=<span class="hljs-string">&quot;auto&quot;</span>, load_in_4bit=<span class="hljs-literal">True</span>
<span class="hljs-meta">... </span>)`}}),Ie=new je({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Ub2tlbml6ZXIlMEElMEF0b2tlbml6ZXIlMjAlM0QlMjBBdXRvVG9rZW5pemVyLmZyb21fcHJldHJhaW5lZCglMjJvcGVubG0tcmVzZWFyY2glMkZvcGVuX2xsYW1hXzdiJTIyKSUwQW1vZGVsX2lucHV0cyUyMCUzRCUyMHRva2VuaXplciglNUIlMjJBJTIwbGlzdCUyMG9mJTIwY29sb3JzJTNBJTIwcmVkJTJDJTIwYmx1ZSUyMiU1RCUyQyUyMHJldHVybl90ZW5zb3JzJTNEJTIycHQlMjIpLnRvKCUyMmN1ZGElMjIp",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer

<span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">&quot;openlm-research/open_llama_7b&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>model_inputs = tokenizer([<span class="hljs-string">&quot;A list of colors: red, blue&quot;</span>], return_tensors=<span class="hljs-string">&quot;pt&quot;</span>).to(<span class="hljs-string">&quot;cuda&quot;</span>)`}}),$e=new je({props:{code:"Z2VuZXJhdGVkX2lkcyUyMCUzRCUyMG1vZGVsLmdlbmVyYXRlKCoqbW9kZWxfaW5wdXRzKSUwQXRva2VuaXplci5iYXRjaF9kZWNvZGUoZ2VuZXJhdGVkX2lkcyUyQyUyMHNraXBfc3BlY2lhbF90b2tlbnMlM0RUcnVlKSU1QjAlNUQ=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>generated_ids = model.generate(**model_inputs)
<span class="hljs-meta">&gt;&gt;&gt; </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">&#x27;A list of colors: red, blue, green, yellow, black, white, and brown&#x27;</span>`}}),Ge=new I({}),Le=new je({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Nb2RlbEZvckNhdXNhbExNJTJDJTIwQXV0b1Rva2VuaXplciUwQSUwQXRva2VuaXplciUyMCUzRCUyMEF1dG9Ub2tlbml6ZXIuZnJvbV9wcmV0cmFpbmVkKCUyMm9wZW5sbS1yZXNlYXJjaCUyRm9wZW5fbGxhbWFfN2IlMjIpJTBBdG9rZW5pemVyLnBhZF90b2tlbiUyMCUzRCUyMHRva2VuaXplci5lb3NfdG9rZW4lMjAlMjAlMjMlMjBMbGFtYSUyMGhhcyUyMG5vJTIwcGFkJTIwdG9rZW4lMjBieSUyMGRlZmF1bHQlMEFtb2RlbCUyMCUzRCUyMEF1dG9Nb2RlbEZvckNhdXNhbExNLmZyb21fcHJldHJhaW5lZCglMEElMjAlMjAlMjAlMjAlMjJvcGVubG0tcmVzZWFyY2glMkZvcGVuX2xsYW1hXzdiJTIyJTJDJTIwZGV2aWNlX21hcCUzRCUyMmF1dG8lMjIlMkMlMjBsb2FkX2luXzRiaXQlM0RUcnVlJTBBKQ==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM, AutoTokenizer

<span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">&quot;openlm-research/open_llama_7b&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.pad_token = tokenizer.eos_token  <span class="hljs-comment"># Llama has no pad token by default</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForCausalLM.from_pretrained(
<span class="hljs-meta">... </span>    <span class="hljs-string">&quot;openlm-research/open_llama_7b&quot;</span>, device_map=<span class="hljs-string">&quot;auto&quot;</span>, load_in_4bit=<span class="hljs-literal">True</span>
<span class="hljs-meta">... </span>)`}}),xe=new I({}),Ve=new je({props:{code:"bW9kZWxfaW5wdXRzJTIwJTNEJTIwdG9rZW5pemVyKCU1QiUyMkElMjBzZXF1ZW5jZSUyMG9mJTIwbnVtYmVycyUzQSUyMDElMkMlMjAyJTIyJTVEJTJDJTIwcmV0dXJuX3RlbnNvcnMlM0QlMjJwdCUyMikudG8oJTIyY3VkYSUyMiklMEElMEElMjMlMjBCeSUyMGRlZmF1bHQlMkMlMjB0aGUlMjBvdXRwdXQlMjB3aWxsJTIwY29udGFpbiUyMHVwJTIwdG8lMjAyMCUyMHRva2VucyUwQWdlbmVyYXRlZF9pZHMlMjAlM0QlMjBtb2RlbC5nZW5lcmF0ZSgqKm1vZGVsX2lucHV0cyklMEF0b2tlbml6ZXIuYmF0Y2hfZGVjb2RlKGdlbmVyYXRlZF9pZHMlMkMlMjBza2lwX3NwZWNpYWxfdG9rZW5zJTNEVHJ1ZSklNUIwJTVEJTBBJTBBJTIzJTIwU2V0dGluZyUyMCU2MG1heF9uZXdfdG9rZW5zJTYwJTIwYWxsb3dzJTIweW91JTIwdG8lMjBjb250cm9sJTIwdGhlJTIwbWF4aW11bSUyMGxlbmd0aCUwQWdlbmVyYXRlZF9pZHMlMjAlM0QlMjBtb2RlbC5nZW5lcmF0ZSgqKm1vZGVsX2lucHV0cyUyQyUyMG1heF9uZXdfdG9rZW5zJTNENTApJTBBdG9rZW5pemVyLmJhdGNoX2RlY29kZShnZW5lcmF0ZWRfaWRzJTJDJTIwc2tpcF9zcGVjaWFsX3Rva2VucyUzRFRydWUpJTVCMCU1RA==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>model_inputs = tokenizer([<span class="hljs-string">&quot;A sequence of numbers: 1, 2&quot;</span>], return_tensors=<span class="hljs-string">&quot;pt&quot;</span>).to(<span class="hljs-string">&quot;cuda&quot;</span>)

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># By default, the output will contain up to 20 tokens</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>generated_ids = model.generate(**model_inputs)
<span class="hljs-meta">&gt;&gt;&gt; </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">&#x27;A sequence of numbers: 1, 2, 3, 4, 5&#x27;</span>

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># Setting \`max_new_tokens\` allows you to control the maximum length</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>generated_ids = model.generate(**model_inputs, max_new_tokens=<span class="hljs-number">50</span>)
<span class="hljs-meta">&gt;&gt;&gt; </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">&#x27;A sequence of numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,&#x27;</span>`}}),Ae=new I({}),Xe=new je({props:{code:"JTIzJTIwU2V0JTIwc2VlZCUyMG9yJTIwcmVwcm9kdWNpYmlsaXR5JTIwLS0lMjB5b3UlMjBkb24ndCUyMG5lZWQlMjB0aGlzJTIwdW5sZXNzJTIweW91JTIwd2FudCUyMGZ1bGwlMjByZXByb2R1Y2liaWxpdHklMEFmcm9tJTIwdHJhbnNmb3JtZXJzJTIwaW1wb3J0JTIwc2V0X3NlZWQlMEFzZXRfc2VlZCgwKSUwQSUwQW1vZGVsX2lucHV0cyUyMCUzRCUyMHRva2VuaXplciglNUIlMjJJJTIwYW0lMjBhJTIwY2F0LiUyMiU1RCUyQyUyMHJldHVybl90ZW5zb3JzJTNEJTIycHQlMjIpLnRvKCUyMmN1ZGElMjIpJTBBJTBBJTIzJTIwTExNJTIwJTJCJTIwZ3JlZWR5JTIwZGVjb2RpbmclMjAlM0QlMjByZXBldGl0aXZlJTJDJTIwYm9yaW5nJTIwb3V0cHV0JTBBZ2VuZXJhdGVkX2lkcyUyMCUzRCUyMG1vZGVsLmdlbmVyYXRlKCoqbW9kZWxfaW5wdXRzKSUwQXRva2VuaXplci5iYXRjaF9kZWNvZGUoZ2VuZXJhdGVkX2lkcyUyQyUyMHNraXBfc3BlY2lhbF90b2tlbnMlM0RUcnVlKSU1QjAlNUQlMEElMEElMjMlMjBXaXRoJTIwc2FtcGxpbmclMkMlMjB0aGUlMjBvdXRwdXQlMjBiZWNvbWVzJTIwbW9yZSUyMGNyZWF0aXZlISUwQWdlbmVyYXRlZF9pZHMlMjAlM0QlMjBtb2RlbC5nZW5lcmF0ZSgqKm1vZGVsX2lucHV0cyUyQyUyMGRvX3NhbXBsZSUzRFRydWUpJTBBdG9rZW5pemVyLmJhdGNoX2RlY29kZShnZW5lcmF0ZWRfaWRzJTJDJTIwc2tpcF9zcGVjaWFsX3Rva2VucyUzRFRydWUpJTVCMCU1RA==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># Set seed or reproducibility -- you don&#x27;t need this unless you want full reproducibility</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> set_seed
<span class="hljs-meta">&gt;&gt;&gt; </span>set_seed(<span class="hljs-number">0</span>)

<span class="hljs-meta">&gt;&gt;&gt; </span>model_inputs = tokenizer([<span class="hljs-string">&quot;I am a cat.&quot;</span>], return_tensors=<span class="hljs-string">&quot;pt&quot;</span>).to(<span class="hljs-string">&quot;cuda&quot;</span>)

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># LLM + greedy decoding = repetitive, boring output</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>generated_ids = model.generate(**model_inputs)
<span class="hljs-meta">&gt;&gt;&gt; </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">&#x27;I am a cat. I am a cat. I am a cat. I am a cat&#x27;</span>

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># With sampling, the output becomes more creative!</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>generated_ids = model.generate(**model_inputs, do_sample=<span class="hljs-literal">True</span>)
<span class="hljs-meta">&gt;&gt;&gt; </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">&#x27;I am a cat.\\nI just need to be. I am always.\\nEvery time&#x27;</span>`}}),ze=new I({}),Be=new je({props:{code:"JTIzJTIwVGhlJTIwdG9rZW5pemVyJTIwaW5pdGlhbGl6ZWQlMjBhYm92ZSUyMGhhcyUyMHJpZ2h0LXBhZGRpbmclMjBhY3RpdmUlMjBieSUyMGRlZmF1bHQlM0ElMjB0aGUlMjAxc3QlMjBzZXF1ZW5jZSUyQyUwQSUyMyUyMHdoaWNoJTIwaXMlMjBzaG9ydGVyJTJDJTIwaGFzJTIwcGFkZGluZyUyMG9uJTIwdGhlJTIwcmlnaHQlMjBzaWRlLiUyMEdlbmVyYXRpb24lMjBmYWlscy4lMEFtb2RlbF9pbnB1dHMlMjAlM0QlMjB0b2tlbml6ZXIoJTBBJTIwJTIwJTIwJTIwJTVCJTIyMSUyQyUyMDIlMkMlMjAzJTIyJTJDJTIwJTIyQSUyQyUyMEIlMkMlMjBDJTJDJTIwRCUyQyUyMEUlMjIlNUQlMkMlMjBwYWRkaW5nJTNEVHJ1ZSUyQyUyMHJldHVybl90ZW5zb3JzJTNEJTIycHQlMjIlMEEpLnRvKCUyMmN1ZGElMjIpJTBBZ2VuZXJhdGVkX2lkcyUyMCUzRCUyMG1vZGVsLmdlbmVyYXRlKCoqbW9kZWxfaW5wdXRzKSUwQXRva2VuaXplci5iYXRjaF9kZWNvZGUoZ2VuZXJhdGVkX2lkcyU1QjAlNUQlMkMlMjBza2lwX3NwZWNpYWxfdG9rZW5zJTNEVHJ1ZSklNUIwJTVEJTBBJTBBJTIzJTIwV2l0aCUyMGxlZnQtcGFkZGluZyUyQyUyMGl0JTIwd29ya3MlMjBhcyUyMGV4cGVjdGVkISUwQXRva2VuaXplciUyMCUzRCUyMEF1dG9Ub2tlbml6ZXIuZnJvbV9wcmV0cmFpbmVkKCUyMm9wZW5sbS1yZXNlYXJjaCUyRm9wZW5fbGxhbWFfN2IlMjIlMkMlMjBwYWRkaW5nX3NpZGUlM0QlMjJsZWZ0JTIyKSUwQXRva2VuaXplci5wYWRfdG9rZW4lMjAlM0QlMjB0b2tlbml6ZXIuZW9zX3Rva2VuJTIwJTIwJTIzJTIwTGxhbWElMjBoYXMlMjBubyUyMHBhZCUyMHRva2VuJTIwYnklMjBkZWZhdWx0JTBBbW9kZWxfaW5wdXRzJTIwJTNEJTIwdG9rZW5pemVyKCUwQSUyMCUyMCUyMCUyMCU1QiUyMjElMkMlMjAyJTJDJTIwMyUyMiUyQyUyMCUyMkElMkMlMjBCJTJDJTIwQyUyQyUyMEQlMkMlMjBFJTIyJTVEJTJDJTIwcGFkZGluZyUzRFRydWUlMkMlMjByZXR1cm5fdGVuc29ycyUzRCUyMnB0JTIyJTBBKS50byglMjJjdWRhJTIyKSUwQWdlbmVyYXRlZF9pZHMlMjAlM0QlMjBtb2RlbC5nZW5lcmF0ZSgqKm1vZGVsX2lucHV0cyklMEF0b2tlbml6ZXIuYmF0Y2hfZGVjb2RlKGdlbmVyYXRlZF9pZHMlMkMlMjBza2lwX3NwZWNpYWxfdG9rZW5zJTNEVHJ1ZSklNUIwJTVE",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># The tokenizer initialized above has right-padding active by default: the 1st sequence,</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># which is shorter, has padding on the right side. Generation fails.</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>model_inputs = tokenizer(
<span class="hljs-meta">... </span>    [<span class="hljs-string">&quot;1, 2, 3&quot;</span>, <span class="hljs-string">&quot;A, B, C, D, E&quot;</span>], padding=<span class="hljs-literal">True</span>, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>
<span class="hljs-meta">... </span>).to(<span class="hljs-string">&quot;cuda&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>generated_ids = model.generate(**model_inputs)
<span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.batch_decode(generated_ids[<span class="hljs-number">0</span>], skip_special_tokens=<span class="hljs-literal">True</span>)[<span class="hljs-number">0</span>]
<span class="hljs-string">&#x27;&#x27;</span>

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># With left-padding, it works as expected!</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">&quot;openlm-research/open_llama_7b&quot;</span>, padding_side=<span class="hljs-string">&quot;left&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>tokenizer.pad_token = tokenizer.eos_token  <span class="hljs-comment"># Llama has no pad token by default</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>model_inputs = tokenizer(
<span class="hljs-meta">... </span>    [<span class="hljs-string">&quot;1, 2, 3&quot;</span>, <span class="hljs-string">&quot;A, B, C, D, E&quot;</span>], padding=<span class="hljs-literal">True</span>, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>
<span class="hljs-meta">... </span>).to(<span class="hljs-string">&quot;cuda&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>generated_ids = model.generate(**model_inputs)
<span class="hljs-meta">&gt;&gt;&gt; </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">&#x27;1, 2, 3, 4, 5, 6,&#x27;</span>`}}),Ne=new I({}),Fe=new I({}),qe=new I({}),He=new I({}),Ye=new I({}),{c(){b=s("meta"),D=u(),w=s("h1"),k=s("a"),V=s("span"),d(U.$$.fragment),$=u(),A=s("span"),Je=n("Generation with LLMs"),G=u(),d(M.$$.fragment),Ee=u(),R=s("p"),Ke=n("LLMs, or Large Language Models, are the key component behind text generation. In a nutshell, they consist of large pretrained transformer models trained to predict the next word (or, more precisely, token) given some input text. Since they predict one token at a time, you need to do something more elaborate to generate new sentences other than just calling the model \u2014 you need to do autoregressive generation."),va=u(),O=s("p"),Rs=n("Autoregressive generation is the inference-time procedure of iteratively calling a model with its own generated outputs, given a few initial inputs. In \u{1F917} Transformers, this is handled by the "),et=s("a"),Xs=n("generate()"),zs=n(" method, which is available to all models with generative capabilities."),ba=u(),tt=s("p"),Cs=n("This tutorial will show you how to:"),wa=u(),L=s("ul"),Lt=s("li"),Bs=n("Generate text with an LLM"),Ns=u(),xt=s("li"),Fs=n("Avoid common pitfalls"),qs=u(),Wt=s("li"),Ss=n("Next steps to help you get the most out your LLM"),Ma=u(),at=s("p"),Qs=n("Before you begin, make sure you have all the necessary libraries installed:"),_a=u(),d(Te.$$.fragment),ka=u(),X=s("h2"),K=s("a"),Vt=s("span"),d(Ue.$$.fragment),Hs=u(),At=s("span"),Ys=n("Generate text"),ja=u(),ee=s("p"),Ps=n("A language model trained for "),st=s("a"),Ds=n("causal language modeling"),Os=n(" takes a sequence of text tokens as input and returns the probability distribution for the next token."),Ja=u(),z=s("figure"),J=s("video"),Ks=u(),Rt=s("figcaption"),el=n('"Forward pass of an LLM"'),Ea=u(),lt=s("p"),tl=n("A critical aspect of autoregressive generation with LLMs is how to select the next token from this probability distribution. Anything goes in this step as long as you end up with a token for the next iteration. This means it can be as simple as selecting the most likely token from the probability distribution or as complex as applying a dozen transformations before sampling from the resulting distribution."),Ta=u(),C=s("figure"),E=s("video"),al=u(),Xt=s("figcaption"),sl=n('"Autoregressive generation iteratively selects the next token from a probability distribution to generate text"'),Ua=u(),te=s("p"),ll=n("The process depicted above is repeated iteratively until some stopping condition is reached. Ideally, the stopping condition is dictated by the model, which should learn when to output an end-of-sequence ("),zt=s("code"),ol=n("EOS"),nl=n(") token. If this is not the case, generation stops when some predefined maximum length is reached."),Za=u(),ae=s("p"),rl=n("Properly setting up the token selection step and the stopping condition is essential to make your model behave as you\u2019d expect on your task. That is why we have a "),ot=s("a"),il=n("GenerationConfig"),pl=n(" file associated with each model, which contains a good default generative parameterization and is loaded alongside your model."),Ia=u(),nt=s("p"),hl=n("Let\u2019s talk code!"),$a=u(),d(se.$$.fragment),Ga=u(),rt=s("p"),ul=n("First, you need to load the model."),La=u(),d(Ze.$$.fragment),xa=u(),le=s("p"),cl=n("You\u2019ll notice two flags in the "),Ct=s("code"),dl=n("from_pretrained"),fl=n(" call:"),Wa=u(),oe=s("ul"),it=s("li"),Bt=s("code"),ml=n("device_map"),gl=n(" ensures the model is moved to your GPU(s)"),yl=u(),ne=s("li"),Nt=s("code"),vl=n("load_in_4bit"),bl=n(" applies "),pt=s("a"),wl=n("4-bit dynamic quantization"),Ml=n(" to massively reduce the resource requirements"),Va=u(),ht=s("p"),_l=n("There are other ways to initialize a model, but this is a good baseline to begin with an LLM."),Aa=u(),re=s("p"),kl=n("Next, you need to preprocess your text input with a "),ut=s("a"),jl=n("tokenizer"),Jl=n("."),Ra=u(),d(Ie.$$.fragment),Xa=u(),x=s("p"),El=n("The "),Ft=s("code"),Tl=n("model_inputs"),Ul=n(" variable holds the tokenized text input, as well as the attention mask. While "),ct=s("a"),Zl=n("generate()"),Il=n(" does its best effort to infer the attention mask when it is not passed, we recommend passing it whenever possible for optimal results."),za=u(),ie=s("p"),$l=n("Finally, call the "),dt=s("a"),Gl=n("generate()"),Ll=n(" method to returns the generated tokens, which should be converted to text before printing."),Ca=u(),d($e.$$.fragment),Ba=u(),ft=s("p"),xl=n("And that\u2019s it! In a few lines of code, you can harness the power of an LLM."),Na=u(),B=s("h2"),pe=s("a"),qt=s("span"),d(Ge.$$.fragment),Wl=u(),St=s("span"),Vl=n("Common pitfalls"),Fa=u(),he=s("p"),Al=n("There are many "),mt=s("a"),Rl=n("generation strategies"),Xl=n(", and sometimes the default values may not be appropriate for your use case. If your outputs aren\u2019t aligned with what you\u2019re expecting, we\u2019ve created a list of the most common pitfalls and how to avoid them."),qa=u(),d(Le.$$.fragment),Sa=u(),N=s("h3"),ue=s("a"),Qt=s("span"),d(xe.$$.fragment),zl=u(),Ht=s("span"),Cl=n("Generated output is too short/long"),Qa=u(),_=s("p"),Bl=n("If not specified in the "),gt=s("a"),Nl=n("GenerationConfig"),Fl=n(" file, "),Yt=s("code"),ql=n("generate"),Sl=n(" returns up to 20 tokens by default. We highly recommend manually setting "),Pt=s("code"),Ql=n("max_new_tokens"),Hl=n(" in your "),Dt=s("code"),Yl=n("generate"),Pl=n(" call to control the maximum number of new tokens it can return. Keep in mind LLMs (more precisely, "),We=s("a"),Dl=n("decoder-only models"),Ol=n(") also return the input prompt as part of the output."),Ha=u(),d(Ve.$$.fragment),Ya=u(),F=s("h3"),ce=s("a"),Ot=s("span"),d(Ae.$$.fragment),Kl=u(),Kt=s("span"),eo=n("Incorrect generation mode"),Pa=u(),j=s("p"),to=n("By default, and unless specified in the "),yt=s("a"),ao=n("GenerationConfig"),so=n(" file, "),ea=s("code"),lo=n("generate"),oo=n(" selects the most likely token at each iteration (greedy decoding). Depending on your task, this may be undesirable; creative tasks like chatbots or writing an essay benefit from sampling. On the other hand, input-grounded tasks like audio transcription or translation benefit from greedy decoding. Enable sampling with "),ta=s("code"),no=n("do_sample=True"),ro=n(", and you can learn more about this topic in this "),Re=s("a"),io=n("blog post"),po=n("."),Da=u(),d(Xe.$$.fragment),Oa=u(),q=s("h3"),de=s("a"),aa=s("span"),d(ze.$$.fragment),ho=u(),sa=s("span"),uo=n("Wrong padding side"),Ka=u(),fe=s("p"),co=n("LLMs are "),Ce=s("a"),fo=n("decoder-only"),mo=n(" architectures, meaning they continue to iterate on your input prompt. If your inputs do not have the same length, they need to be padded. Since LLMs are not trained to continue from pad tokens, your input needs to be left-padded. Make sure you also don\u2019t forget to pass the attention mask to generate!"),es=u(),d(Be.$$.fragment),ts=u(),S=s("h2"),me=s("a"),la=s("span"),d(Ne.$$.fragment),go=u(),oa=s("span"),yo=n("Further resources"),as=u(),vt=s("p"),vo=n("While the autoregressive generation process is relatively straightforward, making the most out of your LLM can be a challenging endeavor because there are many moving parts. For your next steps to help you dive deeper into LLM usage and understanding:"),ss=u(),Q=s("h3"),ge=s("a"),na=s("span"),d(Fe.$$.fragment),bo=u(),ra=s("span"),wo=n("Advanced generate usage"),ls=u(),ye=s("ol"),bt=s("li"),wt=s("a"),Mo=n("Guide"),_o=n(" on how to control different generation methods, how to set up the generation configuration file, and how to stream the output;"),ko=u(),Z=s("li"),jo=n("API reference on "),Mt=s("a"),Jo=n("GenerationConfig"),Eo=n(", "),_t=s("a"),To=n("generate()"),Uo=n(", and "),kt=s("a"),Zo=n("generate-related classes"),Io=n("."),os=u(),H=s("h3"),ve=s("a"),ia=s("span"),d(qe.$$.fragment),$o=u(),pa=s("span"),Go=n("LLM leaderboards"),ns=u(),be=s("ol"),jt=s("li"),Se=s("a"),Lo=n("Open LLM Leaderboard"),xo=n(", which focuses on the quality of the open-source models;"),Wo=u(),Jt=s("li"),Qe=s("a"),Vo=n("Open LLM-Perf Leaderboard"),Ao=n(", which focuses on LLM throughput."),rs=u(),Y=s("h3"),we=s("a"),ha=s("span"),d(He.$$.fragment),Ro=u(),ua=s("span"),Xo=n("Latency and throughput"),is=u(),Et=s("ol"),Tt=s("li"),Ut=s("a"),zo=n("Guide"),Co=n(" on dynamic quantization, which shows you how to drastically reduce your memory requirements."),ps=u(),P=s("h3"),Me=s("a"),ca=s("span"),d(Ye.$$.fragment),Bo=u(),da=s("span"),No=n("Related libraries"),hs=u(),_e=s("ol"),Zt=s("li"),Pe=s("a"),fa=s("code"),Fo=n("text-generation-inference"),qo=n(", a production-ready server for LLMs;"),So=u(),It=s("li"),De=s("a"),ma=s("code"),Qo=n("optimum"),Ho=n(", an extension of \u{1F917} Transformers that optimizes for specific hardware devices."),this.h()},l(e){const i=Ar('[data-svelte="svelte-1phssyn"]',document.head);b=l(i,"META",{name:!0,content:!0}),i.forEach(t),D=c(e),w=l(e,"H1",{class:!0});var Oe=o(w);k=l(Oe,"A",{id:!0,class:!0,href:!0});var ln=o(k);V=l(ln,"SPAN",{});var on=o(V);f(U.$$.fragment,on),on.forEach(t),ln.forEach(t),$=c(Oe),A=l(Oe,"SPAN",{});var nn=o(A);Je=r(nn,"Generation with LLMs"),nn.forEach(t),Oe.forEach(t),G=c(e),f(M.$$.fragment,e),Ee=c(e),R=l(e,"P",{});var rn=o(R);Ke=r(rn,"LLMs, or Large Language Models, are the key component behind text generation. In a nutshell, they consist of large pretrained transformer models trained to predict the next word (or, more precisely, token) given some input text. Since they predict one token at a time, you need to do something more elaborate to generate new sentences other than just calling the model \u2014 you need to do autoregressive generation."),rn.forEach(t),va=c(e),O=l(e,"P",{});var cs=o(O);Rs=r(cs,"Autoregressive generation is the inference-time procedure of iteratively calling a model with its own generated outputs, given a few initial inputs. In \u{1F917} Transformers, this is handled by the "),et=l(cs,"A",{href:!0});var pn=o(et);Xs=r(pn,"generate()"),pn.forEach(t),zs=r(cs," method, which is available to all models with generative capabilities."),cs.forEach(t),ba=c(e),tt=l(e,"P",{});var hn=o(tt);Cs=r(hn,"This tutorial will show you how to:"),hn.forEach(t),wa=c(e),L=l(e,"UL",{});var $t=o(L);Lt=l($t,"LI",{});var un=o(Lt);Bs=r(un,"Generate text with an LLM"),un.forEach(t),Ns=c($t),xt=l($t,"LI",{});var cn=o(xt);Fs=r(cn,"Avoid common pitfalls"),cn.forEach(t),qs=c($t),Wt=l($t,"LI",{});var dn=o(Wt);Ss=r(dn,"Next steps to help you get the most out your LLM"),dn.forEach(t),$t.forEach(t),Ma=c(e),at=l(e,"P",{});var fn=o(at);Qs=r(fn,"Before you begin, make sure you have all the necessary libraries installed:"),fn.forEach(t),_a=c(e),f(Te.$$.fragment,e),ka=c(e),X=l(e,"H2",{class:!0});var ds=o(X);K=l(ds,"A",{id:!0,class:!0,href:!0});var mn=o(K);Vt=l(mn,"SPAN",{});var gn=o(Vt);f(Ue.$$.fragment,gn),gn.forEach(t),mn.forEach(t),Hs=c(ds),At=l(ds,"SPAN",{});var yn=o(At);Ys=r(yn,"Generate text"),yn.forEach(t),ds.forEach(t),ja=c(e),ee=l(e,"P",{});var fs=o(ee);Ps=r(fs,"A language model trained for "),st=l(fs,"A",{href:!0});var vn=o(st);Ds=r(vn,"causal language modeling"),vn.forEach(t),Os=r(fs," takes a sequence of text tokens as input and returns the probability distribution for the next token."),fs.forEach(t),Ja=c(e),z=l(e,"FIGURE",{class:!0});var ms=o(z);J=l(ms,"VIDEO",{style:!0,src:!0}),o(J).forEach(t),Ks=c(ms),Rt=l(ms,"FIGCAPTION",{});var bn=o(Rt);el=r(bn,'"Forward pass of an LLM"'),bn.forEach(t),ms.forEach(t),Ea=c(e),lt=l(e,"P",{});var wn=o(lt);tl=r(wn,"A critical aspect of autoregressive generation with LLMs is how to select the next token from this probability distribution. Anything goes in this step as long as you end up with a token for the next iteration. This means it can be as simple as selecting the most likely token from the probability distribution or as complex as applying a dozen transformations before sampling from the resulting distribution."),wn.forEach(t),Ta=c(e),C=l(e,"FIGURE",{class:!0});var gs=o(C);E=l(gs,"VIDEO",{style:!0,src:!0}),o(E).forEach(t),al=c(gs),Xt=l(gs,"FIGCAPTION",{});var Mn=o(Xt);sl=r(Mn,'"Autoregressive generation iteratively selects the next token from a probability distribution to generate text"'),Mn.forEach(t),gs.forEach(t),Ua=c(e),te=l(e,"P",{});var ys=o(te);ll=r(ys,"The process depicted above is repeated iteratively until some stopping condition is reached. Ideally, the stopping condition is dictated by the model, which should learn when to output an end-of-sequence ("),zt=l(ys,"CODE",{});var _n=o(zt);ol=r(_n,"EOS"),_n.forEach(t),nl=r(ys,") token. If this is not the case, generation stops when some predefined maximum length is reached."),ys.forEach(t),Za=c(e),ae=l(e,"P",{});var vs=o(ae);rl=r(vs,"Properly setting up the token selection step and the stopping condition is essential to make your model behave as you\u2019d expect on your task. That is why we have a "),ot=l(vs,"A",{href:!0});var kn=o(ot);il=r(kn,"GenerationConfig"),kn.forEach(t),pl=r(vs," file associated with each model, which contains a good default generative parameterization and is loaded alongside your model."),vs.forEach(t),Ia=c(e),nt=l(e,"P",{});var jn=o(nt);hl=r(jn,"Let\u2019s talk code!"),jn.forEach(t),$a=c(e),f(se.$$.fragment,e),Ga=c(e),rt=l(e,"P",{});var Jn=o(rt);ul=r(Jn,"First, you need to load the model."),Jn.forEach(t),La=c(e),f(Ze.$$.fragment,e),xa=c(e),le=l(e,"P",{});var bs=o(le);cl=r(bs,"You\u2019ll notice two flags in the "),Ct=l(bs,"CODE",{});var En=o(Ct);dl=r(En,"from_pretrained"),En.forEach(t),fl=r(bs," call:"),bs.forEach(t),Wa=c(e),oe=l(e,"UL",{});var ws=o(oe);it=l(ws,"LI",{});var Yo=o(it);Bt=l(Yo,"CODE",{});var Tn=o(Bt);ml=r(Tn,"device_map"),Tn.forEach(t),gl=r(Yo," ensures the model is moved to your GPU(s)"),Yo.forEach(t),yl=c(ws),ne=l(ws,"LI",{});var ga=o(ne);Nt=l(ga,"CODE",{});var Un=o(Nt);vl=r(Un,"load_in_4bit"),Un.forEach(t),bl=r(ga," applies "),pt=l(ga,"A",{href:!0});var Zn=o(pt);wl=r(Zn,"4-bit dynamic quantization"),Zn.forEach(t),Ml=r(ga," to massively reduce the resource requirements"),ga.forEach(t),ws.forEach(t),Va=c(e),ht=l(e,"P",{});var In=o(ht);_l=r(In,"There are other ways to initialize a model, but this is a good baseline to begin with an LLM."),In.forEach(t),Aa=c(e),re=l(e,"P",{});var Ms=o(re);kl=r(Ms,"Next, you need to preprocess your text input with a "),ut=l(Ms,"A",{href:!0});var $n=o(ut);jl=r($n,"tokenizer"),$n.forEach(t),Jl=r(Ms,"."),Ms.forEach(t),Ra=c(e),f(Ie.$$.fragment,e),Xa=c(e),x=l(e,"P",{});var Gt=o(x);El=r(Gt,"The "),Ft=l(Gt,"CODE",{});var Gn=o(Ft);Tl=r(Gn,"model_inputs"),Gn.forEach(t),Ul=r(Gt," variable holds the tokenized text input, as well as the attention mask. While "),ct=l(Gt,"A",{href:!0});var Ln=o(ct);Zl=r(Ln,"generate()"),Ln.forEach(t),Il=r(Gt," does its best effort to infer the attention mask when it is not passed, we recommend passing it whenever possible for optimal results."),Gt.forEach(t),za=c(e),ie=l(e,"P",{});var _s=o(ie);$l=r(_s,"Finally, call the "),dt=l(_s,"A",{href:!0});var xn=o(dt);Gl=r(xn,"generate()"),xn.forEach(t),Ll=r(_s," method to returns the generated tokens, which should be converted to text before printing."),_s.forEach(t),Ca=c(e),f($e.$$.fragment,e),Ba=c(e),ft=l(e,"P",{});var Wn=o(ft);xl=r(Wn,"And that\u2019s it! In a few lines of code, you can harness the power of an LLM."),Wn.forEach(t),Na=c(e),B=l(e,"H2",{class:!0});var ks=o(B);pe=l(ks,"A",{id:!0,class:!0,href:!0});var Vn=o(pe);qt=l(Vn,"SPAN",{});var An=o(qt);f(Ge.$$.fragment,An),An.forEach(t),Vn.forEach(t),Wl=c(ks),St=l(ks,"SPAN",{});var Rn=o(St);Vl=r(Rn,"Common pitfalls"),Rn.forEach(t),ks.forEach(t),Fa=c(e),he=l(e,"P",{});var js=o(he);Al=r(js,"There are many "),mt=l(js,"A",{href:!0});var Xn=o(mt);Rl=r(Xn,"generation strategies"),Xn.forEach(t),Xl=r(js,", and sometimes the default values may not be appropriate for your use case. If your outputs aren\u2019t aligned with what you\u2019re expecting, we\u2019ve created a list of the most common pitfalls and how to avoid them."),js.forEach(t),qa=c(e),f(Le.$$.fragment,e),Sa=c(e),N=l(e,"H3",{class:!0});var Js=o(N);ue=l(Js,"A",{id:!0,class:!0,href:!0});var zn=o(ue);Qt=l(zn,"SPAN",{});var Cn=o(Qt);f(xe.$$.fragment,Cn),Cn.forEach(t),zn.forEach(t),zl=c(Js),Ht=l(Js,"SPAN",{});var Bn=o(Ht);Cl=r(Bn,"Generated output is too short/long"),Bn.forEach(t),Js.forEach(t),Qa=c(e),_=l(e,"P",{});var T=o(_);Bl=r(T,"If not specified in the "),gt=l(T,"A",{href:!0});var Nn=o(gt);Nl=r(Nn,"GenerationConfig"),Nn.forEach(t),Fl=r(T," file, "),Yt=l(T,"CODE",{});var Fn=o(Yt);ql=r(Fn,"generate"),Fn.forEach(t),Sl=r(T," returns up to 20 tokens by default. We highly recommend manually setting "),Pt=l(T,"CODE",{});var qn=o(Pt);Ql=r(qn,"max_new_tokens"),qn.forEach(t),Hl=r(T," in your "),Dt=l(T,"CODE",{});var Sn=o(Dt);Yl=r(Sn,"generate"),Sn.forEach(t),Pl=r(T," call to control the maximum number of new tokens it can return. Keep in mind LLMs (more precisely, "),We=l(T,"A",{href:!0,rel:!0});var Qn=o(We);Dl=r(Qn,"decoder-only models"),Qn.forEach(t),Ol=r(T,") also return the input prompt as part of the output."),T.forEach(t),Ha=c(e),f(Ve.$$.fragment,e),Ya=c(e),F=l(e,"H3",{class:!0});var Es=o(F);ce=l(Es,"A",{id:!0,class:!0,href:!0});var Hn=o(ce);Ot=l(Hn,"SPAN",{});var Yn=o(Ot);f(Ae.$$.fragment,Yn),Yn.forEach(t),Hn.forEach(t),Kl=c(Es),Kt=l(Es,"SPAN",{});var Pn=o(Kt);eo=r(Pn,"Incorrect generation mode"),Pn.forEach(t),Es.forEach(t),Pa=c(e),j=l(e,"P",{});var W=o(j);to=r(W,"By default, and unless specified in the "),yt=l(W,"A",{href:!0});var Dn=o(yt);ao=r(Dn,"GenerationConfig"),Dn.forEach(t),so=r(W," file, "),ea=l(W,"CODE",{});var On=o(ea);lo=r(On,"generate"),On.forEach(t),oo=r(W," selects the most likely token at each iteration (greedy decoding). Depending on your task, this may be undesirable; creative tasks like chatbots or writing an essay benefit from sampling. On the other hand, input-grounded tasks like audio transcription or translation benefit from greedy decoding. Enable sampling with "),ta=l(W,"CODE",{});var Kn=o(ta);no=r(Kn,"do_sample=True"),Kn.forEach(t),ro=r(W,", and you can learn more about this topic in this "),Re=l(W,"A",{href:!0,rel:!0});var er=o(Re);io=r(er,"blog post"),er.forEach(t),po=r(W,"."),W.forEach(t),Da=c(e),f(Xe.$$.fragment,e),Oa=c(e),q=l(e,"H3",{class:!0});var Ts=o(q);de=l(Ts,"A",{id:!0,class:!0,href:!0});var tr=o(de);aa=l(tr,"SPAN",{});var ar=o(aa);f(ze.$$.fragment,ar),ar.forEach(t),tr.forEach(t),ho=c(Ts),sa=l(Ts,"SPAN",{});var sr=o(sa);uo=r(sr,"Wrong padding side"),sr.forEach(t),Ts.forEach(t),Ka=c(e),fe=l(e,"P",{});var Us=o(fe);co=r(Us,"LLMs are "),Ce=l(Us,"A",{href:!0,rel:!0});var lr=o(Ce);fo=r(lr,"decoder-only"),lr.forEach(t),mo=r(Us," architectures, meaning they continue to iterate on your input prompt. If your inputs do not have the same length, they need to be padded. Since LLMs are not trained to continue from pad tokens, your input needs to be left-padded. Make sure you also don\u2019t forget to pass the attention mask to generate!"),Us.forEach(t),es=c(e),f(Be.$$.fragment,e),ts=c(e),S=l(e,"H2",{class:!0});var Zs=o(S);me=l(Zs,"A",{id:!0,class:!0,href:!0});var or=o(me);la=l(or,"SPAN",{});var nr=o(la);f(Ne.$$.fragment,nr),nr.forEach(t),or.forEach(t),go=c(Zs),oa=l(Zs,"SPAN",{});var rr=o(oa);yo=r(rr,"Further resources"),rr.forEach(t),Zs.forEach(t),as=c(e),vt=l(e,"P",{});var ir=o(vt);vo=r(ir,"While the autoregressive generation process is relatively straightforward, making the most out of your LLM can be a challenging endeavor because there are many moving parts. For your next steps to help you dive deeper into LLM usage and understanding:"),ir.forEach(t),ss=c(e),Q=l(e,"H3",{class:!0});var Is=o(Q);ge=l(Is,"A",{id:!0,class:!0,href:!0});var pr=o(ge);na=l(pr,"SPAN",{});var hr=o(na);f(Fe.$$.fragment,hr),hr.forEach(t),pr.forEach(t),bo=c(Is),ra=l(Is,"SPAN",{});var ur=o(ra);wo=r(ur,"Advanced generate usage"),ur.forEach(t),Is.forEach(t),ls=c(e),ye=l(e,"OL",{});var $s=o(ye);bt=l($s,"LI",{});var Po=o(bt);wt=l(Po,"A",{href:!0});var cr=o(wt);Mo=r(cr,"Guide"),cr.forEach(t),_o=r(Po," on how to control different generation methods, how to set up the generation configuration file, and how to stream the output;"),Po.forEach(t),ko=c($s),Z=l($s,"LI",{});var ke=o(Z);jo=r(ke,"API reference on "),Mt=l(ke,"A",{href:!0});var dr=o(Mt);Jo=r(dr,"GenerationConfig"),dr.forEach(t),Eo=r(ke,", "),_t=l(ke,"A",{href:!0});var fr=o(_t);To=r(fr,"generate()"),fr.forEach(t),Uo=r(ke,", and "),kt=l(ke,"A",{href:!0});var mr=o(kt);Zo=r(mr,"generate-related classes"),mr.forEach(t),Io=r(ke,"."),ke.forEach(t),$s.forEach(t),os=c(e),H=l(e,"H3",{class:!0});var Gs=o(H);ve=l(Gs,"A",{id:!0,class:!0,href:!0});var gr=o(ve);ia=l(gr,"SPAN",{});var yr=o(ia);f(qe.$$.fragment,yr),yr.forEach(t),gr.forEach(t),$o=c(Gs),pa=l(Gs,"SPAN",{});var vr=o(pa);Go=r(vr,"LLM leaderboards"),vr.forEach(t),Gs.forEach(t),ns=c(e),be=l(e,"OL",{});var Ls=o(be);jt=l(Ls,"LI",{});var Do=o(jt);Se=l(Do,"A",{href:!0,rel:!0});var br=o(Se);Lo=r(br,"Open LLM Leaderboard"),br.forEach(t),xo=r(Do,", which focuses on the quality of the open-source models;"),Do.forEach(t),Wo=c(Ls),Jt=l(Ls,"LI",{});var Oo=o(Jt);Qe=l(Oo,"A",{href:!0,rel:!0});var wr=o(Qe);Vo=r(wr,"Open LLM-Perf Leaderboard"),wr.forEach(t),Ao=r(Oo,", which focuses on LLM throughput."),Oo.forEach(t),Ls.forEach(t),rs=c(e),Y=l(e,"H3",{class:!0});var xs=o(Y);we=l(xs,"A",{id:!0,class:!0,href:!0});var Mr=o(we);ha=l(Mr,"SPAN",{});var _r=o(ha);f(He.$$.fragment,_r),_r.forEach(t),Mr.forEach(t),Ro=c(xs),ua=l(xs,"SPAN",{});var kr=o(ua);Xo=r(kr,"Latency and throughput"),kr.forEach(t),xs.forEach(t),is=c(e),Et=l(e,"OL",{});var jr=o(Et);Tt=l(jr,"LI",{});var Ko=o(Tt);Ut=l(Ko,"A",{href:!0});var Jr=o(Ut);zo=r(Jr,"Guide"),Jr.forEach(t),Co=r(Ko," on dynamic quantization, which shows you how to drastically reduce your memory requirements."),Ko.forEach(t),jr.forEach(t),ps=c(e),P=l(e,"H3",{class:!0});var Ws=o(P);Me=l(Ws,"A",{id:!0,class:!0,href:!0});var Er=o(Me);ca=l(Er,"SPAN",{});var Tr=o(ca);f(Ye.$$.fragment,Tr),Tr.forEach(t),Er.forEach(t),Bo=c(Ws),da=l(Ws,"SPAN",{});var Ur=o(da);No=r(Ur,"Related libraries"),Ur.forEach(t),Ws.forEach(t),hs=c(e),_e=l(e,"OL",{});var Vs=o(_e);Zt=l(Vs,"LI",{});var en=o(Zt);Pe=l(en,"A",{href:!0,rel:!0});var Zr=o(Pe);fa=l(Zr,"CODE",{});var Ir=o(fa);Fo=r(Ir,"text-generation-inference"),Ir.forEach(t),Zr.forEach(t),qo=r(en,", a production-ready server for LLMs;"),en.forEach(t),So=c(Vs),It=l(Vs,"LI",{});var tn=o(It);De=l(tn,"A",{href:!0,rel:!0});var $r=o(De);ma=l($r,"CODE",{});var Gr=o(ma);Qo=r(Gr,"optimum"),Gr.forEach(t),$r.forEach(t),Ho=r(tn,", an extension of \u{1F917} Transformers that optimizes for specific hardware devices."),tn.forEach(t),Vs.forEach(t),this.h()},h(){h(b,"name","hf:doc:metadata"),h(b,"content",JSON.stringify(Nr)),h(k,"id","generation-with-llms"),h(k,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(k,"href","#generation-with-llms"),h(w,"class","relative group"),h(et,"href","/docs/transformers/v4.32.0/en/main_classes/text_generation#transformers.GenerationMixin.generate"),h(K,"id","generate-text"),h(K,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(K,"href","#generate-text"),h(X,"class","relative group"),h(st,"href","tasks/language_modeling"),As(J,"max-width","90%"),As(J,"margin","auto"),J.autoplay=!0,J.loop=!0,J.muted=!0,J.playsInline=!0,Lr(J.src,an="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/assisted-generation/gif_1_1080p.mov")||h(J,"src",an),h(z,"class","image table text-center m-0 w-full"),As(E,"max-width","90%"),As(E,"margin","auto"),E.autoplay=!0,E.loop=!0,E.muted=!0,E.playsInline=!0,Lr(E.src,sn="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/assisted-generation/gif_2_1080p.mov")||h(E,"src",sn),h(C,"class","image table text-center m-0 w-full"),h(ot,"href","/docs/transformers/v4.32.0/en/main_classes/text_generation#transformers.GenerationConfig"),h(pt,"href","main_classes/quantization"),h(ut,"href","tokenizer_summary"),h(ct,"href","/docs/transformers/v4.32.0/en/main_classes/text_generation#transformers.GenerationMixin.generate"),h(dt,"href","/docs/transformers/v4.32.0/en/main_classes/text_generation#transformers.GenerationMixin.generate"),h(pe,"id","common-pitfalls"),h(pe,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(pe,"href","#common-pitfalls"),h(B,"class","relative group"),h(mt,"href","generation_strategies"),h(ue,"id","generated-output-is-too-shortlong"),h(ue,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(ue,"href","#generated-output-is-too-shortlong"),h(N,"class","relative group"),h(gt,"href","/docs/transformers/v4.32.0/en/main_classes/text_generation#transformers.GenerationConfig"),h(We,"href","https://huggingface.co/learn/nlp-course/chapter1/6?fw=pt"),h(We,"rel","nofollow"),h(ce,"id","incorrect-generation-mode"),h(ce,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(ce,"href","#incorrect-generation-mode"),h(F,"class","relative group"),h(yt,"href","/docs/transformers/v4.32.0/en/main_classes/text_generation#transformers.GenerationConfig"),h(Re,"href","https://huggingface.co/blog/how-to-generate"),h(Re,"rel","nofollow"),h(de,"id","wrong-padding-side"),h(de,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(de,"href","#wrong-padding-side"),h(q,"class","relative group"),h(Ce,"href","https://huggingface.co/learn/nlp-course/chapter1/6?fw=pt"),h(Ce,"rel","nofollow"),h(me,"id","further-resources"),h(me,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(me,"href","#further-resources"),h(S,"class","relative group"),h(ge,"id","advanced-generate-usage"),h(ge,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(ge,"href","#advanced-generate-usage"),h(Q,"class","relative group"),h(wt,"href","generation_strategies"),h(Mt,"href","/docs/transformers/v4.32.0/en/main_classes/text_generation#transformers.GenerationConfig"),h(_t,"href","/docs/transformers/v4.32.0/en/main_classes/text_generation#transformers.GenerationMixin.generate"),h(kt,"href","internal/generation_utils"),h(ve,"id","llm-leaderboards"),h(ve,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(ve,"href","#llm-leaderboards"),h(H,"class","relative group"),h(Se,"href","https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard"),h(Se,"rel","nofollow"),h(Qe,"href","https://huggingface.co/spaces/optimum/llm-perf-leaderboard"),h(Qe,"rel","nofollow"),h(we,"id","latency-and-throughput"),h(we,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(we,"href","#latency-and-throughput"),h(Y,"class","relative group"),h(Ut,"href","main_classes/quantization"),h(Me,"id","related-libraries"),h(Me,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),h(Me,"href","#related-libraries"),h(P,"class","relative group"),h(Pe,"href","https://github.com/huggingface/text-generation-inference"),h(Pe,"rel","nofollow"),h(De,"href","https://github.com/huggingface/optimum"),h(De,"rel","nofollow")},m(e,i){a(document.head,b),p(e,D,i),p(e,w,i),a(w,k),a(k,V),m(U,V,null),a(w,$),a(w,A),a(A,Je),p(e,G,i),m(M,e,i),p(e,Ee,i),p(e,R,i),a(R,Ke),p(e,va,i),p(e,O,i),a(O,Rs),a(O,et),a(et,Xs),a(O,zs),p(e,ba,i),p(e,tt,i),a(tt,Cs),p(e,wa,i),p(e,L,i),a(L,Lt),a(Lt,Bs),a(L,Ns),a(L,xt),a(xt,Fs),a(L,qs),a(L,Wt),a(Wt,Ss),p(e,Ma,i),p(e,at,i),a(at,Qs),p(e,_a,i),m(Te,e,i),p(e,ka,i),p(e,X,i),a(X,K),a(K,Vt),m(Ue,Vt,null),a(X,Hs),a(X,At),a(At,Ys),p(e,ja,i),p(e,ee,i),a(ee,Ps),a(ee,st),a(st,Ds),a(ee,Os),p(e,Ja,i),p(e,z,i),a(z,J),a(z,Ks),a(z,Rt),a(Rt,el),p(e,Ea,i),p(e,lt,i),a(lt,tl),p(e,Ta,i),p(e,C,i),a(C,E),a(C,al),a(C,Xt),a(Xt,sl),p(e,Ua,i),p(e,te,i),a(te,ll),a(te,zt),a(zt,ol),a(te,nl),p(e,Za,i),p(e,ae,i),a(ae,rl),a(ae,ot),a(ot,il),a(ae,pl),p(e,Ia,i),p(e,nt,i),a(nt,hl),p(e,$a,i),m(se,e,i),p(e,Ga,i),p(e,rt,i),a(rt,ul),p(e,La,i),m(Ze,e,i),p(e,xa,i),p(e,le,i),a(le,cl),a(le,Ct),a(Ct,dl),a(le,fl),p(e,Wa,i),p(e,oe,i),a(oe,it),a(it,Bt),a(Bt,ml),a(it,gl),a(oe,yl),a(oe,ne),a(ne,Nt),a(Nt,vl),a(ne,bl),a(ne,pt),a(pt,wl),a(ne,Ml),p(e,Va,i),p(e,ht,i),a(ht,_l),p(e,Aa,i),p(e,re,i),a(re,kl),a(re,ut),a(ut,jl),a(re,Jl),p(e,Ra,i),m(Ie,e,i),p(e,Xa,i),p(e,x,i),a(x,El),a(x,Ft),a(Ft,Tl),a(x,Ul),a(x,ct),a(ct,Zl),a(x,Il),p(e,za,i),p(e,ie,i),a(ie,$l),a(ie,dt),a(dt,Gl),a(ie,Ll),p(e,Ca,i),m($e,e,i),p(e,Ba,i),p(e,ft,i),a(ft,xl),p(e,Na,i),p(e,B,i),a(B,pe),a(pe,qt),m(Ge,qt,null),a(B,Wl),a(B,St),a(St,Vl),p(e,Fa,i),p(e,he,i),a(he,Al),a(he,mt),a(mt,Rl),a(he,Xl),p(e,qa,i),m(Le,e,i),p(e,Sa,i),p(e,N,i),a(N,ue),a(ue,Qt),m(xe,Qt,null),a(N,zl),a(N,Ht),a(Ht,Cl),p(e,Qa,i),p(e,_,i),a(_,Bl),a(_,gt),a(gt,Nl),a(_,Fl),a(_,Yt),a(Yt,ql),a(_,Sl),a(_,Pt),a(Pt,Ql),a(_,Hl),a(_,Dt),a(Dt,Yl),a(_,Pl),a(_,We),a(We,Dl),a(_,Ol),p(e,Ha,i),m(Ve,e,i),p(e,Ya,i),p(e,F,i),a(F,ce),a(ce,Ot),m(Ae,Ot,null),a(F,Kl),a(F,Kt),a(Kt,eo),p(e,Pa,i),p(e,j,i),a(j,to),a(j,yt),a(yt,ao),a(j,so),a(j,ea),a(ea,lo),a(j,oo),a(j,ta),a(ta,no),a(j,ro),a(j,Re),a(Re,io),a(j,po),p(e,Da,i),m(Xe,e,i),p(e,Oa,i),p(e,q,i),a(q,de),a(de,aa),m(ze,aa,null),a(q,ho),a(q,sa),a(sa,uo),p(e,Ka,i),p(e,fe,i),a(fe,co),a(fe,Ce),a(Ce,fo),a(fe,mo),p(e,es,i),m(Be,e,i),p(e,ts,i),p(e,S,i),a(S,me),a(me,la),m(Ne,la,null),a(S,go),a(S,oa),a(oa,yo),p(e,as,i),p(e,vt,i),a(vt,vo),p(e,ss,i),p(e,Q,i),a(Q,ge),a(ge,na),m(Fe,na,null),a(Q,bo),a(Q,ra),a(ra,wo),p(e,ls,i),p(e,ye,i),a(ye,bt),a(bt,wt),a(wt,Mo),a(bt,_o),a(ye,ko),a(ye,Z),a(Z,jo),a(Z,Mt),a(Mt,Jo),a(Z,Eo),a(Z,_t),a(_t,To),a(Z,Uo),a(Z,kt),a(kt,Zo),a(Z,Io),p(e,os,i),p(e,H,i),a(H,ve),a(ve,ia),m(qe,ia,null),a(H,$o),a(H,pa),a(pa,Go),p(e,ns,i),p(e,be,i),a(be,jt),a(jt,Se),a(Se,Lo),a(jt,xo),a(be,Wo),a(be,Jt),a(Jt,Qe),a(Qe,Vo),a(Jt,Ao),p(e,rs,i),p(e,Y,i),a(Y,we),a(we,ha),m(He,ha,null),a(Y,Ro),a(Y,ua),a(ua,Xo),p(e,is,i),p(e,Et,i),a(Et,Tt),a(Tt,Ut),a(Ut,zo),a(Tt,Co),p(e,ps,i),p(e,P,i),a(P,Me),a(Me,ca),m(Ye,ca,null),a(P,Bo),a(P,da),a(da,No),p(e,hs,i),p(e,_e,i),a(_e,Zt),a(Zt,Pe),a(Pe,fa),a(fa,Fo),a(Zt,qo),a(_e,So),a(_e,It),a(It,De),a(De,ma),a(ma,Qo),a(It,Ho),us=!0},p(e,[i]){const Oe={};i&2&&(Oe.$$scope={dirty:i,ctx:e}),se.$set(Oe)},i(e){us||(g(U.$$.fragment,e),g(M.$$.fragment,e),g(Te.$$.fragment,e),g(Ue.$$.fragment,e),g(se.$$.fragment,e),g(Ze.$$.fragment,e),g(Ie.$$.fragment,e),g($e.$$.fragment,e),g(Ge.$$.fragment,e),g(Le.$$.fragment,e),g(xe.$$.fragment,e),g(Ve.$$.fragment,e),g(Ae.$$.fragment,e),g(Xe.$$.fragment,e),g(ze.$$.fragment,e),g(Be.$$.fragment,e),g(Ne.$$.fragment,e),g(Fe.$$.fragment,e),g(qe.$$.fragment,e),g(He.$$.fragment,e),g(Ye.$$.fragment,e),us=!0)},o(e){y(U.$$.fragment,e),y(M.$$.fragment,e),y(Te.$$.fragment,e),y(Ue.$$.fragment,e),y(se.$$.fragment,e),y(Ze.$$.fragment,e),y(Ie.$$.fragment,e),y($e.$$.fragment,e),y(Ge.$$.fragment,e),y(Le.$$.fragment,e),y(xe.$$.fragment,e),y(Ve.$$.fragment,e),y(Ae.$$.fragment,e),y(Xe.$$.fragment,e),y(ze.$$.fragment,e),y(Be.$$.fragment,e),y(Ne.$$.fragment,e),y(Fe.$$.fragment,e),y(qe.$$.fragment,e),y(He.$$.fragment,e),y(Ye.$$.fragment,e),us=!1},d(e){t(b),e&&t(D),e&&t(w),v(U),e&&t(G),v(M,e),e&&t(Ee),e&&t(R),e&&t(va),e&&t(O),e&&t(ba),e&&t(tt),e&&t(wa),e&&t(L),e&&t(Ma),e&&t(at),e&&t(_a),v(Te,e),e&&t(ka),e&&t(X),v(Ue),e&&t(ja),e&&t(ee),e&&t(Ja),e&&t(z),e&&t(Ea),e&&t(lt),e&&t(Ta),e&&t(C),e&&t(Ua),e&&t(te),e&&t(Za),e&&t(ae),e&&t(Ia),e&&t(nt),e&&t($a),v(se,e),e&&t(Ga),e&&t(rt),e&&t(La),v(Ze,e),e&&t(xa),e&&t(le),e&&t(Wa),e&&t(oe),e&&t(Va),e&&t(ht),e&&t(Aa),e&&t(re),e&&t(Ra),v(Ie,e),e&&t(Xa),e&&t(x),e&&t(za),e&&t(ie),e&&t(Ca),v($e,e),e&&t(Ba),e&&t(ft),e&&t(Na),e&&t(B),v(Ge),e&&t(Fa),e&&t(he),e&&t(qa),v(Le,e),e&&t(Sa),e&&t(N),v(xe),e&&t(Qa),e&&t(_),e&&t(Ha),v(Ve,e),e&&t(Ya),e&&t(F),v(Ae),e&&t(Pa),e&&t(j),e&&t(Da),v(Xe,e),e&&t(Oa),e&&t(q),v(ze),e&&t(Ka),e&&t(fe),e&&t(es),v(Be,e),e&&t(ts),e&&t(S),v(Ne),e&&t(as),e&&t(vt),e&&t(ss),e&&t(Q),v(Fe),e&&t(ls),e&&t(ye),e&&t(os),e&&t(H),v(qe),e&&t(ns),e&&t(be),e&&t(rs),e&&t(Y),v(He),e&&t(is),e&&t(Et),e&&t(ps),e&&t(P),v(Ye),e&&t(hs),e&&t(_e)}}}const Nr={local:"generation-with-llms",sections:[{local:"generate-text",title:"Generate text"},{local:"common-pitfalls",sections:[{local:"generated-output-is-too-shortlong",title:"Generated output is too short/long"},{local:"incorrect-generation-mode",title:"Incorrect generation mode"},{local:"wrong-padding-side",title:"Wrong padding side"}],title:"Common pitfalls"},{local:"further-resources",sections:[{local:"advanced-generate-usage",title:"Advanced generate usage"},{local:"llm-leaderboards",title:"LLM leaderboards"},{local:"latency-and-throughput",title:"Latency and throughput"},{local:"related-libraries",title:"Related libraries"}],title:"Further resources"}],title:"Generation with LLMs"};function Fr(ya){return Rr(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Pr extends xr{constructor(b){super();Wr(this,b,Fr,Br,Vr,{})}}export{Pr as default,Nr as metadata};
