forked from Denilah/CoMA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_lora.py
37 lines (31 loc) · 1.15 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
from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig
import torch
"""
使用该脚本,将lora的权重合并到base model中
"""
def merge_lora_to_base_model():
model_name_or_path = '/hy-tmp/QWen_train/QWen-7b-hf'
adapter_name_or_path = '/hy-tmp/Firefly_train/output/code-qwen-7b-6/final'
save_path = 'checkpoint/qwen-code-sft-merge-6'
config = AutoConfig.from_pretrained(model_name_or_path)
tokenizer = AutoTokenizer.from_pretrained(
model_name_or_path,
trust_remote_code=True,
# llama不支持fast
use_fast=False if config.model_type == 'llama' else True
)
model = AutoModelForCausalLM.from_pretrained(
model_name_or_path,
trust_remote_code=True,
low_cpu_mem_usage=True,
torch_dtype=torch.float16,
# device_map='auto',
device_map={'': 'cpu'}
)
model = PeftModel.from_pretrained(model, adapter_name_or_path, device_map={'': 'cpu'})
model = model.merge_and_unload()
tokenizer.save_pretrained(save_path)
model.save_pretrained(save_path)
if __name__ == '__main__':
merge_lora_to_base_model()