import{s as ye,o as _e,n as Te}from"../chunks/scheduler.78382b47.js";import{S as Le,i as be,e as m,s,c as _,h as ve,a as p,d as n,b as r,f as K,g as T,j as L,k as ee,l as g,m as a,n as b,t as v,o as $,p as M}from"../chunks/index.6dd35eb6.js";import{C as $e,H as ie,E as Me}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.8d7e60c0.js";import{D as he}from"../chunks/Docstring.57cb1354.js";import{C as xe}from"../chunks/CodeBlock.4d4112a9.js";import{E as Ne}from"../chunks/ExampleCodeBlock.7440be11.js";function Ce(G){let i,N="Example:",h,d,c;return d=new xe({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Nb2RlbEZvckNhdXNhbExNJTBBZnJvbSUyMHBlZnQlMjBpbXBvcnQlMjBnZXRfcGVmdF9tb2RlbCUyQyUyMFRhc2tUeXBlJTJDJTIwTE5UdW5pbmdDb25maWclMEElMEFwZWZ0X2NvbmZpZyUyMCUzRCUyMExOVHVuaW5nQ29uZmlnKCUwQSUyMCUyMCUyMCUyMHRhc2tfdHlwZSUzRFRhc2tUeXBlLkNBVVNBTF9MTSUyQyUwQSklMEElMEFtb2RlbCUyMCUzRCUyMEF1dG9Nb2RlbEZvckNhdXNhbExNLmZyb21fcHJldHJhaW5lZCglMjJtZXRhLWxsYW1hJTJGTGxhbWEtMi03Yi1oZiUyMiklMEFtb2RlbCUyMCUzRCUyMGdldF9wZWZ0X21vZGVsKG1vZGVsJTJDJTIwcGVmdF9jb25maWcpJTBBbW9kZWwucHJpbnRfdHJhaW5hYmxlX3BhcmFtZXRlcnMoKQ==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> peft <span class="hljs-keyword">import</span> get_peft_model, TaskType, LNTuningConfig

<span class="hljs-meta">&gt;&gt;&gt; </span>peft_config = LNTuningConfig(
<span class="hljs-meta">... </span>    task_type=TaskType.CAUSAL_LM,
<span class="hljs-meta">... </span>)

<span class="hljs-meta">&gt;&gt;&gt; </span>model = AutoModelForCausalLM.from_pretrained(<span class="hljs-string">&quot;meta-llama/Llama-2-7b-hf&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>model = get_peft_model(model, peft_config)
<span class="hljs-meta">&gt;&gt;&gt; </span>model.print_trainable_parameters()`,wrap:!1}}),{c(){i=m("p"),i.textContent=N,h=s(),_(d.$$.fragment)},l(o){i=p(o,"P",{"data-svelte-h":!0}),L(i)!=="svelte-11lpom8"&&(i.textContent=N),h=r(o),T(d.$$.fragment,o)},m(o,u){a(o,i,u),a(o,h,u),b(d,o,u),c=!0},p:Te,i(o){c||(v(d.$$.fragment,o),c=!0)},o(o){$(d.$$.fragment,o),c=!1},d(o){o&&(n(i),n(h)),M(d,o)}}}function we(G){let i,N,h,d,c,o,u,S,C,le=`LayerNorm Tuning (<a href="https://huggingface.co/papers/2312.11420" rel="nofollow">LN Tuning</a>) is a PEFT method that only fine-tunes the parameters of the LayerNorm layers in a model.
The paper has tested the performance of this method on large language models and has shown that it can achieve strong performance with a significant reduction in the number of trainable parameters and GPU memory usage.
However, the method is not limited to language models and can be applied to any model that uses LayerNorm layers.
In this implementation, the default is that all layernorm layers inside a model is finetuned, but it could be used to target other layer types such as <code>MLP</code> or <code>Attention</code> layers, this can be done by specifying the <code>target_modules</code> in the <code>LNTuningConfig</code>.`,V,w,me="The abstract from the paper is:",X,U,pe="<em>This paper introduces an efficient strategy to transform Large Language Models (LLMs) into Multi-Modal Large Language Models (MLLMs). By conceptualizing this transformation as a domain adaptation process, i.e., transitioning from text understanding to embracing multiple modalities, we intriguingly note that, within each attention block, tuning LayerNorm suffices to yield strong performance. Moreover, when benchmarked against other tuning approaches like full parameter finetuning or LoRA, its benefits on efficiency are substantial. For example, when compared to LoRA on a 13B model scale, performance can be enhanced by an average of over 20% across five multi-modal tasks, and meanwhile, results in a significant reduction of trainable parameters by 41.9% and a decrease in GPU memory usage by 17.6%. On top of this LayerNorm strategy, we showcase that selectively tuning only with conversational data can improve efficiency further. Beyond these empirical outcomes, we provide a comprehensive analysis to explore the role of LayerNorm in adapting LLMs to the multi-modal domain and improving the expressive power of the model.</em>",D,k,z,y,E,te,j,de='This is the configuration class to store the configuration of a <a href="/docs/peft/v0.18.0.rc0/en/package_reference/layernorm_tuning#peft.LNTuningModel">LNTuningModel</a>.',A,H,Q,l,P,ne,R,ce="Creates LayerNorm tuning from a pretrained transformer model.",ae,B,fe='The method is described in detail in <a href="https://huggingface.co/papers/2312.11420" rel="nofollow">https://huggingface.co/papers/2312.11420</a>.',oe,x,se,J,ge="<strong>Attributes</strong>:",re,W,ue='<li><strong>model</strong> (<a href="https://huggingface.co/docs/transformers/v4.57.1/en/main_classes/model#transformers.PreTrainedModel" rel="nofollow">PreTrainedModel</a>) — The model to be adapted.</li> <li><strong>peft_config</strong> (<a href="/docs/peft/v0.18.0.rc0/en/package_reference/layernorm_tuning#peft.LNTuningConfig">LNTuningConfig</a>): The configuration of the Lora model.</li>',I,Z,q,O,Y;return c=new $e({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),u=new ie({props:{title:"LayerNorm Tuning",local:"layernorm-tuning",headingTag:"h1"}}),k=new ie({props:{title:"LNTuningConfig",local:"peft.LNTuningConfig",headingTag:"h2"}}),E=new he({props:{name:"class peft.LNTuningConfig",anchor:"peft.LNTuningConfig",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:"target_modules",val:": Optional[Union[list[str], str]] = None"},{name:"exclude_modules",val:": Optional[Union[list[str], str]] = None"},{name:"modules_to_save",val:": Optional[Union[list[str], str]] = None"}],parametersDescription:[{anchor:"peft.LNTuningConfig.target_modules",description:`<strong>target_modules</strong> (<em>Optional[Union[List[str], str]]</em>) &#x2014;
List of module names or regex expression of the module names to replace with LNTuning. For example,
&#x2018;.<em>decoder.</em>&#x2019; or &#x2018;.<em>encoder.</em>&#x2019;. If this is not specified, modules will be chosen according to the model
architecture. If the architecture is not known, an error will be raised &#x2014; in this case, you should specify
the target modules manually.`,name:"target_modules"},{anchor:"peft.LNTuningConfig.exclude_modules",description:`<strong>exclude_modules</strong> (<em>Optional[Union[List[str], str]]</em>) &#x2014;
The names of the modules to not apply the adapter. When passing a string, a regex match will be performed.
When passing a list of strings, either an exact match will be performed or it is checked if the name of the
module ends with any of the passed strings.`,name:"exclude_modules"},{anchor:"peft.LNTuningConfig.modules_to_save",description:`<strong>modules_to_save</strong> (<em>Optional[Union[List[str], str]]</em>) &#x2014;
List of modules to be set as trainable and saved in the final checkpoint. For example, in Sequence
Classification or Token Classification tasks, the final layer <em>classifier/score</em> are randomly initialized
and as such need to be trainable and saved.`,name:"modules_to_save"}],source:"https://github.com/huggingface/peft/blob/v0.18.0.rc0/src/peft/tuners/ln_tuning/config.py#L24"}}),H=new ie({props:{title:"LNTuningModel",local:"peft.LNTuningModel",headingTag:"h2"}}),P=new he({props:{name:"class peft.LNTuningModel",anchor:"peft.LNTuningModel",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"}],parametersDescription:[{anchor:"peft.LNTuningModel.model",description:"<strong>model</strong> (<code>torch.nn.Module</code>) &#x2014; The model to be adapted.",name:"model"},{anchor:"peft.LNTuningModel.config",description:'<strong>config</strong> (<a href="/docs/peft/v0.18.0.rc0/en/package_reference/layernorm_tuning#peft.LNTuningConfig">LNTuningConfig</a>) &#x2014; The configuration of the Lora model.',name:"config"},{anchor:"peft.LNTuningModel.adapter_name",description:"<strong>adapter_name</strong> (<code>str</code>) &#x2014; The name of the adapter, defaults to <code>&quot;default&quot;</code>.",name:"adapter_name"},{anchor:"peft.LNTuningModel.low_cpu_mem_usage",description:`<strong>low_cpu_mem_usage</strong> (<code>bool</code>, <code>optional</code>, defaults to <code>False</code>) &#x2014;
This option has no effect on LN tuning but exists for consistency with other PEFT methods.`,name:"low_cpu_mem_usage"}],source:"https://github.com/huggingface/peft/blob/v0.18.0.rc0/src/peft/tuners/ln_tuning/model.py#L28",returnDescription:`<script context="module">export const metadata = 'undefined';<\/script>


<p>The adapted model with LayerNorm tuned on.</p>
`,returnType:`<script context="module">export const metadata = 'undefined';<\/script>


<p>‘torch.nn.Module’</p>
`}}),x=new Ne({props:{anchor:"peft.LNTuningModel.example",$$slots:{default:[Ce]},$$scope:{ctx:G}}}),Z=new Me({props:{source:"https://github.com/huggingface/peft/blob/main/docs/source/package_reference/layernorm_tuning.md"}}),{c(){i=m("meta"),N=s(),h=m("p"),d=s(),_(c.$$.fragment),o=s(),_(u.$$.fragment),S=s(),C=m("p"),C.innerHTML=le,V=s(),w=m("p"),w.textContent=me,X=s(),U=m("p"),U.innerHTML=pe,D=s(),_(k.$$.fragment),z=s(),y=m("div"),_(E.$$.fragment),te=s(),j=m("p"),j.innerHTML=de,A=s(),_(H.$$.fragment),Q=s(),l=m("div"),_(P.$$.fragment),ne=s(),R=m("p"),R.textContent=ce,ae=s(),B=m("p"),B.innerHTML=fe,oe=s(),_(x.$$.fragment),se=s(),J=m("p"),J.innerHTML=ge,re=s(),W=m("ul"),W.innerHTML=ue,I=s(),_(Z.$$.fragment),q=s(),O=m("p"),this.h()},l(e){const t=ve("svelte-u9bgzb",document.head);i=p(t,"META",{name:!0,content:!0}),t.forEach(n),N=r(e),h=p(e,"P",{}),K(h).forEach(n),d=r(e),T(c.$$.fragment,e),o=r(e),T(u.$$.fragment,e),S=r(e),C=p(e,"P",{"data-svelte-h":!0}),L(C)!=="svelte-m4j3i2"&&(C.innerHTML=le),V=r(e),w=p(e,"P",{"data-svelte-h":!0}),L(w)!=="svelte-1cwsb16"&&(w.textContent=me),X=r(e),U=p(e,"P",{"data-svelte-h":!0}),L(U)!=="svelte-1kk7on3"&&(U.innerHTML=pe),D=r(e),T(k.$$.fragment,e),z=r(e),y=p(e,"DIV",{class:!0});var F=K(y);T(E.$$.fragment,F),te=r(F),j=p(F,"P",{"data-svelte-h":!0}),L(j)!=="svelte-jd7m1"&&(j.innerHTML=de),F.forEach(n),A=r(e),T(H.$$.fragment,e),Q=r(e),l=p(e,"DIV",{class:!0});var f=K(l);T(P.$$.fragment,f),ne=r(f),R=p(f,"P",{"data-svelte-h":!0}),L(R)!=="svelte-fxwr0m"&&(R.textContent=ce),ae=r(f),B=p(f,"P",{"data-svelte-h":!0}),L(B)!=="svelte-gc2vd"&&(B.innerHTML=fe),oe=r(f),T(x.$$.fragment,f),se=r(f),J=p(f,"P",{"data-svelte-h":!0}),L(J)!=="svelte-1xx6nm4"&&(J.innerHTML=ge),re=r(f),W=p(f,"UL",{"data-svelte-h":!0}),L(W)!=="svelte-1wvk8de"&&(W.innerHTML=ue),f.forEach(n),I=r(e),T(Z.$$.fragment,e),q=r(e),O=p(e,"P",{}),K(O).forEach(n),this.h()},h(){ee(i,"name","hf:doc:metadata"),ee(i,"content",Ue),ee(y,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),ee(l,"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){g(document.head,i),a(e,N,t),a(e,h,t),a(e,d,t),b(c,e,t),a(e,o,t),b(u,e,t),a(e,S,t),a(e,C,t),a(e,V,t),a(e,w,t),a(e,X,t),a(e,U,t),a(e,D,t),b(k,e,t),a(e,z,t),a(e,y,t),b(E,y,null),g(y,te),g(y,j),a(e,A,t),b(H,e,t),a(e,Q,t),a(e,l,t),b(P,l,null),g(l,ne),g(l,R),g(l,ae),g(l,B),g(l,oe),b(x,l,null),g(l,se),g(l,J),g(l,re),g(l,W),a(e,I,t),b(Z,e,t),a(e,q,t),a(e,O,t),Y=!0},p(e,[t]){const F={};t&2&&(F.$$scope={dirty:t,ctx:e}),x.$set(F)},i(e){Y||(v(c.$$.fragment,e),v(u.$$.fragment,e),v(k.$$.fragment,e),v(E.$$.fragment,e),v(H.$$.fragment,e),v(P.$$.fragment,e),v(x.$$.fragment,e),v(Z.$$.fragment,e),Y=!0)},o(e){$(c.$$.fragment,e),$(u.$$.fragment,e),$(k.$$.fragment,e),$(E.$$.fragment,e),$(H.$$.fragment,e),$(P.$$.fragment,e),$(x.$$.fragment,e),$(Z.$$.fragment,e),Y=!1},d(e){e&&(n(N),n(h),n(d),n(o),n(S),n(C),n(V),n(w),n(X),n(U),n(D),n(z),n(y),n(A),n(Q),n(l),n(I),n(q),n(O)),n(i),M(c,e),M(u,e),M(k,e),M(E),M(H,e),M(P),M(x),M(Z,e)}}}const Ue='{"title":"LayerNorm Tuning","local":"layernorm-tuning","sections":[{"title":"LNTuningConfig","local":"peft.LNTuningConfig","sections":[],"depth":2},{"title":"LNTuningModel","local":"peft.LNTuningModel","sections":[],"depth":2}],"depth":1}';function ke(G){return _e(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Re extends Le{constructor(i){super(),be(this,i,ke,we,ye,{})}}export{Re as component};
