import{s as ke,o as Ie,n as oe}from"../chunks/scheduler.e4ff9b64.js";import{S as He,i as Ne,e as m,s as o,c as _,h as Ae,a as u,d as l,b as r,f as Be,g as y,j as c,k as ct,l as Se,m as a,n as $,t as U,o as b,p as d}from"../chunks/index.09f1bca0.js";import{C as Ye,H as yt,E as qe}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.f34641a2.js";import{C as w}from"../chunks/CodeBlock.9a546815.js";import{H as Qe,a as pe}from"../chunks/HfOption.44827c7f.js";function ze(g){let s,M;return s=new w({props:{code:"Y2QlMjBleGFtcGxlcyUyRnRleHRfdG9faW1hZ2UlMEFwaXAlMjBpbnN0YWxsJTIwLXIlMjByZXF1aXJlbWVudHMudHh0",highlighted:`<span class="hljs-built_in">cd</span> examples/text_to_image
pip install -r requirements.txt`,wrap:!1}}),{c(){_(s.$$.fragment)},l(i){y(s.$$.fragment,i)},m(i,f){$(s,i,f),M=!0},p:oe,i(i){M||(U(s.$$.fragment,i),M=!0)},o(i){b(s.$$.fragment,i),M=!1},d(i){d(s,i)}}}function Pe(g){let s,M;return s=new w({props:{code:"Y2QlMjBleGFtcGxlcyUyRnRleHRfdG9faW1hZ2UlMEFwaXAlMjBpbnN0YWxsJTIwLXIlMjByZXF1aXJlbWVudHNfZmxheC50eHQ=",highlighted:`<span class="hljs-built_in">cd</span> examples/text_to_image
pip install -r requirements_flax.txt`,wrap:!1}}),{c(){_(s.$$.fragment)},l(i){y(s.$$.fragment,i)},m(i,f){$(s,i,f),M=!0},p:oe,i(i){M||(U(s.$$.fragment,i),M=!0)},o(i){b(s.$$.fragment,i),M=!1},d(i){d(s,i)}}}function De(g){let s,M,i,f;return s=new pe({props:{id:"installation",option:"PyTorch",$$slots:{default:[ze]},$$scope:{ctx:g}}}),i=new pe({props:{id:"installation",option:"Flax",$$slots:{default:[Pe]},$$scope:{ctx:g}}}),{c(){_(s.$$.fragment),M=o(),_(i.$$.fragment)},l(n){y(s.$$.fragment,n),M=r(n),y(i.$$.fragment,n)},m(n,p){$(s,n,p),a(n,M,p),$(i,n,p),f=!0},p(n,p){const h={};p&2&&(h.$$scope={dirty:p,ctx:n}),s.$set(h);const T={};p&2&&(T.$$scope={dirty:p,ctx:n}),i.$set(T)},i(n){f||(U(s.$$.fragment,n),U(i.$$.fragment,n),f=!0)},o(n){b(s.$$.fragment,n),b(i.$$.fragment,n),f=!1},d(n){n&&l(M),d(s,n),d(i,n)}}}function Oe(g){let s,M='Diffusers使用<a href="https://hf.co/docs/peft" rel="nofollow">PEFT</a>库的<code>LoraConfig</code>配置LoRA适配器参数，包括秩(rank)、alpha值以及目标模块。适配器被注入UNet后，通过<code>lora_layers</code>筛选出需要优化的LoRA层。',i,f,n;return f=new w({props:{code:"dW5ldF9sb3JhX2NvbmZpZyUyMCUzRCUyMExvcmFDb25maWcoJTBBJTIwJTIwJTIwJTIwciUzRGFyZ3MucmFuayUyQyUwQSUyMCUyMCUyMCUyMGxvcmFfYWxwaGElM0RhcmdzLnJhbmslMkMlMEElMjAlMjAlMjAlMjBpbml0X2xvcmFfd2VpZ2h0cyUzRCUyMmdhdXNzaWFuJTIyJTJDJTBBJTIwJTIwJTIwJTIwdGFyZ2V0X21vZHVsZXMlM0QlNUIlMjJ0b19rJTIyJTJDJTIwJTIydG9fcSUyMiUyQyUyMCUyMnRvX3YlMjIlMkMlMjAlMjJ0b19vdXQuMCUyMiU1RCUyQyUwQSklMEElMEF1bmV0LmFkZF9hZGFwdGVyKHVuZXRfbG9yYV9jb25maWcpJTBBbG9yYV9sYXllcnMlMjAlM0QlMjBmaWx0ZXIobGFtYmRhJTIwcCUzQSUyMHAucmVxdWlyZXNfZ3JhZCUyQyUyMHVuZXQucGFyYW1ldGVycygpKQ==",highlighted:`unet_lora_config = LoraConfig(
    r=args.rank,
    lora_alpha=args.rank,
    init_lora_weights=<span class="hljs-string">&quot;gaussian&quot;</span>,
    target_modules=[<span class="hljs-string">&quot;to_k&quot;</span>, <span class="hljs-string">&quot;to_q&quot;</span>, <span class="hljs-string">&quot;to_v&quot;</span>, <span class="hljs-string">&quot;to_out.0&quot;</span>],
)

unet.add_adapter(unet_lora_config)
lora_layers = <span class="hljs-built_in">filter</span>(<span class="hljs-keyword">lambda</span> p: p.requires_grad, unet.parameters())`,wrap:!1}}),{c(){s=m("p"),s.innerHTML=M,i=o(),_(f.$$.fragment)},l(p){s=u(p,"P",{"data-svelte-h":!0}),c(s)!=="svelte-3z58xa"&&(s.innerHTML=M),i=r(p),y(f.$$.fragment,p)},m(p,h){a(p,s,h),a(p,i,h),$(f,p,h),n=!0},p:oe,i(p){n||(U(f.$$.fragment,p),n=!0)},o(p){b(f.$$.fragment,p),n=!1},d(p){p&&(l(s),l(i)),d(f,p)}}}function Ke(g){let s,M='当需要微调文本编码器时（如SDXL模型），Diffusers同样支持通过<a href="https://hf.co/docs/peft" rel="nofollow">PEFT</a>库实现。<code>LoraConfig</code>配置适配器参数后注入文本编码器，并筛选LoRA层进行训练。',i,f,n;return f=new w({props:{code:"dGV4dF9sb3JhX2NvbmZpZyUyMCUzRCUyMExvcmFDb25maWcoJTBBJTIwJTIwJTIwJTIwciUzRGFyZ3MucmFuayUyQyUwQSUyMCUyMCUyMCUyMGxvcmFfYWxwaGElM0RhcmdzLnJhbmslMkMlMEElMjAlMjAlMjAlMjBpbml0X2xvcmFfd2VpZ2h0cyUzRCUyMmdhdXNzaWFuJTIyJTJDJTBBJTIwJTIwJTIwJTIwdGFyZ2V0X21vZHVsZXMlM0QlNUIlMjJxX3Byb2olMjIlMkMlMjAlMjJrX3Byb2olMjIlMkMlMjAlMjJ2X3Byb2olMjIlMkMlMjAlMjJvdXRfcHJvaiUyMiU1RCUyQyUwQSklMEElMEF0ZXh0X2VuY29kZXJfb25lLmFkZF9hZGFwdGVyKHRleHRfbG9yYV9jb25maWcpJTBBdGV4dF9lbmNvZGVyX3R3by5hZGRfYWRhcHRlcih0ZXh0X2xvcmFfY29uZmlnKSUwQXRleHRfbG9yYV9wYXJhbWV0ZXJzX29uZSUyMCUzRCUyMGxpc3QoZmlsdGVyKGxhbWJkYSUyMHAlM0ElMjBwLnJlcXVpcmVzX2dyYWQlMkMlMjB0ZXh0X2VuY29kZXJfb25lLnBhcmFtZXRlcnMoKSkpJTBBdGV4dF9sb3JhX3BhcmFtZXRlcnNfdHdvJTIwJTNEJTIwbGlzdChmaWx0ZXIobGFtYmRhJTIwcCUzQSUyMHAucmVxdWlyZXNfZ3JhZCUyQyUyMHRleHRfZW5jb2Rlcl90d28ucGFyYW1ldGVycygpKSk=",highlighted:`text_lora_config = LoraConfig(
    r=args.rank,
    lora_alpha=args.rank,
    init_lora_weights=<span class="hljs-string">&quot;gaussian&quot;</span>,
    target_modules=[<span class="hljs-string">&quot;q_proj&quot;</span>, <span class="hljs-string">&quot;k_proj&quot;</span>, <span class="hljs-string">&quot;v_proj&quot;</span>, <span class="hljs-string">&quot;out_proj&quot;</span>],
)

text_encoder_one.add_adapter(text_lora_config)
text_encoder_two.add_adapter(text_lora_config)
text_lora_parameters_one = <span class="hljs-built_in">list</span>(<span class="hljs-built_in">filter</span>(<span class="hljs-keyword">lambda</span> p: p.requires_grad, text_encoder_one.parameters()))
text_lora_parameters_two = <span class="hljs-built_in">list</span>(<span class="hljs-built_in">filter</span>(<span class="hljs-keyword">lambda</span> p: p.requires_grad, text_encoder_two.parameters()))`,wrap:!1}}),{c(){s=m("p"),s.innerHTML=M,i=o(),_(f.$$.fragment)},l(p){s=u(p,"P",{"data-svelte-h":!0}),c(s)!=="svelte-15bazwz"&&(s.innerHTML=M),i=r(p),y(f.$$.fragment,p)},m(p,h){a(p,s,h),a(p,i,h),$(f,p,h),n=!0},p:oe,i(p){n||(U(f.$$.fragment,p),n=!0)},o(p){b(f.$$.fragment,p),n=!1},d(p){p&&(l(s),l(i)),d(f,p)}}}function tl(g){let s,M,i,f;return s=new pe({props:{id:"lora",option:"UNet",$$slots:{default:[Oe]},$$scope:{ctx:g}}}),i=new pe({props:{id:"lora",option:"text encoder",$$slots:{default:[Ke]},$$scope:{ctx:g}}}),{c(){_(s.$$.fragment),M=o(),_(i.$$.fragment)},l(n){y(s.$$.fragment,n),M=r(n),y(i.$$.fragment,n)},m(n,p){$(s,n,p),a(n,M,p),$(i,n,p),f=!0},p(n,p){const h={};p&2&&(h.$$scope={dirty:p,ctx:n}),s.$set(h);const T={};p&2&&(T.$$scope={dirty:p,ctx:n}),i.$set(T)},i(n){f||(U(s.$$.fragment,n),U(i.$$.fragment,n),f=!0)},o(n){b(s.$$.fragment,n),b(i.$$.fragment,n),f=!1},d(n){n&&l(M),d(s,n),d(i,n)}}}function el(g){let s,M,i,f,n,p,h,T,x,re="<p>当前功能处于实验阶段，API可能在未来版本中变更。</p>",$t,v,fe='<a href="https://hf.co/papers/2106.09685" rel="nofollow">LoRA（大语言模型的低秩适配）</a> 是一种轻量级训练技术，能显著减少可训练参数量。其原理是通过向模型注入少量新权重参数，仅训练这些新增参数。这使得LoRA训练速度更快、内存效率更高，并生成更小的模型权重文件（通常仅数百MB），便于存储和分享。LoRA还可与DreamBooth等其他训练技术结合以加速训练过程。',Ut,J,me='<p>LoRA具有高度通用性，目前已支持以下应用场景：<a href="https://github.com/huggingface/diffusers/blob/main/examples/dreambooth/train_dreambooth_lora.py" rel="nofollow">DreamBooth</a>、<a href="https://github.com/huggingface/diffusers/blob/main/examples/kandinsky2_2/text_to_image/train_text_to_image_lora_decoder.py" rel="nofollow">Kandinsky 2.2</a>、<a href="https://github.com/huggingface/diffusers/blob/main/examples/text_to_image/train_text_to_image_lora_sdxl.py" rel="nofollow">Stable Diffusion XL</a>、<a href="https://github.com/huggingface/diffusers/blob/main/examples/text_to_image/train_text_to_image_lora.py" rel="nofollow">文生图</a>以及<a href="https://github.com/huggingface/diffusers/blob/main/examples/wuerstchen/text_to_image/train_text_to_image_lora_prior.py" rel="nofollow">Wuerstchen</a>。</p>',bt,V,ue='本指南将通过解析<a href="https://github.com/huggingface/diffusers/blob/main/examples/text_to_image/train_text_to_image_lora.py" rel="nofollow">train_text_to_image_lora.py</a>脚本，帮助您深入理解其工作原理，并掌握如何针对具体需求进行定制化修改。',dt,L,Me="运行脚本前，请确保从源码安装库：",ht,W,gt,F,ce="进入包含训练脚本的示例目录，并安装所需依赖：",wt,C,Tt,j,_e='<p>🤗 Accelerate是一个支持多GPU/TPU训练和混合精度计算的库，它能根据硬件环境自动配置训练方案。参阅🤗 Accelerate<a href="https://huggingface.co/docs/accelerate/quicktour" rel="nofollow">快速入门</a>了解更多。</p>',xt,E,ye="初始化🤗 Accelerate环境：",Jt,G,Ct,B,$e="若要创建默认配置环境（不进行交互式设置）：",jt,Q,Zt,k,Ue="若在非交互环境（如Jupyter notebook）中使用：",Rt,I,Xt,H,be='如需训练自定义数据集，请参考<a href="create_dataset">创建训练数据集指南</a>了解数据准备流程。',vt,Z,de='<p>以下章节重点解析训练脚本中与LoRA相关的核心部分，但不会涵盖所有实现细节。如需完整理解，建议直接阅读<a href="https://github.com/huggingface/diffusers/blob/main/examples/text_to_image/train_text_to_image_lora.py" rel="nofollow">脚本源码</a>，如有疑问欢迎反馈。</p>',Vt,N,Lt,A,he='训练脚本提供众多参数用于定制训练过程。所有参数及其说明均定义在<a href="https://github.com/huggingface/diffusers/blob/dd9a5caf61f04d11c0fa9f3947b69ab0010c9a0f/examples/text_to_image/train_text_to_image_lora.py#L85" rel="nofollow"><code>parse_args()</code></a>函数中。多数参数设有默认值，您也可以通过命令行参数覆盖：',Wt,S,ge="例如增加训练轮次：",Ft,Y,Et,q,we='基础参数说明可参考<a href="text2image#script-parameters">文生图训练指南</a>，此处重点介绍LoRA相关参数：',Gt,z,Te="<li><code>--rank</code>：低秩矩阵的内部维度，数值越高可训练参数越多</li> <li><code>--learning_rate</code>：默认学习率为1e-4，但使用LoRA时可适当提高</li>",Bt,P,Qt,D,xe='数据集预处理和训练循环逻辑位于<a href="https://github.com/huggingface/diffusers/blob/dd9a5caf61f04d11c0fa9f3947b69ab0010c9a0f/examples/text_to_image/train_text_to_image_lora.py#L371" rel="nofollow"><code>main()</code></a>函数，如需定制训练流程，可在此处进行修改。',kt,O,Je='与参数说明类似，训练流程的完整解析请参考<a href="text2image#training-script">文生图指南</a>，下文重点介绍LoRA相关实现。',It,R,Ht,K,Ce='<a href="https://github.com/huggingface/diffusers/blob/e4b8f173b97731686e290b2eb98e7f5df2b1b322/examples/text_to_image/train_text_to_image_lora.py#L529" rel="nofollow">优化器</a>仅对<code>lora_layers</code>参数进行优化：',Nt,tt,At,et,je="除LoRA层设置外，该训练脚本与标准train_text_to_image.py基本相同！",St,lt,Yt,at,Ze="完成所有配置后，即可启动训练脚本！🚀",qt,st,Re='以下示例使用<a href="https://huggingface.co/datasets/lambdalabs/naruto-blip-captions" rel="nofollow">Naruto BLIP captions</a>训练生成火影角色。请设置环境变量<code>MODEL_NAME</code>和<code>DATASET_NAME</code>指定基础模型和数据集，<code>OUTPUT_DIR</code>设置输出目录，<code>HUB_MODEL_ID</code>指定Hub存储库名称。脚本运行后将生成以下文件：',zt,nt,Xe="<li>模型检查点</li> <li><code>pytorch_lora_weights.safetensors</code>（训练好的LoRA权重）</li>",Pt,it,ve="多GPU训练请添加<code>--multi_gpu</code>参数。",Dt,X,Ve="<p>在11GB显存的2080 Ti显卡上完整训练约需5小时。</p>",Ot,pt,Kt,ot,Le="训练完成后，您可以通过以下方式进行推理：",te,rt,ee,ft,le,mt,We="恭喜完成LoRA模型训练！如需进一步了解模型使用方法，可参考以下指南：",ae,ut,Fe='<li>学习如何加载<a href="../using-diffusers/loading_adapters#LoRA">不同格式的LoRA权重</a>（如Kohya或TheLastBen训练的模型）</li> <li>掌握使用PEFT进行<a href="../tutorials/using_peft_for_inference">多LoRA组合推理</a>的技巧</li>',se,Mt,ne,_t,ie;return n=new Ye({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),h=new yt({props:{title:"LoRA 低秩适配",local:"lora-低秩适配",headingTag:"h1"}}),W=new w({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}}),C=new Qe({props:{id:"installation",options:["PyTorch","Flax"],$$slots:{default:[De]},$$scope:{ctx:g}}}),G=new w({props:{code:"YWNjZWxlcmF0ZSUyMGNvbmZpZw==",highlighted:"accelerate config",wrap:!1}}),Q=new w({props:{code:"YWNjZWxlcmF0ZSUyMGNvbmZpZyUyMGRlZmF1bHQ=",highlighted:"accelerate config default",wrap:!1}}),I=new w({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}}),N=new yt({props:{title:"脚本参数",local:"脚本参数",headingTag:"h2"}}),Y=new w({props:{code:"YWNjZWxlcmF0ZSUyMGxhdW5jaCUyMHRyYWluX3RleHRfdG9faW1hZ2VfbG9yYS5weSUyMCU1QyUwQSUyMCUyMC0tbnVtX3RyYWluX2Vwb2NocyUzRDE1MCUyMCU1Qw==",highlighted:`accelerate launch train_text_to_image_lora.py \\
  --num_train_epochs=150 \\`,wrap:!1}}),P=new yt({props:{title:"训练脚本实现",local:"训练脚本实现",headingTag:"h2"}}),R=new Qe({props:{id:"lora",options:["UNet","text encoder"],$$slots:{default:[tl]},$$scope:{ctx:g}}}),tt=new w({props:{code:"b3B0aW1pemVyJTIwJTNEJTIwb3B0aW1pemVyX2NscyglMEElMjAlMjAlMjAlMjBsb3JhX2xheWVycyUyQyUwQSUyMCUyMCUyMCUyMGxyJTNEYXJncy5sZWFybmluZ19yYXRlJTJDJTBBJTIwJTIwJTIwJTIwYmV0YXMlM0QoYXJncy5hZGFtX2JldGExJTJDJTIwYXJncy5hZGFtX2JldGEyKSUyQyUwQSUyMCUyMCUyMCUyMHdlaWdodF9kZWNheSUzRGFyZ3MuYWRhbV93ZWlnaHRfZGVjYXklMkMlMEElMjAlMjAlMjAlMjBlcHMlM0RhcmdzLmFkYW1fZXBzaWxvbiUyQyUwQSk=",highlighted:`optimizer = optimizer_cls(
    lora_layers,
    lr=args.learning_rate,
    betas=(args.adam_beta1, args.adam_beta2),
    weight_decay=args.adam_weight_decay,
    eps=args.adam_epsilon,
)`,wrap:!1}}),lt=new yt({props:{title:"启动训练",local:"启动训练",headingTag:"h2"}}),pt=new w({props:{code:"ZXhwb3J0JTIwTU9ERUxfTkFNRSUzRCUyMnN0YWJsZS1kaWZmdXNpb24tdjEtNSUyRnN0YWJsZS1kaWZmdXNpb24tdjEtNSUyMiUwQWV4cG9ydCUyME9VVFBVVF9ESVIlM0QlMjIlMkZzZGRhdGElMkZmaW5ldHVuZSUyRmxvcmElMkZuYXJ1dG8lMjIlMEFleHBvcnQlMjBIVUJfTU9ERUxfSUQlM0QlMjJuYXJ1dG8tbG9yYSUyMiUwQWV4cG9ydCUyMERBVEFTRVRfTkFNRSUzRCUyMmxhbWJkYWxhYnMlMkZuYXJ1dG8tYmxpcC1jYXB0aW9ucyUyMiUwQSUwQWFjY2VsZXJhdGUlMjBsYXVuY2glMjAtLW1peGVkX3ByZWNpc2lvbiUzRCUyMmZwMTYlMjIlMjAlMjB0cmFpbl90ZXh0X3RvX2ltYWdlX2xvcmEucHklMjAlNUMlMEElMjAlMjAtLXByZXRyYWluZWRfbW9kZWxfbmFtZV9vcl9wYXRoJTNEJTI0TU9ERUxfTkFNRSUyMCU1QyUwQSUyMCUyMC0tZGF0YXNldF9uYW1lJTNEJTI0REFUQVNFVF9OQU1FJTIwJTVDJTBBJTIwJTIwLS1kYXRhbG9hZGVyX251bV93b3JrZXJzJTNEOCUyMCU1QyUwQSUyMCUyMC0tcmVzb2x1dGlvbiUzRDUxMiUyMCU1QyUwQSUyMCUyMC0tY2VudGVyX2Nyb3AlMjAlNUMlMEElMjAlMjAtLXJhbmRvbV9mbGlwJTIwJTVDJTBBJTIwJTIwLS10cmFpbl9iYXRjaF9zaXplJTNEMSUyMCU1QyUwQSUyMCUyMC0tZ3JhZGllbnRfYWNjdW11bGF0aW9uX3N0ZXBzJTNENCUyMCU1QyUwQSUyMCUyMC0tbWF4X3RyYWluX3N0ZXBzJTNEMTUwMDAlMjAlNUMlMEElMjAlMjAtLWxlYXJuaW5nX3JhdGUlM0QxZS0wNCUyMCU1QyUwQSUyMCUyMC0tbWF4X2dyYWRfbm9ybSUzRDElMjAlNUMlMEElMjAlMjAtLWxyX3NjaGVkdWxlciUzRCUyMmNvc2luZSUyMiUyMCU1QyUwQSUyMCUyMC0tbHJfd2FybXVwX3N0ZXBzJTNEMCUyMCU1QyUwQSUyMCUyMC0tb3V0cHV0X2RpciUzRCUyNCU3Qk9VVFBVVF9ESVIlN0QlMjAlNUMlMEElMjAlMjAtLXB1c2hfdG9faHViJTIwJTVDJTBBJTIwJTIwLS1odWJfbW9kZWxfaWQlM0QlMjQlN0JIVUJfTU9ERUxfSUQlN0QlMjAlNUMlMEElMjAlMjAtLXJlcG9ydF90byUzRHdhbmRiJTIwJTVDJTBBJTIwJTIwLS1jaGVja3BvaW50aW5nX3N0ZXBzJTNENTAwJTIwJTVDJTBBJTIwJTIwLS12YWxpZGF0aW9uX3Byb21wdCUzRCUyMiVFOCU5MyU5RCVFOCU4OSVCMiVFNyU5QyVCQyVFNyU5RCU5QiVFNyU5QSU4NCVFNyU4MSVBQiVFNSVCRCVCMSVFNSVCRiU4RCVFOCU4MCU4NSVFOCVBNyU5MiVFOCU4OSVCMiUyMiUyMCU1QyUwQSUyMCUyMC0tc2VlZCUzRDEzMzc=",highlighted:`<span class="hljs-built_in">export</span> MODEL_NAME=<span class="hljs-string">&quot;stable-diffusion-v1-5/stable-diffusion-v1-5&quot;</span>
<span class="hljs-built_in">export</span> OUTPUT_DIR=<span class="hljs-string">&quot;/sddata/finetune/lora/naruto&quot;</span>
<span class="hljs-built_in">export</span> HUB_MODEL_ID=<span class="hljs-string">&quot;naruto-lora&quot;</span>
<span class="hljs-built_in">export</span> DATASET_NAME=<span class="hljs-string">&quot;lambdalabs/naruto-blip-captions&quot;</span>

accelerate launch --mixed_precision=<span class="hljs-string">&quot;fp16&quot;</span>  train_text_to_image_lora.py \\
  --pretrained_model_name_or_path=<span class="hljs-variable">$MODEL_NAME</span> \\
  --dataset_name=<span class="hljs-variable">$DATASET_NAME</span> \\
  --dataloader_num_workers=8 \\
  --resolution=512 \\
  --center_crop \\
  --random_flip \\
  --train_batch_size=1 \\
  --gradient_accumulation_steps=4 \\
  --max_train_steps=15000 \\
  --learning_rate=1e-04 \\
  --max_grad_norm=1 \\
  --lr_scheduler=<span class="hljs-string">&quot;cosine&quot;</span> \\
  --lr_warmup_steps=0 \\
  --output_dir=<span class="hljs-variable">\${OUTPUT_DIR}</span> \\
  --push_to_hub \\
  --hub_model_id=<span class="hljs-variable">\${HUB_MODEL_ID}</span> \\
  --report_to=wandb \\
  --checkpointing_steps=500 \\
  --validation_prompt=<span class="hljs-string">&quot;蓝色眼睛的火影忍者角色&quot;</span> \\
  --seed=1337`,wrap:!1}}),rt=new w({props:{code:"ZnJvbSUyMGRpZmZ1c2VycyUyMGltcG9ydCUyMEF1dG9QaXBlbGluZUZvclRleHQySW1hZ2UlMEFpbXBvcnQlMjB0b3JjaCUwQSUwQXBpcGVsaW5lJTIwJTNEJTIwQXV0b1BpcGVsaW5lRm9yVGV4dDJJbWFnZS5mcm9tX3ByZXRyYWluZWQoJTIyc3RhYmxlLWRpZmZ1c2lvbi12MS01JTJGc3RhYmxlLWRpZmZ1c2lvbi12MS01JTIyJTJDJTIwdG9yY2hfZHR5cGUlM0R0b3JjaC5mbG9hdDE2KS50byglMjJjdWRhJTIyKSUwQXBpcGVsaW5lLmxvYWRfbG9yYV93ZWlnaHRzKCUyMnBhdGglMkZ0byUyRmxvcmElMkZtb2RlbCUyMiUyQyUyMHdlaWdodF9uYW1lJTNEJTIycHl0b3JjaF9sb3JhX3dlaWdodHMuc2FmZXRlbnNvcnMlMjIpJTBBaW1hZ2UlMjAlM0QlMjBwaXBlbGluZSglMjJBJTIwbmFydXRvJTIwd2l0aCUyMGJsdWUlMjBleWVzJTIyKS5pbWFnZXMlNUIwJTVE",highlighted:`<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoPipelineForText2Image
<span class="hljs-keyword">import</span> torch

pipeline = AutoPipelineForText2Image.from_pretrained(<span class="hljs-string">&quot;stable-diffusion-v1-5/stable-diffusion-v1-5&quot;</span>, torch_dtype=torch.float16).to(<span class="hljs-string">&quot;cuda&quot;</span>)
pipeline.load_lora_weights(<span class="hljs-string">&quot;path/to/lora/model&quot;</span>, weight_name=<span class="hljs-string">&quot;pytorch_lora_weights.safetensors&quot;</span>)
image = pipeline(<span class="hljs-string">&quot;A naruto with blue eyes&quot;</span>).images[<span class="hljs-number">0</span>]`,wrap:!1}}),ft=new yt({props:{title:"后续步骤",local:"后续步骤",headingTag:"h2"}}),Mt=new qe({props:{source:"https://github.com/huggingface/diffusers/blob/main/docs/source/zh/training/lora.md"}}),{c(){s=m("meta"),M=o(),i=m("p"),f=o(),_(n.$$.fragment),p=o(),_(h.$$.fragment),T=o(),x=m("blockquote"),x.innerHTML=re,$t=o(),v=m("p"),v.innerHTML=fe,Ut=o(),J=m("blockquote"),J.innerHTML=me,bt=o(),V=m("p"),V.innerHTML=ue,dt=o(),L=m("p"),L.textContent=Me,ht=o(),_(W.$$.fragment),gt=o(),F=m("p"),F.textContent=ce,wt=o(),_(C.$$.fragment),Tt=o(),j=m("blockquote"),j.innerHTML=_e,xt=o(),E=m("p"),E.textContent=ye,Jt=o(),_(G.$$.fragment),Ct=o(),B=m("p"),B.textContent=$e,jt=o(),_(Q.$$.fragment),Zt=o(),k=m("p"),k.textContent=Ue,Rt=o(),_(I.$$.fragment),Xt=o(),H=m("p"),H.innerHTML=be,vt=o(),Z=m("blockquote"),Z.innerHTML=de,Vt=o(),_(N.$$.fragment),Lt=o(),A=m("p"),A.innerHTML=he,Wt=o(),S=m("p"),S.textContent=ge,Ft=o(),_(Y.$$.fragment),Et=o(),q=m("p"),q.innerHTML=we,Gt=o(),z=m("ul"),z.innerHTML=Te,Bt=o(),_(P.$$.fragment),Qt=o(),D=m("p"),D.innerHTML=xe,kt=o(),O=m("p"),O.innerHTML=Je,It=o(),_(R.$$.fragment),Ht=o(),K=m("p"),K.innerHTML=Ce,Nt=o(),_(tt.$$.fragment),At=o(),et=m("p"),et.textContent=je,St=o(),_(lt.$$.fragment),Yt=o(),at=m("p"),at.textContent=Ze,qt=o(),st=m("p"),st.innerHTML=Re,zt=o(),nt=m("ul"),nt.innerHTML=Xe,Pt=o(),it=m("p"),it.innerHTML=ve,Dt=o(),X=m("blockquote"),X.innerHTML=Ve,Ot=o(),_(pt.$$.fragment),Kt=o(),ot=m("p"),ot.textContent=Le,te=o(),_(rt.$$.fragment),ee=o(),_(ft.$$.fragment),le=o(),mt=m("p"),mt.textContent=We,ae=o(),ut=m("ul"),ut.innerHTML=Fe,se=o(),_(Mt.$$.fragment),ne=o(),_t=m("p"),this.h()},l(t){const e=Ae("svelte-u9bgzb",document.head);s=u(e,"META",{name:!0,content:!0}),e.forEach(l),M=r(t),i=u(t,"P",{}),Be(i).forEach(l),f=r(t),y(n.$$.fragment,t),p=r(t),y(h.$$.fragment,t),T=r(t),x=u(t,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),c(x)!=="svelte-rkgp9b"&&(x.innerHTML=re),$t=r(t),v=u(t,"P",{"data-svelte-h":!0}),c(v)!=="svelte-13ej9e2"&&(v.innerHTML=fe),Ut=r(t),J=u(t,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),c(J)!=="svelte-5fkbvk"&&(J.innerHTML=me),bt=r(t),V=u(t,"P",{"data-svelte-h":!0}),c(V)!=="svelte-dgwi9"&&(V.innerHTML=ue),dt=r(t),L=u(t,"P",{"data-svelte-h":!0}),c(L)!=="svelte-1l0wcm7"&&(L.textContent=Me),ht=r(t),y(W.$$.fragment,t),gt=r(t),F=u(t,"P",{"data-svelte-h":!0}),c(F)!=="svelte-1por4m9"&&(F.textContent=ce),wt=r(t),y(C.$$.fragment,t),Tt=r(t),j=u(t,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),c(j)!=="svelte-rjrj98"&&(j.innerHTML=_e),xt=r(t),E=u(t,"P",{"data-svelte-h":!0}),c(E)!=="svelte-cfitki"&&(E.textContent=ye),Jt=r(t),y(G.$$.fragment,t),Ct=r(t),B=u(t,"P",{"data-svelte-h":!0}),c(B)!=="svelte-hx4kz2"&&(B.textContent=$e),jt=r(t),y(Q.$$.fragment,t),Zt=r(t),k=u(t,"P",{"data-svelte-h":!0}),c(k)!=="svelte-9bzu46"&&(k.textContent=Ue),Rt=r(t),y(I.$$.fragment,t),Xt=r(t),H=u(t,"P",{"data-svelte-h":!0}),c(H)!=="svelte-i8b5ke"&&(H.innerHTML=be),vt=r(t),Z=u(t,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),c(Z)!=="svelte-3gyr58"&&(Z.innerHTML=de),Vt=r(t),y(N.$$.fragment,t),Lt=r(t),A=u(t,"P",{"data-svelte-h":!0}),c(A)!=="svelte-sf7iv3"&&(A.innerHTML=he),Wt=r(t),S=u(t,"P",{"data-svelte-h":!0}),c(S)!=="svelte-ty40j0"&&(S.textContent=ge),Ft=r(t),y(Y.$$.fragment,t),Et=r(t),q=u(t,"P",{"data-svelte-h":!0}),c(q)!=="svelte-beuy7"&&(q.innerHTML=we),Gt=r(t),z=u(t,"UL",{"data-svelte-h":!0}),c(z)!=="svelte-11dhbz1"&&(z.innerHTML=Te),Bt=r(t),y(P.$$.fragment,t),Qt=r(t),D=u(t,"P",{"data-svelte-h":!0}),c(D)!=="svelte-17b7hli"&&(D.innerHTML=xe),kt=r(t),O=u(t,"P",{"data-svelte-h":!0}),c(O)!=="svelte-yu3j2z"&&(O.innerHTML=Je),It=r(t),y(R.$$.fragment,t),Ht=r(t),K=u(t,"P",{"data-svelte-h":!0}),c(K)!=="svelte-monf0n"&&(K.innerHTML=Ce),Nt=r(t),y(tt.$$.fragment,t),At=r(t),et=u(t,"P",{"data-svelte-h":!0}),c(et)!=="svelte-3rp6cs"&&(et.textContent=je),St=r(t),y(lt.$$.fragment,t),Yt=r(t),at=u(t,"P",{"data-svelte-h":!0}),c(at)!=="svelte-mxmxgt"&&(at.textContent=Ze),qt=r(t),st=u(t,"P",{"data-svelte-h":!0}),c(st)!=="svelte-x76otw"&&(st.innerHTML=Re),zt=r(t),nt=u(t,"UL",{"data-svelte-h":!0}),c(nt)!=="svelte-6bh016"&&(nt.innerHTML=Xe),Pt=r(t),it=u(t,"P",{"data-svelte-h":!0}),c(it)!=="svelte-1itmr9b"&&(it.innerHTML=ve),Dt=r(t),X=u(t,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),c(X)!=="svelte-hbpw4r"&&(X.innerHTML=Ve),Ot=r(t),y(pt.$$.fragment,t),Kt=r(t),ot=u(t,"P",{"data-svelte-h":!0}),c(ot)!=="svelte-xz2z6a"&&(ot.textContent=Le),te=r(t),y(rt.$$.fragment,t),ee=r(t),y(ft.$$.fragment,t),le=r(t),mt=u(t,"P",{"data-svelte-h":!0}),c(mt)!=="svelte-3ai94s"&&(mt.textContent=We),ae=r(t),ut=u(t,"UL",{"data-svelte-h":!0}),c(ut)!=="svelte-1v6cen1"&&(ut.innerHTML=Fe),se=r(t),y(Mt.$$.fragment,t),ne=r(t),_t=u(t,"P",{}),Be(_t).forEach(l),this.h()},h(){ct(s,"name","hf:doc:metadata"),ct(s,"content",ll),ct(x,"class","warning"),ct(J,"class","tip"),ct(j,"class","tip"),ct(Z,"class","tip"),ct(X,"class","warning")},m(t,e){Se(document.head,s),a(t,M,e),a(t,i,e),a(t,f,e),$(n,t,e),a(t,p,e),$(h,t,e),a(t,T,e),a(t,x,e),a(t,$t,e),a(t,v,e),a(t,Ut,e),a(t,J,e),a(t,bt,e),a(t,V,e),a(t,dt,e),a(t,L,e),a(t,ht,e),$(W,t,e),a(t,gt,e),a(t,F,e),a(t,wt,e),$(C,t,e),a(t,Tt,e),a(t,j,e),a(t,xt,e),a(t,E,e),a(t,Jt,e),$(G,t,e),a(t,Ct,e),a(t,B,e),a(t,jt,e),$(Q,t,e),a(t,Zt,e),a(t,k,e),a(t,Rt,e),$(I,t,e),a(t,Xt,e),a(t,H,e),a(t,vt,e),a(t,Z,e),a(t,Vt,e),$(N,t,e),a(t,Lt,e),a(t,A,e),a(t,Wt,e),a(t,S,e),a(t,Ft,e),$(Y,t,e),a(t,Et,e),a(t,q,e),a(t,Gt,e),a(t,z,e),a(t,Bt,e),$(P,t,e),a(t,Qt,e),a(t,D,e),a(t,kt,e),a(t,O,e),a(t,It,e),$(R,t,e),a(t,Ht,e),a(t,K,e),a(t,Nt,e),$(tt,t,e),a(t,At,e),a(t,et,e),a(t,St,e),$(lt,t,e),a(t,Yt,e),a(t,at,e),a(t,qt,e),a(t,st,e),a(t,zt,e),a(t,nt,e),a(t,Pt,e),a(t,it,e),a(t,Dt,e),a(t,X,e),a(t,Ot,e),$(pt,t,e),a(t,Kt,e),a(t,ot,e),a(t,te,e),$(rt,t,e),a(t,ee,e),$(ft,t,e),a(t,le,e),a(t,mt,e),a(t,ae,e),a(t,ut,e),a(t,se,e),$(Mt,t,e),a(t,ne,e),a(t,_t,e),ie=!0},p(t,[e]){const Ee={};e&2&&(Ee.$$scope={dirty:e,ctx:t}),C.$set(Ee);const Ge={};e&2&&(Ge.$$scope={dirty:e,ctx:t}),R.$set(Ge)},i(t){ie||(U(n.$$.fragment,t),U(h.$$.fragment,t),U(W.$$.fragment,t),U(C.$$.fragment,t),U(G.$$.fragment,t),U(Q.$$.fragment,t),U(I.$$.fragment,t),U(N.$$.fragment,t),U(Y.$$.fragment,t),U(P.$$.fragment,t),U(R.$$.fragment,t),U(tt.$$.fragment,t),U(lt.$$.fragment,t),U(pt.$$.fragment,t),U(rt.$$.fragment,t),U(ft.$$.fragment,t),U(Mt.$$.fragment,t),ie=!0)},o(t){b(n.$$.fragment,t),b(h.$$.fragment,t),b(W.$$.fragment,t),b(C.$$.fragment,t),b(G.$$.fragment,t),b(Q.$$.fragment,t),b(I.$$.fragment,t),b(N.$$.fragment,t),b(Y.$$.fragment,t),b(P.$$.fragment,t),b(R.$$.fragment,t),b(tt.$$.fragment,t),b(lt.$$.fragment,t),b(pt.$$.fragment,t),b(rt.$$.fragment,t),b(ft.$$.fragment,t),b(Mt.$$.fragment,t),ie=!1},d(t){t&&(l(M),l(i),l(f),l(p),l(T),l(x),l($t),l(v),l(Ut),l(J),l(bt),l(V),l(dt),l(L),l(ht),l(gt),l(F),l(wt),l(Tt),l(j),l(xt),l(E),l(Jt),l(Ct),l(B),l(jt),l(Zt),l(k),l(Rt),l(Xt),l(H),l(vt),l(Z),l(Vt),l(Lt),l(A),l(Wt),l(S),l(Ft),l(Et),l(q),l(Gt),l(z),l(Bt),l(Qt),l(D),l(kt),l(O),l(It),l(Ht),l(K),l(Nt),l(At),l(et),l(St),l(Yt),l(at),l(qt),l(st),l(zt),l(nt),l(Pt),l(it),l(Dt),l(X),l(Ot),l(Kt),l(ot),l(te),l(ee),l(le),l(mt),l(ae),l(ut),l(se),l(ne),l(_t)),l(s),d(n,t),d(h,t),d(W,t),d(C,t),d(G,t),d(Q,t),d(I,t),d(N,t),d(Y,t),d(P,t),d(R,t),d(tt,t),d(lt,t),d(pt,t),d(rt,t),d(ft,t),d(Mt,t)}}}const ll='{"title":"LoRA 低秩适配","local":"lora-低秩适配","sections":[{"title":"脚本参数","local":"脚本参数","sections":[],"depth":2},{"title":"训练脚本实现","local":"训练脚本实现","sections":[],"depth":2},{"title":"启动训练","local":"启动训练","sections":[],"depth":2},{"title":"后续步骤","local":"后续步骤","sections":[],"depth":2}],"depth":1}';function al(g){return Ie(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class rl extends He{constructor(s){super(),Ne(this,s,al,el,ke,{})}}export{rl as component};
