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 o,c as g,h as Xt,a as s,d as e,b as i,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 v,p as b}from"../chunks/index.6dd35eb6.js";import{C as Yt,H as Mt,E as Zt}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.8d7e60c0.js";import{D as X}from"../chunks/Docstring.57cb1354.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.',ot,O,Nt="The abstract from the paper is:",it,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,bt,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/v0.18.0.rc0/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/v0.18.0.rc0/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/v0.18.0.rc0/src/peft/tuners/cpt/model.py#L141",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/v0.18.0.rc0/src/peft/tuners/cpt/model.py#L63",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/v0.18.0.rc0/src/peft/tuners/cpt/model.py#L123"}}),S=new X({props:{name:"set_updated_tokens",anchor:"peft.CPTEmbedding.set_updated_tokens",parameters:[],source:"https://github.com/huggingface/peft/blob/v0.18.0.rc0/src/peft/tuners/cpt/model.py#L84"}}),V=new Zt({props:{source:"https://github.com/huggingface/peft/blob/main/docs/source/package_reference/cpt.md"}}),{c(){C=r("meta"),tt=o(),Y=r("p"),et=o(),g(k.$$.fragment),nt=o(),g(L.$$.fragment),at=o(),E=r("p"),E.innerHTML=zt,ot=o(),O=r("p"),O.textContent=Nt,it=o(),M=r("blockquote"),M.innerHTML=jt,rt=o(),I=r("p"),I.innerHTML=Dt,st=o(),g(z.$$.fragment),lt=o(),m=r("div"),g(N.$$.fragment),bt=o(),U=r("p"),U.textContent=Ht,_t=o(),F=r("p"),F.textContent=qt,Ct=o(),G=r("ul"),G.innerHTML=At,xt=o(),R=r("p"),R.innerHTML=St,pt=o(),g(j.$$.fragment),mt=o(),p=r("div"),g(D.$$.fragment),$t=o(),B=r("p"),B.textContent=Vt,Tt=o(),x=r("div"),g(H.$$.fragment),yt=o(),K=r("p"),K.textContent=Ut,Pt=o(),$=r("div"),g(q.$$.fragment),wt=o(),Q=r("p"),Q.textContent=Ft,kt=o(),T=r("div"),g(A.$$.fragment),Lt=o(),J=r("p"),J.textContent=Gt,Et=o(),y=r("div"),g(S.$$.fragment),Ot=o(),W=r("p"),W.textContent=Rt,dt=o(),g(V.$$.fragment),ct=o(),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=i(t),Y=s(t,"P",{}),P(Y).forEach(e),et=i(t),f(k.$$.fragment,t),nt=i(t),f(L.$$.fragment,t),at=i(t),E=s(t,"P",{"data-svelte-h":!0}),d(E)!=="svelte-nhd3j4"&&(E.innerHTML=zt),ot=i(t),O=s(t,"P",{"data-svelte-h":!0}),d(O)!=="svelte-1cwsb16"&&(O.textContent=Nt),it=i(t),M=s(t,"BLOCKQUOTE",{"data-svelte-h":!0}),d(M)!=="svelte-166ut3c"&&(M.innerHTML=jt),rt=i(t),I=s(t,"P",{"data-svelte-h":!0}),d(I)!=="svelte-ih8qk1"&&(I.innerHTML=Dt),st=i(t),f(z.$$.fragment,t),lt=i(t),m=s(t,"DIV",{class:!0});var _=P(m);f(N.$$.fragment,_),bt=i(_),U=s(_,"P",{"data-svelte-h":!0}),d(U)!=="svelte-pvpmqk"&&(U.textContent=Ht),_t=i(_),F=s(_,"P",{"data-svelte-h":!0}),d(F)!=="svelte-jwcs2b"&&(F.textContent=qt),Ct=i(_),G=s(_,"UL",{"data-svelte-h":!0}),d(G)!=="svelte-aqudvo"&&(G.innerHTML=At),xt=i(_),R=s(_,"P",{"data-svelte-h":!0}),d(R)!=="svelte-1rfly87"&&(R.innerHTML=St),_.forEach(e),pt=i(t),f(j.$$.fragment,t),mt=i(t),p=s(t,"DIV",{class:!0});var c=P(p);f(D.$$.fragment,c),$t=i(c),B=s(c,"P",{"data-svelte-h":!0}),d(B)!=="svelte-1cdhmyj"&&(B.textContent=Vt),Tt=i(c),x=s(c,"DIV",{class:!0});var ft=P(x);f(H.$$.fragment,ft),yt=i(ft),K=s(ft,"P",{"data-svelte-h":!0}),d(K)!=="svelte-9bizft"&&(K.textContent=Ut),ft.forEach(e),Pt=i(c),$=s(c,"DIV",{class:!0});var ut=P($);f(q.$$.fragment,ut),wt=i(ut),Q=s(ut,"P",{"data-svelte-h":!0}),d(Q)!=="svelte-3v5t0c"&&(Q.textContent=Ft),ut.forEach(e),kt=i(c),T=s(c,"DIV",{class:!0});var ht=P(T);f(A.$$.fragment,ht),Lt=i(ht),J=s(ht,"P",{"data-svelte-h":!0}),d(J)!=="svelte-pg5azd"&&(J.textContent=Gt),ht.forEach(e),Et=i(c),y=s(c,"DIV",{class:!0});var vt=P(y);f(S.$$.fragment,vt),Ot=i(vt),W=s(vt,"P",{"data-svelte-h":!0}),d(W)!=="svelte-1bcpx1v"&&(W.textContent=Rt),vt.forEach(e),c.forEach(e),dt=i(t),f(V.$$.fragment,t),ct=i(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,ot,n),l(t,O,n),l(t,it,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,bt),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){v(k.$$.fragment,t),v(L.$$.fragment,t),v(z.$$.fragment,t),v(N.$$.fragment,t),v(j.$$.fragment,t),v(D.$$.fragment,t),v(H.$$.fragment,t),v(q.$$.fragment,t),v(A.$$.fragment,t),v(S.$$.fragment,t),v(V.$$.fragment,t),gt=!1},d(t){t&&(e(tt),e(Y),e(et),e(nt),e(at),e(E),e(ot),e(O),e(it),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),b(k,t),b(L,t),b(z,t),b(N),b(j,t),b(D),b(H),b(q),b(A),b(S),b(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};
