forked from deep-diver/LLM-As-Chatbot
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmodel.py
33 lines (28 loc) · 907 Bytes
/
model.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
import torch
from peft import PeftModel
from transformers import LlamaTokenizer, LlamaForCausalLM
def load_model(base, finetuned, multi_gpu):
tokenizer = LlamaTokenizer.from_pretrained(base)
tokenizer.pad_token_id = 0
tokenizer.padding_side = "left"
if not multi_gpu:
model = LlamaForCausalLM.from_pretrained(
base,
load_in_8bit=True,
device_map="auto",
)
model = PeftModel.from_pretrained(
model, finetuned, device_map={'': 0}
)
return model, tokenizer
else:
model = LlamaForCausalLM.from_pretrained(
base,
torch_dtype=torch.float16,
device_map="auto",
)
model = PeftModel.from_pretrained(
model, finetuned, torch_dtype=torch.float16
)
model.half()
return model, tokenizer