import{S as zo,i as Wo,s as Yo,e as r,k as d,w,t as s,M as Vo,c as n,d as t,m as _,a as i,x as v,h as o,b as c,G as a,g as h,y,q as b,o as k,B as x,v as Jo,L as La}from"../../chunks/vendor-hf-doc-builder.js";import{T as Go}from"../../chunks/Tip-hf-doc-builder.js";import{I as Ie}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as ge}from"../../chunks/CodeBlock-hf-doc-builder.js";import{F as Bo,M as Fa}from"../../chunks/Markdown-hf-doc-builder.js";function Ko(N){let p,g;return{c(){p=r("p"),g=s("The text-to-image fine-tuning script is experimental. It\u2019s easy to overfit and run into issues like catastrophic forgetting. We recommend you explore different hyperparameters to get the best results on your dataset.")},l(l){p=n(l,"P",{});var $=i(p);g=o($,"The text-to-image fine-tuning script is experimental. It\u2019s easy to overfit and run into issues like catastrophic forgetting. We recommend you explore different hyperparameters to get the best results on your dataset."),$.forEach(t)},m(l,$){h(l,p,$),a(p,g)},d(l){l&&t(p)}}}function Xo(N){let p,g,l,$,P,S,se,le,pe,D,z,M,G,he,W,fe,ue,$e,U,ce,R,me,T,J,oe,O,K,L,I,X,q,de,we,F,Q,re,ee,ne,Y,V,C,H,ie;return U=new ge({props:{code:`\`\`\`bash
export MODEL_NAME="CompVis/stable-diffusion-v1-4"
export dataset_name="lambdalabs/pokemon-blip-captions"

accelerate launch --mixed_precision="fp16"  train_text_to_image.py \\
  --pretrained_model_name_or_path=$MODEL_NAME \\
  --dataset_name=$dataset_name \\
  --use_ema \\
  --resolution=512 --center_crop --random_flip \\
  --train_batch_size=1 \\
  --gradient_accumulation_steps=4 \\
  --gradient_checkpointing \\
  --max_train_steps=15000 \\
  --learning_rate=1e-05 \\
  --max_grad_norm=1 \\
  --lr_scheduler="constant" --lr_warmup_steps=0 \\
  --output_dir="sd-pokemon-model" `,highlighted:`\`\`\`bash
<span class="hljs-built_in">export</span> MODEL_NAME=<span class="hljs-string">&quot;CompVis/stable-diffusion-v1-4&quot;</span>
<span class="hljs-built_in">export</span> dataset_name=<span class="hljs-string">&quot;lambdalabs/pokemon-blip-captions&quot;</span>

accelerate launch --mixed_precision=<span class="hljs-string">&quot;fp16&quot;</span>  train_text_to_image.py \\
  --pretrained_model_name_or_path=<span class="hljs-variable">$MODEL_NAME</span> \\
  --dataset_name=<span class="hljs-variable">$dataset_name</span> \\
  --use_ema \\
  --resolution=512 --center_crop --random_flip \\
  --train_batch_size=1 \\
  --gradient_accumulation_steps=4 \\
  --gradient_checkpointing \\
  --max_train_steps=15000 \\
  --learning_rate=1e-05 \\
  --max_grad_norm=1 \\
  --lr_scheduler=<span class="hljs-string">&quot;constant&quot;</span> --lr_warmup_steps=0 \\
  --output_dir=<span class="hljs-string">&quot;sd-pokemon-model&quot;</span> `}}),R=new ge({props:{code:`
To finetune on your own dataset, prepare the dataset according to the format required by \u{1F917} [Datasets](https://huggingface.co/docs/datasets/index). You can [upload your dataset to the Hub](https://huggingface.co/docs/datasets/image_dataset#upload-dataset-to-the-hub), or you can [prepare a local folder with your files](https://huggingface.co/docs/datasets/image_dataset#imagefolder).

Modify the script if you want to use custom loading logic. We left pointers in the code in the appropriate places to help you. \u{1F917} The example script below shows how to finetune on a local dataset in \`TRAIN_DIR\` and where to save the model to in \`OUTPUT_DIR\`:

\`\`\`bash
export MODEL_NAME="CompVis/stable-diffusion-v1-4"
export TRAIN_DIR="path_to_your_dataset"
export OUTPUT_DIR="path_to_save_model"

accelerate launch train_text_to_image.py \\
  --pretrained_model_name_or_path=$MODEL_NAME \\
  --train_data_dir=$TRAIN_DIR \\
  --use_ema \\
  --resolution=512 --center_crop --random_flip \\
  --train_batch_size=1 \\
  --gradient_accumulation_steps=4 \\
  --gradient_checkpointing \\
  --mixed_precision="fp16" \\
  --max_train_steps=15000 \\
  --learning_rate=1e-05 \\
  --max_grad_norm=1 \\
  --lr_scheduler="constant" --lr_warmup_steps=0 \\
  --output_dir=\${OUTPUT_DIR}`,highlighted:`
<span class="hljs-keyword">To</span> finetune <span class="hljs-keyword">on</span> your own dataset, <span class="hljs-keyword">prepare</span> the dataset according <span class="hljs-keyword">to</span> the <span class="hljs-keyword">format</span> required <span class="hljs-keyword">by</span> \u{1F917} [Datasets](https://huggingface.co/docs/datasets/<span class="hljs-keyword">index</span>). You can [upload your dataset <span class="hljs-keyword">to</span> the Hub](https://huggingface.co/docs/datasets/image_dataset#upload-dataset-<span class="hljs-keyword">to</span>-the-hub), <span class="hljs-keyword">or</span> you can [<span class="hljs-keyword">prepare</span> a <span class="hljs-keyword">local</span> folder <span class="hljs-keyword">with</span> your files](https://huggingface.co/docs/datasets/image_dataset#imagefolder).

Modify the script <span class="hljs-keyword">if</span> you want <span class="hljs-keyword">to</span> use custom loading logic. We left pointers <span class="hljs-keyword">in</span> the code <span class="hljs-keyword">in</span> the appropriate places <span class="hljs-keyword">to</span> help you. \u{1F917} The example script below shows how <span class="hljs-keyword">to</span> finetune <span class="hljs-keyword">on</span> a <span class="hljs-keyword">local</span> dataset <span class="hljs-keyword">in</span> \`TRAIN_DIR\` <span class="hljs-keyword">and</span> <span class="hljs-keyword">where</span> <span class="hljs-keyword">to</span> save the model <span class="hljs-keyword">to</span> <span class="hljs-keyword">in</span> \`OUTPUT_DIR\`:

\`\`\`bash
export MODEL_NAME=&quot;CompVis/stable-diffusion-v1-4&quot;
export TRAIN_DIR=&quot;path_to_your_dataset&quot;
export OUTPUT_DIR=&quot;path_to_save_model&quot;

accelerate launch train_text_to_image.py \\
  <span class="hljs-comment">--pretrained_model_name_or_path=$MODEL_NAME \\</span>
  <span class="hljs-comment">--train_data_dir=$TRAIN_DIR \\</span>
  <span class="hljs-comment">--use_ema \\</span>
  <span class="hljs-comment">--resolution=512 --center_crop --random_flip \\</span>
  <span class="hljs-comment">--train_batch_size=1 \\</span>
  <span class="hljs-comment">--gradient_accumulation_steps=4 \\</span>
  <span class="hljs-comment">--gradient_checkpointing \\</span>
  <span class="hljs-comment">--mixed_precision=&quot;fp16&quot; \\</span>
  <span class="hljs-comment">--max_train_steps=15000 \\</span>
  <span class="hljs-comment">--learning_rate=1e-05 \\</span>
  <span class="hljs-comment">--max_grad_norm=1 \\</span>
  <span class="hljs-comment">--lr_scheduler=&quot;constant&quot; --lr_warmup_steps=0 \\</span>
  <span class="hljs-comment">--output_dir=\${OUTPUT_DIR}</span>`}}),O=new Ie({}),H=new ge({props:{code:`export MODEL_NAME="CompVis/stable-diffusion-v1-4"
export dataset_name="lambdalabs/pokemon-blip-captions"

accelerate launch --mixed_precision="fp16" --multi_gpu  train_text_to_image.py \\
  --pretrained_model_name_or_path=$MODEL_NAME \\
  --dataset_name=$dataset_name \\
  --use_ema \\
  --resolution=512 --center_crop --random_flip \\
  --train_batch_size=1 \\
  --gradient_accumulation_steps=4 \\
  --gradient_checkpointing \\
  --max_train_steps=15000 \\ 
  --learning_rate=1e-05 \\
  --max_grad_norm=1 \\
  --lr_scheduler="constant" --lr_warmup_steps=0 \\
  --output_dir="sd-pokemon-model" `,highlighted:`<span class="hljs-built_in">export</span> MODEL_NAME=<span class="hljs-string">&quot;CompVis/stable-diffusion-v1-4&quot;</span>
<span class="hljs-built_in">export</span> dataset_name=<span class="hljs-string">&quot;lambdalabs/pokemon-blip-captions&quot;</span>

accelerate launch --mixed_precision=<span class="hljs-string">&quot;fp16&quot;</span> --multi_gpu  train_text_to_image.py \\
  --pretrained_model_name_or_path=<span class="hljs-variable">$MODEL_NAME</span> \\
  --dataset_name=<span class="hljs-variable">$dataset_name</span> \\
  --use_ema \\
  --resolution=512 --center_crop --random_flip \\
  --train_batch_size=1 \\
  --gradient_accumulation_steps=4 \\
  --gradient_checkpointing \\
  --max_train_steps=15000 \\ 
  --learning_rate=1e-05 \\
  --max_grad_norm=1 \\
  --lr_scheduler=<span class="hljs-string">&quot;constant&quot;</span> --lr_warmup_steps=0 \\
  --output_dir=<span class="hljs-string">&quot;sd-pokemon-model&quot;</span> `}}),{c(){p=r("p"),g=s("Launch the "),l=r("a"),$=s("PyTorch training script"),P=s(" for a fine-tuning run on the "),S=r("a"),se=s("Pok\xE9mon BLIP captions"),le=s(" dataset like this."),pe=d(),D=r("p"),z=s("Specify the "),M=r("code"),G=s("MODEL_NAME"),he=s(" environment variable (either a Hub model repository id or a path to the directory containing the model weights) and pass it to the "),W=r("code"),fe=s("~diffusers.DiffusionPipeline.from_pretrained.pretrained_model_name_or_path"),ue=s(" argument."),$e=d(),w(U.$$.fragment),ce=d(),w(R.$$.fragment),me=d(),T=r("h4"),J=r("a"),oe=r("span"),w(O.$$.fragment),K=d(),L=r("span"),I=s("Training with multiple GPUs"),X=d(),q=r("p"),de=r("code"),we=s("accelerate"),F=s(" allows for seamless multi-GPU training. Follow the instructions "),Q=r("a"),re=s("here"),ee=s(`
for running distributed training with `),ne=r("code"),Y=s("accelerate"),V=s(". Here is an example command:"),C=d(),w(H.$$.fragment),this.h()},l(m){p=n(m,"P",{});var j=i(p);g=o(j,"Launch the "),l=n(j,"A",{href:!0,rel:!0});var ve=i(l);$=o(ve,"PyTorch training script"),ve.forEach(t),P=o(j," for a fine-tuning run on the "),S=n(j,"A",{href:!0,rel:!0});var A=i(S);se=o(A,"Pok\xE9mon BLIP captions"),A.forEach(t),le=o(j," dataset like this."),j.forEach(t),pe=_(m),D=n(m,"P",{});var te=i(D);z=o(te,"Specify the "),M=n(te,"CODE",{});var u=i(M);G=o(u,"MODEL_NAME"),u.forEach(t),he=o(te," environment variable (either a Hub model repository id or a path to the directory containing the model weights) and pass it to the "),W=n(te,"CODE",{});var E=i(W);fe=o(E,"~diffusers.DiffusionPipeline.from_pretrained.pretrained_model_name_or_path"),E.forEach(t),ue=o(te," argument."),te.forEach(t),$e=_(m),v(U.$$.fragment,m),ce=_(m),v(R.$$.fragment,m),me=_(m),T=n(m,"H4",{class:!0});var be=i(T);J=n(be,"A",{id:!0,class:!0,href:!0});var ke=i(J);oe=n(ke,"SPAN",{});var _e=i(oe);v(O.$$.fragment,_e),_e.forEach(t),ke.forEach(t),K=_(be),L=n(be,"SPAN",{});var Te=i(L);I=o(Te,"Training with multiple GPUs"),Te.forEach(t),be.forEach(t),X=_(m),q=n(m,"P",{});var Z=i(q);de=n(Z,"CODE",{});var xe=i(de);we=o(xe,"accelerate"),xe.forEach(t),F=o(Z," allows for seamless multi-GPU training. Follow the instructions "),Q=n(Z,"A",{href:!0,rel:!0});var De=i(Q);re=o(De,"here"),De.forEach(t),ee=o(Z,`
for running distributed training with `),ne=n(Z,"CODE",{});var B=i(ne);Y=o(B,"accelerate"),B.forEach(t),V=o(Z,". Here is an example command:"),Z.forEach(t),C=_(m),v(H.$$.fragment,m),this.h()},h(){c(l,"href","https://github.com/huggingface/diffusers/blob/main/examples/text_to_image/train_text_to_image.py"),c(l,"rel","nofollow"),c(S,"href","https://huggingface.co/datasets/lambdalabs/pokemon-blip-captions"),c(S,"rel","nofollow"),c(J,"id","training-with-multiple-gpus"),c(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"),c(J,"href","#training-with-multiple-gpus"),c(T,"class","relative group"),c(Q,"href","https://huggingface.co/docs/accelerate/basic_tutorials/launch"),c(Q,"rel","nofollow")},m(m,j){h(m,p,j),a(p,g),a(p,l),a(l,$),a(p,P),a(p,S),a(S,se),a(p,le),h(m,pe,j),h(m,D,j),a(D,z),a(D,M),a(M,G),a(D,he),a(D,W),a(W,fe),a(D,ue),h(m,$e,j),y(U,m,j),h(m,ce,j),y(R,m,j),h(m,me,j),h(m,T,j),a(T,J),a(J,oe),y(O,oe,null),a(T,K),a(T,L),a(L,I),h(m,X,j),h(m,q,j),a(q,de),a(de,we),a(q,F),a(q,Q),a(Q,re),a(q,ee),a(q,ne),a(ne,Y),a(q,V),h(m,C,j),y(H,m,j),ie=!0},p:La,i(m){ie||(b(U.$$.fragment,m),b(R.$$.fragment,m),b(O.$$.fragment,m),b(H.$$.fragment,m),ie=!0)},o(m){k(U.$$.fragment,m),k(R.$$.fragment,m),k(O.$$.fragment,m),k(H.$$.fragment,m),ie=!1},d(m){m&&t(p),m&&t(pe),m&&t(D),m&&t($e),x(U,m),m&&t(ce),x(R,m),m&&t(me),m&&t(T),x(O),m&&t(X),m&&t(q),m&&t(C),x(H,m)}}}function Qo(N){let p,g;return p=new Fa({props:{$$slots:{default:[Xo]},$$scope:{ctx:N}}}),{c(){w(p.$$.fragment)},l(l){v(p.$$.fragment,l)},m(l,$){y(p,l,$),g=!0},p(l,$){const P={};$&2&&(P.$$scope={dirty:$,ctx:l}),p.$set(P)},i(l){g||(b(p.$$.fragment,l),g=!0)},o(l){k(p.$$.fragment,l),g=!1},d(l){x(p,l)}}}function Zo(N){let p,g,l,$,P,S,se,le,pe,D,z,M,G,he,W,fe,ue,$e,U,ce,R,me,T,J,oe,O,K,L,I,X,q,de,we,F,Q,re,ee,ne,Y,V,C,H,ie,m,j,ve,A,te;return D=new ge({props:{code:"pip install -U -r requirements_flax.txt",highlighted:"pip install -U -r requirements_flax.txt"}}),K=new ge({props:{code:`export MODEL_NAME="runwayml/stable-diffusion-v1-5"
export dataset_name="lambdalabs/pokemon-blip-captions"

python train_text_to_image_flax.py \\
  --pretrained_model_name_or_path=$MODEL_NAME \\
  --dataset_name=$dataset_name \\
  --resolution=512 --center_crop --random_flip \\
  --train_batch_size=1 \\
  --max_train_steps=15000 \\
  --learning_rate=1e-05 \\
  --max_grad_norm=1 \\
  --output_dir="sd-pokemon-model" `,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> dataset_name=<span class="hljs-string">&quot;lambdalabs/pokemon-blip-captions&quot;</span>

python train_text_to_image_flax.py \\
  --pretrained_model_name_or_path=<span class="hljs-variable">$MODEL_NAME</span> \\
  --dataset_name=<span class="hljs-variable">$dataset_name</span> \\
  --resolution=512 --center_crop --random_flip \\
  --train_batch_size=1 \\
  --max_train_steps=15000 \\
  --learning_rate=1e-05 \\
  --max_grad_norm=1 \\
  --output_dir=<span class="hljs-string">&quot;sd-pokemon-model&quot;</span> `}}),A=new ge({props:{code:`export MODEL_NAME="duongna/stable-diffusion-v1-4-flax"
export TRAIN_DIR="path_to_your_dataset"

python train_text_to_image_flax.py \\
  --pretrained_model_name_or_path=$MODEL_NAME \\
  --train_data_dir=$TRAIN_DIR \\
  --resolution=512 --center_crop --random_flip \\
  --train_batch_size=1 \\
  --mixed_precision="fp16" \\
  --max_train_steps=15000 \\
  --learning_rate=1e-05 \\
  --max_grad_norm=1 \\
  --output_dir="sd-pokemon-model"`,highlighted:`<span class="hljs-built_in">export</span> MODEL_NAME=<span class="hljs-string">&quot;duongna/stable-diffusion-v1-4-flax&quot;</span>
<span class="hljs-built_in">export</span> TRAIN_DIR=<span class="hljs-string">&quot;path_to_your_dataset&quot;</span>

python train_text_to_image_flax.py \\
  --pretrained_model_name_or_path=<span class="hljs-variable">$MODEL_NAME</span> \\
  --train_data_dir=<span class="hljs-variable">$TRAIN_DIR</span> \\
  --resolution=512 --center_crop --random_flip \\
  --train_batch_size=1 \\
  --mixed_precision=<span class="hljs-string">&quot;fp16&quot;</span> \\
  --max_train_steps=15000 \\
  --learning_rate=1e-05 \\
  --max_grad_norm=1 \\
  --output_dir=<span class="hljs-string">&quot;sd-pokemon-model&quot;</span>`}}),{c(){p=r("p"),g=s("With Flax, it\u2019s possible to train a Stable Diffusion model faster on TPUs and GPUs thanks to "),l=r("a"),$=s("@duongna211"),P=s(". This is very efficient on TPU hardware but works great on GPUs too. The Flax training script doesn\u2019t support features like gradient checkpointing or gradient accumulation yet, so you\u2019ll need a GPU with at least 30GB of memory or a TPU v3."),S=d(),se=r("p"),le=s("Before running the script, make sure you have the requirements installed:"),pe=d(),w(D.$$.fragment),z=d(),M=r("p"),G=s("Specify the "),he=r("code"),W=s("MODEL_NAME"),fe=s(" environment variable (either a Hub model repository id or a path to the directory containing the model weights) and pass it to the "),ue=r("code"),$e=s("~diffusers.DiffusionPipeline.from_pretrained.pretrained_model_name_or_path"),U=s(" argument."),ce=d(),R=r("p"),me=s("Now you can launch the "),T=r("a"),J=s("Flax training script"),oe=s(" like this:"),O=d(),w(K.$$.fragment),L=d(),I=r("p"),X=s("To finetune on your own dataset, prepare the dataset according to the format required by \u{1F917} "),q=r("a"),de=s("Datasets"),we=s(". You can "),F=r("a"),Q=s("upload your dataset to the Hub"),re=s(", or you can "),ee=r("a"),ne=s("prepare a local folder with your files"),Y=s("."),V=d(),C=r("p"),H=s("Modify the script if you want to use custom loading logic. We left pointers in the code in the appropriate places to help you. \u{1F917} The example script below shows how to finetune on a local dataset in "),ie=r("code"),m=s("TRAIN_DIR"),j=s(":"),ve=d(),w(A.$$.fragment),this.h()},l(u){p=n(u,"P",{});var E=i(p);g=o(E,"With Flax, it\u2019s possible to train a Stable Diffusion model faster on TPUs and GPUs thanks to "),l=n(E,"A",{href:!0,rel:!0});var be=i(l);$=o(be,"@duongna211"),be.forEach(t),P=o(E,". This is very efficient on TPU hardware but works great on GPUs too. The Flax training script doesn\u2019t support features like gradient checkpointing or gradient accumulation yet, so you\u2019ll need a GPU with at least 30GB of memory or a TPU v3."),E.forEach(t),S=_(u),se=n(u,"P",{});var ke=i(se);le=o(ke,"Before running the script, make sure you have the requirements installed:"),ke.forEach(t),pe=_(u),v(D.$$.fragment,u),z=_(u),M=n(u,"P",{});var _e=i(M);G=o(_e,"Specify the "),he=n(_e,"CODE",{});var Te=i(he);W=o(Te,"MODEL_NAME"),Te.forEach(t),fe=o(_e," environment variable (either a Hub model repository id or a path to the directory containing the model weights) and pass it to the "),ue=n(_e,"CODE",{});var Z=i(ue);$e=o(Z,"~diffusers.DiffusionPipeline.from_pretrained.pretrained_model_name_or_path"),Z.forEach(t),U=o(_e," argument."),_e.forEach(t),ce=_(u),R=n(u,"P",{});var xe=i(R);me=o(xe,"Now you can launch the "),T=n(xe,"A",{href:!0,rel:!0});var De=i(T);J=o(De,"Flax training script"),De.forEach(t),oe=o(xe," like this:"),xe.forEach(t),O=_(u),v(K.$$.fragment,u),L=_(u),I=n(u,"P",{});var B=i(I);X=o(B,"To finetune on your own dataset, prepare the dataset according to the format required by \u{1F917} "),q=n(B,"A",{href:!0,rel:!0});var ct=i(q);de=o(ct,"Datasets"),ct.forEach(t),we=o(B,". You can "),F=n(B,"A",{href:!0,rel:!0});var mt=i(F);Q=o(mt,"upload your dataset to the Hub"),mt.forEach(t),re=o(B,", or you can "),ee=n(B,"A",{href:!0,rel:!0});var Qe=i(ee);ne=o(Qe,"prepare a local folder with your files"),Qe.forEach(t),Y=o(B,"."),B.forEach(t),V=_(u),C=n(u,"P",{});var ae=i(C);H=o(ae,"Modify the script if you want to use custom loading logic. We left pointers in the code in the appropriate places to help you. \u{1F917} The example script below shows how to finetune on a local dataset in "),ie=n(ae,"CODE",{});var dt=i(ie);m=o(dt,"TRAIN_DIR"),dt.forEach(t),j=o(ae,":"),ae.forEach(t),ve=_(u),v(A.$$.fragment,u),this.h()},h(){c(l,"href","https://github.com/duongna21"),c(l,"rel","nofollow"),c(T,"href","https://github.com/huggingface/diffusers/blob/main/examples/text_to_image/train_text_to_image_flax.py"),c(T,"rel","nofollow"),c(q,"href","https://huggingface.co/docs/datasets/index"),c(q,"rel","nofollow"),c(F,"href","https://huggingface.co/docs/datasets/image_dataset#upload-dataset-to-the-hub"),c(F,"rel","nofollow"),c(ee,"href","https://huggingface.co/docs/datasets/image_dataset#imagefolder"),c(ee,"rel","nofollow")},m(u,E){h(u,p,E),a(p,g),a(p,l),a(l,$),a(p,P),h(u,S,E),h(u,se,E),a(se,le),h(u,pe,E),y(D,u,E),h(u,z,E),h(u,M,E),a(M,G),a(M,he),a(he,W),a(M,fe),a(M,ue),a(ue,$e),a(M,U),h(u,ce,E),h(u,R,E),a(R,me),a(R,T),a(T,J),a(R,oe),h(u,O,E),y(K,u,E),h(u,L,E),h(u,I,E),a(I,X),a(I,q),a(q,de),a(I,we),a(I,F),a(F,Q),a(I,re),a(I,ee),a(ee,ne),a(I,Y),h(u,V,E),h(u,C,E),a(C,H),a(C,ie),a(ie,m),a(C,j),h(u,ve,E),y(A,u,E),te=!0},p:La,i(u){te||(b(D.$$.fragment,u),b(K.$$.fragment,u),b(A.$$.fragment,u),te=!0)},o(u){k(D.$$.fragment,u),k(K.$$.fragment,u),k(A.$$.fragment,u),te=!1},d(u){u&&t(p),u&&t(S),u&&t(se),u&&t(pe),x(D,u),u&&t(z),u&&t(M),u&&t(ce),u&&t(R),u&&t(O),x(K,u),u&&t(L),u&&t(I),u&&t(V),u&&t(C),u&&t(ve),x(A,u)}}}function er(N){let p,g;return p=new Fa({props:{$$slots:{default:[Zo]},$$scope:{ctx:N}}}),{c(){w(p.$$.fragment)},l(l){v(p.$$.fragment,l)},m(l,$){y(p,l,$),g=!0},p(l,$){const P={};$&2&&(P.$$scope={dirty:$,ctx:l}),p.$set(P)},i(l){g||(b(p.$$.fragment,l),g=!0)},o(l){k(p.$$.fragment,l),g=!1},d(l){x(p,l)}}}function tr(N){let p,g;return{c(){p=r("p"),g=s("Training with Min-SNR weighting strategy is only supported in PyTorch.")},l(l){p=n(l,"P",{});var $=i(p);g=o($,"Training with Min-SNR weighting strategy is only supported in PyTorch."),$.forEach(t)},m(l,$){h(l,p,$),a(p,g)},d(l){l&&t(p)}}}function ar(N){let p,g;return p=new ge({props:{code:`from diffusers import StableDiffusionPipeline

model_path = "path_to_saved_model"
pipe = StableDiffusionPipeline.from_pretrained(model_path, torch_dtype=torch.float16)
pipe.to("cuda")

image = pipe(prompt="yoda").images[0]
image.save("yoda-pokemon.png")`,highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> StableDiffusionPipeline

model_path = <span class="hljs-string">&quot;path_to_saved_model&quot;</span>
pipe = StableDiffusionPipeline.from_pretrained(model_path, torch_dtype=torch.float16)
pipe.to(<span class="hljs-string">&quot;cuda&quot;</span>)

image = pipe(prompt=<span class="hljs-string">&quot;yoda&quot;</span>).images[<span class="hljs-number">0</span>]
image.save(<span class="hljs-string">&quot;yoda-pokemon.png&quot;</span>)`}}),{c(){w(p.$$.fragment)},l(l){v(p.$$.fragment,l)},m(l,$){y(p,l,$),g=!0},p:La,i(l){g||(b(p.$$.fragment,l),g=!0)},o(l){k(p.$$.fragment,l),g=!1},d(l){x(p,l)}}}function sr(N){let p,g;return p=new Fa({props:{$$slots:{default:[ar]},$$scope:{ctx:N}}}),{c(){w(p.$$.fragment)},l(l){v(p.$$.fragment,l)},m(l,$){y(p,l,$),g=!0},p(l,$){const P={};$&2&&(P.$$scope={dirty:$,ctx:l}),p.$set(P)},i(l){g||(b(p.$$.fragment,l),g=!0)},o(l){k(p.$$.fragment,l),g=!1},d(l){x(p,l)}}}function or(N){let p,g;return p=new ge({props:{code:`import jax
import numpy as np
from flax.jax_utils import replicate
from flax.training.common_utils import shard
from diffusers import FlaxStableDiffusionPipeline

model_path = "path_to_saved_model"
pipe, params = FlaxStableDiffusionPipeline.from_pretrained(model_path, dtype=jax.numpy.bfloat16)

prompt = "yoda pokemon"
prng_seed = jax.random.PRNGKey(0)
num_inference_steps = 50

num_samples = jax.device_count()
prompt = num_samples * [prompt]
prompt_ids = pipeline.prepare_inputs(prompt)

# shard inputs and rng
params = replicate(params)
prng_seed = jax.random.split(prng_seed, jax.device_count())
prompt_ids = shard(prompt_ids)

images = pipeline(prompt_ids, params, prng_seed, num_inference_steps, jit=True).images
images = pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:])))
image.save("yoda-pokemon.png")`,highlighted:`<span class="hljs-keyword">import</span> jax
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">from</span> flax.jax_utils <span class="hljs-keyword">import</span> replicate
<span class="hljs-keyword">from</span> flax.training.common_utils <span class="hljs-keyword">import</span> shard
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> FlaxStableDiffusionPipeline

model_path = <span class="hljs-string">&quot;path_to_saved_model&quot;</span>
pipe, params = FlaxStableDiffusionPipeline.from_pretrained(model_path, dtype=jax.numpy.bfloat16)

prompt = <span class="hljs-string">&quot;yoda pokemon&quot;</span>
prng_seed = jax.random.PRNGKey(<span class="hljs-number">0</span>)
num_inference_steps = <span class="hljs-number">50</span>

num_samples = jax.device_count()
prompt = num_samples * [prompt]
prompt_ids = pipeline.prepare_inputs(prompt)

<span class="hljs-comment"># shard inputs and rng</span>
params = replicate(params)
prng_seed = jax.random.split(prng_seed, jax.device_count())
prompt_ids = shard(prompt_ids)

images = pipeline(prompt_ids, params, prng_seed, num_inference_steps, jit=<span class="hljs-literal">True</span>).images
images = pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-<span class="hljs-number">3</span>:])))
image.save(<span class="hljs-string">&quot;yoda-pokemon.png&quot;</span>)`}}),{c(){w(p.$$.fragment)},l(l){v(p.$$.fragment,l)},m(l,$){y(p,l,$),g=!0},p:La,i(l){g||(b(p.$$.fragment,l),g=!0)},o(l){k(p.$$.fragment,l),g=!1},d(l){x(p,l)}}}function rr(N){let p,g;return p=new Fa({props:{$$slots:{default:[or]},$$scope:{ctx:N}}}),{c(){w(p.$$.fragment)},l(l){v(p.$$.fragment,l)},m(l,$){y(p,l,$),g=!0},p(l,$){const P={};$&2&&(P.$$scope={dirty:$,ctx:l}),p.$set(P)},i(l){g||(b(p.$$.fragment,l),g=!0)},o(l){k(p.$$.fragment,l),g=!1},d(l){x(p,l)}}}function nr(N){let p,g,l,$,P,S,se,le,pe,D,z,M,G,he,W,fe,ue,$e,U,ce,R,me,T,J,oe,O,K,L,I,X,q,de,we,F,Q,re,ee,ne,Y,V,C,H,ie,m,j,ve,A,te,u,E,be,ke,_e,Te,Z,xe,De,B,ct,mt,Qe,ae,dt,_t,Ca,Ha,At,Ga,Ba,ea,gt,za,ta,qe,Ue,Tt,Ze,Wa,Dt,Ya,aa,$t,Va,sa,et,oa,Me,Le,qt,tt,Ja,Mt,Ka,ra,wt,Xa,na,at,ia,ye,Qa,Nt,Za,es,St,ts,as,Rt,ss,os,la,Fe,rs,Ot,ns,is,pa,st,ha,Ne,Ce,It,ot,ls,Ut,ps,fa,He,ua,Se,Ge,Lt,rt,hs,Ft,fs,ca,Ee,us,nt,cs,ms,Ct,ds,_s,ma,Be,gs,it,$s,ws,da,je,Ht,vs,ys,lt,bs,Gt,ks,xs,Es,pt,js,Bt,Ps,As,_a,vt,Ts,ga,Pe,Ds,zt,qs,Ms,Wt,Ns,Ss,$a,ze,wa,Re,We,Yt,ht,Rs,Vt,Os,va,Ye,Is,yt,Us,Ls,ya,Oe,Ve,Jt,ft,Fs,Kt,Cs,ba,Je,Hs,bt,Gs,Bs,ka,Ke,xa;return S=new Ie({}),z=new Go({props:{warning:!0,$$slots:{default:[Ko]},$$scope:{ctx:N}}}),O=new ge({props:{code:`pip install git+https://github.com/huggingface/diffusers.git
pip install -U -r requirements.txt`,highlighted:`pip install git+https://github.com/huggingface/diffusers.git
pip install -U -r requirements.txt`}}),F=new ge({props:{code:"accelerate config",highlighted:"accelerate config"}}),H=new Ie({}),Ze=new Ie({}),et=new ge({props:{code:"  --push_to_hub",highlighted:"  --push_to_hub"}}),tt=new Ie({}),at=new ge({props:{code:"  --checkpointing_steps=500",highlighted:"  --checkpointing_steps=500"}}),st=new ge({props:{code:'  --resume_from_checkpoint="checkpoint-1500"',highlighted:'  --resume_from_checkpoint=<span class="hljs-string">&quot;checkpoint-1500&quot;</span>'}}),ot=new Ie({}),He=new Bo({props:{pytorch:!0,tensorflow:!1,jax:!0,$$slots:{jax:[er],pytorch:[Qo]},$$scope:{ctx:N}}}),rt=new Ie({}),ze=new Go({props:{warning:!0,$$slots:{default:[tr]},$$scope:{ctx:N}}}),ht=new Ie({}),ft=new Ie({}),Ke=new Bo({props:{pytorch:!0,tensorflow:!1,jax:!0,$$slots:{jax:[rr],pytorch:[sr]},$$scope:{ctx:N}}}),{c(){p=r("meta"),g=d(),l=r("h1"),$=r("a"),P=r("span"),w(S.$$.fragment),se=d(),le=r("span"),pe=s("Text-to-image"),D=d(),w(z.$$.fragment),M=d(),G=r("p"),he=s("Text-to-image models like Stable Diffusion generate an image from a text prompt. This guide will show you how to finetune the "),W=r("a"),fe=r("code"),ue=s("CompVis/stable-diffusion-v1-4"),$e=s(" model on your own dataset with PyTorch and Flax. All the training scripts for text-to-image finetuning used in this guide can be found in this "),U=r("a"),ce=s("repository"),R=s(" if you\u2019re interested in taking a closer look."),me=d(),T=r("p"),J=s("Before running the scripts, make sure to install the library\u2019s training dependencies:"),oe=d(),w(O.$$.fragment),K=d(),L=r("p"),I=s("And initialize an "),X=r("a"),q=s("\u{1F917} Accelerate"),de=s(" environment with:"),we=d(),w(F.$$.fragment),Q=d(),re=r("p"),ee=s("If you have already cloned the repo, then you won\u2019t need to go through these steps. Instead, you can pass the path to your local checkout to the training script and it will be loaded from there."),ne=d(),Y=r("h2"),V=r("a"),C=r("span"),w(H.$$.fragment),ie=d(),m=r("span"),j=s("Hardware requirements"),ve=d(),A=r("p"),te=s("Using "),u=r("code"),E=s("gradient_checkpointing"),be=s(" and "),ke=r("code"),_e=s("mixed_precision"),Te=s(", it should be possible to finetune the model on a single 24GB GPU. For higher "),Z=r("code"),xe=s("batch_size"),De=s("\u2019s and faster training, it\u2019s better to use GPUs with more than 30GB of GPU memory. You can also use JAX/Flax for fine-tuning on TPUs or GPUs, which will be covered "),B=r("a"),ct=s("below"),mt=s("."),Qe=d(),ae=r("p"),dt=s("You can reduce your memory footprint even more by enabling memory efficient attention with xFormers. Make sure you have "),_t=r("a"),Ca=s("xFormers installed"),Ha=s(" and pass the "),At=r("code"),Ga=s("--enable_xformers_memory_efficient_attention"),Ba=s(" flag to the training script."),ea=d(),gt=r("p"),za=s("xFormers is not available for Flax."),ta=d(),qe=r("h2"),Ue=r("a"),Tt=r("span"),w(Ze.$$.fragment),Wa=d(),Dt=r("span"),Ya=s("Upload model to Hub"),aa=d(),$t=r("p"),Va=s("Store your model on the Hub by adding the following argument to the training script:"),sa=d(),w(et.$$.fragment),oa=d(),Me=r("h2"),Le=r("a"),qt=r("span"),w(tt.$$.fragment),Ja=d(),Mt=r("span"),Ka=s("Save and load checkpoints"),ra=d(),wt=r("p"),Xa=s("It is a good idea to regularly save checkpoints in case anything happens during training. To save a checkpoint, pass the following argument to the training script:"),na=d(),w(at.$$.fragment),ia=d(),ye=r("p"),Qa=s("Every 500 steps, the full training state is saved in a subfolder in the "),Nt=r("code"),Za=s("output_dir"),es=s(". The checkpoint has the format "),St=r("code"),ts=s("checkpoint-"),as=s(" followed by the number of steps trained so far. For example, "),Rt=r("code"),ss=s("checkpoint-1500"),os=s(" is a checkpoint saved after 1500 training steps."),la=d(),Fe=r("p"),rs=s("To load a checkpoint to resume training, pass the argument "),Ot=r("code"),ns=s("--resume_from_checkpoint"),is=s(" to the training script and specify the checkpoint you want to resume from. For example, the following argument resumes training from the checkpoint saved after 1500 training steps:"),pa=d(),w(st.$$.fragment),ha=d(),Ne=r("h2"),Ce=r("a"),It=r("span"),w(ot.$$.fragment),ls=d(),Ut=r("span"),ps=s("Fine-tuning"),fa=d(),w(He.$$.fragment),ua=d(),Se=r("h2"),Ge=r("a"),Lt=r("span"),w(rt.$$.fragment),hs=d(),Ft=r("span"),fs=s("Training with Min-SNR weighting"),ca=d(),Ee=r("p"),us=s("We support training with the Min-SNR weighting strategy proposed in "),nt=r("a"),cs=s("Efficient Diffusion Training via Min-SNR Weighting Strategy"),ms=s(` which helps to achieve faster convergence
by rebalancing the loss. In order to use it, one needs to set the `),Ct=r("code"),ds=s("--snr_gamma"),_s=s(` argument. The recommended
value when using it is 5.0.`),ma=d(),Be=r("p"),gs=s("You can find "),it=r("a"),$s=s("this project on Weights and Biases"),ws=s(" that compares the loss surfaces of the following setups:"),da=d(),je=r("ul"),Ht=r("li"),vs=s("Training without the Min-SNR weighting strategy"),ys=d(),lt=r("li"),bs=s("Training with the Min-SNR weighting strategy ("),Gt=r("code"),ks=s("snr_gamma"),xs=s(" set to 5.0)"),Es=d(),pt=r("li"),js=s("Training with the Min-SNR weighting strategy ("),Bt=r("code"),Ps=s("snr_gamma"),As=s(" set to 1.0)"),_a=d(),vt=r("p"),Ts=s("For our small Pokemons dataset, the effects of Min-SNR weighting strategy might not appear to be pronounced, but for larger datasets, we believe the effects will be more pronounced."),ga=d(),Pe=r("p"),Ds=s("Also, note that in this example, we either predict "),zt=r("code"),qs=s("epsilon"),Ms=s(" (i.e., the noise) or the "),Wt=r("code"),Ns=s("v_prediction"),Ss=s(". For both of these cases, the formulation of the Min-SNR weighting strategy that we have used holds."),$a=d(),w(ze.$$.fragment),wa=d(),Re=r("h2"),We=r("a"),Yt=r("span"),w(ht.$$.fragment),Rs=d(),Vt=r("span"),Os=s("LoRA"),va=d(),Ye=r("p"),Is=s("You can also use Low-Rank Adaptation of Large Language Models (LoRA), a fine-tuning technique for accelerating training large models, for fine-tuning text-to-image models. For more details, take a look at the "),yt=r("a"),Us=s("LoRA training"),Ls=s(" guide."),ya=d(),Oe=r("h2"),Ve=r("a"),Jt=r("span"),w(ft.$$.fragment),Fs=d(),Kt=r("span"),Cs=s("Inference"),ba=d(),Je=r("p"),Hs=s("Now you can load the fine-tuned model for inference by passing the model path or model name on the Hub to the "),bt=r("a"),Gs=s("StableDiffusionPipeline"),Bs=s(":"),ka=d(),w(Ke.$$.fragment),this.h()},l(e){const f=Vo('[data-svelte="svelte-1phssyn"]',document.head);p=n(f,"META",{name:!0,content:!0}),f.forEach(t),g=_(e),l=n(e,"H1",{class:!0});var ut=i(l);$=n(ut,"A",{id:!0,class:!0,href:!0});var Xt=i($);P=n(Xt,"SPAN",{});var Qt=i(P);v(S.$$.fragment,Qt),Qt.forEach(t),Xt.forEach(t),se=_(ut),le=n(ut,"SPAN",{});var Zt=i(le);pe=o(Zt,"Text-to-image"),Zt.forEach(t),ut.forEach(t),D=_(e),v(z.$$.fragment,e),M=_(e),G=n(e,"P",{});var kt=i(G);he=o(kt,"Text-to-image models like Stable Diffusion generate an image from a text prompt. This guide will show you how to finetune the "),W=n(kt,"A",{href:!0,rel:!0});var zs=i(W);fe=n(zs,"CODE",{});var Ws=i(fe);ue=o(Ws,"CompVis/stable-diffusion-v1-4"),Ws.forEach(t),zs.forEach(t),$e=o(kt," model on your own dataset with PyTorch and Flax. All the training scripts for text-to-image finetuning used in this guide can be found in this "),U=n(kt,"A",{href:!0,rel:!0});var Ys=i(U);ce=o(Ys,"repository"),Ys.forEach(t),R=o(kt," if you\u2019re interested in taking a closer look."),kt.forEach(t),me=_(e),T=n(e,"P",{});var Vs=i(T);J=o(Vs,"Before running the scripts, make sure to install the library\u2019s training dependencies:"),Vs.forEach(t),oe=_(e),v(O.$$.fragment,e),K=_(e),L=n(e,"P",{});var Ea=i(L);I=o(Ea,"And initialize an "),X=n(Ea,"A",{href:!0,rel:!0});var Js=i(X);q=o(Js,"\u{1F917} Accelerate"),Js.forEach(t),de=o(Ea," environment with:"),Ea.forEach(t),we=_(e),v(F.$$.fragment,e),Q=_(e),re=n(e,"P",{});var Ks=i(re);ee=o(Ks,"If you have already cloned the repo, then you won\u2019t need to go through these steps. Instead, you can pass the path to your local checkout to the training script and it will be loaded from there."),Ks.forEach(t),ne=_(e),Y=n(e,"H2",{class:!0});var ja=i(Y);V=n(ja,"A",{id:!0,class:!0,href:!0});var Xs=i(V);C=n(Xs,"SPAN",{});var Qs=i(C);v(H.$$.fragment,Qs),Qs.forEach(t),Xs.forEach(t),ie=_(ja),m=n(ja,"SPAN",{});var Zs=i(m);j=o(Zs,"Hardware requirements"),Zs.forEach(t),ja.forEach(t),ve=_(e),A=n(e,"P",{});var Ae=i(A);te=o(Ae,"Using "),u=n(Ae,"CODE",{});var eo=i(u);E=o(eo,"gradient_checkpointing"),eo.forEach(t),be=o(Ae," and "),ke=n(Ae,"CODE",{});var to=i(ke);_e=o(to,"mixed_precision"),to.forEach(t),Te=o(Ae,", it should be possible to finetune the model on a single 24GB GPU. For higher "),Z=n(Ae,"CODE",{});var ao=i(Z);xe=o(ao,"batch_size"),ao.forEach(t),De=o(Ae,"\u2019s and faster training, it\u2019s better to use GPUs with more than 30GB of GPU memory. You can also use JAX/Flax for fine-tuning on TPUs or GPUs, which will be covered "),B=n(Ae,"A",{href:!0});var so=i(B);ct=o(so,"below"),so.forEach(t),mt=o(Ae,"."),Ae.forEach(t),Qe=_(e),ae=n(e,"P",{});var xt=i(ae);dt=o(xt,"You can reduce your memory footprint even more by enabling memory efficient attention with xFormers. Make sure you have "),_t=n(xt,"A",{href:!0});var oo=i(_t);Ca=o(oo,"xFormers installed"),oo.forEach(t),Ha=o(xt," and pass the "),At=n(xt,"CODE",{});var ro=i(At);Ga=o(ro,"--enable_xformers_memory_efficient_attention"),ro.forEach(t),Ba=o(xt," flag to the training script."),xt.forEach(t),ea=_(e),gt=n(e,"P",{});var no=i(gt);za=o(no,"xFormers is not available for Flax."),no.forEach(t),ta=_(e),qe=n(e,"H2",{class:!0});var Pa=i(qe);Ue=n(Pa,"A",{id:!0,class:!0,href:!0});var io=i(Ue);Tt=n(io,"SPAN",{});var lo=i(Tt);v(Ze.$$.fragment,lo),lo.forEach(t),io.forEach(t),Wa=_(Pa),Dt=n(Pa,"SPAN",{});var po=i(Dt);Ya=o(po,"Upload model to Hub"),po.forEach(t),Pa.forEach(t),aa=_(e),$t=n(e,"P",{});var ho=i($t);Va=o(ho,"Store your model on the Hub by adding the following argument to the training script:"),ho.forEach(t),sa=_(e),v(et.$$.fragment,e),oa=_(e),Me=n(e,"H2",{class:!0});var Aa=i(Me);Le=n(Aa,"A",{id:!0,class:!0,href:!0});var fo=i(Le);qt=n(fo,"SPAN",{});var uo=i(qt);v(tt.$$.fragment,uo),uo.forEach(t),fo.forEach(t),Ja=_(Aa),Mt=n(Aa,"SPAN",{});var co=i(Mt);Ka=o(co,"Save and load checkpoints"),co.forEach(t),Aa.forEach(t),ra=_(e),wt=n(e,"P",{});var mo=i(wt);Xa=o(mo,"It is a good idea to regularly save checkpoints in case anything happens during training. To save a checkpoint, pass the following argument to the training script:"),mo.forEach(t),na=_(e),v(at.$$.fragment,e),ia=_(e),ye=n(e,"P",{});var Xe=i(ye);Qa=o(Xe,"Every 500 steps, the full training state is saved in a subfolder in the "),Nt=n(Xe,"CODE",{});var _o=i(Nt);Za=o(_o,"output_dir"),_o.forEach(t),es=o(Xe,". The checkpoint has the format "),St=n(Xe,"CODE",{});var go=i(St);ts=o(go,"checkpoint-"),go.forEach(t),as=o(Xe," followed by the number of steps trained so far. For example, "),Rt=n(Xe,"CODE",{});var $o=i(Rt);ss=o($o,"checkpoint-1500"),$o.forEach(t),os=o(Xe," is a checkpoint saved after 1500 training steps."),Xe.forEach(t),la=_(e),Fe=n(e,"P",{});var Ta=i(Fe);rs=o(Ta,"To load a checkpoint to resume training, pass the argument "),Ot=n(Ta,"CODE",{});var wo=i(Ot);ns=o(wo,"--resume_from_checkpoint"),wo.forEach(t),is=o(Ta," to the training script and specify the checkpoint you want to resume from. For example, the following argument resumes training from the checkpoint saved after 1500 training steps:"),Ta.forEach(t),pa=_(e),v(st.$$.fragment,e),ha=_(e),Ne=n(e,"H2",{class:!0});var Da=i(Ne);Ce=n(Da,"A",{id:!0,class:!0,href:!0});var vo=i(Ce);It=n(vo,"SPAN",{});var yo=i(It);v(ot.$$.fragment,yo),yo.forEach(t),vo.forEach(t),ls=_(Da),Ut=n(Da,"SPAN",{});var bo=i(Ut);ps=o(bo,"Fine-tuning"),bo.forEach(t),Da.forEach(t),fa=_(e),v(He.$$.fragment,e),ua=_(e),Se=n(e,"H2",{class:!0});var qa=i(Se);Ge=n(qa,"A",{id:!0,class:!0,href:!0});var ko=i(Ge);Lt=n(ko,"SPAN",{});var xo=i(Lt);v(rt.$$.fragment,xo),xo.forEach(t),ko.forEach(t),hs=_(qa),Ft=n(qa,"SPAN",{});var Eo=i(Ft);fs=o(Eo,"Training with Min-SNR weighting"),Eo.forEach(t),qa.forEach(t),ca=_(e),Ee=n(e,"P",{});var Et=i(Ee);us=o(Et,"We support training with the Min-SNR weighting strategy proposed in "),nt=n(Et,"A",{href:!0,rel:!0});var jo=i(nt);cs=o(jo,"Efficient Diffusion Training via Min-SNR Weighting Strategy"),jo.forEach(t),ms=o(Et,` which helps to achieve faster convergence
by rebalancing the loss. In order to use it, one needs to set the `),Ct=n(Et,"CODE",{});var Po=i(Ct);ds=o(Po,"--snr_gamma"),Po.forEach(t),_s=o(Et,` argument. The recommended
value when using it is 5.0.`),Et.forEach(t),ma=_(e),Be=n(e,"P",{});var Ma=i(Be);gs=o(Ma,"You can find "),it=n(Ma,"A",{href:!0,rel:!0});var Ao=i(it);$s=o(Ao,"this project on Weights and Biases"),Ao.forEach(t),ws=o(Ma," that compares the loss surfaces of the following setups:"),Ma.forEach(t),da=_(e),je=n(e,"UL",{});var jt=i(je);Ht=n(jt,"LI",{});var To=i(Ht);vs=o(To,"Training without the Min-SNR weighting strategy"),To.forEach(t),ys=_(jt),lt=n(jt,"LI",{});var Na=i(lt);bs=o(Na,"Training with the Min-SNR weighting strategy ("),Gt=n(Na,"CODE",{});var Do=i(Gt);ks=o(Do,"snr_gamma"),Do.forEach(t),xs=o(Na," set to 5.0)"),Na.forEach(t),Es=_(jt),pt=n(jt,"LI",{});var Sa=i(pt);js=o(Sa,"Training with the Min-SNR weighting strategy ("),Bt=n(Sa,"CODE",{});var qo=i(Bt);Ps=o(qo,"snr_gamma"),qo.forEach(t),As=o(Sa," set to 1.0)"),Sa.forEach(t),jt.forEach(t),_a=_(e),vt=n(e,"P",{});var Mo=i(vt);Ts=o(Mo,"For our small Pokemons dataset, the effects of Min-SNR weighting strategy might not appear to be pronounced, but for larger datasets, we believe the effects will be more pronounced."),Mo.forEach(t),ga=_(e),Pe=n(e,"P",{});var Pt=i(Pe);Ds=o(Pt,"Also, note that in this example, we either predict "),zt=n(Pt,"CODE",{});var No=i(zt);qs=o(No,"epsilon"),No.forEach(t),Ms=o(Pt," (i.e., the noise) or the "),Wt=n(Pt,"CODE",{});var So=i(Wt);Ns=o(So,"v_prediction"),So.forEach(t),Ss=o(Pt,". For both of these cases, the formulation of the Min-SNR weighting strategy that we have used holds."),Pt.forEach(t),$a=_(e),v(ze.$$.fragment,e),wa=_(e),Re=n(e,"H2",{class:!0});var Ra=i(Re);We=n(Ra,"A",{id:!0,class:!0,href:!0});var Ro=i(We);Yt=n(Ro,"SPAN",{});var Oo=i(Yt);v(ht.$$.fragment,Oo),Oo.forEach(t),Ro.forEach(t),Rs=_(Ra),Vt=n(Ra,"SPAN",{});var Io=i(Vt);Os=o(Io,"LoRA"),Io.forEach(t),Ra.forEach(t),va=_(e),Ye=n(e,"P",{});var Oa=i(Ye);Is=o(Oa,"You can also use Low-Rank Adaptation of Large Language Models (LoRA), a fine-tuning technique for accelerating training large models, for fine-tuning text-to-image models. For more details, take a look at the "),yt=n(Oa,"A",{href:!0});var Uo=i(yt);Us=o(Uo,"LoRA training"),Uo.forEach(t),Ls=o(Oa," guide."),Oa.forEach(t),ya=_(e),Oe=n(e,"H2",{class:!0});var Ia=i(Oe);Ve=n(Ia,"A",{id:!0,class:!0,href:!0});var Lo=i(Ve);Jt=n(Lo,"SPAN",{});var Fo=i(Jt);v(ft.$$.fragment,Fo),Fo.forEach(t),Lo.forEach(t),Fs=_(Ia),Kt=n(Ia,"SPAN",{});var Co=i(Kt);Cs=o(Co,"Inference"),Co.forEach(t),Ia.forEach(t),ba=_(e),Je=n(e,"P",{});var Ua=i(Je);Hs=o(Ua,"Now you can load the fine-tuned model for inference by passing the model path or model name on the Hub to the "),bt=n(Ua,"A",{href:!0});var Ho=i(bt);Gs=o(Ho,"StableDiffusionPipeline"),Ho.forEach(t),Bs=o(Ua,":"),Ua.forEach(t),ka=_(e),v(Ke.$$.fragment,e),this.h()},h(){c(p,"name","hf:doc:metadata"),c(p,"content",JSON.stringify(ir)),c($,"id","texttoimage"),c($,"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"),c($,"href","#texttoimage"),c(l,"class","relative group"),c(W,"href","https://huggingface.co/CompVis/stable-diffusion-v1-4"),c(W,"rel","nofollow"),c(U,"href","https://github.com/huggingface/diffusers/tree/main/examples/text_to_image"),c(U,"rel","nofollow"),c(X,"href","https://github.com/huggingface/accelerate/"),c(X,"rel","nofollow"),c(V,"id","hardware-requirements"),c(V,"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"),c(V,"href","#hardware-requirements"),c(Y,"class","relative group"),c(B,"href","#flax-jax-finetuning"),c(_t,"href","./optimization/xformers"),c(Ue,"id","upload-model-to-hub"),c(Ue,"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"),c(Ue,"href","#upload-model-to-hub"),c(qe,"class","relative group"),c(Le,"id","save-and-load-checkpoints"),c(Le,"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"),c(Le,"href","#save-and-load-checkpoints"),c(Me,"class","relative group"),c(Ce,"id","finetuning"),c(Ce,"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"),c(Ce,"href","#finetuning"),c(Ne,"class","relative group"),c(Ge,"id","training-with-minsnr-weighting"),c(Ge,"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"),c(Ge,"href","#training-with-minsnr-weighting"),c(Se,"class","relative group"),c(nt,"href","https://arxiv.org/abs/2303.09556"),c(nt,"rel","nofollow"),c(it,"href","https://wandb.ai/sayakpaul/text2image-finetune-minsnr"),c(it,"rel","nofollow"),c(We,"id","lora"),c(We,"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"),c(We,"href","#lora"),c(Re,"class","relative group"),c(yt,"href","lora#text-to-image"),c(Ve,"id","inference"),c(Ve,"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"),c(Ve,"href","#inference"),c(Oe,"class","relative group"),c(bt,"href","/docs/diffusers/v0.16.0/en/api/pipelines/stable_diffusion/text2img#diffusers.StableDiffusionPipeline")},m(e,f){a(document.head,p),h(e,g,f),h(e,l,f),a(l,$),a($,P),y(S,P,null),a(l,se),a(l,le),a(le,pe),h(e,D,f),y(z,e,f),h(e,M,f),h(e,G,f),a(G,he),a(G,W),a(W,fe),a(fe,ue),a(G,$e),a(G,U),a(U,ce),a(G,R),h(e,me,f),h(e,T,f),a(T,J),h(e,oe,f),y(O,e,f),h(e,K,f),h(e,L,f),a(L,I),a(L,X),a(X,q),a(L,de),h(e,we,f),y(F,e,f),h(e,Q,f),h(e,re,f),a(re,ee),h(e,ne,f),h(e,Y,f),a(Y,V),a(V,C),y(H,C,null),a(Y,ie),a(Y,m),a(m,j),h(e,ve,f),h(e,A,f),a(A,te),a(A,u),a(u,E),a(A,be),a(A,ke),a(ke,_e),a(A,Te),a(A,Z),a(Z,xe),a(A,De),a(A,B),a(B,ct),a(A,mt),h(e,Qe,f),h(e,ae,f),a(ae,dt),a(ae,_t),a(_t,Ca),a(ae,Ha),a(ae,At),a(At,Ga),a(ae,Ba),h(e,ea,f),h(e,gt,f),a(gt,za),h(e,ta,f),h(e,qe,f),a(qe,Ue),a(Ue,Tt),y(Ze,Tt,null),a(qe,Wa),a(qe,Dt),a(Dt,Ya),h(e,aa,f),h(e,$t,f),a($t,Va),h(e,sa,f),y(et,e,f),h(e,oa,f),h(e,Me,f),a(Me,Le),a(Le,qt),y(tt,qt,null),a(Me,Ja),a(Me,Mt),a(Mt,Ka),h(e,ra,f),h(e,wt,f),a(wt,Xa),h(e,na,f),y(at,e,f),h(e,ia,f),h(e,ye,f),a(ye,Qa),a(ye,Nt),a(Nt,Za),a(ye,es),a(ye,St),a(St,ts),a(ye,as),a(ye,Rt),a(Rt,ss),a(ye,os),h(e,la,f),h(e,Fe,f),a(Fe,rs),a(Fe,Ot),a(Ot,ns),a(Fe,is),h(e,pa,f),y(st,e,f),h(e,ha,f),h(e,Ne,f),a(Ne,Ce),a(Ce,It),y(ot,It,null),a(Ne,ls),a(Ne,Ut),a(Ut,ps),h(e,fa,f),y(He,e,f),h(e,ua,f),h(e,Se,f),a(Se,Ge),a(Ge,Lt),y(rt,Lt,null),a(Se,hs),a(Se,Ft),a(Ft,fs),h(e,ca,f),h(e,Ee,f),a(Ee,us),a(Ee,nt),a(nt,cs),a(Ee,ms),a(Ee,Ct),a(Ct,ds),a(Ee,_s),h(e,ma,f),h(e,Be,f),a(Be,gs),a(Be,it),a(it,$s),a(Be,ws),h(e,da,f),h(e,je,f),a(je,Ht),a(Ht,vs),a(je,ys),a(je,lt),a(lt,bs),a(lt,Gt),a(Gt,ks),a(lt,xs),a(je,Es),a(je,pt),a(pt,js),a(pt,Bt),a(Bt,Ps),a(pt,As),h(e,_a,f),h(e,vt,f),a(vt,Ts),h(e,ga,f),h(e,Pe,f),a(Pe,Ds),a(Pe,zt),a(zt,qs),a(Pe,Ms),a(Pe,Wt),a(Wt,Ns),a(Pe,Ss),h(e,$a,f),y(ze,e,f),h(e,wa,f),h(e,Re,f),a(Re,We),a(We,Yt),y(ht,Yt,null),a(Re,Rs),a(Re,Vt),a(Vt,Os),h(e,va,f),h(e,Ye,f),a(Ye,Is),a(Ye,yt),a(yt,Us),a(Ye,Ls),h(e,ya,f),h(e,Oe,f),a(Oe,Ve),a(Ve,Jt),y(ft,Jt,null),a(Oe,Fs),a(Oe,Kt),a(Kt,Cs),h(e,ba,f),h(e,Je,f),a(Je,Hs),a(Je,bt),a(bt,Gs),a(Je,Bs),h(e,ka,f),y(Ke,e,f),xa=!0},p(e,[f]){const ut={};f&2&&(ut.$$scope={dirty:f,ctx:e}),z.$set(ut);const Xt={};f&2&&(Xt.$$scope={dirty:f,ctx:e}),He.$set(Xt);const Qt={};f&2&&(Qt.$$scope={dirty:f,ctx:e}),ze.$set(Qt);const Zt={};f&2&&(Zt.$$scope={dirty:f,ctx:e}),Ke.$set(Zt)},i(e){xa||(b(S.$$.fragment,e),b(z.$$.fragment,e),b(O.$$.fragment,e),b(F.$$.fragment,e),b(H.$$.fragment,e),b(Ze.$$.fragment,e),b(et.$$.fragment,e),b(tt.$$.fragment,e),b(at.$$.fragment,e),b(st.$$.fragment,e),b(ot.$$.fragment,e),b(He.$$.fragment,e),b(rt.$$.fragment,e),b(ze.$$.fragment,e),b(ht.$$.fragment,e),b(ft.$$.fragment,e),b(Ke.$$.fragment,e),xa=!0)},o(e){k(S.$$.fragment,e),k(z.$$.fragment,e),k(O.$$.fragment,e),k(F.$$.fragment,e),k(H.$$.fragment,e),k(Ze.$$.fragment,e),k(et.$$.fragment,e),k(tt.$$.fragment,e),k(at.$$.fragment,e),k(st.$$.fragment,e),k(ot.$$.fragment,e),k(He.$$.fragment,e),k(rt.$$.fragment,e),k(ze.$$.fragment,e),k(ht.$$.fragment,e),k(ft.$$.fragment,e),k(Ke.$$.fragment,e),xa=!1},d(e){t(p),e&&t(g),e&&t(l),x(S),e&&t(D),x(z,e),e&&t(M),e&&t(G),e&&t(me),e&&t(T),e&&t(oe),x(O,e),e&&t(K),e&&t(L),e&&t(we),x(F,e),e&&t(Q),e&&t(re),e&&t(ne),e&&t(Y),x(H),e&&t(ve),e&&t(A),e&&t(Qe),e&&t(ae),e&&t(ea),e&&t(gt),e&&t(ta),e&&t(qe),x(Ze),e&&t(aa),e&&t($t),e&&t(sa),x(et,e),e&&t(oa),e&&t(Me),x(tt),e&&t(ra),e&&t(wt),e&&t(na),x(at,e),e&&t(ia),e&&t(ye),e&&t(la),e&&t(Fe),e&&t(pa),x(st,e),e&&t(ha),e&&t(Ne),x(ot),e&&t(fa),x(He,e),e&&t(ua),e&&t(Se),x(rt),e&&t(ca),e&&t(Ee),e&&t(ma),e&&t(Be),e&&t(da),e&&t(je),e&&t(_a),e&&t(vt),e&&t(ga),e&&t(Pe),e&&t($a),x(ze,e),e&&t(wa),e&&t(Re),x(ht),e&&t(va),e&&t(Ye),e&&t(ya),e&&t(Oe),x(ft),e&&t(ba),e&&t(Je),e&&t(ka),x(Ke,e)}}}const ir={local:"texttoimage",sections:[{local:"hardware-requirements",title:"Hardware requirements"},{local:"upload-model-to-hub",title:"Upload model to Hub"},{local:"save-and-load-checkpoints",title:"Save and load checkpoints"},{local:"finetuning",sections:[{local:"training-with-multiple-gpus",title:"Training with multiple GPUs"}],title:"Fine-tuning"},{local:"training-with-minsnr-weighting",title:"Training with Min-SNR weighting"},{local:"lora",title:"LoRA"},{local:"inference",title:"Inference"}],title:"Text-to-image"};function lr(N){return Jo(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class mr extends zo{constructor(p){super();Wo(this,p,lr,nr,Yo,{})}}export{mr as default,ir as metadata};
