import{S as yn,i as bn,s as En,e as a,k as u,w as D,t as r,M as An,c as i,d as t,m as h,a as n,x,h as l,b as p,N as $n,G as o,g as f,y as P,q as S,o as T,B as I,v as Ln}from"../../chunks/vendor-hf-doc-builder.js";import{T as vn}from"../../chunks/Tip-hf-doc-builder.js";import{I as Eo}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as si}from"../../chunks/CodeBlock-hf-doc-builder.js";function kn($e){let d,_;return{c(){d=a("p"),_=r(`LoRA allows us to achieve greater memory efficiency since the pretrained weights are kept frozen and only the LoRA weights are trained, thereby
allowing us to run fine-tuning on consumer GPUs like Tesla T4, RTX 3080 or even RTX 2080 Ti! One can get access to GPUs like T4 in the free
tiers of Kaggle Kernels and Google Colab Notebooks.`)},l(c){d=i(c,"P",{});var m=n(d);_=l(m,`LoRA allows us to achieve greater memory efficiency since the pretrained weights are kept frozen and only the LoRA weights are trained, thereby
allowing us to run fine-tuning on consumer GPUs like Tesla T4, RTX 3080 or even RTX 2080 Ti! One can get access to GPUs like T4 in the free
tiers of Kaggle Kernels and Google Colab Notebooks.`),m.forEach(t)},m(c,m){f(c,d,m),o(d,_)},d(c){c&&t(d)}}}function Rn($e){let d,_;return{c(){d=a("p"),_=r("When using LoRA we can use a much higher learning rate (typically 1e-4 as opposed to ~1e-6) compared to non-LoRA Dreambooth fine-tuning.")},l(c){d=i(c,"P",{});var m=n(d);_=l(m,"When using LoRA we can use a much higher learning rate (typically 1e-4 as opposed to ~1e-6) compared to non-LoRA Dreambooth fine-tuning."),m.forEach(t)},m(c,m){f(c,d,m),o(d,_)},d(c){c&&t(d)}}}function Dn($e){let d,_,c,m,ze,Y,Ao,We,$o,$t,Le,Lo,Lt,g,ko,V,Ro,Do,He,xo,Po,kt,w,So,Ke,To,Io,Ye,No,jo,Rt,v,X,qo,Z,Co,Mo,Oo,Ve,Uo,Go,J,Bo,Xe,zo,Wo,Dt,ke,Ze,Je,Ho,xt,A,F,Ko,Yo,Q,Vo,Xo,Pt,N,St,$,j,Fe,ee,Zo,Qe,Jo,Tt,Re,Fo,It,y,et,te,Qo,ea,tt,oe,ta,oa,ot,ae,aa,Nt,De,ia,jt,q,at,ie,na,ra,it,ne,la,qt,xe,sa,Ct,re,Mt,C,fa,nt,pa,ua,Ot,Pe,ha,Ut,M,Gt,Se,da,Bt,O,Te,ca,le,rt,ma,_a,lt,ga,wa,Ie,va,se,st,ya,ba,ft,Ea,zt,U,Aa,fe,$a,La,Wt,G,ka,pe,Ra,Da,Ht,L,B,pt,ue,xa,ut,Pa,Kt,b,Sa,ht,Ta,Ia,he,Na,ja,Yt,de,Vt,Ne,qa,Xt,je,fi,Zt,k,ce,dt,Ca,Ma,me,Oa,Ua,Jt,E,Ga,_e,ct,Ba,za,ge,mt,Wa,Ha,Ft,we,Qt,z,Ka,_t,Ya,Va,eo,W,Xa,gt,Za,Ja,to,H,Fa,ve,Qa,ei,oo,R,K,wt,ye,ti,vt,oi,ao,qe,be,ai,Ee,yt,ii,ni,io;return Y=new Eo({}),N=new vn({props:{$$slots:{default:[kn]},$$scope:{ctx:$e}}}),ee=new Eo({}),re=new si({props:{code:`export MODEL_NAME="runwayml/stable-diffusion-v1-5"
export INSTANCE_DIR="path-to-instance-images"
export OUTPUT_DIR="path-to-save-model"

accelerate launch train_dreambooth_lora.py \\
  --pretrained_model_name_or_path=$MODEL_NAME  \\
  --instance_data_dir=$INSTANCE_DIR \\
  --output_dir=$OUTPUT_DIR \\
  --instance_prompt="a photo of sks dog" \\
  --resolution=512 \\
  --train_batch_size=1 \\
  --gradient_accumulation_steps=1 \\
  --checkpointing_steps=100 \\
  --learning_rate=1e-4 \\
  --report_to="wandb" \\
  --lr_scheduler="constant" \\
  --lr_warmup_steps=0 \\
  --max_train_steps=500 \\
  --validation_prompt="A photo of sks dog in a bucket" \\
  --validation_epochs=50 \\
  --seed="0" \\
  --push_to_hub`,highlighted:`<span class="hljs-built_in">export</span> MODEL_NAME=<span class="hljs-string">&quot;runwayml/stable-diffusion-v1-5&quot;</span>
<span class="hljs-built_in">export</span> INSTANCE_DIR=<span class="hljs-string">&quot;path-to-instance-images&quot;</span>
<span class="hljs-built_in">export</span> OUTPUT_DIR=<span class="hljs-string">&quot;path-to-save-model&quot;</span>

accelerate launch train_dreambooth_lora.py \\
  --pretrained_model_name_or_path=<span class="hljs-variable">$MODEL_NAME</span>  \\
  --instance_data_dir=<span class="hljs-variable">$INSTANCE_DIR</span> \\
  --output_dir=<span class="hljs-variable">$OUTPUT_DIR</span> \\
  --instance_prompt=<span class="hljs-string">&quot;a photo of sks dog&quot;</span> \\
  --resolution=512 \\
  --train_batch_size=1 \\
  --gradient_accumulation_steps=1 \\
  --checkpointing_steps=100 \\
  --learning_rate=1e-4 \\
  --report_to=<span class="hljs-string">&quot;wandb&quot;</span> \\
  --lr_scheduler=<span class="hljs-string">&quot;constant&quot;</span> \\
  --lr_warmup_steps=0 \\
  --max_train_steps=500 \\
  --validation_prompt=<span class="hljs-string">&quot;A photo of sks dog in a bucket&quot;</span> \\
  --validation_epochs=50 \\
  --seed=<span class="hljs-string">&quot;0&quot;</span> \\
  --push_to_hub`}}),M=new vn({props:{$$slots:{default:[Rn]},$$scope:{ctx:$e}}}),ue=new Eo({}),de=new si({props:{code:`from diffusers import StableDiffusionPipeline
import torch

model_path = "sayakpaul/sd-model-finetuned-lora-t4"
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", torch_dtype=torch.float16)
pipe.unet.load_attn_procs(model_path)
pipe.to("cuda")

prompt = "A pokemon with blue eyes."
image = pipe(prompt, num_inference_steps=30, guidance_scale=7.5).images[0]
image.save("pokemon.png")`,highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionPipeline
<span class="hljs-keyword">import</span> torch

model_path = <span class="hljs-string">&quot;sayakpaul/sd-model-finetuned-lora-t4&quot;</span>
pipe = StableDiffusionPipeline.from_pretrained(<span class="hljs-string">&quot;CompVis/stable-diffusion-v1-4&quot;</span>, torch_dtype=torch.float16)
pipe.unet.load_attn_procs(model_path)
pipe.to(<span class="hljs-string">&quot;cuda&quot;</span>)

prompt = <span class="hljs-string">&quot;A pokemon with blue eyes.&quot;</span>
image = pipe(prompt, num_inference_steps=<span class="hljs-number">30</span>, guidance_scale=<span class="hljs-number">7.5</span>).images[<span class="hljs-number">0</span>]
image.save(<span class="hljs-string">&quot;pokemon.png&quot;</span>)`}}),we=new si({props:{code:`from huggingface_hub.repocard import RepoCard

card = RepoCard.load("sayakpaul/sd-model-finetuned-lora-t4")
base_model = card.data.to_dict()["base_model"]
# 'CompVis/stable-diffusion-v1-4'`,highlighted:`<span class="hljs-keyword">from</span> huggingface_hub.repocard <span class="hljs-keyword">import</span> RepoCard

card = RepoCard.load(<span class="hljs-string">&quot;sayakpaul/sd-model-finetuned-lora-t4&quot;</span>)
base_model = card.data.to_dict()[<span class="hljs-string">&quot;base_model&quot;</span>]
<span class="hljs-comment"># &#x27;CompVis/stable-diffusion-v1-4&#x27;</span>`}}),ye=new Eo({}),{c(){d=a("meta"),_=u(),c=a("h1"),m=a("a"),ze=a("span"),D(Y.$$.fragment),Ao=u(),We=a("span"),$o=r("LoRA Support in Diffusers"),$t=u(),Le=a("p"),Lo=r("Diffusers supports LoRA for faster fine-tuning of Stable Diffusion, allowing greater memory efficiency and easier portability."),Lt=u(),g=a("p"),ko=r(`Low-Rank Adaption of Large Language Models was first introduced by Microsoft in
`),V=a("a"),Ro=r("LoRA: Low-Rank Adaptation of Large Language Models"),Do=r(" by "),He=a("em"),xo=r("Edward J. Hu, Yelong Shen, Phillip Wallis, Zeyuan Allen-Zhu, Yuanzhi Li, Shean Wang, Lu Wang, Weizhu Chen"),Po=r("."),kt=u(),w=a("p"),So=r("In a nutshell, LoRA allows adapting pretrained models by adding pairs of rank-decomposition weight matrices (called "),Ke=a("strong"),To=r("update matrices"),Io=r(`)
to existing weights and `),Ye=a("strong"),No=r("only"),jo=r(" training those newly added weights. This has a couple of advantages:"),Rt=u(),v=a("ul"),X=a("li"),qo=r("Previous pretrained weights are kept frozen so that the model is not so prone to "),Z=a("a"),Co=r("catastrophic forgetting"),Mo=r("."),Oo=u(),Ve=a("li"),Uo=r("Rank-decomposition matrices have significantly fewer parameters than the original model, which means that trained LoRA weights are easily portable."),Go=u(),J=a("li"),Bo=r("LoRA matrices are generally added to the attention layers of the original model and they control to which extent the model is adapted toward new training images via a "),Xe=a("code"),zo=r("scale"),Wo=r(" parameter."),Dt=u(),ke=a("p"),Ze=a("strong"),Je=a("strong"),Ho=r(`Note that the usage of LoRA is not just limited to attention layers. In the original LoRA work, the authors found out that just amending
the attention layers of a language model is sufficient to obtain good downstream performance with great efficiency. This is why, it\u2019s common
to just add the LoRA weights to the attention layers of a model.`),xt=u(),A=a("p"),F=a("a"),Ko=r("cloneofsimo"),Yo=r(" was the first to try out LoRA training for Stable Diffusion in the popular "),Q=a("a"),Vo=r("lora"),Xo=r(" GitHub repository."),Pt=u(),D(N.$$.fragment),St=u(),$=a("h2"),j=a("a"),Fe=a("span"),D(ee.$$.fragment),Zo=u(),Qe=a("span"),Jo=r("Getting started with LoRA for fine-tuning"),Tt=u(),Re=a("p"),Fo=r("Stable Diffusion can be fine-tuned in different ways:"),It=u(),y=a("ul"),et=a("li"),te=a("a"),Qo=r("Textual inversion"),ea=u(),tt=a("li"),oe=a("a"),ta=r("DreamBooth"),oa=u(),ot=a("li"),ae=a("a"),aa=r("Text2Image fine-tuning"),Nt=u(),De=a("p"),ia=r("We provide two end-to-end examples that show how to run fine-tuning with LoRA:"),jt=u(),q=a("ul"),at=a("li"),ie=a("a"),na=r("DreamBooth"),ra=u(),it=a("li"),ne=a("a"),la=r("Text2Image"),qt=u(),xe=a("p"),sa=r("If you want to perform DreamBooth training with LoRA, for instance, you would run:"),Ct=u(),D(re.$$.fragment),Mt=u(),C=a("p"),fa=r(`A similar process can be followed to fully fine-tune Stable Diffusion on a custom dataset using the
`),nt=a("code"),pa=r("examples/text_to_image/train_text_to_image_lora.py"),ua=r(" script."),Ot=u(),Pe=a("p"),ha=r("Refer to the respective examples linked above to learn more."),Ut=u(),D(M.$$.fragment),Gt=u(),Se=a("p"),da=r(`But there is no free lunch. For the given dataset and expected generation quality, you\u2019d still need to experiment with
different hyperparameters. Here are some important ones:`),Bt=u(),O=a("ul"),Te=a("li"),ca=r("Training time"),le=a("ul"),rt=a("li"),ma=r("Learning rate"),_a=u(),lt=a("li"),ga=r("Number of training steps"),wa=u(),Ie=a("li"),va=r("Inference time "),se=a("ul"),st=a("li"),ya=r("Number of steps"),ba=u(),ft=a("li"),Ea=r("Scheduler type"),zt=u(),U=a("p"),Aa=r("Additionally, you can follow "),fe=a("a"),$a=r("this blog"),La=r(` that documents some of our experimental
findings for performing DreamBooth training of Stable Diffusion.`),Wt=u(),G=a("p"),ka=r(`When fine-tuning, the LoRA update matrices are only added to the attention layers. To enable this, we added new weight
loading functionalities. Their details are available `),pe=a("a"),Ra=r("here"),Da=r("."),Ht=u(),L=a("h2"),B=a("a"),pt=a("span"),D(ue.$$.fragment),xa=u(),ut=a("span"),Pa=r("Inference"),Kt=u(),b=a("p"),Sa=r("Assuming you used the "),ht=a("code"),Ta=r("examples/text_to_image/train_text_to_image_lora.py"),Ia=r(" to fine-tune Stable Diffusion on the "),he=a("a"),Na=r(`Pokemon
dataset`),ja=r(", you can perform inference like so:"),Yt=u(),D(de.$$.fragment),Vt=u(),Ne=a("p"),qa=r("Here are some example images you can expect:"),Xt=u(),je=a("img"),Zt=u(),k=a("p"),ce=a("a"),dt=a("code"),Ca=r("sayakpaul/sd-model-finetuned-lora-t4"),Ma=r(" contains "),me=a("a"),Oa=r("LoRA fine-tuned update matrices"),Ua=r(`
which is only 3 MBs in size. During inference, the pre-trained Stable Diffusion checkpoints are loaded alongside these update
matrices and then they are combined to run inference.`),Jt=u(),E=a("p"),Ga=r("You can use the "),_e=a("a"),ct=a("code"),Ba=r("huggingface_hub"),za=r(` library to retrieve the base model
from `),ge=a("a"),mt=a("code"),Wa=r("sayakpaul/sd-model-finetuned-lora-t4"),Ha=r(" like so:"),Ft=u(),D(we.$$.fragment),Qt=u(),z=a("p"),Ka=r("And then you can use "),_t=a("code"),Ya=r("pipe = StableDiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.float16)"),Va=r("."),eo=u(),W=a("p"),Xa=r("This is especially useful when you don\u2019t want to hardcode the base model identifier during initializing the "),gt=a("code"),Za=r("StableDiffusionPipeline"),Ja=r("."),to=u(),H=a("p"),Fa=r(`Inference for DreamBooth training remains the same. Check
`),ve=a("a"),Qa=r("this section"),ei=r(" for more details."),oo=u(),R=a("h2"),K=a("a"),wt=a("span"),D(ye.$$.fragment),ti=u(),vt=a("span"),oi=r("Known limitations"),ao=u(),qe=a("ul"),be=a("li"),ai=r("Currently, we only support LoRA for the attention layers of "),Ee=a("a"),yt=a("code"),ii=r("UNet2DConditionModel"),ni=r("."),this.h()},l(e){const s=An('[data-svelte="svelte-1phssyn"]',document.head);d=i(s,"META",{name:!0,content:!0}),s.forEach(t),_=h(e),c=i(e,"H1",{class:!0});var Ae=n(c);m=i(Ae,"A",{id:!0,class:!0,href:!0});var bt=n(m);ze=i(bt,"SPAN",{});var pi=n(ze);x(Y.$$.fragment,pi),pi.forEach(t),bt.forEach(t),Ao=h(Ae),We=i(Ae,"SPAN",{});var ui=n(We);$o=l(ui,"LoRA Support in Diffusers"),ui.forEach(t),Ae.forEach(t),$t=h(e),Le=i(e,"P",{});var hi=n(Le);Lo=l(hi,"Diffusers supports LoRA for faster fine-tuning of Stable Diffusion, allowing greater memory efficiency and easier portability."),hi.forEach(t),Lt=h(e),g=i(e,"P",{});var Ce=n(g);ko=l(Ce,`Low-Rank Adaption of Large Language Models was first introduced by Microsoft in
`),V=i(Ce,"A",{href:!0,rel:!0});var di=n(V);Ro=l(di,"LoRA: Low-Rank Adaptation of Large Language Models"),di.forEach(t),Do=l(Ce," by "),He=i(Ce,"EM",{});var ci=n(He);xo=l(ci,"Edward J. Hu, Yelong Shen, Phillip Wallis, Zeyuan Allen-Zhu, Yuanzhi Li, Shean Wang, Lu Wang, Weizhu Chen"),ci.forEach(t),Po=l(Ce,"."),Ce.forEach(t),kt=h(e),w=i(e,"P",{});var Me=n(w);So=l(Me,"In a nutshell, LoRA allows adapting pretrained models by adding pairs of rank-decomposition weight matrices (called "),Ke=i(Me,"STRONG",{});var mi=n(Ke);To=l(mi,"update matrices"),mi.forEach(t),Io=l(Me,`)
to existing weights and `),Ye=i(Me,"STRONG",{});var _i=n(Ye);No=l(_i,"only"),_i.forEach(t),jo=l(Me," training those newly added weights. This has a couple of advantages:"),Me.forEach(t),Rt=h(e),v=i(e,"UL",{});var Oe=n(v);X=i(Oe,"LI",{});var no=n(X);qo=l(no,"Previous pretrained weights are kept frozen so that the model is not so prone to "),Z=i(no,"A",{href:!0,rel:!0});var gi=n(Z);Co=l(gi,"catastrophic forgetting"),gi.forEach(t),Mo=l(no,"."),no.forEach(t),Oo=h(Oe),Ve=i(Oe,"LI",{});var wi=n(Ve);Uo=l(wi,"Rank-decomposition matrices have significantly fewer parameters than the original model, which means that trained LoRA weights are easily portable."),wi.forEach(t),Go=h(Oe),J=i(Oe,"LI",{});var ro=n(J);Bo=l(ro,"LoRA matrices are generally added to the attention layers of the original model and they control to which extent the model is adapted toward new training images via a "),Xe=i(ro,"CODE",{});var vi=n(Xe);zo=l(vi,"scale"),vi.forEach(t),Wo=l(ro," parameter."),ro.forEach(t),Oe.forEach(t),Dt=h(e),ke=i(e,"P",{});var yi=n(ke);Ze=i(yi,"STRONG",{});var bi=n(Ze);Je=i(bi,"STRONG",{});var Ei=n(Je);Ho=l(Ei,`Note that the usage of LoRA is not just limited to attention layers. In the original LoRA work, the authors found out that just amending
the attention layers of a language model is sufficient to obtain good downstream performance with great efficiency. This is why, it\u2019s common
to just add the LoRA weights to the attention layers of a model.`),Ei.forEach(t),bi.forEach(t),yi.forEach(t),xt=h(e),A=i(e,"P",{});var Et=n(A);F=i(Et,"A",{href:!0,rel:!0});var Ai=n(F);Ko=l(Ai,"cloneofsimo"),Ai.forEach(t),Yo=l(Et," was the first to try out LoRA training for Stable Diffusion in the popular "),Q=i(Et,"A",{href:!0,rel:!0});var $i=n(Q);Vo=l($i,"lora"),$i.forEach(t),Xo=l(Et," GitHub repository."),Et.forEach(t),Pt=h(e),x(N.$$.fragment,e),St=h(e),$=i(e,"H2",{class:!0});var lo=n($);j=i(lo,"A",{id:!0,class:!0,href:!0});var Li=n(j);Fe=i(Li,"SPAN",{});var ki=n(Fe);x(ee.$$.fragment,ki),ki.forEach(t),Li.forEach(t),Zo=h(lo),Qe=i(lo,"SPAN",{});var Ri=n(Qe);Jo=l(Ri,"Getting started with LoRA for fine-tuning"),Ri.forEach(t),lo.forEach(t),Tt=h(e),Re=i(e,"P",{});var Di=n(Re);Fo=l(Di,"Stable Diffusion can be fine-tuned in different ways:"),Di.forEach(t),It=h(e),y=i(e,"UL",{});var Ue=n(y);et=i(Ue,"LI",{});var xi=n(et);te=i(xi,"A",{href:!0,rel:!0});var Pi=n(te);Qo=l(Pi,"Textual inversion"),Pi.forEach(t),xi.forEach(t),ea=h(Ue),tt=i(Ue,"LI",{});var Si=n(tt);oe=i(Si,"A",{href:!0,rel:!0});var Ti=n(oe);ta=l(Ti,"DreamBooth"),Ti.forEach(t),Si.forEach(t),oa=h(Ue),ot=i(Ue,"LI",{});var Ii=n(ot);ae=i(Ii,"A",{href:!0,rel:!0});var Ni=n(ae);aa=l(Ni,"Text2Image fine-tuning"),Ni.forEach(t),Ii.forEach(t),Ue.forEach(t),Nt=h(e),De=i(e,"P",{});var ji=n(De);ia=l(ji,"We provide two end-to-end examples that show how to run fine-tuning with LoRA:"),ji.forEach(t),jt=h(e),q=i(e,"UL",{});var so=n(q);at=i(so,"LI",{});var qi=n(at);ie=i(qi,"A",{href:!0,rel:!0});var Ci=n(ie);na=l(Ci,"DreamBooth"),Ci.forEach(t),qi.forEach(t),ra=h(so),it=i(so,"LI",{});var Mi=n(it);ne=i(Mi,"A",{href:!0,rel:!0});var Oi=n(ne);la=l(Oi,"Text2Image"),Oi.forEach(t),Mi.forEach(t),so.forEach(t),qt=h(e),xe=i(e,"P",{});var Ui=n(xe);sa=l(Ui,"If you want to perform DreamBooth training with LoRA, for instance, you would run:"),Ui.forEach(t),Ct=h(e),x(re.$$.fragment,e),Mt=h(e),C=i(e,"P",{});var fo=n(C);fa=l(fo,`A similar process can be followed to fully fine-tune Stable Diffusion on a custom dataset using the
`),nt=i(fo,"CODE",{});var Gi=n(nt);pa=l(Gi,"examples/text_to_image/train_text_to_image_lora.py"),Gi.forEach(t),ua=l(fo," script."),fo.forEach(t),Ot=h(e),Pe=i(e,"P",{});var Bi=n(Pe);ha=l(Bi,"Refer to the respective examples linked above to learn more."),Bi.forEach(t),Ut=h(e),x(M.$$.fragment,e),Gt=h(e),Se=i(e,"P",{});var zi=n(Se);da=l(zi,`But there is no free lunch. For the given dataset and expected generation quality, you\u2019d still need to experiment with
different hyperparameters. Here are some important ones:`),zi.forEach(t),Bt=h(e),O=i(e,"UL",{});var po=n(O);Te=i(po,"LI",{});var ri=n(Te);ca=l(ri,"Training time"),le=i(ri,"UL",{});var uo=n(le);rt=i(uo,"LI",{});var Wi=n(rt);ma=l(Wi,"Learning rate"),Wi.forEach(t),_a=h(uo),lt=i(uo,"LI",{});var Hi=n(lt);ga=l(Hi,"Number of training steps"),Hi.forEach(t),uo.forEach(t),ri.forEach(t),wa=h(po),Ie=i(po,"LI",{});var li=n(Ie);va=l(li,"Inference time "),se=i(li,"UL",{});var ho=n(se);st=i(ho,"LI",{});var Ki=n(st);ya=l(Ki,"Number of steps"),Ki.forEach(t),ba=h(ho),ft=i(ho,"LI",{});var Yi=n(ft);Ea=l(Yi,"Scheduler type"),Yi.forEach(t),ho.forEach(t),li.forEach(t),po.forEach(t),zt=h(e),U=i(e,"P",{});var co=n(U);Aa=l(co,"Additionally, you can follow "),fe=i(co,"A",{href:!0,rel:!0});var Vi=n(fe);$a=l(Vi,"this blog"),Vi.forEach(t),La=l(co,` that documents some of our experimental
findings for performing DreamBooth training of Stable Diffusion.`),co.forEach(t),Wt=h(e),G=i(e,"P",{});var mo=n(G);ka=l(mo,`When fine-tuning, the LoRA update matrices are only added to the attention layers. To enable this, we added new weight
loading functionalities. Their details are available `),pe=i(mo,"A",{href:!0,rel:!0});var Xi=n(pe);Ra=l(Xi,"here"),Xi.forEach(t),Da=l(mo,"."),mo.forEach(t),Ht=h(e),L=i(e,"H2",{class:!0});var _o=n(L);B=i(_o,"A",{id:!0,class:!0,href:!0});var Zi=n(B);pt=i(Zi,"SPAN",{});var Ji=n(pt);x(ue.$$.fragment,Ji),Ji.forEach(t),Zi.forEach(t),xa=h(_o),ut=i(_o,"SPAN",{});var Fi=n(ut);Pa=l(Fi,"Inference"),Fi.forEach(t),_o.forEach(t),Kt=h(e),b=i(e,"P",{});var Ge=n(b);Sa=l(Ge,"Assuming you used the "),ht=i(Ge,"CODE",{});var Qi=n(ht);Ta=l(Qi,"examples/text_to_image/train_text_to_image_lora.py"),Qi.forEach(t),Ia=l(Ge," to fine-tune Stable Diffusion on the "),he=i(Ge,"A",{href:!0,rel:!0});var en=n(he);Na=l(en,`Pokemon
dataset`),en.forEach(t),ja=l(Ge,", you can perform inference like so:"),Ge.forEach(t),Yt=h(e),x(de.$$.fragment,e),Vt=h(e),Ne=i(e,"P",{});var tn=n(Ne);qa=l(tn,"Here are some example images you can expect:"),tn.forEach(t),Xt=h(e),je=i(e,"IMG",{src:!0}),Zt=h(e),k=i(e,"P",{});var At=n(k);ce=i(At,"A",{href:!0,rel:!0});var on=n(ce);dt=i(on,"CODE",{});var an=n(dt);Ca=l(an,"sayakpaul/sd-model-finetuned-lora-t4"),an.forEach(t),on.forEach(t),Ma=l(At," contains "),me=i(At,"A",{href:!0,rel:!0});var nn=n(me);Oa=l(nn,"LoRA fine-tuned update matrices"),nn.forEach(t),Ua=l(At,`
which is only 3 MBs in size. During inference, the pre-trained Stable Diffusion checkpoints are loaded alongside these update
matrices and then they are combined to run inference.`),At.forEach(t),Jt=h(e),E=i(e,"P",{});var Be=n(E);Ga=l(Be,"You can use the "),_e=i(Be,"A",{href:!0,rel:!0});var rn=n(_e);ct=i(rn,"CODE",{});var ln=n(ct);Ba=l(ln,"huggingface_hub"),ln.forEach(t),rn.forEach(t),za=l(Be,` library to retrieve the base model
from `),ge=i(Be,"A",{href:!0,rel:!0});var sn=n(ge);mt=i(sn,"CODE",{});var fn=n(mt);Wa=l(fn,"sayakpaul/sd-model-finetuned-lora-t4"),fn.forEach(t),sn.forEach(t),Ha=l(Be," like so:"),Be.forEach(t),Ft=h(e),x(we.$$.fragment,e),Qt=h(e),z=i(e,"P",{});var go=n(z);Ka=l(go,"And then you can use "),_t=i(go,"CODE",{});var pn=n(_t);Ya=l(pn,"pipe = StableDiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.float16)"),pn.forEach(t),Va=l(go,"."),go.forEach(t),eo=h(e),W=i(e,"P",{});var wo=n(W);Xa=l(wo,"This is especially useful when you don\u2019t want to hardcode the base model identifier during initializing the "),gt=i(wo,"CODE",{});var un=n(gt);Za=l(un,"StableDiffusionPipeline"),un.forEach(t),Ja=l(wo,"."),wo.forEach(t),to=h(e),H=i(e,"P",{});var vo=n(H);Fa=l(vo,`Inference for DreamBooth training remains the same. Check
`),ve=i(vo,"A",{href:!0,rel:!0});var hn=n(ve);Qa=l(hn,"this section"),hn.forEach(t),ei=l(vo," for more details."),vo.forEach(t),oo=h(e),R=i(e,"H2",{class:!0});var yo=n(R);K=i(yo,"A",{id:!0,class:!0,href:!0});var dn=n(K);wt=i(dn,"SPAN",{});var cn=n(wt);x(ye.$$.fragment,cn),cn.forEach(t),dn.forEach(t),ti=h(yo),vt=i(yo,"SPAN",{});var mn=n(vt);oi=l(mn,"Known limitations"),mn.forEach(t),yo.forEach(t),ao=h(e),qe=i(e,"UL",{});var _n=n(qe);be=i(_n,"LI",{});var bo=n(be);ai=l(bo,"Currently, we only support LoRA for the attention layers of "),Ee=i(bo,"A",{href:!0,rel:!0});var gn=n(Ee);yt=i(gn,"CODE",{});var wn=n(yt);ii=l(wn,"UNet2DConditionModel"),wn.forEach(t),gn.forEach(t),ni=l(bo,"."),bo.forEach(t),_n.forEach(t),this.h()},h(){p(d,"name","hf:doc:metadata"),p(d,"content",JSON.stringify(xn)),p(m,"id","lora-support-in-diffusers"),p(m,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),p(m,"href","#lora-support-in-diffusers"),p(c,"class","relative group"),p(V,"href","https://arxiv.org/abs/2106.09685"),p(V,"rel","nofollow"),p(Z,"href","https://www.pnas.org/doi/10.1073/pnas.1611835114"),p(Z,"rel","nofollow"),p(F,"href","https://github.com/cloneofsimo"),p(F,"rel","nofollow"),p(Q,"href","https://github.com/cloneofsimo/lora"),p(Q,"rel","nofollow"),p(j,"id","getting-started-with-lora-for-finetuning"),p(j,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),p(j,"href","#getting-started-with-lora-for-finetuning"),p($,"class","relative group"),p(te,"href","https://huggingface.co/docs/diffusers/main/en/training/text_inversion"),p(te,"rel","nofollow"),p(oe,"href","https://huggingface.co/docs/diffusers/main/en/training/dreambooth"),p(oe,"rel","nofollow"),p(ae,"href","https://huggingface.co/docs/diffusers/main/en/training/text2image"),p(ae,"rel","nofollow"),p(ie,"href","https://github.com/huggingface/diffusers/tree/main/examples/dreambooth#training-with-low-rank-adaptation-of-large-language-models-lora"),p(ie,"rel","nofollow"),p(ne,"href","https://github.com/huggingface/diffusers/tree/main/examples/text_to_image#training-with-lora"),p(ne,"rel","nofollow"),p(fe,"href","https://huggingface.co/blog/dreambooth"),p(fe,"rel","nofollow"),p(pe,"href","https://huggingface.co/docs/diffusers/main/en/api/loaders"),p(pe,"rel","nofollow"),p(B,"id","inference"),p(B,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),p(B,"href","#inference"),p(L,"class","relative group"),p(he,"href","https://huggingface.co/datasets/lambdalabs/pokemon-blip-captions"),p(he,"rel","nofollow"),$n(je.src,fi="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pokemon-collage.png")||p(je,"src",fi),p(ce,"href","https://huggingface.co/sayakpaul/sd-model-finetuned-lora-t4"),p(ce,"rel","nofollow"),p(me,"href","https://huggingface.co/sayakpaul/sd-model-finetuned-lora-t4/blob/main/pytorch_lora_weights.bin"),p(me,"rel","nofollow"),p(_e,"href","https://github.com/huggingface/huggingface_hub"),p(_e,"rel","nofollow"),p(ge,"href","https://huggingface.co/sayakpaul/sd-model-finetuned-lora-t4"),p(ge,"rel","nofollow"),p(ve,"href","https://github.com/huggingface/diffusers/tree/main/examples/dreambooth#inference-1"),p(ve,"rel","nofollow"),p(K,"id","known-limitations"),p(K,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),p(K,"href","#known-limitations"),p(R,"class","relative group"),p(Ee,"href","https://huggingface.co/docs/diffusers/main/en/api/models#diffusers.UNet2DConditionModel"),p(Ee,"rel","nofollow")},m(e,s){o(document.head,d),f(e,_,s),f(e,c,s),o(c,m),o(m,ze),P(Y,ze,null),o(c,Ao),o(c,We),o(We,$o),f(e,$t,s),f(e,Le,s),o(Le,Lo),f(e,Lt,s),f(e,g,s),o(g,ko),o(g,V),o(V,Ro),o(g,Do),o(g,He),o(He,xo),o(g,Po),f(e,kt,s),f(e,w,s),o(w,So),o(w,Ke),o(Ke,To),o(w,Io),o(w,Ye),o(Ye,No),o(w,jo),f(e,Rt,s),f(e,v,s),o(v,X),o(X,qo),o(X,Z),o(Z,Co),o(X,Mo),o(v,Oo),o(v,Ve),o(Ve,Uo),o(v,Go),o(v,J),o(J,Bo),o(J,Xe),o(Xe,zo),o(J,Wo),f(e,Dt,s),f(e,ke,s),o(ke,Ze),o(Ze,Je),o(Je,Ho),f(e,xt,s),f(e,A,s),o(A,F),o(F,Ko),o(A,Yo),o(A,Q),o(Q,Vo),o(A,Xo),f(e,Pt,s),P(N,e,s),f(e,St,s),f(e,$,s),o($,j),o(j,Fe),P(ee,Fe,null),o($,Zo),o($,Qe),o(Qe,Jo),f(e,Tt,s),f(e,Re,s),o(Re,Fo),f(e,It,s),f(e,y,s),o(y,et),o(et,te),o(te,Qo),o(y,ea),o(y,tt),o(tt,oe),o(oe,ta),o(y,oa),o(y,ot),o(ot,ae),o(ae,aa),f(e,Nt,s),f(e,De,s),o(De,ia),f(e,jt,s),f(e,q,s),o(q,at),o(at,ie),o(ie,na),o(q,ra),o(q,it),o(it,ne),o(ne,la),f(e,qt,s),f(e,xe,s),o(xe,sa),f(e,Ct,s),P(re,e,s),f(e,Mt,s),f(e,C,s),o(C,fa),o(C,nt),o(nt,pa),o(C,ua),f(e,Ot,s),f(e,Pe,s),o(Pe,ha),f(e,Ut,s),P(M,e,s),f(e,Gt,s),f(e,Se,s),o(Se,da),f(e,Bt,s),f(e,O,s),o(O,Te),o(Te,ca),o(Te,le),o(le,rt),o(rt,ma),o(le,_a),o(le,lt),o(lt,ga),o(O,wa),o(O,Ie),o(Ie,va),o(Ie,se),o(se,st),o(st,ya),o(se,ba),o(se,ft),o(ft,Ea),f(e,zt,s),f(e,U,s),o(U,Aa),o(U,fe),o(fe,$a),o(U,La),f(e,Wt,s),f(e,G,s),o(G,ka),o(G,pe),o(pe,Ra),o(G,Da),f(e,Ht,s),f(e,L,s),o(L,B),o(B,pt),P(ue,pt,null),o(L,xa),o(L,ut),o(ut,Pa),f(e,Kt,s),f(e,b,s),o(b,Sa),o(b,ht),o(ht,Ta),o(b,Ia),o(b,he),o(he,Na),o(b,ja),f(e,Yt,s),P(de,e,s),f(e,Vt,s),f(e,Ne,s),o(Ne,qa),f(e,Xt,s),f(e,je,s),f(e,Zt,s),f(e,k,s),o(k,ce),o(ce,dt),o(dt,Ca),o(k,Ma),o(k,me),o(me,Oa),o(k,Ua),f(e,Jt,s),f(e,E,s),o(E,Ga),o(E,_e),o(_e,ct),o(ct,Ba),o(E,za),o(E,ge),o(ge,mt),o(mt,Wa),o(E,Ha),f(e,Ft,s),P(we,e,s),f(e,Qt,s),f(e,z,s),o(z,Ka),o(z,_t),o(_t,Ya),o(z,Va),f(e,eo,s),f(e,W,s),o(W,Xa),o(W,gt),o(gt,Za),o(W,Ja),f(e,to,s),f(e,H,s),o(H,Fa),o(H,ve),o(ve,Qa),o(H,ei),f(e,oo,s),f(e,R,s),o(R,K),o(K,wt),P(ye,wt,null),o(R,ti),o(R,vt),o(vt,oi),f(e,ao,s),f(e,qe,s),o(qe,be),o(be,ai),o(be,Ee),o(Ee,yt),o(yt,ii),o(be,ni),io=!0},p(e,[s]){const Ae={};s&2&&(Ae.$$scope={dirty:s,ctx:e}),N.$set(Ae);const bt={};s&2&&(bt.$$scope={dirty:s,ctx:e}),M.$set(bt)},i(e){io||(S(Y.$$.fragment,e),S(N.$$.fragment,e),S(ee.$$.fragment,e),S(re.$$.fragment,e),S(M.$$.fragment,e),S(ue.$$.fragment,e),S(de.$$.fragment,e),S(we.$$.fragment,e),S(ye.$$.fragment,e),io=!0)},o(e){T(Y.$$.fragment,e),T(N.$$.fragment,e),T(ee.$$.fragment,e),T(re.$$.fragment,e),T(M.$$.fragment,e),T(ue.$$.fragment,e),T(de.$$.fragment,e),T(we.$$.fragment,e),T(ye.$$.fragment,e),io=!1},d(e){t(d),e&&t(_),e&&t(c),I(Y),e&&t($t),e&&t(Le),e&&t(Lt),e&&t(g),e&&t(kt),e&&t(w),e&&t(Rt),e&&t(v),e&&t(Dt),e&&t(ke),e&&t(xt),e&&t(A),e&&t(Pt),I(N,e),e&&t(St),e&&t($),I(ee),e&&t(Tt),e&&t(Re),e&&t(It),e&&t(y),e&&t(Nt),e&&t(De),e&&t(jt),e&&t(q),e&&t(qt),e&&t(xe),e&&t(Ct),I(re,e),e&&t(Mt),e&&t(C),e&&t(Ot),e&&t(Pe),e&&t(Ut),I(M,e),e&&t(Gt),e&&t(Se),e&&t(Bt),e&&t(O),e&&t(zt),e&&t(U),e&&t(Wt),e&&t(G),e&&t(Ht),e&&t(L),I(ue),e&&t(Kt),e&&t(b),e&&t(Yt),I(de,e),e&&t(Vt),e&&t(Ne),e&&t(Xt),e&&t(je),e&&t(Zt),e&&t(k),e&&t(Jt),e&&t(E),e&&t(Ft),I(we,e),e&&t(Qt),e&&t(z),e&&t(eo),e&&t(W),e&&t(to),e&&t(H),e&&t(oo),e&&t(R),I(ye),e&&t(ao),e&&t(qe)}}}const xn={local:"lora-support-in-diffusers",sections:[{local:"getting-started-with-lora-for-finetuning",title:"Getting started with LoRA for fine-tuning"},{local:"inference",title:"Inference "},{local:"known-limitations",title:"Known limitations "}],title:"LoRA Support in Diffusers "};function Pn($e){return Ln(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class jn extends yn{constructor(d){super();bn(this,d,Pn,Dn,En,{})}}export{jn as default,xn as metadata};
