import{s as ye,o as _e,n as Te}from"../chunks/scheduler.78382b47.js";import{S as be,i as Le,e as m,s,c as _,h as $e,a as p,d as n,b as i,f as K,g as T,j as b,k as ee,l as g,m as a,n as L,t as $,o as v,p as M}from"../chunks/index.6dd35eb6.js";import{C as ve,H as re,E as Me}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.fed9dfe4.js";import{D as he}from"../chunks/Docstring.32e826ec.js";import{C as xe}from"../chunks/CodeBlock.af1ec02e.js";import{E as Ne}from"../chunks/ExampleCodeBlock.7f184f2e.js";function Ce(G){let r,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(){r=m("p"),r.textContent=N,h=s(),_(d.$$.fragment)},l(o){r=p(o,"P",{"data-svelte-h":!0}),b(r)!=="svelte-11lpom8"&&(r.textContent=N),h=i(o),T(d.$$.fragment,o)},m(o,u){a(o,r,u),a(o,h,u),L(d,o,u),c=!0},p:Te,i(o){c||($(d.$$.fragment,o),c=!0)},o(o){v(d.$$.fragment,o),c=!1},d(o){o&&(n(r),n(h)),M(d,o)}}}function we(G){let r,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>",z,k,D,y,E,te,R,de='This is the configuration class to store the configuration of a <a href="/docs/peft/main/en/package_reference/layernorm_tuning#peft.LNTuningModel">LNTuningModel</a>.',A,H,Q,l,P,ne,j,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>:",ie,W,ue='<li><strong>model</strong> (<a href="https://huggingface.co/docs/transformers/main/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/main/en/package_reference/layernorm_tuning#peft.LNTuningConfig">LNTuningConfig</a>): The configuration of the Lora model.</li>',q,Z,I,O,Y;return c=new ve({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),u=new re({props:{title:"LayerNorm Tuning",local:"layernorm-tuning",headingTag:"h1"}}),k=new re({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/main/src/peft/tuners/ln_tuning/config.py#L24"}}),H=new re({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/main/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/main/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(){r=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,z=s(),_(k.$$.fragment),D=s(),y=m("div"),_(E.$$.fragment),te=s(),R=m("p"),R.innerHTML=de,A=s(),_(H.$$.fragment),Q=s(),l=m("div"),_(P.$$.fragment),ne=s(),j=m("p"),j.textContent=ce,ae=s(),B=m("p"),B.innerHTML=fe,oe=s(),_(x.$$.fragment),se=s(),J=m("p"),J.innerHTML=ge,ie=s(),W=m("ul"),W.innerHTML=ue,q=s(),_(Z.$$.fragment),I=s(),O=m("p"),this.h()},l(e){const t=$e("svelte-u9bgzb",document.head);r=p(t,"META",{name:!0,content:!0}),t.forEach(n),N=i(e),h=p(e,"P",{}),K(h).forEach(n),d=i(e),T(c.$$.fragment,e),o=i(e),T(u.$$.fragment,e),S=i(e),C=p(e,"P",{"data-svelte-h":!0}),b(C)!=="svelte-m4j3i2"&&(C.innerHTML=le),V=i(e),w=p(e,"P",{"data-svelte-h":!0}),b(w)!=="svelte-1cwsb16"&&(w.textContent=me),X=i(e),U=p(e,"P",{"data-svelte-h":!0}),b(U)!=="svelte-1kk7on3"&&(U.innerHTML=pe),z=i(e),T(k.$$.fragment,e),D=i(e),y=p(e,"DIV",{class:!0});var F=K(y);T(E.$$.fragment,F),te=i(F),R=p(F,"P",{"data-svelte-h":!0}),b(R)!=="svelte-mquwv4"&&(R.innerHTML=de),F.forEach(n),A=i(e),T(H.$$.fragment,e),Q=i(e),l=p(e,"DIV",{class:!0});var f=K(l);T(P.$$.fragment,f),ne=i(f),j=p(f,"P",{"data-svelte-h":!0}),b(j)!=="svelte-fxwr0m"&&(j.textContent=ce),ae=i(f),B=p(f,"P",{"data-svelte-h":!0}),b(B)!=="svelte-gc2vd"&&(B.innerHTML=fe),oe=i(f),T(x.$$.fragment,f),se=i(f),J=p(f,"P",{"data-svelte-h":!0}),b(J)!=="svelte-1xx6nm4"&&(J.innerHTML=ge),ie=i(f),W=p(f,"UL",{"data-svelte-h":!0}),b(W)!=="svelte-l6pbzn"&&(W.innerHTML=ue),f.forEach(n),q=i(e),T(Z.$$.fragment,e),I=i(e),O=p(e,"P",{}),K(O).forEach(n),this.h()},h(){ee(r,"name","hf:doc:metadata"),ee(r,"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,r),a(e,N,t),a(e,h,t),a(e,d,t),L(c,e,t),a(e,o,t),L(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,z,t),L(k,e,t),a(e,D,t),a(e,y,t),L(E,y,null),g(y,te),g(y,R),a(e,A,t),L(H,e,t),a(e,Q,t),a(e,l,t),L(P,l,null),g(l,ne),g(l,j),g(l,ae),g(l,B),g(l,oe),L(x,l,null),g(l,se),g(l,J),g(l,ie),g(l,W),a(e,q,t),L(Z,e,t),a(e,I,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||($(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=!0)},o(e){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=!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(z),n(D),n(y),n(A),n(Q),n(l),n(q),n(I),n(O)),n(r),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 je extends be{constructor(r){super(),Le(this,r,ke,we,ye,{})}}export{je as component};
