import{s as Ul,o as Tl,n as Rt}from"../chunks/scheduler.53228c21.js";import{S as bl,i as _l,e as p,s as a,c as d,h as jl,a as c,d as l,b as n,f as wl,g as f,j as M,k as We,l as Cl,m as s,n as y,t as h,o as w,p as J}from"../chunks/index.100fac89.js";import{C as gl}from"../chunks/CopyLLMTxtMenu.20a72e0e.js";import{C as j}from"../chunks/CodeBlock.d30a6509.js";import{H as Xe,E as Il}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.4d82655d.js";import{H as Jl,a as Gt}from"../chunks/HfOption.fad27e59.js";function Zl(Z){let i,m;return i=new j({props:{code:"ZXhwb3J0JTIwTU9ERUxfTkFNRSUzRCUyMkNvbXBWaXMlMkZzdGFibGUtZGlmZnVzaW9uLXYxLTQlMjIlMEFleHBvcnQlMjBPVVRQVVRfRElSJTNEJTIycGF0aC10by1zYXZlLW1vZGVsJTIyJTBBZXhwb3J0JTIwSU5TVEFOQ0VfRElSJTNEJTIyLiUyRmRhdGElMkZjYXQlMjIlMEElMEFhY2NlbGVyYXRlJTIwbGF1bmNoJTIwdHJhaW5fY3VzdG9tX2RpZmZ1c2lvbi5weSUyMCU1QyUwQSUyMCUyMC0tcHJldHJhaW5lZF9tb2RlbF9uYW1lX29yX3BhdGglM0QlMjRNT0RFTF9OQU1FJTIwJTIwJTVDJTBBJTIwJTIwLS1pbnN0YW5jZV9kYXRhX2RpciUzRCUyNElOU1RBTkNFX0RJUiUyMCU1QyUwQSUyMCUyMC0tb3V0cHV0X2RpciUzRCUyNE9VVFBVVF9ESVIlMjAlNUMlMEElMjAlMjAtLWNsYXNzX2RhdGFfZGlyJTNELiUyRnJlYWxfcmVnJTJGc2FtcGxlc19jYXQlMkYlMjAlNUMlMEElMjAlMjAtLXdpdGhfcHJpb3JfcHJlc2VydmF0aW9uJTIwJTVDJTBBJTIwJTIwLS1yZWFsX3ByaW9yJTIwJTVDJTBBJTIwJTIwLS1wcmlvcl9sb3NzX3dlaWdodCUzRDEuMCUyMCU1QyUwQSUyMCUyMC0tY2xhc3NfcHJvbXB0JTNEJTIyY2F0JTIyJTIwJTVDJTBBJTIwJTIwLS1udW1fY2xhc3NfaW1hZ2VzJTNEMjAwJTIwJTVDJTBBJTIwJTIwLS1pbnN0YW5jZV9wcm9tcHQlM0QlMjJwaG90byUyMG9mJTIwYSUyMCUzQ25ldzElM0UlMjBjYXQlMjIlMjAlMjAlNUMlMEElMjAlMjAtLXJlc29sdXRpb24lM0Q1MTIlMjAlMjAlNUMlMEElMjAlMjAtLXRyYWluX2JhdGNoX3NpemUlM0QyJTIwJTIwJTVDJTBBJTIwJTIwLS1sZWFybmluZ19yYXRlJTNEMWUtNSUyMCUyMCU1QyUwQSUyMCUyMC0tbHJfd2FybXVwX3N0ZXBzJTNEMCUyMCU1QyUwQSUyMCUyMC0tbWF4X3RyYWluX3N0ZXBzJTNEMjUwJTIwJTVDJTBBJTIwJTIwLS1zY2FsZV9sciUyMCU1QyUwQSUyMCUyMC0taGZsaXAlMjAlMjAlNUMlMEElMjAlMjAtLW1vZGlmaWVyX3Rva2VuJTIwJTIyJTNDbmV3MSUzRSUyMiUyMCU1QyUwQSUyMCUyMC0tdmFsaWRhdGlvbl9wcm9tcHQlM0QlMjIlM0NuZXcxJTNFJTIwY2F0JTIwc2l0dGluZyUyMGluJTIwYSUyMGJ1Y2tldCUyMiUyMCU1QyUwQSUyMCUyMC0tcmVwb3J0X3RvJTNEJTIyd2FuZGIlMjIlMjAlNUMlMEElMjAlMjAtLXB1c2hfdG9faHVi",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> OUTPUT_DIR=<span class="hljs-string">&quot;path-to-save-model&quot;</span>
<span class="hljs-built_in">export</span> INSTANCE_DIR=<span class="hljs-string">&quot;./data/cat&quot;</span>

accelerate launch train_custom_diffusion.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> \\
  --class_data_dir=./real_reg/samples_cat/ \\
  --with_prior_preservation \\
  --real_prior \\
  --prior_loss_weight=1.0 \\
  --class_prompt=<span class="hljs-string">&quot;cat&quot;</span> \\
  --num_class_images=200 \\
  --instance_prompt=<span class="hljs-string">&quot;photo of a &lt;new1&gt; cat&quot;</span>  \\
  --resolution=512  \\
  --train_batch_size=2  \\
  --learning_rate=1e-5  \\
  --lr_warmup_steps=0 \\
  --max_train_steps=250 \\
  --scale_lr \\
  --hflip  \\
  --modifier_token <span class="hljs-string">&quot;&lt;new1&gt;&quot;</span> \\
  --validation_prompt=<span class="hljs-string">&quot;&lt;new1&gt; cat sitting in a bucket&quot;</span> \\
  --report_to=<span class="hljs-string">&quot;wandb&quot;</span> \\
  --push_to_hub`,wrap:!1}}),{c(){d(i.$$.fragment)},l(o){f(i.$$.fragment,o)},m(o,U){y(i,o,U),m=!0},p:Rt,i(o){m||(h(i.$$.fragment,o),m=!0)},o(o){w(i.$$.fragment,o),m=!1},d(o){J(i,o)}}}function $l(Z){let i,m='Custom Diffusion can also learn multiple concepts if you provide a <a href="https://github.com/adobe-research/custom-diffusion/blob/main/assets/concept_list.json" rel="nofollow">JSON</a> file with some details about each concept it should learn.',o,U,r="Run clip-retrieval to collect some real images to use for regularization:",T,b,C,g,ve="Then you can launch the script:",$,I,R;return b=new j({props:{code:"cGlwJTIwaW5zdGFsbCUyMGNsaXAtcmV0cmlldmFsJTBBcHl0aG9uJTIwcmV0cmlldmUucHklMjAtLWNsYXNzX3Byb21wdCUyMCU3QiU3RCUyMC0tY2xhc3NfZGF0YV9kaXIlMjAlN0IlN0QlMjAtLW51bV9jbGFzc19pbWFnZXMlMjAyMDA=",highlighted:`pip install clip-retrieval
python retrieve.py --class_prompt {} --class_data_dir {} --num_class_images 200`,wrap:!1}}),I=new j({props:{code:"ZXhwb3J0JTIwTU9ERUxfTkFN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`<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> OUTPUT_DIR=<span class="hljs-string">&quot;path-to-save-model&quot;</span>

accelerate launch train_custom_diffusion.py \\
  --pretrained_model_name_or_path=<span class="hljs-variable">$MODEL_NAME</span>  \\
  --output_dir=<span class="hljs-variable">$OUTPUT_DIR</span> \\
  --concepts_list=./concept_list.json \\
  --with_prior_preservation \\
  --real_prior \\
  --prior_loss_weight=1.0 \\
  --resolution=512  \\
  --train_batch_size=2  \\
  --learning_rate=1e-5  \\
  --lr_warmup_steps=0 \\
  --max_train_steps=500 \\
  --num_class_images=200 \\
  --scale_lr \\
  --hflip  \\
  --modifier_token <span class="hljs-string">&quot;&lt;new1&gt;+&lt;new2&gt;&quot;</span> \\
  --push_to_hub`,wrap:!1}}),{c(){i=p("p"),i.innerHTML=m,o=a(),U=p("p"),U.textContent=r,T=a(),d(b.$$.fragment),C=a(),g=p("p"),g.textContent=ve,$=a(),d(I.$$.fragment)},l(u){i=c(u,"P",{"data-svelte-h":!0}),M(i)!=="svelte-dc1va4"&&(i.innerHTML=m),o=n(u),U=c(u,"P",{"data-svelte-h":!0}),M(U)!=="svelte-wqrprq"&&(U.textContent=r),T=n(u),f(b.$$.fragment,u),C=n(u),g=c(u,"P",{"data-svelte-h":!0}),M(g)!=="svelte-1etdm7l"&&(g.textContent=ve),$=n(u),f(I.$$.fragment,u)},m(u,_){s(u,i,_),s(u,o,_),s(u,U,_),s(u,T,_),y(b,u,_),s(u,C,_),s(u,g,_),s(u,$,_),y(I,u,_),R=!0},p:Rt,i(u){R||(h(b.$$.fragment,u),h(I.$$.fragment,u),R=!0)},o(u){w(b.$$.fragment,u),w(I.$$.fragment,u),R=!1},d(u){u&&(l(i),l(o),l(U),l(T),l(C),l(g),l($)),J(b,u),J(I,u)}}}function Xl(Z){let i,m,o,U;return i=new Gt({props:{id:"training-inference",option:"single concept",$$slots:{default:[Zl]},$$scope:{ctx:Z}}}),o=new Gt({props:{id:"training-inference",option:"multiple concepts",$$slots:{default:[$l]},$$scope:{ctx:Z}}}),{c(){d(i.$$.fragment),m=a(),d(o.$$.fragment)},l(r){f(i.$$.fragment,r),m=n(r),f(o.$$.fragment,r)},m(r,T){y(i,r,T),s(r,m,T),y(o,r,T),U=!0},p(r,T){const b={};T&2&&(b.$$scope={dirty:T,ctx:r}),i.$set(b);const C={};T&2&&(C.$$scope={dirty:T,ctx:r}),o.$set(C)},i(r){U||(h(i.$$.fragment,r),h(o.$$.fragment,r),U=!0)},o(r){w(i.$$.fragment,r),w(o.$$.fragment,r),U=!1},d(r){r&&l(m),J(i,r),J(o,r)}}}function Wl(Z){let i,m;return i=new j({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGlmZnVzZXJzJTIwaW1wb3J0JTIwRGlmZnVzaW9uUGlwZWxpbmUlMEElMEFwaXBlbGluZSUyMCUzRCUyMERpZmZ1c2lvblBpcGVsaW5lLmZyb21fcHJldHJhaW5lZCglMEElMjAlMjAlMjAlMjAlMjJDb21wVmlzJTJGc3RhYmxlLWRpZmZ1c2lvbi12MS00JTIyJTJDJTIwdG9yY2hfZHR5cGUlM0R0b3JjaC5mbG9hdDE2JTJDJTBBKS50byglMjJjdWRhJTIyKSUwQXBpcGVsaW5lLnVuZXQubG9hZF9hdHRuX3Byb2NzKCUyMnBhdGgtdG8tc2F2ZS1tb2RlbCUyMiUyQyUyMHdlaWdodF9uYW1lJTNEJTIycHl0b3JjaF9jdXN0b21fZGlmZnVzaW9uX3dlaWdodHMuYmluJTIyKSUwQXBpcGVsaW5lLmxvYWRfdGV4dHVhbF9pbnZlcnNpb24oJTIycGF0aC10by1zYXZlLW1vZGVsJTIyJTJDJTIwd2VpZ2h0X25hbWUlM0QlMjIlM0NuZXcxJTNFLmJpbiUyMiklMEElMEFpbWFnZSUyMCUzRCUyMHBpcGVsaW5lKCUwQSUyMCUyMCUyMCUyMCUyMiUzQ25ldzElM0UlMjBjYXQlMjBzaXR0aW5nJTIwaW4lMjBhJTIwYnVja2V0JTIyJTJDJTBBJTIwJTIwJTIwJTIwbnVtX2luZmVyZW5jZV9zdGVwcyUzRDEwMCUyQyUwQSUyMCUyMCUyMCUyMGd1aWRhbmNlX3NjYWxlJTNENi4wJTJDJTBBJTIwJTIwJTIwJTIwZXRhJTNEMS4wJTJDJTBBKS5pbWFnZXMlNUIwJTVEJTBBaW1hZ2Uuc2F2ZSglMjJjYXQucG5nJTIyKQ==",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline

pipeline = DiffusionPipeline.from_pretrained(
    <span class="hljs-string">&quot;CompVis/stable-diffusion-v1-4&quot;</span>, torch_dtype=torch.float16,
).to(<span class="hljs-string">&quot;cuda&quot;</span>)
pipeline.unet.load_attn_procs(<span class="hljs-string">&quot;path-to-save-model&quot;</span>, weight_name=<span class="hljs-string">&quot;pytorch_custom_diffusion_weights.bin&quot;</span>)
pipeline.load_textual_inversion(<span class="hljs-string">&quot;path-to-save-model&quot;</span>, weight_name=<span class="hljs-string">&quot;&lt;new1&gt;.bin&quot;</span>)

image = pipeline(
    <span class="hljs-string">&quot;&lt;new1&gt; cat sitting in a bucket&quot;</span>,
    num_inference_steps=<span class="hljs-number">100</span>,
    guidance_scale=<span class="hljs-number">6.0</span>,
    eta=<span class="hljs-number">1.0</span>,
).images[<span class="hljs-number">0</span>]
image.save(<span class="hljs-string">&quot;cat.png&quot;</span>)`,wrap:!1}}),{c(){d(i.$$.fragment)},l(o){f(i.$$.fragment,o)},m(o,U){y(i,o,U),m=!0},p:Rt,i(o){m||(h(i.$$.fragment,o),m=!0)},o(o){w(i.$$.fragment,o),m=!1},d(o){J(i,o)}}}function vl(Z){let i,m;return i=new j({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwaHVnZ2luZ2ZhY2VfaHViLnJlcG9jYXJkJTIwaW1wb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LjAlMkMlMEElMjAlMjAlMjAlMjBldGElM0QxLjAlMkMlMEEpLmltYWdlcyU1QjAlNUQlMEFpbWFnZS5zYXZlKCUyMm11bHRpLXN1YmplY3QucG5nJTIyKQ==",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> huggingface_hub.repocard <span class="hljs-keyword">import</span> RepoCard
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline

pipeline = DiffusionPipeline.from_pretrained(
    <span class="hljs-string">&quot;CompVis/stable-diffusion-v1-4&quot;</span>, torch_dtype=torch.float16,
).to(<span class="hljs-string">&quot;cuda&quot;</span>)
model_id = <span class="hljs-string">&quot;sayakpaul/custom-diffusion-cat-wooden-pot&quot;</span>
pipeline.unet.load_attn_procs(model_id, weight_name=<span class="hljs-string">&quot;pytorch_custom_diffusion_weights.bin&quot;</span>)
pipeline.load_textual_inversion(model_id, weight_name=<span class="hljs-string">&quot;&lt;new1&gt;.bin&quot;</span>)
pipeline.load_textual_inversion(model_id, weight_name=<span class="hljs-string">&quot;&lt;new2&gt;.bin&quot;</span>)

image = pipeline(
    <span class="hljs-string">&quot;the &lt;new1&gt; cat sculpture in the style of a &lt;new2&gt; wooden pot&quot;</span>,
    num_inference_steps=<span class="hljs-number">100</span>,
    guidance_scale=<span class="hljs-number">6.0</span>,
    eta=<span class="hljs-number">1.0</span>,
).images[<span class="hljs-number">0</span>]
image.save(<span class="hljs-string">&quot;multi-subject.png&quot;</span>)`,wrap:!1}}),{c(){d(i.$$.fragment)},l(o){f(i.$$.fragment,o)},m(o,U){y(i,o,U),m=!0},p:Rt,i(o){m||(h(i.$$.fragment,o),m=!0)},o(o){w(i.$$.fragment,o),m=!1},d(o){J(i,o)}}}function Vl(Z){let i,m,o,U;return i=new Gt({props:{id:"training-inference",option:"single concept",$$slots:{default:[Wl]},$$scope:{ctx:Z}}}),o=new Gt({props:{id:"training-inference",option:"multiple concepts",$$slots:{default:[vl]},$$scope:{ctx:Z}}}),{c(){d(i.$$.fragment),m=a(),d(o.$$.fragment)},l(r){f(i.$$.fragment,r),m=n(r),f(o.$$.fragment,r)},m(r,T){y(i,r,T),s(r,m,T),y(o,r,T),U=!0},p(r,T){const b={};T&2&&(b.$$scope={dirty:T,ctx:r}),i.$set(b);const C={};T&2&&(C.$$scope={dirty:T,ctx:r}),o.$set(C)},i(r){U||(h(i.$$.fragment,r),h(o.$$.fragment,r),U=!0)},o(r){w(i.$$.fragment,r),w(o.$$.fragment,r),U=!1},d(r){r&&l(m),J(i,r),J(o,r)}}}function Nl(Z){let i,m,o,U,r,T,b,C,g,ve='<a href="https://huggingface.co/papers/2212.04488" rel="nofollow">Custom Diffusion</a> is a training technique for personalizing image generation models. Like Textual Inversion, DreamBooth, and LoRA, Custom Diffusion only requires a few (~4-5) example images. This technique works by only training weights in the cross-attention layers, and it uses a special word to represent the newly learned concept. Custom Diffusion is unique because it can also learn multiple concepts at the same time.',$,I,R="If you’re training on a GPU with limited vRAM, you should try enabling xFormers with <code>--enable_xformers_memory_efficient_attention</code> for faster training with lower vRAM requirements (16GB). To save even more memory, add <code>--set_grads_to_none</code> in the training argument to set the gradients to <code>None</code> instead of zero (this option can cause some issues, so if you experience any, try removing this parameter).",u,_,kt='This guide will explore the <a href="https://github.com/huggingface/diffusers/blob/main/examples/custom_diffusion/train_custom_diffusion.py" rel="nofollow">train_custom_diffusion.py</a> script to help you become more familiar with it, and how you can adapt it for your own use-case.',Ne,k,Bt="Before running the script, make sure you install the library from source:",Ge,B,Re,A,At="Navigate to the example folder with the training script and install the required dependencies:",ke,z,Be,X,zt='<p>🤗 Accelerate is a library for helping you train on multiple GPUs/TPUs or with mixed-precision. It’ll automatically configure your training setup based on your hardware and environment. Take a look at the 🤗 Accelerate <a href="https://huggingface.co/docs/accelerate/quicktour" rel="nofollow">Quick tour</a> to learn more.</p>',Ae,x,xt="Initialize an 🤗 Accelerate environment:",ze,E,xe,Y,Et="To setup a default 🤗 Accelerate environment without choosing any configurations:",Ee,Q,Ye,H,Yt="Or if your environment doesn’t support an interactive shell, like a notebook, you can use:",Qe,L,He,S,Qt='Lastly, if you want to train a model on your own dataset, take a look at the <a href="create_dataset">Create a dataset for training</a> guide to learn how to create a dataset that works with the training script.',Le,W,Ht='<p>The following sections highlight parts of the training script that are important for understanding how to modify it, but it doesn’t cover every aspect of the script in detail. If you’re interested in learning more, feel free to read through the <a href="https://github.com/huggingface/diffusers/blob/main/examples/custom_diffusion/train_custom_diffusion.py" rel="nofollow">script</a> and let us know if you have any questions or concerns.</p>',Se,F,Fe,q,Lt='The training script contains all the parameters to help you customize your training run. These are found in the <a href="https://github.com/huggingface/diffusers/blob/64603389da01082055a901f2883c4810d1144edb/examples/custom_diffusion/train_custom_diffusion.py#L319" rel="nofollow"><code>parse_args()</code></a> function. The function comes with default values, but you can also set your own values in the training command if you’d like.',qe,D,St="For example, to change the resolution of the input image:",De,P,Pe,K,Ft='Many of the basic parameters are described in the <a href="dreambooth#script-parameters">DreamBooth</a> training guide, so this guide focuses on the parameters unique to Custom Diffusion:',Ke,O,qt="<li><code>--freeze_model</code>: freezes the key and value parameters in the cross-attention layer; the default is <code>crossattn_kv</code>, but you can set it to <code>crossattn</code> to train all the parameters in the cross-attention layer</li> <li><code>--concepts_list</code>: to learn multiple concepts, provide a path to a JSON file containing the concepts</li> <li><code>--modifier_token</code>: a special word used to represent the learned concept</li> <li><code>--initializer_token</code>: a special word used to initialize the embeddings of the <code>modifier_token</code></li>",Oe,ee,et,te,Dt="Prior preservation loss is a method that uses a model’s own generated samples to help it learn how to generate more diverse images. Because these generated sample images belong to the same class as the images you provided, they help the model retain what it has learned about the class and how it can use what it already knows about the class to make new compositions.",tt,le,Pt='Many of the parameters for prior preservation loss are described in the <a href="dreambooth#prior-preservation-loss">DreamBooth</a> training guide.',lt,se,st,ae,Kt="Custom Diffusion includes training the target images with a small set of real images to prevent overfitting. As you can imagine, this can be easy to do when you’re only training on a few images! Download 200 real images with <code>clip_retrieval</code>. The <code>class_prompt</code> should be the same category as the target images. These images are stored in <code>class_data_dir</code>.",at,ne,nt,ie,Ot="To enable regularization, add the following parameters:",it,oe,el="<li><code>--with_prior_preservation</code>: whether to use prior preservation loss</li> <li><code>--prior_loss_weight</code>: controls the influence of the prior preservation loss on the model</li> <li><code>--real_prior</code>: whether to use a small set of real images to prevent overfitting</li>",ot,re,rt,pe,pt,v,tl='<p>A lot of the code in the Custom Diffusion training script is similar to the <a href="dreambooth#training-script">DreamBooth</a> script. This guide instead focuses on the code that is relevant to Custom Diffusion.</p>',ct,ce,ll="The Custom Diffusion training script has two dataset classes:",Mt,Me,sl='<li><a href="https://github.com/huggingface/diffusers/blob/64603389da01082055a901f2883c4810d1144edb/examples/custom_diffusion/train_custom_diffusion.py#L165" rel="nofollow"><code>CustomDiffusionDataset</code></a>: preprocesses the images, class images, and prompts for training</li> <li><a href="https://github.com/huggingface/diffusers/blob/64603389da01082055a901f2883c4810d1144edb/examples/custom_diffusion/train_custom_diffusion.py#L148" rel="nofollow"><code>PromptDataset</code></a>: prepares the prompts for generating class images</li>',ut,ue,al='Next, the <code>modifier_token</code> is <a href="https://github.com/huggingface/diffusers/blob/64603389da01082055a901f2883c4810d1144edb/examples/custom_diffusion/train_custom_diffusion.py#L811" rel="nofollow">added to the tokenizer</a>, converted to token ids, and the token embeddings are resized to account for the new <code>modifier_token</code>. Then the <code>modifier_token</code> embeddings are initialized with the embeddings of the <code>initializer_token</code>. All parameters in the text encoder are frozen, except for the token embeddings since this is what the model is trying to learn to associate with the concepts.',mt,me,dt,de,nl='Now you’ll need to add the <a href="https://github.com/huggingface/diffusers/blob/64603389da01082055a901f2883c4810d1144edb/examples/custom_diffusion/train_custom_diffusion.py#L911C3-L911C3" rel="nofollow">Custom Diffusion weights</a> to the attention layers. This is a really important step for getting the shape and size of the attention weights correct, and for setting the appropriate number of attention processors in each UNet block.',ft,fe,yt,ye,il='The <a href="https://github.com/huggingface/diffusers/blob/84cd9e8d01adb47f046b1ee449fc76a0c32dc4e2/examples/custom_diffusion/train_custom_diffusion.py#L982" rel="nofollow">optimizer</a> is initialized to update the cross-attention layer parameters:',ht,he,wt,we,ol='In the <a href="https://github.com/huggingface/diffusers/blob/84cd9e8d01adb47f046b1ee449fc76a0c32dc4e2/examples/custom_diffusion/train_custom_diffusion.py#L1048" rel="nofollow">training loop</a>, it is important to only update the embeddings for the concept you’re trying to learn. This means setting the gradients of all the other token embeddings to zero:',Jt,Je,Ut,Ue,Tt,Te,rl="Once you’ve made all your changes or you’re okay with the default configuration, you’re ready to launch the training script! 🚀",bt,be,pl='In this guide, you’ll download and use these example <a href="https://www.cs.cmu.edu/~custom-diffusion/assets/data.zip" rel="nofollow">cat images</a>. You can also create and use your own dataset if you want (see the <a href="create_dataset">Create a dataset for training</a> guide).',_t,_e,cl="Set the environment variable <code>MODEL_NAME</code> to a model id on the Hub or a path to a local model, <code>INSTANCE_DIR</code>  to the path where you just downloaded the cat images to, and <code>OUTPUT_DIR</code> to where you want to save the model. You’ll use <code>&lt;new1&gt;</code> as the special word to tie the newly learned embeddings to. The script creates and saves model checkpoints and a pytorch_custom_diffusion_weights.bin file to your repository.",jt,je,Ml="To monitor training progress with Weights and Biases, add the <code>--report_to=wandb</code> parameter to the training command and specify a validation prompt with <code>--validation_prompt</code>. This is useful for debugging and saving intermediate results.",Ct,V,ul="<p>If you’re training on human faces, the Custom Diffusion team has found the following parameters to work well:</p> <ul><li><code>--learning_rate=5e-6</code></li> <li><code>--max_train_steps</code> can be anywhere between 1000 and 2000</li> <li><code>--freeze_model=crossattn</code></li> <li>use at least 15-20 images to train with</li></ul>",gt,N,It,Ce,ml="Once training is finished, you can use your new Custom Diffusion model for inference.",Zt,G,$t,ge,Xt,Ie,dl="Congratulations on training a model with Custom Diffusion! 🎉 To learn more:",Wt,Ze,fl='<li>Read the <a href="https://www.cs.cmu.edu/~custom-diffusion/" rel="nofollow">Multi-Concept Customization of Text-to-Image Diffusion</a> blog post to learn more details about the experimental results from the Custom Diffusion team.</li>',vt,$e,Vt,Ve,Nt;return r=new gl({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),b=new Xe({props:{title:"Custom Diffusion",local:"custom-diffusion",headingTag:"h1"}}),B=new j({props:{code:"Z2l0JTIwY2xvbmUlMjBodHRwcyUzQSUyRiUyRmdpdGh1Yi5jb20lMkZodWdnaW5nZmFjZSUyRmRpZmZ1c2VycyUwQWNkJTIwZGlmZnVzZXJzJTBBcGlwJTIwaW5zdGFsbCUyMC4=",highlighted:`git <span class="hljs-built_in">clone</span> https://github.com/huggingface/diffusers
<span class="hljs-built_in">cd</span> diffusers
pip install .`,wrap:!1}}),z=new j({props:{code:"Y2QlMjBleGFtcGxlcyUyRmN1c3RvbV9kaWZmdXNpb24lMEFwaXAlMjBpbnN0YWxsJTIwLXIlMjByZXF1aXJlbWVudHMudHh0JTBBcGlwJTIwaW5zdGFsbCUyMGNsaXAtcmV0cmlldmFs",highlighted:`<span class="hljs-built_in">cd</span> examples/custom_diffusion
pip install -r requirements.txt
pip install clip-retrieval`,wrap:!1}}),E=new j({props:{code:"YWNjZWxlcmF0ZSUyMGNvbmZpZw==",highlighted:"accelerate config",wrap:!1}}),Q=new j({props:{code:"YWNjZWxlcmF0ZSUyMGNvbmZpZyUyMGRlZmF1bHQ=",highlighted:"accelerate config default",wrap:!1}}),L=new j({props:{code:"ZnJvbSUyMGFjY2VsZXJhdGUudXRpbHMlMjBpbXBvcnQlMjB3cml0ZV9iYXNpY19jb25maWclMEElMEF3cml0ZV9iYXNpY19jb25maWcoKQ==",highlighted:`<span class="hljs-keyword">from</span> accelerate.utils <span class="hljs-keyword">import</span> write_basic_config

write_basic_config()`,wrap:!1}}),F=new Xe({props:{title:"Script parameters",local:"script-parameters",headingTag:"h2"}}),P=new j({props:{code:"YWNjZWxlcmF0ZSUyMGxhdW5jaCUyMHRyYWluX2N1c3RvbV9kaWZmdXNpb24ucHklMjAlNUMlMEElMjAlMjAtLXJlc29sdXRpb24lM0QyNTY=",highlighted:`accelerate launch train_custom_diffusion.py \\
  --resolution=256`,wrap:!1}}),ee=new Xe({props:{title:"Prior preservation loss",local:"prior-preservation-loss",headingTag:"h3"}}),se=new Xe({props:{title:"Regularization",local:"regularization",headingTag:"h3"}}),ne=new j({props:{code:"cHl0aG9uJTIwcmV0cmlldmUucHklMjAtLWNsYXNzX3Byb21wdCUyMGNhdCUyMC0tY2xhc3NfZGF0YV9kaXIlMjByZWFsX3JlZyUyRnNhbXBsZXNfY2F0JTIwLS1udW1fY2xhc3NfaW1hZ2VzJTIwMjAw",highlighted:'python retrieve.py --class_prompt <span class="hljs-built_in">cat</span> --class_data_dir real_reg/samples_cat --num_class_images 200',wrap:!1}}),re=new j({props:{code:"YWNjZWxlcmF0ZSUyMGxhdW5jaCUyMHRyYWluX2N1c3RvbV9kaWZmdXNpb24ucHklMjAlNUMlMEElMjAlMjAtLXdpdGhfcHJpb3JfcHJlc2VydmF0aW9uJTIwJTVDJTBBJTIwJTIwLS1wcmlvcl9sb3NzX3dlaWdodCUzRDEuMCUyMCU1QyUwQSUyMCUyMC0tY2xhc3NfZGF0YV9kaXIlM0QlMjIuJTJGcmVhbF9yZWclMkZzYW1wbGVzX2NhdCUyMiUyMCU1QyUwQSUyMCUyMC0tY2xhc3NfcHJvbXB0JTNEJTIyY2F0JTIyJTIwJTVDJTBBJTIwJTIwLS1yZWFsX3ByaW9yJTNEVHJ1ZSUyMCU1Qw==",highlighted:`accelerate launch train_custom_diffusion.py \\
  --with_prior_preservation \\
  --prior_loss_weight=1.0 \\
  --class_data_dir=<span class="hljs-string">&quot;./real_reg/samples_cat&quot;</span> \\
  --class_prompt=<span class="hljs-string">&quot;cat&quot;</span> \\
  --real_prior=True \\`,wrap:!1}}),pe=new Xe({props:{title:"Training script",local:"training-script",headingTag:"h2"}}),me=new j({props:{code:"cGFyYW1zX3RvX2ZyZWV6ZSUyMCUzRCUyMGl0ZXJ0b29scy5jaGFpbiglMEElMjAlMjAlMjAlMjB0ZXh0X2VuY29kZXIudGV4dF9tb2RlbC5lbmNvZGVyLnBhcmFtZXRlcnMoKSUyQyUwQSUyMCUyMCUyMCUyMHRleHRfZW5jb2Rlci50ZXh0X21vZGVsLmZpbmFsX2xheWVyX25vcm0ucGFyYW1ldGVycygpJTJDJTBBJTIwJTIwJTIwJTIwdGV4dF9lbmNvZGVyLnRleHRfbW9kZWwuZW1iZWRkaW5ncy5wb3NpdGlvbl9lbWJlZGRpbmcucGFyYW1ldGVycygpJTJDJTBBKSUwQWZyZWV6ZV9wYXJhbXMocGFyYW1zX3RvX2ZyZWV6ZSk=",highlighted:`params_to_freeze = itertools.chain(
    text_encoder.text_model.encoder.parameters(),
    text_encoder.text_model.final_layer_norm.parameters(),
    text_encoder.text_model.embeddings.position_embedding.parameters(),
)
freeze_params(params_to_freeze)`,wrap:!1}}),fe=new j({props:{code:"c3QlMjAlM0QlMjB1bmV0LnN0YXRlX2RpY3QoKSUwQWZvciUyMG5hbWUlMkMlMjBfJTIwaW4lMjB1bmV0LmF0dG5fcHJvY2Vzc29ycy5pdGVtcygpJTNBJTBBJTIwJTIwJTIwJTIwY3Jvc3NfYXR0ZW50aW9uX2RpbSUyMCUzRCUyME5vbmUlMjBpZiUyMG5hbWUuZW5kc3dpdGgoJTIyYXR0bjEucHJvY2Vzc29yJTIyKSUyMGVsc2UlMjB1bmV0LmNvbmZpZy5jcm9zc19hdHRlbnRpb25fZGltJTBBJTIwJTIwJTIwJTIwaWYlMjBuYW1lLnN0YXJ0c3dpdGgoJTIybWlkX2Jsb2NrJTIyKSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGhpZGRlbl9zaXplJTIwJTNEJTIwdW5ldC5jb25maWcuYmxvY2tfb3V0X2NoYW5uZWxzJTVCLTElNUQlMEElMjAlMjAlMjAlMjBlbGlmJTIwbmFtZS5zdGFydHN3aXRoKCUyMnVwX2Jsb2NrcyUyMiklM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlM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rjcm9zc19hdHRlbnRpb25fZGltJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwKS50byh1bmV0LmRldmljZSklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBjdXN0b21fZGlmZnVzaW9uX2F0dG5fcHJvY3MlNUJuYW1lJTVELmxvYWRfc3RhdGVfZGljdCh3ZWlnaHRzKSUwQSUyMCUyMCUyMCUyMGVsc2UlM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBjdXN0b21fZGlmZnVzaW9uX2F0dG5fcHJvY3MlNUJuYW1lJTVEJTIwJTNEJTIwYXR0ZW50aW9uX2NsYXNzKCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHRyYWluX2t2JTNERmFsc2UlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjB0cmFpbl9xX291dCUzREZhbHNlJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwaGlkZGVuX3NpemUlM0RoaWRkZW5fc2l6ZSUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGNyb3NzX2F0dGVudGlvbl9kaW0lM0Rjcm9zc19hdHRlbnRpb25fZGltJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwKSUwQWRlbCUyMHN0JTBBdW5ldC5zZXRfYXR0bl9wcm9jZXNzb3IoY3VzdG9tX2RpZmZ1c2lvbl9hdHRuX3Byb2NzKSUwQWN1c3RvbV9kaWZmdXNpb25fbGF5ZXJzJTIwJTNEJTIwQXR0blByb2NzTGF5ZXJzKHVuZXQuYXR0bl9wcm9jZXNzb3JzKQ==",highlighted:`st = unet.state_dict()
<span class="hljs-keyword">for</span> name, _ <span class="hljs-keyword">in</span> unet.attn_processors.items():
    cross_attention_dim = <span class="hljs-literal">None</span> <span class="hljs-keyword">if</span> name.endswith(<span class="hljs-string">&quot;attn1.processor&quot;</span>) <span class="hljs-keyword">else</span> unet.config.cross_attention_dim
    <span class="hljs-keyword">if</span> name.startswith(<span class="hljs-string">&quot;mid_block&quot;</span>):
        hidden_size = unet.config.block_out_channels[-<span class="hljs-number">1</span>]
    <span class="hljs-keyword">elif</span> name.startswith(<span class="hljs-string">&quot;up_blocks&quot;</span>):
        block_id = <span class="hljs-built_in">int</span>(name[<span class="hljs-built_in">len</span>(<span class="hljs-string">&quot;up_blocks.&quot;</span>)])
        hidden_size = <span class="hljs-built_in">list</span>(<span class="hljs-built_in">reversed</span>(unet.config.block_out_channels))[block_id]
    <span class="hljs-keyword">elif</span> name.startswith(<span class="hljs-string">&quot;down_blocks&quot;</span>):
        block_id = <span class="hljs-built_in">int</span>(name[<span class="hljs-built_in">len</span>(<span class="hljs-string">&quot;down_blocks.&quot;</span>)])
        hidden_size = unet.config.block_out_channels[block_id]
    layer_name = name.split(<span class="hljs-string">&quot;.processor&quot;</span>)[<span class="hljs-number">0</span>]
    weights = {
        <span class="hljs-string">&quot;to_k_custom_diffusion.weight&quot;</span>: st[layer_name + <span class="hljs-string">&quot;.to_k.weight&quot;</span>],
        <span class="hljs-string">&quot;to_v_custom_diffusion.weight&quot;</span>: st[layer_name + <span class="hljs-string">&quot;.to_v.weight&quot;</span>],
    }
    <span class="hljs-keyword">if</span> train_q_out:
        weights[<span class="hljs-string">&quot;to_q_custom_diffusion.weight&quot;</span>] = st[layer_name + <span class="hljs-string">&quot;.to_q.weight&quot;</span>]
        weights[<span class="hljs-string">&quot;to_out_custom_diffusion.0.weight&quot;</span>] = st[layer_name + <span class="hljs-string">&quot;.to_out.0.weight&quot;</span>]
        weights[<span class="hljs-string">&quot;to_out_custom_diffusion.0.bias&quot;</span>] = st[layer_name + <span class="hljs-string">&quot;.to_out.0.bias&quot;</span>]
    <span class="hljs-keyword">if</span> cross_attention_dim <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>:
        custom_diffusion_attn_procs[name] = attention_class(
            train_kv=train_kv,
            train_q_out=train_q_out,
            hidden_size=hidden_size,
            cross_attention_dim=cross_attention_dim,
        ).to(unet.device)
        custom_diffusion_attn_procs[name].load_state_dict(weights)
    <span class="hljs-keyword">else</span>:
        custom_diffusion_attn_procs[name] = attention_class(
            train_kv=<span class="hljs-literal">False</span>,
            train_q_out=<span class="hljs-literal">False</span>,
            hidden_size=hidden_size,
            cross_attention_dim=cross_attention_dim,
        )
<span class="hljs-keyword">del</span> st
unet.set_attn_processor(custom_diffusion_attn_procs)
custom_diffusion_layers = AttnProcsLayers(unet.attn_processors)`,wrap:!1}}),he=new j({props:{code:"b3B0aW1pemVyJTIwJTNEJTIwb3B0aW1pemVyX2NsYXNzKCUwQSUyMCUyMCUyMCUyMGl0ZXJ0b29scy5jaGFpbih0ZXh0X2VuY29kZXIuZ2V0X2lucHV0X2VtYmVkZGluZ3MoKS5wYXJhbWV0ZXJzKCklMkMlMjBjdXN0b21fZGlmZnVzaW9uX2xheWVycy5wYXJhbWV0ZXJzKCkpJTBBJTIwJTIwJTIwJTIwaWYlMjBhcmdzLm1vZGlmaWVyX3Rva2VuJTIwaXMlMjBub3QlMjBOb25lJTBBJTIwJTIwJTIwJTIwZWxzZSUyMGN1c3RvbV9kaWZmdXNpb25fbGF5ZXJzLnBhcmFtZXRlcnMoKSUyQyUwQSUyMCUyMCUyMCUyMGxyJTNEYXJncy5sZWFybmluZ19yYXRlJTJDJTBBJTIwJTIwJTIwJTIwYmV0YXMlM0QoYXJncy5hZGFtX2JldGExJTJDJTIwYXJncy5hZGFtX2JldGEyKSUyQyUwQSUyMCUyMCUyMCUyMHdlaWdodF9kZWNheSUzRGFyZ3MuYWRhbV93ZWlnaHRfZGVjYXklMkMlMEElMjAlMjAlMjAlMjBlcHMlM0RhcmdzLmFkYW1fZXBzaWxvbiUyQyUwQSk=",highlighted:`optimizer = optimizer_class(
    itertools.chain(text_encoder.get_input_embeddings().parameters(), custom_diffusion_layers.parameters())
    <span class="hljs-keyword">if</span> args.modifier_token <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>
    <span class="hljs-keyword">else</span> custom_diffusion_layers.parameters(),
    lr=args.learning_rate,
    betas=(args.adam_beta1, args.adam_beta2),
    weight_decay=args.adam_weight_decay,
    eps=args.adam_epsilon,
)`,wrap:!1}}),Je=new j({props:{code:"aWYlMjBhcmdzLm1vZGlmaWVyX3Rva2VuJTIwaXMlMjBub3QlMjBOb25lJTNBJTBBJTIwJTIwJTIwJTIwaWYlMjBhY2NlbGVyYXRvci5udW1fcHJvY2Vzc2VzJTIwJTNFJTIwMSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGdyYWRzX3RleHRfZW5jb2RlciUyMCUzRCUyMHRleHRfZW5jb2Rlci5tb2R1bGUuZ2V0X2lucHV0X2VtYmVkZGluZ3MoKS53ZWlnaHQuZ3JhZCUwQSUyMCUyMCUyMCUyMGVsc2UlM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBncmFkc190ZXh0X2VuY29kZXIlMjAlM0QlMjB0ZXh0X2VuY29kZXIuZ2V0X2lucHV0X2VtYmVkZGluZ3MoKS53ZWlnaHQuZ3JhZCUwQSUyMCUyMCUyMCUyMGluZGV4X2dyYWRzX3RvX3plcm8lMjAlM0QlMjB0b3JjaC5hcmFuZ2UobGVuKHRva2VuaXplcikpJTIwISUzRCUyMG1vZGlmaWVyX3Rva2VuX2lkJTVCMCU1RCUwQSUyMCUyMCUyMCUyMGZvciUyMGklMjBpbiUyMHJhbmdlKGxlbihtb2RpZmllcl90b2tlbl9pZCU1QjElM0ElNUQpKSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGluZGV4X2dyYWRzX3RvX3plcm8lMjAlM0QlMjBpbmRleF9ncmFkc190b196ZXJvJTIwJTI2JTIwKCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHRvcmNoLmFyYW5nZShsZW4odG9rZW5pemVyKSklMjAhJTNEJTIwbW9kaWZpZXJfdG9rZW5faWQlNUJpJTVEJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwKSUwQSUyMCUyMCUyMCUyMGdyYWRzX3RleHRfZW5jb2Rlci5kYXRhJTVCaW5kZXhfZ3JhZHNfdG9femVybyUyQyUyMCUzQSU1RCUyMCUzRCUyMGdyYWRzX3RleHRfZW5jb2Rlci5kYXRhJTVCJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwaW5kZXhfZ3JhZHNfdG9femVybyUyQyUyMCUzQSUwQSUyMCUyMCUyMCUyMCU1RC5maWxsXygwKQ==",highlighted:`<span class="hljs-keyword">if</span> args.modifier_token <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>:
    <span class="hljs-keyword">if</span> accelerator.num_processes &gt; <span class="hljs-number">1</span>:
        grads_text_encoder = text_encoder.module.get_input_embeddings().weight.grad
    <span class="hljs-keyword">else</span>:
        grads_text_encoder = text_encoder.get_input_embeddings().weight.grad
    index_grads_to_zero = torch.arange(<span class="hljs-built_in">len</span>(tokenizer)) != modifier_token_id[<span class="hljs-number">0</span>]
    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(modifier_token_id[<span class="hljs-number">1</span>:])):
        index_grads_to_zero = index_grads_to_zero &amp; (
            torch.arange(<span class="hljs-built_in">len</span>(tokenizer)) != modifier_token_id[i]
        )
    grads_text_encoder.data[index_grads_to_zero, :] = grads_text_encoder.data[
        index_grads_to_zero, :
    ].fill_(<span class="hljs-number">0</span>)`,wrap:!1}}),Ue=new Xe({props:{title:"Launch the script",local:"launch-the-script",headingTag:"h2"}}),N=new Jl({props:{id:"training-inference",options:["single concept","multiple concepts"],$$slots:{default:[Xl]},$$scope:{ctx:Z}}}),G=new Jl({props:{id:"training-inference",options:["single concept","multiple concepts"],$$slots:{default:[Vl]},$$scope:{ctx:Z}}}),ge=new Xe({props:{title:"Next steps",local:"next-steps",headingTag:"h2"}}),$e=new Il({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/en/training/custom_diffusion.md"}}),{c(){i=p("meta"),m=a(),o=p("p"),U=a(),d(r.$$.fragment),T=a(),d(b.$$.fragment),C=a(),g=p("p"),g.innerHTML=ve,$=a(),I=p("p"),I.innerHTML=R,u=a(),_=p("p"),_.innerHTML=kt,Ne=a(),k=p("p"),k.textContent=Bt,Ge=a(),d(B.$$.fragment),Re=a(),A=p("p"),A.textContent=At,ke=a(),d(z.$$.fragment),Be=a(),X=p("blockquote"),X.innerHTML=zt,Ae=a(),x=p("p"),x.textContent=xt,ze=a(),d(E.$$.fragment),xe=a(),Y=p("p"),Y.textContent=Et,Ee=a(),d(Q.$$.fragment),Ye=a(),H=p("p"),H.textContent=Yt,Qe=a(),d(L.$$.fragment),He=a(),S=p("p"),S.innerHTML=Qt,Le=a(),W=p("blockquote"),W.innerHTML=Ht,Se=a(),d(F.$$.fragment),Fe=a(),q=p("p"),q.innerHTML=Lt,qe=a(),D=p("p"),D.textContent=St,De=a(),d(P.$$.fragment),Pe=a(),K=p("p"),K.innerHTML=Ft,Ke=a(),O=p("ul"),O.innerHTML=qt,Oe=a(),d(ee.$$.fragment),et=a(),te=p("p"),te.textContent=Dt,tt=a(),le=p("p"),le.innerHTML=Pt,lt=a(),d(se.$$.fragment),st=a(),ae=p("p"),ae.innerHTML=Kt,at=a(),d(ne.$$.fragment),nt=a(),ie=p("p"),ie.textContent=Ot,it=a(),oe=p("ul"),oe.innerHTML=el,ot=a(),d(re.$$.fragment),rt=a(),d(pe.$$.fragment),pt=a(),v=p("blockquote"),v.innerHTML=tl,ct=a(),ce=p("p"),ce.textContent=ll,Mt=a(),Me=p("ul"),Me.innerHTML=sl,ut=a(),ue=p("p"),ue.innerHTML=al,mt=a(),d(me.$$.fragment),dt=a(),de=p("p"),de.innerHTML=nl,ft=a(),d(fe.$$.fragment),yt=a(),ye=p("p"),ye.innerHTML=il,ht=a(),d(he.$$.fragment),wt=a(),we=p("p"),we.innerHTML=ol,Jt=a(),d(Je.$$.fragment),Ut=a(),d(Ue.$$.fragment),Tt=a(),Te=p("p"),Te.textContent=rl,bt=a(),be=p("p"),be.innerHTML=pl,_t=a(),_e=p("p"),_e.innerHTML=cl,jt=a(),je=p("p"),je.innerHTML=Ml,Ct=a(),V=p("blockquote"),V.innerHTML=ul,gt=a(),d(N.$$.fragment),It=a(),Ce=p("p"),Ce.textContent=ml,Zt=a(),d(G.$$.fragment),$t=a(),d(ge.$$.fragment),Xt=a(),Ie=p("p"),Ie.textContent=dl,Wt=a(),Ze=p("ul"),Ze.innerHTML=fl,vt=a(),d($e.$$.fragment),Vt=a(),Ve=p("p"),this.h()},l(e){const t=jl("svelte-u9bgzb",document.head);i=c(t,"META",{name:!0,content:!0}),t.forEach(l),m=n(e),o=c(e,"P",{}),wl(o).forEach(l),U=n(e),f(r.$$.fragment,e),T=n(e),f(b.$$.fragment,e),C=n(e),g=c(e,"P",{"data-svelte-h":!0}),M(g)!=="svelte-zoobzt"&&(g.innerHTML=ve),$=n(e),I=c(e,"P",{"data-svelte-h":!0}),M(I)!=="svelte-1pc1nnz"&&(I.innerHTML=R),u=n(e),_=c(e,"P",{"data-svelte-h":!0}),M(_)!=="svelte-u5h0jz"&&(_.innerHTML=kt),Ne=n(e),k=c(e,"P",{"data-svelte-h":!0}),M(k)!=="svelte-l7dm2q"&&(k.textContent=Bt),Ge=n(e),f(B.$$.fragment,e),Re=n(e),A=c(e,"P",{"data-svelte-h":!0}),M(A)!=="svelte-1nnn36i"&&(A.textContent=At),ke=n(e),f(z.$$.fragment,e),Be=n(e),X=c(e,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),M(X)!=="svelte-1in84qc"&&(X.innerHTML=zt),Ae=n(e),x=c(e,"P",{"data-svelte-h":!0}),M(x)!=="svelte-60q53m"&&(x.textContent=xt),ze=n(e),f(E.$$.fragment,e),xe=n(e),Y=c(e,"P",{"data-svelte-h":!0}),M(Y)!=="svelte-di6juu"&&(Y.textContent=Et),Ee=n(e),f(Q.$$.fragment,e),Ye=n(e),H=c(e,"P",{"data-svelte-h":!0}),M(H)!=="svelte-tsz4qp"&&(H.textContent=Yt),Qe=n(e),f(L.$$.fragment,e),He=n(e),S=c(e,"P",{"data-svelte-h":!0}),M(S)!=="svelte-1fkfdql"&&(S.innerHTML=Qt),Le=n(e),W=c(e,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),M(W)!=="svelte-8wry21"&&(W.innerHTML=Ht),Se=n(e),f(F.$$.fragment,e),Fe=n(e),q=c(e,"P",{"data-svelte-h":!0}),M(q)!=="svelte-1t7lm99"&&(q.innerHTML=Lt),qe=n(e),D=c(e,"P",{"data-svelte-h":!0}),M(D)!=="svelte-or9ig"&&(D.textContent=St),De=n(e),f(P.$$.fragment,e),Pe=n(e),K=c(e,"P",{"data-svelte-h":!0}),M(K)!=="svelte-j5tc3k"&&(K.innerHTML=Ft),Ke=n(e),O=c(e,"UL",{"data-svelte-h":!0}),M(O)!=="svelte-11ngzwo"&&(O.innerHTML=qt),Oe=n(e),f(ee.$$.fragment,e),et=n(e),te=c(e,"P",{"data-svelte-h":!0}),M(te)!=="svelte-1oc83h6"&&(te.textContent=Dt),tt=n(e),le=c(e,"P",{"data-svelte-h":!0}),M(le)!=="svelte-1mpzp5"&&(le.innerHTML=Pt),lt=n(e),f(se.$$.fragment,e),st=n(e),ae=c(e,"P",{"data-svelte-h":!0}),M(ae)!=="svelte-13fgxpn"&&(ae.innerHTML=Kt),at=n(e),f(ne.$$.fragment,e),nt=n(e),ie=c(e,"P",{"data-svelte-h":!0}),M(ie)!=="svelte-hwsu6x"&&(ie.textContent=Ot),it=n(e),oe=c(e,"UL",{"data-svelte-h":!0}),M(oe)!=="svelte-6jy6u"&&(oe.innerHTML=el),ot=n(e),f(re.$$.fragment,e),rt=n(e),f(pe.$$.fragment,e),pt=n(e),v=c(e,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),M(v)!=="svelte-10u1fcf"&&(v.innerHTML=tl),ct=n(e),ce=c(e,"P",{"data-svelte-h":!0}),M(ce)!=="svelte-1owi8ao"&&(ce.textContent=ll),Mt=n(e),Me=c(e,"UL",{"data-svelte-h":!0}),M(Me)!=="svelte-1fby5zy"&&(Me.innerHTML=sl),ut=n(e),ue=c(e,"P",{"data-svelte-h":!0}),M(ue)!=="svelte-1jto000"&&(ue.innerHTML=al),mt=n(e),f(me.$$.fragment,e),dt=n(e),de=c(e,"P",{"data-svelte-h":!0}),M(de)!=="svelte-12higtg"&&(de.innerHTML=nl),ft=n(e),f(fe.$$.fragment,e),yt=n(e),ye=c(e,"P",{"data-svelte-h":!0}),M(ye)!=="svelte-bhf9v4"&&(ye.innerHTML=il),ht=n(e),f(he.$$.fragment,e),wt=n(e),we=c(e,"P",{"data-svelte-h":!0}),M(we)!=="svelte-dnomct"&&(we.innerHTML=ol),Jt=n(e),f(Je.$$.fragment,e),Ut=n(e),f(Ue.$$.fragment,e),Tt=n(e),Te=c(e,"P",{"data-svelte-h":!0}),M(Te)!=="svelte-9dei1q"&&(Te.textContent=rl),bt=n(e),be=c(e,"P",{"data-svelte-h":!0}),M(be)!=="svelte-mn6swm"&&(be.innerHTML=pl),_t=n(e),_e=c(e,"P",{"data-svelte-h":!0}),M(_e)!=="svelte-1o5n4y1"&&(_e.innerHTML=cl),jt=n(e),je=c(e,"P",{"data-svelte-h":!0}),M(je)!=="svelte-gvoz0n"&&(je.innerHTML=Ml),Ct=n(e),V=c(e,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),M(V)!=="svelte-33lll3"&&(V.innerHTML=ul),gt=n(e),f(N.$$.fragment,e),It=n(e),Ce=c(e,"P",{"data-svelte-h":!0}),M(Ce)!=="svelte-1mw1g2x"&&(Ce.textContent=ml),Zt=n(e),f(G.$$.fragment,e),$t=n(e),f(ge.$$.fragment,e),Xt=n(e),Ie=c(e,"P",{"data-svelte-h":!0}),M(Ie)!=="svelte-nq8p2g"&&(Ie.textContent=dl),Wt=n(e),Ze=c(e,"UL",{"data-svelte-h":!0}),M(Ze)!=="svelte-1u8bbml"&&(Ze.innerHTML=fl),vt=n(e),f($e.$$.fragment,e),Vt=n(e),Ve=c(e,"P",{}),wl(Ve).forEach(l),this.h()},h(){We(i,"name","hf:doc:metadata"),We(i,"content",Gl),We(X,"class","tip"),We(W,"class","tip"),We(v,"class","tip"),We(V,"class","tip")},m(e,t){Cl(document.head,i),s(e,m,t),s(e,o,t),s(e,U,t),y(r,e,t),s(e,T,t),y(b,e,t),s(e,C,t),s(e,g,t),s(e,$,t),s(e,I,t),s(e,u,t),s(e,_,t),s(e,Ne,t),s(e,k,t),s(e,Ge,t),y(B,e,t),s(e,Re,t),s(e,A,t),s(e,ke,t),y(z,e,t),s(e,Be,t),s(e,X,t),s(e,Ae,t),s(e,x,t),s(e,ze,t),y(E,e,t),s(e,xe,t),s(e,Y,t),s(e,Ee,t),y(Q,e,t),s(e,Ye,t),s(e,H,t),s(e,Qe,t),y(L,e,t),s(e,He,t),s(e,S,t),s(e,Le,t),s(e,W,t),s(e,Se,t),y(F,e,t),s(e,Fe,t),s(e,q,t),s(e,qe,t),s(e,D,t),s(e,De,t),y(P,e,t),s(e,Pe,t),s(e,K,t),s(e,Ke,t),s(e,O,t),s(e,Oe,t),y(ee,e,t),s(e,et,t),s(e,te,t),s(e,tt,t),s(e,le,t),s(e,lt,t),y(se,e,t),s(e,st,t),s(e,ae,t),s(e,at,t),y(ne,e,t),s(e,nt,t),s(e,ie,t),s(e,it,t),s(e,oe,t),s(e,ot,t),y(re,e,t),s(e,rt,t),y(pe,e,t),s(e,pt,t),s(e,v,t),s(e,ct,t),s(e,ce,t),s(e,Mt,t),s(e,Me,t),s(e,ut,t),s(e,ue,t),s(e,mt,t),y(me,e,t),s(e,dt,t),s(e,de,t),s(e,ft,t),y(fe,e,t),s(e,yt,t),s(e,ye,t),s(e,ht,t),y(he,e,t),s(e,wt,t),s(e,we,t),s(e,Jt,t),y(Je,e,t),s(e,Ut,t),y(Ue,e,t),s(e,Tt,t),s(e,Te,t),s(e,bt,t),s(e,be,t),s(e,_t,t),s(e,_e,t),s(e,jt,t),s(e,je,t),s(e,Ct,t),s(e,V,t),s(e,gt,t),y(N,e,t),s(e,It,t),s(e,Ce,t),s(e,Zt,t),y(G,e,t),s(e,$t,t),y(ge,e,t),s(e,Xt,t),s(e,Ie,t),s(e,Wt,t),s(e,Ze,t),s(e,vt,t),y($e,e,t),s(e,Vt,t),s(e,Ve,t),Nt=!0},p(e,[t]){const yl={};t&2&&(yl.$$scope={dirty:t,ctx:e}),N.$set(yl);const hl={};t&2&&(hl.$$scope={dirty:t,ctx:e}),G.$set(hl)},i(e){Nt||(h(r.$$.fragment,e),h(b.$$.fragment,e),h(B.$$.fragment,e),h(z.$$.fragment,e),h(E.$$.fragment,e),h(Q.$$.fragment,e),h(L.$$.fragment,e),h(F.$$.fragment,e),h(P.$$.fragment,e),h(ee.$$.fragment,e),h(se.$$.fragment,e),h(ne.$$.fragment,e),h(re.$$.fragment,e),h(pe.$$.fragment,e),h(me.$$.fragment,e),h(fe.$$.fragment,e),h(he.$$.fragment,e),h(Je.$$.fragment,e),h(Ue.$$.fragment,e),h(N.$$.fragment,e),h(G.$$.fragment,e),h(ge.$$.fragment,e),h($e.$$.fragment,e),Nt=!0)},o(e){w(r.$$.fragment,e),w(b.$$.fragment,e),w(B.$$.fragment,e),w(z.$$.fragment,e),w(E.$$.fragment,e),w(Q.$$.fragment,e),w(L.$$.fragment,e),w(F.$$.fragment,e),w(P.$$.fragment,e),w(ee.$$.fragment,e),w(se.$$.fragment,e),w(ne.$$.fragment,e),w(re.$$.fragment,e),w(pe.$$.fragment,e),w(me.$$.fragment,e),w(fe.$$.fragment,e),w(he.$$.fragment,e),w(Je.$$.fragment,e),w(Ue.$$.fragment,e),w(N.$$.fragment,e),w(G.$$.fragment,e),w(ge.$$.fragment,e),w($e.$$.fragment,e),Nt=!1},d(e){e&&(l(m),l(o),l(U),l(T),l(C),l(g),l($),l(I),l(u),l(_),l(Ne),l(k),l(Ge),l(Re),l(A),l(ke),l(Be),l(X),l(Ae),l(x),l(ze),l(xe),l(Y),l(Ee),l(Ye),l(H),l(Qe),l(He),l(S),l(Le),l(W),l(Se),l(Fe),l(q),l(qe),l(D),l(De),l(Pe),l(K),l(Ke),l(O),l(Oe),l(et),l(te),l(tt),l(le),l(lt),l(st),l(ae),l(at),l(nt),l(ie),l(it),l(oe),l(ot),l(rt),l(pt),l(v),l(ct),l(ce),l(Mt),l(Me),l(ut),l(ue),l(mt),l(dt),l(de),l(ft),l(yt),l(ye),l(ht),l(wt),l(we),l(Jt),l(Ut),l(Tt),l(Te),l(bt),l(be),l(_t),l(_e),l(jt),l(je),l(Ct),l(V),l(gt),l(It),l(Ce),l(Zt),l($t),l(Xt),l(Ie),l(Wt),l(Ze),l(vt),l(Vt),l(Ve)),l(i),J(r,e),J(b,e),J(B,e),J(z,e),J(E,e),J(Q,e),J(L,e),J(F,e),J(P,e),J(ee,e),J(se,e),J(ne,e),J(re,e),J(pe,e),J(me,e),J(fe,e),J(he,e),J(Je,e),J(Ue,e),J(N,e),J(G,e),J(ge,e),J($e,e)}}}const Gl='{"title":"Custom Diffusion","local":"custom-diffusion","sections":[{"title":"Script parameters","local":"script-parameters","sections":[{"title":"Prior preservation loss","local":"prior-preservation-loss","sections":[],"depth":3},{"title":"Regularization","local":"regularization","sections":[],"depth":3}],"depth":2},{"title":"Training script","local":"training-script","sections":[],"depth":2},{"title":"Launch the script","local":"launch-the-script","sections":[],"depth":2},{"title":"Next steps","local":"next-steps","sections":[],"depth":2}],"depth":1}';function Rl(Z){return Tl(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Yl extends bl{constructor(i){super(),_l(this,i,Rl,Nl,Ul,{})}}export{Yl as component};
