LoRA.py 909 B

123456789101112131415161718192021222324252627
  1. from pathlib import Path
  2. import modules.shared as shared
  3. from modules.models import load_model
  4. def add_lora_to_model(lora_name):
  5. from peft import PeftModel
  6. # Is there a more efficient way of returning to the base model?
  7. if lora_name == "None":
  8. print("Reloading the model to remove the LoRA...")
  9. shared.model, shared.tokenizer = load_model(shared.model_name)
  10. else:
  11. print(f"Adding the LoRA {lora_name} to the model...")
  12. params = {}
  13. if shared.args.load_in_8bit:
  14. params['device_map'] = {'': 0}
  15. elif not shared.args.cpu:
  16. params['device_map'] = 'auto'
  17. params['dtype'] = shared.model.dtype
  18. shared.model = PeftModel.from_pretrained(shared.model, Path(f"loras/{lora_name}"), **params)
  19. if not shared.args.load_in_8bit and not shared.args.cpu:
  20. shared.model.half()