merge_model.py 832 Bytes
Newer Older
wanglch's avatar
wanglch committed
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
import torch
from peft import PeftModel
from transformers import AutoTokenizer, AutoModel

base_model_path = '../Finllm/Finllm_computing'
lora_model_path = '../saves/Finllm_computing_train/lora/sft/checkpoint-4000'
new_model_path = '../Finllm/Finllm_computing-merged'


# Loading the base model and tokenzier
model = AutoModel.from_pretrained(
       base_model_path, torch_dtype=torch.float16, trust_remote_code=True
    )

tokenzier = AutoTokenizer.from_pretrained(
       base_model_path, trust_remote_code=True
    )

# Loading the LoRA adapter
lora_model = PeftModel.from_pretrained(model, lora_model_path, torch_dtype=torch.float16,)

# Applying the LoRA
new_model = lora_model.merge_and_unload()

# Saving the target model to new model paths
new_model.save_pretrained(new_model_path)
tokenzier.save_pretrained(new_model_path)