import{s as Me,o as ve,n as $e}from"../chunks/scheduler.78382b47.js";import{S as Te,i as Pe,e as l,s as r,c as y,h as Ee,a as m,d as n,b as a,f as ee,g as M,j as _,k as te,l as d,m as o,n as v,t as $,o as T,p as P}from"../chunks/index.6dd35eb6.js";import{C as Ce,H as le,E as Ue}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.fed9dfe4.js";import{D as ye}from"../chunks/Docstring.32e826ec.js";import{C as we}from"../chunks/CodeBlock.af1ec02e.js";import{E as je}from"../chunks/ExampleCodeBlock.7f184f2e.js";function ke(I){let p,C="Example:",g,f,u;return f=new we({props:{code:"ZnJvbSUyMHBlZnQlMjBpbXBvcnQlMjBQcm9tcHRFbmNvZGVyJTJDJTIwUHJvbXB0RW5jb2RlckNvbmZpZyUwQSUwQWNvbmZpZyUyMCUzRCUyMFByb21wdEVuY29kZXJDb25maWcoJTBBJTIwJTIwJTIwJTIwcGVmdF90eXBlJTNEJTIyUF9UVU5JTkclMjIlMkMlMEElMjAlMjAlMjAlMjB0YXNrX3R5cGUlM0QlMjJTRVFfMl9TRVFfTE0lMjIlMkMlMEElMjAlMjAlMjAlMjBudW1fdmlydHVhbF90b2tlbnMlM0QyMCUyQyUwQSUyMCUyMCUyMCUyMHRva2VuX2RpbSUzRDc2OCUyQyUwQSUyMCUyMCUyMCUyMG51bV90cmFuc2Zvcm1lcl9zdWJtb2R1bGVzJTNEMSUyQyUwQSUyMCUyMCUyMCUyMG51bV9hdHRlbnRpb25faGVhZHMlM0QxMiUyQyUwQSUyMCUyMCUyMCUyMG51bV9sYXllcnMlM0QxMiUyQyUwQSUyMCUyMCUyMCUyMGVuY29kZXJfcmVwYXJhbWV0ZXJpemF0aW9uX3R5cGUlM0QlMjJNTFAlMjIlMkMlMEElMjAlMjAlMjAlMjBlbmNvZGVyX2hpZGRlbl9zaXplJTNENzY4JTJDJTBBKSUwQSUwQXByb21wdF9lbmNvZGVyJTIwJTNEJTIwUHJvbXB0RW5jb2Rlcihjb25maWcp",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> peft <span class="hljs-keyword">import</span> PromptEncoder, PromptEncoderConfig

<span class="hljs-meta">&gt;&gt;&gt; </span>config = PromptEncoderConfig(
<span class="hljs-meta">... </span>    peft_type=<span class="hljs-string">&quot;P_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>    encoder_reparameterization_type=<span class="hljs-string">&quot;MLP&quot;</span>,
<span class="hljs-meta">... </span>    encoder_hidden_size=<span class="hljs-number">768</span>,
<span class="hljs-meta">... </span>)

<span class="hljs-meta">&gt;&gt;&gt; </span>prompt_encoder = PromptEncoder(config)`,wrap:!1}}),{c(){p=l("p"),p.textContent=C,g=r(),y(f.$$.fragment)},l(s){p=m(s,"P",{"data-svelte-h":!0}),_(p)!=="svelte-11lpom8"&&(p.textContent=C),g=a(s),M(f.$$.fragment,s)},m(s,h){o(s,p,h),o(s,g,h),v(f,s,h),u=!0},p:$e,i(s){u||($(f.$$.fragment,s),u=!0)},o(s){T(f.$$.fragment,s),u=!1},d(s){s&&(n(p),n(g)),P(f,s)}}}function xe(I){let p,C,g,f,u,s,h,X,U,me='<a href="https://hf.co/papers/2103.10385" rel="nofollow">P-tuning</a> adds trainable prompt embeddings to the input that is optimized by a prompt encoder to find a better prompt, eliminating the need to manually design prompts. The prompt tokens can be added anywhere in the input sequence, and p-tuning also introduces anchor tokens for improving performance.',O,w,ce="The abstract from the paper is:",Z,j,de="<em>While GPTs with traditional fine-tuning fail to achieve strong results on natural language understanding (NLU), we show that GPTs can be better than or comparable to similar-sized BERTs on NLU tasks with a novel method P-tuning — which employs trainable continuous prompt embeddings. On the knowledge probing (LAMA) benchmark, the best GPT recovers 64\\% (P@1) of world knowledge without any additional text provided during test time, which substantially improves the previous best by 20+ percentage points. On the SuperGlue benchmark, GPTs achieve comparable and sometimes better performance to similar-sized BERTs in supervised learning. Importantly, we find that P-tuning also improves BERTs’ performance in both few-shot and supervised settings while largely reducing the need for prompt engineering. Consequently, P-tuning outperforms the state-of-the-art approaches on the few-shot SuperGlue benchmark.</em>.",A,k,F,b,x,ne,R,fe='This is the configuration class to store the configuration of a <a href="/docs/peft/main/en/package_reference/p_tuning#peft.PromptEncoder">PromptEncoder</a>.',q,z,D,i,L,oe,H,ue="The prompt encoder network that is used to generate the virtual token embeddings for p-tuning.",re,E,ae,Q,he="<strong>Attributes</strong>:",se,S,ge=`<li><strong>embedding</strong> (<code>torch.nn.Embedding</code>) — The embedding layer of the prompt encoder.</li> <li><strong>mlp_head</strong> (<code>torch.nn.Sequential</code>) — The MLP head of the prompt encoder if <code>inference_mode=False</code>.</li> <li><strong>lstm_head</strong> (<code>torch.nn.LSTM</code>) — The LSTM head of the prompt encoder if <code>inference_mode=False</code> and
<code>encoder_reparameterization_type=&quot;LSTM&quot;</code>.</li> <li><strong>token_dim</strong> (<code>int</code>) — The hidden embedding dimension of the base transformer model.</li> <li><strong>input_size</strong> (<code>int</code>) — The input size of the prompt encoder.</li> <li><strong>output_size</strong> (<code>int</code>) — The output size of the prompt encoder.</li> <li><strong>hidden_size</strong> (<code>int</code>) — The hidden size of the prompt encoder.</li> <li><strong>total_virtual_tokens</strong> (<code>int</code>): The total number of virtual tokens of the
prompt encoder.</li> <li><strong>encoder_type</strong> (Union[<code>PromptEncoderReparameterizationType</code>, <code>str</code>]): The encoder type of the prompt
encoder.</li>`,ie,B,_e="Input shape: (<code>batch_size</code>, <code>total_virtual_tokens</code>)",pe,G,be="Output shape: (<code>batch_size</code>, <code>total_virtual_tokens</code>, <code>token_dim</code>)",W,J,Y,V,K;return u=new Ce({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),h=new le({props:{title:"P-tuning",local:"p-tuning",headingTag:"h1"}}),k=new le({props:{title:"PromptEncoderConfig",local:"peft.PromptEncoderConfig",headingTag:"h2"}}),x=new ye({props:{name:"class peft.PromptEncoderConfig",anchor:"peft.PromptEncoderConfig",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:"encoder_reparameterization_type",val:": typing.Union[str, peft.tuners.p_tuning.config.PromptEncoderReparameterizationType] = <PromptEncoderReparameterizationType.MLP: 'MLP'>"},{name:"encoder_hidden_size",val:": int = None"},{name:"encoder_num_layers",val:": int = 2"},{name:"encoder_dropout",val:": float = 0.0"}],parametersDescription:[{anchor:"peft.PromptEncoderConfig.encoder_reparameterization_type",description:`<strong>encoder_reparameterization_type</strong> (Union[<code>PromptEncoderReparameterizationType</code>, <code>str</code>]) &#x2014;
The type of reparameterization to use.`,name:"encoder_reparameterization_type"},{anchor:"peft.PromptEncoderConfig.encoder_hidden_size",description:"<strong>encoder_hidden_size</strong> (<code>int</code>) &#x2014; The hidden size of the prompt encoder.",name:"encoder_hidden_size"},{anchor:"peft.PromptEncoderConfig.encoder_num_layers",description:"<strong>encoder_num_layers</strong> (<code>int</code>) &#x2014; The number of layers of the prompt encoder.",name:"encoder_num_layers"},{anchor:"peft.PromptEncoderConfig.encoder_dropout",description:"<strong>encoder_dropout</strong> (<code>float</code>) &#x2014; The dropout probability of the prompt encoder.",name:"encoder_dropout"}],source:"https://github.com/huggingface/peft/blob/main/src/peft/tuners/p_tuning/config.py#L29"}}),z=new le({props:{title:"PromptEncoder",local:"peft.PromptEncoder",headingTag:"h2"}}),L=new ye({props:{name:"class peft.PromptEncoder",anchor:"peft.PromptEncoder",parameters:[{name:"config",val:""}],parametersDescription:[{anchor:"peft.PromptEncoder.config",description:'<strong>config</strong> (<a href="/docs/peft/main/en/package_reference/p_tuning#peft.PromptEncoderConfig">PromptEncoderConfig</a>) &#x2014; The configuration of the prompt encoder.',name:"config"}],source:"https://github.com/huggingface/peft/blob/main/src/peft/tuners/p_tuning/model.py#L24"}}),E=new je({props:{anchor:"peft.PromptEncoder.example",$$slots:{default:[ke]},$$scope:{ctx:I}}}),J=new Ue({props:{source:"https://github.com/huggingface/peft/blob/main/docs/source/package_reference/p_tuning.md"}}),{c(){p=l("meta"),C=r(),g=l("p"),f=r(),y(u.$$.fragment),s=r(),y(h.$$.fragment),X=r(),U=l("p"),U.innerHTML=me,O=r(),w=l("p"),w.textContent=ce,Z=r(),j=l("p"),j.innerHTML=de,A=r(),y(k.$$.fragment),F=r(),b=l("div"),y(x.$$.fragment),ne=r(),R=l("p"),R.innerHTML=fe,q=r(),y(z.$$.fragment),D=r(),i=l("div"),y(L.$$.fragment),oe=r(),H=l("p"),H.textContent=ue,re=r(),y(E.$$.fragment),ae=r(),Q=l("p"),Q.innerHTML=he,se=r(),S=l("ul"),S.innerHTML=ge,ie=r(),B=l("p"),B.innerHTML=_e,pe=r(),G=l("p"),G.innerHTML=be,W=r(),y(J.$$.fragment),Y=r(),V=l("p"),this.h()},l(e){const t=Ee("svelte-u9bgzb",document.head);p=m(t,"META",{name:!0,content:!0}),t.forEach(n),C=a(e),g=m(e,"P",{}),ee(g).forEach(n),f=a(e),M(u.$$.fragment,e),s=a(e),M(h.$$.fragment,e),X=a(e),U=m(e,"P",{"data-svelte-h":!0}),_(U)!=="svelte-zvad53"&&(U.innerHTML=me),O=a(e),w=m(e,"P",{"data-svelte-h":!0}),_(w)!=="svelte-1cwsb16"&&(w.textContent=ce),Z=a(e),j=m(e,"P",{"data-svelte-h":!0}),_(j)!=="svelte-131yaps"&&(j.innerHTML=de),A=a(e),M(k.$$.fragment,e),F=a(e),b=m(e,"DIV",{class:!0});var N=ee(b);M(x.$$.fragment,N),ne=a(N),R=m(N,"P",{"data-svelte-h":!0}),_(R)!=="svelte-b0lt2j"&&(R.innerHTML=fe),N.forEach(n),q=a(e),M(z.$$.fragment,e),D=a(e),i=m(e,"DIV",{class:!0});var c=ee(i);M(L.$$.fragment,c),oe=a(c),H=m(c,"P",{"data-svelte-h":!0}),_(H)!=="svelte-2xuoj1"&&(H.textContent=ue),re=a(c),M(E.$$.fragment,c),ae=a(c),Q=m(c,"P",{"data-svelte-h":!0}),_(Q)!=="svelte-1xx6nm4"&&(Q.innerHTML=he),se=a(c),S=m(c,"UL",{"data-svelte-h":!0}),_(S)!=="svelte-17fn2on"&&(S.innerHTML=ge),ie=a(c),B=m(c,"P",{"data-svelte-h":!0}),_(B)!=="svelte-b3kmuz"&&(B.innerHTML=_e),pe=a(c),G=m(c,"P",{"data-svelte-h":!0}),_(G)!=="svelte-17eznf9"&&(G.innerHTML=be),c.forEach(n),W=a(e),M(J.$$.fragment,e),Y=a(e),V=m(e,"P",{}),ee(V).forEach(n),this.h()},h(){te(p,"name","hf:doc:metadata"),te(p,"content",ze),te(b,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),te(i,"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){d(document.head,p),o(e,C,t),o(e,g,t),o(e,f,t),v(u,e,t),o(e,s,t),v(h,e,t),o(e,X,t),o(e,U,t),o(e,O,t),o(e,w,t),o(e,Z,t),o(e,j,t),o(e,A,t),v(k,e,t),o(e,F,t),o(e,b,t),v(x,b,null),d(b,ne),d(b,R),o(e,q,t),v(z,e,t),o(e,D,t),o(e,i,t),v(L,i,null),d(i,oe),d(i,H),d(i,re),v(E,i,null),d(i,ae),d(i,Q),d(i,se),d(i,S),d(i,ie),d(i,B),d(i,pe),d(i,G),o(e,W,t),v(J,e,t),o(e,Y,t),o(e,V,t),K=!0},p(e,[t]){const N={};t&2&&(N.$$scope={dirty:t,ctx:e}),E.$set(N)},i(e){K||($(u.$$.fragment,e),$(h.$$.fragment,e),$(k.$$.fragment,e),$(x.$$.fragment,e),$(z.$$.fragment,e),$(L.$$.fragment,e),$(E.$$.fragment,e),$(J.$$.fragment,e),K=!0)},o(e){T(u.$$.fragment,e),T(h.$$.fragment,e),T(k.$$.fragment,e),T(x.$$.fragment,e),T(z.$$.fragment,e),T(L.$$.fragment,e),T(E.$$.fragment,e),T(J.$$.fragment,e),K=!1},d(e){e&&(n(C),n(g),n(f),n(s),n(X),n(U),n(O),n(w),n(Z),n(j),n(A),n(F),n(b),n(q),n(D),n(i),n(W),n(Y),n(V)),n(p),P(u,e),P(h,e),P(k,e),P(x),P(z,e),P(L),P(E),P(J,e)}}}const ze='{"title":"P-tuning","local":"p-tuning","sections":[{"title":"PromptEncoderConfig","local":"peft.PromptEncoderConfig","sections":[],"depth":2},{"title":"PromptEncoder","local":"peft.PromptEncoder","sections":[],"depth":2}],"depth":1}';function Le(I){return ve(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Be extends Te{constructor(p){super(),Pe(this,p,Le,xe,Me,{})}}export{Be as component};
