import{s as ce,n as ue,o as be}from"../chunks/scheduler.78382b47.js";import{S as _e,i as Te,e as r,s as a,c as x,h as ke,a as l,d as n,b as o,f as Z,g as C,j as p,k as D,l as M,m as i,n as L,t as P,o as O,p as H}from"../chunks/index.6dd35eb6.js";import{C as ve,H as ie,E as ye}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.fed9dfe4.js";import{D as he}from"../chunks/Docstring.32e826ec.js";function we(ae){let d,q,S,R,g,U,h,B,c,oe=`The Trainable Tokens method provides a way to target specific token embeddings for fine-tuning without resorting to
training the full embedding matrix or using an adapter on the embedding matrix. It is based on the initial implementation from
<a href="https://github.com/huggingface/peft/pull/1541" rel="nofollow">here</a>.`,F,u,se=`The method only targets specific tokens and selectively trains the token indices you specify. Consequently the
required RAM will be lower and disk memory is also significantly lower than storing the full fine-tuned embedding matrix.`,I,b,re=`Some preliminary benchmarks acquired with <a href="https://github.com/huggingface/peft/blob/main/scripts/train_memory.py" rel="nofollow">this script</a>
suggest that for <code>gemma-2-2b</code> (which has a rather large embedding matrix) you can save ~4 GiB VRAM with Trainable Tokens
over fully fine-tuning the embedding matrix. While LoRA will use comparable amounts of VRAM it might also target
tokens you don’t want to be changed. Note that these are just indications and varying embedding matrix sizes might skew
these numbers a bit.`,j,_,le=`Note that this method does not add tokens for you, you have to add tokens to the tokenizer yourself and resize the
embedding matrix of the model accordingly. This method will only re-train the embeddings for the tokens you specify.
This method can also be used in conjunction with LoRA layers! See <a href="../developer_guides/lora#efficiently-train-tokens-alongside-lora">the LoRA developer guide</a>.`,V,m,de=`<p>Saving the model with <a href="/docs/peft/main/en/package_reference/peft_model#peft.PeftModel.save_pretrained">save_pretrained()</a> or retrieving the state dict using
<a href="/docs/peft/main/en/package_reference/peft_model#peft.get_peft_model_state_dict">get_peft_model_state_dict()</a> when adding new tokens may save the full embedding matrix instead of only the difference
as a precaution because the embedding matrix was resized. To save space you can disable this behavior by setting
<code>save_embedding_layers=False</code> when calling <code>save_pretrained</code>. This is safe to do as long as you don’t modify the
embedding matrix through other means as well, as such changes will be not tracked by trainable tokens.</p>`,G,T,K,s,k,ee,z,me="Configuration for the <code>TrainableTokens</code> method.",te,N,fe=`Allows for training new tokens (and re-training existing ones) without training the full embedding matrix. By
marking a few select tokens (identified by their indices) trainable and leaving the rest untouched, this method can
be used to add new tokens or changing the embedding of existing tokens while saving on memory. Both storage as well
as working memory usage are reduced in contrast to training the embedding matrix fully.`,ne,E,pe="Note that training with FSDP/DeepSpeed might not yet be fully supported.",Q,v,W,y,w,J,$,X,A,Y;return g=new ve({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),h=new ie({props:{title:"Trainable Tokens",local:"trainable-tokens",headingTag:"h1"}}),T=new ie({props:{title:"TrainableTokensConfig",local:"peft.TrainableTokensConfig",headingTag:"h2"}}),k=new he({props:{name:"class peft.TrainableTokensConfig",anchor:"peft.TrainableTokensConfig",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:"token_indices",val:": list[int] = <factory>"},{name:"target_modules",val:": Optional[Union[list[str], str]] = None"},{name:"init_weights",val:": bool = True"}],parametersDescription:[{anchor:"peft.TrainableTokensConfig.token_indices",description:`<strong>token_indices</strong> (<code>list[int]</code>) &#x2014;
List of integers, signifying the indices of the tokens you want to be trainable. To find the index of a
token with a tokenizer, you can tokenize the string and look at the returned <code>input_ids</code>. The closer the
amount of indices is to the total amount of tokens, the less efficient this method gets.`,name:"token_indices"},{anchor:"peft.TrainableTokensConfig.target_modules",description:`<strong>target_modules</strong> (<code>Optional[Union[list[str], str]]</code>) &#x2014;
List of module names or regex expression of the module names to replace with our <code>TrainableTokensLayer</code>. If
not defined, it will attempt to get the model&#x2019;s input embedding layer if the model has a
<code>get_input_embeddings</code> method (transformer models usually do), if that fails the default is &#x2018;embed_tokens&#x2019;.
Other example targets are <code>embedding</code>, <code>encoder.embeddings</code> or <code>decoder.embeddings</code>.`,name:"target_modules"},{anchor:"peft.TrainableTokensConfig.init_weights",description:`<strong>init_weights</strong> (<code>bool</code>) &#x2014;
By default the new token weights are initialized to be the same as the respective token embeddings. This
makes TrainableTokens a no-op when not trained. If set to <code>False</code> the weights will be random values. Do not
change this setting unless you know exactly what you&#x2019;re doing.`,name:"init_weights"}],source:"https://github.com/huggingface/peft/blob/main/src/peft/tuners/trainable_tokens/config.py#L25"}}),v=new ie({props:{title:"TrainableTokensModel",local:"peft.TrainableTokensModel",headingTag:"h2"}}),w=new he({props:{name:"class peft.TrainableTokensModel",anchor:"peft.TrainableTokensModel",parameters:[{name:"model",val:""},{name:"peft_config",val:": Union[PeftConfig, dict[str, PeftConfig]]"},{name:"adapter_name",val:": str"},{name:"low_cpu_mem_usage",val:": bool = False"},{name:"state_dict",val:": Optional[dict[str, torch.Tensor]] = None"}],source:"https://github.com/huggingface/peft/blob/main/src/peft/tuners/trainable_tokens/model.py#L26"}}),$=new ye({props:{source:"https://github.com/huggingface/peft/blob/main/docs/source/package_reference/trainable_tokens.md"}}),{c(){d=r("meta"),q=a(),S=r("p"),R=a(),x(g.$$.fragment),U=a(),x(h.$$.fragment),B=a(),c=r("p"),c.innerHTML=oe,F=a(),u=r("p"),u.textContent=se,I=a(),b=r("p"),b.innerHTML=re,j=a(),_=r("p"),_.innerHTML=le,V=a(),m=r("blockquote"),m.innerHTML=de,G=a(),x(T.$$.fragment),K=a(),s=r("div"),x(k.$$.fragment),ee=a(),z=r("p"),z.innerHTML=me,te=a(),N=r("p"),N.textContent=fe,ne=a(),E=r("p"),E.textContent=pe,Q=a(),x(v.$$.fragment),W=a(),y=r("div"),x(w.$$.fragment),J=a(),x($.$$.fragment),X=a(),A=r("p"),this.h()},l(e){const t=ke("svelte-u9bgzb",document.head);d=l(t,"META",{name:!0,content:!0}),t.forEach(n),q=o(e),S=l(e,"P",{}),Z(S).forEach(n),R=o(e),C(g.$$.fragment,e),U=o(e),C(h.$$.fragment,e),B=o(e),c=l(e,"P",{"data-svelte-h":!0}),p(c)!=="svelte-17vziyk"&&(c.innerHTML=oe),F=o(e),u=l(e,"P",{"data-svelte-h":!0}),p(u)!=="svelte-1vl0i4j"&&(u.textContent=se),I=o(e),b=l(e,"P",{"data-svelte-h":!0}),p(b)!=="svelte-1y50urh"&&(b.innerHTML=re),j=o(e),_=l(e,"P",{"data-svelte-h":!0}),p(_)!=="svelte-60mp98"&&(_.innerHTML=le),V=o(e),m=l(e,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),p(m)!=="svelte-1lp240f"&&(m.innerHTML=de),G=o(e),C(T.$$.fragment,e),K=o(e),s=l(e,"DIV",{class:!0});var f=Z(s);C(k.$$.fragment,f),ee=o(f),z=l(f,"P",{"data-svelte-h":!0}),p(z)!=="svelte-id1b2g"&&(z.innerHTML=me),te=o(f),N=l(f,"P",{"data-svelte-h":!0}),p(N)!=="svelte-11u9ofd"&&(N.textContent=fe),ne=o(f),E=l(f,"P",{"data-svelte-h":!0}),p(E)!=="svelte-xzl0qt"&&(E.textContent=pe),f.forEach(n),Q=o(e),C(v.$$.fragment,e),W=o(e),y=l(e,"DIV",{class:!0});var ge=Z(y);C(w.$$.fragment,ge),ge.forEach(n),J=o(e),C($.$$.fragment,e),X=o(e),A=l(e,"P",{}),Z(A).forEach(n),this.h()},h(){D(d,"name","hf:doc:metadata"),D(d,"content",$e),D(m,"class","tip"),D(s,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),D(y,"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){M(document.head,d),i(e,q,t),i(e,S,t),i(e,R,t),L(g,e,t),i(e,U,t),L(h,e,t),i(e,B,t),i(e,c,t),i(e,F,t),i(e,u,t),i(e,I,t),i(e,b,t),i(e,j,t),i(e,_,t),i(e,V,t),i(e,m,t),i(e,G,t),L(T,e,t),i(e,K,t),i(e,s,t),L(k,s,null),M(s,ee),M(s,z),M(s,te),M(s,N),M(s,ne),M(s,E),i(e,Q,t),L(v,e,t),i(e,W,t),i(e,y,t),L(w,y,null),i(e,J,t),L($,e,t),i(e,X,t),i(e,A,t),Y=!0},p:ue,i(e){Y||(P(g.$$.fragment,e),P(h.$$.fragment,e),P(T.$$.fragment,e),P(k.$$.fragment,e),P(v.$$.fragment,e),P(w.$$.fragment,e),P($.$$.fragment,e),Y=!0)},o(e){O(g.$$.fragment,e),O(h.$$.fragment,e),O(T.$$.fragment,e),O(k.$$.fragment,e),O(v.$$.fragment,e),O(w.$$.fragment,e),O($.$$.fragment,e),Y=!1},d(e){e&&(n(q),n(S),n(R),n(U),n(B),n(c),n(F),n(u),n(I),n(b),n(j),n(_),n(V),n(m),n(G),n(K),n(s),n(Q),n(W),n(y),n(J),n(X),n(A)),n(d),H(g,e),H(h,e),H(T,e),H(k),H(v,e),H(w),H($,e)}}}const $e='{"title":"Trainable Tokens","local":"trainable-tokens","sections":[{"title":"TrainableTokensConfig","local":"peft.TrainableTokensConfig","sections":[],"depth":2},{"title":"TrainableTokensModel","local":"peft.TrainableTokensModel","sections":[],"depth":2}],"depth":1}';function xe(ae){return be(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Oe extends _e{constructor(d){super(),Te(this,d,xe,we,ce,{})}}export{Oe as component};
