Fuses LoRAs for the text encoder. Args: text_encoder (`torch.nn.Module`): The text encoder module to set the adapter layers for. If `None`, it will try to get the `text_encoder` attribute. lora_scale (`float`, defaults to 1.0): Controls h
(text_encoder, lora_scale=1.0, safe_fusing=False, adapter_names=None)
| 53 | |
| 54 | |
| 55 | def fuse_text_encoder_lora(text_encoder, lora_scale=1.0, safe_fusing=False, adapter_names=None): |
| 56 | """ |
| 57 | Fuses LoRAs for the text encoder. |
| 58 | |
| 59 | Args: |
| 60 | text_encoder (`torch.nn.Module`): |
| 61 | The text encoder module to set the adapter layers for. If `None`, it will try to get the `text_encoder` |
| 62 | attribute. |
| 63 | lora_scale (`float`, defaults to 1.0): |
| 64 | Controls how much to influence the outputs with the LoRA parameters. |
| 65 | safe_fusing (`bool`, defaults to `False`): |
| 66 | Whether to check fused weights for NaN values before fusing and if values are NaN not fusing them. |
| 67 | adapter_names (`List[str]` or `str`): |
| 68 | The names of the adapters to use. |
| 69 | """ |
| 70 | merge_kwargs = {"safe_merge": safe_fusing} |
| 71 | |
| 72 | for module in text_encoder.modules(): |
| 73 | if isinstance(module, BaseTunerLayer): |
| 74 | if lora_scale != 1.0: |
| 75 | module.scale_layer(lora_scale) |
| 76 | |
| 77 | # For BC with previous PEFT versions, we need to check the signature |
| 78 | # of the `merge` method to see if it supports the `adapter_names` argument. |
| 79 | supported_merge_kwargs = list(inspect.signature(module.merge).parameters) |
| 80 | if "adapter_names" in supported_merge_kwargs: |
| 81 | merge_kwargs["adapter_names"] = adapter_names |
| 82 | elif "adapter_names" not in supported_merge_kwargs and adapter_names is not None: |
| 83 | raise ValueError( |
| 84 | "The `adapter_names` argument is not supported with your PEFT version. " |
| 85 | "Please upgrade to the latest version of PEFT. `pip install -U peft`" |
| 86 | ) |
| 87 | |
| 88 | module.merge(**merge_kwargs) |
| 89 | |
| 90 | |
| 91 | def unfuse_text_encoder_lora(text_encoder): |