From 0789554f65e31a089e4a81e8a47daf2932b762d6 Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Thu, 10 Aug 2023 09:54:28 -0700 Subject: [PATCH] Allow --lora to use an absolute path --- modules/LoRA.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/modules/LoRA.py b/modules/LoRA.py index 1350783f..10020552 100644 --- a/modules/LoRA.py +++ b/modules/LoRA.py @@ -17,6 +17,14 @@ def add_lora_to_model(lora_names): add_lora_transformers(lora_names) +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}") + + def add_lora_exllama(lora_names): try: @@ -40,7 +48,7 @@ def add_lora_exllama(lora_names): if len(lora_names) > 1: logger.warning('ExLlama can only work with 1 LoRA at the moment. Only the first one in the list will be loaded.') - lora_path = Path(f"{shared.args.lora_dir}/{lora_names[0]}") + lora_path = get_lora_path(lora_names[0]) lora_config_path = lora_path / "adapter_config.json" lora_adapter_path = lora_path / "adapter_model.bin" @@ -81,7 +89,7 @@ def add_lora_autogptq(lora_names): inference_mode=True, ) - lora_path = Path(f"{shared.args.lora_dir}/{lora_names[0]}") + lora_path = get_lora_path(lora_names[0]) logger.info("Applying the following LoRAs to {}: {}".format(shared.model_name, ', '.join([lora_names[0]]))) shared.model = get_gptq_peft_model(shared.model, peft_config, lora_path) shared.lora_names = [lora_names[0]] @@ -101,7 +109,7 @@ def add_lora_transformers(lora_names): if len(removed_set) == 0 and len(prior_set) > 0: logger.info(f"Adding the LoRA(s) named {added_set} to the model...") for lora in added_set: - shared.model.load_adapter(Path(f"{shared.args.lora_dir}/{lora}"), lora) + shared.model.load_adapter(get_lora_path(lora), lora) return @@ -123,9 +131,9 @@ def add_lora_transformers(lora_names): params['device_map'] = {"base_model.model." + k: v for k, v in shared.model.hf_device_map.items()} logger.info("Applying the following LoRAs to {}: {}".format(shared.model_name, ', '.join(lora_names))) - shared.model = PeftModel.from_pretrained(shared.model, Path(f"{shared.args.lora_dir}/{lora_names[0]}"), adapter_name=lora_names[0], **params) + shared.model = PeftModel.from_pretrained(shared.model, get_lora_path(lora_names[0]), adapter_name=lora_names[0], **params) for lora in lora_names[1:]: - shared.model.load_adapter(Path(f"{shared.args.lora_dir}/{lora}"), lora) + shared.model.load_adapter(get_lora_path(lora), lora) shared.lora_names = lora_names