2023-03-17 01:35:53 +01:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
import modules.shared as shared
|
2023-05-22 03:42:34 +02:00
|
|
|
from modules.logging_colors import logger
|
2023-03-24 01:56:26 +01:00
|
|
|
|
2023-04-07 05:15:45 +02:00
|
|
|
|
2023-04-14 19:52:06 +02:00
|
|
|
def add_lora_to_model(lora_names):
|
2026-03-05 22:57:21 +01:00
|
|
|
add_lora_transformers(lora_names)
|
2023-03-17 01:35:53 +01:00
|
|
|
|
2023-06-06 04:29:29 +02:00
|
|
|
|
2023-08-10 18:54:28 +02:00
|
|
|
def get_lora_path(lora_name):
|
|
|
|
|
p = Path(lora_name)
|
|
|
|
|
if p.exists():
|
|
|
|
|
lora_name = p.parts[-1]
|
|
|
|
|
|
|
|
|
|
return Path(f"{shared.args.lora_dir}/{lora_name}")
|
|
|
|
|
|
|
|
|
|
|
2023-06-19 17:31:24 +02:00
|
|
|
def add_lora_transformers(lora_names):
|
2024-09-04 04:40:53 +02:00
|
|
|
from peft import PeftModel
|
|
|
|
|
|
2025-04-20 18:33:47 +02:00
|
|
|
from modules.torch_utils import get_device
|
|
|
|
|
|
2023-06-19 17:31:24 +02:00
|
|
|
prior_set = set(shared.lora_names)
|
|
|
|
|
added_set = set(lora_names) - prior_set
|
|
|
|
|
removed_set = prior_set - set(lora_names)
|
|
|
|
|
|
|
|
|
|
# If no LoRA needs to be added or removed, exit
|
|
|
|
|
if len(added_set) == 0 and len(removed_set) == 0:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Add a LoRA when another LoRA is already present
|
2023-10-22 21:06:22 +02:00
|
|
|
if len(removed_set) == 0 and len(prior_set) > 0 and "__merged" not in shared.model.peft_config.keys():
|
2023-12-20 05:54:32 +01:00
|
|
|
logger.info(f"Adding the LoRA(s) named {added_set} to the model")
|
2023-06-19 17:31:24 +02:00
|
|
|
for lora in added_set:
|
2023-08-10 18:54:28 +02:00
|
|
|
shared.model.load_adapter(get_lora_path(lora), lora)
|
2023-06-19 17:31:24 +02:00
|
|
|
|
2023-10-22 21:06:22 +02:00
|
|
|
if len(lora_names) > 1:
|
|
|
|
|
merge_loras()
|
|
|
|
|
|
2023-11-19 16:59:29 +01:00
|
|
|
shared.lora_names = lora_names
|
2023-06-19 17:31:24 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# If any LoRA needs to be removed, start over
|
|
|
|
|
if len(removed_set) > 0:
|
2023-11-19 16:55:25 +01:00
|
|
|
shared.model = shared.model.unload()
|
2023-06-19 17:31:24 +02:00
|
|
|
|
|
|
|
|
if len(lora_names) > 0:
|
|
|
|
|
params = {}
|
|
|
|
|
if not shared.args.cpu:
|
2026-03-05 19:59:49 +01:00
|
|
|
if not shared.args.load_in_4bit and not shared.args.load_in_8bit:
|
2023-07-07 07:24:07 +02:00
|
|
|
params['dtype'] = shared.model.dtype
|
|
|
|
|
if hasattr(shared.model, "hf_device_map"):
|
|
|
|
|
params['device_map'] = {"base_model.model." + k: v for k, v in shared.model.hf_device_map.items()}
|
2023-06-19 17:31:24 +02:00
|
|
|
|
|
|
|
|
logger.info("Applying the following LoRAs to {}: {}".format(shared.model_name, ', '.join(lora_names)))
|
2023-08-10 18:54:28 +02:00
|
|
|
shared.model = PeftModel.from_pretrained(shared.model, get_lora_path(lora_names[0]), adapter_name=lora_names[0], **params)
|
2023-06-19 17:31:24 +02:00
|
|
|
for lora in lora_names[1:]:
|
2023-08-10 18:54:28 +02:00
|
|
|
shared.model.load_adapter(get_lora_path(lora), lora)
|
2023-06-19 17:31:24 +02:00
|
|
|
|
2023-10-22 21:06:22 +02:00
|
|
|
if len(lora_names) > 1:
|
|
|
|
|
merge_loras()
|
|
|
|
|
|
2023-06-19 17:31:24 +02:00
|
|
|
if not shared.args.load_in_8bit and not shared.args.cpu:
|
|
|
|
|
shared.model.half()
|
|
|
|
|
if not hasattr(shared.model, "hf_device_map"):
|
2025-01-02 04:06:11 +01:00
|
|
|
device = get_device()
|
|
|
|
|
if device:
|
2023-10-27 04:39:51 +02:00
|
|
|
shared.model = shared.model.to(device)
|
2023-10-23 22:07:17 +02:00
|
|
|
|
2023-11-19 16:55:25 +01:00
|
|
|
shared.lora_names = lora_names
|
|
|
|
|
|
2023-10-23 22:07:17 +02:00
|
|
|
|
|
|
|
|
def merge_loras():
|
|
|
|
|
if len(list({shared.model.peft_config[adapter].r for adapter in shared.model.peft_config.keys()})) > 1:
|
|
|
|
|
logger.warning("The loaded LoRAs cannot be merged, as they have dissimilar ranks. Only the first one will be active.")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
shared.model.add_weighted_adapter(shared.lora_names, [1] * len(shared.lora_names), "__merged")
|
|
|
|
|
shared.model.set_adapter("__merged")
|