class_docstr: Load LoRA layers into [`UNet2DConditionModel`] and
  [`CLIPTextModel`](https://huggingface.co/docs/transformers/model_doc/clip#transformers.CLIPTextModel).
class_docstr_tok: ["Load","LoRA","layers","into","[","`","UNet2DConditionModel","`","]","and","[","`","CLIPTextModel","`","]","(","https",":","\/\/huggingface.co\/docs\/transformers\/model_doc\/clip","#","transformers.CLIPTextModel",")","."]
docstr: Fuses the LoRA parameters into the original parameters of the corresponding blocks.
  
  <Tip warning={true}>
  
  This is an experimental API.
  
  </Tip>
  
  Args:
      fuse_unet (`bool`, defaults to `True`): Whether to fuse the UNet LoRA parameters.
      fuse_text_encoder (`bool`, defaults to `True`):
          Whether to fuse the text encoder LoRA parameters. If the text encoder wasn't monkey-patched with the
          LoRA parameters then it won't have any effect.
      lora_scale (`float`, defaults to 1.0):
          Controls how much to influence the outputs with the LoRA parameters.
      safe_fusing (`bool`, defaults to `False`):
          Whether to check fused weights for NaN values before fusing and if values are NaN not fusing them.
      adapter_names (`List[str]`, *optional*):
          Adapter names to be used for fusing. If nothing is passed, all active adapters will be fused.
  
  Example:
  
  ```py
  from diffusers import DiffusionPipeline
  import torch
  
  pipeline = DiffusionPipeline.from_pretrained(
      "stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16
  ).to("cuda")
  pipeline.load_lora_weights("nerijs/pixel-art-xl", weight_name="pixel-art-xl.safetensors", adapter_name="pixel")
  pipeline.fuse_lora(lora_scale=0.7)
  ```
docstr_tok: ["Fuses","the","LoRA","parameters","into","the","original","parameters","of","the","corresponding","blocks",".","<","Tip","warning=","{","true","}",">","This","is","an","experimental","API",".","<","\/Tip",">","Args",":","fuse_unet","(","`","bool","`",",","defaults","to","`","True","`",")",":","Whether","to","fuse","the","UNet","LoRA","parameters",".","fuse_text_encoder","(","`","bool","`",",","defaults","to","`","True","`",")",":","Whether","to","fuse","the","text","encoder","LoRA","parameters",".","If","the","text","encoder","was","n't","monkey-patched","with","the","LoRA","parameters","then","it","wo","n't","have","any","effect",".","lora_scale","(","`","float","`",",","defaults","to","1.0",")",":","Controls","how","much","to","influence","the","outputs","with","the","LoRA","parameters",".","safe_fusing","(","`","bool","`",",","defaults","to","`","False","`",")",":","Whether","to","check","fused","weights","for","NaN","values","before","fusing","and","if","values","are","NaN","not","fusing","them",".","adapter_names","(","`","List","[","str","]","`",",","*","optional","*",")",":","Adapter","names","to","be","used","for","fusing",".","If","nothing","is","passed",",","all","active","adapters","will","be","fused",".","Example",":","``","`","py","from","diffusers","import","DiffusionPipeline","import","torch","pipeline","=","DiffusionPipeline.from_pretrained","(","``","stabilityai\/stable-diffusion-xl-base-1.0","''",",","torch_dtype=torch.float16",")",".to","(","``","cuda","''",")","pipeline.load_lora_weights","(","``","nerijs\/pixel-art-xl","''",",","weight_name=","''","pixel-art-xl.safetensors","''",",","adapter_name=","''","pixel","''",")","pipeline.fuse_lora","(","lora_scale=0.7",")","``","`"]
code: def fuse_lora(
      self,
      fuse_unet: bool = True,
      fuse_text_encoder: bool = True,
      lora_scale: float = 1.0,
      safe_fusing: bool = False,
      adapter_names: Optional[List[str]] = None,
  ):
      r"""
      Fuses the LoRA parameters into the original parameters of the corresponding blocks.
  
      <Tip warning={true}>
  
      This is an experimental API.
  
      </Tip>
  
      Args:
          fuse_unet (`bool`, defaults to `True`): Whether to fuse the UNet LoRA parameters.
          fuse_text_encoder (`bool`, defaults to `True`):
              Whether to fuse the text encoder LoRA parameters. If the text encoder wasn't monkey-patched with the
              LoRA parameters then it won't have any effect.
          lora_scale (`float`, defaults to 1.0):
              Controls how much to influence the outputs with the LoRA parameters.
          safe_fusing (`bool`, defaults to `False`):
              Whether to check fused weights for NaN values before fusing and if values are NaN not fusing them.
          adapter_names (`List[str]`, *optional*):
              Adapter names to be used for fusing. If nothing is passed, all active adapters will be fused.
  
      Example:
  
      ```py
      from diffusers import DiffusionPipeline
      import torch
  
      pipeline = DiffusionPipeline.from_pretrained(
          "stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16
      ).to("cuda")
      pipeline.load_lora_weights("nerijs/pixel-art-xl", weight_name="pixel-art-xl.safetensors", adapter_name="pixel")
      pipeline.fuse_lora(lora_scale=0.7)
      ```
      """
      if fuse_unet or fuse_text_encoder:
          self.num_fused_loras += 1
          if self.num_fused_loras > 1:
              logger.warn(
                  "The current API is supported for operating with a single LoRA file. You are trying to load and fuse more than one LoRA which is not well-supported.",
              )
  
      if fuse_unet:
          unet = (
              getattr(self, self.unet_name)
              if not hasattr(self, "unet")
              else self.unet
          )
          unet.fuse_lora(
              lora_scale,
              safe_fusing=safe_fusing,
              adapter_names=adapter_names,
          )
  
      if USE_PEFT_BACKEND:
          from peft.tuners.tuners_utils import BaseTunerLayer
  
          def fuse_text_encoder_lora(
              text_encoder,
              lora_scale=1.0,
              safe_fusing=False,
              adapter_names=None,
          ):
              merge_kwargs = {"safe_merge": safe_fusing}
  
              for module in text_encoder.modules():
                  if isinstance(module, BaseTunerLayer):
                      if lora_scale != 1.0:
                          module.scale_layer(lora_scale)
  
                      # For BC with previous PEFT versions, we need to check the signature
                      # of the `merge` method to see if it supports the `adapter_names` argument.
                      supported_merge_kwargs = list(
                          inspect.signature(module.merge).parameters
                      )
                      if "adapter_names" in supported_merge_kwargs:
                          merge_kwargs["adapter_names"] = adapter_names
                      elif (
                          "adapter_names" not in supported_merge_kwargs
                          and adapter_names is not None
                      ):
                          raise ValueError(
                              "The `adapter_names` argument is not supported with your PEFT version. "
                              "Please upgrade to the latest version of PEFT. `pip install -U peft`"
                          )
  
                      module.merge(**merge_kwargs)
  
      else:
          deprecate(
              "fuse_text_encoder_lora", "0.27", LORA_DEPRECATION_MESSAGE
          )
  
          def fuse_text_encoder_lora(
              text_encoder, lora_scale=1.0, safe_fusing=False, **kwargs
          ):
              if (
                  "adapter_names" in kwargs
                  and kwargs["adapter_names"] is not None
              ):
                  raise ValueError(
                      "The `adapter_names` argument is not supported in your environment. Please switch to PEFT "
                      "backend to use this argument by installing latest PEFT and transformers."
                      " `pip install -U peft transformers`"
                  )
  
              for _, attn_module in text_encoder_attn_modules(
                  text_encoder
              ):
                  if isinstance(
                      attn_module.q_proj, PatchedLoraProjection
                  ):
                      attn_module.q_proj._fuse_lora(
                          lora_scale, safe_fusing
                      )
                      attn_module.k_proj._fuse_lora(
                          lora_scale, safe_fusing
                      )
                      attn_module.v_proj._fuse_lora(
                          lora_scale, safe_fusing
                      )
                      attn_module.out_proj._fuse_lora(
                          lora_scale, safe_fusing
                      )
  
              for _, mlp_module in text_encoder_mlp_modules(
                  text_encoder
              ):
                  if isinstance(mlp_module.fc1, PatchedLoraProjection):
                      mlp_module.fc1._fuse_lora(lora_scale, safe_fusing)
                      mlp_module.fc2._fuse_lora(lora_scale, safe_fusing)
  
      if fuse_text_encoder:
          if hasattr(self, "text_encoder"):
              fuse_text_encoder_lora(
                  self.text_encoder,
                  lora_scale,
                  safe_fusing,
                  adapter_names=adapter_names,
              )
          if hasattr(self, "text_encoder_2"):
              fuse_text_encoder_lora(
                  self.text_encoder_2,
                  lora_scale,
                  safe_fusing,
                  adapter_names=adapter_names,
              )
  
imports: import inspect
  import os
  from contextlib import nullcontext
  from pathlib import Path
  from typing import Callable, Dict, List, Optional, Union
  import safetensors
  import torch
  from huggingface_hub import model_info
  from huggingface_hub.constants import HF_HUB_OFFLINE
  from huggingface_hub.utils import validate_hf_hub_args
  from packaging import version
  from torch import nn
  from ..None import __version__
  from ..models.modeling_utils import _LOW_CPU_MEM_USAGE_DEFAULT, load_model_dict_into_meta
  from ..utils import USE_PEFT_BACKEND, _get_model_file, convert_state_dict_to_diffusers, convert_state_dict_to_peft, convert_unet_state_dict_to_peft, delete_adapter_layers, deprecate, get_adapter_name, get_peft_kwargs, is_accelerate_available, is_transformers_available, logging, recurse_remove_peft_layers, scale_lora_layers, set_adapter_layers, set_weights_and_activate_adapters
  from .lora_conversion_utils import _convert_kohya_lora_to_diffusers, _maybe_map_sgm_blocks_to_diffusers
label_desc: Use image node_id 15 for calling the LoraLoaderMixin obj's underlying member method code with example usage: obj.fuse_lora(fuse_unet, fuse_text_encoder, lora_scale, safe_fusing, adapter_names) without return types