import{s as Me,o as _e,n as be}from"../chunks/scheduler.4cd3e004.js";import{S as we,i as je,e as p,s as o,c as M,h as Ce,a as c,d as n,b as l,f as V,g as _,j as b,k as G,l as d,m as a,n as w,t as j,o as C,p as U}from"../chunks/index.b664cb2d.js";import{D as pe}from"../chunks/Docstring.9ceb4334.js";import{C as Ue}from"../chunks/CodeBlock.8601e34e.js";import{E as ve}from"../chunks/ExampleCodeBlock.4f57b575.js";import{H as ce,E as xe}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.179b63f8.js";function Je(H){let i,J="Example:",u,m,f;return m=new Ue({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMFN0YWJsZURpZmZ1c2lvblBpcGVsaW5lJTBBZnJvbSUyMHBlZnQlMjBpbXBvcnQlMjBPRlRNb2RlbCUyQyUyME9GVENvbmZpZyUwQSUwQWNvbmZpZ190ZSUyMCUzRCUyME9GVENvbmZpZyglMEElMjAlMjAlMjAlMjByJTNEOCUyQyUwQSUyMCUyMCUyMCUyMHRhcmdldF9tb2R1bGVzJTNEJTVCJTIya19wcm9qJTIyJTJDJTIwJTIycV9wcm9qJTIyJTJDJTIwJTIydl9wcm9qJTIyJTJDJTIwJTIyb3V0X3Byb2olMjIlMkMlMjAlMjJmYzElMjIlMkMlMjAlMjJmYzIlMjIlNUQlMkMlMEElMjAlMjAlMjAlMjBtb2R1bGVfZHJvcG91dCUzRDAuMCUyQyUwQSUyMCUyMCUyMCUyMGluaXRfd2VpZ2h0cyUzRFRydWUlMkMlMEEpJTBBY29uZmlnX3VuZXQlMjAlM0QlMjBPRlRDb25maWcoJTBBJTIwJTIwJTIwJTIwciUzRDglMkMlMEElMjAlMjAlMjAlMjB0YXJnZXRfbW9kdWxlcyUzRCU1QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMnByb2pfaW4lMjIlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjJwcm9qX291dCUyMiUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMnRvX2slMjIlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjJ0b19xJTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIydG9fdiUyMiUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMnRvX291dC4wJTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIyZmYubmV0LjAucHJvaiUyMiUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMmZmLm5ldC4yJTIyJTJDJTBBJTIwJTIwJTIwJTIwJTVEJTJDJTBBJTIwJTIwJTIwJTIwbW9kdWxlX2Ryb3BvdXQlM0QwLjAlMkMlMEElMjAlMjAlMjAlMjBpbml0X3dlaWdodHMlM0RUcnVlJTJDJTBBKSUwQSUwQW1vZGVsJTIwJTNEJTIwU3RhYmxlRGlmZnVzaW9uUGlwZWxpbmUuZnJvbV9wcmV0cmFpbmVkKCUyMnJ1bndheW1sJTJGc3RhYmxlLWRpZmZ1c2lvbi12MS01JTIyKSUwQW1vZGVsLnRleHRfZW5jb2RlciUyMCUzRCUyME9GVE1vZGVsKG1vZGVsLnRleHRfZW5jb2RlciUyQyUyMGNvbmZpZ190ZSUyQyUyMCUyMmRlZmF1bHQlMjIpJTBBbW9kZWwudW5ldCUyMCUzRCUyME9GVE1vZGVsKG1vZGVsLnVuZXQlMkMlMjBjb25maWdfdW5ldCUyQyUyMCUyMmRlZmF1bHQlMjIp",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> OFTModel, OFTConfig

<span class="hljs-meta">&gt;&gt;&gt; </span>config_te = OFTConfig(
<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>    module_dropout=<span class="hljs-number">0.0</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 = OFTConfig(
<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>    module_dropout=<span class="hljs-number">0.0</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 = OFTModel(model.text_encoder, config_te, <span class="hljs-string">&quot;default&quot;</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>model.unet = OFTModel(model.unet, config_unet, <span class="hljs-string">&quot;default&quot;</span>)`,wrap:!1}}),{c(){i=p("p"),i.textContent=J,u=o(),M(m.$$.fragment)},l(s){i=c(s,"P",{"data-svelte-h":!0}),b(i)!=="svelte-11lpom8"&&(i.textContent=J),u=l(s),_(m.$$.fragment,s)},m(s,h){a(s,i,h),a(s,u,h),w(m,s,h),f=!0},p:be,i(s){f||(j(m.$$.fragment,s),f=!0)},o(s){C(m.$$.fragment,s),f=!1},d(s){s&&(n(i),n(u)),U(m,s)}}}function Oe(H){let i,J,u,m,f,s,h,de='<a href="https://hf.co/papers/2306.07280" rel="nofollow">Orthogonal Finetuning (OFT)</a> is a method developed for adapting text-to-image diffusion models. It works by reparameterizing the pretrained weight matrices with its orthogonal matrix to preserve information in the pretrained model. To reduce the number of parameters, OFT introduces a block-diagonal structure in the orthogonal matrix.',z,O,me="The abstract from the paper is:",N,F,fe="<em>Large text-to-image diffusion models have impressive capabilities in generating photorealistic images from text prompts. How to effectively guide or control these powerful models to perform different downstream tasks becomes an important open problem. To tackle this challenge, we introduce a principled finetuning method — Orthogonal Finetuning (OFT), for adapting text-to-image diffusion models to downstream tasks. Unlike existing methods, OFT can provably preserve hyperspherical energy which characterizes the pairwise neuron relationship on the unit hypersphere. We find that this property is crucial for preserving the semantic generation ability of text-to-image diffusion models. To improve finetuning stability, we further propose Constrained Orthogonal Finetuning (COFT) which imposes an additional radius constraint to the hypersphere. Specifically, we consider two important finetuning text-to-image tasks: subject-driven generation where the goal is to generate subject-specific images given a few images of a subject and a text prompt, and controllable generation where the goal is to enable the model to take in additional control signals. We empirically show that our OFT framework outperforms existing methods in generation quality and convergence speed</em>.",D,$,S,g,k,ne,A,he='This is the configuration class to store the configuration of a <a href="/docs/peft/main/en/package_reference/oft#peft.OFTModel">OFTModel</a>.',ae,v,I,se,R,ge="Check if the kwargs are valid for the configuration.",P,q,X,r,Z,oe,B,ue=`Creates Orthogonal Finetuning model from a pretrained model. The method is described in
<a href="https://huggingface.co/papers/2306.07280" rel="nofollow">https://huggingface.co/papers/2306.07280</a>`,le,x,ie,L,ye="<strong>Attributes</strong>:",re,W,Te='<li><strong>model</strong> (<code>~torch.nn.Module</code>) — The model to be adapted.</li> <li><strong>peft_config</strong> (<a href="/docs/peft/main/en/package_reference/oft#peft.OFTConfig">OFTConfig</a>): The configuration of the OFT model.</li>',Y,E,K,Q,ee;return f=new ce({props:{title:"OFT",local:"oft",headingTag:"h1"}}),$=new ce({props:{title:"OFTConfig",local:"peft.OFTConfig",headingTag:"h2"}}),k=new pe({props:{name:"class peft.OFTConfig",anchor:"peft.OFTConfig",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 = 0"},{name:"oft_block_size",val:": int = 32"},{name:"module_dropout",val:": float = 0.0"},{name:"target_modules",val:": Optional[Union[list[str], str]] = None"},{name:"fan_in_fan_out",val:": bool = False"},{name:"bias",val:": Literal['none', 'all', 'oft_only'] = '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:"modules_to_save",val:": Optional[list[str]] = None"},{name:"coft",val:": bool = False"},{name:"eps",val:": float = 6e-05"},{name:"block_share",val:": bool = False"},{name:"use_cayley_neumann",val:": bool = True"},{name:"num_cayley_neumann_terms",val:": int = 5"}],parametersDescription:[{anchor:"peft.OFTConfig.r",description:"<strong>r</strong> (<code>int</code>) &#x2014; OFT rank, number of OFT blocks per injected layer.",name:"r"},{anchor:"peft.OFTConfig.oft_block_size",description:"<strong>oft_block_size</strong> (<code>int</code>) &#x2014; OFT block size across different layers.",name:"oft_block_size"},{anchor:"peft.OFTConfig.module_dropout",description:`<strong>module_dropout</strong> (<code>float</code>) &#x2014;
The multiplicative dropout probability, by setting OFT blocks to identity during training, similar to the
dropout layer in LoRA.`,name:"module_dropout"},{anchor:"peft.OFTConfig.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.OFTConfig.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.OFTConfig.bias",description:`<strong>bias</strong> (<code>str</code>) &#x2014; Bias type for OFT. Can be &#x2018;none&#x2019;, &#x2018;all&#x2019; or &#x2018;oft_only&#x2019;. If &#x2018;all&#x2019; or &#x2018;oft_only&#x2019;, the
corresponding biases will be updated during training. Be aware that this means that, even when disabling
the adapters, the model will not produce the same output as the base model would have without adaptation.`,name:"bias"},{anchor:"peft.OFTConfig.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.OFTConfig.init_weights",description:`<strong>init_weights</strong> (<code>bool</code>) &#x2014;
Whether to perform initialization of OFT weights.`,name:"init_weights"},{anchor:"peft.OFTConfig.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.OFTConfig.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.OFTConfig.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"},{anchor:"peft.OFTConfig.coft",description:`<strong>coft</strong> (<code>bool</code>) &#x2014;
Whether to use the constrained variant of OFT or not, off by default.`,name:"coft"},{anchor:"peft.OFTConfig.eps",description:`<strong>eps</strong> (<code>float</code>) &#x2014;
The control strength of COFT. The freedom of rotation. Only has an effect if <code>coft</code> is set to True.`,name:"eps"},{anchor:"peft.OFTConfig.block_share",description:`<strong>block_share</strong> (<code>bool</code>) &#x2014;
Whether to share the OFT parameters between blocks or not. This is <code>False</code> by default.`,name:"block_share"}],source:"https://github.com/huggingface/peft/blob/main/src/peft/tuners/oft/config.py#L25"}}),I=new pe({props:{name:"check_kwargs",anchor:"peft.OFTConfig.check_kwargs",parameters:[{name:"**kwargs",val:""}],parametersDescription:[{anchor:"peft.OFTConfig.check_kwargs.kwargs",description:`<strong>kwargs</strong> (additional keyword arguments, <em>optional</em>) &#x2014;
Additional keyword arguments passed along to the child class initialization.`,name:"kwargs"}],source:"https://github.com/huggingface/peft/blob/main/src/peft/tuners/oft/config.py#L181"}}),q=new ce({props:{title:"OFTModel",local:"peft.OFTModel",headingTag:"h2"}}),Z=new pe({props:{name:"class peft.OFTModel",anchor:"peft.OFTModel",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.OFTModel.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.OFTModel.config",description:'<strong>config</strong> (<a href="/docs/peft/main/en/package_reference/oft#peft.OFTConfig">OFTConfig</a>) &#x2014; The configuration of the OFT model.',name:"config"},{anchor:"peft.OFTModel.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.OFTModel.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/oft/model.py#L34",returnDescription:`<script context="module">export const metadata = 'undefined';<\/script>


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


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