-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmerge_lora.py
55 lines (43 loc) · 1.96 KB
/
merge_lora.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import argparse
import torch
from peft import PeftModel
from transformers import AutoTokenizer, AutoModelForCausalLM
def apply_lora(base_model_path, target_model_path, lora_path, delta_path=None):
print(f"Loading the base model from {base_model_path}")
base = AutoModelForCausalLM.from_pretrained(
base_model_path, low_cpu_mem_usage=True
)
base_tokenizer = AutoTokenizer.from_pretrained(base_model_path, use_fast=False)
# adding delta
print(f"Loading the LoRA adapter from {delta_path}")
delta_init_model = PeftModel.from_pretrained(
base,
delta_path,
# torch_dtype=torch.float16,
)
print("Adding the delta init")
model = delta_init_model.merge_and_unload()
# merging lora
print(f"Loading the LoRA adapter from {lora_path}")
lora_model = PeftModel.from_pretrained(
model,
lora_path,
)
print("Applying the LoRA")
model = lora_model.merge_and_unload()
embedding_size = model.get_input_embeddings().weight.shape[0]
if len(base_tokenizer) > embedding_size:
print(f"The vocabulary size of the tokenizer in the lora model folder contains {len(base_tokenizer)-embedding_size} more tokens than the base model.")
print("Resizing the token embeddings of the merged model...")
model.resize_token_embeddings(len(base_tokenizer))
print(f"Saving the target model to {target_model_path}")
model.save_pretrained(target_model_path)
base_tokenizer.save_pretrained(target_model_path)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--base-model-path", type=str, required=True)
parser.add_argument("--target-model-path", type=str, required=True)
parser.add_argument("--delta-path", type=str, required=True)
parser.add_argument("--lora-path", type=str, required=True)
args = parser.parse_args()
apply_lora(args.base_model_path, args.target_model_path, args.lora_path, args.delta_path)