import{s as he,o as ge,n as ue}from"../chunks/scheduler.78382b47.js";import{S as Me,i as ye,e as p,s as o,c as y,h as we,a as d,d as a,b as l,f as Y,g as w,j as A,k as K,l as u,m as n,n as b,t as T,o as _,p as j}from"../chunks/index.6dd35eb6.js";import{C as be,H as oe,E as Te}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.8d7e60c0.js";import{D as fe}from"../chunks/Docstring.57cb1354.js";import{C as _e}from"../chunks/CodeBlock.4d4112a9.js";import{E as je}from"../chunks/ExampleCodeBlock.7440be11.js";function ve(W){let r,J="Example:",h,c,m;return c=new _e({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMFN0YWJsZURpZmZ1c2lvblBpcGVsaW5lJTBBZnJvbSUyMHBlZnQlMjBpbXBvcnQlMjBIUkFNb2RlbCUyQyUyMEhSQUNvbmZpZyUwQSUwQWNvbmZpZ190ZSUyMCUzRCUyMEhSQUNvbmZpZyglMEElMjAlMjAlMjAlMjByJTNEOCUyQyUwQSUyMCUyMCUyMCUyMHRhcmdldF9tb2R1bGVzJTNEJTVCJTIya19wcm9qJTIyJTJDJTIwJTIycV9wcm9qJTIyJTJDJTIwJTIydl9wcm9qJTIyJTJDJTIwJTIyb3V0X3Byb2olMjIlMkMlMjAlMjJmYzElMjIlMkMlMjAlMjJmYzIlMjIlNUQlMkMlMEElMjAlMjAlMjAlMjBpbml0X3dlaWdodHMlM0RUcnVlJTJDJTBBKSUwQWNvbmZpZ191bmV0JTIwJTNEJTIwSFJBQ29uZmlnKCUwQSUyMCUyMCUyMCUyMHIlM0Q4JTJDJTBBJTIwJTIwJTIwJTIwdGFyZ2V0X21vZHVsZXMlM0QlNUIlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjJwcm9qX2luJTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIycHJval9vdXQlMjIlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjJ0b19rJTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIydG9fcSUyMiUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMnRvX3YlMjIlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjJ0b19vdXQuMCUyMiUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMmZmLm5ldC4wLnByb2olMjIlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjJmZi5uZXQuMiUyMiUyQyUwQSUyMCUyMCUyMCUyMCU1RCUyQyUwQSUyMCUyMCUyMCUyMGluaXRfd2VpZ2h0cyUzRFRydWUlMkMlMEEpJTBBJTBBbW9kZWwlMjAlM0QlMjBTdGFibGVEaWZmdXNpb25QaXBlbGluZS5mcm9tX3ByZXRyYWluZWQoJTIycnVud2F5bWwlMkZzdGFibGUtZGlmZnVzaW9uLXYxLTUlMjIpJTBBbW9kZWwudGV4dF9lbmNvZGVyJTIwJTNEJTIwSFJBTW9kZWwobW9kZWwudGV4dF9lbmNvZGVyJTJDJTIwY29uZmlnX3RlJTJDJTIwJTIyZGVmYXVsdCUyMiklMEFtb2RlbC51bmV0JTIwJTNEJTIwSFJBTW9kZWwobW9kZWwudW5ldCUyQyUyMGNvbmZpZ191bmV0JTJDJTIwJTIyZGVmYXVsdCUyMik=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionPipeline
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> peft <span class="hljs-keyword">import</span> HRAModel, HRAConfig

<span class="hljs-meta">&gt;&gt;&gt; </span>config_te = HRAConfig(
<span class="hljs-meta">... </span>    r=<span class="hljs-number">8</span>,
<span class="hljs-meta">... </span>    target_modules=[<span class="hljs-string">&quot;k_proj&quot;</span>, <span class="hljs-string">&quot;q_proj&quot;</span>, <span class="hljs-string">&quot;v_proj&quot;</span>, <span class="hljs-string">&quot;out_proj&quot;</span>, <span class="hljs-string">&quot;fc1&quot;</span>, <span class="hljs-string">&quot;fc2&quot;</span>],
<span class="hljs-meta">... </span>    init_weights=<span class="hljs-literal">True</span>,
<span class="hljs-meta">... </span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>config_unet = HRAConfig(
<span class="hljs-meta">... </span>    r=<span class="hljs-number">8</span>,
<span class="hljs-meta">... </span>    target_modules=[
<span class="hljs-meta">... </span>        <span class="hljs-string">&quot;proj_in&quot;</span>,
<span class="hljs-meta">... </span>        <span class="hljs-string">&quot;proj_out&quot;</span>,
<span class="hljs-meta">... </span>        <span class="hljs-string">&quot;to_k&quot;</span>,
<span class="hljs-meta">... </span>        <span class="hljs-string">&quot;to_q&quot;</span>,
<span class="hljs-meta">... </span>        <span class="hljs-string">&quot;to_v&quot;</span>,
<span class="hljs-meta">... </span>        <span class="hljs-string">&quot;to_out.0&quot;</span>,
<span class="hljs-meta">... </span>        <span class="hljs-string">&quot;ff.net.0.proj&quot;</span>,
<span class="hljs-meta">... </span>        <span class="hljs-string">&quot;ff.net.2&quot;</span>,
<span class="hljs-meta">... </span>    ],
<span class="hljs-meta">... </span>    init_weights=<span class="hljs-literal">True</span>,
<span class="hljs-meta">... </span>)

<span class="hljs-meta">&gt;&gt;&gt; </span>model = StableDiffusionPipeline.from_pretrained(<span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>model.text_encoder = HRAModel(model.text_encoder, config_te, <span class="hljs-string">&quot;default&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>model.unet = HRAModel(model.unet, config_unet, <span class="hljs-string">&quot;default&quot;</span>)`,wrap:!1}}),{c(){r=p("p"),r.textContent=J,h=o(),y(c.$$.fragment)},l(s){r=d(s,"P",{"data-svelte-h":!0}),A(r)!=="svelte-11lpom8"&&(r.textContent=J),h=l(s),w(c.$$.fragment,s)},m(s,f){n(s,r,f),n(s,h,f),b(c,s,f),m=!0},p:ue,i(s){m||(T(c.$$.fragment,s),m=!0)},o(s){_(c.$$.fragment,s),m=!1},d(s){s&&(a(r),a(h)),j(c,s)}}}function Ae(W){let r,J,h,c,m,s,f,G,U,le='<a href="https://huggingface.co/papers/2405.17484" rel="nofollow">HRA</a> is a simple but effective adapter-based fine-tuning method by leveraging Householder reflections. This method harnesses the advantages of both strategies, reducing parameters and computation costs while penalizing the loss of pre-training knowledge. It consistently achieves better performance with fewer trainable parameters and outperforms state-of-the-art adapters across different models, including large language models (LLMs) and conditional image generators.',N,C,re="The abstract from the paper is:",S,H,ie='<p>While following different technical routes, both low-rank and orthogonal adaptation techniques can efficiently adapt large-scale pre-training models in specific tasks or domains based on a small piece of trainable parameters. In this study, we bridge the gap between these two techniques, proposing a simple but effective adaptation method based on Householder reflections. Given a pre-trained model, our method fine-tunes its layers by multiplying each frozen weight matrix with an orthogonal matrix constructed by a chain of learnable Householder reflections (HRs). This HR-based orthogonal fine-tuning is equivalent to an adaptive low-rank adaptation. Moreover, we show that the orthogonality of the reflection planes corresponding to the HRs impacts the model capacity and regularity. The analysis motivates us to regularize the orthogonality of the HRs, leading to different implementations of the proposed Householder reflection adaptation (HRA) method. Compared with state-of-the-art methods, HRA achieves superior performance with fewer learnable parameters when adapting large language models and conditional image generators. The code is available at <a href="https://github.com/huggingface/peft/tree/main/src/peft/tuners/hra" rel="nofollow">peft</a> and <a href="https://github.com/DaShenZi721/HRA" rel="nofollow">HRA</a>.</p>',V,$,O,M,R,ee,Z,pe='This is the configuration class to store the configuration of a <a href="/docs/peft/v0.18.0.rc0/en/package_reference/hra#peft.HRAModel">HRAModel</a>.',D,I,X,i,x,te,B,de=`Creates Householder reflection adaptation (HRA) model from a pretrained model. The method is described in
<a href="https://huggingface.co/papers/2405.17484" rel="nofollow">https://huggingface.co/papers/2405.17484</a>`,ae,v,ne,E,ce="<strong>Attributes</strong>:",se,L,me='<li><strong>model</strong> (<code>~torch.nn.Module</code>) — The model to be adapted.</li> <li><strong>peft_config</strong> (<a href="/docs/peft/v0.18.0.rc0/en/package_reference/hra#peft.HRAConfig">HRAConfig</a>): The configuration of the HRA model.</li>',F,k,P,Q,z;return m=new be({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),f=new oe({props:{title:"Bridging The Gap between Low-rank and Orthogonal Adaptation via Householder Reflection Adaptation (HRA)",local:"bridging-the-gap-between-low-rank-and-orthogonal-adaptation-via-householder-reflection-adaptation-hra",headingTag:"h1"}}),$=new oe({props:{title:"HRAConfig",local:"peft.HRAConfig",headingTag:"h2"}}),R=new fe({props:{name:"class peft.HRAConfig",anchor:"peft.HRAConfig",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:"r",val:": int = 8"},{name:"apply_GS",val:": bool = False"},{name:"target_modules",val:": Optional[Union[list[str], str]] = None"},{name:"exclude_modules",val:": Optional[Union[list[str], str]] = None"},{name:"init_weights",val:": bool = True"},{name:"layers_to_transform",val:": Optional[Union[list[int], int]] = None"},{name:"layers_pattern",val:": Optional[Union[list[str], str]] = None"},{name:"bias",val:": str = 'none'"},{name:"modules_to_save",val:": Optional[list[str]] = None"}],parametersDescription:[{anchor:"peft.HRAConfig.r",description:`<strong>r</strong> (<code>int</code>) &#x2014;
The rank of HRA across different layers. It is best to set &#x2018;r&#x2019; to an even number; otherwise, the default
initialization method will not work.`,name:"r"},{anchor:"peft.HRAConfig.apply_GS",description:`<strong>apply_GS</strong> (<code>bool</code>) &#x2014;
Whether to apply Gram-Schmidt orthogonalization.`,name:"apply_GS"},{anchor:"peft.HRAConfig.target_modules",description:`<strong>target_modules</strong> (<code>Optional[Union[List[str], str]]</code>) &#x2014;
The names of the modules to apply the adapter to. If this is specified, only the modules with the specified
names will be replaced. 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. If this is specified as &#x2018;all-linear&#x2019;, then all linear modules are chosen, excluding
the output layer. 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.HRAConfig.exclude_modules",description:`<strong>exclude_modules</strong> (<code>Optional[Union[List[str], str]]</code>) &#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.HRAConfig.init_weights",description:`<strong>init_weights</strong> (<code>bool</code>) &#x2014;
Whether to perform initialization of HRA weights.`,name:"init_weights"},{anchor:"peft.HRAConfig.layers_to_transform",description:`<strong>layers_to_transform</strong> (<code>Union[List[int], int]</code>) &#x2014;
The layer indices to transform. If a list of ints is passed, it will apply the adapter to the layer indices
that are specified in this list. If a single integer is passed, it will apply the transformations on the
layer at this index.`,name:"layers_to_transform"},{anchor:"peft.HRAConfig.layers_pattern",description:`<strong>layers_pattern</strong> (<code>Optional[Union[List[str], str]]</code>) &#x2014;
The layer pattern name, used only if <code>layers_to_transform</code> is different from <code>None</code>. This should target the
<code>nn.ModuleList</code> of the model, which is often called <code>&apos;layers&apos;</code> or <code>&apos;h&apos;</code>.`,name:"layers_pattern"},{anchor:"peft.HRAConfig.modules_to_save",description:`<strong>modules_to_save</strong> (<code>List[str]</code>) &#x2014;
List of modules apart from adapter layers to be set as trainable and saved in the final checkpoint.`,name:"modules_to_save"}],source:"https://github.com/huggingface/peft/blob/v0.18.0.rc0/src/peft/tuners/hra/config.py#L25"}}),I=new oe({props:{title:"HRAModel",local:"peft.HRAModel",headingTag:"h2"}}),x=new fe({props:{name:"class peft.HRAModel",anchor:"peft.HRAModel",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.HRAModel.model",description:"<strong>model</strong> (<code>torch.nn.Module</code>) &#x2014; The model to which the adapter tuner layers will be attached.",name:"model"},{anchor:"peft.HRAModel.config",description:'<strong>config</strong> (<a href="/docs/peft/v0.18.0.rc0/en/package_reference/hra#peft.HRAConfig">HRAConfig</a>) &#x2014; The configuration of the HRA model.',name:"config"},{anchor:"peft.HRAModel.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.HRAModel.low_cpu_mem_usage",description:`<strong>low_cpu_mem_usage</strong> (<code>bool</code>, <code>optional</code>, defaults to <code>False</code>) &#x2014;
Create empty adapter weights on meta device. Useful to speed up the loading process.`,name:"low_cpu_mem_usage"}],source:"https://github.com/huggingface/peft/blob/v0.18.0.rc0/src/peft/tuners/hra/model.py#L24",returnDescription:`<script context="module">export const metadata = 'undefined';<\/script>


<p>The HRA model.</p>
`,returnType:`<script context="module">export const metadata = 'undefined';<\/script>


<p><code>torch.nn.Module</code></p>
`}}),v=new je({props:{anchor:"peft.HRAModel.example",$$slots:{default:[ve]},$$scope:{ctx:W}}}),k=new Te({props:{source:"https://github.com/huggingface/peft/blob/main/docs/source/package_reference/hra.md"}}),{c(){r=p("meta"),J=o(),h=p("p"),c=o(),y(m.$$.fragment),s=o(),y(f.$$.fragment),G=o(),U=p("p"),U.innerHTML=le,N=o(),C=p("p"),C.textContent=re,S=o(),H=p("blockquote"),H.innerHTML=ie,V=o(),y($.$$.fragment),O=o(),M=p("div"),y(R.$$.fragment),ee=o(),Z=p("p"),Z.innerHTML=pe,D=o(),y(I.$$.fragment),X=o(),i=p("div"),y(x.$$.fragment),te=o(),B=p("p"),B.innerHTML=de,ae=o(),y(v.$$.fragment),ne=o(),E=p("p"),E.innerHTML=ce,se=o(),L=p("ul"),L.innerHTML=me,F=o(),y(k.$$.fragment),P=o(),Q=p("p"),this.h()},l(e){const t=we("svelte-u9bgzb",document.head);r=d(t,"META",{name:!0,content:!0}),t.forEach(a),J=l(e),h=d(e,"P",{}),Y(h).forEach(a),c=l(e),w(m.$$.fragment,e),s=l(e),w(f.$$.fragment,e),G=l(e),U=d(e,"P",{"data-svelte-h":!0}),A(U)!=="svelte-1wazsr6"&&(U.innerHTML=le),N=l(e),C=d(e,"P",{"data-svelte-h":!0}),A(C)!=="svelte-1cwsb16"&&(C.textContent=re),S=l(e),H=d(e,"BLOCKQUOTE",{"data-svelte-h":!0}),A(H)!=="svelte-bkd9l0"&&(H.innerHTML=ie),V=l(e),w($.$$.fragment,e),O=l(e),M=d(e,"DIV",{class:!0});var q=Y(M);w(R.$$.fragment,q),ee=l(q),Z=d(q,"P",{"data-svelte-h":!0}),A(Z)!=="svelte-it06in"&&(Z.innerHTML=pe),q.forEach(a),D=l(e),w(I.$$.fragment,e),X=l(e),i=d(e,"DIV",{class:!0});var g=Y(i);w(x.$$.fragment,g),te=l(g),B=d(g,"P",{"data-svelte-h":!0}),A(B)!=="svelte-oky5id"&&(B.innerHTML=de),ae=l(g),w(v.$$.fragment,g),ne=l(g),E=d(g,"P",{"data-svelte-h":!0}),A(E)!=="svelte-1xx6nm4"&&(E.innerHTML=ce),se=l(g),L=d(g,"UL",{"data-svelte-h":!0}),A(L)!=="svelte-1xbvs6h"&&(L.innerHTML=me),g.forEach(a),F=l(e),w(k.$$.fragment,e),P=l(e),Q=d(e,"P",{}),Y(Q).forEach(a),this.h()},h(){K(r,"name","hf:doc:metadata"),K(r,"content",Je),K(M,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),K(i,"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){u(document.head,r),n(e,J,t),n(e,h,t),n(e,c,t),b(m,e,t),n(e,s,t),b(f,e,t),n(e,G,t),n(e,U,t),n(e,N,t),n(e,C,t),n(e,S,t),n(e,H,t),n(e,V,t),b($,e,t),n(e,O,t),n(e,M,t),b(R,M,null),u(M,ee),u(M,Z),n(e,D,t),b(I,e,t),n(e,X,t),n(e,i,t),b(x,i,null),u(i,te),u(i,B),u(i,ae),b(v,i,null),u(i,ne),u(i,E),u(i,se),u(i,L),n(e,F,t),b(k,e,t),n(e,P,t),n(e,Q,t),z=!0},p(e,[t]){const q={};t&2&&(q.$$scope={dirty:t,ctx:e}),v.$set(q)},i(e){z||(T(m.$$.fragment,e),T(f.$$.fragment,e),T($.$$.fragment,e),T(R.$$.fragment,e),T(I.$$.fragment,e),T(x.$$.fragment,e),T(v.$$.fragment,e),T(k.$$.fragment,e),z=!0)},o(e){_(m.$$.fragment,e),_(f.$$.fragment,e),_($.$$.fragment,e),_(R.$$.fragment,e),_(I.$$.fragment,e),_(x.$$.fragment,e),_(v.$$.fragment,e),_(k.$$.fragment,e),z=!1},d(e){e&&(a(J),a(h),a(c),a(s),a(G),a(U),a(N),a(C),a(S),a(H),a(V),a(O),a(M),a(D),a(X),a(i),a(F),a(P),a(Q)),a(r),j(m,e),j(f,e),j($,e),j(R),j(I,e),j(x),j(v),j(k,e)}}}const Je='{"title":"Bridging The Gap between Low-rank and Orthogonal Adaptation via Householder Reflection Adaptation (HRA)","local":"bridging-the-gap-between-low-rank-and-orthogonal-adaptation-via-householder-reflection-adaptation-hra","sections":[{"title":"HRAConfig","local":"peft.HRAConfig","sections":[],"depth":2},{"title":"HRAModel","local":"peft.HRAModel","sections":[],"depth":2}],"depth":1}';function Ue(W){return ge(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class ke extends Me{constructor(r){super(),ye(this,r,Ue,Ae,he,{})}}export{ke as component};
