Buckets:

hf-doc-build/doc / diffusers /main /ko /tutorials /using_peft_for_inference.html
mishig's picture
download
raw
123 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;LoRA&quot;,&quot;local&quot;:&quot;lora&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;torch.compile&quot;,&quot;local&quot;:&quot;torchcompile&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;가중치 스케일&quot;,&quot;local&quot;:&quot;weight-scale&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;스케줄링 확장&quot;,&quot;local&quot;:&quot;scale-scheduling&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Hotswapping&quot;,&quot;local&quot;:&quot;hotswapping&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;컴파일된 모델&quot;,&quot;local&quot;:&quot;compiled-models&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;set_adapters&quot;,&quot;local&quot;:&quot;merge&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;set_adapters&quot;,&quot;local&quot;:&quot;merge&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;add_weighted_adapter&quot;,&quot;local&quot;:&quot;addweightedadapter&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;fuse_lora&quot;,&quot;local&quot;:&quot;fuselora&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;set_adapters&quot;,&quot;local&quot;:&quot;manage&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;set_adapters&quot;,&quot;local&quot;:&quot;manage&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;save_lora_adapter&quot;,&quot;local&quot;:&quot;saveloraadapter&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;unload_lora_weights&quot;,&quot;local&quot;:&quot;unloadloraweights&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;disable_lora&quot;,&quot;local&quot;:&quot;disablelora&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;get_active_adapters&quot;,&quot;local&quot;:&quot;getactiveadapters&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;get_list_adapters&quot;,&quot;local&quot;:&quot;getlistadapters&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;delete_adapters&quot;,&quot;local&quot;:&quot;deleteadapters&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;다음 단계&quot;,&quot;local&quot;:&quot;next-steps&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/>
<link href="/docs/diffusers/main/en/_app/immutable/entry/start.vZc-dOUR.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/chunks/Bafuik5c.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/chunks/Bupkjr9f.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/entry/app.B6c3Ml05.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/chunks/CyvZ3FS5.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/chunks/l0QnnOED.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/chunks/DnVIJfC5.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/chunks/DsnmJJEf.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/nodes/0.BQlx092V.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/chunks/CoSrdQax.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/nodes/376.aDDGc1bk.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/chunks/B4AMoTAu.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/chunks/B_Tpb3TG.js" rel="modulepreload">
<!--1e0v2s5--><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;LoRA&quot;,&quot;local&quot;:&quot;lora&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;torch.compile&quot;,&quot;local&quot;:&quot;torchcompile&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Weight scale&quot;,&quot;local&quot;:&quot;weight-scale&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Scale scheduling&quot;,&quot;local&quot;:&quot;scale-scheduling&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Hotswapping&quot;,&quot;local&quot;:&quot;hotswapping&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Compiled models&quot;,&quot;local&quot;:&quot;compiled-models&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Merge&quot;,&quot;local&quot;:&quot;merge&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;set_adapters&quot;,&quot;local&quot;:&quot;merge&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;add_weighted_adapter&quot;,&quot;local&quot;:&quot;addweightedadapter&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;fuse_lora&quot;,&quot;local&quot;:&quot;fuselora&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Manage&quot;,&quot;local&quot;:&quot;manage&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;set_adapters&quot;,&quot;local&quot;:&quot;manage&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;save_lora_adapter&quot;,&quot;local&quot;:&quot;saveloraadapter&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;unload_lora_weights&quot;,&quot;local&quot;:&quot;unloadloraweights&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;disable_lora&quot;,&quot;local&quot;:&quot;disablelora&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;get_active_adapters&quot;,&quot;local&quot;:&quot;getactiveadapters&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;get_list_adapters&quot;,&quot;local&quot;:&quot;getlistadapters&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;delete_adapters&quot;,&quot;local&quot;:&quot;deleteadapters&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Next steps&quot;,&quot;local&quot;:&quot;next-steps&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/><!---->
<link href="/docs/diffusers/main/en/_app/immutable/assets/0.tn0RQdqM.css" rel="modulepreload"> <!--[--><!--[0--><!--[--><!--[0--><!--[--><p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: inline-end; margin-inline-start: 10px; display: inline-flex; position: relative; z-index: 10;"><div dir="ltr" class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----></span> <span dir="auto">Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg><!----></button></div> <!--[-1--><!--]--></div><!----> <!--[0--><h1 class="relative group"><a id="lora" 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" href="#lora"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->LoRA<!----></span></h1><!--]--><!----> <p><a href="https://huggingface.co/papers/2106.09685" rel="nofollow">LoRA (Low-Rank Adaptation)</a>는 원래 가중치를 고정하고 소수의 새로운 파라미터만 학습하여 사전 학습된 모델을 새로운 작업에 맞게 조정합니다. 적응 과정이 전체 미세 조정보다 빠르고 비용이 적게 들기 때문에, 새로운 스타일로 이미지를 생성하는 작업에 실용적으로 적용할 수 있습니다.</p> <p>LoRA 체크포인트는 일반적으로 몇백 MB에 불과합니다. <code>pipeline.load_lora_weights</code>로 로드하고 <code>weight_name</code>을 체크포인트 파일 이름으로 설정하세요. 공유된 매개변수는 <a href="/docs/diffusers/main/ko/api/loaders/lora#diffusers.loaders.StableDiffusionLoraLoaderMixin.load_lora_weights">load_lora_weights()</a>를 참조하세요.</p> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">텍스트-이미지</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">텍스트-투-비디오</div><!--]--></div> <div class="language-select"><div><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoPipelineForText2Image
pipeline = AutoPipelineForText2Image.from_pretrained(
<span class="hljs-string">&quot;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>,
dtype=torch.float16
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span>
pipeline.load_lora_weights(
<span class="hljs-string">&quot;ostris/super-cereal-sdxl-lora&quot;</span>,
weight_name=<span class="hljs-string">&quot;cereal_box_sdxl_v1.safetensors&quot;</span>,
adapter_name=<span class="hljs-string">&quot;cereal&quot;</span>
)
pipeline(<span class="hljs-string">&quot;bears, pizza bites&quot;</span>).images[<span class="hljs-number">0</span>]<!----></pre></div><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> LTXConditionPipeline
<span class="hljs-keyword">from</span> diffusers.utils <span class="hljs-keyword">import</span> export_to_video, load_image
pipeline = LTXConditionPipeline.from_pretrained(
<span class="hljs-string">&quot;Lightricks/LTX-Video-0.9.5&quot;</span>, dtype=torch.bfloat16
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span>
pipeline.load_lora_weights(
<span class="hljs-string">&quot;Lightricks/LTX-Video-Cakeify-LoRA&quot;</span>,
weight_name=<span class="hljs-string">&quot;ltxv_095_cakeify_lora.safetensors&quot;</span>,
adapter_name=<span class="hljs-string">&quot;cakeify&quot;</span>
)
pipeline.set_adapters(<span class="hljs-string">&quot;cakeify&quot;</span>)
<span class="hljs-comment"># use &quot;CAKEIFY&quot; to trigger the LoRA</span>
prompt = <span class="hljs-string">&quot;CAKEIFY a person using a knife to cut a cake shaped like a Pikachu plushie&quot;</span>
image = load_image(<span class="hljs-string">&quot;https://huggingface.co/Lightricks/LTX-Video-Cakeify-LoRA/resolve/main/assets/images/pikachu.png&quot;</span>)
video = pipeline(
prompt=prompt,
image=image,
width=<span class="hljs-number">576</span>,
height=<span class="hljs-number">576</span>,
num_frames=<span class="hljs-number">161</span>,
decode_timestep=<span class="hljs-number">0.03</span>,
decode_noise_scale=<span class="hljs-number">0.025</span>,
num_inference_steps=<span class="hljs-number">50</span>,
).frames[<span class="hljs-number">0</span>]
export_to_video(video, <span class="hljs-string">&quot;output.mp4&quot;</span>, fps=<span class="hljs-number">26</span>)<!----></pre></div><!----></div><!----><!----></div><!----> <p>이 페이지의 pipeline LoRA API는 모델 패밀리 전체에서 동일하게 작동합니다. 노이즈 제거 모듈은 <a href="../api/pipelines/stable_diffusion/overview">Stable Diffusion</a>과 <a href="../api/pipelines/stable_diffusion/stable_diffusion_xl">SDXL</a>에서는 <code>pipeline.unet</code>이고, <a href="../api/pipelines/flux">Flux</a>, <a href="../api/pipelines/stable_diffusion/stable_diffusion_3">Stable Diffusion 3</a>, <a href="../api/pipelines/aura_flow">AuraFlow</a>, <a href="../api/pipelines/ltx_video">LTX</a>와 같은 DiT 스타일 모델에서는 <code>pipeline.transformer</code>입니다. <a href="../api/pipelines/cogvideox">CogVideoX</a>, <a href="../api/pipelines/hunyuan_video">HunyuanVideo</a>, <a href="../api/pipelines/wan">Wan</a>과 같은 비디오 모델에도 동일한 패턴이 적용됩니다. 모델 수준의 PEFT 메서드는 해당 속성과 일치하는 <code>prefix</code>(<code>"unet"</code> 또는 <code>"transformer"</code>)를 사용합니다.</p> <p><code>pipeline.load_lora_weights</code>는 denoiser(UNet 또는 transformer)와 text encoders에 LoRA 가중치를 로드하는 권장 방법입니다. 공유된 파라미터에 대해서는 <a href="/docs/diffusers/main/ko/api/loaders/lora#diffusers.loaders.StableDiffusionLoraLoaderMixin.load_lora_weights">load_lora_weights()</a>를 참고하세요. 다음 경우를 처리할 수 있습니다:</p> <ul><li>checkpoint은 denoiser와 텍스트 인코더 간에 키를 공유합니다</li> <li>checkpoint는 별도의 denoiser 및 text-encoder 키를 사용합니다</li></ul> <p><a href="/docs/diffusers/main/ko/api/loaders/peft#diffusers.loaders.PeftAdapterMixin.load_lora_adapter">load_lora_adapter()</a> 메서드는 UNet이나 transformer처럼 <code>PeftAdapterMixin</code>을 서브클래스로 상속하는 모든 Diffusers 모델에서 <em>모델 수준</em>으로 LoRA 어댑터를 로드합니다. 이 메서드는 어댑터 구성을 생성하고 준비한 후, 해당 모듈에 가중치를 로드합니다.</p> <p>대상 모듈(<code>"unet"</code> 또는 <code>"transformer"</code>)의 state dict 키를 필터링하려면 <code>prefix</code>를 사용합니다. 기본값은 <code>"transformer"</code>이므로 Stable Diffusion과 SDXL은 <code>prefix="unet"</code>를 전달해야 합니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoPipelineForText2Image
pipeline = AutoPipelineForText2Image.from_pretrained(
<span class="hljs-string">&quot;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>,
dtype=torch.float16
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span>
pipeline.unet.load_lora_adapter(
<span class="hljs-string">&quot;jbilcke-hf/sdxl-cinematic-1&quot;</span>,
weight_name=<span class="hljs-string">&quot;pytorch_lora_weights.safetensors&quot;</span>,
adapter_name=<span class="hljs-string">&quot;cinematic&quot;</span>,
prefix=<span class="hljs-string">&quot;unet&quot;</span>
)
<span class="hljs-comment"># use cnmt in the prompt to trigger the LoRA</span>
pipeline(<span class="hljs-string">&quot;A cute cnmt eating a slice of pizza, stunning color scheme, masterpiece, illustration&quot;</span>).images[<span class="hljs-number">0</span>]<!----></pre></div><!----> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> FluxPipeline
pipeline = FluxPipeline.from_pretrained(
<span class="hljs-string">&quot;black-forest-labs/FLUX.1-dev&quot;</span>, dtype=torch.bfloat16
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span>
pipeline.transformer.load_lora_adapter(
<span class="hljs-string">&quot;alvarobartt/ghibli-characters-flux-lora&quot;</span>,
adapter_name=<span class="hljs-string">&quot;lora&quot;</span>,
prefix=<span class="hljs-string">&quot;transformer&quot;</span>,
)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="torchcompile" 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" href="#torchcompile"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->torch.compile<!----></span></h2><!--]--><!----> <p><a href="../optimization/fp16#torchcompile">torch.compile</a>는 최적화된 커널을 사용하도록 PyTorch 모델을 컴파일하여 추론 속도를 높입니다. 컴파일하기 전에 LoRA 가중치를 베이스 모델에 병합한 뒤 언로드해야 합니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline
<span class="hljs-comment"># load base model and LoRA</span>
pipeline = DiffusionPipeline.from_pretrained(
<span class="hljs-string">&quot;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>,
dtype=torch.float16
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span>
pipeline.load_lora_weights(
<span class="hljs-string">&quot;ostris/ikea-instructions-lora-sdxl&quot;</span>,
weight_name=<span class="hljs-string">&quot;ikea_instructions_xl_v1_5.safetensors&quot;</span>,
adapter_name=<span class="hljs-string">&quot;ikea&quot;</span>
)
<span class="hljs-comment"># activate LoRA and set adapter weight</span>
pipeline.set_adapters(<span class="hljs-string">&quot;ikea&quot;</span>, adapter_weights=<span class="hljs-number">0.7</span>)
<span class="hljs-comment"># fuse LoRAs and unload weights</span>
pipeline.fuse_lora(adapter_names=[<span class="hljs-string">&quot;ikea&quot;</span>], lora_scale=<span class="hljs-number">1.0</span>)
pipeline.unload_lora_weights()<!----></pre></div><!----> <p>일반적으로 파이프라인에서 가장 많은 연산이 필요한 구성 요소인 denoiser를 컴파일합니다.</p> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">unet</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">transformer</div><!--]--></div> <div class="language-select"><div><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!---->pipeline.unet.to(memory_format=torch.channels_last)
pipeline.unet = torch.<span class="hljs-built_in">compile</span>(pipeline.unet, mode=<span class="hljs-string">&quot;reduce-overhead&quot;</span>, fullgraph=<span class="hljs-literal">True</span>)
pipeline(<span class="hljs-string">&quot;A bowl of ramen shaped like a cute kawaii bear&quot;</span>).images[<span class="hljs-number">0</span>]<!----></pre></div><!----></div><!----> <div hidden=""><p>Flux 및 기타 DiT 파이프라인의 경우, 대신 <code>pipeline.transformer</code>를 컴파일하세요.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!---->pipeline.transformer = torch.<span class="hljs-built_in">compile</span>(pipeline.transformer, mode=<span class="hljs-string">&quot;reduce-overhead&quot;</span>, fullgraph=<span class="hljs-literal">True</span>)<!----></pre></div><!----><!----></div><!----><!----></div><!----> <p>컴파일된 모델과 여러 LoRA를 함께 사용할 때 재컴파일을 피하는 방법은 <a href="#hotswapping">hotswapping</a> 섹션을 참고하세요.</p> <!--[1--><h2 class="relative group"><a id="weight-scale" 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" href="#weight-scale"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->가중치 스케일<!----></span></h2><!--]--><!----> <p><a href="/docs/diffusers/main/ko/api/loaders/lora#diffusers.loaders.lora_base.LoraBaseMixin.set_adapters">set_adapters()</a>의 <code>adapter_weights</code> 매개변수를 사용하여 적용할 LoRA의 양을 제어합니다. 가중치가 <code>0</code>이면 기본 모델 가중치만 사용하는 것과 동일하며, <code>1</code>이면 LoRA를 완전히 사용하는 것과 동일합니다.</p> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">스칼라 가중치</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">컴포넌트별 스케일</div><!--]--></div> <div class="language-select"><div><p><code>adapter_weights</code>에 스칼라를 전달하여 <a href="/docs/diffusers/main/ko/api/loaders/lora#diffusers.loaders.lora_base.LoraBaseMixin.set_adapters">set_adapters()</a>에서 LoRA가 적용되는 강도를 제어합니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoPipelineForText2Image
pipeline = AutoPipelineForText2Image.from_pretrained(
<span class="hljs-string">&quot;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>,
dtype=torch.float16
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span>
pipeline.load_lora_weights(
<span class="hljs-string">&quot;ostris/super-cereal-sdxl-lora&quot;</span>,
weight_name=<span class="hljs-string">&quot;cereal_box_sdxl_v1.safetensors&quot;</span>,
adapter_name=<span class="hljs-string">&quot;cereal&quot;</span>
)
pipeline.set_adapters(<span class="hljs-string">&quot;cereal&quot;</span>, adapter_weights=<span class="hljs-number">1.0</span>)
pipeline(<span class="hljs-string">&quot;bears, pizza bites&quot;</span>).images[<span class="hljs-number">0</span>]<!----></pre></div><!----> <p><code>set_adapters</code>를 기본 패턴으로 사용하지만, 일부 파이프라인은 호출 시 <code>cross_attention_kwargs={"scale": 1.0}</code>을 받아서 어텐션을 스케일링하는 보조 수단으로 사용할 수 있습니다. 크로스 어텐션을 사용하지 않는 파이프라인은 일반적으로 <code>attention_kwargs</code>를 대신 노출합니다(또는 Flux와 Stable Diffusion 3의 경우 <code>joint_attention_kwargs</code>).</p><!----></div><!----> <div hidden=""><blockquote class="warning"><p><a href="/docs/diffusers/main/ko/api/loaders/peft#diffusers.loaders.PeftAdapterMixin.set_adapters">set_adapters()</a> 메서드는 어텐션 가중치만 스케일링합니다. LoRA에 ResNet 또는 다운/업 샘플러가 포함된 경우, 이러한 구성 요소의 스케일 값은 <code>1.0</code>으로 유지됩니다.</p></blockquote> <p>각 텍스트 인코더 또는 디노이저 블록을 스케일링하려면 딕셔너리를 전달합니다. 딕셔너리의 키는 파이프라인 모듈을 따릅니다. 아래 예시는 SDXL(<code>unet</code> 및 텍스트 인코더)입니다. Flux는 <code>transformer</code>와 <code>text_encoder</code>를 사용합니다. SD3는 <code>transformer</code>와 여러 텍스트 인코더를 사용합니다.</p> <p>이 SDXL 예시에서 UNet의 <code>"down"</code> 블록은 0.9로 스케일링되며, <code>"up"</code> 아래에서 <code>"block_0"</code>과 <code>"block_1"</code>의 어텐션 블록 스케일을 추가로 지정할 수 있습니다. <code>"mid"</code>와 같은 블록이 지정되지 않은 경우 기본값 <code>1.0</code>이 사용됩니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoPipelineForText2Image
pipeline = AutoPipelineForText2Image.from_pretrained(
<span class="hljs-string">&quot;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>,
dtype=torch.float16
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span>
pipeline.load_lora_weights(
<span class="hljs-string">&quot;ostris/super-cereal-sdxl-lora&quot;</span>,
weight_name=<span class="hljs-string">&quot;cereal_box_sdxl_v1.safetensors&quot;</span>,
adapter_name=<span class="hljs-string">&quot;cereal&quot;</span>
)
scales = {
<span class="hljs-string">&quot;text_encoder&quot;</span>: <span class="hljs-number">0.5</span>,
<span class="hljs-string">&quot;text_encoder_2&quot;</span>: <span class="hljs-number">0.5</span>,
<span class="hljs-string">&quot;unet&quot;</span>: {
<span class="hljs-string">&quot;down&quot;</span>: <span class="hljs-number">0.9</span>,
<span class="hljs-string">&quot;up&quot;</span>: {
<span class="hljs-string">&quot;block_0&quot;</span>: <span class="hljs-number">0.6</span>,
<span class="hljs-string">&quot;block_1&quot;</span>: [<span class="hljs-number">0.4</span>, <span class="hljs-number">0.8</span>, <span class="hljs-number">1.0</span>],
}
}
}
pipeline.set_adapters(<span class="hljs-string">&quot;cereal&quot;</span>, scales)
pipeline(<span class="hljs-string">&quot;bears, pizza bites&quot;</span>).images[<span class="hljs-number">0</span>]<!----></pre></div><!----><!----></div><!----><!----></div><!----> <!--[2--><h3 class="relative group"><a id="scale-scheduling" 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" href="#scale-scheduling"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->스케줄링 확장<!----></span></h3><!--]--><!----> <p>샘플링 중에 LoRA 스케일을 동적으로 조정하면, 특정 단계에서 스케일을 증가 또는 감소시키는 것이 더 유리할 수 있으므로 전체 구성과 레이아웃에 대한 제어가 더 잘 됩니다.</p> <p>아래 예시의 <a href="https://huggingface.co/alvarobartt/ghibli-characters-flux-lora" rel="nofollow">character LoRA</a>는 캐릭터 생성을 확립하기 위해 처음에는 더 높은 scale로 시작하여 첫 20단계 동안 점진적으로 감소합니다. 이후 단계에서는 LoRA가 학습되지 않은 이미지의 다른 부분에 LoRA 기능이 과도하게 추가되는 것을 방지하기 위해 <code>0.2</code>의 scale만 적용됩니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> FluxPipeline
pipeline = FluxPipeline.from_pretrained(
<span class="hljs-string">&quot;black-forest-labs/FLUX.1-dev&quot;</span>, dtype=torch.bfloat16
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span>
pipeline.load_lora_weights(<span class="hljs-string">&quot;alvarobartt/ghibli-characters-flux-lora&quot;</span>, adapter_name=<span class="hljs-string">&quot;lora&quot;</span>)
num_inference_steps = <span class="hljs-number">30</span>
lora_steps = <span class="hljs-number">20</span>
lora_scales = torch.linspace(<span class="hljs-number">1.5</span>, <span class="hljs-number">0.7</span>, lora_steps).tolist()
lora_scales += [<span class="hljs-number">0.2</span>] * (num_inference_steps - lora_steps + <span class="hljs-number">1</span>)
pipeline.set_adapters(<span class="hljs-string">&quot;lora&quot;</span>, lora_scales[<span class="hljs-number">0</span>])
<span class="hljs-keyword">def</span> <span class="hljs-title function_">callback</span>(<span class="hljs-params">pipeline: FluxPipeline, step: <span class="hljs-built_in">int</span>, timestep: torch.LongTensor, callback_kwargs: <span class="hljs-built_in">dict</span></span>):
pipeline.set_adapters(<span class="hljs-string">&quot;lora&quot;</span>, lora_scales[step + <span class="hljs-number">1</span>])
<span class="hljs-keyword">return</span> callback_kwargs
prompt = <span class="hljs-string">&quot;&quot;&quot;
Ghibli style The Grinch, a mischievous green creature with a sly grin, peeking out from behind a snow-covered tree while plotting his antics,
in a quaint snowy village decorated for the holidays, warm light glowing from cozy homes, with playful snowflakes dancing in the air
&quot;&quot;&quot;</span>
pipeline(
prompt=prompt,
guidance_scale=<span class="hljs-number">3.0</span>,
num_inference_steps=num_inference_steps,
generator=torch.Generator().manual_seed(<span class="hljs-number">42</span>),
callback_on_step_end=callback,
).images[<span class="hljs-number">0</span>]<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="hotswapping" 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" href="#hotswapping"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Hotswapping<!----></span></h2><!--]--><!----> <p>LoRA를 핫스왑하는 것은 <code>load_lora_weights</code>를 여러 번 호출할 때 발생하는 메모리 누적과, 모델이 컴파일된 경우 재컴파일 문제를 피하면서 여러 LoRA를 효율적으로 다루는 방법입니다. 이 워크플로우는 로드된 LoRA가 필요하며, 새로운 LoRA 가중치가 기존에 로드된 LoRA의 위치를 그대로 대체하기 때문입니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline
<span class="hljs-comment"># load base model and LoRAs</span>
pipeline = DiffusionPipeline.from_pretrained(
<span class="hljs-string">&quot;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>,
dtype=torch.float16
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span>
pipeline.load_lora_weights(
<span class="hljs-string">&quot;ostris/ikea-instructions-lora-sdxl&quot;</span>,
weight_name=<span class="hljs-string">&quot;ikea_instructions_xl_v1_5.safetensors&quot;</span>,
adapter_name=<span class="hljs-string">&quot;ikea&quot;</span>
)<!----></pre></div><!----> <blockquote class="warning"><p>LoRA가 텍스트 인코더를 대상으로 하는 경우 핫스왑은 지원되지 않습니다.</p></blockquote> <p><code>load_lora_weights</code>에 <code>hotswap=True</code>를 설정하여 두 번째 LoRA를 교체합니다. <code>adapter_name</code> 매개변수를 사용하여 교체할 LoRA를 지정합니다. 로드할 때 <code>adapter_name</code>을 생략하면 Diffusers는 어댑터 이름을 <code>default_0</code>, <code>default_1</code> 등으로 지정합니다. 이름을 붙인 어댑터는 명시적으로 지정하기가 더 쉽습니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!---->pipeline.load_lora_weights(
<span class="hljs-string">&quot;lordjia/by-feng-zikai&quot;</span>,
hotswap=<span class="hljs-literal">True</span>,
adapter_name=<span class="hljs-string">&quot;ikea&quot;</span>
)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="compiled-models" 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" href="#compiled-models"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->컴파일된 모델<!----></span></h3><!--]--><!----> <p>컴파일된 모델의 경우, LoRA를 핫스왑할 때 재컴파일을 피하려면 <a href="/docs/diffusers/main/ko/api/loaders/lora#diffusers.loaders.lora_base.LoraBaseMixin.enable_lora_hotswap">enable_lora_hotswap()</a>를 사용하세요. 이 메서드는 첫 번째 LoRA를 로드하기 <em>전에</em> 호출해야 하며, <code>torch.compile</code>은 첫 번째 LoRA를 로드한 <em>후에</em> 호출해야 합니다.</p> <blockquote class="tip"><p>두 번째 LoRA가 첫 번째 LoRA와 동일한 LoRA 랭크와 스케일을 대상으로 하는 경우 <a href="/docs/diffusers/main/ko/api/loaders/lora#diffusers.loaders.lora_base.LoraBaseMixin.enable_lora_hotswap">enable_lora_hotswap()</a> 메서드가 항상 필요한 것은 아닙니다.</p></blockquote> <p><a href="/docs/diffusers/main/ko/api/loaders/lora#diffusers.loaders.lora_base.LoraBaseMixin.enable_lora_hotswap">enable_lora_hotswap()</a> 내에서 <code>target_rank</code>를 로드할 어댑터 중 가장 높은 랭크로 설정합니다(기본값 128).</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline
<span class="hljs-comment"># load base model and LoRAs</span>
pipeline = DiffusionPipeline.from_pretrained(
<span class="hljs-string">&quot;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>,
dtype=torch.float16
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span>
<span class="hljs-comment"># 1. enable_lora_hotswap</span>
pipeline.enable_lora_hotswap(target_rank=<span class="hljs-number">128</span>)
pipeline.load_lora_weights(
<span class="hljs-string">&quot;ostris/ikea-instructions-lora-sdxl&quot;</span>,
weight_name=<span class="hljs-string">&quot;ikea_instructions_xl_v1_5.safetensors&quot;</span>,
adapter_name=<span class="hljs-string">&quot;ikea&quot;</span>
)
<span class="hljs-comment"># 2. torch.compile</span>
pipeline.unet = torch.<span class="hljs-built_in">compile</span>(pipeline.unet, mode=<span class="hljs-string">&quot;reduce-overhead&quot;</span>, fullgraph=<span class="hljs-literal">True</span>)
<span class="hljs-comment"># 3. hotswap</span>
pipeline.load_lora_weights(
<span class="hljs-string">&quot;lordjia/by-feng-zikai&quot;</span>,
hotswap=<span class="hljs-literal">True</span>,
adapter_name=<span class="hljs-string">&quot;ikea&quot;</span>
)<!----></pre></div><!----> <blockquote class="tip"><p>모델이 다시 컴파일되었는지 감지하려면 코드를 <code>with torch._dynamo.config.patch(error_on_recompile=True)</code> 컨텍스트 매니저 안으로 이동하세요. 위의 모든 단계를 따랐음에도 모델이 다시 컴파일되는 경우, 재현 가능한 예시를 포함하여 <a href="https://github.com/huggingface/diffusers/issues" rel="nofollow">이슈</a>를 등록해 주세요.</p></blockquote> <p>이 기능을 사용하여 추론 시 다양한 해상도를 예상하는 경우, 컴파일 시 <code>dynamic=True</code>를 설정해야 합니다. 자세한 내용은 <a href="../optimization/fp16#dynamic-shape-compilation">동적 형상 컴파일</a>을 참고하세요.</p> <p>재컴파일을 피할 수 없는 시나리오가 여전히 존재합니다. 예를 들어, 핫스왑된 LoRA가 초기 어댑터보다 더 많은 레이어를 대상으로 하는 경우입니다. 가장 많은 레이어를 대상으로 하는 LoRA를 <em>먼저</em> 로드하도록 하세요. 이 제한 사항에 대한 자세한 내용은 PEFT <a href="https://huggingface.co/docs/peft/main/en/package_reference/hotswap#peft.utils.hotswap.hotswap_adapter" rel="nofollow">hotswapping</a> 문서를 참조하세요.</p> <details><summary>Why `target_rank` and load order matter</summary> <p><a href="/docs/diffusers/main/ko/api/loaders/lora#diffusers.loaders.lora_base.LoraBaseMixin.enable_lora_hotswap">enable_lora_hotswap()</a> 메서드는 LoRA 스케일링 계수를 float에서 torch.tensors로 변환하고, 가중치 데이터가 교체될 때 전체 속성을 다시 할당하는 것을 방지하기 위해 가중치의 shape를 필요한 가장 큰 shape로 패딩합니다.</p> <p><code>target_rank</code> 인수가 중요한 이유입니다. 값이 0으로 패딩되어도 결과는 변하지 않습니다. 다만 패딩 크기에 따라 계산이 느려질 수 있습니다.</p> <p>새로운 LoRA 속성이 추가되지 않으므로, 이후의 각 LoRA는 첫 번째 LoRA가 대상이 되는 레이어와 동일한 레이어 또는 그 부분 집합만 대상으로 할 수 있습니다. LoRA 로드 순서를 선택하는 것이 중요한 이유는, LoRA들이 서로 겹치지 않는 레이어를 대상으로 할 경우 모든 대상 레이어의 합집합을 대상으로 하는 더미 LoRA를 생성하게 될 수 있기 때문입니다.</p> <p>컴파일된 모델의 LoRA 핫스왑에 대한 자세한 내용은 <a href="https://huggingface.co/blog/lora-fast" rel="nofollow">LoRA fast</a> 블로그 포스트를 참고하고, 구현 세부 사항은 <a href="https://github.com/huggingface/peft/blob/92d65cafa51c829484ad3d95cf71d09de57ff066/src/peft/utils/hotswap.py" rel="nofollow"><code>hotswap.py</code></a> 파일을 확인해 보세요.</p></details> <!--[1--><h2 class="relative group"><a id="merge" 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" href="#merge"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->병합<!----></span></h2><!--]--><!----> <p>각 LoRA의 가중치를 병합하여 여러 기존 스타일의 블렌드를 생성할 수 있습니다. LoRA를 병합하는 방법은 여러 가지가 있으며, 각각은 가중치를 병합하는 <em>방법</em>이 서로 다릅니다(생성 품질에 영향을 미칠 수 있음).</p> <!--[2--><h3 class="relative group"><a id="merge" 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" href="#merge"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->set_adapters<!----></span></h3><!--]--><!----> <p><a href="/docs/diffusers/main/ko/api/loaders/lora#diffusers.loaders.lora_base.LoraBaseMixin.set_adapters">set_adapters()</a> 메서드는 가중치 행렬을 연결(concatenation)하여 LoRA를 병합합니다. <a href="/docs/diffusers/main/ko/api/loaders/lora#diffusers.loaders.lora_base.LoraBaseMixin.set_adapters">set_adapters()</a>에 LoRA 이름을 전달하고 <code>adapter_weights</code> 매개변수를 사용하여 각 LoRA의 스케일링을 제어합니다. 예를 들어, <code>adapter_weights=[0.5, 0.5]</code>인 경우 출력은 두 LoRA의 평균이 됩니다.</p> <blockquote class="tip"><p>각 병합된 LoRA를 얼마나 적용할지 제어하려면 <a href="/docs/diffusers/main/ko/api/loaders/lora#diffusers.loaders.lora_base.LoraBaseMixin.set_adapters">set_adapters()</a>에서 <code>adapter_weights</code>를 우선 사용하세요. 자세한 내용은 <a href="#weight-scale">Weight scale</a> 섹션을 참조하세요.</p></blockquote> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!----><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;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>,
dtype=torch.float16
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span>
pipeline.load_lora_weights(
<span class="hljs-string">&quot;ostris/ikea-instructions-lora-sdxl&quot;</span>,
weight_name=<span class="hljs-string">&quot;ikea_instructions_xl_v1_5.safetensors&quot;</span>,
adapter_name=<span class="hljs-string">&quot;ikea&quot;</span>
)
pipeline.load_lora_weights(
<span class="hljs-string">&quot;lordjia/by-feng-zikai&quot;</span>,
weight_name=<span class="hljs-string">&quot;fengzikai_v1.0_XL.safetensors&quot;</span>,
adapter_name=<span class="hljs-string">&quot;feng&quot;</span>
)
pipeline.set_adapters([<span class="hljs-string">&quot;ikea&quot;</span>, <span class="hljs-string">&quot;feng&quot;</span>], adapter_weights=[<span class="hljs-number">0.7</span>, <span class="hljs-number">0.8</span>])
<span class="hljs-comment"># use by Feng Zikai to activate the lordjia/by-feng-zikai LoRA</span>
pipeline(<span class="hljs-string">&quot;A bowl of ramen shaped like a cute kawaii bear, by Feng Zikai&quot;</span>).images[<span class="hljs-number">0</span>]<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="addweightedadapter" 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" href="#addweightedadapter"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->add_weighted_adapter<!----></span></h3><!--]--><!----> <blockquote class="tip"><p>이것은 실험적인 방법이며, 자세한 내용은 PEFT의 <a href="https://huggingface.co/docs/peft/developer_guides/model_merging" rel="nofollow">모델 병합</a>을 참고하세요. 이 통합의 동기 및 설계에 관심이 있다면 이 <a href="https://github.com/huggingface/diffusers/issues/6892" rel="nofollow">이슈</a>를 확인해 보세요.</p></blockquote> <p><a href="https://huggingface.co/docs/peft/main/en/package_reference/lora#peft.LoraModel.add_weighted_adapter" rel="nofollow">add_weighted_adapter</a> 메서드는 <a href="https://huggingface.co/papers/2306.01708" rel="nofollow">TIES</a> 또는 <a href="https://huggingface.co/papers/2311.03099" rel="nofollow">DARE</a>와 같이 더 효율적인 병합 방법을 사용할 수 있게 합니다. 이러한 병합 방법은 병합된 모델에서 중복되고 잠재적으로 간섭을 일으킬 수 있는 파라미터를 제거합니다. 병합하려면 LoRA 랭크가 동일한 랭크를 가져야 한다는 점을 유의하세요. 아래 단계별 설명에서는 SDXL의 UNet을 사용합니다.</p> <p>Diffusers와 PEFT의 최신 안정 버전이 설치되어 있는지 확인하세요.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-bash "><!---->pip install -U -q diffusers peft<!----></pre></div><!----> <p>LoRA UNet에 해당하는 UNet을 로드합니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> copy
<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoModel, DiffusionPipeline
<span class="hljs-keyword">from</span> peft <span class="hljs-keyword">import</span> get_peft_model, LoraConfig, PeftModel
unet = AutoModel.from_pretrained(
<span class="hljs-string">&quot;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>,
dtype=torch.float16,
use_safetensors=<span class="hljs-literal">True</span>,
variant=<span class="hljs-string">&quot;fp16&quot;</span>,
subfolder=<span class="hljs-string">&quot;unet&quot;</span>,
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span><!----></pre></div><!----> <p>파이프라인을 로드하고, UNet을 전달한 다음, LoRA를 로드합니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!---->pipeline = DiffusionPipeline.from_pretrained(
<span class="hljs-string">&quot;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>,
variant=<span class="hljs-string">&quot;fp16&quot;</span>,
dtype=torch.float16,
unet=unet
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span>
pipeline.load_lora_weights(
<span class="hljs-string">&quot;ostris/ikea-instructions-lora-sdxl&quot;</span>,
weight_name=<span class="hljs-string">&quot;ikea_instructions_xl_v1_5.safetensors&quot;</span>,
adapter_name=<span class="hljs-string">&quot;ikea&quot;</span>
)<!----></pre></div><!----> <p>로딩한 첫 번째 UNet과 파이프라인의 LoRA UNet을 결합하여 LoRA 체크포인트에서 <a href="https://huggingface.co/docs/peft/main/en/package_reference/peft_model#peft.PeftModel" rel="nofollow">PeftModel</a>을 생성합니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!---->sdxl_unet = copy.deepcopy(unet)
ikea_peft_model = get_peft_model(
sdxl_unet,
pipeline.unet.peft_config[<span class="hljs-string">&quot;ikea&quot;</span>],
adapter_name=<span class="hljs-string">&quot;ikea&quot;</span>
)
original_state_dict = {<span class="hljs-string">f&quot;base_model.model.<span class="hljs-subst">{k}</span>&quot;</span>: v <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> pipeline.unet.state_dict().items()}
ikea_peft_model.load_state_dict(original_state_dict, strict=<span class="hljs-literal">True</span>)<!----></pre></div><!----> <blockquote class="tip"><p>아래와 같이 <code>ikea_peft_model</code>을 Hub에 업로드하여 저장하고 재사용할 수 있습니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!---->ikea_peft_model.push_to_hub(<span class="hljs-string">&quot;ikea_peft_model&quot;</span>, token=TOKEN)<!----></pre></div><!----></blockquote> <p>이 과정을 반복하여 두 번째 LoRA에 대한 <a href="https://huggingface.co/docs/peft/main/en/package_reference/peft_model#peft.PeftModel" rel="nofollow">PeftModel</a>을 생성합니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!---->pipeline.delete_adapters(<span class="hljs-string">&quot;ikea&quot;</span>)
sdxl_unet.delete_adapters(<span class="hljs-string">&quot;ikea&quot;</span>)
pipeline.load_lora_weights(
<span class="hljs-string">&quot;lordjia/by-feng-zikai&quot;</span>,
weight_name=<span class="hljs-string">&quot;fengzikai_v1.0_XL.safetensors&quot;</span>,
adapter_name=<span class="hljs-string">&quot;feng&quot;</span>
)
pipeline.set_adapters(adapter_names=<span class="hljs-string">&quot;feng&quot;</span>)
feng_peft_model = get_peft_model(
sdxl_unet,
pipeline.unet.peft_config[<span class="hljs-string">&quot;feng&quot;</span>],
adapter_name=<span class="hljs-string">&quot;feng&quot;</span>
)
original_state_dict = {<span class="hljs-string">f&quot;base_model.model.<span class="hljs-subst">{k}</span>&quot;</span>: v <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> pipeline.unet.state_dict().items()}
feng_peft_model.load_state_dict(original_state_dict, strict=<span class="hljs-literal">True</span>)<!----></pre></div><!----> <p>기본 UNet 모델을 로드하고 어댑터를 로드합니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!---->base_unet = AutoModel.from_pretrained(
<span class="hljs-string">&quot;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>,
dtype=torch.float16,
use_safetensors=<span class="hljs-literal">True</span>,
variant=<span class="hljs-string">&quot;fp16&quot;</span>,
subfolder=<span class="hljs-string">&quot;unet&quot;</span>,
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span>
model = PeftModel.from_pretrained(
base_unet,
<span class="hljs-string">&quot;stevhliu/ikea_peft_model&quot;</span>,
use_safetensors=<span class="hljs-literal">True</span>,
subfolder=<span class="hljs-string">&quot;ikea&quot;</span>,
adapter_name=<span class="hljs-string">&quot;ikea&quot;</span>
)
model.load_adapter(
<span class="hljs-string">&quot;stevhliu/feng_peft_model&quot;</span>,
use_safetensors=<span class="hljs-literal">True</span>,
subfolder=<span class="hljs-string">&quot;feng&quot;</span>,
adapter_name=<span class="hljs-string">&quot;feng&quot;</span>
)<!----></pre></div><!----> <p><a href="https://huggingface.co/docs/peft/main/en/package_reference/lora#peft.LoraModel.add_weighted_adapter" rel="nofollow">add_weighted_adapter</a>를 사용하여 LoRA를 병합하고, <code>combination_type</code>으로 병합 방식을 지정합니다. 아래 예제는 <code>"dare_linear"</code> 메서드를 사용합니다(이러한 병합 방법에 대해 자세히 알아보려면 이 <a href="https://huggingface.co/blog/peft_merging" rel="nofollow">블로그 게시물</a>을 참고하세요). 이 메서드는 일부 가중치를 무작위로 제거한 후, <code>weights</code>의 각 LoRA에 설정된 가중치에 따라 텐서의 가중 합을 수행합니다.</p> <p><a href="/docs/diffusers/main/ko/api/loaders/peft#diffusers.loaders.PeftAdapterMixin.set_adapters">set_adapters()</a>로 병합된 LoRA를 활성화합니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!---->model.add_weighted_adapter(
adapters=[<span class="hljs-string">&quot;ikea&quot;</span>, <span class="hljs-string">&quot;feng&quot;</span>],
combination_type=<span class="hljs-string">&quot;dare_linear&quot;</span>,
weights=[<span class="hljs-number">1.0</span>, <span class="hljs-number">1.0</span>],
adapter_name=<span class="hljs-string">&quot;ikea-feng&quot;</span>
)
model.set_adapters(<span class="hljs-string">&quot;ikea-feng&quot;</span>)
pipeline = DiffusionPipeline.from_pretrained(
<span class="hljs-string">&quot;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>,
unet=model,
variant=<span class="hljs-string">&quot;fp16&quot;</span>,
dtype=torch.float16,
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span>
pipeline(<span class="hljs-string">&quot;A bowl of ramen shaped like a cute kawaii bear, by Feng Zikai&quot;</span>).images[<span class="hljs-number">0</span>]<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="fuselora" 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" href="#fuselora"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->fuse_lora<!----></span></h3><!--]--><!----> <p><a href="/docs/diffusers/main/ko/api/loaders/lora#diffusers.loaders.lora_base.LoraBaseMixin.fuse_lora">fuse_lora()</a> 메서드는 LoRA 가중치를 기반 모델의 원래 denoiser(UNet 또는 transformer) 및 text encoder 가중치에 직접 병합합니다. 이를 통해 각 LoRA마다 기반 모델을 로드하는 오버헤드가 줄어들며, 모델이 한 번만 로드되므로 메모리 사용량이 감소하고 추론 속도가 향상됩니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!----><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;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>,
dtype=torch.float16
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span>
pipeline.load_lora_weights(
<span class="hljs-string">&quot;ostris/ikea-instructions-lora-sdxl&quot;</span>,
weight_name=<span class="hljs-string">&quot;ikea_instructions_xl_v1_5.safetensors&quot;</span>,
adapter_name=<span class="hljs-string">&quot;ikea&quot;</span>
)
pipeline.load_lora_weights(
<span class="hljs-string">&quot;lordjia/by-feng-zikai&quot;</span>,
weight_name=<span class="hljs-string">&quot;fengzikai_v1.0_XL.safetensors&quot;</span>,
adapter_name=<span class="hljs-string">&quot;feng&quot;</span>
)
pipeline.set_adapters([<span class="hljs-string">&quot;ikea&quot;</span>, <span class="hljs-string">&quot;feng&quot;</span>], adapter_weights=[<span class="hljs-number">0.7</span>, <span class="hljs-number">0.8</span>])<!----></pre></div><!----> <p><a href="/docs/diffusers/main/ko/api/loaders/lora#diffusers.loaders.lora_base.LoraBaseMixin.fuse_lora">fuse_lora()</a>를 호출하여 이를 병합합니다. <code>lora_scale</code> 매개변수는 LoRA 가중치로 출력을 얼마나 스케일링할지를 제어합니다. 파이프라인에서 <code>cross_attention_kwargs</code>에 <code>scale</code>를 전달해도 작동하지 않으므로, 지금 이 시점에 해당 조정을 수행하는 것이 중요합니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!---->pipeline.fuse_lora(adapter_names=[<span class="hljs-string">&quot;ikea&quot;</span>, <span class="hljs-string">&quot;feng&quot;</span>], lora_scale=<span class="hljs-number">1.0</span>)<!----></pre></div><!----> <p>기저 모델에 이미 융합되었으므로 LoRA 가중치를 언로드합니다. <a href="/docs/diffusers/main/ko/api/pipelines/overview#diffusers.DiffusionPipeline.save_pretrained">save_pretrained()</a>를 사용하여 로컬에 저장하거나 <code>~PushToHubMixin.push_to_hub</code>를 사용하여 Hub에 저장하여 융합된 파이프라인을 저장합니다.</p> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">로컬에 저장</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">Hub에 저장</div><!--]--></div> <div class="language-select"><div><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!---->pipeline.unload_lora_weights()
pipeline.save_pretrained(<span class="hljs-string">&quot;path/to/fused-pipeline&quot;</span>)<!----></pre></div><!----></div><!----> <div hidden=""><div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!---->pipeline.unload_lora_weights()
pipeline.push_to_hub(<span class="hljs-string">&quot;fused-ikea-feng&quot;</span>)<!----></pre></div><!----></div><!----><!----></div><!----> <p>융합된 파이프라인은 이제 각 LoRA를 별도로 로드할 필요 없이 추론을 위해 빠르게 로드할 수 있습니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!---->pipeline = DiffusionPipeline.from_pretrained(
<span class="hljs-string">&quot;username/fused-ikea-feng&quot;</span>, dtype=torch.float16,
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span>
pipeline(<span class="hljs-string">&quot;A bowl of ramen shaped like a cute kawaii bear, by Feng Zikai&quot;</span>).images[<span class="hljs-number">0</span>]<!----></pre></div><!----> <p><code>lora_scale</code> 값을 변경하려는 경우와 같이 기본 모델의 가중치를 복원하려면 <a href="/docs/diffusers/main/ko/api/loaders/lora#diffusers.loaders.lora_base.LoraBaseMixin.unfuse_lora">unfuse_lora()</a>를 사용하세요. 단일 LoRA만 융합된 경우에만 unfuse할 수 있습니다. 예를 들어, 위에서 언급한 파이프라인에는 여러 개의 융합된 LoRA가 있으므로 작동하지 않습니다. 이러한 경우 전체 모델을 다시 로드해야 합니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!---->pipeline.unfuse_lora()<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="manage" 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" href="#manage"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->관리<!----></span></h2><!--]--><!----> <p>Diffusers는 LoRA를 다루는 작업을 관리하는 데 도움이 되는 여러 메서드를 제공합니다. 이러한 메서드는 여러 LoRA를 함께 사용할 때 특히 유용합니다.</p> <!--[2--><h3 class="relative group"><a id="manage" 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" href="#manage"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->set_adapters<!----></span></h3><!--]--><!----> <p><a href="/docs/diffusers/main/ko/api/loaders/lora#diffusers.loaders.lora_base.LoraBaseMixin.set_adapters">set_adapters()</a>는 여러 개의 활성 LoRA가 있는 경우 현재 사용할 LoRA를 활성화합니다. 이를 통해 이름을 지정하여 서로 다른 LoRA 간에 전환할 수 있습니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!----><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;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>,
dtype=torch.float16
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span>
pipeline.load_lora_weights(
<span class="hljs-string">&quot;ostris/ikea-instructions-lora-sdxl&quot;</span>,
weight_name=<span class="hljs-string">&quot;ikea_instructions_xl_v1_5.safetensors&quot;</span>,
adapter_name=<span class="hljs-string">&quot;ikea&quot;</span>
)
pipeline.load_lora_weights(
<span class="hljs-string">&quot;lordjia/by-feng-zikai&quot;</span>,
weight_name=<span class="hljs-string">&quot;fengzikai_v1.0_XL.safetensors&quot;</span>,
adapter_name=<span class="hljs-string">&quot;feng&quot;</span>
)
<span class="hljs-comment"># activates the feng LoRA instead of the ikea LoRA</span>
pipeline.set_adapters(<span class="hljs-string">&quot;feng&quot;</span>)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="saveloraadapter" 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" href="#saveloraadapter"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->save_lora_adapter<!----></span></h3><!--]--><!----> <p><code>pipeline.unet</code> 또는 <code>pipeline.transformer</code>로 로드한 동일한 모듈에서 <a href="/docs/diffusers/main/ko/api/loaders/peft#diffusers.loaders.PeftAdapterMixin.save_lora_adapter">save_lora_adapter()</a>를 사용하여 어댑터를 저장합니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoPipelineForText2Image
pipeline = AutoPipelineForText2Image.from_pretrained(
<span class="hljs-string">&quot;stabilityai/stable-diffusion-xl-base-1.0&quot;</span>,
dtype=torch.float16
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span>
pipeline.unet.load_lora_adapter(
<span class="hljs-string">&quot;jbilcke-hf/sdxl-cinematic-1&quot;</span>,
weight_name=<span class="hljs-string">&quot;pytorch_lora_weights.safetensors&quot;</span>,
adapter_name=<span class="hljs-string">&quot;cinematic&quot;</span>,
prefix=<span class="hljs-string">&quot;unet&quot;</span>
)
pipeline.unet.save_lora_adapter(<span class="hljs-string">&quot;path/to/save&quot;</span>, adapter_name=<span class="hljs-string">&quot;cinematic&quot;</span>)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="unloadloraweights" 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" href="#unloadloraweights"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->unload_lora_weights<!----></span></h3><!--]--><!----> <p><a href="/docs/diffusers/main/ko/api/loaders/lora#diffusers.loaders.lora_base.LoraBaseMixin.unload_lora_weights">unload_lora_weights()</a> 메서드는 파이프라인의 LoRA 가중치를 해제하여 원래 모델의 가중치를 복원합니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!---->pipeline.unload_lora_weights()<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="disablelora" 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" href="#disablelora"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->disable_lora<!----></span></h3><!--]--><!----> <p><a href="/docs/diffusers/main/ko/api/loaders/lora#diffusers.loaders.lora_base.LoraBaseMixin.disable_lora">disable_lora()</a> 메서드는 모든 LoRA를 비활성화합니다(파이프라인에는 여전히 유지됨) 그리고 파이프라인을 기본 모델 가중치로 복원합니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!---->pipeline.disable_lora()<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="getactiveadapters" 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" href="#getactiveadapters"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->get_active_adapters<!----></span></h3><!--]--><!----> <p><a href="/docs/diffusers/main/ko/api/loaders/lora#diffusers.loaders.lora_base.LoraBaseMixin.get_active_adapters">get_active_adapters()</a> 메서드는 파이프라인에 연결된 활성 LoRA 목록을 반환합니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!---->pipeline.get_active_adapters()
<span class="hljs-comment"># [&quot;cereal&quot;, &quot;ikea&quot;]</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="getlistadapters" 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" href="#getlistadapters"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->get_list_adapters<!----></span></h3><!--]--><!----> <p><a href="/docs/diffusers/main/ko/api/loaders/lora#diffusers.loaders.lora_base.LoraBaseMixin.get_list_adapters">get_list_adapters()</a> 메서드는 파이프라인의 각 구성 요소에 대해 로드된 모든 LoRA를 반환합니다. 사전의 키는 <code>unet</code> 또는 <code>transformer</code>와 같이 로드된 모듈 및 텍스트 인코더와 일치합니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!---->pipeline.get_list_adapters()
<span class="hljs-comment"># {&quot;unet&quot;: [&quot;cereal&quot;, &quot;ikea&quot;], &quot;text_encoder_2&quot;: [&quot;cereal&quot;]}</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="deleteadapters" 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" href="#deleteadapters"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->delete_adapters<!----></span></h3><!--]--><!----> <p><a href="/docs/diffusers/main/ko/api/loaders/lora#diffusers.loaders.lora_base.LoraBaseMixin.delete_adapters">delete_adapters()</a> 메서드는 모델에서 LoRA와 해당 레이어를 완전히 제거합니다.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 복사됨</div><!----></button><!----></div> <pre class="language-py "><!---->pipeline.delete_adapters(<span class="hljs-string">&quot;ikea&quot;</span>)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="next-steps" 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" href="#next-steps"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->다음 단계<!----></span></h2><!--]--><!----> <ul><li><a href="https://lorastudio.co/models" rel="nofollow">LoRA Studio</a>에서 사용할 수 있는 다양한 LoRA를 탐색하거나, 아래 Space를 통해 Civitai에서 좋아하는 LoRA를 Hub에 업로드할 수 있습니다.</li></ul> <iframe src="https://multimodalart-civitai-to-hf.hf.space" frameborder="0" width="850" height="450"></iframe> <ul><li><p>You can find additional LoRAs in the <a href="https://huggingface.co/spaces/multimodalart/flux-lora-the-explorer" rel="nofollow">FLUX LoRA the Explorer</a> and <a href="https://huggingface.co/spaces/multimodalart/LoraTheExplorer" rel="nofollow">LoRA the Explorer</a> Spaces.</p></li> <li><p>FlashAttention-3와 fp8 양자화와 같은 방법으로 LoRA 추론을 최적화하는 방법을 배우려면 <a href="https://huggingface.co/blog/lora-fast" rel="nofollow">Diffusers와 PEFT를 활용한 Flux용 Fast LoRA 추론</a> 블로그 게시물을 확인하세요.</p></li></ul> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/diffusers/blob/main/docs/source/en/tutorials/using_peft_for_inference.md" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg><!----> <span><span class="underline">Update</span> on GitHub</span></a><!----> <p></p><!--]--><!----><!--]--><!--]--><!--]--> <!--[-1--><!--]--><!--]-->
<script>
{
__sveltekit_1rjuniz = {
base: "/docs/diffusers/main/en",
assets: "/docs/diffusers/main/en"
};
const element = document.currentScript.parentElement;
Promise.all([
import("/docs/diffusers/main/en/_app/immutable/entry/start.vZc-dOUR.js"),
import("/docs/diffusers/main/en/_app/immutable/entry/app.B6c3Ml05.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 376],
data: [null,null],
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
123 kB
·
Xet hash:
59eb6b34f5628fcffa0bad67cb0b56fa11b9a4784b68c34ccc66e64e278a1493

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.