-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmerge_weights.py
40 lines (33 loc) · 1002 Bytes
/
merge_weights.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
import torch
from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer
import os
import argparse
def parse_args():
"""
Function to parse arguments
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--base_model_path",
type=str,
)
parser.add_argument(
"--lora_path",
type=str,
)
args = parser.parse_args()
return args
if __name__ == "__main__":
ARGS = parse_args()
base_model = AutoModelForCausalLM.from_pretrained(
ARGS.base_model_path,
torch_dtype=torch.bfloat16,
device_map="auto",
)
tokenizer = AutoTokenizer.from_pretrained(ARGS.base_model_path)
lora_model = PeftModel.from_pretrained(base_model, ARGS.lora_path)
model = lora_model.merge_and_unload()
output_path = os.path.join(ARGS.lora_path, "merged_model")
model.save_pretrained(output_path)
tokenizer.save_pretrained(output_path)