import{s as Te,o as be,n as we}from"../chunks/scheduler.4cd3e004.js";import{S as ye,i as Je,e as m,s as i,c as w,h as ve,a as l,d as n,b as r,f as A,g as y,j as _,k as K,l as c,m as a,n as J,t as v,o as M,p as $}from"../chunks/index.b664cb2d.js";import{D as _e}from"../chunks/Docstring.9ceb4334.js";import{C as Me}from"../chunks/CodeBlock.8601e34e.js";import{E as $e}from"../chunks/ExampleCodeBlock.4f57b575.js";import{H as re,E as ke}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.179b63f8.js";function Pe(X){let p,k="Example:",h,g,f;return g=new Me({props:{code:"ZnJvbSUyMHBlZnQlMjBpbXBvcnQlMjBQcm9tcHRFbWJlZGRpbmclMkMlMjBQcm9tcHRUdW5pbmdDb25maWclMEElMEFjb25maWclMjAlM0QlMjBQcm9tcHRUdW5pbmdDb25maWcoJTBBJTIwJTIwJTIwJTIwcGVmdF90eXBlJTNEJTIyUFJPTVBUX1RVTklORyUyMiUyQyUwQSUyMCUyMCUyMCUyMHRhc2tfdHlwZSUzRCUyMlNFUV8yX1NFUV9MTSUyMiUyQyUwQSUyMCUyMCUyMCUyMG51bV92aXJ0dWFsX3Rva2VucyUzRDIwJTJDJTBBJTIwJTIwJTIwJTIwdG9rZW5fZGltJTNENzY4JTJDJTBBJTIwJTIwJTIwJTIwbnVtX3RyYW5zZm9ybWVyX3N1Ym1vZHVsZXMlM0QxJTJDJTBBJTIwJTIwJTIwJTIwbnVtX2F0dGVudGlvbl9oZWFkcyUzRDEyJTJDJTBBJTIwJTIwJTIwJTIwbnVtX2xheWVycyUzRDEyJTJDJTBBJTIwJTIwJTIwJTIwcHJvbXB0X3R1bmluZ19pbml0JTNEJTIyVEVYVCUyMiUyQyUwQSUyMCUyMCUyMCUyMHByb21wdF90dW5pbmdfaW5pdF90ZXh0JTNEJTIyUHJlZGljdCUyMGlmJTIwc2VudGltZW50JTIwb2YlMjB0aGlzJTIwcmV2aWV3JTIwaXMlMjBwb3NpdGl2ZSUyQyUyMG5lZ2F0aXZlJTIwb3IlMjBuZXV0cmFsJTIyJTJDJTBBJTIwJTIwJTIwJTIwdG9rZW5pemVyX25hbWVfb3JfcGF0aCUzRCUyMnQ1LWJhc2UlMjIlMkMlMEEpJTBBJTBBJTIzJTIwdDVfbW9kZWwuc2hhcmVkJTIwaXMlMjB0aGUlMjB3b3JkJTIwZW1iZWRkaW5ncyUyMG9mJTIwdGhlJTIwYmFzZSUyMG1vZGVsJTBBcHJvbXB0X2VtYmVkZGluZyUyMCUzRCUyMFByb21wdEVtYmVkZGluZyhjb25maWclMkMlMjB0NV9tb2RlbC5zaGFyZWQp",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> peft <span class="hljs-keyword">import</span> PromptEmbedding, PromptTuningConfig

<span class="hljs-meta">&gt;&gt;&gt; </span>config = PromptTuningConfig(
<span class="hljs-meta">... </span>    peft_type=<span class="hljs-string">&quot;PROMPT_TUNING&quot;</span>,
<span class="hljs-meta">... </span>    task_type=<span class="hljs-string">&quot;SEQ_2_SEQ_LM&quot;</span>,
<span class="hljs-meta">... </span>    num_virtual_tokens=<span class="hljs-number">20</span>,
<span class="hljs-meta">... </span>    token_dim=<span class="hljs-number">768</span>,
<span class="hljs-meta">... </span>    num_transformer_submodules=<span class="hljs-number">1</span>,
<span class="hljs-meta">... </span>    num_attention_heads=<span class="hljs-number">12</span>,
<span class="hljs-meta">... </span>    num_layers=<span class="hljs-number">12</span>,
<span class="hljs-meta">... </span>    prompt_tuning_init=<span class="hljs-string">&quot;TEXT&quot;</span>,
<span class="hljs-meta">... </span>    prompt_tuning_init_text=<span class="hljs-string">&quot;Predict if sentiment of this review is positive, negative or neutral&quot;</span>,
<span class="hljs-meta">... </span>    tokenizer_name_or_path=<span class="hljs-string">&quot;t5-base&quot;</span>,
<span class="hljs-meta">... </span>)

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-comment"># t5_model.shared is the word embeddings of the base model</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>prompt_embedding = PromptEmbedding(config, t5_model.shared)`,wrap:!1}}),{c(){p=m("p"),p.textContent=k,h=i(),w(g.$$.fragment)},l(o){p=l(o,"P",{"data-svelte-h":!0}),_(p)!=="svelte-11lpom8"&&(p.textContent=k),h=r(o),y(g.$$.fragment,o)},m(o,u){a(o,p,u),a(o,h,u),J(g,o,u),f=!0},p:we,i(o){f||(v(g.$$.fragment,o),f=!0)},o(o){M(g.$$.fragment,o),f=!1},d(o){o&&(n(p),n(h)),$(g,o)}}}function Ie(X){let p,k,h,g,f,o,u,pe='<a href="https://hf.co/papers/2104.08691" rel="nofollow">Prompt tuning</a> adds task-specific prompts to the input, and these prompt parameters are updated independently of the pretrained model parameters which are frozen.',O,P,me="The abstract from the paper is:",L,I,le="<em>In this work, we explore “prompt tuning”, a simple yet effective mechanism for learning “soft prompts” to condition frozen language models to perform specific downstream tasks. Unlike the discrete text prompts used by GPT-3, soft prompts are learned through backpropagation and can be tuned to incorporate signal from any number of labeled examples. Our end-to-end learned approach outperforms GPT-3’s “few-shot” learning by a large margin. More remarkably, through ablations on model size using T5, we show that prompt tuning becomes more competitive with scale: as models exceed billions of parameters, our method “closes the gap” and matches the strong performance of model tuning (where all model weights are tuned). This finding is especially relevant in that large models are costly to share and serve, and the ability to reuse one frozen model for multiple downstream tasks can ease this burden. Our method can be seen as a simplification of the recently proposed “prefix tuning” of Li and Liang (2021), and we provide a comparison to this and other similar approaches. Finally, we show that conditioning a frozen model with soft prompts confers benefits in robustness to domain transfer, as compared to full model tuning</em>.",R,U,D,T,C,ee,z,de='This is the configuration class to store the configuration of a <a href="/docs/peft/main/en/package_reference/prompt_tuning#peft.PromptEmbedding">PromptEmbedding</a>.',F,x,Q,s,j,te,V,ce="The model to encode virtual tokens into prompt embeddings.",ne,N,ge="<strong>Attributes</strong>:",ae,Z,fe="<li><strong>embedding</strong> (<code>torch.nn.Embedding</code>) — The embedding layer of the prompt embedding.</li>",oe,b,se,G,ue="Input Shape: (<code>batch_size</code>, <code>total_virtual_tokens</code>)",ie,H,he="Output Shape: (<code>batch_size</code>, <code>total_virtual_tokens</code>, <code>token_dim</code>)",S,E,q,W,Y;return f=new re({props:{title:"Prompt tuning",local:"prompt-tuning",headingTag:"h1"}}),U=new re({props:{title:"PromptTuningConfig",local:"peft.PromptTuningConfig",headingTag:"h2"}}),C=new _e({props:{name:"class peft.PromptTuningConfig",anchor:"peft.PromptTuningConfig",parameters:[{name:"task_type",val:": Optional[Union[str, TaskType]] = None"},{name:"peft_type",val:": Optional[Union[str, PeftType]] = None"},{name:"auto_mapping",val:": Optional[dict] = None"},{name:"peft_version",val:": Optional[str] = None"},{name:"base_model_name_or_path",val:": Optional[str] = None"},{name:"revision",val:": Optional[str] = None"},{name:"inference_mode",val:": bool = False"},{name:"num_virtual_tokens",val:": int = None"},{name:"token_dim",val:": int = None"},{name:"num_transformer_submodules",val:": Optional[int] = None"},{name:"num_attention_heads",val:": Optional[int] = None"},{name:"num_layers",val:": Optional[int] = None"},{name:"modules_to_save",val:": Optional[list[str]] = None"},{name:"prompt_tuning_init",val:": typing.Union[peft.tuners.prompt_tuning.config.PromptTuningInit, str] = <PromptTuningInit.RANDOM: 'RANDOM'>"},{name:"prompt_tuning_init_text",val:": typing.Optional[str] = None"},{name:"tokenizer_name_or_path",val:": typing.Optional[str] = None"},{name:"tokenizer_kwargs",val:": typing.Optional[dict] = None"}],parametersDescription:[{anchor:"peft.PromptTuningConfig.prompt_tuning_init",description:"<strong>prompt_tuning_init</strong> (Union[<code>PromptTuningInit</code>, <code>str</code>]) &#x2014; The initialization of the prompt embedding.",name:"prompt_tuning_init"},{anchor:"peft.PromptTuningConfig.prompt_tuning_init_text",description:`<strong>prompt_tuning_init_text</strong> (<code>str</code>, <em>optional</em>) &#x2014;
The text to initialize the prompt embedding. Only used if <code>prompt_tuning_init</code> is <code>TEXT</code>.`,name:"prompt_tuning_init_text"},{anchor:"peft.PromptTuningConfig.tokenizer_name_or_path",description:`<strong>tokenizer_name_or_path</strong> (<code>str</code>, <em>optional</em>) &#x2014;
The name or path of the tokenizer. Only used if <code>prompt_tuning_init</code> is <code>TEXT</code>.`,name:"tokenizer_name_or_path"},{anchor:"peft.PromptTuningConfig.tokenizer_kwargs",description:`<strong>tokenizer_kwargs</strong> (<code>dict</code>, <em>optional</em>) &#x2014;
The keyword arguments to pass to <code>AutoTokenizer.from_pretrained</code>. Only used if <code>prompt_tuning_init</code> is
<code>TEXT</code>.`,name:"tokenizer_kwargs"}],source:"https://github.com/huggingface/peft/blob/main/src/peft/tuners/prompt_tuning/config.py#L29"}}),x=new re({props:{title:"PromptEmbedding",local:"peft.PromptEmbedding",headingTag:"h2"}}),j=new _e({props:{name:"class peft.PromptEmbedding",anchor:"peft.PromptEmbedding",parameters:[{name:"config",val:""},{name:"word_embeddings",val:""}],parametersDescription:[{anchor:"peft.PromptEmbedding.config",description:'<strong>config</strong> (<a href="/docs/peft/main/en/package_reference/prompt_tuning#peft.PromptTuningConfig">PromptTuningConfig</a>) &#x2014; The configuration of the prompt embedding.',name:"config"},{anchor:"peft.PromptEmbedding.word_embeddings",description:"<strong>word_embeddings</strong> (<code>torch.nn.Module</code>) &#x2014; The word embeddings of the base transformer model.",name:"word_embeddings"}],source:"https://github.com/huggingface/peft/blob/main/src/peft/tuners/prompt_tuning/model.py#L24"}}),b=new $e({props:{anchor:"peft.PromptEmbedding.example",$$slots:{default:[Pe]},$$scope:{ctx:X}}}),E=new ke({props:{source:"https://github.com/huggingface/peft/blob/main/docs/source/package_reference/prompt_tuning.md"}}),{c(){p=m("meta"),k=i(),h=m("p"),g=i(),w(f.$$.fragment),o=i(),u=m("p"),u.innerHTML=pe,O=i(),P=m("p"),P.textContent=me,L=i(),I=m("p"),I.innerHTML=le,R=i(),w(U.$$.fragment),D=i(),T=m("div"),w(C.$$.fragment),ee=i(),z=m("p"),z.innerHTML=de,F=i(),w(x.$$.fragment),Q=i(),s=m("div"),w(j.$$.fragment),te=i(),V=m("p"),V.textContent=ce,ne=i(),N=m("p"),N.innerHTML=ge,ae=i(),Z=m("ul"),Z.innerHTML=fe,oe=i(),w(b.$$.fragment),se=i(),G=m("p"),G.innerHTML=ue,ie=i(),H=m("p"),H.innerHTML=he,S=i(),w(E.$$.fragment),q=i(),W=m("p"),this.h()},l(e){const t=ve("svelte-u9bgzb",document.head);p=l(t,"META",{name:!0,content:!0}),t.forEach(n),k=r(e),h=l(e,"P",{}),A(h).forEach(n),g=r(e),y(f.$$.fragment,e),o=r(e),u=l(e,"P",{"data-svelte-h":!0}),_(u)!=="svelte-4zhj2m"&&(u.innerHTML=pe),O=r(e),P=l(e,"P",{"data-svelte-h":!0}),_(P)!=="svelte-1cwsb16"&&(P.textContent=me),L=r(e),I=l(e,"P",{"data-svelte-h":!0}),_(I)!=="svelte-18zszvh"&&(I.innerHTML=le),R=r(e),y(U.$$.fragment,e),D=r(e),T=l(e,"DIV",{class:!0});var B=A(T);y(C.$$.fragment,B),ee=r(B),z=l(B,"P",{"data-svelte-h":!0}),_(z)!=="svelte-rd1z9p"&&(z.innerHTML=de),B.forEach(n),F=r(e),y(x.$$.fragment,e),Q=r(e),s=l(e,"DIV",{class:!0});var d=A(s);y(j.$$.fragment,d),te=r(d),V=l(d,"P",{"data-svelte-h":!0}),_(V)!=="svelte-1sa5ela"&&(V.textContent=ce),ne=r(d),N=l(d,"P",{"data-svelte-h":!0}),_(N)!=="svelte-1xx6nm4"&&(N.innerHTML=ge),ae=r(d),Z=l(d,"UL",{"data-svelte-h":!0}),_(Z)!=="svelte-1nc06m8"&&(Z.innerHTML=fe),oe=r(d),y(b.$$.fragment,d),se=r(d),G=l(d,"P",{"data-svelte-h":!0}),_(G)!=="svelte-16nez6j"&&(G.innerHTML=ue),ie=r(d),H=l(d,"P",{"data-svelte-h":!0}),_(H)!=="svelte-cqt1it"&&(H.innerHTML=he),d.forEach(n),S=r(e),y(E.$$.fragment,e),q=r(e),W=l(e,"P",{}),A(W).forEach(n),this.h()},h(){K(p,"name","hf:doc:metadata"),K(p,"content",Ue),K(T,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),K(s,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8")},m(e,t){c(document.head,p),a(e,k,t),a(e,h,t),a(e,g,t),J(f,e,t),a(e,o,t),a(e,u,t),a(e,O,t),a(e,P,t),a(e,L,t),a(e,I,t),a(e,R,t),J(U,e,t),a(e,D,t),a(e,T,t),J(C,T,null),c(T,ee),c(T,z),a(e,F,t),J(x,e,t),a(e,Q,t),a(e,s,t),J(j,s,null),c(s,te),c(s,V),c(s,ne),c(s,N),c(s,ae),c(s,Z),c(s,oe),J(b,s,null),c(s,se),c(s,G),c(s,ie),c(s,H),a(e,S,t),J(E,e,t),a(e,q,t),a(e,W,t),Y=!0},p(e,[t]){const B={};t&2&&(B.$$scope={dirty:t,ctx:e}),b.$set(B)},i(e){Y||(v(f.$$.fragment,e),v(U.$$.fragment,e),v(C.$$.fragment,e),v(x.$$.fragment,e),v(j.$$.fragment,e),v(b.$$.fragment,e),v(E.$$.fragment,e),Y=!0)},o(e){M(f.$$.fragment,e),M(U.$$.fragment,e),M(C.$$.fragment,e),M(x.$$.fragment,e),M(j.$$.fragment,e),M(b.$$.fragment,e),M(E.$$.fragment,e),Y=!1},d(e){e&&(n(k),n(h),n(g),n(o),n(u),n(O),n(P),n(L),n(I),n(R),n(D),n(T),n(F),n(Q),n(s),n(S),n(q),n(W)),n(p),$(f,e),$(U,e),$(C),$(x,e),$(j),$(b),$(E,e)}}}const Ue='{"title":"Prompt tuning","local":"prompt-tuning","sections":[{"title":"PromptTuningConfig","local":"peft.PromptTuningConfig","sections":[],"depth":2},{"title":"PromptEmbedding","local":"peft.PromptEmbedding","sections":[],"depth":2}],"depth":1}';function Ce(X){return be(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ne extends ye{constructor(p){super(),Je(this,p,Ce,Ie,Te,{})}}export{Ne as component};
