import{s as be,n as ve,o as we}from"../chunks/scheduler.4cd3e004.js";import{S as $e,i as Ce,e as i,s as r,c as P,h as Le,a as s,d as n,b as a,f as te,g as A,j as f,k as ne,l as c,m as o,n as O,t as U,o as E,p as R}from"../chunks/index.b664cb2d.js";import{D as ye}from"../chunks/Docstring.9ceb4334.js";import{H as le,E as Me}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.179b63f8.js";function ke(fe){let m,N,H,S,u,W,h,ce='<a href="https://huggingface.co/papers/2405.03003" rel="nofollow">FourierFT</a> is a parameter-efficient fine-tuning technique that leverages Discrete Fourier Transform to compress the model’s tunable weights. This method outperforms LoRA in the GLUE benchmark and common ViT classification tasks using much less parameters.',z,g,de="FourierFT currently has the following constraints:",B,_,me="<li>Only <code>nn.Linear</code> layers are supported.</li> <li>Quantized layers are not supported.</li>",j,F,pe="If these constraints don’t work for your use case, consider other methods instead.",I,T,ue="The abstract from the paper is:",V,x,he="<p>Low-rank adaptation (LoRA) has recently gained much interest in fine-tuning foundation models. It effectively reduces the number of trainable parameters by incorporating low-rank matrices A and B to represent the weight change, i.e., Delta W=BA. Despite LoRA’s progress, it faces storage challenges when handling extensive customization adaptations or larger base models. In this work, we aim to further compress trainable parameters by enjoying the powerful expressiveness of the Fourier transform. Specifically, we introduce FourierFT, which treats Delta W as a matrix in the spatial domain and learns only a small fraction of its spectral coefficients. With the trained spectral coefficients, we implement the inverse discrete Fourier transform to recover Delta W. Empirically, our FourierFT method shows comparable or better performance with fewer parameters than LoRA on various tasks, including natural language understanding, natural language generation, instruction tuning, and image classification. For example, when performing instruction tuning on the LLaMA2-7B model, FourierFT surpasses LoRA with only 0.064M trainable parameters, compared to LoRA’s 33.5M.</p>",G,y,Q,p,b,oe,C,ge='This is the configuration class to store the configuration of a <a href="/docs/peft/main/en/package_reference/fourierft#peft.FourierFTModel">FourierFTModel</a>.',K,v,J,l,w,re,L,_e="Creates FourierFT model from a pretrained transformers model.",ae,M,Fe='The method is described in detail in <a href="https://huggingface.co/papers/2405.03003" rel="nofollow">https://huggingface.co/papers/2405.03003</a>.',ie,k,Te="<strong>Attributes</strong>:",se,q,xe='<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/fourierft#peft.FourierFTConfig">FourierFTConfig</a>): The configuration of the Fourier model.</li>',X,$,Y,D,Z;return u=new le({props:{title:"FourierFT: Discrete Fourier Transformation Fine-Tuning",local:"fourierft-discrete-fourier-transformation-fine-tuning",headingTag:"h1"}}),y=new le({props:{title:"FourierFTConfig",local:"peft.FourierFTConfig",headingTag:"h2"}}),b=new ye({props:{name:"class peft.FourierFTConfig",anchor:"peft.FourierFTConfig",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:"n_frequency",val:": int = 1000"},{name:"scaling",val:": float = 150.0"},{name:"random_loc_seed",val:": Optional[int] = 777"},{name:"fan_in_fan_out",val:": bool = False"},{name:"target_modules",val:": Optional[Union[list[str], str]] = None"},{name:"exclude_modules",val:": Optional[Union[list[str], str]] = None"},{name:"bias",val:": str = 'none'"},{name:"modules_to_save",val:": Optional[list[str]] = None"},{name:"layers_to_transform",val:": Optional[Union[list[int], int]] = None"},{name:"layers_pattern",val:": Optional[Union[list[str], str]] = None"},{name:"n_frequency_pattern",val:": Optional[dict] = <factory>"},{name:"init_weights",val:": bool = False"}],parametersDescription:[{anchor:"peft.FourierFTConfig.n_frequency",description:`<strong>n_frequency</strong> (<code>int</code>) &#x2014;
Num of learnable frequencies for the Discrete Fourier Transform. &#x2018;n_frequency&#x2019; is an integer that is
greater than 0 and less than or equal to d^2 (assuming the weight W has dimensions of d by d).
Additionally, it is the number of trainable parameters required to update each delta W weight.
&#x2018;n_frequency&#x2019; will affect the performance and efficiency for PEFT. Specifically, it has little impact on
training speed, but higher values of it (typically) result in larger GPU memory costs and better accuracy.
With the same <code>target_modules</code>, the number of parameters of LoRA is (2<em>d</em>r/n_frequency) times that of
FourierFT. The following examples of settings regarding &#x2018;n_frequency&#x2019; can be used as reference for users.
For NLU tasks with the RoBERTa-large model, adopting &#x2018;n_frequency&#x2019;: 1000 can almost achieve similar results
as &#x2018;r&#x2019;: 8 in LoRA. At this time, the number of parameters of LoRA is about 16 times that of FourierFT. For
image classification tasks with Vit-large models, adopting &#x2018;n_frequency&#x2019;: 3000 can almost achieve similar
results as &#x2018;r&#x2019;: 16 in LoRA, where the number of parameters of LoRA is about 11 times that of FourierFT.`,name:"n_frequency"},{anchor:"peft.FourierFTConfig.scaling",description:`<strong>scaling</strong> (<code>float</code>) &#x2014;
The scaling value for the delta W matrix. This is an important hyperparameter used for scaling, similar to
the &#x2018;lora_alpha&#x2019; parameter in the LoRA method. &#x2018;scaling&#x2019; can be determined during the hyperparameter search
process. However, if users want to skip this process, one can refer to the settings in the following
scenarios. This parameter can be set to 100.0 or 150.0 for both RoBERTa-base and RoBERTa-large models
across all NLU (GLUE) tasks. This parameter can be set to 300.0 for both LLaMA family models for all
instruction tuning. This parameter can be set to 300.0 for both ViT-base and ViT-large models across all
image classification tasks.`,name:"scaling"},{anchor:"peft.FourierFTConfig.random_loc_seed",description:`<strong>random_loc_seed</strong> (<code>int</code>) &#x2014;
Seed for the random location of the frequencies, i.e., the spectral entry matrix.`,name:"random_loc_seed"},{anchor:"peft.FourierFTConfig.target_modules",description:`<strong>target_modules</strong> (<code>Union[list[str],str]</code>) &#x2014;
List of module names or regex expression of the module names to replace with FourierFT. For example, [&#x2018;q&#x2019;,
&#x2018;v&#x2019;] or &#x2018;.<em>decoder.</em>(SelfAttention|EncDecAttention).*(q|v)$&#x2018;. Only linear layers are supported.`,name:"target_modules"},{anchor:"peft.FourierFTConfig.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.FourierFTConfig.fan_in_fan_out",description:`<strong>fan_in_fan_out</strong> (<code>bool</code>) &#x2014;
Set this to True if the layer to replace stores weight like (fan_in, fan_out).`,name:"fan_in_fan_out"},{anchor:"peft.FourierFTConfig.bias",description:`<strong>bias</strong> (<code>str</code>) &#x2014;
Bias type for FourierFT. Can be &#x2018;none&#x2019;, &#x2018;all&#x2019; or &#x2018;fourier_only&#x2019;.`,name:"bias"},{anchor:"peft.FourierFTConfig.modules_to_save",description:`<strong>modules_to_save</strong> (<code>list[str]</code>) &#x2014;
List of modules apart from FourierFT layers to be set as trainable and saved in the final checkpoint. For
example, in Sequence Classification or Token Classification tasks, the final layer <code>classifier/score</code> are
randomly initialized and as such need to be trainable and saved.`,name:"modules_to_save"},{anchor:"peft.FourierFTConfig.layers_to_transform",description:`<strong>layers_to_transform</strong> (<code>Union[list[int],int]</code>) &#x2014;
The layer indexes to transform, is this argument is specified, PEFT will transform only the layers indexes
that are specified inside this list. If a single integer is passed, PEFT will transform only the layer at
this index.`,name:"layers_to_transform"},{anchor:"peft.FourierFTConfig.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 to None and if the layer pattern is
not in the common layers pattern. 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.FourierFTConfig.n_frequency_pattern",description:`<strong>n_frequency_pattern</strong> (<code>dict</code>) &#x2014;
The mapping from layer names or regexp expression to n_frequency which are different from the default
specified. For example, <code>{model.decoder.layers.0.encoder_attn.k_proj: 1000</code>}.`,name:"n_frequency_pattern"},{anchor:"peft.FourierFTConfig.init_weights",description:`<strong>init_weights</strong> (<code>bool</code>) &#x2014;
The initialization of the Fourier weights. Set this to False (the default) if the spectrum are initialized
to a standard normal distribution. Set this to True if the spectrum are initialized to zeros.`,name:"init_weights"}],source:"https://github.com/huggingface/peft/blob/main/src/peft/tuners/fourierft/config.py#L25"}}),v=new le({props:{title:"FourierFTModel",local:"peft.FourierFTModel",headingTag:"h2"}}),w=new ye({props:{name:"class peft.FourierFTModel",anchor:"peft.FourierFTModel",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.FourierFTModel.model",description:"<strong>model</strong> (<code>torch.nn.Module</code>) &#x2014; The model to be adapted.",name:"model"},{anchor:"peft.FourierFTModel.config",description:'<strong>config</strong> (<a href="/docs/peft/main/en/package_reference/fourierft#peft.FourierFTConfig">FourierFTConfig</a>) &#x2014; The configuration of the FourierFT model.',name:"config"},{anchor:"peft.FourierFTModel.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.FourierFTModel.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/main/src/peft/tuners/fourierft/model.py#L31",returnDescription:`<script context="module">export const metadata = 'undefined';<\/script>


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


<p><code>torch.nn.Module</code></p>
`}}),$=new Me({props:{source:"https://github.com/huggingface/peft/blob/main/docs/source/package_reference/fourierft.md"}}),{c(){m=i("meta"),N=r(),H=i("p"),S=r(),P(u.$$.fragment),W=r(),h=i("p"),h.innerHTML=ce,z=r(),g=i("p"),g.textContent=de,B=r(),_=i("ul"),_.innerHTML=me,j=r(),F=i("p"),F.textContent=pe,I=r(),T=i("p"),T.textContent=ue,V=r(),x=i("blockquote"),x.innerHTML=he,G=r(),P(y.$$.fragment),Q=r(),p=i("div"),P(b.$$.fragment),oe=r(),C=i("p"),C.innerHTML=ge,K=r(),P(v.$$.fragment),J=r(),l=i("div"),P(w.$$.fragment),re=r(),L=i("p"),L.textContent=_e,ae=r(),M=i("p"),M.innerHTML=Fe,ie=r(),k=i("p"),k.innerHTML=Te,se=r(),q=i("ul"),q.innerHTML=xe,X=r(),P($.$$.fragment),Y=r(),D=i("p"),this.h()},l(e){const t=Le("svelte-u9bgzb",document.head);m=s(t,"META",{name:!0,content:!0}),t.forEach(n),N=a(e),H=s(e,"P",{}),te(H).forEach(n),S=a(e),A(u.$$.fragment,e),W=a(e),h=s(e,"P",{"data-svelte-h":!0}),f(h)!=="svelte-1our0gb"&&(h.innerHTML=ce),z=a(e),g=s(e,"P",{"data-svelte-h":!0}),f(g)!=="svelte-7ls0zq"&&(g.textContent=de),B=a(e),_=s(e,"UL",{"data-svelte-h":!0}),f(_)!=="svelte-8km6hk"&&(_.innerHTML=me),j=a(e),F=s(e,"P",{"data-svelte-h":!0}),f(F)!=="svelte-hr8j55"&&(F.textContent=pe),I=a(e),T=s(e,"P",{"data-svelte-h":!0}),f(T)!=="svelte-1cwsb16"&&(T.textContent=ue),V=a(e),x=s(e,"BLOCKQUOTE",{"data-svelte-h":!0}),f(x)!=="svelte-1lxn511"&&(x.innerHTML=he),G=a(e),A(y.$$.fragment,e),Q=a(e),p=s(e,"DIV",{class:!0});var ee=te(p);A(b.$$.fragment,ee),oe=a(ee),C=s(ee,"P",{"data-svelte-h":!0}),f(C)!=="svelte-1j2s7ln"&&(C.innerHTML=ge),ee.forEach(n),K=a(e),A(v.$$.fragment,e),J=a(e),l=s(e,"DIV",{class:!0});var d=te(l);A(w.$$.fragment,d),re=a(d),L=s(d,"P",{"data-svelte-h":!0}),f(L)!=="svelte-16dbami"&&(L.textContent=_e),ae=a(d),M=s(d,"P",{"data-svelte-h":!0}),f(M)!=="svelte-15mietp"&&(M.innerHTML=Fe),ie=a(d),k=s(d,"P",{"data-svelte-h":!0}),f(k)!=="svelte-1xx6nm4"&&(k.innerHTML=Te),se=a(d),q=s(d,"UL",{"data-svelte-h":!0}),f(q)!=="svelte-1ffj8s8"&&(q.innerHTML=xe),d.forEach(n),X=a(e),A($.$$.fragment,e),Y=a(e),D=s(e,"P",{}),te(D).forEach(n),this.h()},h(){ne(m,"name","hf:doc:metadata"),ne(m,"content",qe),ne(p,"class","docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"),ne(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){c(document.head,m),o(e,N,t),o(e,H,t),o(e,S,t),O(u,e,t),o(e,W,t),o(e,h,t),o(e,z,t),o(e,g,t),o(e,B,t),o(e,_,t),o(e,j,t),o(e,F,t),o(e,I,t),o(e,T,t),o(e,V,t),o(e,x,t),o(e,G,t),O(y,e,t),o(e,Q,t),o(e,p,t),O(b,p,null),c(p,oe),c(p,C),o(e,K,t),O(v,e,t),o(e,J,t),o(e,l,t),O(w,l,null),c(l,re),c(l,L),c(l,ae),c(l,M),c(l,ie),c(l,k),c(l,se),c(l,q),o(e,X,t),O($,e,t),o(e,Y,t),o(e,D,t),Z=!0},p:ve,i(e){Z||(U(u.$$.fragment,e),U(y.$$.fragment,e),U(b.$$.fragment,e),U(v.$$.fragment,e),U(w.$$.fragment,e),U($.$$.fragment,e),Z=!0)},o(e){E(u.$$.fragment,e),E(y.$$.fragment,e),E(b.$$.fragment,e),E(v.$$.fragment,e),E(w.$$.fragment,e),E($.$$.fragment,e),Z=!1},d(e){e&&(n(N),n(H),n(S),n(W),n(h),n(z),n(g),n(B),n(_),n(j),n(F),n(I),n(T),n(V),n(x),n(G),n(Q),n(p),n(K),n(J),n(l),n(X),n(Y),n(D)),n(m),R(u,e),R(y,e),R(b),R(v,e),R(w),R($,e)}}}const qe='{"title":"FourierFT: Discrete Fourier Transformation Fine-Tuning","local":"fourierft-discrete-fourier-transformation-fine-tuning","sections":[{"title":"FourierFTConfig","local":"peft.FourierFTConfig","sections":[],"depth":2},{"title":"FourierFTModel","local":"peft.FourierFTModel","sections":[],"depth":2}],"depth":1}';function Pe(fe){return we(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Re extends $e{constructor(m){super(),Ce(this,m,Pe,ke,be,{})}}export{Re as component};
