forked from meta-llama/llama-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_lora_weights.py
40 lines (31 loc) · 985 Bytes
/
merge_lora_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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# This software may be used and distributed according to the terms of the Llama 2 Community License Agreement.
import fire
import torch
from peft import PeftModel
from transformers import LlamaForCausalLM, LlamaTokenizer
def main(base_model: str,
peft_model: str,
output_dir: str):
model = LlamaForCausalLM.from_pretrained(
base_model,
load_in_8bit=False,
torch_dtype=torch.float16,
device_map="auto",
offload_folder="tmp",
)
tokenizer = LlamaTokenizer.from_pretrained(
base_model
)
model = PeftModel.from_pretrained(
model,
peft_model,
torch_dtype=torch.float16,
device_map="auto",
offload_folder="tmp",
)
model = model.merge_and_unload()
model.save_pretrained(output_dir)
tokenizer.save_pretrained(output_dir)
if __name__ == "__main__":
fire.Fire(main)