import{s as ue,o as he,n as ye}from"../chunks/scheduler.4cd3e004.js";import{S as _e,i as be,e as m,s,c as T,h as Te,a as p,d as n,b as i,f as I,g as L,j as _,k as Y,l as g,m as a,n as v,t as M,o as $,p as x}from"../chunks/index.b664cb2d.js";import{D as ge}from"../chunks/Docstring.9ceb4334.js";import{C as Le}from"../chunks/CodeBlock.8601e34e.js";import{E as ve}from"../chunks/ExampleCodeBlock.4f57b575.js";import{H as se,E as Me}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.179b63f8.js";function $e(O){let r,N="Example:",h,d,c;return d=new Le({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(),T(d.$$.fragment)},l(o){r=p(o,"P",{"data-svelte-h":!0}),_(r)!=="svelte-11lpom8"&&(r.textContent=N),h=i(o),L(d.$$.fragment,o)},m(o,u){a(o,r,u),a(o,h,u),v(d,o,u),c=!0},p:ye,i(o){c||(M(d.$$.fragment,o),c=!0)},o(o){$(d.$$.fragment,o),c=!1},d(o){o&&(n(r),n(h)),x(d,o)}}}function xe(O){let r,N,h,d,c,o,u,ie=`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>.`,G,C,re="The abstract from the paper is:",S,w,le="<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>",V,U,X,y,k,K,F,me='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>.',D,E,z,l,H,ee,R,pe="Creates LayerNorm tuning from a pretrained transformer model.",te,j,de='The method is described in detail in <a href="https://huggingface.co/papers/2312.11420" rel="nofollow">https://huggingface.co/papers/2312.11420</a>.',ne,b,ae,B,ce="<strong>Attributes</strong>:",oe,J,fe='<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>',A,P,Q,W,q;return c=new se({props:{title:"LayerNorm Tuning",local:"layernorm-tuning",headingTag:"h1"}}),U=new se({props:{title:"LNTuningConfig",local:"peft.LNTuningConfig",headingTag:"h2"}}),k=new ge({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"}}),E=new se({props:{title:"LNTuningModel",local:"peft.LNTuningModel",headingTag:"h2"}}),H=new ge({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>
`}}),b=new ve({props:{anchor:"peft.LNTuningModel.example",$$slots:{default:[$e]},$$scope:{ctx:O}}}),P=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(),T(c.$$.fragment),o=s(),u=m("p"),u.innerHTML=ie,G=s(),C=m("p"),C.textContent=re,S=s(),w=m("p"),w.innerHTML=le,V=s(),T(U.$$.fragment),X=s(),y=m("div"),T(k.$$.fragment),K=s(),F=m("p"),F.innerHTML=me,D=s(),T(E.$$.fragment),z=s(),l=m("div"),T(H.$$.fragment),ee=s(),R=m("p"),R.textContent=pe,te=s(),j=m("p"),j.innerHTML=de,ne=s(),T(b.$$.fragment),ae=s(),B=m("p"),B.innerHTML=ce,oe=s(),J=m("ul"),J.innerHTML=fe,A=s(),T(P.$$.fragment),Q=s(),W=m("p"),this.h()},l(e){const t=Te("svelte-u9bgzb",document.head);r=p(t,"META",{name:!0,content:!0}),t.forEach(n),N=i(e),h=p(e,"P",{}),I(h).forEach(n),d=i(e),L(c.$$.fragment,e),o=i(e),u=p(e,"P",{"data-svelte-h":!0}),_(u)!=="svelte-m4j3i2"&&(u.innerHTML=ie),G=i(e),C=p(e,"P",{"data-svelte-h":!0}),_(C)!=="svelte-1cwsb16"&&(C.textContent=re),S=i(e),w=p(e,"P",{"data-svelte-h":!0}),_(w)!=="svelte-1kk7on3"&&(w.innerHTML=le),V=i(e),L(U.$$.fragment,e),X=i(e),y=p(e,"DIV",{class:!0});var Z=I(y);L(k.$$.fragment,Z),K=i(Z),F=p(Z,"P",{"data-svelte-h":!0}),_(F)!=="svelte-mquwv4"&&(F.innerHTML=me),Z.forEach(n),D=i(e),L(E.$$.fragment,e),z=i(e),l=p(e,"DIV",{class:!0});var f=I(l);L(H.$$.fragment,f),ee=i(f),R=p(f,"P",{"data-svelte-h":!0}),_(R)!=="svelte-fxwr0m"&&(R.textContent=pe),te=i(f),j=p(f,"P",{"data-svelte-h":!0}),_(j)!=="svelte-gc2vd"&&(j.innerHTML=de),ne=i(f),L(b.$$.fragment,f),ae=i(f),B=p(f,"P",{"data-svelte-h":!0}),_(B)!=="svelte-1xx6nm4"&&(B.innerHTML=ce),oe=i(f),J=p(f,"UL",{"data-svelte-h":!0}),_(J)!=="svelte-l6pbzn"&&(J.innerHTML=fe),f.forEach(n),A=i(e),L(P.$$.fragment,e),Q=i(e),W=p(e,"P",{}),I(W).forEach(n),this.h()},h(){Y(r,"name","hf:doc:metadata"),Y(r,"content",Ne),Y(y,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),Y(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),v(c,e,t),a(e,o,t),a(e,u,t),a(e,G,t),a(e,C,t),a(e,S,t),a(e,w,t),a(e,V,t),v(U,e,t),a(e,X,t),a(e,y,t),v(k,y,null),g(y,K),g(y,F),a(e,D,t),v(E,e,t),a(e,z,t),a(e,l,t),v(H,l,null),g(l,ee),g(l,R),g(l,te),g(l,j),g(l,ne),v(b,l,null),g(l,ae),g(l,B),g(l,oe),g(l,J),a(e,A,t),v(P,e,t),a(e,Q,t),a(e,W,t),q=!0},p(e,[t]){const Z={};t&2&&(Z.$$scope={dirty:t,ctx:e}),b.$set(Z)},i(e){q||(M(c.$$.fragment,e),M(U.$$.fragment,e),M(k.$$.fragment,e),M(E.$$.fragment,e),M(H.$$.fragment,e),M(b.$$.fragment,e),M(P.$$.fragment,e),q=!0)},o(e){$(c.$$.fragment,e),$(U.$$.fragment,e),$(k.$$.fragment,e),$(E.$$.fragment,e),$(H.$$.fragment,e),$(b.$$.fragment,e),$(P.$$.fragment,e),q=!1},d(e){e&&(n(N),n(h),n(d),n(o),n(u),n(G),n(C),n(S),n(w),n(V),n(X),n(y),n(D),n(z),n(l),n(A),n(Q),n(W)),n(r),x(c,e),x(U,e),x(k),x(E,e),x(H),x(b),x(P,e)}}}const Ne='{"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 Ce(O){return he(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ze extends _e{constructor(r){super(),be(this,r,Ce,xe,ue,{})}}export{Ze as component};
