import{s as Bt,n as Kt,o as Qt}from"../chunks/scheduler.78382b47.js";import{S as Jt,i as Wt,e as r,s as i,c as g,h as Xt,a as s,d as e,b as o,f as P,g as f,j as d,k as w,l as a,m as l,n as u,t as h,o as b,p as v}from"../chunks/index.6dd35eb6.js";import{C as Yt,H as Mt,E as Zt}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.fed9dfe4.js";import{D as X}from"../chunks/Docstring.32e826ec.js";function te(It){let C,tt,Y,et,k,nt,L,at,E,zt='<a href="https://huggingface.co/papers/2410.17222" rel="nofollow">CPT</a> combines In-Context Learning (ICL), Prompt Tuning (PT), and adversarial optimization to improve few-shot learning by refining context embeddings. CPT updates the context tokens by optimizing both the context and the training examples, encapsulating them into a novel loss design that minimizes overfitting, enables more effective optimization, and drives significant improvements in classification tasks.',it,O,Nt="The abstract from the paper is:",ot,M,jt="<p>Large Language Models (LLMs) can perform few-shot learning using either optimization-based approaches or In-Context Learning (ICL). Optimization-based methods often suffer from overfitting, as they require updating a large number of parameters with limited data. In contrast, ICL avoids overfitting but typically underperforms compared to optimization-based methods and is highly sensitive to the selection, order, and format of demonstration examples. To overcome these challenges, we introduce Context-aware Prompt Tuning (CPT), a method inspired by ICL, Prompt Tuning (PT), and adversarial attacks. CPT builds on the ICL strategy of concatenating examples before the input, extending it by incorporating PT-like learning to refine the context embedding through iterative optimization, extracting deeper insights from the training examples. Our approach carefully modifies specific context tokens, considering the unique structure of the examples within the context. In addition to updating the context with PT-like optimization, CPT draws inspiration from adversarial attacks, adjusting the input based on the labels present in the context while preserving the inherent value of the user-provided data. To ensure robustness and stability during optimization, we employ a projected gradient descent algorithm, constraining token embeddings to remain close to their original values and safeguarding the quality of the context. Our method has demonstrated superior accuracy across multiple classification tasks using various LLM models, outperforming existing baselines and effectively addressing the overfitting challenge in few-shot learning.</p>",rt,I,Dt='Take a look at <a href="https://github.com/huggingface/peft/blob/main/examples/cpt_finetuning/README.md" rel="nofollow">Example</a> for a step-by-step guide on how to train a model with CPT.',st,z,lt,m,N,vt,U,Ht="CPT Configuration class extending PeftConfig for Context-aware Prompt Tuning (CPT).",_t,F,qt="This class introduces additional parameters required for CPT, such as:",Ct,G,At="<li>Token type masks</li> <li>Prompt tuning initialization</li> <li>Loss weighting</li> <li>Projection settings</li>",xt,R,St='For more details, see the paper: <a href="https://huggingface.co/papers/2410.17222" rel="nofollow">https://huggingface.co/papers/2410.17222</a>',pt,j,mt,p,D,$t,B,Vt=`CPTEmbedding is a custom embedding layer designed for Context-aware Prompt Tuning (CPT) in PEFT. It initializes
embeddings, applies prompt-specific projections, and computes loss using label masks.`,Tt,x,H,yt,K,Ut="Computes the loss for CPT models with optional exponential decay.",Pt,$,q,wt,Q,Ft="Computes the prompt embeddings and applies delta adjustments.",kt,T,A,Lt,J,Gt="Applies epsilon-based projection to the delta embeddings to control their norm.",Et,y,S,Ot,W,Rt="Sets up a backward hook to selectively update token gradients based on the CPT token type mask.",dt,V,ct,Z,gt;return k=new Yt({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),L=new Mt({props:{title:"Context-aware Prompt Tuning: Advancing In-Context Learning with Adversarial Methods",local:"context-aware-prompt-tuning-advancing-in-context-learning-with-adversarial-methods",headingTag:"h1"}}),z=new Mt({props:{title:"CPTConfig",local:"peft.CPTConfig",headingTag:"h2"}}),N=new X({props:{name:"class peft.CPTConfig",anchor:"peft.CPTConfig",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:"cpt_token_ids",val:": typing.Optional[list[int]] = None"},{name:"cpt_mask",val:": typing.Optional[list[int]] = None"},{name:"cpt_tokens_type_mask",val:": typing.Optional[list[int]] = None"},{name:"opt_weighted_loss_type",val:": typing.Optional[typing.Literal['none', 'decay']] = 'none'"},{name:"opt_loss_decay_factor",val:": typing.Optional[float] = 1.0"},{name:"opt_projection_epsilon",val:": typing.Optional[float] = 0.1"},{name:"opt_projection_format_epsilon",val:": typing.Optional[float] = 0.1"},{name:"tokenizer_name_or_path",val:": typing.Optional[str] = None"}],source:"https://github.com/huggingface/peft/blob/main/src/peft/tuners/cpt/config.py#L23"}}),j=new Mt({props:{title:"CPTEmbedding",local:"peft.CPTEmbedding",headingTag:"h2"}}),D=new X({props:{name:"class peft.CPTEmbedding",anchor:"peft.CPTEmbedding",parameters:[{name:"config",val:""},{name:"word_embeddings",val:""}],source:"https://github.com/huggingface/peft/blob/main/src/peft/tuners/cpt/model.py#L23"}}),H=new X({props:{name:"calculate_loss",anchor:"peft.CPTEmbedding.calculate_loss",parameters:[{name:"base_model_output",val:""},{name:"labels",val:""},{name:"cpt_type_mask",val:""},{name:"config",val:""}],parametersDescription:[{anchor:"peft.CPTEmbedding.calculate_loss.base_model_output",description:`<strong>base_model_output</strong> (ModelOutput) &#x2014;
Output from the base model containing logits.`,name:"base_model_output"},{anchor:"peft.CPTEmbedding.calculate_loss.labels",description:`<strong>labels</strong> (torch.Tensor) &#x2014;
Ground-truth labels for the input tokens.`,name:"labels"},{anchor:"peft.CPTEmbedding.calculate_loss.cpt_type_mask",description:`<strong>cpt_type_mask</strong> (torch.Tensor) &#x2014;
Token type mask used for filtering valid loss terms.`,name:"cpt_type_mask"},{anchor:"peft.CPTEmbedding.calculate_loss.config",description:`<strong>config</strong> (Namespace) &#x2014;
Configuration object containing loss-related hyperparameters.`,name:"config"}],source:"https://github.com/huggingface/peft/blob/main/src/peft/tuners/cpt/model.py#L143",returnDescription:`<script context="module">export const metadata = 'undefined';<\/script>


<p>The base model output with computed loss.</p>
`,returnType:`<script context="module">export const metadata = 'undefined';<\/script>


<p>ModelOutput</p>
`}}),q=new X({props:{name:"forward",anchor:"peft.CPTEmbedding.forward",parameters:[{name:"indices",val:""}],parametersDescription:[{anchor:"peft.CPTEmbedding.forward.indices",description:`<strong>indices</strong> (torch.Tensor) &#x2014;
Indices of the tokens to be embedded.`,name:"indices"}],source:"https://github.com/huggingface/peft/blob/main/src/peft/tuners/cpt/model.py#L65",returnDescription:`<script context="module">export const metadata = 'undefined';<\/script>


<p>Sum of prompt embeddings and delta embeddings.</p>
`,returnType:`<script context="module">export const metadata = 'undefined';<\/script>


<p>torch.Tensor</p>
`}}),A=new X({props:{name:"get_projection",anchor:"peft.CPTEmbedding.get_projection",parameters:[],source:"https://github.com/huggingface/peft/blob/main/src/peft/tuners/cpt/model.py#L125"}}),S=new X({props:{name:"set_updated_tokens",anchor:"peft.CPTEmbedding.set_updated_tokens",parameters:[],source:"https://github.com/huggingface/peft/blob/main/src/peft/tuners/cpt/model.py#L86"}}),V=new Zt({props:{source:"https://github.com/huggingface/peft/blob/main/docs/source/package_reference/cpt.md"}}),{c(){C=r("meta"),tt=i(),Y=r("p"),et=i(),g(k.$$.fragment),nt=i(),g(L.$$.fragment),at=i(),E=r("p"),E.innerHTML=zt,it=i(),O=r("p"),O.textContent=Nt,ot=i(),M=r("blockquote"),M.innerHTML=jt,rt=i(),I=r("p"),I.innerHTML=Dt,st=i(),g(z.$$.fragment),lt=i(),m=r("div"),g(N.$$.fragment),vt=i(),U=r("p"),U.textContent=Ht,_t=i(),F=r("p"),F.textContent=qt,Ct=i(),G=r("ul"),G.innerHTML=At,xt=i(),R=r("p"),R.innerHTML=St,pt=i(),g(j.$$.fragment),mt=i(),p=r("div"),g(D.$$.fragment),$t=i(),B=r("p"),B.textContent=Vt,Tt=i(),x=r("div"),g(H.$$.fragment),yt=i(),K=r("p"),K.textContent=Ut,Pt=i(),$=r("div"),g(q.$$.fragment),wt=i(),Q=r("p"),Q.textContent=Ft,kt=i(),T=r("div"),g(A.$$.fragment),Lt=i(),J=r("p"),J.textContent=Gt,Et=i(),y=r("div"),g(S.$$.fragment),Ot=i(),W=r("p"),W.textContent=Rt,dt=i(),g(V.$$.fragment),ct=i(),Z=r("p"),this.h()},l(t){const n=Xt("svelte-u9bgzb",document.head);C=s(n,"META",{name:!0,content:!0}),n.forEach(e),tt=o(t),Y=s(t,"P",{}),P(Y).forEach(e),et=o(t),f(k.$$.fragment,t),nt=o(t),f(L.$$.fragment,t),at=o(t),E=s(t,"P",{"data-svelte-h":!0}),d(E)!=="svelte-nhd3j4"&&(E.innerHTML=zt),it=o(t),O=s(t,"P",{"data-svelte-h":!0}),d(O)!=="svelte-1cwsb16"&&(O.textContent=Nt),ot=o(t),M=s(t,"BLOCKQUOTE",{"data-svelte-h":!0}),d(M)!=="svelte-166ut3c"&&(M.innerHTML=jt),rt=o(t),I=s(t,"P",{"data-svelte-h":!0}),d(I)!=="svelte-ih8qk1"&&(I.innerHTML=Dt),st=o(t),f(z.$$.fragment,t),lt=o(t),m=s(t,"DIV",{class:!0});var _=P(m);f(N.$$.fragment,_),vt=o(_),U=s(_,"P",{"data-svelte-h":!0}),d(U)!=="svelte-pvpmqk"&&(U.textContent=Ht),_t=o(_),F=s(_,"P",{"data-svelte-h":!0}),d(F)!=="svelte-jwcs2b"&&(F.textContent=qt),Ct=o(_),G=s(_,"UL",{"data-svelte-h":!0}),d(G)!=="svelte-aqudvo"&&(G.innerHTML=At),xt=o(_),R=s(_,"P",{"data-svelte-h":!0}),d(R)!=="svelte-1rfly87"&&(R.innerHTML=St),_.forEach(e),pt=o(t),f(j.$$.fragment,t),mt=o(t),p=s(t,"DIV",{class:!0});var c=P(p);f(D.$$.fragment,c),$t=o(c),B=s(c,"P",{"data-svelte-h":!0}),d(B)!=="svelte-1cdhmyj"&&(B.textContent=Vt),Tt=o(c),x=s(c,"DIV",{class:!0});var ft=P(x);f(H.$$.fragment,ft),yt=o(ft),K=s(ft,"P",{"data-svelte-h":!0}),d(K)!=="svelte-9bizft"&&(K.textContent=Ut),ft.forEach(e),Pt=o(c),$=s(c,"DIV",{class:!0});var ut=P($);f(q.$$.fragment,ut),wt=o(ut),Q=s(ut,"P",{"data-svelte-h":!0}),d(Q)!=="svelte-3v5t0c"&&(Q.textContent=Ft),ut.forEach(e),kt=o(c),T=s(c,"DIV",{class:!0});var ht=P(T);f(A.$$.fragment,ht),Lt=o(ht),J=s(ht,"P",{"data-svelte-h":!0}),d(J)!=="svelte-pg5azd"&&(J.textContent=Gt),ht.forEach(e),Et=o(c),y=s(c,"DIV",{class:!0});var bt=P(y);f(S.$$.fragment,bt),Ot=o(bt),W=s(bt,"P",{"data-svelte-h":!0}),d(W)!=="svelte-1bcpx1v"&&(W.textContent=Rt),bt.forEach(e),c.forEach(e),dt=o(t),f(V.$$.fragment,t),ct=o(t),Z=s(t,"P",{}),P(Z).forEach(e),this.h()},h(){w(C,"name","hf:doc:metadata"),w(C,"content",ee),w(m,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),w(x,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),w($,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),w(T,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),w(y,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),w(p,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8")},m(t,n){a(document.head,C),l(t,tt,n),l(t,Y,n),l(t,et,n),u(k,t,n),l(t,nt,n),u(L,t,n),l(t,at,n),l(t,E,n),l(t,it,n),l(t,O,n),l(t,ot,n),l(t,M,n),l(t,rt,n),l(t,I,n),l(t,st,n),u(z,t,n),l(t,lt,n),l(t,m,n),u(N,m,null),a(m,vt),a(m,U),a(m,_t),a(m,F),a(m,Ct),a(m,G),a(m,xt),a(m,R),l(t,pt,n),u(j,t,n),l(t,mt,n),l(t,p,n),u(D,p,null),a(p,$t),a(p,B),a(p,Tt),a(p,x),u(H,x,null),a(x,yt),a(x,K),a(p,Pt),a(p,$),u(q,$,null),a($,wt),a($,Q),a(p,kt),a(p,T),u(A,T,null),a(T,Lt),a(T,J),a(p,Et),a(p,y),u(S,y,null),a(y,Ot),a(y,W),l(t,dt,n),u(V,t,n),l(t,ct,n),l(t,Z,n),gt=!0},p:Kt,i(t){gt||(h(k.$$.fragment,t),h(L.$$.fragment,t),h(z.$$.fragment,t),h(N.$$.fragment,t),h(j.$$.fragment,t),h(D.$$.fragment,t),h(H.$$.fragment,t),h(q.$$.fragment,t),h(A.$$.fragment,t),h(S.$$.fragment,t),h(V.$$.fragment,t),gt=!0)},o(t){b(k.$$.fragment,t),b(L.$$.fragment,t),b(z.$$.fragment,t),b(N.$$.fragment,t),b(j.$$.fragment,t),b(D.$$.fragment,t),b(H.$$.fragment,t),b(q.$$.fragment,t),b(A.$$.fragment,t),b(S.$$.fragment,t),b(V.$$.fragment,t),gt=!1},d(t){t&&(e(tt),e(Y),e(et),e(nt),e(at),e(E),e(it),e(O),e(ot),e(M),e(rt),e(I),e(st),e(lt),e(m),e(pt),e(mt),e(p),e(dt),e(ct),e(Z)),e(C),v(k,t),v(L,t),v(z,t),v(N),v(j,t),v(D),v(H),v(q),v(A),v(S),v(V,t)}}}const ee='{"title":"Context-aware Prompt Tuning: Advancing In-Context Learning with Adversarial Methods","local":"context-aware-prompt-tuning-advancing-in-context-learning-with-adversarial-methods","sections":[{"title":"CPTConfig","local":"peft.CPTConfig","sections":[],"depth":2},{"title":"CPTEmbedding","local":"peft.CPTEmbedding","sections":[],"depth":2}],"depth":1}';function ne(It){return Qt(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class se extends Jt{constructor(C){super(),Wt(this,C,ne,te,Bt,{})}}export{se as component};
