forked from FLock-io/testnet-training-node-quickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge.py
27 lines (24 loc) · 761 Bytes
/
merge.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
import json
import torch
from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer
def merge_lora_to_base_model(
model_name_or_path: str, adapter_name_or_path: str, save_path: str
):
tokenizer = AutoTokenizer.from_pretrained(
adapter_name_or_path,
use_fast=True,
)
model = AutoModelForCausalLM.from_pretrained(
model_name_or_path,
trust_remote_code=True,
low_cpu_mem_usage=True,
torch_dtype=torch.float16,
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)