import{s as ge,n as pe,o as ce}from"../chunks/scheduler.4cd3e004.js";import{S as ue,i as be,e as r,s as i,c as L,h as _e,a as l,d as n,b as o,f as X,g as P,j as h,k as A,l as $,m as a,n as O,t as H,o as N,p as z}from"../chunks/index.b664cb2d.js";import{D as he}from"../chunks/Docstring.9ceb4334.js";import{H as te,E as Te}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.179b63f8.js";function ke(ne){let d,D,E,S,g,R,p,ae=`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>.`,U,c,ie=`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.`,B,u,oe=`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.`,F,b,se=`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>.`,I,m,re=`<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/functional#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>`,j,_,V,s,T,Y,x,le="Configuration for the <code>TrainableTokens</code> method.",Z,C,de=`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.`,ee,M,me="Note that training with FSDP/DeepSpeed might not yet be fully supported.",G,k,K,v,y,Q,w,W,q,J;return g=new te({props:{title:"Trainable Tokens",local:"trainable-tokens",headingTag:"h1"}}),_=new te({props:{title:"TrainableTokensConfig",local:"peft.TrainableTokensConfig",headingTag:"h2"}}),T=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"}}),k=new te({props:{title:"TrainableTokensModel",local:"peft.TrainableTokensModel",headingTag:"h2"}}),y=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"}}),w=new Te({props:{source:"https://github.com/huggingface/peft/blob/main/docs/source/package_reference/trainable_tokens.md"}}),{c(){d=r("meta"),D=i(),E=r("p"),S=i(),L(g.$$.fragment),R=i(),p=r("p"),p.innerHTML=ae,U=i(),c=r("p"),c.textContent=ie,B=i(),u=r("p"),u.innerHTML=oe,F=i(),b=r("p"),b.innerHTML=se,I=i(),m=r("blockquote"),m.innerHTML=re,j=i(),L(_.$$.fragment),V=i(),s=r("div"),L(T.$$.fragment),Y=i(),x=r("p"),x.innerHTML=le,Z=i(),C=r("p"),C.textContent=de,ee=i(),M=r("p"),M.textContent=me,G=i(),L(k.$$.fragment),K=i(),v=r("div"),L(y.$$.fragment),Q=i(),L(w.$$.fragment),W=i(),q=r("p"),this.h()},l(e){const t=_e("svelte-u9bgzb",document.head);d=l(t,"META",{name:!0,content:!0}),t.forEach(n),D=o(e),E=l(e,"P",{}),X(E).forEach(n),S=o(e),P(g.$$.fragment,e),R=o(e),p=l(e,"P",{"data-svelte-h":!0}),h(p)!=="svelte-17vziyk"&&(p.innerHTML=ae),U=o(e),c=l(e,"P",{"data-svelte-h":!0}),h(c)!=="svelte-1vl0i4j"&&(c.textContent=ie),B=o(e),u=l(e,"P",{"data-svelte-h":!0}),h(u)!=="svelte-1y50urh"&&(u.innerHTML=oe),F=o(e),b=l(e,"P",{"data-svelte-h":!0}),h(b)!=="svelte-60mp98"&&(b.innerHTML=se),I=o(e),m=l(e,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),h(m)!=="svelte-m8q2pl"&&(m.innerHTML=re),j=o(e),P(_.$$.fragment,e),V=o(e),s=l(e,"DIV",{class:!0});var f=X(s);P(T.$$.fragment,f),Y=o(f),x=l(f,"P",{"data-svelte-h":!0}),h(x)!=="svelte-id1b2g"&&(x.innerHTML=le),Z=o(f),C=l(f,"P",{"data-svelte-h":!0}),h(C)!=="svelte-11u9ofd"&&(C.textContent=de),ee=o(f),M=l(f,"P",{"data-svelte-h":!0}),h(M)!=="svelte-xzl0qt"&&(M.textContent=me),f.forEach(n),G=o(e),P(k.$$.fragment,e),K=o(e),v=l(e,"DIV",{class:!0});var fe=X(v);P(y.$$.fragment,fe),fe.forEach(n),Q=o(e),P(w.$$.fragment,e),W=o(e),q=l(e,"P",{}),X(q).forEach(n),this.h()},h(){A(d,"name","hf:doc:metadata"),A(d,"content",ve),A(m,"class","tip"),A(s,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),A(v,"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){$(document.head,d),a(e,D,t),a(e,E,t),a(e,S,t),O(g,e,t),a(e,R,t),a(e,p,t),a(e,U,t),a(e,c,t),a(e,B,t),a(e,u,t),a(e,F,t),a(e,b,t),a(e,I,t),a(e,m,t),a(e,j,t),O(_,e,t),a(e,V,t),a(e,s,t),O(T,s,null),$(s,Y),$(s,x),$(s,Z),$(s,C),$(s,ee),$(s,M),a(e,G,t),O(k,e,t),a(e,K,t),a(e,v,t),O(y,v,null),a(e,Q,t),O(w,e,t),a(e,W,t),a(e,q,t),J=!0},p:pe,i(e){J||(H(g.$$.fragment,e),H(_.$$.fragment,e),H(T.$$.fragment,e),H(k.$$.fragment,e),H(y.$$.fragment,e),H(w.$$.fragment,e),J=!0)},o(e){N(g.$$.fragment,e),N(_.$$.fragment,e),N(T.$$.fragment,e),N(k.$$.fragment,e),N(y.$$.fragment,e),N(w.$$.fragment,e),J=!1},d(e){e&&(n(D),n(E),n(S),n(R),n(p),n(U),n(c),n(B),n(u),n(F),n(b),n(I),n(m),n(j),n(V),n(s),n(G),n(K),n(v),n(Q),n(W),n(q)),n(d),z(g,e),z(_,e),z(T),z(k,e),z(y),z(w,e)}}}const ve='{"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 ye(ne){return ce(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Me extends ue{constructor(d){super(),be(this,d,ye,ke,ge,{})}}export{Me as component};
